simde-math.h 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /* SPDX-License-Identifier: MIT
  2. *
  3. * Permission is hereby granted, free of charge, to any person
  4. * obtaining a copy of this software and associated documentation
  5. * files (the "Software"), to deal in the Software without
  6. * restriction, including without limitation the rights to use, copy,
  7. * modify, merge, publish, distribute, sublicense, and/or sell copies
  8. * of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be
  12. * included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Copyright:
  24. * 2017-2020 Evan Nemerson <[email protected]>
  25. */
  26. /* Attempt to find math functions. Functions may be in <cmath>,
  27. * <math.h>, compiler built-ins/intrinsics, or platform/architecture
  28. * specific headers. In some cases, especially those not built in to
  29. * libm, we may need to define our own implementations. */
  30. #if !defined(SIMDE_MATH_H)
  31. #include "hedley.h"
  32. #include "simde-features.h"
  33. #if defined(__has_builtin)
  34. #define SIMDE_MATH_BUILTIN_LIBM(func) __has_builtin(__builtin_##func)
  35. #elif HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  36. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || HEDLEY_GCC_VERSION_CHECK(4, 4, 0)
  37. #define SIMDE_MATH_BUILTIN_LIBM(func) (1)
  38. #else
  39. #define SIMDE_MATH_BUILTIN_LIBM(func) (0)
  40. #endif
  41. #if defined(HUGE_VAL)
  42. /* Looks like <math.h> or <cmath> has already been included. */
  43. /* The math.h from libc++ (yes, the C header from the C++ standard
  44. * library) will define an isnan function, but not an isnan macro
  45. * like the C standard requires. So we detect the header guards
  46. * macro libc++ uses. */
  47. #if defined(isnan) || (defined(_LIBCPP_MATH_H) && !defined(_LIBCPP_CMATH))
  48. #define SIMDE_MATH_HAVE_MATH_H
  49. #elif defined(__cplusplus)
  50. #define SIMDE_MATH_HAVE_CMATH
  51. #endif
  52. #elif defined(__has_include)
  53. #if defined(__cplusplus) && (__cplusplus >= 201103L) && __has_include(<cmath>)
  54. #define SIMDE_MATH_HAVE_CMATH
  55. #include <cmath>
  56. #elif __has_include(<math.h>)
  57. #define SIMDE_MATH_HAVE_MATH_H
  58. #include <math.h>
  59. #elif !defined(SIMDE_MATH_NO_LIBM)
  60. #define SIMDE_MATH_NO_LIBM
  61. #endif
  62. #elif !defined(SIMDE_MATH_NO_LIBM)
  63. #if defined(__cplusplus) && (__cplusplus >= 201103L)
  64. #define SIMDE_MATH_HAVE_CMATH
  65. HEDLEY_DIAGNOSTIC_PUSH
  66. #if defined(HEDLEY_MSVC_VERSION)
  67. /* VS 14 emits this diagnostic about noexcept being used on a
  68. * <cmath> function, which we can't do anything about. */
  69. #pragma warning(disable : 4996)
  70. #endif
  71. #include <cmath>
  72. HEDLEY_DIAGNOSTIC_POP
  73. #else
  74. #define SIMDE_MATH_HAVE_MATH_H
  75. #include <math.h>
  76. #endif
  77. #endif
  78. #if !defined(__cplusplus)
  79. /* If this is a problem we *might* be able to avoid including
  80. * <complex.h> on some compilers (gcc, clang, and others which
  81. * implement builtins like __builtin_cexpf). If you don't have
  82. * a <complex.h> please file an issue and we'll take a look. */
  83. #include <complex.h>
  84. #if !defined(HEDLEY_MSVC_VERSION)
  85. typedef float _Complex simde_cfloat32;
  86. typedef double _Complex simde_cfloat64;
  87. #else
  88. typedef _Fcomplex simde_cfloat32;
  89. typedef _Dcomplex simde_cfloat64;
  90. #endif
  91. #if HEDLEY_HAS_BUILTIN(__builtin_complex) || \
  92. HEDLEY_GCC_VERSION_CHECK(4, 7, 0) || \
  93. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0)
  94. #define SIMDE_MATH_CMPLX(x, y) __builtin_complex((double)(x), (double)(y))
  95. #define SIMDE_MATH_CMPLXF(x, y) __builtin_complex((float)(x), (float)(y))
  96. #elif defined(HEDLEY_MSVC_VERSION)
  97. #define SIMDE_MATH_CMPLX(x, y) ((simde_cfloat64){(x), (y)})
  98. #define SIMDE_MATH_CMPLXF(x, y) ((simde_cfloat32){(x), (y)})
  99. #elif defined(CMPLX) && defined(CMPLXF)
  100. #define SIMDE_MATH_CMPLX(x, y) CMPLX(x, y)
  101. #define SIMDE_MATH_CMPLXF(x, y) CMPLXF(x, y)
  102. #else
  103. /* CMPLX / CMPLXF are in C99, but these seem to be necessary in
  104. * some compilers that aren't even MSVC. */
  105. #define SIMDE_MATH_CMPLX(x, y) \
  106. (HEDLEY_STATIC_CAST(double, x) + HEDLEY_STATIC_CAST(double, y) * I)
  107. #define SIMDE_MATH_CMPLXF(x, y) \
  108. (HEDLEY_STATIC_CAST(float, x) + HEDLEY_STATIC_CAST(float, y) * I)
  109. #endif
  110. #if !defined(simde_math_creal)
  111. #if SIMDE_MATH_BUILTIN_LIBM(creal)
  112. #define simde_math_creal(z) __builtin_creal(z)
  113. #else
  114. #define simde_math_creal(z) creal(z)
  115. #endif
  116. #endif
  117. #if !defined(simde_math_crealf)
  118. #if SIMDE_MATH_BUILTIN_LIBM(crealf)
  119. #define simde_math_crealf(z) __builtin_crealf(z)
  120. #else
  121. #define simde_math_crealf(z) crealf(z)
  122. #endif
  123. #endif
  124. #if !defined(simde_math_cimag)
  125. #if SIMDE_MATH_BUILTIN_LIBM(cimag)
  126. #define simde_math_cimag(z) __builtin_cimag(z)
  127. #else
  128. #define simde_math_cimag(z) cimag(z)
  129. #endif
  130. #endif
  131. #if !defined(simde_math_cimagf)
  132. #if SIMDE_MATH_BUILTIN_LIBM(cimagf)
  133. #define simde_math_cimagf(z) __builtin_cimagf(z)
  134. #else
  135. #define simde_math_cimagf(z) cimagf(z)
  136. #endif
  137. #endif
  138. #else
  139. HEDLEY_DIAGNOSTIC_PUSH
  140. #if defined(HEDLEY_MSVC_VERSION)
  141. #pragma warning(disable : 4530)
  142. #endif
  143. #include <complex>
  144. HEDLEY_DIAGNOSTIC_POP
  145. typedef std::complex<float> simde_cfloat32;
  146. typedef std::complex<double> simde_cfloat64;
  147. #define SIMDE_MATH_CMPLX(x, y) (std::complex<double>(x, y))
  148. #define SIMDE_MATH_CMPLXF(x, y) (std::complex<float>(x, y))
  149. #if !defined(simde_math_creal)
  150. #define simde_math_creal(z) ((z).real())
  151. #endif
  152. #if !defined(simde_math_crealf)
  153. #define simde_math_crealf(z) ((z).real())
  154. #endif
  155. #if !defined(simde_math_cimag)
  156. #define simde_math_cimag(z) ((z).imag())
  157. #endif
  158. #if !defined(simde_math_cimagf)
  159. #define simde_math_cimagf(z) ((z).imag())
  160. #endif
  161. #endif
  162. #if !defined(SIMDE_MATH_INFINITY)
  163. #if HEDLEY_HAS_BUILTIN(__builtin_inf) || HEDLEY_GCC_VERSION_CHECK(3, 3, 0) || \
  164. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  165. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  166. HEDLEY_CRAY_VERSION_CHECK(8, 1, 0)
  167. #define SIMDE_MATH_INFINITY (__builtin_inf())
  168. #elif defined(INFINITY)
  169. #define SIMDE_MATH_INFINITY INFINITY
  170. #endif
  171. #endif
  172. #if !defined(SIMDE_INFINITYF)
  173. #if HEDLEY_HAS_BUILTIN(__builtin_inff) || HEDLEY_GCC_VERSION_CHECK(3, 3, 0) || \
  174. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  175. HEDLEY_CRAY_VERSION_CHECK(8, 1, 0) || \
  176. HEDLEY_IBM_VERSION_CHECK(13, 1, 0)
  177. #define SIMDE_MATH_INFINITYF (__builtin_inff())
  178. #elif defined(INFINITYF)
  179. #define SIMDE_MATH_INFINITYF INFINITYF
  180. #elif defined(SIMDE_MATH_INFINITY)
  181. #define SIMDE_MATH_INFINITYF HEDLEY_STATIC_CAST(float, SIMDE_MATH_INFINITY)
  182. #endif
  183. #endif
  184. #if !defined(SIMDE_MATH_NAN)
  185. #if HEDLEY_HAS_BUILTIN(__builtin_nan) || HEDLEY_GCC_VERSION_CHECK(3, 3, 0) || \
  186. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  187. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  188. HEDLEY_CRAY_VERSION_CHECK(8, 1, 0) || \
  189. HEDLEY_IBM_VERSION_CHECK(13, 1, 0)
  190. #define SIMDE_MATH_NAN (__builtin_nan(""))
  191. #elif defined(NAN)
  192. #define SIMDE_MATH_NAN NAN
  193. #endif
  194. #endif
  195. #if !defined(SIMDE_NANF)
  196. #if HEDLEY_HAS_BUILTIN(__builtin_nanf) || HEDLEY_GCC_VERSION_CHECK(3, 3, 0) || \
  197. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  198. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  199. HEDLEY_CRAY_VERSION_CHECK(8, 1, 0)
  200. #define SIMDE_MATH_NANF (__builtin_nanf(""))
  201. #elif defined(NANF)
  202. #define SIMDE_MATH_NANF NANF
  203. #elif defined(SIMDE_MATH_NAN)
  204. #define SIMDE_MATH_NANF HEDLEY_STATIC_CAST(float, SIMDE_MATH_NAN)
  205. #endif
  206. #endif
  207. #if !defined(SIMDE_MATH_PI)
  208. #if defined(M_PI)
  209. #define SIMDE_MATH_PI M_PI
  210. #else
  211. #define SIMDE_MATH_PI 3.14159265358979323846
  212. #endif
  213. #endif
  214. #if !defined(SIMDE_MATH_PIF)
  215. #if defined(M_PI)
  216. #define SIMDE_MATH_PIF HEDLEY_STATIC_CAST(float, M_PI)
  217. #else
  218. #define SIMDE_MATH_PIF 3.14159265358979323846f
  219. #endif
  220. #endif
  221. #if !defined(SIMDE_MATH_FLT_MIN)
  222. #if defined(FLT_MIN)
  223. #define SIMDE_MATH_FLT_MIN FLT_MIN
  224. #elif defined(__FLT_MIN__)
  225. #define SIMDE_MATH_FLT_MIN __FLT_MIN__
  226. #elif defined(__cplusplus)
  227. #include <cfloat>
  228. #define SIMDE_MATH_FLT_MIN FLT_MIN
  229. #else
  230. #include <float.h>
  231. #define SIMDE_MATH_FLT_MIN FLT_MIN
  232. #endif
  233. #endif
  234. #if !defined(SIMDE_MATH_DBL_MIN)
  235. #if defined(DBL_MIN)
  236. #define SIMDE_MATH_DBL_MIN DBL_MIN
  237. #elif defined(__DBL_MIN__)
  238. #define SIMDE_MATH_DBL_MIN __DBL_MIN__
  239. #elif defined(__cplusplus)
  240. #include <cfloat>
  241. #define SIMDE_MATH_DBL_MIN DBL_MIN
  242. #else
  243. #include <float.h>
  244. #define SIMDE_MATH_DBL_MIN DBL_MIN
  245. #endif
  246. #endif
  247. /*** Classification macros from C99 ***/
  248. #if !defined(simde_math_isinf)
  249. #if SIMDE_MATH_BUILTIN_LIBM(isinf)
  250. #define simde_math_isinf(v) __builtin_isinf(v)
  251. #elif defined(isinf) || defined(SIMDE_MATH_HAVE_MATH_H)
  252. #define simde_math_isinf(v) isinf(v)
  253. #elif defined(SIMDE_MATH_HAVE_CMATH)
  254. #define simde_math_isinf(v) std::isinf(v)
  255. #endif
  256. #endif
  257. #if !defined(simde_math_isinff)
  258. #if HEDLEY_HAS_BUILTIN(__builtin_isinff) || \
  259. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  260. HEDLEY_ARM_VERSION_CHECK(4, 1, 0)
  261. #define simde_math_isinff(v) __builtin_isinff(v)
  262. #elif defined(SIMDE_MATH_HAVE_CMATH)
  263. #define simde_math_isinff(v) std::isinf(v)
  264. #elif defined(simde_math_isinf)
  265. #define simde_math_isinff(v) simde_math_isinf(HEDLEY_STATIC_CAST(double, v))
  266. #endif
  267. #endif
  268. #if !defined(simde_math_isnan)
  269. #if SIMDE_MATH_BUILTIN_LIBM(isnan)
  270. #define simde_math_isnan(v) __builtin_isnan(v)
  271. #elif defined(isnan) || defined(SIMDE_MATH_HAVE_MATH_H)
  272. #define simde_math_isnan(v) isnan(v)
  273. #elif defined(SIMDE_MATH_HAVE_CMATH)
  274. #define simde_math_isnan(v) std::isnan(v)
  275. #endif
  276. #endif
  277. #if !defined(simde_math_isnanf)
  278. #if HEDLEY_HAS_BUILTIN(__builtin_isnanf) || \
  279. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  280. HEDLEY_ARM_VERSION_CHECK(4, 1, 0)
  281. /* XL C/C++ has __builtin_isnan but not __builtin_isnanf */
  282. #define simde_math_isnanf(v) __builtin_isnanf(v)
  283. #elif defined(SIMDE_MATH_HAVE_CMATH)
  284. #define simde_math_isnanf(v) std::isnan(v)
  285. #elif defined(simde_math_isnan)
  286. #define simde_math_isnanf(v) simde_math_isnan(HEDLEY_STATIC_CAST(double, v))
  287. #endif
  288. #endif
  289. #if !defined(simde_math_isnormal)
  290. #if SIMDE_MATH_BUILTIN_LIBM(isnormal)
  291. #define simde_math_isnormal(v) __builtin_isnormal(v)
  292. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  293. #define simde_math_isnormal(v) isnormal(v)
  294. #elif defined(SIMDE_MATH_HAVE_CMATH)
  295. #define simde_math_isnormal(v) std::isnormal(v)
  296. #endif
  297. #endif
  298. #if !defined(simde_math_isnormalf)
  299. #if HEDLEY_HAS_BUILTIN(__builtin_isnormalf)
  300. #define simde_math_isnormalf(v) __builtin_isnormalf(v)
  301. #elif SIMDE_MATH_BUILTIN_LIBM(isnormal)
  302. #define simde_math_isnormalf(v) __builtin_isnormal(v)
  303. #elif defined(isnormalf)
  304. #define simde_math_isnormalf(v) isnormalf(v)
  305. #elif defined(isnormal) || defined(SIMDE_MATH_HAVE_MATH_H)
  306. #define simde_math_isnormalf(v) isnormal(v)
  307. #elif defined(SIMDE_MATH_HAVE_CMATH)
  308. #define simde_math_isnormalf(v) std::isnormal(v)
  309. #elif defined(simde_math_isnormal)
  310. #define simde_math_isnormalf(v) simde_math_isnormal(v)
  311. #endif
  312. #endif
  313. /*** Functions from C99 ***/
  314. #if !defined(simde_math_abs)
  315. #if SIMDE_MATH_BUILTIN_LIBM(abs)
  316. #define simde_math_abs(v) __builtin_abs(v)
  317. #elif defined(SIMDE_MATH_HAVE_CMATH)
  318. #define simde_math_abs(v) std::abs(v)
  319. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  320. #define simde_math_abs(v) abs(v)
  321. #endif
  322. #endif
  323. #if !defined(simde_math_absf)
  324. #if SIMDE_MATH_BUILTIN_LIBM(absf)
  325. #define simde_math_absf(v) __builtin_absf(v)
  326. #elif defined(SIMDE_MATH_HAVE_CMATH)
  327. #define simde_math_absf(v) std::abs(v)
  328. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  329. #define simde_math_absf(v) absf(v)
  330. #endif
  331. #endif
  332. #if !defined(simde_math_acos)
  333. #if SIMDE_MATH_BUILTIN_LIBM(acos)
  334. #define simde_math_acos(v) __builtin_acos(v)
  335. #elif defined(SIMDE_MATH_HAVE_CMATH)
  336. #define simde_math_acos(v) std::acos(v)
  337. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  338. #define simde_math_acos(v) acos(v)
  339. #endif
  340. #endif
  341. #if !defined(simde_math_acosf)
  342. #if SIMDE_MATH_BUILTIN_LIBM(acosf)
  343. #define simde_math_acosf(v) __builtin_acosf(v)
  344. #elif defined(SIMDE_MATH_HAVE_CMATH)
  345. #define simde_math_acosf(v) std::acos(v)
  346. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  347. #define simde_math_acosf(v) acosf(v)
  348. #endif
  349. #endif
  350. #if !defined(simde_math_acosh)
  351. #if SIMDE_MATH_BUILTIN_LIBM(acosh)
  352. #define simde_math_acosh(v) __builtin_acosh(v)
  353. #elif defined(SIMDE_MATH_HAVE_CMATH)
  354. #define simde_math_acosh(v) std::acosh(v)
  355. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  356. #define simde_math_acosh(v) acosh(v)
  357. #endif
  358. #endif
  359. #if !defined(simde_math_acoshf)
  360. #if SIMDE_MATH_BUILTIN_LIBM(acoshf)
  361. #define simde_math_acoshf(v) __builtin_acoshf(v)
  362. #elif defined(SIMDE_MATH_HAVE_CMATH)
  363. #define simde_math_acoshf(v) std::acosh(v)
  364. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  365. #define simde_math_acoshf(v) acoshf(v)
  366. #endif
  367. #endif
  368. #if !defined(simde_math_asin)
  369. #if SIMDE_MATH_BUILTIN_LIBM(asin)
  370. #define simde_math_asin(v) __builtin_asin(v)
  371. #elif defined(SIMDE_MATH_HAVE_CMATH)
  372. #define simde_math_asin(v) std::asin(v)
  373. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  374. #define simde_math_asin(v) asin(v)
  375. #endif
  376. #endif
  377. #if !defined(simde_math_asinf)
  378. #if SIMDE_MATH_BUILTIN_LIBM(asinf)
  379. #define simde_math_asinf(v) __builtin_asinf(v)
  380. #elif defined(SIMDE_MATH_HAVE_CMATH)
  381. #define simde_math_asinf(v) std::asin(v)
  382. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  383. #define simde_math_asinf(v) asinf(v)
  384. #endif
  385. #endif
  386. #if !defined(simde_math_asinh)
  387. #if SIMDE_MATH_BUILTIN_LIBM(asinh)
  388. #define simde_math_asinh(v) __builtin_asinh(v)
  389. #elif defined(SIMDE_MATH_HAVE_CMATH)
  390. #define simde_math_asinh(v) std::asinh(v)
  391. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  392. #define simde_math_asinh(v) asinh(v)
  393. #endif
  394. #endif
  395. #if !defined(simde_math_asinhf)
  396. #if SIMDE_MATH_BUILTIN_LIBM(asinhf)
  397. #define simde_math_asinhf(v) __builtin_asinhf(v)
  398. #elif defined(SIMDE_MATH_HAVE_CMATH)
  399. #define simde_math_asinhf(v) std::asinh(v)
  400. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  401. #define simde_math_asinhf(v) asinhf(v)
  402. #endif
  403. #endif
  404. #if !defined(simde_math_atan)
  405. #if SIMDE_MATH_BUILTIN_LIBM(atan)
  406. #define simde_math_atan(v) __builtin_atan(v)
  407. #elif defined(SIMDE_MATH_HAVE_CMATH)
  408. #define simde_math_atan(v) std::atan(v)
  409. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  410. #define simde_math_atan(v) atan(v)
  411. #endif
  412. #endif
  413. #if !defined(simde_math_atan2)
  414. #if SIMDE_MATH_BUILTIN_LIBM(atan2)
  415. #define simde_math_atan2(y, x) __builtin_atan2(y, x)
  416. #elif defined(SIMDE_MATH_HAVE_CMATH)
  417. #define simde_math_atan2(y, x) std::atan2(y, x)
  418. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  419. #define simde_math_atan2(y, x) atan2(y, x)
  420. #endif
  421. #endif
  422. #if !defined(simde_math_atan2f)
  423. #if SIMDE_MATH_BUILTIN_LIBM(atan2f)
  424. #define simde_math_atan2f(y, x) __builtin_atan2f(y, x)
  425. #elif defined(SIMDE_MATH_HAVE_CMATH)
  426. #define simde_math_atan2f(y, x) std::atan2(y, x)
  427. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  428. #define simde_math_atan2f(y, x) atan2f(y, x)
  429. #endif
  430. #endif
  431. #if !defined(simde_math_atanf)
  432. #if SIMDE_MATH_BUILTIN_LIBM(atanf)
  433. #define simde_math_atanf(v) __builtin_atanf(v)
  434. #elif defined(SIMDE_MATH_HAVE_CMATH)
  435. #define simde_math_atanf(v) std::atan(v)
  436. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  437. #define simde_math_atanf(v) atanf(v)
  438. #endif
  439. #endif
  440. #if !defined(simde_math_atanh)
  441. #if SIMDE_MATH_BUILTIN_LIBM(atanh)
  442. #define simde_math_atanh(v) __builtin_atanh(v)
  443. #elif defined(SIMDE_MATH_HAVE_CMATH)
  444. #define simde_math_atanh(v) std::atanh(v)
  445. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  446. #define simde_math_atanh(v) atanh(v)
  447. #endif
  448. #endif
  449. #if !defined(simde_math_atanhf)
  450. #if SIMDE_MATH_BUILTIN_LIBM(atanhf)
  451. #define simde_math_atanhf(v) __builtin_atanhf(v)
  452. #elif defined(SIMDE_MATH_HAVE_CMATH)
  453. #define simde_math_atanhf(v) std::atanh(v)
  454. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  455. #define simde_math_atanhf(v) atanhf(v)
  456. #endif
  457. #endif
  458. #if !defined(simde_math_cbrt)
  459. #if SIMDE_MATH_BUILTIN_LIBM(cbrt)
  460. #define simde_math_cbrt(v) __builtin_cbrt(v)
  461. #elif defined(SIMDE_MATH_HAVE_CMATH)
  462. #define simde_math_cbrt(v) std::cbrt(v)
  463. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  464. #define simde_math_cbrt(v) cbrt(v)
  465. #endif
  466. #endif
  467. #if !defined(simde_math_cbrtf)
  468. #if SIMDE_MATH_BUILTIN_LIBM(cbrtf)
  469. #define simde_math_cbrtf(v) __builtin_cbrtf(v)
  470. #elif defined(SIMDE_MATH_HAVE_CMATH)
  471. #define simde_math_cbrtf(v) std::cbrt(v)
  472. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  473. #define simde_math_cbrtf(v) cbrtf(v)
  474. #endif
  475. #endif
  476. #if !defined(simde_math_ceil)
  477. #if SIMDE_MATH_BUILTIN_LIBM(ceil)
  478. #define simde_math_ceil(v) __builtin_ceil(v)
  479. #elif defined(SIMDE_MATH_HAVE_CMATH)
  480. #define simde_math_ceil(v) std::ceil(v)
  481. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  482. #define simde_math_ceil(v) ceil(v)
  483. #endif
  484. #endif
  485. #if !defined(simde_math_ceilf)
  486. #if SIMDE_MATH_BUILTIN_LIBM(ceilf)
  487. #define simde_math_ceilf(v) __builtin_ceilf(v)
  488. #elif defined(SIMDE_MATH_HAVE_CMATH)
  489. #define simde_math_ceilf(v) std::ceil(v)
  490. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  491. #define simde_math_ceilf(v) ceilf(v)
  492. #endif
  493. #endif
  494. #if !defined(simde_math_copysign)
  495. #if SIMDE_MATH_BUILTIN_LIBM(copysign)
  496. #define simde_math_copysign(x, y) __builtin_copysign(x, y)
  497. #elif defined(SIMDE_MATH_HAVE_CMATH)
  498. #define simde_math_copysign(x, y) std::copysign(x, y)
  499. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  500. #define simde_math_copysign(x, y) copysign(x, y)
  501. #endif
  502. #endif
  503. #if !defined(simde_math_copysignf)
  504. #if SIMDE_MATH_BUILTIN_LIBM(copysignf)
  505. #define simde_math_copysignf(x, y) __builtin_copysignf(x, y)
  506. #elif defined(SIMDE_MATH_HAVE_CMATH)
  507. #define simde_math_copysignf(x, y) std::copysignf(x, y)
  508. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  509. #define simde_math_copysignf(x, y) copysignf(x, y)
  510. #endif
  511. #endif
  512. #if !defined(simde_math_cos)
  513. #if SIMDE_MATH_BUILTIN_LIBM(cos)
  514. #define simde_math_cos(v) __builtin_cos(v)
  515. #elif defined(SIMDE_MATH_HAVE_CMATH)
  516. #define simde_math_cos(v) std::cos(v)
  517. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  518. #define simde_math_cos(v) cos(v)
  519. #endif
  520. #endif
  521. #if !defined(simde_math_cosf)
  522. #if SIMDE_MATH_BUILTIN_LIBM(cosf)
  523. #define simde_math_cosf(v) __builtin_cosf(v)
  524. #elif defined(SIMDE_MATH_HAVE_CMATH)
  525. #define simde_math_cosf(v) std::cos(v)
  526. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  527. #define simde_math_cosf(v) cosf(v)
  528. #endif
  529. #endif
  530. #if !defined(simde_math_cosh)
  531. #if SIMDE_MATH_BUILTIN_LIBM(cosh)
  532. #define simde_math_cosh(v) __builtin_cosh(v)
  533. #elif defined(SIMDE_MATH_HAVE_CMATH)
  534. #define simde_math_cosh(v) std::cosh(v)
  535. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  536. #define simde_math_cosh(v) cosh(v)
  537. #endif
  538. #endif
  539. #if !defined(simde_math_coshf)
  540. #if SIMDE_MATH_BUILTIN_LIBM(coshf)
  541. #define simde_math_coshf(v) __builtin_coshf(v)
  542. #elif defined(SIMDE_MATH_HAVE_CMATH)
  543. #define simde_math_coshf(v) std::cosh(v)
  544. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  545. #define simde_math_coshf(v) coshf(v)
  546. #endif
  547. #endif
  548. #if !defined(simde_math_erf)
  549. #if SIMDE_MATH_BUILTIN_LIBM(erf)
  550. #define simde_math_erf(v) __builtin_erf(v)
  551. #elif defined(SIMDE_MATH_HAVE_CMATH)
  552. #define simde_math_erf(v) std::erf(v)
  553. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  554. #define simde_math_erf(v) erf(v)
  555. #endif
  556. #endif
  557. #if !defined(simde_math_erff)
  558. #if SIMDE_MATH_BUILTIN_LIBM(erff)
  559. #define simde_math_erff(v) __builtin_erff(v)
  560. #elif defined(SIMDE_MATH_HAVE_CMATH)
  561. #define simde_math_erff(v) std::erf(v)
  562. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  563. #define simde_math_erff(v) erff(v)
  564. #endif
  565. #endif
  566. #if !defined(simde_math_erfc)
  567. #if SIMDE_MATH_BUILTIN_LIBM(erfc)
  568. #define simde_math_erfc(v) __builtin_erfc(v)
  569. #elif defined(SIMDE_MATH_HAVE_CMATH)
  570. #define simde_math_erfc(v) std::erfc(v)
  571. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  572. #define simde_math_erfc(v) erfc(v)
  573. #endif
  574. #endif
  575. #if !defined(simde_math_erfcf)
  576. #if SIMDE_MATH_BUILTIN_LIBM(erfcf)
  577. #define simde_math_erfcf(v) __builtin_erfcf(v)
  578. #elif defined(SIMDE_MATH_HAVE_CMATH)
  579. #define simde_math_erfcf(v) std::erfc(v)
  580. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  581. #define simde_math_erfcf(v) erfcf(v)
  582. #endif
  583. #endif
  584. #if !defined(simde_math_exp)
  585. #if SIMDE_MATH_BUILTIN_LIBM(exp)
  586. #define simde_math_exp(v) __builtin_exp(v)
  587. #elif defined(SIMDE_MATH_HAVE_CMATH)
  588. #define simde_math_exp(v) std::exp(v)
  589. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  590. #define simde_math_exp(v) exp(v)
  591. #endif
  592. #endif
  593. #if !defined(simde_math_expf)
  594. #if SIMDE_MATH_BUILTIN_LIBM(expf)
  595. #define simde_math_expf(v) __builtin_expf(v)
  596. #elif defined(SIMDE_MATH_HAVE_CMATH)
  597. #define simde_math_expf(v) std::exp(v)
  598. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  599. #define simde_math_expf(v) expf(v)
  600. #endif
  601. #endif
  602. #if !defined(simde_math_expm1)
  603. #if SIMDE_MATH_BUILTIN_LIBM(expm1)
  604. #define simde_math_expm1(v) __builtin_expm1(v)
  605. #elif defined(SIMDE_MATH_HAVE_CMATH)
  606. #define simde_math_expm1(v) std::expm1(v)
  607. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  608. #define simde_math_expm1(v) expm1(v)
  609. #endif
  610. #endif
  611. #if !defined(simde_math_expm1f)
  612. #if SIMDE_MATH_BUILTIN_LIBM(expm1f)
  613. #define simde_math_expm1f(v) __builtin_expm1f(v)
  614. #elif defined(SIMDE_MATH_HAVE_CMATH)
  615. #define simde_math_expm1f(v) std::expm1(v)
  616. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  617. #define simde_math_expm1f(v) expm1f(v)
  618. #endif
  619. #endif
  620. #if !defined(simde_math_exp2)
  621. #if SIMDE_MATH_BUILTIN_LIBM(exp2)
  622. #define simde_math_exp2(v) __builtin_exp2(v)
  623. #elif defined(SIMDE_MATH_HAVE_CMATH)
  624. #define simde_math_exp2(v) std::exp2(v)
  625. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  626. #define simde_math_exp2(v) exp2(v)
  627. #endif
  628. #endif
  629. #if !defined(simde_math_exp2f)
  630. #if SIMDE_MATH_BUILTIN_LIBM(exp2f)
  631. #define simde_math_exp2f(v) __builtin_exp2f(v)
  632. #elif defined(SIMDE_MATH_HAVE_CMATH)
  633. #define simde_math_exp2f(v) std::exp2(v)
  634. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  635. #define simde_math_exp2f(v) exp2f(v)
  636. #endif
  637. #endif
  638. #if HEDLEY_HAS_BUILTIN(__builtin_exp10) || HEDLEY_GCC_VERSION_CHECK(3, 4, 0)
  639. #define simde_math_exp10(v) __builtin_exp10(v)
  640. #else
  641. #define simde_math_exp10(v) pow(10.0, (v))
  642. #endif
  643. #if HEDLEY_HAS_BUILTIN(__builtin_exp10f) || HEDLEY_GCC_VERSION_CHECK(3, 4, 0)
  644. #define simde_math_exp10f(v) __builtin_exp10f(v)
  645. #else
  646. #define simde_math_exp10f(v) powf(10.0f, (v))
  647. #endif
  648. #if !defined(simde_math_fabs)
  649. #if SIMDE_MATH_BUILTIN_LIBM(fabs)
  650. #define simde_math_fabs(v) __builtin_fabs(v)
  651. #elif defined(SIMDE_MATH_HAVE_CMATH)
  652. #define simde_math_fabs(v) std::fabs(v)
  653. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  654. #define simde_math_fabs(v) fabs(v)
  655. #endif
  656. #endif
  657. #if !defined(simde_math_fabsf)
  658. #if SIMDE_MATH_BUILTIN_LIBM(fabsf)
  659. #define simde_math_fabsf(v) __builtin_fabsf(v)
  660. #elif defined(SIMDE_MATH_HAVE_CMATH)
  661. #define simde_math_fabsf(v) std::fabs(v)
  662. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  663. #define simde_math_fabsf(v) fabsf(v)
  664. #endif
  665. #endif
  666. #if !defined(simde_math_floor)
  667. #if SIMDE_MATH_BUILTIN_LIBM(floor)
  668. #define simde_math_floor(v) __builtin_floor(v)
  669. #elif defined(SIMDE_MATH_HAVE_CMATH)
  670. #define simde_math_floor(v) std::floor(v)
  671. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  672. #define simde_math_floor(v) floor(v)
  673. #endif
  674. #endif
  675. #if !defined(simde_math_floorf)
  676. #if SIMDE_MATH_BUILTIN_LIBM(floorf)
  677. #define simde_math_floorf(v) __builtin_floorf(v)
  678. #elif defined(SIMDE_MATH_HAVE_CMATH)
  679. #define simde_math_floorf(v) std::floor(v)
  680. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  681. #define simde_math_floorf(v) floorf(v)
  682. #endif
  683. #endif
  684. #if !defined(simde_math_hypot)
  685. #if SIMDE_MATH_BUILTIN_LIBM(hypot)
  686. #define simde_math_hypot(y, x) __builtin_hypot(y, x)
  687. #elif defined(SIMDE_MATH_HAVE_CMATH)
  688. #define simde_math_hypot(y, x) std::hypot(y, x)
  689. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  690. #define simde_math_hypot(y, x) hypot(y, x)
  691. #endif
  692. #endif
  693. #if !defined(simde_math_hypotf)
  694. #if SIMDE_MATH_BUILTIN_LIBM(hypotf)
  695. #define simde_math_hypotf(y, x) __builtin_hypotf(y, x)
  696. #elif defined(SIMDE_MATH_HAVE_CMATH)
  697. #define simde_math_hypotf(y, x) std::hypot(y, x)
  698. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  699. #define simde_math_hypotf(y, x) hypotf(y, x)
  700. #endif
  701. #endif
  702. #if !defined(simde_math_log)
  703. #if SIMDE_MATH_BUILTIN_LIBM(log)
  704. #define simde_math_log(v) __builtin_log(v)
  705. #elif defined(SIMDE_MATH_HAVE_CMATH)
  706. #define simde_math_log(v) std::log(v)
  707. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  708. #define simde_math_log(v) log(v)
  709. #endif
  710. #endif
  711. #if !defined(simde_math_logf)
  712. #if SIMDE_MATH_BUILTIN_LIBM(logf)
  713. #define simde_math_logf(v) __builtin_logf(v)
  714. #elif defined(SIMDE_MATH_HAVE_CMATH)
  715. #define simde_math_logf(v) std::log(v)
  716. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  717. #define simde_math_logf(v) logf(v)
  718. #endif
  719. #endif
  720. #if !defined(simde_math_logb)
  721. #if SIMDE_MATH_BUILTIN_LIBM(logb)
  722. #define simde_math_logb(v) __builtin_logb(v)
  723. #elif defined(SIMDE_MATH_HAVE_CMATH)
  724. #define simde_math_logb(v) std::logb(v)
  725. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  726. #define simde_math_logb(v) logb(v)
  727. #endif
  728. #endif
  729. #if !defined(simde_math_logbf)
  730. #if SIMDE_MATH_BUILTIN_LIBM(logbf)
  731. #define simde_math_logbf(v) __builtin_logbf(v)
  732. #elif defined(SIMDE_MATH_HAVE_CMATH)
  733. #define simde_math_logbf(v) std::logb(v)
  734. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  735. #define simde_math_logbf(v) logbf(v)
  736. #endif
  737. #endif
  738. #if !defined(simde_math_log1p)
  739. #if SIMDE_MATH_BUILTIN_LIBM(log1p)
  740. #define simde_math_log1p(v) __builtin_log1p(v)
  741. #elif defined(SIMDE_MATH_HAVE_CMATH)
  742. #define simde_math_log1p(v) std::log1p(v)
  743. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  744. #define simde_math_log1p(v) log1p(v)
  745. #endif
  746. #endif
  747. #if !defined(simde_math_log1pf)
  748. #if SIMDE_MATH_BUILTIN_LIBM(log1pf)
  749. #define simde_math_log1pf(v) __builtin_log1pf(v)
  750. #elif defined(SIMDE_MATH_HAVE_CMATH)
  751. #define simde_math_log1pf(v) std::log1p(v)
  752. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  753. #define simde_math_log1pf(v) log1pf(v)
  754. #endif
  755. #endif
  756. #if !defined(simde_math_log2)
  757. #if SIMDE_MATH_BUILTIN_LIBM(log2)
  758. #define simde_math_log2(v) __builtin_log2(v)
  759. #elif defined(SIMDE_MATH_HAVE_CMATH)
  760. #define simde_math_log2(v) std::log2(v)
  761. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  762. #define simde_math_log2(v) log2(v)
  763. #endif
  764. #endif
  765. #if !defined(simde_math_log2f)
  766. #if SIMDE_MATH_BUILTIN_LIBM(log2f)
  767. #define simde_math_log2f(v) __builtin_log2f(v)
  768. #elif defined(SIMDE_MATH_HAVE_CMATH)
  769. #define simde_math_log2f(v) std::log2(v)
  770. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  771. #define simde_math_log2f(v) log2f(v)
  772. #endif
  773. #endif
  774. #if !defined(simde_math_log10)
  775. #if SIMDE_MATH_BUILTIN_LIBM(log10)
  776. #define simde_math_log10(v) __builtin_log10(v)
  777. #elif defined(SIMDE_MATH_HAVE_CMATH)
  778. #define simde_math_log10(v) std::log10(v)
  779. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  780. #define simde_math_log10(v) log10(v)
  781. #endif
  782. #endif
  783. #if !defined(simde_math_log10f)
  784. #if SIMDE_MATH_BUILTIN_LIBM(log10f)
  785. #define simde_math_log10f(v) __builtin_log10f(v)
  786. #elif defined(SIMDE_MATH_HAVE_CMATH)
  787. #define simde_math_log10f(v) std::log10(v)
  788. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  789. #define simde_math_log10f(v) log10f(v)
  790. #endif
  791. #endif
  792. #if !defined(simde_math_nearbyint)
  793. #if SIMDE_MATH_BUILTIN_LIBM(nearbyint)
  794. #define simde_math_nearbyint(v) __builtin_nearbyint(v)
  795. #elif defined(SIMDE_MATH_HAVE_CMATH)
  796. #define simde_math_nearbyint(v) std::nearbyint(v)
  797. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  798. #define simde_math_nearbyint(v) nearbyint(v)
  799. #endif
  800. #endif
  801. #if !defined(simde_math_nearbyintf)
  802. #if SIMDE_MATH_BUILTIN_LIBM(nearbyintf)
  803. #define simde_math_nearbyintf(v) __builtin_nearbyintf(v)
  804. #elif defined(SIMDE_MATH_HAVE_CMATH)
  805. #define simde_math_nearbyintf(v) std::nearbyint(v)
  806. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  807. #define simde_math_nearbyintf(v) nearbyintf(v)
  808. #endif
  809. #endif
  810. #if !defined(simde_math_pow)
  811. #if SIMDE_MATH_BUILTIN_LIBM(pow)
  812. #define simde_math_pow(y, x) __builtin_pow(y, x)
  813. #elif defined(SIMDE_MATH_HAVE_CMATH)
  814. #define simde_math_pow(y, x) std::pow(y, x)
  815. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  816. #define simde_math_pow(y, x) pow(y, x)
  817. #endif
  818. #endif
  819. #if !defined(simde_math_powf)
  820. #if SIMDE_MATH_BUILTIN_LIBM(powf)
  821. #define simde_math_powf(y, x) __builtin_powf(y, x)
  822. #elif defined(SIMDE_MATH_HAVE_CMATH)
  823. #define simde_math_powf(y, x) std::pow(y, x)
  824. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  825. #define simde_math_powf(y, x) powf(y, x)
  826. #endif
  827. #endif
  828. #if !defined(simde_math_rint)
  829. #if SIMDE_MATH_BUILTIN_LIBM(rint)
  830. #define simde_math_rint(v) __builtin_rint(v)
  831. #elif defined(SIMDE_MATH_HAVE_CMATH)
  832. #define simde_math_rint(v) std::rint(v)
  833. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  834. #define simde_math_rint(v) rint(v)
  835. #endif
  836. #endif
  837. #if !defined(simde_math_rintf)
  838. #if SIMDE_MATH_BUILTIN_LIBM(rintf)
  839. #define simde_math_rintf(v) __builtin_rintf(v)
  840. #elif defined(SIMDE_MATH_HAVE_CMATH)
  841. #define simde_math_rintf(v) std::rint(v)
  842. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  843. #define simde_math_rintf(v) rintf(v)
  844. #endif
  845. #endif
  846. #if !defined(simde_math_round)
  847. #if SIMDE_MATH_BUILTIN_LIBM(round)
  848. #define simde_math_round(v) __builtin_round(v)
  849. #elif defined(SIMDE_MATH_HAVE_CMATH)
  850. #define simde_math_round(v) std::round(v)
  851. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  852. #define simde_math_round(v) round(v)
  853. #endif
  854. #endif
  855. #if !defined(simde_math_roundf)
  856. #if SIMDE_MATH_BUILTIN_LIBM(roundf)
  857. #define simde_math_roundf(v) __builtin_roundf(v)
  858. #elif defined(SIMDE_MATH_HAVE_CMATH)
  859. #define simde_math_roundf(v) std::round(v)
  860. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  861. #define simde_math_roundf(v) roundf(v)
  862. #endif
  863. #endif
  864. #if !defined(simde_math_sin)
  865. #if SIMDE_MATH_BUILTIN_LIBM(sin)
  866. #define simde_math_sin(v) __builtin_sin(v)
  867. #elif defined(SIMDE_MATH_HAVE_CMATH)
  868. #define simde_math_sin(v) std::sin(v)
  869. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  870. #define simde_math_sin(v) sin(v)
  871. #endif
  872. #endif
  873. #if !defined(simde_math_sinf)
  874. #if SIMDE_MATH_BUILTIN_LIBM(sinf)
  875. #define simde_math_sinf(v) __builtin_sinf(v)
  876. #elif defined(SIMDE_MATH_HAVE_CMATH)
  877. #define simde_math_sinf(v) std::sin(v)
  878. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  879. #define simde_math_sinf(v) sinf(v)
  880. #endif
  881. #endif
  882. #if !defined(simde_math_sinh)
  883. #if SIMDE_MATH_BUILTIN_LIBM(sinh)
  884. #define simde_math_sinh(v) __builtin_sinh(v)
  885. #elif defined(SIMDE_MATH_HAVE_CMATH)
  886. #define simde_math_sinh(v) std::sinh(v)
  887. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  888. #define simde_math_sinh(v) sinh(v)
  889. #endif
  890. #endif
  891. #if !defined(simde_math_sinhf)
  892. #if SIMDE_MATH_BUILTIN_LIBM(sinhf)
  893. #define simde_math_sinhf(v) __builtin_sinhf(v)
  894. #elif defined(SIMDE_MATH_HAVE_CMATH)
  895. #define simde_math_sinhf(v) std::sinh(v)
  896. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  897. #define simde_math_sinhf(v) sinhf(v)
  898. #endif
  899. #endif
  900. #if !defined(simde_math_sqrt)
  901. #if SIMDE_MATH_BUILTIN_LIBM(sqrt)
  902. #define simde_math_sqrt(v) __builtin_sqrt(v)
  903. #elif defined(SIMDE_MATH_HAVE_CMATH)
  904. #define simde_math_sqrt(v) std::sqrt(v)
  905. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  906. #define simde_math_sqrt(v) sqrt(v)
  907. #endif
  908. #endif
  909. #if !defined(simde_math_sqrtf)
  910. #if SIMDE_MATH_BUILTIN_LIBM(sqrtf)
  911. #define simde_math_sqrtf(v) __builtin_sqrtf(v)
  912. #elif defined(SIMDE_MATH_HAVE_CMATH)
  913. #define simde_math_sqrtf(v) std::sqrt(v)
  914. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  915. #define simde_math_sqrtf(v) sqrtf(v)
  916. #endif
  917. #endif
  918. #if !defined(simde_math_tan)
  919. #if SIMDE_MATH_BUILTIN_LIBM(tan)
  920. #define simde_math_tan(v) __builtin_tan(v)
  921. #elif defined(SIMDE_MATH_HAVE_CMATH)
  922. #define simde_math_tan(v) std::tan(v)
  923. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  924. #define simde_math_tan(v) tan(v)
  925. #endif
  926. #endif
  927. #if !defined(simde_math_tanf)
  928. #if SIMDE_MATH_BUILTIN_LIBM(tanf)
  929. #define simde_math_tanf(v) __builtin_tanf(v)
  930. #elif defined(SIMDE_MATH_HAVE_CMATH)
  931. #define simde_math_tanf(v) std::tan(v)
  932. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  933. #define simde_math_tanf(v) tanf(v)
  934. #endif
  935. #endif
  936. #if !defined(simde_math_tanh)
  937. #if SIMDE_MATH_BUILTIN_LIBM(tanh)
  938. #define simde_math_tanh(v) __builtin_tanh(v)
  939. #elif defined(SIMDE_MATH_HAVE_CMATH)
  940. #define simde_math_tanh(v) std::tanh(v)
  941. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  942. #define simde_math_tanh(v) tanh(v)
  943. #endif
  944. #endif
  945. #if !defined(simde_math_tanhf)
  946. #if SIMDE_MATH_BUILTIN_LIBM(tanhf)
  947. #define simde_math_tanhf(v) __builtin_tanhf(v)
  948. #elif defined(SIMDE_MATH_HAVE_CMATH)
  949. #define simde_math_tanhf(v) std::tanh(v)
  950. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  951. #define simde_math_tanhf(v) tanhf(v)
  952. #endif
  953. #endif
  954. #if !defined(simde_math_trunc)
  955. #if SIMDE_MATH_BUILTIN_LIBM(trunc)
  956. #define simde_math_trunc(v) __builtin_trunc(v)
  957. #elif defined(SIMDE_MATH_HAVE_CMATH)
  958. #define simde_math_trunc(v) std::trunc(v)
  959. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  960. #define simde_math_trunc(v) trunc(v)
  961. #endif
  962. #endif
  963. #if !defined(simde_math_truncf)
  964. #if SIMDE_MATH_BUILTIN_LIBM(truncf)
  965. #define simde_math_truncf(v) __builtin_truncf(v)
  966. #elif defined(SIMDE_MATH_HAVE_CMATH)
  967. #define simde_math_truncf(v) std::trunc(v)
  968. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  969. #define simde_math_truncf(v) truncf(v)
  970. #endif
  971. #endif
  972. /*** Complex functions ***/
  973. #if !defined(simde_math_cexp)
  974. #if defined(__cplusplus)
  975. #define simde_math_cexp(v) std::cexp(v)
  976. #elif SIMDE_MATH_BUILTIN_LIBM(cexp)
  977. #define simde_math_cexp(v) __builtin_cexp(v)
  978. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  979. #define simde_math_cexp(v) cexp(v)
  980. #endif
  981. #endif
  982. #if !defined(simde_math_cexpf)
  983. #if defined(__cplusplus)
  984. #define simde_math_cexpf(v) std::exp(v)
  985. #elif SIMDE_MATH_BUILTIN_LIBM(cexpf)
  986. #define simde_math_cexpf(v) __builtin_cexpf(v)
  987. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  988. #define simde_math_cexpf(v) cexpf(v)
  989. #endif
  990. #endif
  991. /*** Additional functions not in libm ***/
  992. #if defined(simde_math_fabs) && defined(simde_math_sqrt) && \
  993. defined(simde_math_exp)
  994. static HEDLEY_INLINE double simde_math_cdfnorm(double x)
  995. {
  996. /* https://www.johndcook.com/blog/cpp_phi/
  997. * Public Domain */
  998. static const double a1 = 0.254829592;
  999. static const double a2 = -0.284496736;
  1000. static const double a3 = 1.421413741;
  1001. static const double a4 = -1.453152027;
  1002. static const double a5 = 1.061405429;
  1003. static const double p = 0.3275911;
  1004. const int sign = x < 0;
  1005. x = simde_math_fabs(x) / simde_math_sqrt(2.0);
  1006. /* A&S formula 7.1.26 */
  1007. double t = 1.0 / (1.0 + p * x);
  1008. double y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t *
  1009. simde_math_exp(-x * x);
  1010. return 0.5 * (1.0 + (sign ? -y : y));
  1011. }
  1012. #define simde_math_cdfnorm simde_math_cdfnorm
  1013. #endif
  1014. #if defined(simde_math_fabsf) && defined(simde_math_sqrtf) && \
  1015. defined(simde_math_expf)
  1016. static HEDLEY_INLINE float simde_math_cdfnormf(float x)
  1017. {
  1018. /* https://www.johndcook.com/blog/cpp_phi/
  1019. * Public Domain */
  1020. static const float a1 = 0.254829592f;
  1021. static const float a2 = -0.284496736f;
  1022. static const float a3 = 1.421413741f;
  1023. static const float a4 = -1.453152027f;
  1024. static const float a5 = 1.061405429f;
  1025. static const float p = 0.3275911f;
  1026. const int sign = x < 0;
  1027. x = simde_math_fabsf(x) / simde_math_sqrtf(2.0f);
  1028. /* A&S formula 7.1.26 */
  1029. float t = 1.0f / (1.0f + p * x);
  1030. float y = 1.0f - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t *
  1031. simde_math_expf(-x * x);
  1032. return 0.5f * (1.0f + (sign ? -y : y));
  1033. }
  1034. #define simde_math_cdfnormf simde_math_cdfnormf
  1035. #endif
  1036. HEDLEY_DIAGNOSTIC_PUSH
  1037. SIMDE_DIAGNOSTIC_DISABLE_FLOAT_EQUAL_
  1038. #if !defined(simde_math_cdfnorminv) && defined(simde_math_log) && \
  1039. defined(simde_math_sqrt)
  1040. /*https://web.archive.org/web/20150910081113/http://home.online.no/~pjacklam/notes/invnorm/impl/sprouse/ltqnorm.c*/
  1041. static HEDLEY_INLINE double simde_math_cdfnorminv(double p)
  1042. {
  1043. static const double a[] = {
  1044. -3.969683028665376e+01, 2.209460984245205e+02,
  1045. -2.759285104469687e+02, 1.383577518672690e+02,
  1046. -3.066479806614716e+01, 2.506628277459239e+00};
  1047. static const double b[] = {-5.447609879822406e+01,
  1048. 1.615858368580409e+02,
  1049. -1.556989798598866e+02,
  1050. 6.680131188771972e+01,
  1051. -1.328068155288572e+01};
  1052. static const double c[] = {
  1053. -7.784894002430293e-03, -3.223964580411365e-01,
  1054. -2.400758277161838e+00, -2.549732539343734e+00,
  1055. 4.374664141464968e+00, 2.938163982698783e+00};
  1056. static const double d[] = {7.784695709041462e-03, 3.224671290700398e-01,
  1057. 2.445134137142996e+00,
  1058. 3.754408661907416e+00};
  1059. static const double low = 0.02425;
  1060. static const double high = 0.97575;
  1061. double q, r;
  1062. if (p < 0 || p > 1) {
  1063. return 0.0;
  1064. } else if (p == 0) {
  1065. return -SIMDE_MATH_INFINITY;
  1066. } else if (p == 1) {
  1067. return SIMDE_MATH_INFINITY;
  1068. } else if (p < low) {
  1069. q = simde_math_sqrt(-2.0 * simde_math_log(p));
  1070. return (((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q +
  1071. c[4]) * q +
  1072. c[5]) /
  1073. (((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1));
  1074. } else if (p > high) {
  1075. q = simde_math_sqrt(-2.0 * simde_math_log(1.0 - p));
  1076. return -(((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q +
  1077. c[4]) * q +
  1078. c[5]) /
  1079. (((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1));
  1080. } else {
  1081. q = p - 0.5;
  1082. r = q * q;
  1083. return (((((a[0] * r + a[1]) * r + a[2]) * r + a[3]) * r +
  1084. a[4]) * r +
  1085. a[5]) *
  1086. q /
  1087. (((((b[0] * r + b[1]) * r + b[2]) * r + b[3]) * r +
  1088. b[4]) * r +
  1089. 1);
  1090. }
  1091. }
  1092. #define simde_math_cdfnorminv simde_math_cdfnorminv
  1093. #endif
  1094. #if !defined(simde_math_cdfnorminvf) && defined(simde_math_logf) && \
  1095. defined(simde_math_sqrtf)
  1096. static HEDLEY_INLINE float simde_math_cdfnorminvf(float p)
  1097. {
  1098. static const float a[] = {
  1099. -3.969683028665376e+01f, 2.209460984245205e+02f,
  1100. -2.759285104469687e+02f, 1.383577518672690e+02f,
  1101. -3.066479806614716e+01f, 2.506628277459239e+00f};
  1102. static const float b[] = {-5.447609879822406e+01f,
  1103. 1.615858368580409e+02f,
  1104. -1.556989798598866e+02f,
  1105. 6.680131188771972e+01f,
  1106. -1.328068155288572e+01f};
  1107. static const float c[] = {
  1108. -7.784894002430293e-03f, -3.223964580411365e-01f,
  1109. -2.400758277161838e+00f, -2.549732539343734e+00f,
  1110. 4.374664141464968e+00f, 2.938163982698783e+00f};
  1111. static const float d[] = {7.784695709041462e-03f,
  1112. 3.224671290700398e-01f,
  1113. 2.445134137142996e+00f,
  1114. 3.754408661907416e+00f};
  1115. static const float low = 0.02425f;
  1116. static const float high = 0.97575f;
  1117. float q, r;
  1118. if (p < 0 || p > 1) {
  1119. return 0.0f;
  1120. } else if (p == 0) {
  1121. return -SIMDE_MATH_INFINITYF;
  1122. } else if (p == 1) {
  1123. return SIMDE_MATH_INFINITYF;
  1124. } else if (p < low) {
  1125. q = simde_math_sqrtf(-2.0f * simde_math_logf(p));
  1126. return (((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q +
  1127. c[4]) * q +
  1128. c[5]) /
  1129. (((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1));
  1130. } else if (p > high) {
  1131. q = simde_math_sqrtf(-2.0f * simde_math_logf(1.0f - p));
  1132. return -(((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q +
  1133. c[4]) * q +
  1134. c[5]) /
  1135. (((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1));
  1136. } else {
  1137. q = p - 0.5f;
  1138. r = q * q;
  1139. return (((((a[0] * r + a[1]) * r + a[2]) * r + a[3]) * r +
  1140. a[4]) * r +
  1141. a[5]) *
  1142. q /
  1143. (((((b[0] * r + b[1]) * r + b[2]) * r + b[3]) * r +
  1144. b[4]) * r +
  1145. 1);
  1146. }
  1147. }
  1148. #define simde_math_cdfnorminvf simde_math_cdfnorminvf
  1149. #endif
  1150. #if !defined(simde_math_erfinv) && defined(simde_math_log) && \
  1151. defined(simde_math_copysign) && defined(simde_math_sqrt)
  1152. static HEDLEY_INLINE double simde_math_erfinv(double x)
  1153. {
  1154. /* https://stackoverflow.com/questions/27229371/inverse-error-function-in-c
  1155. *
  1156. * The original answer on SO uses a constant of 0.147, but in my
  1157. * testing 0.14829094707965850830078125 gives a lower average absolute error
  1158. * (0.0001410958211636170744895935 vs. 0.0001465479290345683693885803).
  1159. * That said, if your goal is to minimize the *maximum* absolute
  1160. * error, 0.15449436008930206298828125 provides significantly better
  1161. * results; 0.0009250640869140625000000000 vs ~ 0.005. */
  1162. double tt1, tt2, lnx;
  1163. double sgn = simde_math_copysign(1.0, x);
  1164. x = (1.0 - x) * (1.0 + x);
  1165. lnx = simde_math_log(x);
  1166. tt1 = 2.0 / (SIMDE_MATH_PI * 0.14829094707965850830078125) + 0.5 * lnx;
  1167. tt2 = (1.0 / 0.14829094707965850830078125) * lnx;
  1168. return sgn * simde_math_sqrt(-tt1 + simde_math_sqrt(tt1 * tt1 - tt2));
  1169. }
  1170. #define simde_math_erfinv simde_math_erfinv
  1171. #endif
  1172. #if !defined(simde_math_erfinvf) && defined(simde_math_logf) && \
  1173. defined(simde_math_copysignf) && defined(simde_math_sqrtf)
  1174. static HEDLEY_INLINE float simde_math_erfinvf(float x)
  1175. {
  1176. float tt1, tt2, lnx;
  1177. float sgn = simde_math_copysignf(1.0f, x);
  1178. x = (1.0f - x) * (1.0f + x);
  1179. lnx = simde_math_logf(x);
  1180. tt1 = 2.0f / (SIMDE_MATH_PIF * 0.14829094707965850830078125f) +
  1181. 0.5f * lnx;
  1182. tt2 = (1.0f / 0.14829094707965850830078125f) * lnx;
  1183. return sgn * simde_math_sqrtf(-tt1 + simde_math_sqrtf(tt1 * tt1 - tt2));
  1184. }
  1185. #define simde_math_erfinvf simde_math_erfinvf
  1186. #endif
  1187. #if !defined(simde_math_erfcinv) && defined(simde_math_erfinv) && \
  1188. defined(simde_math_log) && defined(simde_math_sqrt)
  1189. static HEDLEY_INLINE double simde_math_erfcinv(double x)
  1190. {
  1191. if (x >= 0.0625 && x < 2.0) {
  1192. return simde_math_erfinv(1.0 - x);
  1193. } else if (x < 0.0625 && x >= 1.0e-100) {
  1194. double p[6] = {0.1550470003116, 1.382719649631, 0.690969348887,
  1195. -1.128081391617, 0.680544246825, -0.16444156791};
  1196. double q[3] = {0.155024849822, 1.385228141995, 1.000000000000};
  1197. const double t = 1.0 / simde_math_sqrt(-simde_math_log(x));
  1198. return (p[0] / t + p[1] +
  1199. t * (p[2] + t * (p[3] + t * (p[4] + t * p[5])))) /
  1200. (q[0] + t * (q[1] + t * (q[2])));
  1201. } else if (x < 1.0e-100 && x >= SIMDE_MATH_DBL_MIN) {
  1202. double p[4] = {0.00980456202915, 0.363667889171, 0.97302949837,
  1203. -0.5374947401};
  1204. double q[3] = {0.00980451277802, 0.363699971544,
  1205. 1.000000000000};
  1206. const double t = 1.0 / simde_math_sqrt(-simde_math_log(x));
  1207. return (p[0] / t + p[1] + t * (p[2] + t * p[3])) /
  1208. (q[0] + t * (q[1] + t * (q[2])));
  1209. } else if (!simde_math_isnormal(x)) {
  1210. return SIMDE_MATH_INFINITY;
  1211. } else {
  1212. return -SIMDE_MATH_INFINITY;
  1213. }
  1214. }
  1215. #define simde_math_erfcinv simde_math_erfcinv
  1216. #endif
  1217. #if !defined(simde_math_erfcinvf) && defined(simde_math_erfinvf) && \
  1218. defined(simde_math_logf) && defined(simde_math_sqrtf)
  1219. static HEDLEY_INLINE float simde_math_erfcinvf(float x)
  1220. {
  1221. if (x >= 0.0625f && x < 2.0f) {
  1222. return simde_math_erfinvf(1.0f - x);
  1223. } else if (x < 0.0625f && x >= SIMDE_MATH_FLT_MIN) {
  1224. static const float p[6] = {0.1550470003116f, 1.382719649631f,
  1225. 0.690969348887f, -1.128081391617f,
  1226. 0.680544246825f - 0.164441567910f};
  1227. static const float q[3] = {0.155024849822f, 1.385228141995f,
  1228. 1.000000000000f};
  1229. const float t = 1.0f / simde_math_sqrtf(-simde_math_logf(x));
  1230. return (p[0] / t + p[1] +
  1231. t * (p[2] + t * (p[3] + t * (p[4] + t * p[5])))) /
  1232. (q[0] + t * (q[1] + t * (q[2])));
  1233. } else if (x < SIMDE_MATH_FLT_MIN && simde_math_isnormalf(x)) {
  1234. static const float p[4] = {0.00980456202915f, 0.36366788917100f,
  1235. 0.97302949837000f,
  1236. -0.5374947401000f};
  1237. static const float q[3] = {0.00980451277802f, 0.36369997154400f,
  1238. 1.00000000000000f};
  1239. const float t = 1.0f / simde_math_sqrtf(-simde_math_logf(x));
  1240. return (p[0] / t + p[1] + t * (p[2] + t * p[3])) /
  1241. (q[0] + t * (q[1] + t * (q[2])));
  1242. } else {
  1243. return simde_math_isnormalf(x) ? -SIMDE_MATH_INFINITYF
  1244. : SIMDE_MATH_INFINITYF;
  1245. }
  1246. }
  1247. #define simde_math_erfcinvf simde_math_erfcinvf
  1248. #endif
  1249. HEDLEY_DIAGNOSTIC_POP
  1250. static HEDLEY_INLINE double simde_math_rad2deg(double radians)
  1251. {
  1252. return radians * (180.0 / SIMDE_MATH_PI);
  1253. }
  1254. static HEDLEY_INLINE float simde_math_rad2degf(float radians)
  1255. {
  1256. return radians * (180.0f / SIMDE_MATH_PIF);
  1257. }
  1258. static HEDLEY_INLINE double simde_math_deg2rad(double degrees)
  1259. {
  1260. return degrees * (SIMDE_MATH_PI / 180.0);
  1261. }
  1262. static HEDLEY_INLINE float simde_math_deg2radf(float degrees)
  1263. {
  1264. return degrees * (SIMDE_MATH_PIF / 180.0f);
  1265. }
  1266. #endif /* !defined(SIMDE_MATH_H) */