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.2 kB

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