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

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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__("cpuid"
  113. : "=a" (*eax),
  114. "=b" (*ebx),
  115. "=c" (*ecx),
  116. "=d" (*edx)
  117. : "0" (op));
  118. #endif
  119. }
  120. /*
  121. #define WHEREAMI
  122. */
  123. static __inline int WhereAmI(void){
  124. int eax, ebx, ecx, edx;
  125. int apicid;
  126. cpuid(1, &eax, &ebx, &ecx, &edx);
  127. apicid = BITMASK(ebx, 24, 0xff);
  128. return apicid;
  129. }
  130. #ifdef CORE_BARCELONA
  131. #define IFLUSH gotoblas_iflush()
  132. #define IFLUSH_HALF gotoblas_iflush_half()
  133. #endif
  134. #ifdef ENABLE_SSE_EXCEPTION
  135. #define IDEBUG_START \
  136. { \
  137. unsigned int fp_sse_mode, new_fp_mode; \
  138. __asm__ __volatile__ ("stmxcsr %0" : "=m" (fp_sse_mode) : ); \
  139. new_fp_mode = fp_sse_mode & ~0xd00; \
  140. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (new_fp_mode) );
  141. #define IDEBUG_END \
  142. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (fp_sse_mode) ); \
  143. }
  144. #endif
  145. #ifdef XDOUBLE
  146. #define GET_IMAGE(res) __asm__ __volatile__("fstpt %0" : "=m"(res) : : "memory")
  147. #elif defined(DOUBLE)
  148. #define GET_IMAGE(res) __asm__ __volatile__("movsd %%xmm1, %0" : "=m"(res) : : "memory")
  149. #else
  150. #define GET_IMAGE(res) __asm__ __volatile__("movss %%xmm1, %0" : "=m"(res) : : "memory")
  151. #endif
  152. #define GET_IMAGE_CANCEL
  153. #ifdef SMP
  154. #if defined(USE64BITINT)
  155. static __inline blasint blas_quickdivide(blasint x, blasint y){
  156. return x / y;
  157. }
  158. #elif defined (C_MSVC)
  159. static __inline BLASLONG blas_quickdivide(BLASLONG x, BLASLONG y){
  160. return x / y;
  161. }
  162. #else
  163. extern unsigned int blas_quick_divide_table[];
  164. static __inline int blas_quickdivide(unsigned int x, unsigned int y){
  165. unsigned int result;
  166. if (y <= 1) return x;
  167. #if (MAX_CPU_NUMBER > 64)
  168. if (y > 64) {
  169. result = x / y;
  170. return result;
  171. }
  172. #endif
  173. y = blas_quick_divide_table[y];
  174. __asm__ __volatile__ ("mull %0" :"=d" (result) :"a"(x), "0" (y));
  175. return result;
  176. }
  177. #endif
  178. #endif
  179. #endif
  180. #ifndef PAGESIZE
  181. #define PAGESIZE ( 4 << 10)
  182. #endif
  183. #define HUGE_PAGESIZE ( 2 << 20)
  184. #define BUFFER_SIZE (32 << 20)
  185. #define SEEK_ADDRESS
  186. #ifdef F_INTERFACE_G77
  187. #define RETURN_BY_STACK
  188. #define NEED_F2CCONV
  189. #endif
  190. #ifdef F_INTERFACE_G95
  191. #define RETURN_BY_PACKED
  192. #endif
  193. #ifdef F_INTERFACE_GFORT
  194. #ifdef OS_WINDOWS
  195. #ifndef DOUBLE
  196. #define RETURN_BY_REGS
  197. #else
  198. #define RETURN_BY_STACK
  199. #endif
  200. #else
  201. #define RETURN_BY_PACKED
  202. #endif
  203. #endif
  204. #ifdef F_INTERFACE_INTEL
  205. #define RETURN_BY_STACK
  206. #endif
  207. #ifdef F_INTERFACE_FUJITSU
  208. #define RETURN_BY_STACK
  209. #endif
  210. #ifdef F_INTERFACE_FLANG
  211. #define RETURN_BY_STACK
  212. #endif
  213. #ifdef F_INTERFACE_PGI
  214. #define RETURN_BY_STACK
  215. #endif
  216. #ifdef F_INTERFACE_PATHSCALE
  217. #define RETURN_BY_PACKED
  218. #endif
  219. #ifdef F_INTERFACE_SUN
  220. #define RETURN_BY_PACKED
  221. #endif
  222. #ifdef ASSEMBLER
  223. #if defined(PILEDRIVER) || defined(BULLDOZER) || defined(STEAMROLLER) || defined(EXCAVATOR)
  224. //Enable some optimazation for barcelona.
  225. #define BARCELONA_OPTIMIZATION
  226. #endif
  227. #if defined(HAVE_3DNOW)
  228. #define EMMS femms
  229. #elif defined(HAVE_MMX)
  230. #define EMMS emms
  231. #endif
  232. #ifndef EMMS
  233. #define EMMS
  234. #endif
  235. #define BRANCH .byte 0x3e
  236. #define NOBRANCH .byte 0x2e
  237. #define PADDING .byte 0x66
  238. #ifdef OS_WINDOWS
  239. #define ARG1 %rcx
  240. #define ARG2 %rdx
  241. #define ARG3 %r8
  242. #define ARG4 %r9
  243. #else
  244. #define ARG1 %rdi
  245. #define ARG2 %rsi
  246. #define ARG3 %rdx
  247. #define ARG4 %rcx
  248. #define ARG5 %r8
  249. #define ARG6 %r9
  250. #endif
  251. #ifndef COMPLEX
  252. #ifdef XDOUBLE
  253. #define LOCAL_BUFFER_SIZE QLOCAL_BUFFER_SIZE
  254. #elif defined DOUBLE
  255. #define LOCAL_BUFFER_SIZE DLOCAL_BUFFER_SIZE
  256. #else
  257. #define LOCAL_BUFFER_SIZE SLOCAL_BUFFER_SIZE
  258. #endif
  259. #else
  260. #ifdef XDOUBLE
  261. #define LOCAL_BUFFER_SIZE XLOCAL_BUFFER_SIZE
  262. #elif defined DOUBLE
  263. #define LOCAL_BUFFER_SIZE ZLOCAL_BUFFER_SIZE
  264. #else
  265. #define LOCAL_BUFFER_SIZE CLOCAL_BUFFER_SIZE
  266. #endif
  267. #endif
  268. #if defined(OS_WINDOWS)
  269. #if LOCAL_BUFFER_SIZE > 16384
  270. #define STACK_TOUCHING \
  271. movl $0, 4096 * 4(%rsp);\
  272. movl $0, 4096 * 3(%rsp);\
  273. movl $0, 4096 * 2(%rsp);\
  274. movl $0, 4096 * 1(%rsp);
  275. #elif LOCAL_BUFFER_SIZE > 12288
  276. #define STACK_TOUCHING \
  277. movl $0, 4096 * 3(%rsp);\
  278. movl $0, 4096 * 2(%rsp);\
  279. movl $0, 4096 * 1(%rsp);
  280. #elif LOCAL_BUFFER_SIZE > 8192
  281. #define STACK_TOUCHING \
  282. movl $0, 4096 * 2(%rsp);\
  283. movl $0, 4096 * 1(%rsp);
  284. #elif LOCAL_BUFFER_SIZE > 4096
  285. #define STACK_TOUCHING \
  286. movl $0, 4096 * 1(%rsp);
  287. #else
  288. #define STACK_TOUCHING
  289. #endif
  290. #else
  291. #define STACK_TOUCHING
  292. #endif
  293. #if defined(CORE2)
  294. #define movapd movaps
  295. #define andpd andps
  296. #define movlpd movlps
  297. #define movhpd movhps
  298. #endif
  299. #ifndef F_INTERFACE
  300. #define REALNAME ASMNAME
  301. #else
  302. #define REALNAME ASMFNAME
  303. #endif
  304. #ifdef OS_DARWIN
  305. #define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
  306. #define EPILOGUE .subsections_via_symbols
  307. #define PROFCODE
  308. #endif
  309. #ifdef OS_WINDOWS
  310. #define SAVEREGISTERS \
  311. subq $256, %rsp;\
  312. movups %xmm6, 0(%rsp);\
  313. movups %xmm7, 16(%rsp);\
  314. movups %xmm8, 32(%rsp);\
  315. movups %xmm9, 48(%rsp);\
  316. movups %xmm10, 64(%rsp);\
  317. movups %xmm11, 80(%rsp);\
  318. movups %xmm12, 96(%rsp);\
  319. movups %xmm13, 112(%rsp);\
  320. movups %xmm14, 128(%rsp);\
  321. movups %xmm15, 144(%rsp)
  322. #define RESTOREREGISTERS \
  323. movups 0(%rsp), %xmm6;\
  324. movups 16(%rsp), %xmm7;\
  325. movups 32(%rsp), %xmm8;\
  326. movups 48(%rsp), %xmm9;\
  327. movups 64(%rsp), %xmm10;\
  328. movups 80(%rsp), %xmm11;\
  329. movups 96(%rsp), %xmm12;\
  330. movups 112(%rsp), %xmm13;\
  331. movups 128(%rsp), %xmm14;\
  332. movups 144(%rsp), %xmm15;\
  333. addq $256, %rsp
  334. #else
  335. #define SAVEREGISTERS
  336. #define RESTOREREGISTERS
  337. #endif
  338. #if defined(OS_WINDOWS) && !defined(C_PGI)
  339. #define PROLOGUE \
  340. .text; \
  341. .align 16; \
  342. .globl REALNAME ;\
  343. .def REALNAME;.scl 2;.type 32;.endef; \
  344. REALNAME:
  345. #define PROFCODE
  346. #define EPILOGUE .end
  347. #endif
  348. #if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(__ELF__) || defined(C_PGI)
  349. #define PROLOGUE \
  350. .text; \
  351. .align 512; \
  352. .globl REALNAME ;\
  353. .type REALNAME, @function; \
  354. REALNAME:
  355. #ifdef PROFILE
  356. #define PROFCODE call *mcount@GOTPCREL(%rip)
  357. #else
  358. #define PROFCODE
  359. #endif
  360. #define EPILOGUE \
  361. .size REALNAME, .-REALNAME; \
  362. .section .note.GNU-stack,"",@progbits
  363. #endif
  364. #endif
  365. #ifdef XDOUBLE
  366. #define FLD fldt
  367. #define FST fstpt
  368. #define MOVQ movq
  369. #elif defined(DOUBLE)
  370. #define FLD fldl
  371. #define FST fstpl
  372. #define FSTU fstl
  373. #define FMUL fmull
  374. #define FADD faddl
  375. #define MOVSD movsd
  376. #define MULSD mulsd
  377. #define MULPD mulpd
  378. #define CMPEQPD cmpeqpd
  379. #define COMISD comisd
  380. #define PSRLQ psrlq
  381. #define ANDPD andpd
  382. #define ADDPD addpd
  383. #define ADDSD addsd
  384. #define SUBPD subpd
  385. #define SUBSD subsd
  386. #define MOVQ movq
  387. #define MOVUPD movupd
  388. #define XORPD xorpd
  389. #else
  390. #define FLD flds
  391. #define FST fstps
  392. #define FSTU fsts
  393. #define FMUL fmuls
  394. #define FADD fadds
  395. #define MOVSD movss
  396. #define MULSD mulss
  397. #define MULPD mulps
  398. #define CMPEQPD cmpeqps
  399. #define COMISD comiss
  400. #define PSRLQ psrld
  401. #define ANDPD andps
  402. #define ADDPD addps
  403. #define ADDSD addss
  404. #define SUBPD subps
  405. #define SUBSD subss
  406. #define MOVQ movd
  407. #define MOVUPD movups
  408. #define XORPD xorps
  409. #endif
  410. #define HALT hlt
  411. #ifdef OS_DARWIN
  412. #define ALIGN_2 .align 2
  413. #define ALIGN_3 .align 3
  414. #define ALIGN_4 .align 4
  415. #define ALIGN_5 .align 5
  416. #define ffreep fstp
  417. #endif
  418. #ifndef ALIGN_2
  419. #define ALIGN_2 .align 4
  420. #endif
  421. #ifndef ALIGN_3
  422. #define ALIGN_3 .align 8
  423. #endif
  424. #ifndef ALIGN_4
  425. #define ALIGN_4 .align 16
  426. #endif
  427. #ifndef ALIGN_5
  428. #define ALIGN_5 .align 32
  429. #endif
  430. #ifndef ALIGN_6
  431. #define ALIGN_6 .align 64
  432. #endif
  433. // ffreep %st(0).
  434. // Because Clang didn't support ffreep, we directly use the opcode.
  435. // Please check out http://www.sandpile.org/x86/opc_fpu.htm
  436. #ifndef ffreep
  437. #define ffreep .byte 0xdf, 0xc0 #
  438. #endif
  439. #endif