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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 (32 << 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_FUJITSU
  237. #define RETURN_BY_STACK
  238. #endif
  239. #ifdef F_INTERFACE_FLANG
  240. #define RETURN_BY_STACK
  241. #endif
  242. #ifdef F_INTERFACE_PGI
  243. #define RETURN_BY_STACK
  244. #endif
  245. #ifdef F_INTERFACE_PATHSCALE
  246. #define RETURN_BY_PACKED
  247. #endif
  248. #ifdef F_INTERFACE_SUN
  249. #define RETURN_BY_PACKED
  250. #endif
  251. #ifdef ASSEMBLER
  252. #if defined(PILEDRIVER) || defined(BULLDOZER) || defined(STEAMROLLER) || defined(EXCAVATOR)
  253. //Enable some optimization for barcelona.
  254. #define BARCELONA_OPTIMIZATION
  255. #endif
  256. #if defined(HAVE_3DNOW)
  257. #define EMMS femms
  258. #elif defined(HAVE_MMX)
  259. #define EMMS emms
  260. #endif
  261. #ifndef EMMS
  262. #define EMMS
  263. #endif
  264. #define BRANCH .byte 0x3e
  265. #define NOBRANCH .byte 0x2e
  266. #define PADDING .byte 0x66
  267. #ifdef OS_WINDOWS
  268. #define ARG1 %rcx
  269. #define ARG2 %rdx
  270. #define ARG3 %r8
  271. #define ARG4 %r9
  272. #else
  273. #define ARG1 %rdi
  274. #define ARG2 %rsi
  275. #define ARG3 %rdx
  276. #define ARG4 %rcx
  277. #define ARG5 %r8
  278. #define ARG6 %r9
  279. #endif
  280. #ifndef COMPLEX
  281. #ifdef XDOUBLE
  282. #define LOCAL_BUFFER_SIZE QLOCAL_BUFFER_SIZE
  283. #elif defined DOUBLE
  284. #define LOCAL_BUFFER_SIZE DLOCAL_BUFFER_SIZE
  285. #else
  286. #define LOCAL_BUFFER_SIZE SLOCAL_BUFFER_SIZE
  287. #endif
  288. #else
  289. #ifdef XDOUBLE
  290. #define LOCAL_BUFFER_SIZE XLOCAL_BUFFER_SIZE
  291. #elif defined DOUBLE
  292. #define LOCAL_BUFFER_SIZE ZLOCAL_BUFFER_SIZE
  293. #else
  294. #define LOCAL_BUFFER_SIZE CLOCAL_BUFFER_SIZE
  295. #endif
  296. #endif
  297. #if defined(OS_WINDOWS)
  298. #if LOCAL_BUFFER_SIZE > 16384
  299. #define STACK_TOUCHING \
  300. movl $0, 4096 * 4(%rsp);\
  301. movl $0, 4096 * 3(%rsp);\
  302. movl $0, 4096 * 2(%rsp);\
  303. movl $0, 4096 * 1(%rsp);
  304. #elif LOCAL_BUFFER_SIZE > 12288
  305. #define STACK_TOUCHING \
  306. movl $0, 4096 * 3(%rsp);\
  307. movl $0, 4096 * 2(%rsp);\
  308. movl $0, 4096 * 1(%rsp);
  309. #elif LOCAL_BUFFER_SIZE > 8192
  310. #define STACK_TOUCHING \
  311. movl $0, 4096 * 2(%rsp);\
  312. movl $0, 4096 * 1(%rsp);
  313. #elif LOCAL_BUFFER_SIZE > 4096
  314. #define STACK_TOUCHING \
  315. movl $0, 4096 * 1(%rsp);
  316. #else
  317. #define STACK_TOUCHING
  318. #endif
  319. #else
  320. #define STACK_TOUCHING
  321. #endif
  322. #if defined(CORE2)
  323. #define movapd movaps
  324. #define andpd andps
  325. #define movlpd movlps
  326. #define movhpd movhps
  327. #endif
  328. #ifndef F_INTERFACE
  329. #define REALNAME ASMNAME
  330. #else
  331. #define REALNAME ASMFNAME
  332. #endif
  333. #ifdef OS_DARWIN
  334. #define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
  335. #define EPILOGUE .subsections_via_symbols
  336. #define PROFCODE
  337. #endif
  338. #ifdef OS_WINDOWS
  339. #define SAVEREGISTERS \
  340. subq $256, %rsp;\
  341. movups %xmm6, 0(%rsp);\
  342. movups %xmm7, 16(%rsp);\
  343. movups %xmm8, 32(%rsp);\
  344. movups %xmm9, 48(%rsp);\
  345. movups %xmm10, 64(%rsp);\
  346. movups %xmm11, 80(%rsp);\
  347. movups %xmm12, 96(%rsp);\
  348. movups %xmm13, 112(%rsp);\
  349. movups %xmm14, 128(%rsp);\
  350. movups %xmm15, 144(%rsp)
  351. #define RESTOREREGISTERS \
  352. movups 0(%rsp), %xmm6;\
  353. movups 16(%rsp), %xmm7;\
  354. movups 32(%rsp), %xmm8;\
  355. movups 48(%rsp), %xmm9;\
  356. movups 64(%rsp), %xmm10;\
  357. movups 80(%rsp), %xmm11;\
  358. movups 96(%rsp), %xmm12;\
  359. movups 112(%rsp), %xmm13;\
  360. movups 128(%rsp), %xmm14;\
  361. movups 144(%rsp), %xmm15;\
  362. addq $256, %rsp
  363. #else
  364. #define SAVEREGISTERS
  365. #define RESTOREREGISTERS
  366. #endif
  367. #if defined(OS_WINDOWS) && !defined(C_PGI)
  368. #define PROLOGUE \
  369. .text; \
  370. .align 16; \
  371. .globl REALNAME ;\
  372. .def REALNAME;.scl 2;.type 32;.endef; \
  373. REALNAME:
  374. #define PROFCODE
  375. #define EPILOGUE .end
  376. #endif
  377. #if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(__ELF__) || defined(C_PGI)
  378. #define PROLOGUE \
  379. .text; \
  380. .align 512; \
  381. .globl REALNAME ;\
  382. .type REALNAME, @function; \
  383. REALNAME: \
  384. _CET_ENDBR
  385. #ifdef PROFILE
  386. #define PROFCODE call *mcount@GOTPCREL(%rip)
  387. #else
  388. #define PROFCODE
  389. #endif
  390. #define EPILOGUE \
  391. .size REALNAME, .-REALNAME; \
  392. .section .note.GNU-stack,"",@progbits
  393. #endif
  394. #endif
  395. #ifdef XDOUBLE
  396. #define FLD fldt
  397. #define FST fstpt
  398. #define MOVQ movq
  399. #elif defined(DOUBLE)
  400. #define FLD fldl
  401. #define FST fstpl
  402. #define FSTU fstl
  403. #define FMUL fmull
  404. #define FADD faddl
  405. #define MOVSD movsd
  406. #define MULSD mulsd
  407. #define MULPD mulpd
  408. #define CMPEQPD cmpeqpd
  409. #define COMISD comisd
  410. #define PSRLQ psrlq
  411. #define ANDPD andpd
  412. #define ADDPD addpd
  413. #define ADDSD addsd
  414. #define SUBPD subpd
  415. #define SUBSD subsd
  416. #define MOVQ movq
  417. #define MOVUPD movupd
  418. #define XORPD xorpd
  419. #else
  420. #define FLD flds
  421. #define FST fstps
  422. #define FSTU fsts
  423. #define FMUL fmuls
  424. #define FADD fadds
  425. #define MOVSD movss
  426. #define MULSD mulss
  427. #define MULPD mulps
  428. #define CMPEQPD cmpeqps
  429. #define COMISD comiss
  430. #define PSRLQ psrld
  431. #define ANDPD andps
  432. #define ADDPD addps
  433. #define ADDSD addss
  434. #define SUBPD subps
  435. #define SUBSD subss
  436. #define MOVQ movd
  437. #define MOVUPD movups
  438. #define XORPD xorps
  439. #endif
  440. #define HALT hlt
  441. #ifdef OS_DARWIN
  442. #define ALIGN_2 .align 2
  443. #define ALIGN_3 .align 3
  444. #define ALIGN_4 .align 4
  445. #define ALIGN_5 .align 5
  446. #define ffreep fstp
  447. #endif
  448. #ifndef ALIGN_2
  449. #define ALIGN_2 .align 4
  450. #endif
  451. #ifndef ALIGN_3
  452. #define ALIGN_3 .align 8
  453. #endif
  454. #ifndef ALIGN_4
  455. #define ALIGN_4 .align 16
  456. #endif
  457. #ifndef ALIGN_5
  458. #define ALIGN_5 .align 32
  459. #endif
  460. #ifndef ALIGN_6
  461. #define ALIGN_6 .align 64
  462. #endif
  463. // ffreep %st(0).
  464. // Because Clang didn't support ffreep, we directly use the opcode.
  465. // Please check out http://www.sandpile.org/x86/opc_fpu.htm
  466. #ifndef ffreep
  467. #define ffreep .byte 0xdf, 0xc0 #
  468. #endif
  469. #endif