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__("cpuid"
145 static __inline int WhereAmI(void){
146 int eax, ebx, ecx, edx;
149 cpuid(1, &eax, &ebx, &ecx, &edx);
150 apicid = BITMASK(ebx, 24, 0xff);
156 #ifdef CORE_BARCELONA
157 #define IFLUSH gotoblas_iflush()
158 #define IFLUSH_HALF gotoblas_iflush_half()
161 #ifdef ENABLE_SSE_EXCEPTION
163 #define IDEBUG_START \
165 unsigned int fp_sse_mode, new_fp_mode; \
166 __asm__ __volatile__ ("stmxcsr %0" : "=m" (fp_sse_mode) : ); \
167 new_fp_mode = fp_sse_mode & ~0xd00; \
168 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (new_fp_mode) );
171 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (fp_sse_mode) ); \
177 #define GET_IMAGE(res) __asm__ __volatile__("fstpt %0" : "=m"(res) : : "memory")
178 #elif defined(DOUBLE)
179 #define GET_IMAGE(res) __asm__ __volatile__("movsd %%xmm1, %0" : "=m"(res) : : "memory")
181 #define GET_IMAGE(res) __asm__ __volatile__("movss %%xmm1, %0" : "=m"(res) : : "memory")
184 #define GET_IMAGE_CANCEL
187 #if defined(USE64BITINT)
188 static __inline blasint blas_quickdivide(blasint x, blasint y){
191 #elif defined (C_MSVC)
192 static __inline BLASLONG blas_quickdivide(BLASLONG x, BLASLONG y){
196 extern unsigned int blas_quick_divide_table[];
198 static __inline int blas_quickdivide(unsigned int x, unsigned int y){
202 if (y <= 1) return x;
204 #if (MAX_CPU_NUMBER > 64)
211 y = blas_quick_divide_table[y];
213 __asm__ __volatile__ ("mull %0" :"=d" (result) :"a"(x), "0" (y));
223 #define PAGESIZE ( 4 << 10)
225 #define HUGE_PAGESIZE ( 2 << 20)
227 #define BUFFER_SIZE (32 << 20)
231 #ifdef F_INTERFACE_G77
232 #define RETURN_BY_STACK
236 #ifdef F_INTERFACE_G95
237 #define RETURN_BY_PACKED
240 #ifdef F_INTERFACE_GFORT
243 #define RETURN_BY_REGS
245 #define RETURN_BY_STACK
248 #define RETURN_BY_PACKED
252 #ifdef F_INTERFACE_INTEL
253 #define RETURN_BY_STACK
256 #ifdef F_INTERFACE_FUJITSU
257 #define RETURN_BY_STACK
260 #ifdef F_INTERFACE_FLANG
261 #define RETURN_BY_STACK
264 #ifdef F_INTERFACE_PGI
265 #define RETURN_BY_STACK
268 #ifdef F_INTERFACE_PATHSCALE
269 #define RETURN_BY_PACKED
272 #ifdef F_INTERFACE_SUN
273 #define RETURN_BY_PACKED
278 #if defined(PILEDRIVER) || defined(BULLDOZER) || defined(STEAMROLLER) || defined(EXCAVATOR)
279 //Enable some optimazation for barcelona.
280 #define BARCELONA_OPTIMIZATION
283 #if defined(HAVE_3DNOW)
285 #elif defined(HAVE_MMX)
293 #define BRANCH .byte 0x3e
294 #define NOBRANCH .byte 0x2e
295 #define PADDING .byte 0x66
313 #define LOCAL_BUFFER_SIZE QLOCAL_BUFFER_SIZE
315 #define LOCAL_BUFFER_SIZE DLOCAL_BUFFER_SIZE
317 #define LOCAL_BUFFER_SIZE SLOCAL_BUFFER_SIZE
321 #define LOCAL_BUFFER_SIZE XLOCAL_BUFFER_SIZE
323 #define LOCAL_BUFFER_SIZE ZLOCAL_BUFFER_SIZE
325 #define LOCAL_BUFFER_SIZE CLOCAL_BUFFER_SIZE
329 #if defined(OS_WINDOWS)
330 #if LOCAL_BUFFER_SIZE > 16384
331 #define STACK_TOUCHING \
332 movl $0, 4096 * 4(%rsp);\
333 movl $0, 4096 * 3(%rsp);\
334 movl $0, 4096 * 2(%rsp);\
335 movl $0, 4096 * 1(%rsp);
336 #elif LOCAL_BUFFER_SIZE > 12288
337 #define STACK_TOUCHING \
338 movl $0, 4096 * 3(%rsp);\
339 movl $0, 4096 * 2(%rsp);\
340 movl $0, 4096 * 1(%rsp);
341 #elif LOCAL_BUFFER_SIZE > 8192
342 #define STACK_TOUCHING \
343 movl $0, 4096 * 2(%rsp);\
344 movl $0, 4096 * 1(%rsp);
345 #elif LOCAL_BUFFER_SIZE > 4096
346 #define STACK_TOUCHING \
347 movl $0, 4096 * 1(%rsp);
349 #define STACK_TOUCHING
352 #define STACK_TOUCHING
356 #define movapd movaps
358 #define movlpd movlps
359 #define movhpd movhps
363 #define REALNAME ASMNAME
365 #define REALNAME ASMFNAME
369 #define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
370 #define EPILOGUE .subsections_via_symbols
375 #define SAVEREGISTERS \
377 movups %xmm6, 0(%rsp);\
378 movups %xmm7, 16(%rsp);\
379 movups %xmm8, 32(%rsp);\
380 movups %xmm9, 48(%rsp);\
381 movups %xmm10, 64(%rsp);\
382 movups %xmm11, 80(%rsp);\
383 movups %xmm12, 96(%rsp);\
384 movups %xmm13, 112(%rsp);\
385 movups %xmm14, 128(%rsp);\
386 movups %xmm15, 144(%rsp)
388 #define RESTOREREGISTERS \
389 movups 0(%rsp), %xmm6;\
390 movups 16(%rsp), %xmm7;\
391 movups 32(%rsp), %xmm8;\
392 movups 48(%rsp), %xmm9;\
393 movups 64(%rsp), %xmm10;\
394 movups 80(%rsp), %xmm11;\
395 movups 96(%rsp), %xmm12;\
396 movups 112(%rsp), %xmm13;\
397 movups 128(%rsp), %xmm14;\
398 movups 144(%rsp), %xmm15;\
401 #define SAVEREGISTERS
402 #define RESTOREREGISTERS
405 #if defined(OS_WINDOWS) && !defined(C_PGI)
410 .def REALNAME;.scl 2;.type 32;.endef; \
415 #define EPILOGUE .end
418 #if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(__ELF__) || defined(C_PGI)
423 .type REALNAME, @function; \
427 #define PROFCODE call *mcount@GOTPCREL(%rip)
433 .size REALNAME, .-REALNAME; \
434 .section .note.GNU-stack,"",@progbits
445 #elif defined(DOUBLE)
454 #define CMPEQPD cmpeqpd
455 #define COMISD comisd
463 #define MOVUPD movupd
474 #define CMPEQPD cmpeqps
475 #define COMISD comiss
483 #define MOVUPD movups
490 #define ALIGN_2 .align 2
491 #define ALIGN_3 .align 3
492 #define ALIGN_4 .align 4
493 #define ALIGN_5 .align 5
498 #define ALIGN_2 .align 4
502 #define ALIGN_3 .align 8
506 #define ALIGN_4 .align 16
510 #define ALIGN_5 .align 32
514 #define ALIGN_6 .align 64
518 // Because Clang didn't support ffreep, we directly use the opcode.
519 // Please check out http://www.sandpile.org/x86/opc_fpu.htm
521 #define ffreep .byte 0xdf, 0xc0 #