1 /*********************************************************************/
2 /* Copyright 2009, 2010 The University of Texas at Austin. */
3 /* All rights reserved. */
5 /* Redistribution and use in source and binary forms, with or */
6 /* without modification, are permitted provided that the following */
7 /* conditions are met: */
9 /* 1. Redistributions of source code must retain the above */
10 /* copyright notice, this list of conditions and the following */
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. */
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. */
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 /*********************************************************************/
57 static void __inline blas_lock(volatile BLASULONG *address){
62 while (*address) {YIELDING;};
64 #if defined(_MSC_VER) && !defined(__clang__)
65 // use intrinsic instead of inline assembly
66 ret = _InterlockedExchange((volatile LONG *)address, 1);
77 : "=r"(ret), "=m"(*address)
78 : "0"(1), "m"(*address)
85 #define BLAS_LOCK_DEFINED
87 static __inline unsigned long long rpcc(void){
88 #if defined(_MSC_VER) && !defined(__clang__)
89 return __rdtsc(); // use MSVC intrinsic
93 __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
95 return ((unsigned long long)a + ((unsigned long long)d << 32));
100 static __inline unsigned long getstackaddr(void){
101 #if defined(_MSC_VER) && !defined(__clang__)
102 return (unsigned long)_ReturnAddress(); // use MSVC intrinsic
106 __asm__ __volatile__ ("mov %%esp, %0"
107 : "=r"(addr) : : "memory");
114 static __inline long double sqrt_long(long double val) {
115 #if defined(_MSC_VER) && !defined(__clang__)
116 return sqrt(val); // not sure if this will use fsqrt
120 __asm__ __volatile__ ("fldt %1\n"
122 "fstpt %0\n" : "=m" (result) : "m"(val));
127 #define SQRT(a) sqrt_long(a)
129 /* This is due to gcc's bug */
130 void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx);
134 static __inline int WhereAmI(void){
135 int eax, ebx, ecx, edx;
138 cpuid(1, &eax, &ebx, &ecx, &edx);
139 apicid = BITMASK(ebx, 24, 0xff);
144 #ifdef ENABLE_SSE_EXCEPTION
146 #define IDEBUG_START \
148 unsigned int fp_sse_mode, new_fp_mode; \
149 __asm__ __volatile__ ("stmxcsr %0" : "=m" (fp_sse_mode) : ); \
150 new_fp_mode = fp_sse_mode & ~0xd00; \
151 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (new_fp_mode) );
154 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (fp_sse_mode) ); \
160 #define GET_IMAGE(res) __asm__ __volatile__("fstpt %0" : "=m"(res) : : "memory")
161 #elif defined(DOUBLE)
162 #define GET_IMAGE(res) __asm__ __volatile__("fstpl %0" : "=m"(res) : : "memory")
164 #define GET_IMAGE(res) __asm__ __volatile__("fstps %0" : "=m"(res) : : "memory");
167 #define GET_IMAGE_CANCEL __asm__ __volatile__ ("ffree %st")
170 extern unsigned int blas_quick_divide_table[];
172 static __inline int blas_quickdivide(unsigned int x, unsigned int y){
176 if (y <= 1) return x;
178 #if defined(_MSC_VER) && !defined(__clang__)
182 #if (MAX_CPU_NUMBER > 64)
189 y = blas_quick_divide_table[y];
191 __asm__ __volatile__ ("mull %0" :"=d" (result), "+a"(x): "0" (y));
201 #define PAGESIZE ( 4 << 10)
203 #define HUGE_PAGESIZE ( 4 << 20)
205 #define BUFFER_SIZE (16 << 20)
209 #if defined(DOUBLE) || defined(XDOUBLE)
211 #define MMXSTORE movq
214 #define MMXSTORE movd
217 #if defined(PILEDRIVER) || defined(BULLDOZER) || defined(STEAMROLLER) || defined(EXCAVATOR)
218 //Enable some optimization for barcelona.
219 #define BARCELONA_OPTIMIZATION
222 #if defined(HAVE_3DNOW)
224 #elif defined(HAVE_MMX)
232 #if defined(CORE2) || defined(PENTIUM4)
233 #define movapd movaps
236 #define BRANCH .byte 0x3e
237 #define NOBRANCH .byte 0x2e
238 #define PADDING .byte 0x66;
243 #define LOCAL_BUFFER_SIZE QLOCAL_BUFFER_SIZE
245 #define LOCAL_BUFFER_SIZE DLOCAL_BUFFER_SIZE
247 #define LOCAL_BUFFER_SIZE SLOCAL_BUFFER_SIZE
251 #define LOCAL_BUFFER_SIZE XLOCAL_BUFFER_SIZE
253 #define LOCAL_BUFFER_SIZE ZLOCAL_BUFFER_SIZE
255 #define LOCAL_BUFFER_SIZE CLOCAL_BUFFER_SIZE
259 #if defined(OS_WINDOWS)
260 #if LOCAL_BUFFER_SIZE > 16384
261 #define STACK_TOUCHING \
262 movl $0, 4096 * 4(%esp);\
263 movl $0, 4096 * 3(%esp);\
264 movl $0, 4096 * 2(%esp);\
265 movl $0, 4096 * 1(%esp);
266 #elif LOCAL_BUFFER_SIZE > 12288
267 #define STACK_TOUCHING \
268 movl $0, 4096 * 3(%esp);\
269 movl $0, 4096 * 2(%esp);\
270 movl $0, 4096 * 1(%esp);
271 #elif LOCAL_BUFFER_SIZE > 8192
272 #define STACK_TOUCHING \
273 movl $0, 4096 * 2(%esp);\
274 movl $0, 4096 * 1(%esp);
275 #elif LOCAL_BUFFER_SIZE > 4096
276 #define STACK_TOUCHING \
277 movl $0, 4096 * 1(%esp);
279 #define STACK_TOUCHING
282 #define STACK_TOUCHING
286 #define REALNAME ASMNAME
288 #define REALNAME ASMFNAME
291 #if defined(F_INTERFACE_PATHSCALE) || defined(F_INTERFACE_OPEN64)
292 #define RETURN_BY_STRUCT
293 #elif defined(F_INTERFACE_GFORT) || defined(F_INTERFACE_G95)
294 #define RETURN_BY_COMPLEX
296 #define RETURN_BY_STACK
300 #define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
301 #define EPILOGUE .subsections_via_symbols
305 #if defined(OS_WINNT) || defined(OS_CYGWIN_NT) || defined(OS_INTERIX)
306 #define SAVEREGISTERS \
308 movups %xmm6, 0(%esp);\
309 movups %xmm7, 16(%esp)
311 #define RESTOREREGISTERS \
312 movups 0(%esp), %xmm6;\
313 movups 16(%esp), %xmm7;\
316 #define SAVEREGISTERS
317 #define RESTOREREGISTERS
320 #if defined(OS_WINNT) || defined(OS_CYGWIN_NT) || defined(OS_INTERIX)
325 .def REALNAME;.scl 2;.type 32;.endef; \
331 #define EPILOGUE .end
333 #define EPILOGUE .end REALNAME
337 #if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(__ELF__)
342 .type REALNAME, @function; \
347 #define PROFCODE call mcount
353 .size REALNAME, .-REALNAME; \
354 .section .note.GNU-stack,"",@progbits
366 #elif defined(DOUBLE)
390 #define ALIGN_2 .align 2
391 #define ALIGN_3 .align 3
392 #define ALIGN_4 .align 4
393 #define ALIGN_5 .align 5
398 #define ALIGN_2 .align 4
402 #define ALIGN_3 .align 8
406 #define ALIGN_4 .align 16
410 #define ALIGN_5 .align 32
414 #define ALIGN_6 .align 64
417 // Because Clang didn't support ffreep, we directly use the opcode.
418 // Please check out http://www.sandpile.org/x86/opc_fpu.htm
420 #define ffreep .byte 0xdf, 0xc0 #