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");
66 static void __inline blas_lock(volatile BLASULONG *address){
75 while (*address) {YIELDING;};
80 : "=r"(ret), "=m"(*address)
81 : "0"(1), "m"(*address)
84 ret=InterlockedExchange64((volatile LONG64 *)(address), 1);
89 #define BLAS_LOCK_DEFINED
91 static __inline BLASULONG rpcc(void){
97 __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
99 return ((BLASULONG)a + ((BLASULONG)d << 32));
107 static __inline BLASULONG getstackaddr(void){
110 __asm__ __volatile__ ("movq %%rsp, %0"
111 : "=r"(addr) : : "memory");
117 static __inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
121 __cpuid(cpuinfo, op);
127 __asm__ __volatile__("cpuid"
140 static __inline int WhereAmI(void){
141 int eax, ebx, ecx, edx;
144 cpuid(1, &eax, &ebx, &ecx, &edx);
145 apicid = BITMASK(ebx, 24, 0xff);
151 #ifdef CORE_BARCELONA
152 #define IFLUSH gotoblas_iflush()
153 #define IFLUSH_HALF gotoblas_iflush_half()
156 #ifdef ENABLE_SSE_EXCEPTION
158 #define IDEBUG_START \
160 unsigned int fp_sse_mode, new_fp_mode; \
161 __asm__ __volatile__ ("stmxcsr %0" : "=m" (fp_sse_mode) : ); \
162 new_fp_mode = fp_sse_mode & ~0xd00; \
163 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (new_fp_mode) );
166 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (fp_sse_mode) ); \
172 #define GET_IMAGE(res) __asm__ __volatile__("fstpt %0" : "=m"(res) : : "memory")
173 #elif defined(DOUBLE)
174 #define GET_IMAGE(res) __asm__ __volatile__("movsd %%xmm1, %0" : "=m"(res) : : "memory")
176 #define GET_IMAGE(res) __asm__ __volatile__("movss %%xmm1, %0" : "=m"(res) : : "memory")
179 #define GET_IMAGE_CANCEL
182 #if defined(USE64BITINT)
183 static __inline blasint blas_quickdivide(blasint x, blasint y){
186 #elif defined (C_MSVC)
187 static __inline BLASLONG blas_quickdivide(BLASLONG x, BLASLONG y){
191 extern unsigned int blas_quick_divide_table[];
193 static __inline int blas_quickdivide(unsigned int x, unsigned int y){
197 if (y <= 1) return x;
199 #if (MAX_CPU_NUMBER > 64)
206 y = blas_quick_divide_table[y];
208 __asm__ __volatile__ ("mull %0" :"=d" (result) :"a"(x), "0" (y));
218 #define PAGESIZE ( 4 << 10)
220 #define HUGE_PAGESIZE ( 2 << 20)
222 #define BUFFER_SIZE (32 << 20)
226 #ifdef F_INTERFACE_G77
227 #define RETURN_BY_STACK
231 #ifdef F_INTERFACE_G95
232 #define RETURN_BY_PACKED
235 #ifdef F_INTERFACE_GFORT
238 #define RETURN_BY_REGS
240 #define RETURN_BY_STACK
243 #define RETURN_BY_PACKED
247 #ifdef F_INTERFACE_INTEL
248 #define RETURN_BY_STACK
251 #ifdef F_INTERFACE_FUJITSU
252 #define RETURN_BY_STACK
255 #ifdef F_INTERFACE_FLANG
256 #define RETURN_BY_STACK
259 #ifdef F_INTERFACE_PGI
260 #define RETURN_BY_STACK
263 #ifdef F_INTERFACE_PATHSCALE
264 #define RETURN_BY_PACKED
267 #ifdef F_INTERFACE_SUN
268 #define RETURN_BY_PACKED
273 #if defined(PILEDRIVER) || defined(BULLDOZER) || defined(STEAMROLLER) || defined(EXCAVATOR)
274 //Enable some optimazation for barcelona.
275 #define BARCELONA_OPTIMIZATION
278 #if defined(HAVE_3DNOW)
280 #elif defined(HAVE_MMX)
288 #define BRANCH .byte 0x3e
289 #define NOBRANCH .byte 0x2e
290 #define PADDING .byte 0x66
308 #define LOCAL_BUFFER_SIZE QLOCAL_BUFFER_SIZE
310 #define LOCAL_BUFFER_SIZE DLOCAL_BUFFER_SIZE
312 #define LOCAL_BUFFER_SIZE SLOCAL_BUFFER_SIZE
316 #define LOCAL_BUFFER_SIZE XLOCAL_BUFFER_SIZE
318 #define LOCAL_BUFFER_SIZE ZLOCAL_BUFFER_SIZE
320 #define LOCAL_BUFFER_SIZE CLOCAL_BUFFER_SIZE
324 #if defined(OS_WINDOWS)
325 #if LOCAL_BUFFER_SIZE > 16384
326 #define STACK_TOUCHING \
327 movl $0, 4096 * 4(%rsp);\
328 movl $0, 4096 * 3(%rsp);\
329 movl $0, 4096 * 2(%rsp);\
330 movl $0, 4096 * 1(%rsp);
331 #elif LOCAL_BUFFER_SIZE > 12288
332 #define STACK_TOUCHING \
333 movl $0, 4096 * 3(%rsp);\
334 movl $0, 4096 * 2(%rsp);\
335 movl $0, 4096 * 1(%rsp);
336 #elif LOCAL_BUFFER_SIZE > 8192
337 #define STACK_TOUCHING \
338 movl $0, 4096 * 2(%rsp);\
339 movl $0, 4096 * 1(%rsp);
340 #elif LOCAL_BUFFER_SIZE > 4096
341 #define STACK_TOUCHING \
342 movl $0, 4096 * 1(%rsp);
344 #define STACK_TOUCHING
347 #define STACK_TOUCHING
351 #define movapd movaps
353 #define movlpd movlps
354 #define movhpd movhps
358 #define REALNAME ASMNAME
360 #define REALNAME ASMFNAME
364 #define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
365 #define EPILOGUE .subsections_via_symbols
370 #define SAVEREGISTERS \
372 movups %xmm6, 0(%rsp);\
373 movups %xmm7, 16(%rsp);\
374 movups %xmm8, 32(%rsp);\
375 movups %xmm9, 48(%rsp);\
376 movups %xmm10, 64(%rsp);\
377 movups %xmm11, 80(%rsp);\
378 movups %xmm12, 96(%rsp);\
379 movups %xmm13, 112(%rsp);\
380 movups %xmm14, 128(%rsp);\
381 movups %xmm15, 144(%rsp)
383 #define RESTOREREGISTERS \
384 movups 0(%rsp), %xmm6;\
385 movups 16(%rsp), %xmm7;\
386 movups 32(%rsp), %xmm8;\
387 movups 48(%rsp), %xmm9;\
388 movups 64(%rsp), %xmm10;\
389 movups 80(%rsp), %xmm11;\
390 movups 96(%rsp), %xmm12;\
391 movups 112(%rsp), %xmm13;\
392 movups 128(%rsp), %xmm14;\
393 movups 144(%rsp), %xmm15;\
396 #define SAVEREGISTERS
397 #define RESTOREREGISTERS
400 #if defined(OS_WINDOWS) && !defined(C_PGI)
405 .def REALNAME;.scl 2;.type 32;.endef; \
410 #define EPILOGUE .end
413 #if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(__ELF__) || defined(C_PGI)
418 .type REALNAME, @function; \
422 #define PROFCODE call *mcount@GOTPCREL(%rip)
428 .size REALNAME, .-REALNAME; \
429 .section .note.GNU-stack,"",@progbits
440 #elif defined(DOUBLE)
449 #define CMPEQPD cmpeqpd
450 #define COMISD comisd
458 #define MOVUPD movupd
469 #define CMPEQPD cmpeqps
470 #define COMISD comiss
478 #define MOVUPD movups
485 #define ALIGN_2 .align 2
486 #define ALIGN_3 .align 3
487 #define ALIGN_4 .align 4
488 #define ALIGN_5 .align 5
493 #define ALIGN_2 .align 4
497 #define ALIGN_3 .align 8
501 #define ALIGN_4 .align 16
505 #define ALIGN_5 .align 32
509 #define ALIGN_6 .align 64
513 // Because Clang didn't support ffreep, we directly use the opcode.
514 // Please check out http://www.sandpile.org/x86/opc_fpu.htm
516 #define ffreep .byte 0xdf, 0xc0 #