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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. #define MB
  45. #define WMB
  46. #ifdef C_SUN
  47. #define __asm__ __asm
  48. #define __volatile__
  49. #endif
  50. static void __inline blas_lock(volatile BLASULONG *address){
  51. int ret;
  52. do {
  53. while (*address) {YIELDING;};
  54. #if defined(_MSC_VER) && !defined(__clang__)
  55. // use intrinsic instead of inline assembly
  56. ret = _InterlockedExchange((volatile LONG *)address, 1);
  57. // inline assembly
  58. /*__asm {
  59. mov eax, address
  60. mov ebx, 1
  61. xchg [eax], ebx
  62. mov ret, ebx
  63. }*/
  64. #else
  65. __asm__ __volatile__(
  66. "xchgl %0, %1\n"
  67. : "=r"(ret), "=m"(*address)
  68. : "0"(1), "m"(*address)
  69. : "memory");
  70. #endif
  71. } while (ret);
  72. }
  73. #define BLAS_LOCK_DEFINED
  74. static __inline unsigned long long rpcc(void){
  75. #if defined(_MSC_VER) && !defined(__clang__)
  76. return __rdtsc(); // use MSVC intrinsic
  77. #else
  78. unsigned int a, d;
  79. __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
  80. return ((unsigned long long)a + ((unsigned long long)d << 32));
  81. #endif
  82. };
  83. #define RPCC_DEFINED
  84. static __inline unsigned long getstackaddr(void){
  85. #if defined(_MSC_VER) && !defined(__clang__)
  86. return (unsigned long)_ReturnAddress(); // use MSVC intrinsic
  87. #else
  88. unsigned long addr;
  89. __asm__ __volatile__ ("mov %%esp, %0"
  90. : "=r"(addr) : : "memory");
  91. return addr;
  92. #endif
  93. };
  94. static __inline long double sqrt_long(long double val) {
  95. #if defined(_MSC_VER) && !defined(__clang__)
  96. return sqrt(val); // not sure if this will use fsqrt
  97. #else
  98. long double result;
  99. __asm__ __volatile__ ("fldt %1\n"
  100. "fsqrt\n"
  101. "fstpt %0\n" : "=m" (result) : "m"(val));
  102. return result;
  103. #endif
  104. }
  105. #define SQRT(a) sqrt_long(a)
  106. /* This is due to gcc's bug */
  107. void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx);
  108. #define WHEREAMI
  109. static __inline int WhereAmI(void){
  110. int eax, ebx, ecx, edx;
  111. int apicid;
  112. cpuid(1, &eax, &ebx, &ecx, &edx);
  113. apicid = BITMASK(ebx, 24, 0xff);
  114. return apicid;
  115. }
  116. #ifdef ENABLE_SSE_EXCEPTION
  117. #define IDEBUG_START \
  118. { \
  119. unsigned int fp_sse_mode, new_fp_mode; \
  120. __asm__ __volatile__ ("stmxcsr %0" : "=m" (fp_sse_mode) : ); \
  121. new_fp_mode = fp_sse_mode & ~0xd00; \
  122. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (new_fp_mode) );
  123. #define IDEBUG_END \
  124. __asm__ __volatile__ ("ldmxcsr %0" : : "m" (fp_sse_mode) ); \
  125. }
  126. #endif
  127. #ifdef XDOUBLE
  128. #define GET_IMAGE(res) __asm__ __volatile__("fstpt %0" : "=m"(res) : : "memory")
  129. #elif defined(DOUBLE)
  130. #define GET_IMAGE(res) __asm__ __volatile__("fstpl %0" : "=m"(res) : : "memory")
  131. #else
  132. #define GET_IMAGE(res) __asm__ __volatile__("fstps %0" : "=m"(res) : : "memory");
  133. #endif
  134. #define GET_IMAGE_CANCEL __asm__ __volatile__ ("ffree %st")
  135. #ifdef SMP
  136. extern unsigned int blas_quick_divide_table[];
  137. static __inline int blas_quickdivide(unsigned int x, unsigned int y){
  138. unsigned int result;
  139. if (y <= 1) return x;
  140. #if defined(_MSC_VER) && !defined(__clang__)
  141. result = x/y;
  142. return result;
  143. #else
  144. #if (MAX_CPU_NUMBER > 64)
  145. if ( y > 64) {
  146. result = x/y;
  147. return result;
  148. }
  149. #endif
  150. y = blas_quick_divide_table[y];
  151. __asm__ __volatile__ ("mull %0" :"=d" (result), "+a"(x): "0" (y));
  152. return result;
  153. #endif
  154. }
  155. #endif
  156. #endif
  157. #ifndef PAGESIZE
  158. #define PAGESIZE ( 4 << 10)
  159. #endif
  160. #define HUGE_PAGESIZE ( 4 << 20)
  161. #define BUFFER_SIZE (16 << 20)
  162. #define SEEK_ADDRESS
  163. #if defined(DOUBLE) || defined(XDOUBLE)
  164. #define MMXLOAD movq
  165. #define MMXSTORE movq
  166. #else
  167. #define MMXLOAD movd
  168. #define MMXSTORE movd
  169. #endif
  170. #if defined(PILEDRIVER) || defined(BULLDOZER) || defined(STEAMROLLER) || defined(EXCAVATOR)
  171. //Enable some optimazation for barcelona.
  172. #define BARCELONA_OPTIMIZATION
  173. #endif
  174. #if defined(HAVE_3DNOW)
  175. #define EMMS femms
  176. #elif defined(HAVE_MMX)
  177. #define EMMS emms
  178. #endif
  179. #ifndef EMMS
  180. #define EMMS
  181. #endif
  182. #if defined(CORE2) || defined(PENTIUM4)
  183. #define movapd movaps
  184. #endif
  185. #define BRANCH .byte 0x3e
  186. #define NOBRANCH .byte 0x2e
  187. #define PADDING .byte 0x66;
  188. #define HALT hlt
  189. #ifndef COMPLEX
  190. #ifdef XDOUBLE
  191. #define LOCAL_BUFFER_SIZE QLOCAL_BUFFER_SIZE
  192. #elif defined DOUBLE
  193. #define LOCAL_BUFFER_SIZE DLOCAL_BUFFER_SIZE
  194. #else
  195. #define LOCAL_BUFFER_SIZE SLOCAL_BUFFER_SIZE
  196. #endif
  197. #else
  198. #ifdef XDOUBLE
  199. #define LOCAL_BUFFER_SIZE XLOCAL_BUFFER_SIZE
  200. #elif defined DOUBLE
  201. #define LOCAL_BUFFER_SIZE ZLOCAL_BUFFER_SIZE
  202. #else
  203. #define LOCAL_BUFFER_SIZE CLOCAL_BUFFER_SIZE
  204. #endif
  205. #endif
  206. #if defined(OS_WINDOWS)
  207. #if LOCAL_BUFFER_SIZE > 16384
  208. #define STACK_TOUCHING \
  209. movl $0, 4096 * 4(%esp);\
  210. movl $0, 4096 * 3(%esp);\
  211. movl $0, 4096 * 2(%esp);\
  212. movl $0, 4096 * 1(%esp);
  213. #elif LOCAL_BUFFER_SIZE > 12288
  214. #define STACK_TOUCHING \
  215. movl $0, 4096 * 3(%esp);\
  216. movl $0, 4096 * 2(%esp);\
  217. movl $0, 4096 * 1(%esp);
  218. #elif LOCAL_BUFFER_SIZE > 8192
  219. #define STACK_TOUCHING \
  220. movl $0, 4096 * 2(%esp);\
  221. movl $0, 4096 * 1(%esp);
  222. #elif LOCAL_BUFFER_SIZE > 4096
  223. #define STACK_TOUCHING \
  224. movl $0, 4096 * 1(%esp);
  225. #else
  226. #define STACK_TOUCHING
  227. #endif
  228. #else
  229. #define STACK_TOUCHING
  230. #endif
  231. #ifndef F_INTERFACE
  232. #define REALNAME ASMNAME
  233. #else
  234. #define REALNAME ASMFNAME
  235. #endif
  236. #if defined(F_INTERFACE_PATHSCALE) || defined(F_INTERFACE_OPEN64)
  237. #define RETURN_BY_STRUCT
  238. #elif defined(F_INTERFACE_GFORT) || defined(F_INTERFACE_G95)
  239. #define RETURN_BY_COMPLEX
  240. #else
  241. #define RETURN_BY_STACK
  242. #endif
  243. #ifdef OS_DARWIN
  244. #define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
  245. #define EPILOGUE .subsections_via_symbols
  246. #define PROFCODE
  247. #endif
  248. #if defined(OS_WINNT) || defined(OS_CYGWIN_NT) || defined(OS_INTERIX)
  249. #define SAVEREGISTERS \
  250. subl $32, %esp;\
  251. movups %xmm6, 0(%esp);\
  252. movups %xmm7, 16(%esp)
  253. #define RESTOREREGISTERS \
  254. movups 0(%esp), %xmm6;\
  255. movups 16(%esp), %xmm7;\
  256. addl $32, %esp
  257. #else
  258. #define SAVEREGISTERS
  259. #define RESTOREREGISTERS
  260. #endif
  261. #if defined(OS_WINNT) || defined(OS_CYGWIN_NT) || defined(OS_INTERIX)
  262. #define PROLOGUE \
  263. .text; \
  264. .align 16; \
  265. .globl REALNAME ;\
  266. .def REALNAME;.scl 2;.type 32;.endef; \
  267. REALNAME:
  268. #define PROFCODE
  269. #ifdef __clang__
  270. #define EPILOGUE .end
  271. #else
  272. #define EPILOGUE .end REALNAME
  273. #endif
  274. #endif
  275. #if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(__ELF__)
  276. #define PROLOGUE \
  277. .text; \
  278. .align 16; \
  279. .globl REALNAME ;\
  280. .type REALNAME, @function; \
  281. REALNAME:
  282. #ifdef PROFILE
  283. #define PROFCODE call mcount
  284. #else
  285. #define PROFCODE
  286. #endif
  287. #define EPILOGUE \
  288. .size REALNAME, .-REALNAME; \
  289. .section .note.GNU-stack,"",@progbits
  290. #endif
  291. #ifdef XDOUBLE
  292. #define FLD fldt
  293. #define FST fstpt
  294. #define FSTU fstt
  295. #define FMUL fmult
  296. #define FADD faddt
  297. #define FSUB fsubt
  298. #define FSUBR fsubrt
  299. #elif defined(DOUBLE)
  300. #define FLD fldl
  301. #define FST fstpl
  302. #define FSTU fstl
  303. #define FMUL fmull
  304. #define FADD faddl
  305. #define FSUB fsubl
  306. #define FSUBR fsubrl
  307. #else
  308. #define FLD flds
  309. #define FST fstps
  310. #define FSTU fsts
  311. #define FMUL fmuls
  312. #define FADD fadds
  313. #define FSUB fsubs
  314. #define FSUBR fsubrs
  315. #endif
  316. #endif
  317. #ifdef C_SUN
  318. #define ffreep fstp
  319. #endif
  320. #ifdef __APPLE__
  321. #define ALIGN_2 .align 2
  322. #define ALIGN_3 .align 3
  323. #define ALIGN_4 .align 4
  324. #define ALIGN_5 .align 5
  325. #define ffreep fstp
  326. #endif
  327. #ifndef ALIGN_2
  328. #define ALIGN_2 .align 4
  329. #endif
  330. #ifndef ALIGN_3
  331. #define ALIGN_3 .align 8
  332. #endif
  333. #ifndef ALIGN_4
  334. #define ALIGN_4 .align 16
  335. #endif
  336. #ifndef ALIGN_5
  337. #define ALIGN_5 .align 32
  338. #endif
  339. #ifndef ALIGN_6
  340. #define ALIGN_6 .align 64
  341. #endif
  342. // ffreep %st(0).
  343. // Because Clang didn't support ffreep, we directly use the opcode.
  344. // Please check out http://www.sandpile.org/x86/opc_fpu.htm
  345. #ifndef ffreep
  346. #define ffreep .byte 0xdf, 0xc0 #
  347. #endif