Merge pull request #2157 from martin-frbg/2154-2
[platform/upstream/openblas.git] / common.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_H
40 #define COMMON_H
41
42 #ifdef __cplusplus
43 extern "C" {
44         /* Assume C declarations for C++ */
45 #endif  /* __cplusplus */
46
47 #ifndef _GNU_SOURCE
48 #define _GNU_SOURCE
49 #endif
50
51 #ifndef __USE_XOPEN
52 #define __USE_XOPEN
53 #endif
54
55 #ifndef __USE_SVID
56 #define __USE_SVID
57 #endif
58
59 #ifdef BUILD_KERNEL
60 #include "config_kernel.h"
61 #else
62 #include "config.h"
63 #endif
64
65 #undef ENABLE_SSE_EXCEPTION
66
67 #if defined(SMP_SERVER) || defined(SMP_ONDEMAND)
68 #define SMP
69 #endif
70
71 #if defined(OS_WINNT) || defined(OS_CYGWIN_NT) || defined(OS_INTERIX)
72 #define WINDOWS_ABI
73 #define OS_WINDOWS
74
75 #ifdef DOUBLE
76 #define DOUBLE_DEFINED DOUBLE
77 #undef  DOUBLE
78 #endif
79 #endif
80
81 #if !defined(NOINCLUDE) && !defined(ASSEMBLER)
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85
86 #if !defined(_MSC_VER)
87 #include <unistd.h>
88 #elif _MSC_VER < 1900
89 #define snprintf _snprintf
90 #endif
91 #include <time.h>
92
93 #ifdef OS_LINUX
94 #include <malloc.h>
95 #include <sched.h>
96 #endif
97
98 #if defined(OS_DARWIN) || defined(OS_FREEBSD) || defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLY) || defined(OS_ANDROID)
99 #include <sched.h>
100 #endif
101
102 #ifdef OS_ANDROID
103 #define NO_SYSV_IPC
104 //Android NDK only supports complex.h since Android 5.0
105 #if __ANDROID_API__ < 21
106 #define FORCE_OPENBLAS_COMPLEX_STRUCT
107 #endif
108 #endif
109
110 #ifdef OS_HAIKU
111 #define NO_SYSV_IPC
112 #endif
113
114 #ifdef OS_WINDOWS
115 #ifdef  ATOM
116 #define GOTO_ATOM ATOM
117 #undef  ATOM
118 #endif
119 #include <windows.h>
120 #include <math.h>
121 #ifdef  GOTO_ATOM
122 #define ATOM GOTO_ATOM
123 #undef  GOTO_ATOM
124 #endif
125 #else
126 #include <sys/mman.h>
127 #ifndef NO_SYSV_IPC
128 #include <sys/shm.h>
129 #endif
130 #include <sys/time.h>
131 #include <time.h>
132 #include <unistd.h>
133 #include <math.h>
134 #if defined(SMP) || defined(USE_LOCKING)
135 #include <pthread.h>
136 #endif
137 #endif
138
139 #if defined(OS_SUNOS)
140 #include <thread.h>
141 #endif
142
143 #ifdef __DECC
144 #include <c_asm.h>
145 #include <machine/builtins.h>
146 #endif
147
148 #if defined(ARCH_IA64) && defined(ENABLE_SSE_EXCEPTION)
149 #include <fenv.h>
150 #endif
151
152 #endif
153
154 #if defined(OS_WINDOWS) && defined(DOUBLE_DEFINED)
155 #define DOUBLE DOUBLE_DEFINED
156 #undef DOUBLE_DEFINED
157 #endif
158
159 #undef DEBUG_INFO
160 #define SMP_DEBUG
161 #undef MALLOC_DEBUG
162 #undef SMP_ALLOC_DEBUG
163
164 #ifndef ZERO
165 #ifdef XDOUBLE
166 #define ZERO  0.e0L
167 #elif defined DOUBLE
168 #define ZERO  0.e0
169 #else
170 #define ZERO  0.e0f
171 #endif
172 #endif
173
174 #ifndef ONE
175 #ifdef XDOUBLE
176 #define ONE  1.e0L
177 #elif defined DOUBLE
178 #define ONE  1.e0
179 #else
180 #define ONE  1.e0f
181 #endif
182 #endif
183
184 #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
185
186 #define ALLOCA_ALIGN 63UL
187
188 #define NUM_BUFFERS MAX(50,(MAX_CPU_NUMBER * 2 * MAX_PARALLEL_NUMBER))
189
190 #ifdef NEEDBUNDERSCORE
191 #define BLASFUNC(FUNC) FUNC##_
192 #else
193 #define BLASFUNC(FUNC) FUNC
194 #endif
195
196 #undef  USE_PTHREAD_LOCK
197 #undef  USE_PTHREAD_SPINLOCK
198
199 #if defined(USE_PTHREAD_LOCK) && defined(USE_PTHREAD_SPINLOCK)
200 #error "You can't specify both LOCK operation!"
201 #endif
202
203 #if defined(SMP) || defined(USE_LOCKING)
204 #define USE_PTHREAD_LOCK
205 #undef  USE_PTHREAD_SPINLOCK
206 #endif
207
208 #ifdef OS_WINDOWS
209 #undef  USE_PTHREAD_LOCK
210 #undef  USE_PTHREAD_SPINLOCK
211 #endif
212
213 #if   defined(USE_PTHREAD_LOCK)
214 #define   LOCK_COMMAND(x)   pthread_mutex_lock(x)
215 #define UNLOCK_COMMAND(x)   pthread_mutex_unlock(x)
216 #elif defined(USE_PTHREAD_SPINLOCK)
217 #ifndef ASSEMBLER
218 typedef volatile int pthread_spinlock_t;
219 int pthread_spin_lock (pthread_spinlock_t *__lock);
220 int pthread_spin_unlock (pthread_spinlock_t *__lock);
221 #endif
222 #define   LOCK_COMMAND(x)   pthread_spin_lock(x)
223 #define UNLOCK_COMMAND(x)   pthread_spin_unlock(x)
224 #else
225 #define   LOCK_COMMAND(x)   blas_lock(x)
226 #define UNLOCK_COMMAND(x)   blas_unlock(x)
227 #endif
228
229 #define GOTO_SHMID      0x510510
230
231 #if 0
232 #ifndef __CUDACC__
233 #define __global__
234 #define __device__
235 #define __host__
236 #define __shared__
237 #endif
238 #endif
239
240 #ifndef ASSEMBLER
241
242 #ifdef QUAD_PRECISION
243 typedef struct {
244   unsigned long x[2];
245 }  xdouble;
246 #elif defined EXPRECISION
247 #define xdouble long double
248 #else
249 #define xdouble double
250 #endif
251
252 #if defined(OS_WINDOWS) && defined(__64BIT__)
253 typedef long long BLASLONG;
254 typedef unsigned long long BLASULONG;
255 #else
256 typedef long BLASLONG;
257 typedef unsigned long BLASULONG;
258 #endif
259
260 #ifdef USE64BITINT
261 typedef BLASLONG blasint;
262 #if defined(OS_WINDOWS) && defined(__64BIT__)
263 #define blasabs(x) llabs(x)
264 #else
265 #define blasabs(x) labs(x)
266 #endif
267 #else
268 typedef int blasint;
269 #define blasabs(x) abs(x)
270 #endif
271 #else
272 #ifdef USE64BITINT
273 #define INTSHIFT        3
274 #define INTSIZE         8
275 #else
276 #define INTSHIFT        2
277 #define INTSIZE         4
278 #endif
279 #endif
280
281 #ifdef XDOUBLE
282 #define FLOAT   xdouble
283 #ifdef QUAD_PRECISION
284 #define XFLOAT  xidouble
285 #endif
286 #ifdef QUAD_PRECISION
287 #define SIZE    32
288 #define  BASE_SHIFT 5
289 #define ZBASE_SHIFT 6
290 #else
291 #define SIZE    16
292 #define  BASE_SHIFT 4
293 #define ZBASE_SHIFT 5
294 #endif
295 #elif defined(DOUBLE)
296 #define FLOAT   double
297 #define SIZE    8
298 #define  BASE_SHIFT 3
299 #define ZBASE_SHIFT 4
300 #else
301 #define FLOAT   float
302 #define SIZE    4
303 #define  BASE_SHIFT 2
304 #define ZBASE_SHIFT 3
305 #endif
306
307 #ifndef XFLOAT
308 #define XFLOAT  FLOAT
309 #endif
310
311 #ifndef COMPLEX
312 #define COMPSIZE  1
313 #else
314 #define COMPSIZE  2
315 #endif
316
317
318 #define Address_H(x) (((x)+(1<<15))>>16)
319 #define Address_L(x) ((x)-((Address_H(x))<<16))
320
321 #ifndef MAX_CPU_NUMBER
322 #define MAX_CPU_NUMBER 2
323 #endif
324
325 #if defined(OS_SUNOS)
326 #define YIELDING        thr_yield()
327 #endif
328
329 #if defined(OS_WINDOWS)
330 #if defined(_MSC_VER) && !defined(__clang__)
331 #define YIELDING    YieldProcessor()
332 #else
333 #define YIELDING        SwitchToThread()
334 #endif
335 #endif
336
337 #if defined(ARMV7) || defined(ARMV6) || defined(ARMV8) || defined(ARMV5)
338 #define YIELDING        asm volatile ("nop;nop;nop;nop;nop;nop;nop;nop; \n");
339 #endif
340
341 #ifdef BULLDOZER
342 #ifndef YIELDING
343 #define YIELDING        __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
344 #endif
345 #endif
346
347 #ifdef POWER8
348 #ifndef YIELDING
349 #define YIELDING        __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
350 #endif
351 #endif
352
353 #ifdef POWER9
354 #ifndef YIELDING
355 #define YIELDING        __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
356 #endif
357 #endif
358
359 /*
360 #ifdef PILEDRIVER
361 #ifndef YIELDING
362 #define YIELDING        __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
363 #endif
364 #endif
365 */
366
367 /*
368 #ifdef STEAMROLLER
369 #ifndef YIELDING
370 #define YIELDING        __asm__ __volatile__ ("nop;nop;nop;nop;nop;nop;nop;nop;\n");
371 #endif
372 #endif
373 */
374
375 #ifndef YIELDING
376 #define YIELDING        sched_yield()
377 #endif
378
379 /***
380 To alloc job_t on heap or statck.
381 please https://github.com/xianyi/OpenBLAS/issues/246
382 ***/
383 #if defined(OS_WINDOWS)
384 #define GETRF_MEM_ALLOC_THRESHOLD 32
385 #define BLAS3_MEM_ALLOC_THRESHOLD 32
386 #endif
387
388 #ifndef GETRF_MEM_ALLOC_THRESHOLD
389 #define GETRF_MEM_ALLOC_THRESHOLD 80
390 #endif
391
392 #ifndef BLAS3_MEM_ALLOC_THRESHOLD
393 #define BLAS3_MEM_ALLOC_THRESHOLD 160
394 #endif
395
396 #ifdef QUAD_PRECISION
397 #include "common_quad.h"
398 #endif
399
400 #ifdef ARCH_ALPHA
401 #include "common_alpha.h"
402 #endif
403
404 #ifdef ARCH_X86
405 #include "common_x86.h"
406 #endif
407
408 #ifdef ARCH_X86_64
409 #include "common_x86_64.h"
410 #endif
411
412 #ifdef ARCH_IA64
413 #include "common_ia64.h"
414 #endif
415
416 #ifdef ARCH_POWER
417 #include "common_power.h"
418 #endif
419
420 #ifdef sparc
421 #include "common_sparc.h"
422 #endif
423
424 #ifdef ARCH_MIPS
425 #include "common_mips.h"
426 #endif
427
428 #ifdef ARCH_MIPS64
429 #include "common_mips64.h"
430 #endif
431
432 #ifdef ARCH_ARM
433 #include "common_arm.h"
434 #endif
435
436 #ifdef ARCH_ARM64
437 #include "common_arm64.h"
438 #endif
439
440 #ifdef ARCH_ZARCH
441 #include "common_zarch.h"
442 #endif
443
444 #ifndef ASSEMBLER
445 #ifdef OS_WINDOWSSTORE
446 typedef char env_var_t[MAX_PATH];
447 #define readenv(p, n) 0
448 #else
449 #if defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)
450 typedef char env_var_t[MAX_PATH];
451 #define readenv(p, n) GetEnvironmentVariable((LPCTSTR)(n), (LPTSTR)(p), sizeof(p))
452 #else
453 typedef char* env_var_t;
454 #define readenv(p, n) ((p)=getenv(n))
455 #endif
456 #endif
457
458 #if !defined(RPCC_DEFINED) && !defined(OS_WINDOWS)
459 #ifdef _POSIX_MONOTONIC_CLOCK
460 #if defined(__GLIBC_PREREQ) // cut the if condition if two lines, otherwise will fail at __GLIBC_PREREQ(2, 17)
461 #if __GLIBC_PREREQ(2, 17) // don't require -lrt
462 #define USE_MONOTONIC
463 #endif
464 #elif defined(OS_ANDROID)
465 #define USE_MONOTONIC
466 #endif
467 #endif
468 /* use similar scale as x86 rdtsc for timeouts to work correctly */
469 static inline unsigned long long rpcc(void){
470 #ifdef USE_MONOTONIC
471   struct timespec ts;
472   clock_gettime(CLOCK_MONOTONIC, &ts);
473   return (unsigned long long)ts.tv_sec * 1000000000ull + ts.tv_nsec;
474 #else
475   struct timeval tv;
476   gettimeofday(&tv,NULL);
477   return (unsigned long long)tv.tv_sec * 1000000000ull + tv.tv_usec * 1000;
478 #endif
479 }
480 #define RPCC_DEFINED
481 #define RPCC64BIT
482 #endif // !RPCC_DEFINED
483
484 #if !defined(BLAS_LOCK_DEFINED) && defined(__GNUC__)
485 static void __inline blas_lock(volatile BLASULONG *address){
486
487   do {
488     while (*address) {YIELDING;};
489
490   } while (!__sync_bool_compare_and_swap(address, 0, 1));
491 }
492 #define BLAS_LOCK_DEFINED
493 #endif
494
495 #ifndef RPCC_DEFINED
496 #error "rpcc() implementation is missing for your platform"
497 #endif
498 #ifndef BLAS_LOCK_DEFINED
499 #error "blas_lock() implementation is missing for your platform"
500 #endif
501 #endif // !ASSEMBLER
502
503 #ifdef OS_LINUX
504 #include "common_linux.h"
505 #endif
506
507 #define MMAP_ACCESS (PROT_READ | PROT_WRITE)
508
509 #ifdef __NetBSD__
510 #define MMAP_POLICY (MAP_PRIVATE | MAP_ANON)
511 #else
512 #define MMAP_POLICY (MAP_PRIVATE | MAP_ANONYMOUS)
513 #endif
514
515 #ifndef ASSEMBLER
516 /* C99 supports complex floating numbers natively, which GCC also offers as an
517    extension since version 3.0.  If neither are available, use a compatible
518    structure as fallback (see Clause 6.2.5.13 of the C99 standard). */
519 #if ((defined(__STDC_IEC_559_COMPLEX__) || __STDC_VERSION__ >= 199901L || \
520       (__GNUC__ >= 3 && !defined(__cplusplus))) && !(defined(FORCE_OPENBLAS_COMPLEX_STRUCT))) && !defined(_MSC_VER)
521   #define OPENBLAS_COMPLEX_C99
522   #ifndef __cplusplus
523     #include <complex.h>
524   #endif
525   typedef float _Complex openblas_complex_float;
526   typedef double _Complex openblas_complex_double;
527   typedef xdouble _Complex openblas_complex_xdouble;
528   #define openblas_make_complex_float(real, imag)    ((real) + ((imag) * _Complex_I))
529   #define openblas_make_complex_double(real, imag)   ((real) + ((imag) * _Complex_I))
530   #define openblas_make_complex_xdouble(real, imag)  ((real) + ((imag) * _Complex_I))
531 #else
532   #define OPENBLAS_COMPLEX_STRUCT
533   typedef struct { float real, imag; } openblas_complex_float;
534   typedef struct { double real, imag; } openblas_complex_double;
535   typedef struct { xdouble real, imag; } openblas_complex_xdouble;
536   #define openblas_make_complex_float(real, imag)    {(real), (imag)}
537   #define openblas_make_complex_double(real, imag)   {(real), (imag)}
538   #define openblas_make_complex_xdouble(real, imag)  {(real), (imag)}
539 #endif
540 #endif
541
542 #include "param.h"
543 #include "common_param.h"
544
545 #ifndef STDERR
546 #define STDERR stderr
547 #endif
548
549 #ifndef MASK
550 #define MASK(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
551 #endif
552
553 #if defined(XDOUBLE) || defined(DOUBLE)
554 #define FLOATRET        FLOAT
555 #else
556 #ifdef NEED_F2CCONV
557 #define FLOATRET        double
558 #else
559 #define FLOATRET        float
560 #endif
561 #endif
562
563 #ifndef ASSEMBLER
564 #ifndef NOINCLUDE
565 /* Inclusion of a standard header file is needed for definition of __STDC_*
566    predefined macros with some compilers (e.g. GCC 4.7 on Linux).  This occurs
567    as a side effect of including either <features.h> or <stdc-predef.h>. */
568 #include <stdio.h>
569 #endif  // NOINCLUDE
570
571 #ifdef XDOUBLE
572 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_xdouble
573 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_xdouble(r,i)
574 #elif defined(DOUBLE)
575 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_double
576 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_double(r,i)
577 #else
578 #define OPENBLAS_COMPLEX_FLOAT openblas_complex_float
579 #define OPENBLAS_MAKE_COMPLEX_FLOAT(r,i) openblas_make_complex_float(r,i)
580 #endif
581
582 #if defined(C_PGI) || defined(C_SUN)
583   #if defined(__STDC_IEC_559_COMPLEX__)
584      #define CREAL(X)   creal(X)
585      #define CIMAG(X)   cimag(X)
586   #else
587      #define CREAL(X)   (*((FLOAT *)&X + 0))
588      #define CIMAG(X)   (*((FLOAT *)&X + 1))
589   #endif
590 #else
591 #ifdef OPENBLAS_COMPLEX_STRUCT
592 #define CREAL(Z)        ((Z).real)
593 #define CIMAG(Z)        ((Z).imag)
594 #else
595 #define CREAL   __real__
596 #define CIMAG   __imag__
597 #endif
598 #endif
599
600 #endif  // ASSEMBLER
601
602 #ifndef IFLUSH
603 #define IFLUSH
604 #endif
605
606 #ifndef IFLUSH_HALF
607 #define IFLUSH_HALF
608 #endif
609
610 #if defined(C_GCC) && (( __GNUC__ <= 3) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 2)))
611 #ifdef USE_OPENMP
612 #undef USE_OPENMP
613 #endif
614 #endif
615
616 #if defined(C_MSVC)
617 #define inline __inline
618 #endif
619
620 #ifndef ASSEMBLER
621
622 #ifndef MIN
623 #define MIN(a,b)   (a>b? b:a)
624 #endif
625
626 #ifndef MAX
627 #define MAX(a,b)   (a<b? b:a)
628 #endif
629
630 #define TOUPPER(a) {if ((a) > 0x60) (a) -= 0x20;}
631
632 #if defined(__FreeBSD__) || defined(__APPLE__)
633 #define MAP_ANONYMOUS MAP_ANON
634 #endif
635
636 /* Common Memory Management Routine */
637 void  blas_set_parameter(void);
638 int   blas_get_cpu_number(void);
639 void *blas_memory_alloc  (int);
640 void  blas_memory_free   (void *);
641 void *blas_memory_alloc_nolock  (int); //use malloc without blas_lock
642 void  blas_memory_free_nolock   (void *);
643
644 int  get_num_procs (void);
645
646 #if defined(OS_LINUX) && defined(SMP) && !defined(NO_AFFINITY)
647 int  get_num_nodes (void);
648 int get_num_proc   (int);
649 int get_node_equal (void);
650 #endif
651
652 void goto_set_num_threads(int);
653
654 void gotoblas_affinity_init(void);
655 void gotoblas_affinity_quit(void);
656 void gotoblas_dynamic_init(void);
657 void gotoblas_dynamic_quit(void);
658 void gotoblas_profile_init(void);
659 void gotoblas_profile_quit(void);
660
661 #ifdef USE_OPENMP
662
663 #ifndef C_MSVC
664 int omp_in_parallel(void);
665 int omp_get_num_procs(void);
666 #else
667 __declspec(dllimport) int __cdecl omp_in_parallel(void);
668 __declspec(dllimport) int __cdecl omp_get_num_procs(void);
669 #endif
670
671 #if (__STDC_VERSION__ >= 201112L)
672 #if defined(C_GCC) && ( __GNUC__ < 7) 
673 // workaround for GCC bug 65467
674 #ifndef _Atomic
675 #define _Atomic volatile
676 #endif
677 #endif
678 #include <stdatomic.h>
679 #else
680 #ifndef _Atomic
681 #define _Atomic volatile
682 #endif
683 #endif
684
685 #else
686 #ifdef __ELF__
687 int omp_in_parallel  (void) __attribute__ ((weak));
688 int omp_get_num_procs(void) __attribute__ ((weak));
689 #endif
690 #endif
691
692 static __inline void blas_unlock(volatile BLASULONG *address){
693   MB;
694   *address = 0;
695 }
696
697 #ifdef OS_WINDOWSSTORE
698 static __inline int readenv_atoi(char *env) {
699         return 0;
700 }
701 #else
702 #ifdef OS_WINDOWS
703 static __inline int readenv_atoi(char *env) {
704   env_var_t p;
705   return readenv(p,env) ? 0 : atoi(p);
706 }
707 #else
708 static __inline int readenv_atoi(char *env) {
709   char *p;
710   if (( p = getenv(env) ))
711         return (atoi(p));
712   else
713         return(0);
714 }
715 #endif
716 #endif
717
718 #if !defined(XDOUBLE) || !defined(QUAD_PRECISION)
719
720 static __inline void compinv(FLOAT *b, FLOAT ar, FLOAT ai){
721
722 #ifndef UNIT
723   FLOAT ratio, den;
724
725   if (
726 #ifdef XDOUBLE
727       (fabsl(ar)) >= (fabsl(ai))
728 #elif defined DOUBLE
729       (fabs (ar)) >= (fabs (ai))
730 #else
731       (fabsf(ar)) >= (fabsf(ai))
732 #endif
733       ) {
734     ratio = ai / ar;
735     den   = (FLOAT)(ONE / (ar * (ONE + ratio * ratio)));
736     ar =  den;
737     ai = -ratio * den;
738   } else {
739     ratio = ar / ai;
740     den   = (FLOAT)(ONE /(ai * (ONE + ratio * ratio)));
741     ar =  ratio * den;
742     ai = -den;
743   }
744   b[0] = ar;
745   b[1] = ai;
746 #else
747   b[0] = ONE;
748   b[1] = ZERO;
749 #endif
750
751 }
752 #endif
753
754 #ifdef MALLOC_DEBUG
755 void *blas_debug_alloc(int);
756 void *blas_debug_free(void *);
757 #undef malloc
758 #undef free
759 #define malloc(a) blas_debug_alloc(a)
760 #define free(a)   blas_debug_free (a)
761 #endif
762
763 #ifndef COPYOVERHEAD
764 #define GEMMRETTYPE  int
765 #else
766
767 typedef struct {
768   double outercopy;
769   double innercopy;
770   double kernel;
771   double mflops;
772 } copyoverhead_t;
773
774 #define GEMMRETTYPE  copyoverhead_t
775 #endif
776 #endif
777
778 #ifndef BUILD_KERNEL
779 #define KNAME(A, B) A
780 #else
781 #define KNAME(A, B) A##B
782 #endif
783
784 #include "common_interface.h"
785 #ifdef SANITY_CHECK
786 #include "common_reference.h"
787 #endif
788 #include "common_macro.h"
789 #include "common_level1.h"
790 #include "common_level2.h"
791 #include "common_level3.h"
792 #include "common_lapack.h"
793
794 #ifdef CBLAS
795 # define OPENBLAS_CONST     /* see comment in cblas.h */
796 # include "cblas.h"
797 #endif
798
799 #ifndef ASSEMBLER
800 #include "common_stackalloc.h"
801 #if 0
802 #include "symcopy.h"
803 #endif
804
805 #if defined(SMP_SERVER) && defined(SMP_ONDEMAND)
806 #error Both SMP_SERVER and SMP_ONDEMAND are specified.
807 #endif
808
809 #if defined(SMP_SERVER) || defined(SMP_ONDEMAND)
810 #include "common_thread.h"
811 #endif
812
813 #endif
814
815 #define INFO_NUM 99
816
817 #ifndef DEFAULT_CPU_NUMBER
818 #define DEFAULT_CPU_NUMBER 4
819 #endif
820
821 #ifndef IDEBUG_START
822 #define IDEBUG_START
823 #endif
824
825 #ifndef IDEBUG_END
826 #define IDEBUG_END
827 #endif
828
829 #if !defined(ASSEMBLER) && defined(FUNCTION_PROFILE)
830
831 typedef struct {
832   int func;
833   unsigned long long calls, fops, area, cycles, tcycles;
834 } func_profile_t;
835
836 extern func_profile_t function_profile_table[];
837 extern int gotoblas_profile;
838
839 #ifdef XDOUBLE
840 #define NUMOPT  QNUMOPT
841 #elif defined DOUBLE
842 #define NUMOPT  DNUMOPT
843 #else
844 #define NUMOPT  SNUMOPT
845 #endif
846
847 #define FUNCTION_PROFILE_START() { unsigned long long profile_start = rpcc(), profile_end;
848 #ifdef SMP
849 #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
850         if (gotoblas_profile) { \
851         profile_end = rpcc(); \
852         function_profile_table[PROFILE_FUNC_NAME].calls ++; \
853         function_profile_table[PROFILE_FUNC_NAME].area    += SIZE * COMPSIZE * (AREA); \
854         function_profile_table[PROFILE_FUNC_NAME].fops    += (COMP) * (OPS) / NUMOPT; \
855         function_profile_table[PROFILE_FUNC_NAME].cycles  += (profile_end - profile_start); \
856         function_profile_table[PROFILE_FUNC_NAME].tcycles += blas_cpu_number * (profile_end - profile_start); \
857         } \
858         }
859 #else
860 #define FUNCTION_PROFILE_END(COMP, AREA, OPS) \
861         if (gotoblas_profile) { \
862         profile_end = rpcc(); \
863         function_profile_table[PROFILE_FUNC_NAME].calls ++; \
864         function_profile_table[PROFILE_FUNC_NAME].area    += SIZE * COMPSIZE * (AREA); \
865         function_profile_table[PROFILE_FUNC_NAME].fops    += (COMP) * (OPS) / NUMOPT; \
866         function_profile_table[PROFILE_FUNC_NAME].cycles  += (profile_end - profile_start); \
867         function_profile_table[PROFILE_FUNC_NAME].tcycles += (profile_end - profile_start); \
868         } \
869         }
870 #endif
871
872 #else
873 #define FUNCTION_PROFILE_START()
874 #define FUNCTION_PROFILE_END(COMP, AREA, OPS)
875 #endif
876
877 #if 1
878 #define PRINT_DEBUG_CNAME
879 #define PRINT_DEBUG_NAME
880 #else
881 #define PRINT_DEBUG_CNAME if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_CNAME)
882 #define PRINT_DEBUG_NAME  if (readenv_atoi("GOTO_DEBUG")) fprintf(stderr, "GotoBLAS : %s\n", CHAR_NAME)
883 #endif
884
885 #ifdef __cplusplus
886 }
887
888 #endif  /* __cplusplus */
889
890 #endif