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