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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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(HAVE_3DNOW)
  172. #define EMMS femms
  173. #elif defined(HAVE_MMX)
  174. #define EMMS emms
  175. #endif
  176. #ifndef EMMS
  177. #define EMMS
  178. #endif
  179. #define BRANCH .byte 0x3e
  180. #define NOBRANCH .byte 0x2e
  181. #define PADDING .byte 0x66
  182. #ifdef OS_WINDOWS
  183. #define ARG1 %rcx
  184. #define ARG2 %rdx
  185. #define ARG3 %r8
  186. #define ARG4 %r9
  187. #else
  188. #define ARG1 %rdi
  189. #define ARG2 %rsi
  190. #define ARG3 %rdx
  191. #define ARG4 %rcx
  192. #define ARG5 %r8
  193. #define ARG6 %r9
  194. #endif
  195. #ifndef COMPLEX
  196. #ifdef XDOUBLE
  197. #define LOCAL_BUFFER_SIZE QLOCAL_BUFFER_SIZE
  198. #elif defined DOUBLE
  199. #define LOCAL_BUFFER_SIZE DLOCAL_BUFFER_SIZE
  200. #else
  201. #define LOCAL_BUFFER_SIZE SLOCAL_BUFFER_SIZE
  202. #endif
  203. #else
  204. #ifdef XDOUBLE
  205. #define LOCAL_BUFFER_SIZE XLOCAL_BUFFER_SIZE
  206. #elif defined DOUBLE
  207. #define LOCAL_BUFFER_SIZE ZLOCAL_BUFFER_SIZE
  208. #else
  209. #define LOCAL_BUFFER_SIZE CLOCAL_BUFFER_SIZE
  210. #endif
  211. #endif
  212. #if defined(OS_WINDOWS)
  213. #if LOCAL_BUFFER_SIZE > 16384
  214. #define STACK_TOUCHING \
  215. movl $0, 4096 * 4(%rsp);\
  216. movl $0, 4096 * 3(%rsp);\
  217. movl $0, 4096 * 2(%rsp);\
  218. movl $0, 4096 * 1(%rsp);
  219. #elif LOCAL_BUFFER_SIZE > 12288
  220. #define STACK_TOUCHING \
  221. movl $0, 4096 * 3(%rsp);\
  222. movl $0, 4096 * 2(%rsp);\
  223. movl $0, 4096 * 1(%rsp);
  224. #elif LOCAL_BUFFER_SIZE > 8192
  225. #define STACK_TOUCHING \
  226. movl $0, 4096 * 2(%rsp);\
  227. movl $0, 4096 * 1(%rsp);
  228. #elif LOCAL_BUFFER_SIZE > 4096
  229. #define STACK_TOUCHING \
  230. movl $0, 4096 * 1(%rsp);
  231. #else
  232. #define STACK_TOUCHING
  233. #endif
  234. #else
  235. #define STACK_TOUCHING
  236. #endif
  237. #if defined(CORE2)
  238. #define movapd movaps
  239. #define andpd andps
  240. #define movlpd movlps
  241. #define movhpd movhps
  242. #endif
  243. #ifndef F_INTERFACE
  244. #define REALNAME ASMNAME
  245. #else
  246. #define REALNAME ASMFNAME
  247. #endif
  248. #ifdef OS_DARWIN
  249. #define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
  250. #define EPILOGUE .subsections_via_symbols
  251. #define PROFCODE
  252. #endif
  253. #ifdef OS_WINDOWS
  254. #define SAVEREGISTERS \
  255. subq $256, %rsp;\
  256. movups %xmm6, 0(%rsp);\
  257. movups %xmm7, 16(%rsp);\
  258. movups %xmm8, 32(%rsp);\
  259. movups %xmm9, 48(%rsp);\
  260. movups %xmm10, 64(%rsp);\
  261. movups %xmm11, 80(%rsp);\
  262. movups %xmm12, 96(%rsp);\
  263. movups %xmm13, 112(%rsp);\
  264. movups %xmm14, 128(%rsp);\
  265. movups %xmm15, 144(%rsp)
  266. #define RESTOREREGISTERS \
  267. movups 0(%rsp), %xmm6;\
  268. movups 16(%rsp), %xmm7;\
  269. movups 32(%rsp), %xmm8;\
  270. movups 48(%rsp), %xmm9;\
  271. movups 64(%rsp), %xmm10;\
  272. movups 80(%rsp), %xmm11;\
  273. movups 96(%rsp), %xmm12;\
  274. movups 112(%rsp), %xmm13;\
  275. movups 128(%rsp), %xmm14;\
  276. movups 144(%rsp), %xmm15;\
  277. addq $256, %rsp
  278. #else
  279. #define SAVEREGISTERS
  280. #define RESTOREREGISTERS
  281. #endif
  282. #if defined(OS_WINDOWS) && !defined(C_PGI)
  283. #define PROLOGUE \
  284. .text; \
  285. .align 16; \
  286. .globl REALNAME ;\
  287. .def REALNAME;.scl 2;.type 32;.endef; \
  288. REALNAME:
  289. #define PROFCODE
  290. #define EPILOGUE .end REALNAME
  291. #endif
  292. #if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(__ELF__) || defined(C_PGI)
  293. #define PROLOGUE \
  294. .text; \
  295. .align 512; \
  296. .globl REALNAME ;\
  297. .type REALNAME, @function; \
  298. REALNAME:
  299. #ifdef PROFILE
  300. #define PROFCODE call *mcount@GOTPCREL(%rip)
  301. #else
  302. #define PROFCODE
  303. #endif
  304. #define EPILOGUE .size REALNAME, .-REALNAME
  305. #endif
  306. #endif
  307. #ifdef XDOUBLE
  308. #define FLD fldt
  309. #define FST fstpt
  310. #define MOVQ movq
  311. #elif defined(DOUBLE)
  312. #define FLD fldl
  313. #define FST fstpl
  314. #define FSTU fstl
  315. #define FMUL fmull
  316. #define FADD faddl
  317. #define MOVSD movsd
  318. #define MULSD mulsd
  319. #define MULPD mulpd
  320. #define CMPEQPD cmpeqpd
  321. #define COMISD comisd
  322. #define PSRLQ psrlq
  323. #define ANDPD andpd
  324. #define ADDPD addpd
  325. #define ADDSD addsd
  326. #define SUBPD subpd
  327. #define SUBSD subsd
  328. #define MOVQ movq
  329. #define MOVUPD movupd
  330. #define XORPD xorpd
  331. #else
  332. #define FLD flds
  333. #define FST fstps
  334. #define FSTU fsts
  335. #define FMUL fmuls
  336. #define FADD fadds
  337. #define MOVSD movss
  338. #define MULSD mulss
  339. #define MULPD mulps
  340. #define CMPEQPD cmpeqps
  341. #define COMISD comiss
  342. #define PSRLQ psrld
  343. #define ANDPD andps
  344. #define ADDPD addps
  345. #define ADDSD addss
  346. #define SUBPD subps
  347. #define SUBSD subss
  348. #define MOVQ movd
  349. #define MOVUPD movups
  350. #define XORPD xorps
  351. #endif
  352. #define HALT hlt
  353. #ifdef OS_DARWIN
  354. #define ALIGN_2 .align 2
  355. #define ALIGN_3 .align 3
  356. #define ALIGN_4 .align 4
  357. #define ALIGN_5 .align 5
  358. #define ffreep fstp
  359. #endif
  360. #ifndef ALIGN_2
  361. #define ALIGN_2 .align 4
  362. #endif
  363. #ifndef ALIGN_3
  364. #define ALIGN_3 .align 8
  365. #endif
  366. #ifndef ALIGN_4
  367. #define ALIGN_4 .align 16
  368. #endif
  369. #ifndef ALIGN_5
  370. #define ALIGN_5 .align 32
  371. #endif
  372. #ifndef ALIGN_6
  373. #define ALIGN_6 .align 64
  374. #endif
  375. // ffreep %st(0).
  376. // Because Clang didn't support ffreep, we directly use the opcode.
  377. // Please check out http://www.sandpile.org/x86/opc_fpu.htm
  378. #ifndef ffreep
  379. #define ffreep .byte 0xdf, 0xc0 #
  380. #endif
  381. #endif