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