Debian Codegraph @

third-party/simde/simde-math.h

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