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