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