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