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

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