23template<
typename T>
inline bool is_zero(
const T &a)
33template<
typename T>
inline T abs(
const T &a)
38template<
typename T>
inline T sign(
const T &a)
40 return (
T(0) < a) - (a <
T(0));
43template<
typename T>
inline T min(
const T &a,
const T &
b)
45 static_assert(std::is_arithmetic_v<T>,
"math::min on non-arithmetic type is likely unintended");
46 return std::min(a,
b);
49template<
typename T>
inline T max(
const T &a,
const T &
b)
51 static_assert(std::is_arithmetic_v<T>,
"math::max on non-arithmetic type is likely unintended");
52 return std::max(a,
b);
57 static_assert(std::is_arithmetic_v<T>,
58 "math::max_inplace on non-arithmetic type is likely unintended");
64 static_assert(std::is_arithmetic_v<T>,
65 "math::min_inplace on non-arithmetic type is likely unintended");
71 return std::clamp(a,
min,
max);
74template<
typename T>
inline T step(
const T &edge,
const T &value)
76 return value < edge ? 0 : 1;
79template<
typename T>
inline T mod(
const T &a,
const T &
b)
81 return std::fmod(a,
b);
86 return (
b != 0) ? std::fmod(a,
b) : 0;
91 return a - std::floor(a /
b) *
b;
96 return (
b != 0) ? a - std::floor(a /
b) *
b : 0;
101 static_assert(std::is_arithmetic_v<T>,
102 "math::min_max on non-arithmetic type is likely unintended");
103 min = std::min(value,
min);
104 max = std::max(value,
max);
109 return (
b != 0) ? a /
b :
T(0.0f);
112template<
typename T>
inline T floor(
const T &a)
114 return std::floor(a);
117template<
typename T>
inline T round(
const T &a)
119 return std::round(a);
130 if constexpr (std::is_integral_v<T>) {
132 return ((a %
b) +
b) %
b;
138template<
typename T>
inline T ceil(
const T &a)
145 return std::abs(a -
b);
148template<
typename T>
inline T fract(
const T &a)
150 return a - std::floor(a);
153template<
typename T>
inline T sqrt(
const T &a)
160template<
typename T>
inline T rcp(
const T &a)
162 static_assert(!std::is_integral_v<T>,
"T must not be an integral type.");
170 static_assert(!std::is_integral_v<T>,
"T must be not be an integral type.");
171 return a ?
T(1) / a :
T(0);
174template<
typename T>
inline T cos(
const T &a)
179template<
typename T>
inline T sin(
const T &a)
184template<
typename T>
inline T tan(
const T &a)
189template<
typename T>
inline T acos(
const T &a)
196 return std::pow(
x,
power);
201 return (
x < 0 || (
x == 0 &&
power <= 0)) ?
x : std::pow(
x,
power);
206 return (
x < 0 || (
x == 0 &&
power <= 0)) ? fallback : std::pow(
x,
power);
214template<
typename T>
inline T cube(
const T &a)
219template<
typename T>
inline T exp(
const T &
x)
238 const float f = std::abs(
x);
240 const float m = (f < 1.0f) ? 1.0f - (1.0f - f) : 1.0f;
248 const float a = std::sqrt(1.0f - m) *
249 (1.5707963267f + m * (-0.213300989f + m * (0.077980478f + m * -0.02164095f)));
253template<
typename T>
inline T asin(
const T &a)
258template<
typename T>
inline T atan(
const T &a)
265 return std::atan2(
y,
x);
270 return std::hypot(
y,
x);
273template<
typename T,
typename FactorT>
276 auto result = a * (1 - t) +
b * t;
277 if constexpr (std::is_integral_v<T> && std::is_floating_point_v<FactorT>) {
285 if constexpr (std::is_integral_v<T>) {
287 using Unsigned = std::make_unsigned_t<T>;
289 Unsigned smaller = a;
296 return a +
sign *
T(Unsigned(larger - smaller) / 2);
299 return (a +
b) *
T(0.5);
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
ccl_device_inline float2 power(const float2 v, const float e)
T safe_floored_mod(const T &a, const T &b)
float safe_acos_approx(float x)
constexpr bool is_math_float_type
T clamp(const T &a, const T &min, const T &max)
bool is_any_zero(const T &a)
T safe_divide(const T &a, const T &b)
constexpr bool is_math_integral_type
T distance(const T &a, const T &b)
T floored_mod(const T &a, const T &b)
void min_inplace(T &a, const T &b)
T min(const T &a, const T &b)
T midpoint(const T &a, const T &b)
T safe_mod(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T safe_pow(const T &x, const T &power)
T fallback_pow(const T &x, const T &power, const T &fallback)
T atan2(const T &y, const T &x)
void min_max(const T &value, T &min, T &max)
T mod_periodic(const T &a, const T &b)
T max(const T &a, const T &b)
T hypot(const T &y, const T &x)
void max_inplace(T &a, const T &b)