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