16# include <hip/hip_vector_types.h>
19#if !defined(__KERNEL_METAL__)
24#if !defined(__KERNEL_GPU__)
34# define M_PI_F (3.1415926535897932f)
37# define M_PI_2_F (1.5707963267948966f)
40# define M_PI_4_F (0.7853981633974830f)
43# define M_1_PI_F (0.3183098861837067f)
46# define M_2_PI_F (0.6366197723675813f)
49# define M_1_2PI_F (0.1591549430918953f)
52# define M_1_4PI_F (0.0795774715459476f)
55# define M_SQRT_PI_8_F (0.6266570686577501f)
58# define M_LN_2PI_F (1.8378770664093454f)
63# define M_2PI_F (6.2831853071795864f)
66# define M_4PI_F (12.566370614359172f)
69# define M_PI_4F 0.78539816339744830962f
74# define M_SQRT2_F (1.4142135623730950f)
77# define M_CBRT2_F 1.2599210498948732f
80# define M_SQRT1_2F 0.70710678118654752440f
83# define M_SQRT3_F (1.7320508075688772f)
86# define M_LN2_F (0.6931471805599453f)
89# define M_LN10_F (2.3025850929940457f)
94#if !defined(__HIP__) && !defined(__KERNEL_ONEAPI__)
98 return (a >
b) ? a :
b;
103 return (a <
b) ? a :
b;
109#if defined(__KERNEL_METAL__)
110using metal::isfinite;
119#if !defined(__KERNEL_GPU__) || defined(__KERNEL_ONEAPI__)
120# ifndef __KERNEL_ONEAPI__
125# define isfinite(x) sycl::isfinite((x))
126# define isnan(x) sycl::isnan((x))
131 return (
x > 0) ?
x : -
x;
136 return (a >
b) ? a :
b;
141 return (a <
b) ? a :
b;
146 return (a >
b) ? a :
b;
151 return (a <
b) ? a :
b;
156 return (a >
b) ? a :
b;
161 return (a <
b) ? a :
b;
174 return (a >
b) ? a :
b;
180 return (a <
b) ? a :
b;
185 return (a >
b) ? a :
b;
190 return (a <
b) ? a :
b;
195 return (a >
b) ? a :
b;
200 return (a <
b) ? a :
b;
237#if !defined(__KERNEL_METAL__) && !defined(__KERNEL_ONEAPI__)
314#if defined(__KERNEL_METAL__)
327 return ((
uint64_t)ptr) & 0xFFFFFFFF;
332 return (((
uint64_t)ptr) >> 32) & 0xFFFFFFFF;
342 return (a << 16) |
b;
359 return (
x << 1) > 0xff000000u;
366 return (f == f) && (
x == 0 ||
x == (1u << 31) || (f != 2.0f * f)) && !((
x << 1) > 0xff000000u);
375#if defined(__KERNEL_METAL__)
378using metal::smoothstep;
382 return min(
max(a, mn), mx);
387 return min(
max(a, mn), mx);
396 else if (
x >= edge1) {
400 const float t = (
x - edge0) / (edge1 - edge0);
401 result = (3.0f - 2.0f * t) * (t * t);
419 return a + t * (
b - a);
426template<
typename T1,
typename T2>
429 return (1.0f - t) * a + t *
b;
432#if !defined(__KERNEL_METAL__)
435# ifdef __KERNEL_OPTIX__
446# elif defined(__KERNEL_CUDA__)
447 return __saturatef(a);
449 return clamp(a, 0.0f, 1.0f);
484 const float range =
max -
min;
485 return (range != 0.0f) ? value - (range *
floorf((value -
min) / range)) :
min;
490 return (
b != 0.0f) ?
fabsf(
fractf((a -
b) / (
b * 2.0f)) *
b * 2.0f -
b) : 0.0f;
497 return fminf(a,
b) - h * h * h * k * (1.0f / 6.0f);
504 return (f < 0.0f) ? -1.0f : 1.0f;
520 return (
x == 0.0f &&
y == 0.0f) ? 0.0f :
atan2f(
y,
x);
540 const float ff = f * f;
541 return (3.0f * ff - 2.0f * ff * f);
546 return (
x % m + m) % m;
551 return a + t * (
b - a);
556 return (
x - a) / (
b - a);
563 (((d + 3.0f * (
b - c) - a) *
x + (2.0f * a - 5.0f *
b + 4.0f * c - d)) *
x +
578#if defined(__KERNEL_METAL__)
579 return (f > 0.0f) ? metal::rsqrt(f) : 0.0f;
581 return (f > 0.0f) ? 1.0f /
sqrtf(f) : 0.0f;
607 if (
fmodf(-
y, 2.0f) == 0.0f) {
629 return (
b != 0.0f) ? a /
b : 0.0f;
643 return (
b != 0.0f) ?
fmodf(a,
b) : 0.0f;
648 return (
b != 0.0f) ? a -
floorf(a /
b) *
b : 0.0f;
669 return s_sq > 0.0004f ? 1.0f -
safe_sqrtf(1.0f - s_sq) : 0.5f * s_sq;
694#ifdef __KERNEL_METAL__
699 const float _1_180 = 1.0f / 180.0f;
700 const float log2pi = 1.83787706641f;
701 const float logx = metal::log(
x);
702 return (log2pi - logx +
703 x * (logx * 2.0f + metal::log(
x *
sinhf(1.0f /
x) + (_1_180 /
powf(
x, 6.0f))) - 2.0f)) *
725 return bits * (1.0f / (
float)0xFFFFFFFF);
733#if !defined(__KERNEL_GPU__)
736 return std::popcount(
x);
740 return std::popcount(
x);
742#elif defined(__KERNEL_ONEAPI__)
743# define popcount(x) sycl::popcount(x)
744#elif defined(__KERNEL_HIP__)
746# define popcount(x) __popcll(x)
747#elif defined(__KERNEL_METAL__)
748using metal::popcount;
750# define popcount(x) __popc(x)
755#if defined(__KERNEL_CUDA__) || defined(__KERNEL_OPTIX__) || defined(__KERNEL_HIP__)
757#elif defined(__KERNEL_METAL__)
758 return metal::clz(
x);
759#elif defined(__KERNEL_ONEAPI__)
764 unsigned long leading_zero = 0;
765 _BitScanReverse(&leading_zero,
x);
766 return (31 - leading_zero);
768 return __builtin_clz(
x);
775#if defined(__KERNEL_CUDA__) || defined(__KERNEL_OPTIX__) || defined(__KERNEL_HIP__)
776 return (__ffs(
x) - 1);
777#elif defined(__KERNEL_METAL__)
778 return metal::ctz(
x);
779#elif defined(__KERNEL_ONEAPI__)
784 unsigned long ctz = 0;
785 _BitScanForward(&ctz,
x);
788 return __builtin_ctz(
x);
795#if defined(__KERNEL_CUDA__) || defined(__KERNEL_OPTIX__) || defined(__KERNEL_HIP__)
797#elif defined(__KERNEL_METAL__)
798 return (
x != 0) ? metal::ctz(
x) + 1 : 0;
803 return __builtin_ffs(
x);
820 if (
fabsf(a -
b) < abs_diff) {
824 if ((a < 0.0f) != (
b < 0.0f)) {
844# define __has_builtin(v) 0
851#if defined(__KERNEL_CUDA__)
853#elif defined(__KERNEL_METAL__)
854 return metal::reverse_bits(
x);
855#elif defined(__aarch64__) || (defined(_M_ARM64) && !defined(_MSC_VER))
857 __asm__(
"rbit %w0, %w1" :
"=r"(
x) :
"r"(
x));
859#elif defined(__arm__) && ((__ARM_ARCH > 7) || __ARM_ARCH == 6 && __ARM_ARCH_ISA_THUMB >= 2)
862 __asm__(
"rbit %0, %1" :
"=r"(
x) :
"r"(
x));
864#elif __has_builtin(__builtin_bitreverse32)
865 return __builtin_bitreverse32(
x);
868 x = ((
x & 0x55555555) << 1) | ((
x & 0xAAAAAAAA) >> 1);
870 x = ((
x & 0x33333333) << 2) | ((
x & 0xCCCCCCCC) >> 2);
872 x = ((
x & 0x0F0F0F0F) << 4) | ((
x & 0xF0F0F0F0) >> 4);
875 return _byteswap_ulong(
x);
876# elif defined(__INTEL_COMPILER)
877 return (uint32_t)_bswap((
int)
x);
880 return __builtin_bswap32(
x);
892 const bool valid_linear = (a == 0.0f) && (
b != 0.0f);
895 const float discriminant =
sqr(
b) - 4.0f * a * c;
897 const bool valid_quadratic = (a != 0.0f) && (discriminant > -1e-5f);
899 if (valid_quadratic) {
905 const float r1 = temp / a;
906 const float r2 = c / temp;
912 return (valid_linear || valid_quadratic);
928 return value >=
min && value <=
max;
937template<
typename T1,
typename T2>
950 return {
max(first.min, second.min),
min(first.max, second.max)};
976 return {a.min +
b.min, a.max +
b.max};
990 return {
min(a.min,
b.min),
max(a.max,
b.max)};
996 return {
min(a.min, v),
max(a.max, v)};
1003 return ((val <= 0.0f) ?
1005 ((val > (1.0f - 0.5f / 255.0f)) ? 255 : (
uchar)((255.0f * val) + 0.5f)));
1010 return val * (1.0f / 255.0f);
unsigned long long int uint64_t
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
#define ccl_device_forceinline
#define ccl_device_inline
#define ccl_device_template_spec
#define ccl_device_inline_method
#define CCL_NAMESPACE_END
#define assert(assertion)
T smoothstep(T, T, T) RET
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
float endvalue_preserving_mix(float a, float b, float t)
float cos_from_sin(float s)
float sin_from_cos(float c)
float safe_divide(float a, float b)
__forceinline LightTreeMeasure operator+(const LightTreeMeasure &a, const LightTreeMeasure &b)
OrientationBounds merge(const OrientationBounds &cone_a, const OrientationBounds &cone_b)
ccl_device_inline float floorfrac(const float x, ccl_private int *i)
ccl_device_inline bool isnan_safe(const float f)
ccl_device_inline float smoothstepf(const float f)
ccl_device_inline float byte_to_float(const uchar val)
ccl_device_inline Interval< T1 > operator/=(ccl_private Interval< T1 > &interval, const T2 f)
ccl_device_inline uint prev_power_of_two(const uint x)
ccl_device_inline int ceil_to_int(const float f)
ccl_device float safe_asinf(const float a)
ccl_device_inline uint uint16_unpack_from_uint_1(const uint i)
ccl_device_inline float or_mask(const float x, const uint y)
ccl_device_inline float signf(const float f)
ccl_device float safe_powf(const float a, const float b)
ccl_device_inline T min4(const T &a, const T &b, const T &c, const T &d)
ccl_device_inline float pow22(const float a)
ccl_device_inline uint count_leading_zeros(const uint x)
ccl_device float bits_to_01(const uint bits)
ccl_device_inline float inversesqrtf(const float f)
ccl_device_inline float compatible_atan2(const float y, const float x)
ccl_device float compatible_powf(const float x, const float y)
ccl_device_inline Extrema< T > operator*(const Extrema< T > a, const T b)
ccl_device_inline float safe_sqrtf(const float f)
ccl_device_inline uint pointer_pack_to_uint_1(T *ptr)
ccl_device_inline float cubic_interp(const float a, const float b, float c, const float d, float x)
ccl_device_inline int floor_to_int(const float f)
ccl_device_inline uint32_t reverse_integer_bits(uint32_t x)
ccl_device_inline int power_of_2(const int a)
ccl_device_inline Extrema< T > operator+=(ccl_private Extrema< T > &a, const ccl_private Extrema< T > &b)
ccl_device_inline float sin_sqr_to_one_minus_cos(const float s_sq)
ccl_device float safe_acosf(const float a)
ccl_device_inline float compatible_signf(const float f)
ccl_device_inline float xor_mask(const float x, const uint y)
ccl_device_inline uint uint16_pack_to_uint(const uint a, const uint b)
ccl_device_inline int as_int(const uint i)
ccl_device_inline float nonzerof(const float f, const float eps)
ccl_device_inline bool solve_quadratic(const float a, const float b, const float c, ccl_private float &x1, ccl_private float &x2)
ccl_device_inline float smoothminf(const float a, const float b, float k)
ccl_device_inline uint pointer_pack_to_uint_0(T *ptr)
ccl_device_inline T * pointer_unpack_from_uint(const uint a, const uint b)
ccl_device_inline uint next_power_of_two(const uint x)
ccl_device_inline float inverse_lerp(const float a, const float b, const float x)
ccl_device_inline T max4(const T &a, const T &b, const T &c, const T &d)
ccl_device_inline float pow20(const float a)
ccl_device_inline int float_to_int(const float f)
ccl_device_inline uint find_first_set(const uint x)
ccl_device_inline Interval< T > intervals_intersection(const ccl_private Interval< T > &first, const ccl_private Interval< T > &second)
ccl_device_inline float ensure_finite(const float v)
ccl_device_inline uchar float_to_byte(const float val)
ccl_device_inline float interp(const float a, const float b, const float t)
ccl_device_inline uint uint16_unpack_from_uint_0(const uint i)
ccl_device_inline uint popcount(const uint x)
ccl_device_inline float wrapf(const float value, const float max, const float min)
ccl_device_inline bool compare_floats(const float a, const float b, float abs_diff, const int ulp_diff)
ccl_device float safe_logf(const float a, const float b)
ccl_device_inline bool isfinite_safe(const float f)
ccl_device_inline float one_minus_cos(const float angle)
ccl_device_inline float beta(const float x, const float y)
ccl_device_inline float pingpongf(const float a, const float b)
ccl_device_inline uint count_trailing_zeros(const uint x)
ccl_device_inline T make_zero()
float safe_floored_modulo(float a, float b)
float safe_modulo(float a, float b)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ccl_device_inline_method Extrema(T value)
ccl_device_inline_method Extrema(T min_, T max_)
ccl_device_inline_method T range() const
ccl_device_inline_method T length() const
ccl_device_inline_method bool contains(T value) const
ccl_device_inline_method bool is_empty() const