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