Merge branch 'cmake' into develop
[platform/upstream/openblas.git] / common_x86.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 #define MB
45 #define WMB
46
47 #ifdef C_SUN
48 #define __asm__ __asm
49 #define __volatile__
50 #endif
51
52 static void __inline blas_lock(volatile BLASULONG *address){
53
54   int ret;
55
56   do {
57     while (*address) {YIELDING;};
58
59 #if defined(_MSC_VER) && !defined(__clang__)
60         // use intrinsic instead of inline assembly
61         ret = _InterlockedExchange(address, 1);
62         // inline assembly
63         /*__asm {
64                 mov eax, address
65                 mov ebx, 1
66                 xchg [eax], ebx
67                 mov ret, ebx
68         }*/
69 #else
70     __asm__ __volatile__(
71                          "xchgl %0, %1\n"
72                          : "=r"(ret), "=m"(*address)
73                          : "0"(1), "m"(*address)
74                          : "memory");
75 #endif
76
77   } while (ret);
78
79 }
80 #define BLAS_LOCK_DEFINED
81
82 static __inline unsigned long long rpcc(void){
83 #if defined(_MSC_VER) && !defined(__clang__)
84   return __rdtsc(); // use MSVC intrinsic
85 #else
86   unsigned int a, d;
87
88   __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
89
90   return ((unsigned long long)a + ((unsigned long long)d << 32));
91 #endif
92 };
93 #define RPCC_DEFINED
94
95 static __inline unsigned long getstackaddr(void){
96 #if defined(_MSC_VER) && !defined(__clang__)
97   return (unsigned long)_ReturnAddress(); // use MSVC intrinsic
98 #else
99   unsigned long addr;
100
101   __asm__ __volatile__ ("mov %%esp, %0"
102                          : "=r"(addr) : : "memory");
103
104   return addr;
105 #endif
106 };
107
108
109 static __inline long double sqrt_long(long double val) {
110 #if defined(_MSC_VER) && !defined(__clang__)
111   return sqrt(val); // not sure if this will use fsqrt
112 #else
113   long double result;
114
115   __asm__ __volatile__ ("fldt %1\n"
116                     "fsqrt\n"
117                     "fstpt %0\n" : "=m" (result) : "m"(val));
118   return result;
119 #endif
120 }
121
122 #define SQRT(a)  sqrt_long(a)
123
124 /* This is due to gcc's bug */
125 void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx);
126
127 #define WHEREAMI
128
129 static __inline int WhereAmI(void){
130   int eax, ebx, ecx, edx;
131   int apicid;
132
133   cpuid(1, &eax, &ebx, &ecx, &edx);
134   apicid  = BITMASK(ebx, 24, 0xff);
135
136   return apicid;
137 }
138
139 #ifdef ENABLE_SSE_EXCEPTION
140
141 #define IDEBUG_START \
142 { \
143   unsigned int fp_sse_mode, new_fp_mode; \
144   __asm__ __volatile__ ("stmxcsr %0" : "=m" (fp_sse_mode) : ); \
145   new_fp_mode = fp_sse_mode & ~0xd00; \
146   __asm__ __volatile__ ("ldmxcsr %0" : : "m" (new_fp_mode) );
147
148 #define IDEBUG_END \
149   __asm__ __volatile__ ("ldmxcsr %0" : : "m" (fp_sse_mode) ); \
150 }
151
152 #endif
153
154 #ifdef XDOUBLE
155 #define GET_IMAGE(res)  __asm__ __volatile__("fstpt %0" : "=m"(res) : : "memory")
156 #elif defined(DOUBLE)
157 #define GET_IMAGE(res)  __asm__ __volatile__("fstpl %0" : "=m"(res) : : "memory")
158 #else
159 #define GET_IMAGE(res)  __asm__ __volatile__("fstps %0" : "=m"(res) : : "memory");
160 #endif
161
162 #define GET_IMAGE_CANCEL        __asm__ __volatile__ ("ffree %st")
163
164 #ifdef SMP
165 extern unsigned int blas_quick_divide_table[];
166
167 static __inline int blas_quickdivide(unsigned int x, unsigned int y){
168
169   unsigned int result;
170
171   if (y <= 1) return x;
172
173   y = blas_quick_divide_table[y];
174
175 #if defined(_MSC_VER) && !defined(__clang__)
176   (void*)result;
177   return x*y;
178 #else
179   __asm__ __volatile__  ("mull %0" :"=d" (result) :"a"(x), "0" (y));
180
181   return result;
182 #endif
183 }
184 #endif
185
186 #endif
187
188 #ifndef PAGESIZE
189 #define PAGESIZE        ( 4 << 10)
190 #endif
191 #define HUGE_PAGESIZE   ( 4 << 20)
192
193 #define BUFFER_SIZE     (16 << 20)
194
195 #define SEEK_ADDRESS
196
197 #if defined(DOUBLE) || defined(XDOUBLE)
198 #define MMXLOAD         movq
199 #define MMXSTORE        movq
200 #else
201 #define MMXLOAD         movd
202 #define MMXSTORE        movd
203 #endif
204
205 #if defined(PILEDRIVER) || defined(BULLDOZER) || defined(STEAMROLLER) || defined(EXCAVATOR)
206 //Enable some optimazation for barcelona.
207 #define BARCELONA_OPTIMIZATION
208 #endif
209
210 #if defined(HAVE_3DNOW)
211 #define EMMS    femms
212 #elif defined(HAVE_MMX)
213 #define EMMS    emms
214 #endif
215
216 #ifndef EMMS
217 #define EMMS
218 #endif
219
220 #if defined(CORE2) || defined(PENTIUM4)
221 #define movapd  movaps
222 #endif
223
224 #define BRANCH          .byte 0x3e
225 #define NOBRANCH        .byte 0x2e
226 #define PADDING         .byte 0x66;
227 #define HALT            hlt
228
229 #ifndef COMPLEX
230 #ifdef XDOUBLE
231 #define LOCAL_BUFFER_SIZE  QLOCAL_BUFFER_SIZE
232 #elif defined DOUBLE
233 #define LOCAL_BUFFER_SIZE  DLOCAL_BUFFER_SIZE
234 #else
235 #define LOCAL_BUFFER_SIZE  SLOCAL_BUFFER_SIZE
236 #endif
237 #else
238 #ifdef XDOUBLE
239 #define LOCAL_BUFFER_SIZE  XLOCAL_BUFFER_SIZE
240 #elif defined DOUBLE
241 #define LOCAL_BUFFER_SIZE  ZLOCAL_BUFFER_SIZE
242 #else
243 #define LOCAL_BUFFER_SIZE  CLOCAL_BUFFER_SIZE
244 #endif
245 #endif
246
247 #if defined(OS_WINDOWS)
248 #if   LOCAL_BUFFER_SIZE > 16384
249 #define STACK_TOUCHING \
250         movl    $0,  4096 * 4(%esp);\
251         movl    $0,  4096 * 3(%esp);\
252         movl    $0,  4096 * 2(%esp);\
253         movl    $0,  4096 * 1(%esp);
254 #elif LOCAL_BUFFER_SIZE > 12288
255 #define STACK_TOUCHING \
256         movl    $0,  4096 * 3(%esp);\
257         movl    $0,  4096 * 2(%esp);\
258         movl    $0,  4096 * 1(%esp);
259 #elif LOCAL_BUFFER_SIZE > 8192
260 #define STACK_TOUCHING \
261         movl    $0,  4096 * 2(%esp);\
262         movl    $0,  4096 * 1(%esp);
263 #elif LOCAL_BUFFER_SIZE > 4096
264 #define STACK_TOUCHING \
265         movl    $0,  4096 * 1(%esp);
266 #else
267 #define STACK_TOUCHING
268 #endif
269 #else
270 #define STACK_TOUCHING
271 #endif
272
273 #ifndef F_INTERFACE
274 #define REALNAME ASMNAME
275 #else
276 #define REALNAME ASMFNAME
277 #endif
278
279 #if defined(F_INTERFACE_PATHSCALE) || defined(F_INTERFACE_OPEN64)
280 #define RETURN_BY_STRUCT
281 #elif defined(F_INTERFACE_GFORT) || defined(F_INTERFACE_G95)
282 #define RETURN_BY_COMPLEX
283 #else
284 #define RETURN_BY_STACK
285 #endif
286
287 #ifdef OS_DARWIN
288 #define PROLOGUE .text;.align 5; .globl REALNAME; REALNAME:
289 #define EPILOGUE        .subsections_via_symbols
290 #define PROFCODE
291 #endif
292
293 #if defined(OS_WINNT) || defined(OS_CYGWIN_NT) || defined(OS_INTERIX)
294 #define SAVEREGISTERS \
295         subl    $32, %esp;\
296         movups  %xmm6,    0(%esp);\
297         movups  %xmm7,   16(%esp)
298
299 #define RESTOREREGISTERS \
300         movups     0(%esp), %xmm6;\
301         movups    16(%esp), %xmm7;\
302         addl    $32, %esp
303 #else
304 #define SAVEREGISTERS
305 #define RESTOREREGISTERS
306 #endif
307
308 #if defined(OS_WINNT) || defined(OS_CYGWIN_NT) || defined(OS_INTERIX)
309 #define PROLOGUE \
310         .text; \
311         .align 16; \
312         .globl REALNAME ;\
313         .def REALNAME;.scl      2;.type 32;.endef; \
314 REALNAME:
315
316 #define PROFCODE
317
318 #ifdef __clang__
319 #define EPILOGUE .end
320 #else
321 #define EPILOGUE .end    REALNAME
322 #endif
323 #endif
324
325 #if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(__ELF__)
326 #define PROLOGUE \
327         .text; \
328         .align 16; \
329         .globl REALNAME ;\
330        .type REALNAME, @function; \
331 REALNAME:
332
333 #ifdef PROFILE
334 #define PROFCODE call mcount
335 #else
336 #define PROFCODE
337 #endif
338
339 #define EPILOGUE \
340         .size    REALNAME, .-REALNAME; \
341         .section .note.GNU-stack,"",@progbits
342
343 #endif
344
345 #ifdef XDOUBLE
346 #define FLD     fldt
347 #define FST     fstpt
348 #define FSTU    fstt
349 #define FMUL    fmult
350 #define FADD    faddt
351 #define FSUB    fsubt
352 #define FSUBR   fsubrt
353 #elif defined(DOUBLE)
354 #define FLD     fldl
355 #define FST     fstpl
356 #define FSTU    fstl
357 #define FMUL    fmull
358 #define FADD    faddl
359 #define FSUB    fsubl
360 #define FSUBR   fsubrl
361 #else
362 #define FLD     flds
363 #define FST     fstps
364 #define FSTU    fsts
365 #define FMUL    fmuls
366 #define FADD    fadds
367 #define FSUB    fsubs
368 #define FSUBR   fsubrs
369 #endif
370 #endif
371
372 #ifdef C_SUN
373 #define ffreep  fstp
374 #endif
375
376 #ifdef __APPLE__
377 #define ALIGN_2 .align 2
378 #define ALIGN_3 .align 3
379 #define ALIGN_4 .align 4
380 #define ALIGN_5 .align 5
381 #define ffreep  fstp
382 #endif
383
384 #ifndef ALIGN_2
385 #define ALIGN_2 .align 4
386 #endif
387
388 #ifndef ALIGN_3
389 #define ALIGN_3 .align 8
390 #endif
391
392 #ifndef ALIGN_4
393 #define ALIGN_4 .align 16
394 #endif
395
396 #ifndef ALIGN_5
397 #define ALIGN_5 .align 32
398 #endif
399
400 #ifndef ALIGN_6
401 #define ALIGN_6 .align 64
402 #endif
403 // ffreep %st(0).
404 // Because Clang didn't support ffreep, we directly use the opcode.
405 // Please check out http://www.sandpile.org/x86/opc_fpu.htm
406 #ifndef ffreep
407 #define ffreep .byte 0xdf, 0xc0 #
408 #endif