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