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