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