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

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