You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common_x86_64.h 12 kB

6 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #ifndef COMMON_X86
  39. #define COMMON_X86
  40. #ifndef ASSEMBLER
  41. #ifdef C_MSVC
  42. #include <intrin.h>
  43. #endif
  44. #ifdef C_SUN
  45. #define __asm__ __asm
  46. #define __volatile__
  47. #endif
  48. /*
  49. #ifdef HAVE_SSE2
  50. #define MB __asm__ __volatile__ ("mfence");
  51. #define WMB __asm__ __volatile__ ("sfence");
  52. #else
  53. #define MB
  54. #define WMB
  55. #endif
  56. */
  57. #ifdef __GNUC__
  58. #define MB do { __asm__ __volatile__("": : :"memory"); } while (0)
  59. #define WMB do { __asm__ __volatile__("": : :"memory"); } while (0)
  60. #else
  61. #define MB do {} while (0)
  62. #define WMB do {} while (0)
  63. #endif
  64. static void __inline blas_lock(volatile BLASULONG *address){
  65. #ifndef C_MSVC
  66. int ret;
  67. #else
  68. BLASULONG ret;
  69. #endif
  70. do {
  71. while (*address) {YIELDING;};
  72. #ifndef C_MSVC
  73. __asm__ __volatile__(
  74. "xchgl %0, %1\n"
  75. : "=r"(ret), "=m"(*address)
  76. : "0"(1), "m"(*address)
  77. : "memory");
  78. #else
  79. ret=InterlockedExchange64((volatile LONG64 *)(address), 1);
  80. #endif
  81. } while (ret);
  82. }
  83. #define BLAS_LOCK_DEFINED
  84. static __inline BLASULONG rpcc(void){
  85. #ifdef C_MSVC
  86. return __rdtsc();
  87. #else
  88. BLASULONG a, d;
  89. __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
  90. return ((BLASULONG)a + ((BLASULONG)d << 32));
  91. #endif
  92. }
  93. #define RPCC_DEFINED
  94. #define RPCC64BIT
  95. #ifndef C_MSVC
  96. static __inline BLASULONG getstackaddr(void){
  97. BLASULONG addr;
  98. __asm__ __volatile__ ("movq %%rsp, %0"
  99. : "=r"(addr) : : "memory");
  100. return addr;
  101. }
  102. #endif
  103. static __inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
  104. #ifdef C_MSVC
  105. int cpuinfo[4];
  106. __cpuid(cpuinfo, op);
  107. *eax=cpuinfo[0];
  108. *ebx=cpuinfo[1];
  109. *ecx=cpuinfo[2];
  110. *edx=cpuinfo[3];
  111. #else
  112. __asm__ __volatile__("mov $0, %%ecx;"
  113. "cpuid"
  114. : "=a" (*eax),
  115. "=b" (*ebx),
  116. "=c" (*ecx),
  117. "=d" (*edx)
  118. : "0" (op));
  119. #endif
  120. }
  121. /*
  122. #define WHEREAMI
  123. */
  124. static __inline int WhereAmI(void){
  125. int eax, ebx, ecx, edx;
  126. int apicid;
  127. cpuid(1, &eax, &ebx, &ecx, &edx);
  128. apicid = BITMASK(ebx, 24, 0xff);
  129. return apicid;
  130. }
  131. #ifdef CORE_BARCELONA
  132. #define IFLUSH gotoblas_iflush()
  133. #define IFLUSH_HALF gotoblas_iflush_half()
  134. #endif
  135. #ifdef ENABLE_SSE_EXCEPTION
  136. #define IDEBUG_START \
  137. { \
  138. unsigned int fp_sse_mode, new_fp_mode; \
  139. __asm__ __volatile__ ("stmxcsr %0" : "=m" (fp_sse_mode) : ); \
  140. new_fp_mode = fp_sse_mode & ~0xd00; \
  141. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (new_fp_mode) );
  142. #define IDEBUG_END \
  143. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (fp_sse_mode) ); \
  144. }
  145. #endif
  146. #ifdef XDOUBLE
  147. #define GET_IMAGE(res) __asm__ __volatile__("fstpt %0" : "=m"(res) : : "memory")
  148. #elif defined(DOUBLE)
  149. #define GET_IMAGE(res) __asm__ __volatile__("movsd %%xmm1, %0" : "=m"(res) : : "memory")
  150. #else
  151. #define GET_IMAGE(res) __asm__ __volatile__("movss %%xmm1, %0" : "=m"(res) : : "memory")
  152. #endif
  153. #define GET_IMAGE_CANCEL
  154. #ifdef SMP
  155. #if defined(USE64BITINT)
  156. static __inline blasint blas_quickdivide(blasint x, blasint y){
  157. return x / y;
  158. }
  159. #elif defined (C_MSVC)
  160. static __inline BLASLONG blas_quickdivide(BLASLONG x, BLASLONG y){
  161. return x / y;
  162. }
  163. #else
  164. extern unsigned int blas_quick_divide_table[];
  165. static __inline int blas_quickdivide(unsigned int x, unsigned int y){
  166. unsigned int result;
  167. if (y <= 1) return x;
  168. #if (MAX_CPU_NUMBER > 64)
  169. if (y > 64) {
  170. result = x / y;
  171. return result;
  172. }
  173. #endif
  174. y = blas_quick_divide_table[y];
  175. __asm__ __volatile__ ("mull %0" :"=d" (result), "+a"(x) : "0" (y));
  176. return result;
  177. }
  178. #endif
  179. #endif
  180. #endif
  181. #ifndef PAGESIZE
  182. #define PAGESIZE ( 4 << 10)
  183. #endif
  184. #define HUGE_PAGESIZE ( 2 << 20)
  185. #define BUFFER_SIZE (32 << 20)
  186. #define SEEK_ADDRESS
  187. #ifdef F_INTERFACE_G77
  188. #define RETURN_BY_STACK
  189. #define NEED_F2CCONV
  190. #endif
  191. #ifdef F_INTERFACE_G95
  192. #define RETURN_BY_PACKED
  193. #endif
  194. #ifdef F_INTERFACE_GFORT
  195. #ifdef OS_WINDOWS
  196. #ifndef DOUBLE
  197. #define RETURN_BY_REGS
  198. #else
  199. #define RETURN_BY_STACK
  200. #endif
  201. #else
  202. #define RETURN_BY_PACKED
  203. #endif
  204. #endif
  205. #ifdef F_INTERFACE_INTEL
  206. #define RETURN_BY_STACK
  207. #endif
  208. #ifdef F_INTERFACE_FUJITSU
  209. #define RETURN_BY_STACK
  210. #endif
  211. #ifdef F_INTERFACE_FLANG
  212. #define RETURN_BY_STACK
  213. #endif
  214. #ifdef F_INTERFACE_PGI
  215. #define RETURN_BY_STACK
  216. #endif
  217. #ifdef F_INTERFACE_PATHSCALE
  218. #define RETURN_BY_PACKED
  219. #endif
  220. #ifdef F_INTERFACE_SUN
  221. #define RETURN_BY_PACKED
  222. #endif
  223. #ifdef ASSEMBLER
  224. #if defined(PILEDRIVER) || defined(BULLDOZER) || defined(STEAMROLLER) || defined(EXCAVATOR)
  225. //Enable some optimization for barcelona.
  226. #define BARCELONA_OPTIMIZATION
  227. #endif
  228. #if defined(HAVE_3DNOW)
  229. #define EMMS femms
  230. #elif defined(HAVE_MMX)
  231. #define EMMS emms
  232. #endif
  233. #ifndef EMMS
  234. #define EMMS
  235. #endif
  236. #define BRANCH .byte 0x3e
  237. #define NOBRANCH .byte 0x2e
  238. #define PADDING .byte 0x66
  239. #ifdef OS_WINDOWS
  240. #define ARG1 %rcx
  241. #define ARG2 %rdx
  242. #define ARG3 %r8
  243. #define ARG4 %r9
  244. #else
  245. #define ARG1 %rdi
  246. #define ARG2 %rsi
  247. #define ARG3 %rdx
  248. #define ARG4 %rcx
  249. #define ARG5 %r8
  250. #define ARG6 %r9
  251. #endif
  252. #ifndef COMPLEX
  253. #ifdef XDOUBLE
  254. #define LOCAL_BUFFER_SIZE QLOCAL_BUFFER_SIZE
  255. #elif defined DOUBLE
  256. #define LOCAL_BUFFER_SIZE DLOCAL_BUFFER_SIZE
  257. #else
  258. #define LOCAL_BUFFER_SIZE SLOCAL_BUFFER_SIZE
  259. #endif
  260. #else
  261. #ifdef XDOUBLE
  262. #define LOCAL_BUFFER_SIZE XLOCAL_BUFFER_SIZE
  263. #elif defined DOUBLE
  264. #define LOCAL_BUFFER_SIZE ZLOCAL_BUFFER_SIZE
  265. #else
  266. #define LOCAL_BUFFER_SIZE CLOCAL_BUFFER_SIZE
  267. #endif
  268. #endif
  269. #if defined(OS_WINDOWS)
  270. #if LOCAL_BUFFER_SIZE > 16384
  271. #define STACK_TOUCHING \
  272. movl $0, 4096 * 4(%rsp);\
  273. movl $0, 4096 * 3(%rsp);\
  274. movl $0, 4096 * 2(%rsp);\
  275. movl $0, 4096 * 1(%rsp);
  276. #elif LOCAL_BUFFER_SIZE > 12288
  277. #define STACK_TOUCHING \
  278. movl $0, 4096 * 3(%rsp);\
  279. movl $0, 4096 * 2(%rsp);\
  280. movl $0, 4096 * 1(%rsp);
  281. #elif LOCAL_BUFFER_SIZE > 8192
  282. #define STACK_TOUCHING \
  283. movl $0, 4096 * 2(%rsp);\
  284. movl $0, 4096 * 1(%rsp);
  285. #elif LOCAL_BUFFER_SIZE > 4096
  286. #define STACK_TOUCHING \
  287. movl $0, 4096 * 1(%rsp);
  288. #else
  289. #define STACK_TOUCHING
  290. #endif
  291. #else
  292. #define STACK_TOUCHING
  293. #endif
  294. #if defined(CORE2)
  295. #define movapd movaps
  296. #define andpd andps
  297. #define movlpd movlps
  298. #define movhpd movhps
  299. #endif
  300. #ifndef F_INTERFACE
  301. #define REALNAME ASMNAME
  302. #else
  303. #define REALNAME ASMFNAME
  304. #endif
  305. #ifdef OS_DARWIN
  306. #define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
  307. #define EPILOGUE .subsections_via_symbols
  308. #define PROFCODE
  309. #endif
  310. #ifdef OS_WINDOWS
  311. #define SAVEREGISTERS \
  312. subq $256, %rsp;\
  313. movups %xmm6, 0(%rsp);\
  314. movups %xmm7, 16(%rsp);\
  315. movups %xmm8, 32(%rsp);\
  316. movups %xmm9, 48(%rsp);\
  317. movups %xmm10, 64(%rsp);\
  318. movups %xmm11, 80(%rsp);\
  319. movups %xmm12, 96(%rsp);\
  320. movups %xmm13, 112(%rsp);\
  321. movups %xmm14, 128(%rsp);\
  322. movups %xmm15, 144(%rsp)
  323. #define RESTOREREGISTERS \
  324. movups 0(%rsp), %xmm6;\
  325. movups 16(%rsp), %xmm7;\
  326. movups 32(%rsp), %xmm8;\
  327. movups 48(%rsp), %xmm9;\
  328. movups 64(%rsp), %xmm10;\
  329. movups 80(%rsp), %xmm11;\
  330. movups 96(%rsp), %xmm12;\
  331. movups 112(%rsp), %xmm13;\
  332. movups 128(%rsp), %xmm14;\
  333. movups 144(%rsp), %xmm15;\
  334. addq $256, %rsp
  335. #else
  336. #define SAVEREGISTERS
  337. #define RESTOREREGISTERS
  338. #endif
  339. #if defined(OS_WINDOWS) && !defined(C_PGI)
  340. #define PROLOGUE \
  341. .text; \
  342. .align 16; \
  343. .globl REALNAME ;\
  344. .def REALNAME;.scl 2;.type 32;.endef; \
  345. REALNAME:
  346. #define PROFCODE
  347. #define EPILOGUE .end
  348. #endif
  349. #if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(__ELF__) || defined(C_PGI)
  350. #define PROLOGUE \
  351. .text; \
  352. .align 512; \
  353. .globl REALNAME ;\
  354. .type REALNAME, @function; \
  355. REALNAME:
  356. #ifdef PROFILE
  357. #define PROFCODE call *mcount@GOTPCREL(%rip)
  358. #else
  359. #define PROFCODE
  360. #endif
  361. #define EPILOGUE \
  362. .size REALNAME, .-REALNAME; \
  363. .section .note.GNU-stack,"",@progbits
  364. #endif
  365. #endif
  366. #ifdef XDOUBLE
  367. #define FLD fldt
  368. #define FST fstpt
  369. #define MOVQ movq
  370. #elif defined(DOUBLE)
  371. #define FLD fldl
  372. #define FST fstpl
  373. #define FSTU fstl
  374. #define FMUL fmull
  375. #define FADD faddl
  376. #define MOVSD movsd
  377. #define MULSD mulsd
  378. #define MULPD mulpd
  379. #define CMPEQPD cmpeqpd
  380. #define COMISD comisd
  381. #define PSRLQ psrlq
  382. #define ANDPD andpd
  383. #define ADDPD addpd
  384. #define ADDSD addsd
  385. #define SUBPD subpd
  386. #define SUBSD subsd
  387. #define MOVQ movq
  388. #define MOVUPD movupd
  389. #define XORPD xorpd
  390. #else
  391. #define FLD flds
  392. #define FST fstps
  393. #define FSTU fsts
  394. #define FMUL fmuls
  395. #define FADD fadds
  396. #define MOVSD movss
  397. #define MULSD mulss
  398. #define MULPD mulps
  399. #define CMPEQPD cmpeqps
  400. #define COMISD comiss
  401. #define PSRLQ psrld
  402. #define ANDPD andps
  403. #define ADDPD addps
  404. #define ADDSD addss
  405. #define SUBPD subps
  406. #define SUBSD subss
  407. #define MOVQ movd
  408. #define MOVUPD movups
  409. #define XORPD xorps
  410. #endif
  411. #define HALT hlt
  412. #ifdef OS_DARWIN
  413. #define ALIGN_2 .align 2
  414. #define ALIGN_3 .align 3
  415. #define ALIGN_4 .align 4
  416. #define ALIGN_5 .align 5
  417. #define ffreep fstp
  418. #endif
  419. #ifndef ALIGN_2
  420. #define ALIGN_2 .align 4
  421. #endif
  422. #ifndef ALIGN_3
  423. #define ALIGN_3 .align 8
  424. #endif
  425. #ifndef ALIGN_4
  426. #define ALIGN_4 .align 16
  427. #endif
  428. #ifndef ALIGN_5
  429. #define ALIGN_5 .align 32
  430. #endif
  431. #ifndef ALIGN_6
  432. #define ALIGN_6 .align 64
  433. #endif
  434. // ffreep %st(0).
  435. // Because Clang didn't support ffreep, we directly use the opcode.
  436. // Please check out http://www.sandpile.org/x86/opc_fpu.htm
  437. #ifndef ffreep
  438. #define ffreep .byte 0xdf, 0xc0 #
  439. #endif
  440. #endif