51 return a >= T{0} ? a : -a;
78 MOCHI_CONCEPT(std::is_arithmetic_v<From>&& std::is_arithmetic_v<To>)>
80 return static_cast<To
>(a);
88 MOCHI_CONCEPT(std::is_pointer_v<From>&& std::is_pointer_v<To>)>
90 static_assert(
sizeof(To) ==
sizeof(From),
"Invalid cast. Types must be the same size.");
91 return reinterpret_cast<To
>(a);
99 MOCHI_CONCEPT(std::is_arithmetic_v<From>&& std::is_arithmetic_v<To>)>
101 static_assert(
sizeof(To) ==
sizeof(From),
"Invalid cast. Types must be the same size.");
103 memcpy(&b, &a,
sizeof(a));
118 if constexpr (std::is_floating_point_v<T>) {
119 return std::fma(a, b, c);
127 if constexpr (std::is_floating_point_v<T>) {
128 return std::fma(a, b, -c);
136 if constexpr (std::is_floating_point_v<T>) {
137 return std::fma(-a, b, c);
145 if constexpr (std::is_floating_point_v<T>) {
146 return std::fma(-a, b, -c);
156template <
typename T, MOCHI_CONCEPT(std::is_arithmetic_v<T>)>
158 if constexpr (std::is_integral_v<T>) {
161 return std::isfinite(a);
171 return (a <= b) ? a : b;
175template <
typename T,
typename... More>
177 return Min(a,
Min(b, c, args...));
182 return (a >= b) ? a : b;
186template <
typename T,
typename... More>
188 return Max(a,
Max(b, c, args...));
191template <
typename ValT,
typename MinT,
typename MaxT>
193 return Min(ValT(max),
Max(ValT(min), value));
196template <
typename ValT,
typename MinT,
typename MaxT,
bool kMinInclusive,
bool kMaxInclusive>
201 std::integral_constant<bool, kMinInclusive> = std::true_type{},
202 std::integral_constant<bool, kMaxInclusive> = std::false_type{}) {
204 if constexpr (kMinInclusive) {
205 test1 = (value >= min);
207 test1 = (value > min);
210 if constexpr (kMaxInclusive) {
211 test2 = (value <= max);
213 test2 = (value < max);
215 return static_cast<ValT
>(test1 && test2);
238template <
typename T, MOCHI_CONCEPT(std::is_arithmetic_v<T>)>
241 static_assert(!std::is_same_v<std::remove_cv_t<T>,
bool>,
"NearEqual not supported for bool");
242 return Abs(a - b) <= epsilon;
245template <
typename T, MOCHI_CONCEPT(std::is_arithmetic_v<T>)>
248 static_assert(!std::is_same_v<std::remove_cv_t<T>,
bool>,
"NearEqualRel not supported for bool");
249 auto scaledEpsilon = epsilon *
Max(
Abs(a),
Abs(b));
250 auto tolerance =
Max(epsilon, scaledEpsilon);
254template <
typename T, MOCHI_CONCEPT(std::is_arithmetic_v<T>)>
258 static_assert(!std::is_same_v<std::remove_cv_t<T>,
bool>,
"NearZero not supported for bool");
259 return Abs(a) <= epsilon;
270 return static_cast<int>(a.size());
274template <
typename T,
size_t N>
276 return static_cast<int>(N);
283template <
typename T,
typename Frac>
285 return T(a * (Frac{1} - t) + b * t);
296 return Lerp<T>(outA, outB, (value - inA) / (inB - inA));
306 return Lerp<T>(outA, outB,
Clamp<T>((value - inA) / (inB - inA), T{0}, T{1}));
313template <
typename B,
typename E>
315 return std::pow(base, exp);
344 std::is_integral_v<T> && !std::is_same_v<std::remove_cv_t<T>,
bool>,
345 "IsPowerOfTwo requires a non-bool integral type");
346 return (a > T(0)) && ((a & (a - T(1))) == T(0));
354 std::is_integral_v<T> && !std::is_same_v<std::remove_cv_t<T>,
bool>,
355 "NextPowerOfTwo requires a non-bool integral type");
356 [[maybe_unused]]
constexpr T kMaxPowerOfTwo = T(1) << (std::numeric_limits<T>::digits - 1);
358 a <= kMaxPowerOfTwo,
"Input exceeds the largest representable power of two.");
362 using UnsignedT = std::make_unsigned_t<T>;
364 std::numeric_limits<UnsignedT>::digits > 4 && std::numeric_limits<UnsignedT>::digits <= 64,
365 "Unsupported integral type");
366 auto result =
static_cast<UnsignedT
>(a - T(1));
367 result |= result >> 1;
368 result |= result >> 2;
369 result |= result >> 4;
370 if constexpr (std::numeric_limits<UnsignedT>::digits > 8) {
371 result |= result >> 8;
373 if constexpr (std::numeric_limits<UnsignedT>::digits > 16) {
374 result |= result >> 16;
376 if constexpr (std::numeric_limits<UnsignedT>::digits > 32) {
377 result |= result >> 32;
379 return static_cast<T
>(result + UnsignedT(1));
408 return std::floor(a);
418 return std::round(a);
424 std::is_integral_v<T> && !std::is_same_v<std::remove_cv_t<T>,
bool>,
"Unsupported type");
425 return ((numToRound + multiple - 1) / multiple) * multiple;
431 std::is_integral_v<T> && !std::is_same_v<std::remove_cv_t<T>,
bool>,
"Unsupported type");
432 return (numToRound / multiple) * multiple;
445 static_assert(std::is_arithmetic_v<T>,
"Unsupported type");
446 return condition ? a : b;
456 return Select(x >= T{}, T{1}, T{-1});
498 return std::atan2(y, x);
#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects,...)
constexpr T NegMulAdd(T a, T b, T c)
constexpr bool IsPowerOfTwo(T a)
constexpr T const & Min(T const &a, T const &b)
constexpr T RoundDown(T numToRound, T multiple)
constexpr auto Equal(T const &a, T const &b)
constexpr T NextPowerOfTwo(T a)
constexpr To StaticCast(From const &a)
constexpr T kDefaultNearEqualEpsilon
constexpr ValT Rect(ValT value, MinT min, MaxT max, std::integral_constant< bool, kMinInclusive >=std::true_type{}, std::integral_constant< bool, kMaxInclusive >=std::false_type{})
constexpr T NegMulSub(T a, T b, T c)
constexpr T ATan2(T y, T x)
constexpr T RoundUp(T numToRound, T multiple)
constexpr auto NotEqual(T const &a, T const &b)
constexpr T MulSub(T a, T b, T c)
constexpr T Select(bool condition, T a, T b)
constexpr T SignedSqrt(T a)
constexpr T IntegralSqrt(T a)
constexpr ValT Clamp(ValT value, MinT min, MaxT max)
constexpr T SignedSqr(T a)
constexpr T RemapAndClamp(T value, T inA, T inB, T outA, T outB)
constexpr int isize(T const &a)
constexpr T Lerp(T a, T b, Frac t)
constexpr T const & Max(T const &a, T const &b)
constexpr T RcpApprox(T a)
constexpr T MulAdd(T a, T b, T c)
bool NearEqual(TransformRT const &a, TransformRT const &b, real epsilon=kDefaultNearEqualEpsilon< real >)
constexpr B Pow(B base, E exp)
constexpr auto NearEqualRel(T const &a, T const &b, T epsilon=kDefaultNearEqualEpsilon< T >)
bool IsFinite(TransformRT const &a)
constexpr T Remap(T value, T inA, T inB, T outA, T outB)
constexpr auto NearZero(T const &a, T epsilon=kDefaultNearEqualEpsilon< T >)