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