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 /*********************************************************************/
55 #define MB __asm__ __volatile__ ("mfence");
56 #define WMB __asm__ __volatile__ ("sfence");
64 #define MB do { __asm__ __volatile__("": : :"memory"); } while (0)
65 #define WMB do { __asm__ __volatile__("": : :"memory"); } while (0)
67 #define MB do {} while (0)
68 #define WMB do {} while (0)
71 static void __inline blas_lock(volatile BLASULONG *address){
80 while (*address) {YIELDING;};
85 : "=r"(ret), "=m"(*address)
86 : "0"(1), "m"(*address)
89 ret=InterlockedExchange64((volatile LONG64 *)(address), 1);
94 #define BLAS_LOCK_DEFINED
96 static __inline BLASULONG rpcc(void){
102 __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
104 return ((BLASULONG)a + ((BLASULONG)d << 32));
112 static __inline BLASULONG getstackaddr(void){
115 __asm__ __volatile__ ("movq %%rsp, %0"
116 : "=r"(addr) : : "memory");
122 static __inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
126 __cpuid(cpuinfo, op);
132 __asm__ __volatile__("mov $0, %%ecx;"
146 static __inline int WhereAmI(void){
147 int eax, ebx, ecx, edx;
150 cpuid(1, &eax, &ebx, &ecx, &edx);
151 apicid = BITMASK(ebx, 24, 0xff);
157 #ifdef CORE_BARCELONA
158 #define IFLUSH gotoblas_iflush()
159 #define IFLUSH_HALF gotoblas_iflush_half()
162 #ifdef ENABLE_SSE_EXCEPTION
164 #define IDEBUG_START \
166 unsigned int fp_sse_mode, new_fp_mode; \
167 __asm__ __volatile__ ("stmxcsr %0" : "=m" (fp_sse_mode) : ); \
168 new_fp_mode = fp_sse_mode & ~0xd00; \
169 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (new_fp_mode) );
172 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (fp_sse_mode) ); \
178 #define GET_IMAGE(res) __asm__ __volatile__("fstpt %0" : "=m"(res) : : "memory")
179 #elif defined(DOUBLE)
180 #define GET_IMAGE(res) __asm__ __volatile__("movsd %%xmm1, %0" : "=m"(res) : : "memory")
182 #define GET_IMAGE(res) __asm__ __volatile__("movss %%xmm1, %0" : "=m"(res) : : "memory")
185 #define GET_IMAGE_CANCEL
188 #if defined(USE64BITINT)
189 static __inline blasint blas_quickdivide(blasint x, blasint y){
192 #elif defined (C_MSVC)
193 static __inline BLASLONG blas_quickdivide(BLASLONG x, BLASLONG y){
197 extern unsigned int blas_quick_divide_table[];
199 static __inline int blas_quickdivide(unsigned int x, unsigned int y){
203 if (y <= 1) return x;
205 #if (MAX_CPU_NUMBER > 64)
212 y = blas_quick_divide_table[y];
214 __asm__ __volatile__ ("mull %0" :"=d" (result), "+a"(x) : "0" (y));
224 #define PAGESIZE ( 4 << 10)
226 #define HUGE_PAGESIZE ( 2 << 20)
228 #define BUFFER_SIZE (32 << 20)
232 #ifdef F_INTERFACE_G77
233 #define RETURN_BY_STACK
237 #ifdef F_INTERFACE_G95
238 #define RETURN_BY_PACKED
241 #ifdef F_INTERFACE_GFORT
244 #define RETURN_BY_REGS
246 #define RETURN_BY_STACK
249 #define RETURN_BY_PACKED
253 #ifdef F_INTERFACE_INTEL
254 #define RETURN_BY_STACK
257 #ifdef F_INTERFACE_FUJITSU
258 #define RETURN_BY_STACK
261 #ifdef F_INTERFACE_FLANG
262 #define RETURN_BY_STACK
265 #ifdef F_INTERFACE_PGI
266 #define RETURN_BY_STACK
269 #ifdef F_INTERFACE_PATHSCALE
270 #define RETURN_BY_PACKED
273 #ifdef F_INTERFACE_SUN
274 #define RETURN_BY_PACKED
279 #if defined(PILEDRIVER) || defined(BULLDOZER) || defined(STEAMROLLER) || defined(EXCAVATOR)
280 //Enable some optimization for barcelona.
281 #define BARCELONA_OPTIMIZATION
284 #if defined(HAVE_3DNOW)
286 #elif defined(HAVE_MMX)
294 #define BRANCH .byte 0x3e
295 #define NOBRANCH .byte 0x2e
296 #define PADDING .byte 0x66
314 #define LOCAL_BUFFER_SIZE QLOCAL_BUFFER_SIZE
316 #define LOCAL_BUFFER_SIZE DLOCAL_BUFFER_SIZE
318 #define LOCAL_BUFFER_SIZE SLOCAL_BUFFER_SIZE
322 #define LOCAL_BUFFER_SIZE XLOCAL_BUFFER_SIZE
324 #define LOCAL_BUFFER_SIZE ZLOCAL_BUFFER_SIZE
326 #define LOCAL_BUFFER_SIZE CLOCAL_BUFFER_SIZE
330 #if defined(OS_WINDOWS)
331 #if LOCAL_BUFFER_SIZE > 16384
332 #define STACK_TOUCHING \
333 movl $0, 4096 * 4(%rsp);\
334 movl $0, 4096 * 3(%rsp);\
335 movl $0, 4096 * 2(%rsp);\
336 movl $0, 4096 * 1(%rsp);
337 #elif LOCAL_BUFFER_SIZE > 12288
338 #define STACK_TOUCHING \
339 movl $0, 4096 * 3(%rsp);\
340 movl $0, 4096 * 2(%rsp);\
341 movl $0, 4096 * 1(%rsp);
342 #elif LOCAL_BUFFER_SIZE > 8192
343 #define STACK_TOUCHING \
344 movl $0, 4096 * 2(%rsp);\
345 movl $0, 4096 * 1(%rsp);
346 #elif LOCAL_BUFFER_SIZE > 4096
347 #define STACK_TOUCHING \
348 movl $0, 4096 * 1(%rsp);
350 #define STACK_TOUCHING
353 #define STACK_TOUCHING
357 #define movapd movaps
359 #define movlpd movlps
360 #define movhpd movhps
364 #define REALNAME ASMNAME
366 #define REALNAME ASMFNAME
370 #define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
371 #define EPILOGUE .subsections_via_symbols
376 #define SAVEREGISTERS \
378 movups %xmm6, 0(%rsp);\
379 movups %xmm7, 16(%rsp);\
380 movups %xmm8, 32(%rsp);\
381 movups %xmm9, 48(%rsp);\
382 movups %xmm10, 64(%rsp);\
383 movups %xmm11, 80(%rsp);\
384 movups %xmm12, 96(%rsp);\
385 movups %xmm13, 112(%rsp);\
386 movups %xmm14, 128(%rsp);\
387 movups %xmm15, 144(%rsp)
389 #define RESTOREREGISTERS \
390 movups 0(%rsp), %xmm6;\
391 movups 16(%rsp), %xmm7;\
392 movups 32(%rsp), %xmm8;\
393 movups 48(%rsp), %xmm9;\
394 movups 64(%rsp), %xmm10;\
395 movups 80(%rsp), %xmm11;\
396 movups 96(%rsp), %xmm12;\
397 movups 112(%rsp), %xmm13;\
398 movups 128(%rsp), %xmm14;\
399 movups 144(%rsp), %xmm15;\
402 #define SAVEREGISTERS
403 #define RESTOREREGISTERS
406 #if defined(OS_WINDOWS) && !defined(C_PGI)
411 .def REALNAME;.scl 2;.type 32;.endef; \
416 #define EPILOGUE .end
419 #if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(__ELF__) || defined(C_PGI)
424 .type REALNAME, @function; \
428 #define PROFCODE call *mcount@GOTPCREL(%rip)
434 .size REALNAME, .-REALNAME; \
435 .section .note.GNU-stack,"",@progbits
446 #elif defined(DOUBLE)
455 #define CMPEQPD cmpeqpd
456 #define COMISD comisd
464 #define MOVUPD movupd
475 #define CMPEQPD cmpeqps
476 #define COMISD comiss
484 #define MOVUPD movups
491 #define ALIGN_2 .align 2
492 #define ALIGN_3 .align 3
493 #define ALIGN_4 .align 4
494 #define ALIGN_5 .align 5
499 #define ALIGN_2 .align 4
503 #define ALIGN_3 .align 8
507 #define ALIGN_4 .align 16
511 #define ALIGN_5 .align 32
515 #define ALIGN_6 .align 64
519 // Because Clang didn't support ffreep, we directly use the opcode.
520 // Please check out http://www.sandpile.org/x86/opc_fpu.htm
522 #define ffreep .byte 0xdf, 0xc0 #