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

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