You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.h 22 kB

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