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