simde-math.h 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  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. #include <stdint.h>
  34. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  35. #include <arm_neon.h>
  36. #endif
  37. HEDLEY_DIAGNOSTIC_PUSH
  38. SIMDE_DISABLE_UNWANTED_DIAGNOSTICS
  39. /* SLEEF support
  40. * https://sleef.org/
  41. *
  42. * If you include <sleef.h> prior to including SIMDe, SIMDe will use
  43. * SLEEF. You can also define SIMDE_MATH_SLEEF_ENABLE prior to
  44. * including SIMDe to force the issue.
  45. *
  46. * Note that SLEEF does requires linking to libsleef.
  47. *
  48. * By default, SIMDe will use the 1 ULP functions, but if you use
  49. * SIMDE_ACCURACY_PREFERENCE of 0 we will use up to 4 ULP. This is
  50. * only the case for the simde_math_* functions; for code in other
  51. * SIMDe headers which calls SLEEF directly we may use functions with
  52. * greater error if the API we're implementing is less precise (for
  53. * example, SVML guarantees 4 ULP, so we will generally use the 3.5
  54. * ULP functions from SLEEF). */
  55. #if !defined(SIMDE_MATH_SLEEF_DISABLE)
  56. #if defined(__SLEEF_H__)
  57. #define SIMDE_MATH_SLEEF_ENABLE
  58. #endif
  59. #endif
  60. #if defined(SIMDE_MATH_SLEEF_ENABLE) && !defined(__SLEEF_H__)
  61. HEDLEY_DIAGNOSTIC_PUSH
  62. SIMDE_DIAGNOSTIC_DISABLE_IGNORED_QUALIFIERS_
  63. #include <sleef.h>
  64. HEDLEY_DIAGNOSTIC_POP
  65. #endif
  66. #if defined(SIMDE_MATH_SLEEF_ENABLE) && defined(__SLEEF_H__)
  67. #if defined(SLEEF_VERSION_MAJOR)
  68. #define SIMDE_MATH_SLEEF_VERSION_CHECK(major, minor, patch) \
  69. (HEDLEY_VERSION_ENCODE(SLEEF_VERSION_MAJOR, SLEEF_VERSION_MINOR, \
  70. SLEEF_VERSION_PATCHLEVEL) >= \
  71. HEDLEY_VERSION_ENCODE(major, minor, patch))
  72. #else
  73. #define SIMDE_MATH_SLEEF_VERSION_CHECK(major, minor, patch) \
  74. (HEDLEY_VERSION_ENCODE(3, 0, 0) >= \
  75. HEDLEY_VERSION_ENCODE(major, minor, patch))
  76. #endif
  77. #else
  78. #define SIMDE_MATH_SLEEF_VERSION_CHECK(major, minor, patch) (0)
  79. #endif
  80. #if defined(__has_builtin)
  81. #define SIMDE_MATH_BUILTIN_LIBM(func) __has_builtin(__builtin_##func)
  82. #elif HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  83. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || HEDLEY_GCC_VERSION_CHECK(4, 4, 0)
  84. #define SIMDE_MATH_BUILTIN_LIBM(func) (1)
  85. #else
  86. #define SIMDE_MATH_BUILTIN_LIBM(func) (0)
  87. #endif
  88. #if defined(HUGE_VAL)
  89. /* Looks like <math.h> or <cmath> has already been included. */
  90. /* The math.h from libc++ (yes, the C header from the C++ standard
  91. * library) will define an isnan function, but not an isnan macro
  92. * like the C standard requires. So we detect the header guards
  93. * macro libc++ uses. */
  94. #if defined(isnan) || (defined(_LIBCPP_MATH_H) && !defined(_LIBCPP_CMATH))
  95. #define SIMDE_MATH_HAVE_MATH_H
  96. #elif defined(__cplusplus)
  97. #define SIMDE_MATH_HAVE_CMATH
  98. #endif
  99. #elif defined(__has_include)
  100. #if defined(__cplusplus) && (__cplusplus >= 201103L) && __has_include(<cmath>)
  101. #define SIMDE_MATH_HAVE_CMATH
  102. #include <cmath>
  103. #elif __has_include(<math.h>)
  104. #define SIMDE_MATH_HAVE_MATH_H
  105. #include <math.h>
  106. #elif !defined(SIMDE_MATH_NO_LIBM)
  107. #define SIMDE_MATH_NO_LIBM
  108. #endif
  109. #elif !defined(SIMDE_MATH_NO_LIBM)
  110. #if defined(__cplusplus) && (__cplusplus >= 201103L)
  111. #define SIMDE_MATH_HAVE_CMATH
  112. HEDLEY_DIAGNOSTIC_PUSH
  113. #if defined(HEDLEY_MSVC_VERSION)
  114. /* VS 14 emits this diagnostic about noexcept being used on a
  115. * <cmath> function, which we can't do anything about. */
  116. #pragma warning(disable : 4996)
  117. #endif
  118. #include <cmath>
  119. HEDLEY_DIAGNOSTIC_POP
  120. #else
  121. #define SIMDE_MATH_HAVE_MATH_H
  122. #include <math.h>
  123. #endif
  124. #endif
  125. /* Try to avoid including <complex> since it pulls in a *lot* of code. */
  126. #if HEDLEY_HAS_BUILTIN(__builtin_creal) || \
  127. HEDLEY_GCC_VERSION_CHECK(4, 7, 0) || \
  128. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0)
  129. HEDLEY_DIAGNOSTIC_PUSH
  130. SIMDE_DIAGNOSTIC_DISABLE_C99_EXTENSIONS_
  131. typedef __complex__ float simde_cfloat32;
  132. typedef __complex__ double simde_cfloat64;
  133. HEDLEY_DIAGNOSTIC_POP
  134. #define SIMDE_MATH_CMPLX(x, y) \
  135. (HEDLEY_STATIC_CAST(double, x) + \
  136. HEDLEY_STATIC_CAST(double, y) * (__extension__ 1.0j))
  137. #define SIMDE_MATH_CMPLXF(x, y) \
  138. (HEDLEY_STATIC_CAST(float, x) + \
  139. HEDLEY_STATIC_CAST(float, y) * (__extension__ 1.0fj))
  140. #if !defined(simde_math_creal)
  141. #define simde_math_crealf(z) __builtin_crealf(z)
  142. #endif
  143. #if !defined(simde_math_crealf)
  144. #define simde_math_creal(z) __builtin_creal(z)
  145. #endif
  146. #if !defined(simde_math_cimag)
  147. #define simde_math_cimagf(z) __builtin_cimagf(z)
  148. #endif
  149. #if !defined(simde_math_cimagf)
  150. #define simde_math_cimag(z) __builtin_cimag(z)
  151. #endif
  152. #elif !defined(__cplusplus)
  153. #include <complex.h>
  154. #if !defined(HEDLEY_MSVC_VERSION)
  155. typedef float _Complex simde_cfloat32;
  156. typedef double _Complex simde_cfloat64;
  157. #else
  158. typedef _Fcomplex simde_cfloat32;
  159. typedef _Dcomplex simde_cfloat64;
  160. #endif
  161. #if defined(HEDLEY_MSVC_VERSION)
  162. #define SIMDE_MATH_CMPLX(x, y) ((simde_cfloat64){(x), (y)})
  163. #define SIMDE_MATH_CMPLXF(x, y) ((simde_cfloat32){(x), (y)})
  164. #elif defined(CMPLX) && defined(CMPLXF)
  165. #define SIMDE_MATH_CMPLX(x, y) CMPLX(x, y)
  166. #define SIMDE_MATH_CMPLXF(x, y) CMPLXF(x, y)
  167. #else
  168. #define SIMDE_MATH_CMPLX(x, y) \
  169. (HEDLEY_STATIC_CAST(double, x) + HEDLEY_STATIC_CAST(double, y) * I)
  170. #define SIMDE_MATH_CMPLXF(x, y) \
  171. (HEDLEY_STATIC_CAST(float, x) + HEDLEY_STATIC_CAST(float, y) * I)
  172. #endif
  173. #if !defined(simde_math_creal)
  174. #define simde_math_creal(z) creal(z)
  175. #endif
  176. #if !defined(simde_math_crealf)
  177. #define simde_math_crealf(z) crealf(z)
  178. #endif
  179. #if !defined(simde_math_cimag)
  180. #define simde_math_cimag(z) cimag(z)
  181. #endif
  182. #if !defined(simde_math_cimagf)
  183. #define simde_math_cimagf(z) cimagf(z)
  184. #endif
  185. #else
  186. HEDLEY_DIAGNOSTIC_PUSH
  187. #if defined(HEDLEY_MSVC_VERSION)
  188. #pragma warning(disable : 4530)
  189. #endif
  190. #include <complex>
  191. HEDLEY_DIAGNOSTIC_POP
  192. typedef std::complex<float> simde_cfloat32;
  193. typedef std::complex<double> simde_cfloat64;
  194. #define SIMDE_MATH_CMPLX(x, y) (std::complex<double>(x, y))
  195. #define SIMDE_MATH_CMPLXF(x, y) (std::complex<float>(x, y))
  196. #if !defined(simde_math_creal)
  197. #define simde_math_creal(z) ((z).real())
  198. #endif
  199. #if !defined(simde_math_crealf)
  200. #define simde_math_crealf(z) ((z).real())
  201. #endif
  202. #if !defined(simde_math_cimag)
  203. #define simde_math_cimag(z) ((z).imag())
  204. #endif
  205. #if !defined(simde_math_cimagf)
  206. #define simde_math_cimagf(z) ((z).imag())
  207. #endif
  208. #endif
  209. #if !defined(SIMDE_MATH_INFINITY)
  210. #if HEDLEY_HAS_BUILTIN(__builtin_inf) || HEDLEY_GCC_VERSION_CHECK(3, 3, 0) || \
  211. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  212. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  213. HEDLEY_CRAY_VERSION_CHECK(8, 1, 0)
  214. #define SIMDE_MATH_INFINITY (__builtin_inf())
  215. #elif defined(INFINITY)
  216. #define SIMDE_MATH_INFINITY INFINITY
  217. #endif
  218. #endif
  219. #if !defined(SIMDE_INFINITYF)
  220. #if HEDLEY_HAS_BUILTIN(__builtin_inff) || HEDLEY_GCC_VERSION_CHECK(3, 3, 0) || \
  221. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  222. HEDLEY_CRAY_VERSION_CHECK(8, 1, 0) || \
  223. HEDLEY_IBM_VERSION_CHECK(13, 1, 0)
  224. #define SIMDE_MATH_INFINITYF (__builtin_inff())
  225. #elif defined(INFINITYF)
  226. #define SIMDE_MATH_INFINITYF INFINITYF
  227. #elif defined(SIMDE_MATH_INFINITY)
  228. #define SIMDE_MATH_INFINITYF HEDLEY_STATIC_CAST(float, SIMDE_MATH_INFINITY)
  229. #endif
  230. #endif
  231. #if !defined(SIMDE_MATH_NAN)
  232. #if HEDLEY_HAS_BUILTIN(__builtin_nan) || HEDLEY_GCC_VERSION_CHECK(3, 3, 0) || \
  233. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  234. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  235. HEDLEY_CRAY_VERSION_CHECK(8, 1, 0) || \
  236. HEDLEY_IBM_VERSION_CHECK(13, 1, 0)
  237. #define SIMDE_MATH_NAN (__builtin_nan(""))
  238. #elif defined(NAN)
  239. #define SIMDE_MATH_NAN NAN
  240. #endif
  241. #endif
  242. #if !defined(SIMDE_NANF)
  243. #if HEDLEY_HAS_BUILTIN(__builtin_nanf) || HEDLEY_GCC_VERSION_CHECK(3, 3, 0) || \
  244. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  245. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  246. HEDLEY_CRAY_VERSION_CHECK(8, 1, 0)
  247. #define SIMDE_MATH_NANF (__builtin_nanf(""))
  248. #elif defined(NANF)
  249. #define SIMDE_MATH_NANF NANF
  250. #elif defined(SIMDE_MATH_NAN)
  251. #define SIMDE_MATH_NANF HEDLEY_STATIC_CAST(float, SIMDE_MATH_NAN)
  252. #endif
  253. #endif
  254. #if !defined(SIMDE_MATH_PI)
  255. #if defined(M_PI)
  256. #define SIMDE_MATH_PI M_PI
  257. #else
  258. #define SIMDE_MATH_PI 3.14159265358979323846
  259. #endif
  260. #endif
  261. #if !defined(SIMDE_MATH_PIF)
  262. #if defined(M_PI)
  263. #define SIMDE_MATH_PIF HEDLEY_STATIC_CAST(float, M_PI)
  264. #else
  265. #define SIMDE_MATH_PIF 3.14159265358979323846f
  266. #endif
  267. #endif
  268. #if !defined(SIMDE_MATH_PI_OVER_180)
  269. #define SIMDE_MATH_PI_OVER_180 \
  270. 0.0174532925199432957692369076848861271344287188854172545609719144
  271. #endif
  272. #if !defined(SIMDE_MATH_PI_OVER_180F)
  273. #define SIMDE_MATH_PI_OVER_180F \
  274. 0.0174532925199432957692369076848861271344287188854172545609719144f
  275. #endif
  276. #if !defined(SIMDE_MATH_180_OVER_PI)
  277. #define SIMDE_MATH_180_OVER_PI \
  278. 57.295779513082320876798154814105170332405472466564321549160243861
  279. #endif
  280. #if !defined(SIMDE_MATH_180_OVER_PIF)
  281. #define SIMDE_MATH_180_OVER_PIF \
  282. 57.295779513082320876798154814105170332405472466564321549160243861f
  283. #endif
  284. #if !defined(SIMDE_MATH_FLT_MIN)
  285. #if defined(FLT_MIN)
  286. #define SIMDE_MATH_FLT_MIN FLT_MIN
  287. #elif defined(__FLT_MIN__)
  288. #define SIMDE_MATH_FLT_MIN __FLT_MIN__
  289. #elif defined(__cplusplus)
  290. #include <cfloat>
  291. #define SIMDE_MATH_FLT_MIN FLT_MIN
  292. #else
  293. #include <float.h>
  294. #define SIMDE_MATH_FLT_MIN FLT_MIN
  295. #endif
  296. #endif
  297. #if !defined(SIMDE_MATH_DBL_MIN)
  298. #if defined(DBL_MIN)
  299. #define SIMDE_MATH_DBL_MIN DBL_MIN
  300. #elif defined(__DBL_MIN__)
  301. #define SIMDE_MATH_DBL_MIN __DBL_MIN__
  302. #elif defined(__cplusplus)
  303. #include <cfloat>
  304. #define SIMDE_MATH_DBL_MIN DBL_MIN
  305. #else
  306. #include <float.h>
  307. #define SIMDE_MATH_DBL_MIN DBL_MIN
  308. #endif
  309. #endif
  310. /*** Classification macros from C99 ***/
  311. #if !defined(simde_math_isinf)
  312. #if SIMDE_MATH_BUILTIN_LIBM(isinf)
  313. #define simde_math_isinf(v) __builtin_isinf(v)
  314. #elif defined(isinf) || defined(SIMDE_MATH_HAVE_MATH_H)
  315. #define simde_math_isinf(v) isinf(v)
  316. #elif defined(SIMDE_MATH_HAVE_CMATH)
  317. #define simde_math_isinf(v) std::isinf(v)
  318. #endif
  319. #endif
  320. #if !defined(simde_math_isinff)
  321. #if HEDLEY_HAS_BUILTIN(__builtin_isinff) || \
  322. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  323. HEDLEY_ARM_VERSION_CHECK(4, 1, 0)
  324. #define simde_math_isinff(v) __builtin_isinff(v)
  325. #elif defined(SIMDE_MATH_HAVE_CMATH)
  326. #define simde_math_isinff(v) std::isinf(v)
  327. #elif defined(simde_math_isinf)
  328. #define simde_math_isinff(v) simde_math_isinf(HEDLEY_STATIC_CAST(double, v))
  329. #endif
  330. #endif
  331. #if !defined(simde_math_isnan)
  332. #if SIMDE_MATH_BUILTIN_LIBM(isnan)
  333. #define simde_math_isnan(v) __builtin_isnan(v)
  334. #elif defined(isnan) || defined(SIMDE_MATH_HAVE_MATH_H)
  335. #define simde_math_isnan(v) isnan(v)
  336. #elif defined(SIMDE_MATH_HAVE_CMATH)
  337. #define simde_math_isnan(v) std::isnan(v)
  338. #endif
  339. #endif
  340. #if !defined(simde_math_isnanf)
  341. #if HEDLEY_HAS_BUILTIN(__builtin_isnanf) || \
  342. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  343. HEDLEY_ARM_VERSION_CHECK(4, 1, 0)
  344. /* XL C/C++ has __builtin_isnan but not __builtin_isnanf */
  345. #define simde_math_isnanf(v) __builtin_isnanf(v)
  346. #elif defined(SIMDE_MATH_HAVE_CMATH)
  347. #define simde_math_isnanf(v) std::isnan(v)
  348. #elif defined(simde_math_isnan)
  349. #define simde_math_isnanf(v) simde_math_isnan(HEDLEY_STATIC_CAST(double, v))
  350. #endif
  351. #endif
  352. #if !defined(simde_math_isnormal)
  353. #if SIMDE_MATH_BUILTIN_LIBM(isnormal)
  354. #define simde_math_isnormal(v) __builtin_isnormal(v)
  355. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  356. #define simde_math_isnormal(v) isnormal(v)
  357. #elif defined(SIMDE_MATH_HAVE_CMATH)
  358. #define simde_math_isnormal(v) std::isnormal(v)
  359. #endif
  360. #endif
  361. #if !defined(simde_math_isnormalf)
  362. #if HEDLEY_HAS_BUILTIN(__builtin_isnormalf)
  363. #define simde_math_isnormalf(v) __builtin_isnormalf(v)
  364. #elif SIMDE_MATH_BUILTIN_LIBM(isnormal)
  365. #define simde_math_isnormalf(v) __builtin_isnormal(v)
  366. #elif defined(isnormalf)
  367. #define simde_math_isnormalf(v) isnormalf(v)
  368. #elif defined(isnormal) || defined(SIMDE_MATH_HAVE_MATH_H)
  369. #define simde_math_isnormalf(v) isnormal(v)
  370. #elif defined(SIMDE_MATH_HAVE_CMATH)
  371. #define simde_math_isnormalf(v) std::isnormal(v)
  372. #elif defined(simde_math_isnormal)
  373. #define simde_math_isnormalf(v) simde_math_isnormal(v)
  374. #endif
  375. #endif
  376. /*** Manipulation functions ***/
  377. #if !defined(simde_math_nextafter)
  378. #if (HEDLEY_HAS_BUILTIN(__builtin_nextafter) && \
  379. !defined(HEDLEY_IBM_VERSION)) || \
  380. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  381. HEDLEY_GCC_VERSION_CHECK(3, 4, 0) || \
  382. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0)
  383. #define simde_math_nextafter(x, y) __builtin_nextafter(x, y)
  384. #elif defined(SIMDE_MATH_HAVE_CMATH)
  385. #define simde_math_nextafter(x, y) std::nextafter(x, y)
  386. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  387. #define simde_math_nextafter(x, y) nextafter(x, y)
  388. #endif
  389. #endif
  390. #if !defined(simde_math_nextafterf)
  391. #if (HEDLEY_HAS_BUILTIN(__builtin_nextafterf) && \
  392. !defined(HEDLEY_IBM_VERSION)) || \
  393. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  394. HEDLEY_GCC_VERSION_CHECK(3, 4, 0) || \
  395. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0)
  396. #define simde_math_nextafterf(x, y) __builtin_nextafterf(x, y)
  397. #elif defined(SIMDE_MATH_HAVE_CMATH)
  398. #define simde_math_nextafterf(x, y) std::nextafter(x, y)
  399. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  400. #define simde_math_nextafterf(x, y) nextafterf(x, y)
  401. #endif
  402. #endif
  403. /*** Functions from C99 ***/
  404. #if !defined(simde_math_abs)
  405. #if SIMDE_MATH_BUILTIN_LIBM(abs)
  406. #define simde_math_abs(v) __builtin_abs(v)
  407. #elif defined(SIMDE_MATH_HAVE_CMATH)
  408. #define simde_math_abs(v) std::abs(v)
  409. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  410. #define simde_math_abs(v) abs(v)
  411. #endif
  412. #endif
  413. #if !defined(simde_math_fabsf)
  414. #if SIMDE_MATH_BUILTIN_LIBM(fabsf)
  415. #define simde_math_fabsf(v) __builtin_fabsf(v)
  416. #elif defined(SIMDE_MATH_HAVE_CMATH)
  417. #define simde_math_fabsf(v) std::abs(v)
  418. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  419. #define simde_math_fabsf(v) fabsf(v)
  420. #endif
  421. #endif
  422. #if !defined(simde_math_acos)
  423. #if SIMDE_MATH_BUILTIN_LIBM(acos)
  424. #define simde_math_acos(v) __builtin_acos(v)
  425. #elif defined(SIMDE_MATH_HAVE_CMATH)
  426. #define simde_math_acos(v) std::acos(v)
  427. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  428. #define simde_math_acos(v) acos(v)
  429. #endif
  430. #endif
  431. #if !defined(simde_math_acosf)
  432. #if SIMDE_MATH_BUILTIN_LIBM(acosf)
  433. #define simde_math_acosf(v) __builtin_acosf(v)
  434. #elif defined(SIMDE_MATH_HAVE_CMATH)
  435. #define simde_math_acosf(v) std::acos(v)
  436. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  437. #define simde_math_acosf(v) acosf(v)
  438. #endif
  439. #endif
  440. #if !defined(simde_math_acosh)
  441. #if SIMDE_MATH_BUILTIN_LIBM(acosh)
  442. #define simde_math_acosh(v) __builtin_acosh(v)
  443. #elif defined(SIMDE_MATH_HAVE_CMATH)
  444. #define simde_math_acosh(v) std::acosh(v)
  445. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  446. #define simde_math_acosh(v) acosh(v)
  447. #endif
  448. #endif
  449. #if !defined(simde_math_acoshf)
  450. #if SIMDE_MATH_BUILTIN_LIBM(acoshf)
  451. #define simde_math_acoshf(v) __builtin_acoshf(v)
  452. #elif defined(SIMDE_MATH_HAVE_CMATH)
  453. #define simde_math_acoshf(v) std::acosh(v)
  454. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  455. #define simde_math_acoshf(v) acoshf(v)
  456. #endif
  457. #endif
  458. #if !defined(simde_math_asin)
  459. #if SIMDE_MATH_BUILTIN_LIBM(asin)
  460. #define simde_math_asin(v) __builtin_asin(v)
  461. #elif defined(SIMDE_MATH_HAVE_CMATH)
  462. #define simde_math_asin(v) std::asin(v)
  463. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  464. #define simde_math_asin(v) asin(v)
  465. #endif
  466. #endif
  467. #if !defined(simde_math_asinf)
  468. #if SIMDE_MATH_BUILTIN_LIBM(asinf)
  469. #define simde_math_asinf(v) __builtin_asinf(v)
  470. #elif defined(SIMDE_MATH_HAVE_CMATH)
  471. #define simde_math_asinf(v) std::asin(v)
  472. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  473. #define simde_math_asinf(v) asinf(v)
  474. #endif
  475. #endif
  476. #if !defined(simde_math_asinh)
  477. #if SIMDE_MATH_BUILTIN_LIBM(asinh)
  478. #define simde_math_asinh(v) __builtin_asinh(v)
  479. #elif defined(SIMDE_MATH_HAVE_CMATH)
  480. #define simde_math_asinh(v) std::asinh(v)
  481. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  482. #define simde_math_asinh(v) asinh(v)
  483. #endif
  484. #endif
  485. #if !defined(simde_math_asinhf)
  486. #if SIMDE_MATH_BUILTIN_LIBM(asinhf)
  487. #define simde_math_asinhf(v) __builtin_asinhf(v)
  488. #elif defined(SIMDE_MATH_HAVE_CMATH)
  489. #define simde_math_asinhf(v) std::asinh(v)
  490. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  491. #define simde_math_asinhf(v) asinhf(v)
  492. #endif
  493. #endif
  494. #if !defined(simde_math_atan)
  495. #if SIMDE_MATH_BUILTIN_LIBM(atan)
  496. #define simde_math_atan(v) __builtin_atan(v)
  497. #elif defined(SIMDE_MATH_HAVE_CMATH)
  498. #define simde_math_atan(v) std::atan(v)
  499. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  500. #define simde_math_atan(v) atan(v)
  501. #endif
  502. #endif
  503. #if !defined(simde_math_atan2)
  504. #if SIMDE_MATH_BUILTIN_LIBM(atan2)
  505. #define simde_math_atan2(y, x) __builtin_atan2(y, x)
  506. #elif defined(SIMDE_MATH_HAVE_CMATH)
  507. #define simde_math_atan2(y, x) std::atan2(y, x)
  508. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  509. #define simde_math_atan2(y, x) atan2(y, x)
  510. #endif
  511. #endif
  512. #if !defined(simde_math_atan2f)
  513. #if SIMDE_MATH_BUILTIN_LIBM(atan2f)
  514. #define simde_math_atan2f(y, x) __builtin_atan2f(y, x)
  515. #elif defined(SIMDE_MATH_HAVE_CMATH)
  516. #define simde_math_atan2f(y, x) std::atan2(y, x)
  517. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  518. #define simde_math_atan2f(y, x) atan2f(y, x)
  519. #endif
  520. #endif
  521. #if !defined(simde_math_atanf)
  522. #if SIMDE_MATH_BUILTIN_LIBM(atanf)
  523. #define simde_math_atanf(v) __builtin_atanf(v)
  524. #elif defined(SIMDE_MATH_HAVE_CMATH)
  525. #define simde_math_atanf(v) std::atan(v)
  526. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  527. #define simde_math_atanf(v) atanf(v)
  528. #endif
  529. #endif
  530. #if !defined(simde_math_atanh)
  531. #if SIMDE_MATH_BUILTIN_LIBM(atanh)
  532. #define simde_math_atanh(v) __builtin_atanh(v)
  533. #elif defined(SIMDE_MATH_HAVE_CMATH)
  534. #define simde_math_atanh(v) std::atanh(v)
  535. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  536. #define simde_math_atanh(v) atanh(v)
  537. #endif
  538. #endif
  539. #if !defined(simde_math_atanhf)
  540. #if SIMDE_MATH_BUILTIN_LIBM(atanhf)
  541. #define simde_math_atanhf(v) __builtin_atanhf(v)
  542. #elif defined(SIMDE_MATH_HAVE_CMATH)
  543. #define simde_math_atanhf(v) std::atanh(v)
  544. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  545. #define simde_math_atanhf(v) atanhf(v)
  546. #endif
  547. #endif
  548. #if !defined(simde_math_cbrt)
  549. #if SIMDE_MATH_BUILTIN_LIBM(cbrt)
  550. #define simde_math_cbrt(v) __builtin_cbrt(v)
  551. #elif defined(SIMDE_MATH_HAVE_CMATH)
  552. #define simde_math_cbrt(v) std::cbrt(v)
  553. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  554. #define simde_math_cbrt(v) cbrt(v)
  555. #endif
  556. #endif
  557. #if !defined(simde_math_cbrtf)
  558. #if SIMDE_MATH_BUILTIN_LIBM(cbrtf)
  559. #define simde_math_cbrtf(v) __builtin_cbrtf(v)
  560. #elif defined(SIMDE_MATH_HAVE_CMATH)
  561. #define simde_math_cbrtf(v) std::cbrt(v)
  562. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  563. #define simde_math_cbrtf(v) cbrtf(v)
  564. #endif
  565. #endif
  566. #if !defined(simde_math_ceil)
  567. #if SIMDE_MATH_BUILTIN_LIBM(ceil)
  568. #define simde_math_ceil(v) __builtin_ceil(v)
  569. #elif defined(SIMDE_MATH_HAVE_CMATH)
  570. #define simde_math_ceil(v) std::ceil(v)
  571. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  572. #define simde_math_ceil(v) ceil(v)
  573. #endif
  574. #endif
  575. #if !defined(simde_math_ceilf)
  576. #if SIMDE_MATH_BUILTIN_LIBM(ceilf)
  577. #define simde_math_ceilf(v) __builtin_ceilf(v)
  578. #elif defined(SIMDE_MATH_HAVE_CMATH)
  579. #define simde_math_ceilf(v) std::ceil(v)
  580. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  581. #define simde_math_ceilf(v) ceilf(v)
  582. #endif
  583. #endif
  584. #if !defined(simde_math_copysign)
  585. #if SIMDE_MATH_BUILTIN_LIBM(copysign)
  586. #define simde_math_copysign(x, y) __builtin_copysign(x, y)
  587. #elif defined(SIMDE_MATH_HAVE_CMATH)
  588. #define simde_math_copysign(x, y) std::copysign(x, y)
  589. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  590. #define simde_math_copysign(x, y) copysign(x, y)
  591. #endif
  592. #endif
  593. #if !defined(simde_math_copysignf)
  594. #if SIMDE_MATH_BUILTIN_LIBM(copysignf)
  595. #define simde_math_copysignf(x, y) __builtin_copysignf(x, y)
  596. #elif defined(SIMDE_MATH_HAVE_CMATH)
  597. #define simde_math_copysignf(x, y) std::copysignf(x, y)
  598. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  599. #define simde_math_copysignf(x, y) copysignf(x, y)
  600. #endif
  601. #endif
  602. #if !defined(simde_math_cos)
  603. #if SIMDE_MATH_BUILTIN_LIBM(cos)
  604. #define simde_math_cos(v) __builtin_cos(v)
  605. #elif defined(SIMDE_MATH_HAVE_CMATH)
  606. #define simde_math_cos(v) std::cos(v)
  607. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  608. #define simde_math_cos(v) cos(v)
  609. #endif
  610. #endif
  611. #if !defined(simde_math_cosf)
  612. #if defined(SIMDE_MATH_SLEEF_ENABLE)
  613. #if SIMDE_ACCURACY_PREFERENCE < 1
  614. #define simde_math_cosf(v) Sleef_cosf_u35(v)
  615. #else
  616. #define simde_math_cosf(v) Sleef_cosf_u10(v)
  617. #endif
  618. #elif SIMDE_MATH_BUILTIN_LIBM(cosf)
  619. #define simde_math_cosf(v) __builtin_cosf(v)
  620. #elif defined(SIMDE_MATH_HAVE_CMATH)
  621. #define simde_math_cosf(v) std::cos(v)
  622. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  623. #define simde_math_cosf(v) cosf(v)
  624. #endif
  625. #endif
  626. #if !defined(simde_math_cosh)
  627. #if SIMDE_MATH_BUILTIN_LIBM(cosh)
  628. #define simde_math_cosh(v) __builtin_cosh(v)
  629. #elif defined(SIMDE_MATH_HAVE_CMATH)
  630. #define simde_math_cosh(v) std::cosh(v)
  631. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  632. #define simde_math_cosh(v) cosh(v)
  633. #endif
  634. #endif
  635. #if !defined(simde_math_coshf)
  636. #if SIMDE_MATH_BUILTIN_LIBM(coshf)
  637. #define simde_math_coshf(v) __builtin_coshf(v)
  638. #elif defined(SIMDE_MATH_HAVE_CMATH)
  639. #define simde_math_coshf(v) std::cosh(v)
  640. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  641. #define simde_math_coshf(v) coshf(v)
  642. #endif
  643. #endif
  644. #if !defined(simde_math_erf)
  645. #if SIMDE_MATH_BUILTIN_LIBM(erf)
  646. #define simde_math_erf(v) __builtin_erf(v)
  647. #elif defined(SIMDE_MATH_HAVE_CMATH)
  648. #define simde_math_erf(v) std::erf(v)
  649. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  650. #define simde_math_erf(v) erf(v)
  651. #endif
  652. #endif
  653. #if !defined(simde_math_erff)
  654. #if SIMDE_MATH_BUILTIN_LIBM(erff)
  655. #define simde_math_erff(v) __builtin_erff(v)
  656. #elif defined(SIMDE_MATH_HAVE_CMATH)
  657. #define simde_math_erff(v) std::erf(v)
  658. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  659. #define simde_math_erff(v) erff(v)
  660. #endif
  661. #endif
  662. #if !defined(simde_math_erfc)
  663. #if SIMDE_MATH_BUILTIN_LIBM(erfc)
  664. #define simde_math_erfc(v) __builtin_erfc(v)
  665. #elif defined(SIMDE_MATH_HAVE_CMATH)
  666. #define simde_math_erfc(v) std::erfc(v)
  667. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  668. #define simde_math_erfc(v) erfc(v)
  669. #endif
  670. #endif
  671. #if !defined(simde_math_erfcf)
  672. #if SIMDE_MATH_BUILTIN_LIBM(erfcf)
  673. #define simde_math_erfcf(v) __builtin_erfcf(v)
  674. #elif defined(SIMDE_MATH_HAVE_CMATH)
  675. #define simde_math_erfcf(v) std::erfc(v)
  676. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  677. #define simde_math_erfcf(v) erfcf(v)
  678. #endif
  679. #endif
  680. #if !defined(simde_math_exp)
  681. #if SIMDE_MATH_BUILTIN_LIBM(exp)
  682. #define simde_math_exp(v) __builtin_exp(v)
  683. #elif defined(SIMDE_MATH_HAVE_CMATH)
  684. #define simde_math_exp(v) std::exp(v)
  685. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  686. #define simde_math_exp(v) exp(v)
  687. #endif
  688. #endif
  689. #if !defined(simde_math_expf)
  690. #if SIMDE_MATH_BUILTIN_LIBM(expf)
  691. #define simde_math_expf(v) __builtin_expf(v)
  692. #elif defined(SIMDE_MATH_HAVE_CMATH)
  693. #define simde_math_expf(v) std::exp(v)
  694. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  695. #define simde_math_expf(v) expf(v)
  696. #endif
  697. #endif
  698. #if !defined(simde_math_expm1)
  699. #if SIMDE_MATH_BUILTIN_LIBM(expm1)
  700. #define simde_math_expm1(v) __builtin_expm1(v)
  701. #elif defined(SIMDE_MATH_HAVE_CMATH)
  702. #define simde_math_expm1(v) std::expm1(v)
  703. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  704. #define simde_math_expm1(v) expm1(v)
  705. #endif
  706. #endif
  707. #if !defined(simde_math_expm1f)
  708. #if SIMDE_MATH_BUILTIN_LIBM(expm1f)
  709. #define simde_math_expm1f(v) __builtin_expm1f(v)
  710. #elif defined(SIMDE_MATH_HAVE_CMATH)
  711. #define simde_math_expm1f(v) std::expm1(v)
  712. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  713. #define simde_math_expm1f(v) expm1f(v)
  714. #endif
  715. #endif
  716. #if !defined(simde_math_exp2)
  717. #if SIMDE_MATH_BUILTIN_LIBM(exp2)
  718. #define simde_math_exp2(v) __builtin_exp2(v)
  719. #elif defined(SIMDE_MATH_HAVE_CMATH)
  720. #define simde_math_exp2(v) std::exp2(v)
  721. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  722. #define simde_math_exp2(v) exp2(v)
  723. #endif
  724. #endif
  725. #if !defined(simde_math_exp2f)
  726. #if SIMDE_MATH_BUILTIN_LIBM(exp2f)
  727. #define simde_math_exp2f(v) __builtin_exp2f(v)
  728. #elif defined(SIMDE_MATH_HAVE_CMATH)
  729. #define simde_math_exp2f(v) std::exp2(v)
  730. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  731. #define simde_math_exp2f(v) exp2f(v)
  732. #endif
  733. #endif
  734. #if HEDLEY_HAS_BUILTIN(__builtin_exp10) || HEDLEY_GCC_VERSION_CHECK(3, 4, 0)
  735. #define simde_math_exp10(v) __builtin_exp10(v)
  736. #else
  737. #define simde_math_exp10(v) pow(10.0, (v))
  738. #endif
  739. #if HEDLEY_HAS_BUILTIN(__builtin_exp10f) || HEDLEY_GCC_VERSION_CHECK(3, 4, 0)
  740. #define simde_math_exp10f(v) __builtin_exp10f(v)
  741. #else
  742. #define simde_math_exp10f(v) powf(10.0f, (v))
  743. #endif
  744. #if !defined(simde_math_fabs)
  745. #if SIMDE_MATH_BUILTIN_LIBM(fabs)
  746. #define simde_math_fabs(v) __builtin_fabs(v)
  747. #elif defined(SIMDE_MATH_HAVE_CMATH)
  748. #define simde_math_fabs(v) std::fabs(v)
  749. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  750. #define simde_math_fabs(v) fabs(v)
  751. #endif
  752. #endif
  753. #if !defined(simde_math_fabsf)
  754. #if SIMDE_MATH_BUILTIN_LIBM(fabsf)
  755. #define simde_math_fabsf(v) __builtin_fabsf(v)
  756. #elif defined(SIMDE_MATH_HAVE_CMATH)
  757. #define simde_math_fabsf(v) std::fabs(v)
  758. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  759. #define simde_math_fabsf(v) fabsf(v)
  760. #endif
  761. #endif
  762. #if !defined(simde_math_floor)
  763. #if SIMDE_MATH_BUILTIN_LIBM(floor)
  764. #define simde_math_floor(v) __builtin_floor(v)
  765. #elif defined(SIMDE_MATH_HAVE_CMATH)
  766. #define simde_math_floor(v) std::floor(v)
  767. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  768. #define simde_math_floor(v) floor(v)
  769. #endif
  770. #endif
  771. #if !defined(simde_math_floorf)
  772. #if SIMDE_MATH_BUILTIN_LIBM(floorf)
  773. #define simde_math_floorf(v) __builtin_floorf(v)
  774. #elif defined(SIMDE_MATH_HAVE_CMATH)
  775. #define simde_math_floorf(v) std::floor(v)
  776. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  777. #define simde_math_floorf(v) floorf(v)
  778. #endif
  779. #endif
  780. #if !defined(simde_math_fma)
  781. #if SIMDE_MATH_BUILTIN_LIBM(fma)
  782. #define simde_math_fma(x, y, z) __builtin_fma(x, y, z)
  783. #elif defined(SIMDE_MATH_HAVE_CMATH)
  784. #define simde_math_fma(x, y, z) std::fma(x, y, z)
  785. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  786. #define simde_math_fma(x, y, z) fma(x, y, z)
  787. #endif
  788. #endif
  789. #if !defined(simde_math_fmaf)
  790. #if SIMDE_MATH_BUILTIN_LIBM(fmaf)
  791. #define simde_math_fmaf(x, y, z) __builtin_fmaf(x, y, z)
  792. #elif defined(SIMDE_MATH_HAVE_CMATH)
  793. #define simde_math_fmaf(x, y, z) std::fma(x, y, z)
  794. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  795. #define simde_math_fmaf(x, y, z) fmaf(x, y, z)
  796. #endif
  797. #endif
  798. #if !defined(simde_math_fmax)
  799. #if SIMDE_MATH_BUILTIN_LIBM(fmax)
  800. #define simde_math_fmax(x, y, z) __builtin_fmax(x, y, z)
  801. #elif defined(SIMDE_MATH_HAVE_CMATH)
  802. #define simde_math_fmax(x, y, z) std::fmax(x, y, z)
  803. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  804. #define simde_math_fmax(x, y, z) fmax(x, y, z)
  805. #endif
  806. #endif
  807. #if !defined(simde_math_fmaxf)
  808. #if SIMDE_MATH_BUILTIN_LIBM(fmaxf)
  809. #define simde_math_fmaxf(x, y, z) __builtin_fmaxf(x, y, z)
  810. #elif defined(SIMDE_MATH_HAVE_CMATH)
  811. #define simde_math_fmaxf(x, y, z) std::fmax(x, y, z)
  812. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  813. #define simde_math_fmaxf(x, y, z) fmaxf(x, y, z)
  814. #endif
  815. #endif
  816. #if !defined(simde_math_hypot)
  817. #if SIMDE_MATH_BUILTIN_LIBM(hypot)
  818. #define simde_math_hypot(y, x) __builtin_hypot(y, x)
  819. #elif defined(SIMDE_MATH_HAVE_CMATH)
  820. #define simde_math_hypot(y, x) std::hypot(y, x)
  821. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  822. #define simde_math_hypot(y, x) hypot(y, x)
  823. #endif
  824. #endif
  825. #if !defined(simde_math_hypotf)
  826. #if SIMDE_MATH_BUILTIN_LIBM(hypotf)
  827. #define simde_math_hypotf(y, x) __builtin_hypotf(y, x)
  828. #elif defined(SIMDE_MATH_HAVE_CMATH)
  829. #define simde_math_hypotf(y, x) std::hypot(y, x)
  830. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  831. #define simde_math_hypotf(y, x) hypotf(y, x)
  832. #endif
  833. #endif
  834. #if !defined(simde_math_log)
  835. #if SIMDE_MATH_BUILTIN_LIBM(log)
  836. #define simde_math_log(v) __builtin_log(v)
  837. #elif defined(SIMDE_MATH_HAVE_CMATH)
  838. #define simde_math_log(v) std::log(v)
  839. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  840. #define simde_math_log(v) log(v)
  841. #endif
  842. #endif
  843. #if !defined(simde_math_logf)
  844. #if SIMDE_MATH_BUILTIN_LIBM(logf)
  845. #define simde_math_logf(v) __builtin_logf(v)
  846. #elif defined(SIMDE_MATH_HAVE_CMATH)
  847. #define simde_math_logf(v) std::log(v)
  848. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  849. #define simde_math_logf(v) logf(v)
  850. #endif
  851. #endif
  852. #if !defined(simde_math_logb)
  853. #if SIMDE_MATH_BUILTIN_LIBM(logb)
  854. #define simde_math_logb(v) __builtin_logb(v)
  855. #elif defined(SIMDE_MATH_HAVE_CMATH)
  856. #define simde_math_logb(v) std::logb(v)
  857. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  858. #define simde_math_logb(v) logb(v)
  859. #endif
  860. #endif
  861. #if !defined(simde_math_logbf)
  862. #if SIMDE_MATH_BUILTIN_LIBM(logbf)
  863. #define simde_math_logbf(v) __builtin_logbf(v)
  864. #elif defined(SIMDE_MATH_HAVE_CMATH)
  865. #define simde_math_logbf(v) std::logb(v)
  866. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  867. #define simde_math_logbf(v) logbf(v)
  868. #endif
  869. #endif
  870. #if !defined(simde_math_log1p)
  871. #if SIMDE_MATH_BUILTIN_LIBM(log1p)
  872. #define simde_math_log1p(v) __builtin_log1p(v)
  873. #elif defined(SIMDE_MATH_HAVE_CMATH)
  874. #define simde_math_log1p(v) std::log1p(v)
  875. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  876. #define simde_math_log1p(v) log1p(v)
  877. #endif
  878. #endif
  879. #if !defined(simde_math_log1pf)
  880. #if SIMDE_MATH_BUILTIN_LIBM(log1pf)
  881. #define simde_math_log1pf(v) __builtin_log1pf(v)
  882. #elif defined(SIMDE_MATH_HAVE_CMATH)
  883. #define simde_math_log1pf(v) std::log1p(v)
  884. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  885. #define simde_math_log1pf(v) log1pf(v)
  886. #endif
  887. #endif
  888. #if !defined(simde_math_log2)
  889. #if SIMDE_MATH_BUILTIN_LIBM(log2)
  890. #define simde_math_log2(v) __builtin_log2(v)
  891. #elif defined(SIMDE_MATH_HAVE_CMATH)
  892. #define simde_math_log2(v) std::log2(v)
  893. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  894. #define simde_math_log2(v) log2(v)
  895. #endif
  896. #endif
  897. #if !defined(simde_math_log2f)
  898. #if SIMDE_MATH_BUILTIN_LIBM(log2f)
  899. #define simde_math_log2f(v) __builtin_log2f(v)
  900. #elif defined(SIMDE_MATH_HAVE_CMATH)
  901. #define simde_math_log2f(v) std::log2(v)
  902. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  903. #define simde_math_log2f(v) log2f(v)
  904. #endif
  905. #endif
  906. #if !defined(simde_math_log10)
  907. #if SIMDE_MATH_BUILTIN_LIBM(log10)
  908. #define simde_math_log10(v) __builtin_log10(v)
  909. #elif defined(SIMDE_MATH_HAVE_CMATH)
  910. #define simde_math_log10(v) std::log10(v)
  911. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  912. #define simde_math_log10(v) log10(v)
  913. #endif
  914. #endif
  915. #if !defined(simde_math_log10f)
  916. #if SIMDE_MATH_BUILTIN_LIBM(log10f)
  917. #define simde_math_log10f(v) __builtin_log10f(v)
  918. #elif defined(SIMDE_MATH_HAVE_CMATH)
  919. #define simde_math_log10f(v) std::log10(v)
  920. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  921. #define simde_math_log10f(v) log10f(v)
  922. #endif
  923. #endif
  924. #if !defined(simde_math_modf)
  925. #if SIMDE_MATH_BUILTIN_LIBM(modf)
  926. #define simde_math_modf(x, iptr) __builtin_modf(x, iptr)
  927. #elif defined(SIMDE_MATH_HAVE_CMATH)
  928. #define simde_math_modf(x, iptr) std::modf(x, iptr)
  929. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  930. #define simde_math_modf(x, iptr) modf(x, iptr)
  931. #endif
  932. #endif
  933. #if !defined(simde_math_modff)
  934. #if SIMDE_MATH_BUILTIN_LIBM(modff)
  935. #define simde_math_modff(x, iptr) __builtin_modff(x, iptr)
  936. #elif defined(SIMDE_MATH_HAVE_CMATH)
  937. #define simde_math_modff(x, iptr) std::modf(x, iptr)
  938. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  939. #define simde_math_modff(x, iptr) modff(x, iptr)
  940. #endif
  941. #endif
  942. #if !defined(simde_math_nearbyint)
  943. #if SIMDE_MATH_BUILTIN_LIBM(nearbyint)
  944. #define simde_math_nearbyint(v) __builtin_nearbyint(v)
  945. #elif defined(SIMDE_MATH_HAVE_CMATH)
  946. #define simde_math_nearbyint(v) std::nearbyint(v)
  947. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  948. #define simde_math_nearbyint(v) nearbyint(v)
  949. #endif
  950. #endif
  951. #if !defined(simde_math_nearbyintf)
  952. #if SIMDE_MATH_BUILTIN_LIBM(nearbyintf)
  953. #define simde_math_nearbyintf(v) __builtin_nearbyintf(v)
  954. #elif defined(SIMDE_MATH_HAVE_CMATH)
  955. #define simde_math_nearbyintf(v) std::nearbyint(v)
  956. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  957. #define simde_math_nearbyintf(v) nearbyintf(v)
  958. #endif
  959. #endif
  960. #if !defined(simde_math_pow)
  961. #if SIMDE_MATH_BUILTIN_LIBM(pow)
  962. #define simde_math_pow(y, x) __builtin_pow(y, x)
  963. #elif defined(SIMDE_MATH_HAVE_CMATH)
  964. #define simde_math_pow(y, x) std::pow(y, x)
  965. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  966. #define simde_math_pow(y, x) pow(y, x)
  967. #endif
  968. #endif
  969. #if !defined(simde_math_powf)
  970. #if SIMDE_MATH_BUILTIN_LIBM(powf)
  971. #define simde_math_powf(y, x) __builtin_powf(y, x)
  972. #elif defined(SIMDE_MATH_HAVE_CMATH)
  973. #define simde_math_powf(y, x) std::pow(y, x)
  974. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  975. #define simde_math_powf(y, x) powf(y, x)
  976. #endif
  977. #endif
  978. #if !defined(simde_math_rint)
  979. #if SIMDE_MATH_BUILTIN_LIBM(rint)
  980. #define simde_math_rint(v) __builtin_rint(v)
  981. #elif defined(SIMDE_MATH_HAVE_CMATH)
  982. #define simde_math_rint(v) std::rint(v)
  983. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  984. #define simde_math_rint(v) rint(v)
  985. #endif
  986. #endif
  987. #if !defined(simde_math_rintf)
  988. #if SIMDE_MATH_BUILTIN_LIBM(rintf)
  989. #define simde_math_rintf(v) __builtin_rintf(v)
  990. #elif defined(SIMDE_MATH_HAVE_CMATH)
  991. #define simde_math_rintf(v) std::rint(v)
  992. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  993. #define simde_math_rintf(v) rintf(v)
  994. #endif
  995. #endif
  996. #if !defined(simde_math_round)
  997. #if SIMDE_MATH_BUILTIN_LIBM(round)
  998. #define simde_math_round(v) __builtin_round(v)
  999. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1000. #define simde_math_round(v) std::round(v)
  1001. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1002. #define simde_math_round(v) round(v)
  1003. #endif
  1004. #endif
  1005. #if !defined(simde_math_roundf)
  1006. #if SIMDE_MATH_BUILTIN_LIBM(roundf)
  1007. #define simde_math_roundf(v) __builtin_roundf(v)
  1008. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1009. #define simde_math_roundf(v) std::round(v)
  1010. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1011. #define simde_math_roundf(v) roundf(v)
  1012. #endif
  1013. #endif
  1014. #if !defined(simde_math_roundeven)
  1015. #if HEDLEY_HAS_BUILTIN(__builtin_roundeven) || \
  1016. HEDLEY_GCC_VERSION_CHECK(10, 0, 0)
  1017. #define simde_math_roundeven(v) __builtin_roundeven(v)
  1018. #elif defined(simde_math_round) && defined(simde_math_fabs)
  1019. static HEDLEY_INLINE double simde_math_roundeven(double v)
  1020. {
  1021. double rounded = simde_math_round(v);
  1022. double diff = rounded - v;
  1023. if (HEDLEY_UNLIKELY(simde_math_fabs(diff) == 0.5) &&
  1024. (HEDLEY_STATIC_CAST(int64_t, rounded) & 1)) {
  1025. rounded = v - diff;
  1026. }
  1027. return rounded;
  1028. }
  1029. #define simde_math_roundeven simde_math_roundeven
  1030. #endif
  1031. #endif
  1032. #if !defined(simde_math_roundevenf)
  1033. #if HEDLEY_HAS_BUILTIN(__builtin_roundevenf) || \
  1034. HEDLEY_GCC_VERSION_CHECK(10, 0, 0)
  1035. #define simde_math_roundevenf(v) __builtin_roundevenf(v)
  1036. #elif defined(simde_math_roundf) && defined(simde_math_fabsf)
  1037. static HEDLEY_INLINE float simde_math_roundevenf(float v)
  1038. {
  1039. float rounded = simde_math_roundf(v);
  1040. float diff = rounded - v;
  1041. if (HEDLEY_UNLIKELY(simde_math_fabsf(diff) == 0.5f) &&
  1042. (HEDLEY_STATIC_CAST(int32_t, rounded) & 1)) {
  1043. rounded = v - diff;
  1044. }
  1045. return rounded;
  1046. }
  1047. #define simde_math_roundevenf simde_math_roundevenf
  1048. #endif
  1049. #endif
  1050. #if !defined(simde_math_sin)
  1051. #if SIMDE_MATH_BUILTIN_LIBM(sin)
  1052. #define simde_math_sin(v) __builtin_sin(v)
  1053. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1054. #define simde_math_sin(v) std::sin(v)
  1055. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1056. #define simde_math_sin(v) sin(v)
  1057. #endif
  1058. #endif
  1059. #if !defined(simde_math_sinf)
  1060. #if SIMDE_MATH_BUILTIN_LIBM(sinf)
  1061. #define simde_math_sinf(v) __builtin_sinf(v)
  1062. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1063. #define simde_math_sinf(v) std::sin(v)
  1064. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1065. #define simde_math_sinf(v) sinf(v)
  1066. #endif
  1067. #endif
  1068. #if !defined(simde_math_sinh)
  1069. #if SIMDE_MATH_BUILTIN_LIBM(sinh)
  1070. #define simde_math_sinh(v) __builtin_sinh(v)
  1071. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1072. #define simde_math_sinh(v) std::sinh(v)
  1073. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1074. #define simde_math_sinh(v) sinh(v)
  1075. #endif
  1076. #endif
  1077. #if !defined(simde_math_sinhf)
  1078. #if SIMDE_MATH_BUILTIN_LIBM(sinhf)
  1079. #define simde_math_sinhf(v) __builtin_sinhf(v)
  1080. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1081. #define simde_math_sinhf(v) std::sinh(v)
  1082. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1083. #define simde_math_sinhf(v) sinhf(v)
  1084. #endif
  1085. #endif
  1086. #if !defined(simde_math_sqrt)
  1087. #if SIMDE_MATH_BUILTIN_LIBM(sqrt)
  1088. #define simde_math_sqrt(v) __builtin_sqrt(v)
  1089. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1090. #define simde_math_sqrt(v) std::sqrt(v)
  1091. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1092. #define simde_math_sqrt(v) sqrt(v)
  1093. #endif
  1094. #endif
  1095. #if !defined(simde_math_sqrtf)
  1096. #if SIMDE_MATH_BUILTIN_LIBM(sqrtf)
  1097. #define simde_math_sqrtf(v) __builtin_sqrtf(v)
  1098. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1099. #define simde_math_sqrtf(v) std::sqrt(v)
  1100. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1101. #define simde_math_sqrtf(v) sqrtf(v)
  1102. #endif
  1103. #endif
  1104. #if !defined(simde_math_tan)
  1105. #if SIMDE_MATH_BUILTIN_LIBM(tan)
  1106. #define simde_math_tan(v) __builtin_tan(v)
  1107. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1108. #define simde_math_tan(v) std::tan(v)
  1109. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1110. #define simde_math_tan(v) tan(v)
  1111. #endif
  1112. #endif
  1113. #if !defined(simde_math_tanf)
  1114. #if SIMDE_MATH_BUILTIN_LIBM(tanf)
  1115. #define simde_math_tanf(v) __builtin_tanf(v)
  1116. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1117. #define simde_math_tanf(v) std::tan(v)
  1118. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1119. #define simde_math_tanf(v) tanf(v)
  1120. #endif
  1121. #endif
  1122. #if !defined(simde_math_tanh)
  1123. #if SIMDE_MATH_BUILTIN_LIBM(tanh)
  1124. #define simde_math_tanh(v) __builtin_tanh(v)
  1125. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1126. #define simde_math_tanh(v) std::tanh(v)
  1127. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1128. #define simde_math_tanh(v) tanh(v)
  1129. #endif
  1130. #endif
  1131. #if !defined(simde_math_tanhf)
  1132. #if SIMDE_MATH_BUILTIN_LIBM(tanhf)
  1133. #define simde_math_tanhf(v) __builtin_tanhf(v)
  1134. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1135. #define simde_math_tanhf(v) std::tanh(v)
  1136. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1137. #define simde_math_tanhf(v) tanhf(v)
  1138. #endif
  1139. #endif
  1140. #if !defined(simde_math_trunc)
  1141. #if SIMDE_MATH_BUILTIN_LIBM(trunc)
  1142. #define simde_math_trunc(v) __builtin_trunc(v)
  1143. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1144. #define simde_math_trunc(v) std::trunc(v)
  1145. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1146. #define simde_math_trunc(v) trunc(v)
  1147. #endif
  1148. #endif
  1149. #if !defined(simde_math_truncf)
  1150. #if SIMDE_MATH_BUILTIN_LIBM(truncf)
  1151. #define simde_math_truncf(v) __builtin_truncf(v)
  1152. #elif defined(SIMDE_MATH_HAVE_CMATH)
  1153. #define simde_math_truncf(v) std::trunc(v)
  1154. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1155. #define simde_math_truncf(v) truncf(v)
  1156. #endif
  1157. #endif
  1158. /*** Complex functions ***/
  1159. #if !defined(simde_math_cexp)
  1160. #if SIMDE_MATH_BUILTIN_LIBM(cexp)
  1161. #define simde_math_cexp(v) __builtin_cexp(v)
  1162. #elif defined(__cplusplus)
  1163. #define simde_math_cexp(v) std::cexp(v)
  1164. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1165. #define simde_math_cexp(v) cexp(v)
  1166. #endif
  1167. #endif
  1168. #if !defined(simde_math_cexpf)
  1169. #if SIMDE_MATH_BUILTIN_LIBM(cexpf)
  1170. #define simde_math_cexpf(v) __builtin_cexpf(v)
  1171. #elif defined(__cplusplus)
  1172. #define simde_math_cexpf(v) std::exp(v)
  1173. #elif defined(SIMDE_MATH_HAVE_MATH_H)
  1174. #define simde_math_cexpf(v) cexpf(v)
  1175. #endif
  1176. #endif
  1177. /*** Additional functions not in libm ***/
  1178. #if defined(simde_math_fabs) && defined(simde_math_sqrt) && \
  1179. defined(simde_math_exp)
  1180. static HEDLEY_INLINE double simde_math_cdfnorm(double x)
  1181. {
  1182. /* https://www.johndcook.com/blog/cpp_phi/
  1183. * Public Domain */
  1184. static const double a1 = 0.254829592;
  1185. static const double a2 = -0.284496736;
  1186. static const double a3 = 1.421413741;
  1187. static const double a4 = -1.453152027;
  1188. static const double a5 = 1.061405429;
  1189. static const double p = 0.3275911;
  1190. const int sign = x < 0;
  1191. x = simde_math_fabs(x) / simde_math_sqrt(2.0);
  1192. /* A&S formula 7.1.26 */
  1193. double t = 1.0 / (1.0 + p * x);
  1194. double y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t *
  1195. simde_math_exp(-x * x);
  1196. return 0.5 * (1.0 + (sign ? -y : y));
  1197. }
  1198. #define simde_math_cdfnorm simde_math_cdfnorm
  1199. #endif
  1200. #if defined(simde_math_fabsf) && defined(simde_math_sqrtf) && \
  1201. defined(simde_math_expf)
  1202. static HEDLEY_INLINE float simde_math_cdfnormf(float x)
  1203. {
  1204. /* https://www.johndcook.com/blog/cpp_phi/
  1205. * Public Domain */
  1206. static const float a1 = 0.254829592f;
  1207. static const float a2 = -0.284496736f;
  1208. static const float a3 = 1.421413741f;
  1209. static const float a4 = -1.453152027f;
  1210. static const float a5 = 1.061405429f;
  1211. static const float p = 0.3275911f;
  1212. const int sign = x < 0;
  1213. x = simde_math_fabsf(x) / simde_math_sqrtf(2.0f);
  1214. /* A&S formula 7.1.26 */
  1215. float t = 1.0f / (1.0f + p * x);
  1216. float y = 1.0f - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t *
  1217. simde_math_expf(-x * x);
  1218. return 0.5f * (1.0f + (sign ? -y : y));
  1219. }
  1220. #define simde_math_cdfnormf simde_math_cdfnormf
  1221. #endif
  1222. HEDLEY_DIAGNOSTIC_PUSH
  1223. SIMDE_DIAGNOSTIC_DISABLE_FLOAT_EQUAL_
  1224. #if !defined(simde_math_cdfnorminv) && defined(simde_math_log) && \
  1225. defined(simde_math_sqrt)
  1226. /*https://web.archive.org/web/20150910081113/http://home.online.no/~pjacklam/notes/invnorm/impl/sprouse/ltqnorm.c*/
  1227. static HEDLEY_INLINE double simde_math_cdfnorminv(double p)
  1228. {
  1229. static const double a[] = {
  1230. -3.969683028665376e+01, 2.209460984245205e+02,
  1231. -2.759285104469687e+02, 1.383577518672690e+02,
  1232. -3.066479806614716e+01, 2.506628277459239e+00};
  1233. static const double b[] = {-5.447609879822406e+01,
  1234. 1.615858368580409e+02,
  1235. -1.556989798598866e+02,
  1236. 6.680131188771972e+01,
  1237. -1.328068155288572e+01};
  1238. static const double c[] = {
  1239. -7.784894002430293e-03, -3.223964580411365e-01,
  1240. -2.400758277161838e+00, -2.549732539343734e+00,
  1241. 4.374664141464968e+00, 2.938163982698783e+00};
  1242. static const double d[] = {7.784695709041462e-03, 3.224671290700398e-01,
  1243. 2.445134137142996e+00,
  1244. 3.754408661907416e+00};
  1245. static const double low = 0.02425;
  1246. static const double high = 0.97575;
  1247. double q, r;
  1248. if (p < 0 || p > 1) {
  1249. return 0.0;
  1250. } else if (p == 0) {
  1251. return -SIMDE_MATH_INFINITY;
  1252. } else if (p == 1) {
  1253. return SIMDE_MATH_INFINITY;
  1254. } else if (p < low) {
  1255. q = simde_math_sqrt(-2.0 * simde_math_log(p));
  1256. return (((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q +
  1257. c[4]) * q +
  1258. c[5]) /
  1259. (((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1));
  1260. } else if (p > high) {
  1261. q = simde_math_sqrt(-2.0 * simde_math_log(1.0 - p));
  1262. return -(((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q +
  1263. c[4]) * q +
  1264. c[5]) /
  1265. (((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1));
  1266. } else {
  1267. q = p - 0.5;
  1268. r = q * q;
  1269. return (((((a[0] * r + a[1]) * r + a[2]) * r + a[3]) * r +
  1270. a[4]) * r +
  1271. a[5]) *
  1272. q /
  1273. (((((b[0] * r + b[1]) * r + b[2]) * r + b[3]) * r +
  1274. b[4]) * r +
  1275. 1);
  1276. }
  1277. }
  1278. #define simde_math_cdfnorminv simde_math_cdfnorminv
  1279. #endif
  1280. #if !defined(simde_math_cdfnorminvf) && defined(simde_math_logf) && \
  1281. defined(simde_math_sqrtf)
  1282. static HEDLEY_INLINE float simde_math_cdfnorminvf(float p)
  1283. {
  1284. static const float a[] = {
  1285. -3.969683028665376e+01f, 2.209460984245205e+02f,
  1286. -2.759285104469687e+02f, 1.383577518672690e+02f,
  1287. -3.066479806614716e+01f, 2.506628277459239e+00f};
  1288. static const float b[] = {-5.447609879822406e+01f,
  1289. 1.615858368580409e+02f,
  1290. -1.556989798598866e+02f,
  1291. 6.680131188771972e+01f,
  1292. -1.328068155288572e+01f};
  1293. static const float c[] = {
  1294. -7.784894002430293e-03f, -3.223964580411365e-01f,
  1295. -2.400758277161838e+00f, -2.549732539343734e+00f,
  1296. 4.374664141464968e+00f, 2.938163982698783e+00f};
  1297. static const float d[] = {7.784695709041462e-03f,
  1298. 3.224671290700398e-01f,
  1299. 2.445134137142996e+00f,
  1300. 3.754408661907416e+00f};
  1301. static const float low = 0.02425f;
  1302. static const float high = 0.97575f;
  1303. float q, r;
  1304. if (p < 0 || p > 1) {
  1305. return 0.0f;
  1306. } else if (p == 0) {
  1307. return -SIMDE_MATH_INFINITYF;
  1308. } else if (p == 1) {
  1309. return SIMDE_MATH_INFINITYF;
  1310. } else if (p < low) {
  1311. q = simde_math_sqrtf(-2.0f * simde_math_logf(p));
  1312. return (((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q +
  1313. c[4]) * q +
  1314. c[5]) /
  1315. (((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1));
  1316. } else if (p > high) {
  1317. q = simde_math_sqrtf(-2.0f * simde_math_logf(1.0f - p));
  1318. return -(((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q +
  1319. c[4]) * q +
  1320. c[5]) /
  1321. (((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1));
  1322. } else {
  1323. q = p - 0.5f;
  1324. r = q * q;
  1325. return (((((a[0] * r + a[1]) * r + a[2]) * r + a[3]) * r +
  1326. a[4]) * r +
  1327. a[5]) *
  1328. q /
  1329. (((((b[0] * r + b[1]) * r + b[2]) * r + b[3]) * r +
  1330. b[4]) * r +
  1331. 1);
  1332. }
  1333. }
  1334. #define simde_math_cdfnorminvf simde_math_cdfnorminvf
  1335. #endif
  1336. #if !defined(simde_math_erfinv) && defined(simde_math_log) && \
  1337. defined(simde_math_copysign) && defined(simde_math_sqrt)
  1338. static HEDLEY_INLINE double simde_math_erfinv(double x)
  1339. {
  1340. /* https://stackoverflow.com/questions/27229371/inverse-error-function-in-c
  1341. *
  1342. * The original answer on SO uses a constant of 0.147, but in my
  1343. * testing 0.14829094707965850830078125 gives a lower average absolute error
  1344. * (0.0001410958211636170744895935 vs. 0.0001465479290345683693885803).
  1345. * That said, if your goal is to minimize the *maximum* absolute
  1346. * error, 0.15449436008930206298828125 provides significantly better
  1347. * results; 0.0009250640869140625000000000 vs ~ 0.005. */
  1348. double tt1, tt2, lnx;
  1349. double sgn = simde_math_copysign(1.0, x);
  1350. x = (1.0 - x) * (1.0 + x);
  1351. lnx = simde_math_log(x);
  1352. tt1 = 2.0 / (SIMDE_MATH_PI * 0.14829094707965850830078125) + 0.5 * lnx;
  1353. tt2 = (1.0 / 0.14829094707965850830078125) * lnx;
  1354. return sgn * simde_math_sqrt(-tt1 + simde_math_sqrt(tt1 * tt1 - tt2));
  1355. }
  1356. #define simde_math_erfinv simde_math_erfinv
  1357. #endif
  1358. #if !defined(simde_math_erfinvf) && defined(simde_math_logf) && \
  1359. defined(simde_math_copysignf) && defined(simde_math_sqrtf)
  1360. static HEDLEY_INLINE float simde_math_erfinvf(float x)
  1361. {
  1362. float tt1, tt2, lnx;
  1363. float sgn = simde_math_copysignf(1.0f, x);
  1364. x = (1.0f - x) * (1.0f + x);
  1365. lnx = simde_math_logf(x);
  1366. tt1 = 2.0f / (SIMDE_MATH_PIF * 0.14829094707965850830078125f) +
  1367. 0.5f * lnx;
  1368. tt2 = (1.0f / 0.14829094707965850830078125f) * lnx;
  1369. return sgn * simde_math_sqrtf(-tt1 + simde_math_sqrtf(tt1 * tt1 - tt2));
  1370. }
  1371. #define simde_math_erfinvf simde_math_erfinvf
  1372. #endif
  1373. #if !defined(simde_math_erfcinv) && defined(simde_math_erfinv) && \
  1374. defined(simde_math_log) && defined(simde_math_sqrt)
  1375. static HEDLEY_INLINE double simde_math_erfcinv(double x)
  1376. {
  1377. if (x >= 0.0625 && x < 2.0) {
  1378. return simde_math_erfinv(1.0 - x);
  1379. } else if (x < 0.0625 && x >= 1.0e-100) {
  1380. double p[6] = {0.1550470003116, 1.382719649631, 0.690969348887,
  1381. -1.128081391617, 0.680544246825, -0.16444156791};
  1382. double q[3] = {0.155024849822, 1.385228141995, 1.000000000000};
  1383. const double t = 1.0 / simde_math_sqrt(-simde_math_log(x));
  1384. return (p[0] / t + p[1] +
  1385. t * (p[2] + t * (p[3] + t * (p[4] + t * p[5])))) /
  1386. (q[0] + t * (q[1] + t * (q[2])));
  1387. } else if (x < 1.0e-100 && x >= SIMDE_MATH_DBL_MIN) {
  1388. double p[4] = {0.00980456202915, 0.363667889171, 0.97302949837,
  1389. -0.5374947401};
  1390. double q[3] = {0.00980451277802, 0.363699971544,
  1391. 1.000000000000};
  1392. const double t = 1.0 / simde_math_sqrt(-simde_math_log(x));
  1393. return (p[0] / t + p[1] + t * (p[2] + t * p[3])) /
  1394. (q[0] + t * (q[1] + t * (q[2])));
  1395. } else if (!simde_math_isnormal(x)) {
  1396. return SIMDE_MATH_INFINITY;
  1397. } else {
  1398. return -SIMDE_MATH_INFINITY;
  1399. }
  1400. }
  1401. #define simde_math_erfcinv simde_math_erfcinv
  1402. #endif
  1403. #if !defined(simde_math_erfcinvf) && defined(simde_math_erfinvf) && \
  1404. defined(simde_math_logf) && defined(simde_math_sqrtf)
  1405. static HEDLEY_INLINE float simde_math_erfcinvf(float x)
  1406. {
  1407. if (x >= 0.0625f && x < 2.0f) {
  1408. return simde_math_erfinvf(1.0f - x);
  1409. } else if (x < 0.0625f && x >= SIMDE_MATH_FLT_MIN) {
  1410. static const float p[6] = {0.1550470003116f, 1.382719649631f,
  1411. 0.690969348887f, -1.128081391617f,
  1412. 0.680544246825f - 0.164441567910f};
  1413. static const float q[3] = {0.155024849822f, 1.385228141995f,
  1414. 1.000000000000f};
  1415. const float t = 1.0f / simde_math_sqrtf(-simde_math_logf(x));
  1416. return (p[0] / t + p[1] +
  1417. t * (p[2] + t * (p[3] + t * (p[4] + t * p[5])))) /
  1418. (q[0] + t * (q[1] + t * (q[2])));
  1419. } else if (x < SIMDE_MATH_FLT_MIN && simde_math_isnormalf(x)) {
  1420. static const float p[4] = {0.00980456202915f, 0.36366788917100f,
  1421. 0.97302949837000f,
  1422. -0.5374947401000f};
  1423. static const float q[3] = {0.00980451277802f, 0.36369997154400f,
  1424. 1.00000000000000f};
  1425. const float t = 1.0f / simde_math_sqrtf(-simde_math_logf(x));
  1426. return (p[0] / t + p[1] + t * (p[2] + t * p[3])) /
  1427. (q[0] + t * (q[1] + t * (q[2])));
  1428. } else {
  1429. return simde_math_isnormalf(x) ? -SIMDE_MATH_INFINITYF
  1430. : SIMDE_MATH_INFINITYF;
  1431. }
  1432. }
  1433. #define simde_math_erfcinvf simde_math_erfcinvf
  1434. #endif
  1435. HEDLEY_DIAGNOSTIC_POP
  1436. static HEDLEY_INLINE double simde_math_rad2deg(double radians)
  1437. {
  1438. return radians * SIMDE_MATH_180_OVER_PI;
  1439. }
  1440. static HEDLEY_INLINE float simde_math_rad2degf(float radians)
  1441. {
  1442. return radians * SIMDE_MATH_180_OVER_PIF;
  1443. }
  1444. static HEDLEY_INLINE double simde_math_deg2rad(double degrees)
  1445. {
  1446. return degrees * SIMDE_MATH_PI_OVER_180;
  1447. }
  1448. static HEDLEY_INLINE float simde_math_deg2radf(float degrees)
  1449. {
  1450. return degrees * (SIMDE_MATH_PI_OVER_180F);
  1451. }
  1452. /*** Saturated arithmetic ***/
  1453. static HEDLEY_INLINE int8_t simde_math_adds_i8(int8_t a, int8_t b)
  1454. {
  1455. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1456. return vqaddb_s8(a, b);
  1457. #else
  1458. uint8_t a_ = HEDLEY_STATIC_CAST(uint8_t, a);
  1459. uint8_t b_ = HEDLEY_STATIC_CAST(uint8_t, b);
  1460. uint8_t r_ = a_ + b_;
  1461. a_ = (a_ >> ((8 * sizeof(r_)) - 1)) + INT8_MAX;
  1462. if (HEDLEY_STATIC_CAST(int8_t, ((a_ ^ b_) | ~(b_ ^ r_))) >= 0) {
  1463. r_ = a_;
  1464. }
  1465. return HEDLEY_STATIC_CAST(int8_t, r_);
  1466. #endif
  1467. }
  1468. static HEDLEY_INLINE int16_t simde_math_adds_i16(int16_t a, int16_t b)
  1469. {
  1470. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1471. return vqaddh_s16(a, b);
  1472. #else
  1473. uint16_t a_ = HEDLEY_STATIC_CAST(uint16_t, a);
  1474. uint16_t b_ = HEDLEY_STATIC_CAST(uint16_t, b);
  1475. uint16_t r_ = a_ + b_;
  1476. a_ = (a_ >> ((8 * sizeof(r_)) - 1)) + INT16_MAX;
  1477. if (HEDLEY_STATIC_CAST(int16_t, ((a_ ^ b_) | ~(b_ ^ r_))) >= 0) {
  1478. r_ = a_;
  1479. }
  1480. return HEDLEY_STATIC_CAST(int16_t, r_);
  1481. #endif
  1482. }
  1483. static HEDLEY_INLINE int32_t simde_math_adds_i32(int32_t a, int32_t b)
  1484. {
  1485. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1486. return vqadds_s32(a, b);
  1487. #else
  1488. uint32_t a_ = HEDLEY_STATIC_CAST(uint32_t, a);
  1489. uint32_t b_ = HEDLEY_STATIC_CAST(uint32_t, b);
  1490. uint32_t r_ = a_ + b_;
  1491. a_ = (a_ >> ((8 * sizeof(r_)) - 1)) + INT32_MAX;
  1492. if (HEDLEY_STATIC_CAST(int32_t, ((a_ ^ b_) | ~(b_ ^ r_))) >= 0) {
  1493. r_ = a_;
  1494. }
  1495. return HEDLEY_STATIC_CAST(int32_t, r_);
  1496. #endif
  1497. }
  1498. static HEDLEY_INLINE int64_t simde_math_adds_i64(int64_t a, int64_t b)
  1499. {
  1500. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1501. return vqaddd_s64(a, b);
  1502. #else
  1503. uint64_t a_ = HEDLEY_STATIC_CAST(uint64_t, a);
  1504. uint64_t b_ = HEDLEY_STATIC_CAST(uint64_t, b);
  1505. uint64_t r_ = a_ + b_;
  1506. a_ = (a_ >> ((8 * sizeof(r_)) - 1)) + INT64_MAX;
  1507. if (HEDLEY_STATIC_CAST(int64_t, ((a_ ^ b_) | ~(b_ ^ r_))) >= 0) {
  1508. r_ = a_;
  1509. }
  1510. return HEDLEY_STATIC_CAST(int64_t, r_);
  1511. #endif
  1512. }
  1513. static HEDLEY_INLINE uint8_t simde_math_adds_u8(uint8_t a, uint8_t b)
  1514. {
  1515. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1516. return vqaddb_u8(a, b);
  1517. #else
  1518. uint8_t r = a + b;
  1519. r |= -(r < a);
  1520. return r;
  1521. #endif
  1522. }
  1523. static HEDLEY_INLINE uint16_t simde_math_adds_u16(uint16_t a, uint16_t b)
  1524. {
  1525. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1526. return vqaddh_u16(a, b);
  1527. #else
  1528. uint16_t r = a + b;
  1529. r |= -(r < a);
  1530. return r;
  1531. #endif
  1532. }
  1533. static HEDLEY_INLINE uint32_t simde_math_adds_u32(uint32_t a, uint32_t b)
  1534. {
  1535. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1536. return vqadds_u32(a, b);
  1537. #else
  1538. uint32_t r = a + b;
  1539. r |= -(r < a);
  1540. return r;
  1541. #endif
  1542. }
  1543. static HEDLEY_INLINE uint64_t simde_math_adds_u64(uint64_t a, uint64_t b)
  1544. {
  1545. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1546. return vqaddd_u64(a, b);
  1547. #else
  1548. uint64_t r = a + b;
  1549. r |= -(r < a);
  1550. return r;
  1551. #endif
  1552. }
  1553. static HEDLEY_INLINE int8_t simde_math_subs_i8(int8_t a, int8_t b)
  1554. {
  1555. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1556. return vqsubb_s8(a, b);
  1557. #else
  1558. uint8_t a_ = HEDLEY_STATIC_CAST(uint8_t, a);
  1559. uint8_t b_ = HEDLEY_STATIC_CAST(uint8_t, b);
  1560. uint8_t r_ = a_ - b_;
  1561. a_ = (a_ >> 7) + INT8_MAX;
  1562. if (HEDLEY_STATIC_CAST(int8_t, (a_ ^ b_) & (a_ ^ r_)) < 0) {
  1563. r_ = a_;
  1564. }
  1565. return HEDLEY_STATIC_CAST(int8_t, r_);
  1566. #endif
  1567. }
  1568. static HEDLEY_INLINE int16_t simde_math_subs_i16(int16_t a, int16_t b)
  1569. {
  1570. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1571. return vqsubh_s16(a, b);
  1572. #else
  1573. uint16_t a_ = HEDLEY_STATIC_CAST(uint16_t, a);
  1574. uint16_t b_ = HEDLEY_STATIC_CAST(uint16_t, b);
  1575. uint16_t r_ = a_ - b_;
  1576. a_ = (a_ >> 15) + INT16_MAX;
  1577. if (HEDLEY_STATIC_CAST(int16_t, (a_ ^ b_) & (a_ ^ r_)) < 0) {
  1578. r_ = a_;
  1579. }
  1580. return HEDLEY_STATIC_CAST(int16_t, r_);
  1581. #endif
  1582. }
  1583. static HEDLEY_INLINE int32_t simde_math_subs_i32(int32_t a, int32_t b)
  1584. {
  1585. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1586. return vqsubs_s32(a, b);
  1587. #else
  1588. uint32_t a_ = HEDLEY_STATIC_CAST(uint32_t, a);
  1589. uint32_t b_ = HEDLEY_STATIC_CAST(uint32_t, b);
  1590. uint32_t r_ = a_ - b_;
  1591. a_ = (a_ >> 31) + INT32_MAX;
  1592. if (HEDLEY_STATIC_CAST(int32_t, (a_ ^ b_) & (a_ ^ r_)) < 0) {
  1593. r_ = a_;
  1594. }
  1595. return HEDLEY_STATIC_CAST(int32_t, r_);
  1596. #endif
  1597. }
  1598. static HEDLEY_INLINE int64_t simde_math_subs_i64(int64_t a, int64_t b)
  1599. {
  1600. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1601. return vqsubd_s64(a, b);
  1602. #else
  1603. uint64_t a_ = HEDLEY_STATIC_CAST(uint64_t, a);
  1604. uint64_t b_ = HEDLEY_STATIC_CAST(uint64_t, b);
  1605. uint64_t r_ = a_ - b_;
  1606. a_ = (a_ >> 63) + INT64_MAX;
  1607. if (HEDLEY_STATIC_CAST(int64_t, (a_ ^ b_) & (a_ ^ r_)) < 0) {
  1608. r_ = a_;
  1609. }
  1610. return HEDLEY_STATIC_CAST(int64_t, r_);
  1611. #endif
  1612. }
  1613. static HEDLEY_INLINE uint8_t simde_math_subs_u8(uint8_t a, uint8_t b)
  1614. {
  1615. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1616. return vqsubb_u8(a, b);
  1617. #else
  1618. uint8_t res = a - b;
  1619. res &= -(res <= a);
  1620. return res;
  1621. #endif
  1622. }
  1623. static HEDLEY_INLINE uint16_t simde_math_subs_u16(uint16_t a, uint16_t b)
  1624. {
  1625. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1626. return vqsubh_u16(a, b);
  1627. #else
  1628. uint16_t res = a - b;
  1629. res &= -(res <= a);
  1630. return res;
  1631. #endif
  1632. }
  1633. static HEDLEY_INLINE uint32_t simde_math_subs_u32(uint32_t a, uint32_t b)
  1634. {
  1635. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1636. return vqsubs_u32(a, b);
  1637. #else
  1638. uint32_t res = a - b;
  1639. res &= -(res <= a);
  1640. return res;
  1641. #endif
  1642. }
  1643. static HEDLEY_INLINE uint64_t simde_math_subs_u64(uint64_t a, uint64_t b)
  1644. {
  1645. #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
  1646. return vqsubd_u64(a, b);
  1647. #else
  1648. uint64_t res = a - b;
  1649. res &= -(res <= a);
  1650. return res;
  1651. #endif
  1652. }
  1653. HEDLEY_DIAGNOSTIC_POP
  1654. #endif /* !defined(SIMDE_MATH_H) */