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 9.1 kB

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