Merge branch 'cmake' of https://github.com/hpanderson/OpenBLAS into hpanderson_cmake
[platform/upstream/openblas.git] / common_x86_64.h
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
39 #ifndef COMMON_X86
40 #define COMMON_X86
41
42 #ifndef ASSEMBLER
43
44 #ifdef C_SUN
45 #define __asm__ __asm
46 #define __volatile__
47 #endif
48
49 /*
50 #ifdef HAVE_SSE2
51 #define MB   __asm__ __volatile__ ("mfence");
52 #define WMB  __asm__ __volatile__ ("sfence");
53 #else
54 #define MB
55 #define WMB
56 #endif
57 */
58
59 #define MB
60 #define WMB
61
62 static void __inline blas_lock(volatile BLASULONG *address){
63
64   int ret;
65
66   do {
67     while (*address) {YIELDING;};
68
69     __asm__ __volatile__(
70                          "xchgl %0, %1\n"
71                          : "=r"(ret), "=m"(*address)
72                          : "0"(1), "m"(*address)
73                          : "memory");
74
75   } while (ret);
76 }
77
78 static __inline BLASULONG rpcc(void){
79   BLASULONG a, d;
80
81   __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
82
83   return ((BLASULONG)a + ((BLASULONG)d << 32));
84 }
85
86 #define RPCC64BIT
87
88 static __inline BLASULONG getstackaddr(void){
89   BLASULONG addr;
90
91   __asm__ __volatile__ ("movq %%rsp, %0"
92                          : "=r"(addr) : : "memory");
93
94   return addr;
95 }
96
97 static __inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
98
99         __asm__ __volatile__("cpuid"
100                              : "=a" (*eax),
101                              "=b" (*ebx),
102                              "=c" (*ecx),
103                              "=d" (*edx)
104                              : "0" (op));
105 }
106
107 /*
108 #define WHEREAMI
109 */
110
111 static inline int WhereAmI(void){
112   int eax, ebx, ecx, edx;
113   int apicid;
114
115   cpuid(1, &eax, &ebx, &ecx, &edx);
116   apicid  = BITMASK(ebx, 24, 0xff);
117
118   return apicid;
119 }
120
121
122 #ifdef CORE_BARCELONA
123 #define IFLUSH          gotoblas_iflush()
124 #define IFLUSH_HALF     gotoblas_iflush_half()
125 #endif
126
127 #ifdef ENABLE_SSE_EXCEPTION
128
129 #define IDEBUG_START \
130 { \
131   unsigned int fp_sse_mode, new_fp_mode; \
132   __asm__ __volatile__ ("stmxcsr %0" : "=m" (fp_sse_mode) : ); \
133   new_fp_mode = fp_sse_mode & ~0xd00; \
134   __asm__ __volatile__ ("ldmxcsr %0" : : "m" (new_fp_mode) );
135
136 #define IDEBUG_END \
137   __asm__ __volatile__ ("ldmxcsr %0" : : "m" (fp_sse_mode) ); \
138 }
139
140 #endif
141
142 #ifdef XDOUBLE
143 #define GET_IMAGE(res)  __asm__ __volatile__("fstpt %0" : "=m"(res) : : "memory")
144 #elif defined(DOUBLE)
145 #define GET_IMAGE(res)  __asm__ __volatile__("movsd %%xmm1, %0" : "=m"(res) : : "memory")
146 #else
147 #define GET_IMAGE(res)  __asm__ __volatile__("movss %%xmm1, %0" : "=m"(res) : : "memory")
148 #endif
149
150 #define GET_IMAGE_CANCEL
151
152 #ifdef SMP
153 #ifdef USE64BITINT
154 static __inline blasint blas_quickdivide(blasint x, blasint y){
155   return x / y;
156 }
157 #else
158 extern unsigned int blas_quick_divide_table[];
159
160 static __inline int blas_quickdivide(unsigned int x, unsigned int y){
161
162   unsigned int result;
163
164   if (y <= 1) return x;
165
166   y = blas_quick_divide_table[y];
167
168   __asm__ __volatile__  ("mull %0" :"=d" (result) :"a"(x), "0" (y));
169
170   return result;
171 }
172 #endif
173 #endif
174
175 #endif
176
177 #ifndef PAGESIZE
178 #define PAGESIZE        ( 4 << 10)
179 #endif
180 #define HUGE_PAGESIZE   ( 2 << 20)
181
182 #define BUFFER_SIZE     (32 << 20)
183
184 #define SEEK_ADDRESS
185
186 #ifdef F_INTERFACE_G77
187 #define RETURN_BY_STACK
188 #define NEED_F2CCONV
189 #endif
190
191 #ifdef F_INTERFACE_G95
192 #define RETURN_BY_PACKED
193 #endif
194
195 #ifdef F_INTERFACE_GFORT
196 #ifdef OS_WINDOWS
197 #ifndef DOUBLE
198 #define RETURN_BY_REGS
199 #else
200 #define RETURN_BY_STACK
201 #endif
202 #else
203 #define RETURN_BY_PACKED
204 #endif
205 #endif
206
207 #ifdef F_INTERFACE_INTEL
208 #define RETURN_BY_STACK
209 #endif
210
211 #ifdef F_INTERFACE_FUJITSU
212 #define RETURN_BY_STACK
213 #endif
214
215 #ifdef F_INTERFACE_PGI
216 #define RETURN_BY_STACK
217 #endif
218
219 #ifdef F_INTERFACE_PATHSCALE
220 #define RETURN_BY_PACKED
221 #endif
222
223 #ifdef F_INTERFACE_SUN
224 #define RETURN_BY_PACKED
225 #endif
226
227 #ifdef ASSEMBLER
228
229 #if defined(PILEDRIVER) || defined(BULLDOZER) || defined(STEAMROLLER) || defined(EXCAVATOR)
230 //Enable some optimazation for barcelona.
231 #define BARCELONA_OPTIMIZATION
232 #endif
233
234 #if defined(HAVE_3DNOW)
235 #define EMMS    femms
236 #elif defined(HAVE_MMX)
237 #define EMMS    emms
238 #endif
239
240 #ifndef EMMS
241 #define EMMS
242 #endif
243
244 #define BRANCH          .byte 0x3e
245 #define NOBRANCH        .byte 0x2e
246 #define PADDING         .byte 0x66
247
248 #ifdef OS_WINDOWS
249 #define ARG1    %rcx
250 #define ARG2    %rdx
251 #define ARG3    %r8
252 #define ARG4    %r9
253 #else
254 #define ARG1    %rdi
255 #define ARG2    %rsi
256 #define ARG3    %rdx
257 #define ARG4    %rcx
258 #define ARG5    %r8
259 #define ARG6    %r9
260 #endif
261
262 #ifndef COMPLEX
263 #ifdef XDOUBLE
264 #define LOCAL_BUFFER_SIZE  QLOCAL_BUFFER_SIZE
265 #elif defined DOUBLE
266 #define LOCAL_BUFFER_SIZE  DLOCAL_BUFFER_SIZE
267 #else
268 #define LOCAL_BUFFER_SIZE  SLOCAL_BUFFER_SIZE
269 #endif
270 #else
271 #ifdef XDOUBLE
272 #define LOCAL_BUFFER_SIZE  XLOCAL_BUFFER_SIZE
273 #elif defined DOUBLE
274 #define LOCAL_BUFFER_SIZE  ZLOCAL_BUFFER_SIZE
275 #else
276 #define LOCAL_BUFFER_SIZE  CLOCAL_BUFFER_SIZE
277 #endif
278 #endif
279
280 #if defined(OS_WINDOWS)
281 #if   LOCAL_BUFFER_SIZE > 16384
282 #define STACK_TOUCHING \
283         movl    $0,  4096 * 4(%rsp);\
284         movl    $0,  4096 * 3(%rsp);\
285         movl    $0,  4096 * 2(%rsp);\
286         movl    $0,  4096 * 1(%rsp);
287 #elif LOCAL_BUFFER_SIZE > 12288
288 #define STACK_TOUCHING \
289         movl    $0,  4096 * 3(%rsp);\
290         movl    $0,  4096 * 2(%rsp);\
291         movl    $0,  4096 * 1(%rsp);
292 #elif LOCAL_BUFFER_SIZE > 8192
293 #define STACK_TOUCHING \
294         movl    $0,  4096 * 2(%rsp);\
295         movl    $0,  4096 * 1(%rsp);
296 #elif LOCAL_BUFFER_SIZE > 4096
297 #define STACK_TOUCHING \
298         movl    $0,  4096 * 1(%rsp);
299 #else
300 #define STACK_TOUCHING
301 #endif
302 #else
303 #define STACK_TOUCHING
304 #endif
305
306 #if defined(CORE2)
307 #define movapd  movaps
308 #define andpd   andps
309 #define movlpd  movlps
310 #define movhpd  movhps
311 #endif
312
313 #ifndef F_INTERFACE
314 #define REALNAME ASMNAME
315 #else
316 #define REALNAME ASMFNAME
317 #endif
318
319 #ifdef OS_DARWIN
320 #define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
321 #define EPILOGUE        .subsections_via_symbols
322 #define PROFCODE
323 #endif
324
325 #ifdef OS_WINDOWS
326 #define SAVEREGISTERS \
327         subq    $256, %rsp;\
328         movups  %xmm6,    0(%rsp);\
329         movups  %xmm7,   16(%rsp);\
330         movups  %xmm8,   32(%rsp);\
331         movups  %xmm9,   48(%rsp);\
332         movups  %xmm10,  64(%rsp);\
333         movups  %xmm11,  80(%rsp);\
334         movups  %xmm12,  96(%rsp);\
335         movups  %xmm13, 112(%rsp);\
336         movups  %xmm14, 128(%rsp);\
337         movups  %xmm15, 144(%rsp)
338
339 #define RESTOREREGISTERS \
340         movups     0(%rsp), %xmm6;\
341         movups    16(%rsp), %xmm7;\
342         movups    32(%rsp), %xmm8;\
343         movups    48(%rsp), %xmm9;\
344         movups    64(%rsp), %xmm10;\
345         movups    80(%rsp), %xmm11;\
346         movups    96(%rsp), %xmm12;\
347         movups   112(%rsp), %xmm13;\
348         movups   128(%rsp), %xmm14;\
349         movups   144(%rsp), %xmm15;\
350         addq    $256, %rsp
351 #else
352 #define SAVEREGISTERS
353 #define RESTOREREGISTERS
354 #endif
355
356 #if defined(OS_WINDOWS) && !defined(C_PGI)
357 #define PROLOGUE \
358         .text; \
359         .align 16; \
360         .globl REALNAME ;\
361         .def REALNAME;.scl      2;.type 32;.endef; \
362 REALNAME:
363
364 #define PROFCODE
365
366 #define EPILOGUE .end    REALNAME
367 #endif
368
369 #if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(__ELF__) || defined(C_PGI)
370 #define PROLOGUE \
371         .text; \
372         .align 512; \
373         .globl REALNAME ;\
374        .type REALNAME, @function; \
375 REALNAME:
376
377 #ifdef PROFILE
378 #define PROFCODE call *mcount@GOTPCREL(%rip)
379 #else
380 #define PROFCODE
381 #endif
382
383 #define EPILOGUE \
384         .size    REALNAME, .-REALNAME; \
385         .section .note.GNU-stack,"",@progbits
386
387
388 #endif
389
390 #endif
391
392 #ifdef XDOUBLE
393 #define FLD     fldt
394 #define FST     fstpt
395 #define MOVQ    movq
396 #elif defined(DOUBLE)
397 #define FLD     fldl
398 #define FST     fstpl
399 #define FSTU    fstl
400 #define FMUL    fmull
401 #define FADD    faddl
402 #define MOVSD   movsd
403 #define MULSD   mulsd
404 #define MULPD   mulpd
405 #define CMPEQPD cmpeqpd
406 #define COMISD  comisd
407 #define PSRLQ   psrlq
408 #define ANDPD   andpd
409 #define ADDPD   addpd
410 #define ADDSD   addsd
411 #define SUBPD   subpd
412 #define SUBSD   subsd
413 #define MOVQ    movq
414 #define MOVUPD  movupd
415 #define XORPD   xorpd
416 #else
417 #define FLD     flds
418 #define FST     fstps
419 #define FSTU    fsts
420 #define FMUL    fmuls
421 #define FADD    fadds
422 #define MOVSD   movss
423 #define MULSD   mulss
424 #define MULPD   mulps
425 #define CMPEQPD cmpeqps
426 #define COMISD  comiss
427 #define PSRLQ   psrld
428 #define ANDPD   andps
429 #define ADDPD   addps
430 #define ADDSD   addss
431 #define SUBPD   subps
432 #define SUBSD   subss
433 #define MOVQ    movd
434 #define MOVUPD  movups
435 #define XORPD   xorps
436 #endif
437
438 #define HALT    hlt
439
440 #ifdef OS_DARWIN
441 #define ALIGN_2 .align 2
442 #define ALIGN_3 .align 3
443 #define ALIGN_4 .align 4
444 #define ALIGN_5 .align 5
445 #define ffreep  fstp
446 #endif
447
448 #ifndef ALIGN_2
449 #define ALIGN_2 .align 4
450 #endif
451
452 #ifndef ALIGN_3
453 #define ALIGN_3 .align 8
454 #endif
455
456 #ifndef ALIGN_4
457 #define ALIGN_4 .align 16
458 #endif
459
460 #ifndef ALIGN_5
461 #define ALIGN_5 .align 32
462 #endif
463
464 #ifndef ALIGN_6
465 #define ALIGN_6 .align 64
466 #endif
467
468 // ffreep %st(0).
469 // Because Clang didn't support ffreep, we directly use the opcode.
470 // Please check out http://www.sandpile.org/x86/opc_fpu.htm
471 #ifndef ffreep
472 #define ffreep .byte 0xdf, 0xc0 #
473 #endif
474 #endif