202 #if defined(SYS_MATHLIB_OPENLIBM)
203 # include "SYS_FastMath.h"
218 #if !defined(SESI_ALLOW_DRAND48)
224 #define srand48(X) static_assert(0, "Use SYSsrand48() instead")
225 #define drand48(X) 0; { static_assert(0, "Use SYSdrand48() instead"); }
232 template<
typename F >
236 return int(F(0) <
a) -
int(
a < F(0));
240 template<
typename F >
241 constexpr
inline bool
297 #if defined(__cplusplus)
303 #define h_min(a, b) (((a) > (b)) ? (b) : (a))
304 #define h_argmin(a, b) (((a) > (b)) ? 1 : 0)
305 #define h_max(a, b) (((a) < (b)) ? (b) : (a))
306 #define h_argmax(a, b) (((a) < (b)) ? 1 : 0)
308 #define h_abs(a) (((a) > 0) ? (a) : -(a))
309 #define h_sgn(a) (((a) > 0) ? 1 : (((a) < 0) ? -1 : 0))
311 #define SYS_DEF_MINMAX_FUNC1(NAME, RETTYPE, ATYPE) \
312 static inline RETTYPE SYS##NAME(ATYPE a) { return h_##NAME(a); }
314 #define SYS_DEF_MINMAX_FUNC2(NAME, RETTYPE, ATYPE, BTYPE) \
315 static inline RETTYPE SYS##NAME(ATYPE a, BTYPE b) { return h_##NAME(a,b); }
317 #define SYS_MIN(RETTYPE, ATYPE, BTYPE) SYS_DEF_MINMAX_FUNC2(min, RETTYPE, ATYPE, BTYPE)
318 #define SYS_MAX(RETTYPE, ATYPE, BTYPE) SYS_DEF_MINMAX_FUNC2(max, RETTYPE, ATYPE, BTYPE)
319 #define SYS_ABS(RETTYPE, ATYPE) SYS_DEF_MINMAX_FUNC1(abs, RETTYPE, ATYPE)
320 #define SYS_SGN(RETTYPE, ATYPE) SYS_DEF_MINMAX_FUNC1(sgn, RETTYPE, ATYPE)
321 #define SYS_ARGMIN(RETTYPE, ATYPE, BTYPE) SYS_DEF_MINMAX_FUNC2(argmin, RETTYPE, ATYPE, BTYPE)
322 #define SYS_ARGMAX(RETTYPE, ATYPE, BTYPE) SYS_DEF_MINMAX_FUNC2(argmax, RETTYPE, ATYPE, BTYPE)
328 constexpr SYS_MAX (
int16, int16, int16 )
329 constexpr SYS_ABS ( int16, int16 )
330 constexpr SYS_SGN ( int16, int16 )
331 constexpr SYS_ARGMIN (
int, int16, int16 )
332 constexpr SYS_ARGMAX (
int, int16, int16 )
333 constexpr SYS_MIN (
int32, int32, int32 )
334 constexpr SYS_MAX ( int32, int32, int32 )
335 constexpr SYS_ABS ( int32, int32 )
336 constexpr SYS_SGN ( int32, int32 )
337 constexpr SYS_ARGMIN (
int, int32, int32 )
338 constexpr SYS_ARGMAX (
int, int32, int32 )
339 constexpr SYS_MIN (
int64, int64, int64 )
340 constexpr SYS_MAX ( int64, int64, int64 )
341 constexpr SYS_MIN ( int64, int32, int64 )
342 constexpr SYS_MAX ( int64, int32, int64 )
343 constexpr SYS_MIN ( int64, int64, int32 )
344 constexpr SYS_MAX ( int64, int64, int32 )
345 constexpr SYS_ABS ( int64, int64 )
346 constexpr SYS_SGN ( int64, int64 )
347 constexpr SYS_ARGMIN (
int, int64, int64 )
348 constexpr SYS_ARGMAX (
int, int64, int64 )
349 constexpr SYS_MIN (
uint16, uint16, uint16 )
350 constexpr SYS_MAX ( uint16, uint16, uint16 )
351 constexpr SYS_ARGMIN (
int, uint16, uint16 )
352 constexpr SYS_ARGMAX (
int, uint16, uint16 )
353 constexpr SYS_MIN (
uint32, uint32, uint32 )
354 constexpr SYS_MAX ( uint32, uint32, uint32 )
355 constexpr SYS_ARGMIN (
int, uint32, uint32 )
356 constexpr SYS_ARGMAX (
int, uint32, uint32 )
357 constexpr SYS_MIN (
uint64, uint64, uint64 )
358 constexpr SYS_MAX ( uint64, uint64, uint64 )
359 constexpr SYS_ARGMIN (
int, uint64, uint64 )
360 constexpr SYS_ARGMAX (
int, uint64, uint64 )
361 constexpr SYS_MIN (
fpreal32, fpreal32, fpreal32 )
362 constexpr SYS_MAX ( fpreal32, fpreal32, fpreal32 )
363 constexpr SYS_SGN ( fpreal32, fpreal32 )
364 constexpr SYS_ARGMIN (
int, fpreal32, fpreal32 )
365 constexpr SYS_ARGMAX (
int, fpreal32, fpreal32 )
366 constexpr SYS_MIN (
fpreal64, fpreal64, fpreal64 )
367 constexpr SYS_MAX ( fpreal64, fpreal64, fpreal64 )
368 constexpr SYS_SGN ( fpreal64, fpreal64 )
369 constexpr SYS_ARGMIN (
int, fpreal64, fpreal64 )
370 constexpr SYS_ARGMAX (
int, fpreal64, fpreal64 )
371 SYS_MIN (
fpreal16, fpreal16, fpreal16 )
372 SYS_MAX ( fpreal16, fpreal16, fpreal16 )
373 SYS_SGN ( fpreal16, fpreal16 )
374 SYS_ARGMIN (
int, fpreal16, fpreal16 )
375 SYS_ARGMAX (
int, fpreal16, fpreal16 )
379 constexpr SYS_MIN (
size_t,
size_t,
size_t )
380 constexpr SYS_MAX (
size_t,
size_t,
size_t )
381 constexpr SYS_ARGMIN (
int,
size_t,
size_t )
382 constexpr SYS_ARGMAX (
int,
size_t,
size_t )
383 constexpr SYS_MIN (
size_t,
long,
long )
384 constexpr SYS_MAX (
size_t,
long,
long )
385 constexpr SYS_ARGMIN (
int,
long,
long )
386 constexpr SYS_ARGMAX (
int,
long,
long )
396 #undef SYS_DEF_MINMAX_FUNC1
397 #undef SYS_DEF_MINMAX_FUNC2
404 #define h_clamp(val, min, max, tol) \
405 ((val <= min+tol) ? min : ((val >= max-tol) ? max : val))
407 static constexpr
inline int
409 {
return h_clamp(v, min, max, 0); }
411 static constexpr
inline uint
413 {
return h_clamp(v, min, max, 0); }
415 static constexpr
inline int64
419 static constexpr
inline uint64
420 SYSclamp(uint64 v, uint64 min, uint64 max)
423 static constexpr
inline fpreal32
424 SYSclamp(fpreal32 v, fpreal32 min, fpreal32 max, fpreal32 tol=(fpreal32)0)
425 {
return h_clamp(v, min, max, tol); }
427 static constexpr
inline fpreal64
428 SYSclamp(fpreal64 v, fpreal64 min, fpreal64 max, fpreal64 tol=(fpreal64)0)
429 {
return h_clamp(v, min, max, tol); }
434 static constexpr
inline float SYSclamp01(
float v)
436 static constexpr
inline double SYSclamp01(
double v)
440 static constexpr
inline float SYSclamp01_excl1(
float v)
441 {
return SYSclamp(v, 0.
f, 1.
f - FLT_EPSILON/FLT_RADIX); }
442 static constexpr
inline double SYSclamp01_excl1(
double v)
443 {
return SYSclamp(v, 0.0, 1.0 - DBL_EPSILON/FLT_RADIX); }
447 template<
typename OUTTYPE,
typename INTYPE>
448 constexpr OUTTYPE SYSclampInt(INTYPE
value)
452 return OUTTYPE(value);
456 #if defined(SYS_MATHLIB_COREMATH)
457 #define SYS_MF(X) ::cr_##X
458 #elif defined(SYS_MATHLIB_SYSTEM)
459 #define SYS_MF(X) ::X
460 #elif defined(SYS_MATHLIB_OPENLIBM)
461 #define SYS_MF(X) SYS_FastMath::X
463 #error Unknown math library
466 #define SYS_MF_STDLIB(X) ::X
468 #if defined(SYS_MATHLIB_COREMATH)
471 #define DECLARE_COREMATH_UNARY_FUNCTIONS(FUNC) \
472 float cr_##FUNC##f(float x); \
473 double cr_##FUNC(double x);
474 #define DECLARE_COREMATH_BINARY_FUNCTIONS(FUNC, ARG_A, ARG_B) \
475 float cr_##FUNC##f(float ARG_A, float ARG_B); \
476 double cr_##FUNC(double ARG_A, double ARG_B);
478 DECLARE_COREMATH_UNARY_FUNCTIONS(acos);
479 DECLARE_COREMATH_UNARY_FUNCTIONS(acosh);
480 DECLARE_COREMATH_UNARY_FUNCTIONS(acospi);
481 DECLARE_COREMATH_UNARY_FUNCTIONS(asin);
482 DECLARE_COREMATH_UNARY_FUNCTIONS(asinh);
483 DECLARE_COREMATH_UNARY_FUNCTIONS(asinpi);
484 DECLARE_COREMATH_UNARY_FUNCTIONS(atan);
485 DECLARE_COREMATH_UNARY_FUNCTIONS(atanh);
486 DECLARE_COREMATH_UNARY_FUNCTIONS(atanpi);
487 DECLARE_COREMATH_UNARY_FUNCTIONS(
cbrt);
488 DECLARE_COREMATH_UNARY_FUNCTIONS(cos);
489 DECLARE_COREMATH_UNARY_FUNCTIONS(cosh);
490 DECLARE_COREMATH_UNARY_FUNCTIONS(cospi);
491 DECLARE_COREMATH_UNARY_FUNCTIONS(erf);
492 DECLARE_COREMATH_UNARY_FUNCTIONS(erfc);
493 DECLARE_COREMATH_UNARY_FUNCTIONS(exp);
494 DECLARE_COREMATH_UNARY_FUNCTIONS(exp10);
495 DECLARE_COREMATH_UNARY_FUNCTIONS(exp10m1);
496 DECLARE_COREMATH_UNARY_FUNCTIONS(exp2);
497 DECLARE_COREMATH_UNARY_FUNCTIONS(exp2m1);
498 DECLARE_COREMATH_UNARY_FUNCTIONS(expm1);
499 DECLARE_COREMATH_UNARY_FUNCTIONS(
log);
500 DECLARE_COREMATH_UNARY_FUNCTIONS(log10);
501 DECLARE_COREMATH_UNARY_FUNCTIONS(log10p1);
502 DECLARE_COREMATH_UNARY_FUNCTIONS(log1p);
503 DECLARE_COREMATH_UNARY_FUNCTIONS(log2);
504 DECLARE_COREMATH_UNARY_FUNCTIONS(log2p1);
505 DECLARE_COREMATH_UNARY_FUNCTIONS(
rsqrt);
506 DECLARE_COREMATH_UNARY_FUNCTIONS(sin);
507 DECLARE_COREMATH_UNARY_FUNCTIONS(sinh);
508 DECLARE_COREMATH_UNARY_FUNCTIONS(sinpi);
509 DECLARE_COREMATH_UNARY_FUNCTIONS(tan);
510 DECLARE_COREMATH_UNARY_FUNCTIONS(tanh);
511 DECLARE_COREMATH_UNARY_FUNCTIONS(tanpi);
512 DECLARE_COREMATH_UNARY_FUNCTIONS(tgamma);
514 DECLARE_COREMATH_BINARY_FUNCTIONS(atan2,
y,
x);
515 DECLARE_COREMATH_BINARY_FUNCTIONS(atan2pi,
y,
x);
516 DECLARE_COREMATH_BINARY_FUNCTIONS(hypot,
x,
y);
517 DECLARE_COREMATH_BINARY_FUNCTIONS(
pow,
x,
y);
519 extern void cr_sincosf(
float x,
float *
s,
float *
c);
520 extern void cr_sincos(
double x,
double *
s,
double *
c);
522 #undef DECLARE_COREMATH_UNARY_FUNCTIONS
523 #undef DECLARE_COREMATH_BINARY_FUNCTIONS
526 #if defined(SYS_MATH_BUILDING_SHARED_LIB)
527 #define SYS_MATH_API SYS_API
528 #elif defined(SESI_INTERNAL)
529 #define SYS_MATH_API static inline
534 #define SYS_MATH_API static inline
537 #define SYS_MATH_UNARY_IMPL(func) \
538 SYS_MATH_API fpreal64 SYS##func(fpreal64 x) \
539 { return SYS_MF(func)(x); } \
540 SYS_MATH_API fpreal32 SYS##func(fpreal32 x) \
541 { return SYS_MF(func##f)(x); } \
542 SYS_MATH_API fpreal64 SYS##func(int64 x) \
543 { return SYS_MF(func)((fpreal64)x); } \
544 SYS_MATH_API fpreal64 SYS##func(int32 x) \
545 { return SYS_MF(func)((fpreal64)x); }
546 #define SYS_MATH_BINARY_IMPL(func, VARA, VARB) \
547 SYS_MATH_API fpreal32 SYS##func(fpreal32 VARA, fpreal32 VARB) \
548 { return SYS_MF(func##f)(VARA, VARB); } \
549 SYS_MATH_API fpreal64 SYS##func(fpreal64 VARA, fpreal64 VARB) \
550 { return SYS_MF(func)(VARA, VARB); }
552 #define SYS_MATH_UNARY_DEFN(func) \
553 SYS_MATH_API fpreal64 SYS##func(fpreal64 x); \
554 SYS_MATH_API fpreal32 SYS##func(fpreal32 x); \
555 SYS_MATH_API fpreal64 SYS##func(int64 x); \
556 SYS_MATH_API fpreal64 SYS##func(int32 x);
557 #define SYS_MATH_BINARY_DEFN(func, VARA, VARB) \
558 SYS_MATH_API fpreal32 SYS##func(fpreal32 VARA, fpreal32 VARB); \
559 SYS_MATH_API fpreal64 SYS##func(fpreal64 VARA, fpreal64 VARB);
561 #if defined(SESI_INTERNAL) && !defined(SYS_MATH_BUILDING_SHARED_LIB)
562 #define SYS_MATH_UNARY(func) SYS_MATH_UNARY_IMPL(func)
563 #define SYS_MATH_BINARY(func, VARA, VARB) SYS_MATH_BINARY_IMPL(func, VARA, VARB)
565 #define SYS_MATH_UNARY(func) SYS_MATH_UNARY_DEFN(func)
566 #define SYS_MATH_BINARY(func, VARA, VARB) SYS_MATH_BINARY_DEFN(func, VARA, VARB)
569 #define SYS_MATH_UNARY_STDLIB(func) \
570 static inline fpreal32 SYS##func(fpreal32 x) \
571 { return SYS_MF_STDLIB(func##f)(x); } \
572 static inline fpreal64 SYS##func(fpreal64 x) \
573 { return SYS_MF_STDLIB(func)(x); } \
574 static inline fpreal64 SYS##func(int32 x) \
575 { return SYS_MF_STDLIB(func)((fpreal64)x); } \
576 static inline fpreal64 SYS##func(int64 x) \
577 { return SYS_MF_STDLIB(func)((fpreal64)x); }
578 #define SYS_MATH_BINARY_STDLIB(func, VARA, VARB) \
579 static inline fpreal32 SYS##func(fpreal32 VARA, fpreal32 VARB) \
580 { return SYS_MF_STDLIB(func##f)(VARA, VARB); } \
581 static inline fpreal64 SYS##func(fpreal64 VARA, fpreal64 VARB) \
582 { return SYS_MF_STDLIB(func)(VARA, VARB); }
584 #define SYS_MATH_UNARY_SUFFIX_WRAPPER_IMPL(FUNC, SUFFIX, WRAPPER) \
585 SYS_MATH_API fpreal32 SYS##FUNC##SUFFIX(fpreal32 x) \
586 { return WRAPPER(FUNC##f)(x); } \
587 SYS_MATH_API fpreal64 SYS##FUNC##SUFFIX(fpreal64 x) \
588 { return WRAPPER(FUNC) (x); }
589 #define SYS_MATH_BINARY_SUFFIX_WRAPPER_IMPL(FUNC, SUFFIX, WRAPPER, VARA, VARB) \
590 SYS_MATH_API fpreal32 SYS##FUNC##SUFFIX(fpreal32 VARA, fpreal32 VARB) \
591 { return WRAPPER(FUNC##f)(VARA, VARB); } \
592 SYS_MATH_API fpreal64 SYS##FUNC##SUFFIX(fpreal64 VARA, fpreal64 VARB) \
593 { return WRAPPER(FUNC) (VARA, VARB); }
595 #define SYS_MATH_UNARY_SUFFIX_WRAPPER_DEFN(FUNC, SUFFIX, WRAPPER) \
596 SYS_MATH_API fpreal32 SYS##FUNC##SUFFIX(fpreal32 x); \
597 SYS_MATH_API fpreal64 SYS##FUNC##SUFFIX(fpreal64 x);
598 #define SYS_MATH_BINARY_SUFFIX_WRAPPER_DEFN(FUNC, SUFFIX, WRAPPER, VARA, VARB) \
599 SYS_MATH_API fpreal32 SYS##FUNC##SUFFIX(fpreal32 VARA, fpreal32 VARB); \
600 SYS_MATH_API fpreal64 SYS##FUNC##SUFFIX(fpreal64 VARA, fpreal64 VARB);
602 #if defined(SESI_INTERNAL) && !defined(SYS_MATH_BUILDING_SHARED_LIB)
603 #define SYS_MATH_UNARY_SUFFIX_WRAPPER(FUNC, SUFFIX, WRAPPER) \
604 SYS_MATH_UNARY_SUFFIX_WRAPPER_IMPL(FUNC, SUFFIX, WRAPPER)
605 #define SYS_MATH_BINARY_SUFFIX_WRAPPER(FUNC, SUFFIX, WRAPPER, VARA, VARB) \
606 SYS_MATH_BINARY_SUFFIX_WRAPPER_IMPL(FUNC, SUFFIX, WRAPPER, VARA, VARB)
608 #define SYS_MATH_UNARY_SUFFIX_WRAPPER(FUNC, SUFFIX, WRAPPER) \
609 SYS_MATH_UNARY_SUFFIX_WRAPPER_DEFN(FUNC, SUFFIX, WRAPPER)
610 #define SYS_MATH_BINARY_SUFFIX_WRAPPER(FUNC, SUFFIX, WRAPPER, VARA, VARB) \
611 SYS_MATH_BINARY_SUFFIX_WRAPPER_DEFN(FUNC, SUFFIX, WRAPPER, VARA, VARB)
614 #define SYS_MATH_UNSAFE_UNARY(FUNC) \
615 SYS_MATH_UNARY_SUFFIX_WRAPPER(FUNC, Unsafe, SYS_MF)
616 #define SYS_MATH_SAFE_UNARY(FUNC) \
617 SYS_MATH_UNARY_SUFFIX_WRAPPER(FUNC, , SYS_MF)
618 #define SYS_MATH_UNSAFE_UNARY_STDLIB(FUNC) \
619 SYS_MATH_UNARY_SUFFIX_WRAPPER_IMPL(FUNC, Unsafe, SYS_MF_STDLIB)
620 #define SYS_MATH_SAFE_UNARY_STDLIB(FUNC) \
621 SYS_MATH_UNARY_SUFFIX_WRAPPER_IMPL(FUNC, , SYS_MF_STDLIB)
622 #define SYS_MATH_UNSAFE_BINARY(FUNC, VARA, VARB) \
623 SYS_MATH_BINARY_SUFFIX_WRAPPER(FUNC, Unsafe, SYS_MF, VARA, VARB)
624 #define SYS_MATH_SAFE_BINARY(FUNC, VARA, VARB) \
625 SYS_MATH_BINARY_SUFFIX_WRAPPER(FUNC, , SYS_MF, VARA, VARB);
627 #define SYS_MATH_UNARY_PI_IMPL(FUNC) \
628 SYS_MATH_API fpreal32 SYS##FUNC##pi(fpreal32 x) { return SYS_MF(FUNC##pi##f)(x); } \
629 SYS_MATH_API fpreal64 SYS##FUNC##pi(fpreal64 x) { return SYS_MF(FUNC##pi) (x); } \
630 SYS_MATH_API fpreal32 SYS##FUNC##pi(int32 x) { return SYS_MF(FUNC##pi) (x); } \
631 SYS_MATH_API fpreal64 SYS##FUNC##pi(int64 x) { return SYS_MF(FUNC##pi) (x); }
633 #define SYS_MATH_UNARY_PI_DEFN(FUNC) \
634 SYS_MATH_API fpreal32 SYS##FUNC##pi(fpreal32 x); \
635 SYS_MATH_API fpreal64 SYS##FUNC##pi(fpreal64 x); \
636 SYS_MATH_API fpreal32 SYS##FUNC##pi(int32 x); \
637 SYS_MATH_API fpreal64 SYS##FUNC##pi(int64 x);
639 #if defined(SESI_INTERNAL) && !defined(SYS_MATH_BUILDING_SHARED_LIB)
640 #if defined(SYS_MATHLIB_COREMATH)
641 #define SYS_MATH_UNARY_PI(FUNC) SYS_MATH_UNARY_PI_IMPL(FUNC)
643 #define SYS_MATH_UNARY_PI(FUNC) \
644 SYS_MATH_API fpreal32 SYS##FUNC##pi(fpreal32 x) { return SYS##FUNC(SYSfmod(x, 2.0f) * M_PI); } \
645 SYS_MATH_API fpreal64 SYS##FUNC##pi(fpreal64 x) { return SYS##FUNC(SYSfmod(x, 2.0 ) * M_PI); } \
646 SYS_MATH_API fpreal32 SYS##FUNC##pi(int32 x) { return SYS##FUNC((x % 2) * M_PI); } \
647 SYS_MATH_API fpreal64 SYS##FUNC##pi(int64 x) { return SYS##FUNC((x % 2) * M_PI); }
650 #define SYS_MATH_UNARY_PI(FUNC) SYS_MATH_UNARY_PI_DEFN(FUNC)
659 SYS_MATH_UNARY(asinh)
661 SYS_MATH_BINARY(
pow,
x,
y)
662 SYS_MATH_BINARY(atan2,
y, x)
663 SYS_MATH_BINARY(hypot, x, y)
665 SYS_MATH_UNARY_STDLIB(
sqrt)
666 SYS_MATH_UNARY_STDLIB(
trunc)
667 SYS_MATH_BINARY_STDLIB(fmod, x, y)
668 SYS_MATH_BINARY_STDLIB(copysign, x, y)
670 #if defined(SYS_MATHLIB_COREMATH)
671 SYS_MATH_UNSAFE_UNARY(
rsqrt);
673 static inline fpreal32 SYSrsqrtUnsafe(fpreal32 x) {
return 1.0f / SYS_MF(sqrtf)(
x); }
674 static inline fpreal64 SYSrsqrtUnsafe(fpreal64 x) {
return 1.0 / SYS_MF(
sqrt) (
x); }
677 static inline fpreal32 SYSrsqrt(fpreal32 x) {
return x > 0 ? SYSrsqrtUnsafe(x) : 0.0
f; }
678 static inline fpreal64 SYSrsqrt(fpreal64 x) {
return x > 0 ? SYSrsqrtUnsafe(x) : 0.0; }
680 SYS_MATH_UNSAFE_UNARY(asin);
681 static inline fpreal32 SYSasin(fpreal32 x) {
return SYSasinUnsafe(
SYSclamp(x, -1.0
f, 1.0
f)); }
682 static inline fpreal64 SYSasin(fpreal64 x) {
return SYSasinUnsafe(
SYSclamp(x, -1.0, 1.0)); }
684 SYS_MATH_UNSAFE_UNARY(acos);
685 static inline fpreal32 SYSacos(fpreal32 x) {
return SYSacosUnsafe(
SYSclamp(x, -1.0
f, 1.0
f)); }
686 static inline fpreal64 SYSacos(fpreal64 x) {
return SYSacosUnsafe(
SYSclamp(x, -1.0, 1.0)); }
689 SYS_MATH_SAFE_UNARY(atan);
690 #if defined(SESI_INTERNAL) && !defined(SYS_MATH_BUILDING_SHARED_LIB)
691 SYS_MATH_API fpreal32 SYSatan(fpreal32 y, fpreal32 x) {
return SYS_MF(atan2f)(
y,
x); }
692 SYS_MATH_API fpreal64 SYSatan(fpreal64 y, fpreal64 x) {
return SYS_MF(atan2) (
y,
x); }
694 SYS_MATH_API fpreal32 SYSatan(fpreal32 y, fpreal32 x);
695 SYS_MATH_API fpreal64 SYSatan(fpreal64 y, fpreal64 x);
698 SYS_MATH_UNSAFE_UNARY(acosh);
699 static inline fpreal32 SYSacosh(fpreal32 x) {
return x < 1.0f ? 0.0f : SYSacoshUnsafe(x); }
700 static inline fpreal64 SYSacosh(fpreal64 x) {
return x < 1.0f ? 0.0 : SYSacoshUnsafe(x); }
702 SYS_MATH_UNSAFE_UNARY(atanh);
703 static inline fpreal32 SYSatanh(fpreal32 x) {
return SYSatanhUnsafe(
SYSclamp(x, -1.0
f + FLT_EPSILON/FLT_RADIX, 1.0
f - FLT_EPSILON/FLT_RADIX)); }
704 static inline fpreal64 SYSatanh(fpreal64 x) {
return SYSatanhUnsafe(
SYSclamp(x, -1.0 + DBL_EPSILON/FLT_RADIX, 1.0 - DBL_EPSILON/FLT_RADIX)); }
706 #if defined(SYS_MATHLIB_COREMATH)
707 SYS_MATH_SAFE_BINARY(atan2pi, y, x);
708 SYS_MATH_UNSAFE_UNARY(asinpi);
709 SYS_MATH_UNSAFE_UNARY(acospi);
710 SYS_MATH_SAFE_UNARY(atanpi);
712 static inline fpreal32 SYSatan2pi(fpreal32 y, fpreal32 x) {
return SYSatan(y, x) / (
fpreal32)
M_PI; }
713 static inline fpreal64 SYSatan2pi(fpreal64 y, fpreal64 x) {
return SYSatan(y, x) /
M_PI; }
715 static inline fpreal32 SYSasinpiUnsafe(fpreal32 x) {
return SYSasinUnsafe(x) / (
fpreal32)
M_PI; }
716 static inline fpreal64 SYSasinpiUnsafe(fpreal64 x) {
return SYSasinUnsafe(x) /
M_PI; }
718 static inline fpreal32 SYSacospiUnsafe(fpreal32 x) {
return SYSacosUnsafe(x) / (
fpreal32)
M_PI; }
719 static inline fpreal64 SYSacospiUnsafe(fpreal64 x) {
return SYSacosUnsafe(x) /
M_PI; }
721 static inline fpreal32 SYSatanpi(fpreal32 x) {
return SYSatan(x) / (
fpreal32)
M_PI; }
722 static inline fpreal64 SYSatanpi(fpreal64 x) {
return SYSatan(x) /
M_PI; }
725 static inline fpreal32 SYSasinpi(fpreal32 x) {
return SYSasinpiUnsafe(
SYSclamp(x, -1.0
f, 1.0
f)); }
726 static inline fpreal64 SYSasinpi(fpreal64 x) {
return SYSasinpiUnsafe(
SYSclamp(x, -1.0, 1.0 )); }
728 static inline fpreal32 SYSacospi(fpreal32 x) {
return SYSacospiUnsafe(
SYSclamp(x, -1.0
f, 1.0
f)); }
729 static inline fpreal64 SYSacospi(fpreal64 x) {
return SYSacospiUnsafe(
SYSclamp(x, -1.0, 1.0 )); }
731 SYS_MATH_UNARY_PI(sin);
732 SYS_MATH_UNARY_PI(cos);
733 SYS_MATH_UNARY_PI(tan);
735 SYS_MATH_UNSAFE_UNARY(
log);
736 static inline fpreal32 SYSlog(fpreal32 x) {
return x > 0.0f ? SYSlogUnsafe(x) : 0.0
f; }
737 static inline fpreal64 SYSlog(fpreal64 x) {
return x > 0.0 ? SYSlogUnsafe(x) : 0.0; }
739 #if defined(SYS_MATHLIB_COREMATH)
740 SYS_MATH_UNSAFE_UNARY(log2);
742 SYS_MATH_UNSAFE_UNARY_STDLIB(log2);
744 static inline fpreal32 SYSlog2(fpreal32 x) {
return x > 0.0f ? SYSlog2Unsafe(x) : 0.0
f; }
745 static inline fpreal64 SYSlog2(fpreal64 x) {
return x > 0.0 ? SYSlog2Unsafe(x) : 0.0; }
747 SYS_MATH_UNSAFE_UNARY(log10);
748 static inline fpreal32 SYSlog10(fpreal32 x) {
return x > 0.0f ? SYSlog10Unsafe(x) : 0.0
f; }
749 static inline fpreal64 SYSlog10(fpreal64 x) {
return x > 0.0 ? SYSlog10Unsafe(x) : 0.0; }
751 SYS_MATH_UNSAFE_UNARY(log1p);
752 static inline fpreal32 SYSlog1p(fpreal32 x) {
return x > -1.0f ? SYSlog1pUnsafe(x) : 0.0
f; }
753 static inline fpreal64 SYSlog1p(fpreal64 x) {
return x > -1.0 ? SYSlog1pUnsafe(x) : 0.0; }
755 #if defined(SYS_MATHLIB_COREMATH)
756 SYS_MATH_SAFE_UNARY(exp2)
757 SYS_MATH_SAFE_UNARY(erf)
758 SYS_MATH_SAFE_UNARY(erfc)
759 SYS_MATH_SAFE_UNARY(tgamma)
762 SYS_MATH_SAFE_UNARY_STDLIB(exp2)
763 SYS_MATH_SAFE_UNARY_STDLIB(erf)
764 SYS_MATH_SAFE_UNARY_STDLIB(erfc)
765 SYS_MATH_SAFE_UNARY_STDLIB(tgamma)
768 SYS_MATH_UNARY(expm1)
770 #if defined(SYS_MATHLIB_COREMATH)
771 SYS_MATH_SAFE_UNARY(exp2m1);
772 SYS_MATH_SAFE_UNARY(exp10);
773 SYS_MATH_SAFE_UNARY(exp10m1);
775 SYS_MATH_UNSAFE_UNARY(log2p1);
776 SYS_MATH_UNSAFE_UNARY(log10p1);
778 static inline fpreal32 SYSexp2m1(fpreal32 x) {
return SYSpow(2.0
f, x) - 1.0f; }
779 static inline fpreal64 SYSexp2m1(fpreal64 x) {
return SYSpow(2.0, x) - 1.0; }
781 static inline fpreal32 SYSexp10(fpreal32 x) {
return SYSpow(10.0
f, x); }
782 static inline fpreal64 SYSexp10(fpreal64 x) {
return SYSpow(10.0, x); }
784 static inline fpreal32 SYSexp10m1(fpreal32 x) {
return SYSpow(10.0
f, x) - 1.0f; }
785 static inline fpreal64 SYSexp10m1(fpreal64 x) {
return SYSpow(10.0, x) - 1.0; }
787 static inline fpreal32 SYSlog2p1Unsafe(fpreal32 x) {
return SYSlog2Unsafe(x + 1.0
f); }
788 static inline fpreal64 SYSlog2p1Unsafe(fpreal64 x) {
return SYSlog2Unsafe(x + 1.0 ); }
790 static inline fpreal32 SYSlog10p1Unsafe(fpreal32 x) {
return SYSlog10Unsafe(x + 1.0
f); }
791 static inline fpreal64 SYSlog10p1Unsafe(fpreal64 x) {
return SYSlog10Unsafe(x + 1.0 ); }
794 static inline fpreal32 SYSlog2p1(fpreal32 x) {
return x > -1.0f ? SYSlog2p1Unsafe(x) : 0.0
f; }
795 static inline fpreal64 SYSlog2p1(fpreal64 x) {
return x > -1.0 ? SYSlog2p1Unsafe(x) : 0.0; }
797 static inline fpreal32 SYSlog10p1(fpreal32 x) {
return x > -1.0f ? SYSlog10p1Unsafe(x) : 0.0
f; }
798 static inline fpreal64 SYSlog10p1(fpreal64 x) {
return x > -1.0 ? SYSlog10p1Unsafe(x) : 0.0; }
800 static constexpr
inline fpreal32 SYSsafediv(fpreal32 x, fpreal32 y)
802 static constexpr
inline fpreal64 SYSsafediv(fpreal64 x, fpreal64 y)
804 static constexpr
inline fpreal32 SYSsafesqrt(fpreal32 x)
805 {
return x > 0 ? SYSsqrt(x) : 0; }
806 static constexpr
inline fpreal64 SYSsafesqrt(fpreal64 x)
807 {
return x > 0 ? SYSsqrt(x) : 0; }
808 static constexpr
inline fpreal32 SYSsafefmod(fpreal32 x, fpreal32 y)
809 {
return y != 0 ? SYSfmod(x, y) : 0; }
810 static constexpr
inline fpreal64 SYSsafefmod(fpreal64 x, fpreal64 y)
811 {
return y != 0 ? SYSfmod(x, y) : 0; }
813 #if defined(SYS_MATHLIB_COREMATH)
814 # if defined(SESI_INTERNAL) && !defined(SYS_MATH_BUILDING_SHARED_LIB)
815 SYS_MATH_API
void SYSsincos(fpreal32 x, fpreal32 *
s, fpreal32 *
c)
816 { SYS_MF(sincosf)(
x,
s,
c); }
817 SYS_MATH_API
void SYSsincos(fpreal64 x, fpreal64 *
s, fpreal64 *
c)
819 SYS_MATH_API
void SYSsincos(fpreal32 x, fpreal16 *
s, fpreal16 *
c)
822 SYS_MF(sincosf)(
x,&s32,&c32);
827 SYS_MATH_API
void SYSsincos(fpreal32 x, fpreal32 *
s, fpreal32 *
c);
828 SYS_MATH_API
void SYSsincos(fpreal64 x, fpreal64 *
s, fpreal64 *
c);
829 SYS_MATH_API
void SYSsincos(fpreal32 x, fpreal16 *
s, fpreal16 *
c);
834 static inline void SYSsincos(fpreal32 x, fpreal32 *
s, fpreal32 *
c)
835 { *s = SYS_MF(sinf)(
x); *c = SYS_MF(cosf)(
x); }
836 static inline void SYSsincos(fpreal64 x, fpreal64 *
s, fpreal64 *
c)
837 { *s = SYS_MF(sin)(
x); *c = SYS_MF(cos)(
x); }
838 static inline void SYSsincos(fpreal32 x, fpreal16 *
s, fpreal16 *
c)
846 static inline void SYSsincos(fpreal32 x, fpreal32 *
s, fpreal32 *
c)
847 { __sincosf(x,s,c); }
848 static inline void SYSsincos(fpreal64 x, fpreal64 *
s, fpreal64 *
c)
850 static inline void SYSsincos(fpreal32 x, fpreal16 *
s, fpreal16 *
c)
853 __sincosf(x,&s32,&c32);
858 static inline void SYSsincos(fpreal32 x, fpreal32 *
s, fpreal32 *
c)
859 { SYS_MF(sincosf)(
x,
s,
c); }
860 static inline void SYSsincos(fpreal64 x, fpreal64 *
s, fpreal64 *
c)
862 static inline void SYSsincos(fpreal32 x, fpreal16 *
s, fpreal16 *
c)
865 SYS_MF(sincosf)(
x,&s32,&c32);
873 static inline int32 SYSlog2Int(uint32 x)
878 uint32
r = (x > 0xFFFFu) << 4; x >>=
r;
879 uint32 shift = (x > 0xFFu ) << 3; x >>= shift; r |= shift;
880 shift = (x > 0xFu ) << 2; x >>= shift; r |= shift;
881 shift = (x > 0x3u ) << 1;
882 return r | shift | (x >> (shift+1));
885 #undef SYS_MATH_BINARY
886 #undef SYS_MATH_BINARY_DEFN
887 #undef SYS_MATH_BINARY_IMPL
888 #undef SYS_MATH_BINARY_STDLIB
889 #undef SYS_MATH_BINARY_SUFFIX_WRAPPER
890 #undef SYS_MATH_BINARY_SUFFIX_WRAPPER_DEFN
891 #undef SYS_MATH_BINARY_SUFFIX_WRAPPER_IMPL
892 #undef SYS_MATH_SAFE_BINARY
893 #undef SYS_MATH_SAFE_UNARY
894 #undef SYS_MATH_SAFE_UNARY_STDLIB
895 #undef SYS_MATH_UNARY
896 #undef SYS_MATH_UNARY_DEFN
897 #undef SYS_MATH_UNARY_IMPL
898 #undef SYS_MATH_UNARY_PI
899 #undef SYS_MATH_UNARY_PI_DEFN
900 #undef SYS_MATH_UNARY_PI_IMPL
901 #undef SYS_MATH_UNARY_STDLIB
902 #undef SYS_MATH_UNARY_SUFFIX_WRAPPER
903 #undef SYS_MATH_UNARY_SUFFIX_WRAPPER_DEFN
904 #undef SYS_MATH_UNARY_SUFFIX_WRAPPER_IMPL
905 #undef SYS_MATH_UNSAFE_BINARY
906 #undef SYS_MATH_UNSAFE_UNARY
907 #undef SYS_MATH_UNSAFE_UNARY_STDLIB
909 static inline fpreal32
SYSabs (fpreal32 x) {
return SYS_MF_STDLIB(fabsf)(
x); }
910 static inline fpreal64
SYSabs (fpreal64 x) {
return SYS_MF_STDLIB(fabs) (
x); }
911 static inline fpreal32 SYSfabs(fpreal32 x) {
return SYS_MF_STDLIB(fabsf)(
x); }
912 static inline fpreal64 SYSfabs(fpreal64 x) {
return SYS_MF_STDLIB(fabs) (
x); }
916 static inline fpreal32 SYSsafepow(fpreal32 x, fpreal32 y) {
return x > 0 ? SYSpow(x, y) : x < 0 ? SYSpow(x,
SYSrint(y)) : y == 0 ? 1 : 0; }
917 static inline fpreal64 SYSsafepow(fpreal64 x, fpreal64 y) {
return x > 0 ? SYSpow(x, y) : x < 0 ? SYSpow(x,
SYSrint(y)) : y == 0 ? 1 : 0; }
919 static constexpr
inline fpreal32
SYSrecip(fpreal32 x) {
return 1.0f /
x; }
920 static constexpr
inline fpreal64
SYSrecip(fpreal64 x) {
return 1.0 /
x; }
921 static constexpr
inline fpreal32
SYSrecip(int32 x) {
return 1.0f / (
fpreal32)x; }
922 static constexpr
inline fpreal64
SYSrecip(int64 x) {
return 1.0 / (
fpreal64)x; }
924 static constexpr
inline fpreal32 SYSsaferecip(fpreal32 x) {
return SYSsafediv(1.0
f, x); }
925 static constexpr
inline fpreal64 SYSsaferecip(fpreal64 x) {
return SYSsafediv(1.0, x); }
926 static constexpr
inline fpreal32 SYSsaferecip(int32 x) {
return SYSsafediv(1.0
f, (fpreal32)x); }
927 static constexpr
inline fpreal64 SYSsaferecip(int64 x) {
return SYSsafediv(1.0, (fpreal64)x); }
929 static constexpr
inline fpreal32 SYSdegToRad(fpreal32 x) {
return x*(
fpreal32)(
M_PI/180.0); }
930 static constexpr
inline fpreal64 SYSdegToRad(fpreal64 x) {
return x*(
fpreal64)(
M_PI/180.0); }
931 static constexpr
inline fpreal64 SYSdegToRad(int32 x) {
return x*(
fpreal64)(
M_PI/180.0); }
932 static constexpr
inline fpreal64 SYSdegToRad(int64 x) {
return x*(
fpreal64)(
M_PI/180.0); }
934 static constexpr
inline fpreal32 SYSradToDeg(fpreal32 x) {
return x*(
fpreal32)(180.0/
M_PI); }
935 static constexpr
inline fpreal64 SYSradToDeg(fpreal64 x) {
return x*(
fpreal64)(180.0/
M_PI); }
936 static constexpr
inline fpreal64 SYSradToDeg(int32 x) {
return x*(
fpreal64)(180.0/
M_PI); }
937 static constexpr
inline fpreal64 SYSradToDeg(int64 x) {
return x*(
fpreal64)(180.0/
M_PI); }
939 static constexpr
inline fpreal32 SYSpow2(fpreal32 x) {
return x*
x; }
940 static constexpr
inline fpreal64 SYSpow2(fpreal64 x) {
return x*
x; }
941 static constexpr
inline fpreal32 SYSpow3(fpreal32 x) {
return x*x*
x; }
942 static constexpr
inline fpreal64 SYSpow3(fpreal64 x) {
return x*x*
x; }
943 static constexpr
inline fpreal32 SYSpow4(fpreal32 x) {
return SYSpow2(SYSpow2(x)); }
944 static constexpr
inline fpreal64 SYSpow4(fpreal64 x) {
return SYSpow2(SYSpow2(x)); }
945 static constexpr
inline fpreal32 SYSpow5(fpreal32 x) {
return SYSpow4(x) *
x; }
946 static constexpr
inline fpreal64 SYSpow5(fpreal64 x) {
return SYSpow4(x) *
x; }
948 #define h_compare(func, code) \
949 static inline bool func(fpreal32 a, fpreal32 b, \
950 fpreal32 tol=SYS_FTOLERANCE) \
952 static inline bool func(fpreal64 a, fpreal64 b, \
953 fpreal64 tol=SYS_FTOLERANCE_D) \
955 #define h_compare_ce(func, code) \
956 static constexpr inline bool func(fpreal32 a, fpreal32 b, \
957 fpreal32 tol=SYS_FTOLERANCE) \
959 static constexpr inline bool func(fpreal64 a, fpreal64 b, \
960 fpreal64 tol=SYS_FTOLERANCE_D) \
963 static constexpr
inline bool
965 {
return a >= -tol && a <= tol; }
967 static constexpr
inline bool
969 {
return a >= -tol && a <= tol; }
971 static constexpr
inline bool
973 {
return a >= -tol && a <= tol; }
975 static constexpr
inline bool
977 {
return a >= -tol && a <= tol; }
980 h_compare_ce(SYSisGreater, (
a-
b) > tol)
981 h_compare_ce(SYSisGreaterOrEqual, (
a-
b) >= -tol)
982 h_compare_ce(SYSisLess, (
a-
b) < -tol)
983 h_compare_ce(SYSisLessOrEqual, (
a-
b) <= tol)
987 constexpr
inline bool SYSisEqual(int32
a, int32
b) {
return a ==
b; }
988 constexpr
inline bool SYSisEqual(int64
a, int64
b) {
return a ==
b; }
989 constexpr
inline bool SYSisGreater(int32
a, int32
b) {
return a >
b; }
990 constexpr
inline bool SYSisGreater(int64
a, int64
b) {
return a >
b; }
991 constexpr
inline bool SYSisGreaterOrEqual(int32
a, int32
b) {
return a >=
b; }
992 constexpr
inline bool SYSisGreaterOrEqual(int64
a, int64
b) {
return a >=
b; }
993 constexpr
inline bool SYSisLess(int32
a, int32
b) {
return a <
b; }
994 constexpr
inline bool SYSisLess(int64
a, int64
b) {
return a <
b; }
995 constexpr
inline bool SYSisLessOrEqual(int32
a, int32
b) {
return a <=
b; }
996 constexpr
inline bool SYSisLessOrEqual(int64
a, int64
b) {
return a <=
b; }
999 SYSalmostEqual(fpreal32
a, fpreal32
b,
int ulps=50, fpreal32 tol = 1e-6)
1002 if (SYSfabs(a - b) <= tol)
1015 if ((a < 0) != (b < 0))
1033 SYSalmostEqual(fpreal64 a, fpreal64 b, int64 ulps, fpreal64 tol = 1e-15)
1036 if (SYSfabs(a - b) <= tol)
1049 if ((a < 0) != (b < 0))
1065 SYSalmostEqual(fpreal64 a, fpreal64 b, int32 ulps=50, fpreal64 tol = 1e-15)
1067 return SYSalmostEqual(a, b, (int64)ulps, tol);
1075 #define h_max3(type) \
1076 static constexpr inline type \
1077 SYSmax(type v0, type v1, type v2) { \
1078 return SYSmax(v2, SYSmax(v0, v1)); \
1080 #define h_max4(type) \
1081 static constexpr inline type \
1082 SYSmax(type v0, type v1, type v2, type v3) { \
1083 return SYSmax(SYSmax(v0, v1), SYSmax(v2, v3)); \
1085 #define h_argmax3(type) \
1086 static constexpr inline int \
1087 SYSargmax(type v0, type v1, type v2) { \
1088 return v2 > SYSmax(v0, v1) ? 2 : SYSargmax(v0, v1); \
1090 #define h_argmax4(type) \
1091 static constexpr inline int \
1092 SYSargmax(type v0, type v1, type v2, type v3) { \
1093 return SYSmax(v0, v1) < SYSmax(v2, v3) ? \
1094 (SYSargmax(v2, v3) + 2) : SYSargmax(v0, v1); \
1096 #define h_min3(type) \
1097 static constexpr inline type \
1098 SYSmin(type v0, type v1, type v2) { \
1099 return SYSmin(v2, SYSmin(v0, v1)); \
1101 #define h_min4(type) \
1102 static constexpr inline type \
1103 SYSmin(type v0, type v1, type v2, type v3) { \
1104 return SYSmin(SYSmin(v0, v1), SYSmin(v2, v3)); \
1106 #define h_argmin3(type) \
1107 static constexpr inline int \
1108 SYSargmin(type v0, type v1, type v2) { \
1109 return v2 < SYSmin(v0, v1) ? 2 : SYSargmin(v0, v1); \
1111 #define h_argmin4(type) \
1112 static constexpr inline int \
1113 SYSargmin(type v0, type v1, type v2, type v3) { \
1114 return SYSmin(v0, v1) > SYSmin(v2, v3) ? \
1115 (SYSargmin(v2, v3) + 2) : SYSargmin(v0, v1); \
1119 #define h_max(type) h_min3(type) h_min4(type) h_max3(type) h_max4(type) \
1120 h_argmin3(type) h_argmin4(type) \
1121 h_argmax3(type) h_argmax4(type)
1123 #define h_avg3(type) \
1124 static constexpr inline type \
1125 SYSavg(type v0, type v1, type v2) { \
1126 return (v0+v1+v2) * ((type)(1.0/3.0)); \
1128 #define h_avg4(type) \
1129 static constexpr inline type \
1130 SYSavg(type v0, type v1, type v2, type v3) { \
1131 return (v0+v1+v2+v3) * ((type)0.25); \
1134 #define h_avg(type) h_avg3(type) h_avg4(type)
1144 h_max(fpreal32) h_avg(fpreal32)
1145 h_max(fpreal64) h_avg(fpreal64)
1147 static constexpr inline int32
1148 SYSavg(int32 a, int32 b, int32
c)
1149 {
return (a + b + c + 1) / 3; }
1150 static constexpr
inline int32
1151 SYSavg(int32 a, int32 b, int32 c, int32 d)
1152 {
return (a + b + c + d + 2) / 4; }
1154 static constexpr
inline int64
1155 SYSavg(int64 a, int64 b, int64 c)
1156 {
return (a + b + c + 1) / 3; }
1157 static constexpr
inline int64
1158 SYSavg(int64 a, int64 b, int64 c, int64 d)
1159 {
return (a + b + c + d + 2) / 4; }
1181 static constexpr
inline fpreal32
1184 return v1 + (v2 -
v1)*t;
1187 static constexpr
inline fpreal64
1190 return v1 + (v2 -
v1)*t;
1203 static constexpr
inline fpreal32
1204 SYSbilerp(fpreal32 u0v0, fpreal32 u1v0, fpreal32 u0v1, fpreal32 u1v1,
1205 fpreal32 u, fpreal32 v)
1210 static constexpr
inline fpreal64
1211 SYSbilerp(fpreal64 u0v0, fpreal64 u1v0, fpreal64 u0v1, fpreal64 u1v1,
1212 fpreal64 u, fpreal64 v)
1228 static constexpr
inline fpreal32
1231 return v0*(1-u-
v) + v1*u + v2*v;
1234 static constexpr
inline fpreal64
1237 return v0*(1-u-
v) + v1*u + v2*v;
1246 static constexpr
inline fpreal32
1247 SYSwrapmod(fpreal32 a, fpreal32 b)
1249 fpreal32
r = SYSsafefmod(a, b);
1250 return ((a < 0) ^ (b < 0) && r != 0.0
f) ? r+b :
r;
1253 static constexpr
inline fpreal64
1254 SYSwrapmod(fpreal64 a, fpreal64 b)
1256 fpreal64 r = SYSsafefmod(a, b);
1257 return ((a < 0) ^ (b < 0) && r != 0.0) ? r+b :
r;
1260 static constexpr
inline int32
1261 SYSwrapmod(int32 a, int32 b)
1266 return ((a < 0) ^ (b < 0) && r) ? r+b :
r;
1269 static constexpr
inline int64
1270 SYSwrapmod(int64 a, int64 b)
1275 return ((a < 0) ^ (b < 0) && r) ? r+b :
r;
1278 static constexpr
inline fpreal32
1279 SYSsmooth(fpreal32 min, fpreal32 max, fpreal32 val)
1281 if (val <= min)
return 0;
1282 if (val >= max)
return 1;
1283 fpreal32
t = max -
min;
1284 if (
SYSequalZero(t, (fpreal32)1e-8))
return (fpreal32).5;
1285 t = (val -
min) / t;
1286 return t*t*((
fpreal32)3.0 - (fpreal32)2.0*
t);
1289 static constexpr
inline fpreal64
1290 SYSsmooth(fpreal64 min, fpreal64 max, fpreal64 val)
1292 if (val <= min)
return 0;
1293 if (val >= max)
return 1;
1294 fpreal64 t = max -
min;
1295 if (
SYSequalZero(t, (fpreal64)1e-18))
return (fpreal64).5;
1296 t = (val -
min) / t;
1297 return t*t*((
fpreal64)3.0 - (fpreal64)2.0*
t);
1300 static constexpr
inline fpreal32
1301 SYSsmooth(fpreal32 min, fpreal32 max, fpreal32 value, fpreal32 roll)
1305 fpreal32
f = SYSsmooth(min, max, value);
1306 return roll < (
fpreal32)1 ? (fpreal32)1-SYSpow((fpreal32)1-f,
1307 (fpreal32)1/roll) : SYSpow(f, roll);
1312 static constexpr
inline fpreal64
1313 SYSsmooth(fpreal64 min, fpreal64 max, fpreal64 value, fpreal64 roll)
1317 fpreal64 f = SYSsmooth(min, max, value);
1318 return roll < (
fpreal64)1 ? (fpreal64)1-SYSpow((fpreal64)1-f,
1319 (fpreal64)1/roll) : SYSpow(f, roll);
1324 static constexpr
inline fpreal32
1325 SYSsmoother(fpreal32 min, fpreal32 max, fpreal32 val)
1327 if (val <= min)
return 0;
1328 if (val >= max)
return 1;
1329 fpreal32 t = max -
min;
1330 if (
SYSequalZero(t, (fpreal32)1e-8))
return (fpreal32).5;
1331 t = (val -
min) / t;
1335 static constexpr
inline fpreal64
1336 SYSsmoother(fpreal64 min, fpreal64 max, fpreal64 val)
1338 if (val <= min)
return 0;
1339 if (val >= max)
return 1;
1340 fpreal64 t = max -
min;
1341 if (
SYSequalZero(t, (fpreal64)1e-18))
return (fpreal64).5;
1342 t = (val -
min) / t;
1346 static constexpr
inline fpreal32
1347 SYSfit(fpreal32 val, fpreal32 omin, fpreal32 omax, fpreal32 nmin, fpreal32 nmax)
1349 fpreal32 d = omax - omin;
1350 fpreal32 tmp = (d == 0) ? 0.5f * (nmin+nmax) :
SYSlerp(nmin, nmax, (val-omin)/d);
1355 static constexpr
inline fpreal64
1356 SYSfit(fpreal64 val, fpreal64 omin, fpreal64 omax, fpreal64 nmin, fpreal64 nmax)
1358 fpreal64 d = omax - omin;
1359 fpreal64 tmp = (d == 0) ? 0.5 * (nmin+nmax) :
SYSlerp(nmin, nmax, (val-omin)/d);
1365 static constexpr
inline fpreal32
1366 SYSefit(fpreal32 v, fpreal32 omin, fpreal32 omax, fpreal32 nmin, fpreal32 nmax)
1368 fpreal32 d = omax - omin;
1369 return (d == 0) ? 0.5f * (nmin+nmax) :
SYSlerp(nmin, nmax, (v-omin)/d);
1372 static constexpr
inline fpreal64
1373 SYSefit(fpreal64 v, fpreal64 omin, fpreal64 omax, fpreal64 nmin, fpreal64 nmax)
1375 fpreal64 d = omax - omin;
1376 return (d == 0) ? 0.5 * (nmin+nmax) :
SYSlerp(nmin, nmax, (v-omin)/d);
1379 static constexpr
inline fpreal32
1380 SYSfit01(fpreal32 val, fpreal32 nmin, fpreal32 nmax)
1382 if (val < 0)
return nmin;
1383 if (val > 1)
return nmax;
1384 return SYSlerp(nmin, nmax, val);
1387 static constexpr
inline fpreal64
1388 SYSfit01(fpreal64 val, fpreal64 nmin, fpreal64 nmax)
1390 if (val < 0)
return nmin;
1391 if (val > 1)
return nmax;
1392 return SYSlerp(nmin, nmax, val);
1397 static constexpr
inline fpreal32
1398 SYSinvlerp(fpreal32 v, fpreal32 omin, fpreal32 omax)
1400 return SYSefit(v, omin, omax, 0.0f, 1.0f);
1403 static constexpr
inline fpreal64
1404 SYSinvlerp(fpreal64 v, fpreal64 omin, fpreal64 omax)
1406 return SYSefit(v, omin, omax, 0.0, 1.0);
1410 static inline fpreal32
1411 SYShat(fpreal32 x, fpreal32 dx)
1413 const fpreal32 ax =
SYSabs(x);
1414 return (ax > dx ? 0 : 1 - ax / dx);
1417 static inline fpreal64
1418 SYShat(fpreal64 x, fpreal64 dx)
1420 const fpreal64 ax =
SYSabs(x);
1421 return (ax > dx ? 0 : 1 - ax / dx);
1425 static constexpr
inline fpreal32
1426 SYSdhat(fpreal32 x, fpreal32 dx)
1431 static constexpr
inline fpreal64
1432 SYSdhat(fpreal64 x, fpreal64 dx)
1439 template<
typename T>
1441 SYSroundDownToMultipleOf(T val, T multiple)
1447 int rem = val % multiple;
1452 return val - multiple - rem;
1459 SYSroundDownToMultipleOf<fpreal32>(fpreal32
val, fpreal32 multiple)
1463 fpreal32 modulus = SYSfmod(val, multiple);
1466 fpreal32 retval = val - modulus;
1467 if( val < (fpreal32)0 && modulus!=(fpreal32)0 )
1474 SYSroundDownToMultipleOf<fpreal64>(fpreal64
val, fpreal64 multiple)
1478 fpreal64 modulus = SYSfmod(val, multiple);
1481 fpreal64 retval = val - modulus;
1482 if( val < (fpreal64)0 && modulus!=(fpreal64)0 )
1487 template<
typename T>
1489 SYSroundUpToMultipleOf(T val, T multiple)
1495 int rem = val % multiple;
1502 return val + multiple - rem;
1507 SYSroundUpToMultipleOf<fpreal32>(fpreal32
val, fpreal32 multiple)
1512 modulus = SYSfmod(val, multiple);
1515 if (val > (fpreal32)0)
1517 return val - modulus;
1522 SYSroundUpToMultipleOf<fpreal64>(fpreal64
val, fpreal64 multiple)
1527 modulus = SYSfmod(val, multiple);
1530 if (val > (fpreal64)0)
1532 return val - modulus;
1535 inline constexpr uint32
1536 SYSroundUpPow2(uint32 val)
1552 static constexpr
inline uint32
1553 SYSwang_inthash(uint32 key)
1556 key += ~(key << 16);
1566 static constexpr
inline uint32
1567 SYSmultiplicative_inthash(uint32 key)
1572 return key * 2654435761u;
1575 static constexpr
inline uint64
1576 SYSmultiplicative_inthash64(uint64 key)
1579 return key * 11400714819323198485llu;
1582 static constexpr
inline uint64
1583 SYSwang_inthash64(uint64 key)
1586 key += ~(key << 32ULL);
1588 key += ~(key << 13);
1592 key += ~(key << 27);
1597 static constexpr
inline uint32
1598 SYSsharpe_inthash(uint32 key)
1607 key ^= ~(key >> 15);
1613 static constexpr
inline uint32
1614 SYSwang2_inthash(uint32 key)
1618 constexpr
uint c2=0x27d4eb2d;
1619 key = (key ^ 61) ^ (key >> 16);
1620 key = key + (key << 3);
1621 key = key ^ (key >> 4);
1623 key = key ^ (key >> 15);
1627 static constexpr
inline uint64
1628 SYSwang2_inthash(uint64 key)
1632 key = (~key) + (key << 21);
1633 key = key ^ (key >> 24);
1634 key = (key + (key << 3)) + (key << 8);
1635 key = key ^ (key >> 14);
1636 key = (key + (key << 2)) + (key << 4);
1637 key = key ^ (key >> 28);
1638 key = key + (key << 31);
1643 SYSreal_hash(fpreal16 a,
int lowmask=0x3)
1645 return SYSwang_inthash(a.
bits() & (~lowmask));
1649 SYSreal_hashseed(fpreal16 a,
uint seed,
int lowmask=0x3)
1651 return SYSwang_inthash(seed + (a.
bits() & (~lowmask)));
1655 SYSreal_hash(fpreal32 a,
int lowmask=0xf)
1659 return SYSwang_inthash(ai.
uval & (~lowmask));
1663 SYSreal_hashseed(fpreal32 a,
uint seed,
int lowmask=0xf)
1667 return SYSwang_inthash(seed + (ai.
uval & (~lowmask)));
1671 SYSreal_hash(fpreal64 a,
int lowmask=0xf)
1675 return SYSwang_inthash64(ai.
uval & (~lowmask));
1679 SYSreal_hashseed(fpreal64 a,
uint seed, int64 lowmask=0xf)
1683 return SYSwang_inthash64(seed + (ai.
uval & (~lowmask)));
1686 static constexpr
inline uint
1687 SYSvector_hash(
const uint8 *vector,
int size)
1690 for (
int i = 0; i <
size; ++i)
1691 hash = SYSwang_inthash(hash + vector[i]);
1695 static constexpr
inline uint
1696 SYSvector_hash(
const int8 *vector,
int size)
1699 for (
int i = 0; i <
size; ++i)
1700 hash = SYSwang_inthash(hash + vector[i]);
1704 static constexpr
inline uint
1705 SYSvector_hash(
const int16 *vector,
int size)
1708 for (
int i = 0; i <
size; ++i)
1709 hash = SYSwang_inthash(hash + vector[i]);
1713 static constexpr
inline uint
1714 SYSvector_hash(
const int32 *vector,
int size)
1717 for (
int i = 0; i <
size; ++i)
1718 hash = SYSwang_inthash(hash + vector[i]);
1722 static constexpr
inline uint
1723 SYSvector_hash(
const int64 *vector,
int size)
1726 for (
int i = 0; i <
size; ++i)
1727 hash = (
uint)SYSwang_inthash64(hash + vector[i]);
1732 SYSvector_hash(
const fpreal16 *vector,
int size)
1735 for (
int i = 0; i <
size; ++i)
1736 hash = SYSreal_hashseed(vector[i], hash);
1741 SYSvector_hash(
const fpreal32 *vector,
int size)
1744 for (
int i = 0; i <
size; ++i)
1745 hash = SYSreal_hashseed(vector[i], hash);
1750 SYSvector_hash(
const fpreal64 *vector,
int size)
1753 for (
int i = 0; i <
size; ++i)
1754 hash = SYSreal_hashseed(vector[i], hash);
1763 static inline size_t
1764 SYSvector3_hash(
const P &vector)
1766 static constexpr
size_t p1 = 73856093;
1767 static constexpr
size_t p2 = 19349663;
1768 static constexpr
size_t p3 = 83492791;
1770 return size_t(vector.x()*p1) ^
size_t(vector.y()*p2) ^
size_t(vector.z()*p3);
1774 inline static fpreal32
1775 SYShashToFloat01(
uint hash)
1778 tmp.
uval = 0x3f800000 | (0x007fffff & hash);
1779 return tmp.
fval-1.0F;
1783 static inline fpreal32
1784 SYSfastRandom(
uint &seed)
1786 seed = seed*1664525 + 1013904223;
1787 return SYShashToFloat01(seed);
1790 inline static fpreal32
1791 SYSrandom(
uint &seed)
1793 seed = seed*1664525 + 1013904223;
1794 return SYShashToFloat01(SYSwang_inthash(seed));
1797 static constexpr
uint
1798 SYSfastRandomInt(
uint &seed)
1800 seed = seed*1664525 + 1013904223;
1801 return SYSwang_inthash(seed);
1804 inline static fpreal32
1805 SYSfastRandomZero(
uint &seed)
1807 return SYSfastRandom(seed) - 0.5F;
1810 inline static fpreal32
1811 SYSrandomZero(
uint &seed)
1813 return SYSrandom(seed) - 0.5F;
1816 template <
typename T>
1817 static constexpr
inline void
1818 SYSminmax(T
v0, T
v1, T
v2, T
v3, T &min, T &max)
1820 min =
SYSmin(v0, v1, v2, v3);
1821 max =
SYSmax(v0, v1, v2, v3);
1824 template <
typename T>
1825 static constexpr
inline void
1826 SYSminmax(T
v0, T
v1, T
v2, T &min, T &max)
1828 min =
SYSmin(v0, v1, v2);
1829 max =
SYSmax(v0, v1, v2);
1832 template <
typename T>
1833 static constexpr
inline void
1834 SYSminmax(T
v0, T
v1, T &min, T &max)
1840 SYS_API void SYSsincosDeg(fpreal64
degrees, fpreal64 *
s, fpreal64 *c);
1841 SYS_API void SYSsincosDeg(fpreal32
degrees, fpreal32 *
s, fpreal32 *c);
1844 SYSgetSinCosFromSlope(fpreal32 slope, fpreal32 &sintheta, fpreal32 &costheta)
1846 fpreal32 one_over_m;
1847 sintheta = slope / SYSsqrt(slope*slope + (fpreal32)1);
1848 if ((slope =
SYSabs(slope)) > (fpreal32)1)
1851 costheta = one_over_m / SYSsqrt(one_over_m*one_over_m + 1);
1854 costheta = SYSsqrt((fpreal32)1 - sintheta*sintheta);
1858 SYSgetSinCosFromSlope(fpreal64 slope, fpreal64 &sintheta, fpreal64 &costheta)
1860 fpreal64 one_over_m;
1861 sintheta = slope / SYSsqrt(slope*slope + (fpreal64)1);
1862 if ((slope =
SYSabs(slope)) > (fpreal64)1)
1865 costheta = one_over_m / SYSsqrt(one_over_m*one_over_m + 1);
1868 costheta = SYSsqrt((fpreal64)1 - sintheta*sintheta);
1871 inline constexpr
static bool
1872 SYSsameSign( fpreal32
v0, fpreal32
v1 )
1877 inline constexpr
static bool
1878 SYSsameSign( fpreal64 v0, fpreal64 v1 )
1883 inline constexpr
static bool
1884 SYSsameSign( int32 v0, int32 v1 )
1886 return (v0 ^ v1) >= 0;
1889 inline constexpr
static bool
1890 SYSsameSign( int64 v0, int64 v1 )
1892 return (v0 ^ v1) >= 0;
1896 SYSnextPrime(
uint num)
1901 static constexpr
inline int
1902 SYShexCharToInt(
char c)
1906 if (c >=
'0' && c <=
'9')
1908 if (c >=
'a' && c <=
'f')
1909 return c -
'a' + 10;
1910 if (c >=
'A' && c <=
'F')
1911 return c -
'A' + 10;
1915 static constexpr
inline char
1916 SYSintToHexChar(
int value)
1919 return value < 10 ?
'0' + value :
'a' + value - 10;
1922 SYS_API void SYSsort(
int &a,
int &b);
1923 SYS_API void SYSsort(
int &a,
int &b,
int &c);
1924 SYS_API void SYSsort(int64 &a, int64 &b);
1925 SYS_API void SYSsort(int64 &a, int64 &b, int64 &c);
1926 SYS_API void SYSsort(
float &a,
float &b);
1927 SYS_API void SYSsort(
float &a,
float &b,
float &c);
1928 SYS_API void SYSsort(
double &a,
double &b);
1929 SYS_API void SYSsort(
double &a,
double &b,
double &c);
1933 static constexpr
inline void
1934 SYSdivMod(
int numerator,
int denominator,
int "ient,
int &remainder)
1936 quotient = numerator / denominator;
1937 remainder = numerator % denominator;
1940 static constexpr
inline void
1941 SYSdivMod(int64 numerator, int64 denominator, int64 "ient, int64 &remainder)
1943 quotient = numerator / denominator;
1944 remainder = numerator % denominator;
1952 #define SYSmax(a,b) ((a) > (b) ? (a) : (b))
1953 #define SYSmin(a,b) ((a) < (b) ? (a) : (b))
1954 #define SYSabs(a) ((a) < 0 ? (a) : -(a))
bool SYSisNormal(fpreal64 f)
bool SYSisEqual(const UT_Vector2T< T > &a, const UT_Vector2T< T > &b, S tol)
Componentwise equality.
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
UT_Vector3T< T > SYSrecip(const UT_Vector3T< T > &v)
SYS_API fpreal32 SYSceil(const fpreal32 val)
SYS_API void SYSsrand48(long seed)
GLsizei const GLfloat * value
bool SYSisFinite(fpreal64 f)
bool SYSisInf(fpreal64 f)
bool SYSisInteger(const UT_Vector2T< T > &v1)
Componentwise integer test.
vfloat4 sqrt(const vfloat4 &a)
OIIO_HOSTDEVICE void sincos(float x, float *sine, float *cosine)
constexpr bool SYSisNan(const F f)
GLboolean GLboolean GLboolean GLboolean a
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
UT_Matrix2T< T > SYSbilerp(const UT_Matrix2T< T > &u0v0, const UT_Matrix2T< T > &u1v0, const UT_Matrix2T< T > &u0v1, const UT_Matrix2T< T > &u1v1, S u, S v)
Bilinear interpolation.
GLfloat GLfloat GLfloat v2
UT_Matrix2T< T > SYSlerp(const UT_Matrix2T< T > &v1, const UT_Matrix2T< T > &v2, S t)
unsigned long long uint64
GLfloat GLfloat GLfloat GLfloat v3
UT_Matrix2T< T > SYSbarycentric(const UT_Matrix2T< T > &v0, const UT_Matrix2T< T > &v1, const UT_Matrix2T< T > &v2, S u, S v)
Barycentric interpolation.
ImageBuf OIIO_API pow(const ImageBuf &A, cspan< float > B, ROI roi={}, int nthreads=0)
IMATH_HOSTDEVICE constexpr int trunc(T x) IMATH_NOEXCEPT
SYS_API fpreal32 SYSroundAngle(fpreal32 base, fpreal32 source)
UT_Vector3T< T > SYSclamp(const UT_Vector3T< T > &v, const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
GLsizei GLsizei GLchar * source
SYS_API bool SYSisPrime(uint num)
SYS_API fpreal32 SYSfloor(const fpreal32 val)
constexpr int SYSsignum(const F a) noexcept
SYS_API double SYSdrand48()
UT_Vector2T< T > SYSinvlerp(const UT_Vector2T< T > &a, const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2)
Componentwise inverse linear interpolation.
SYS_API bool SYSisInt(const char *str)
GLboolean GLboolean GLboolean b
bool SYSequalZero(const UT_Vector3T< T > &v)
SYS_API uint SYSmakePrime(uint num)
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
fpreal32 SYSrint(const fpreal32 val)
unsigned short bits() const
OIIO_FORCEINLINE T log(const T &v)
vfloat4 rsqrt(const vfloat4 &a)
Fully accurate 1/sqrt.
SYS_API bool SYSisFloat(const char *str)
bool isNormalized() const
OIIO_FORCEINLINE OIIO_HOSTDEVICE T degrees(T rad)
Convert radians to degrees.