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