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 11 kB

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