math_utils.h
| File Path | engine/include/conjure/core/math/math_utils.h |
| API ↗ | Math |
Scalar helpers that need no glm: constants, angle conversion, approximate comparison, interpolation and integer alignment / power-of-two utilities. Everything here is pure, constexpr where the standard library allows it, and safe to call from any thread.
Concepts
unsigned_integer
template <typename IntegerType> concept unsigned_integer = std::unsigned_integral<IntegerType> && !std::same_as<IntegerType, bool>;
An unsigned integer type other than bool: what the alignment and power-of-two helpers accept.
Constants
| Name | Type | Value | Description |
|---|---|---|---|
DEGREES_TO_RADIANS |
inline constexpr f32 |
PI / 180.0f |
|
EPSILON |
inline constexpr f32 |
1.0e-6f |
|
EPSILON_F64 |
inline constexpr f64 |
1.0e-12 |
|
F32_INFINITY |
inline constexpr f32 |
std:: |
|
F32_MAX |
inline constexpr f32 |
std:: |
|
HALF_PI |
inline constexpr f32 |
0.5f * PI |
|
INVERSE_PI |
inline constexpr f32 |
1.0f / PI |
|
PI |
inline constexpr f32 |
3.14159265358979323846f |
|
QUARTER_PI |
inline constexpr f32 |
0.25f * PI |
|
RADIANS_TO_DEGREES |
inline constexpr f32 |
180.0f / PI |
|
TWO_PI |
inline constexpr f32 |
2.0f * PI |
Functions
Public
absolute
absoluteAbsolute value usable in constant expressions.
align_down
align_downtemplate <unsigned_integer IntegerType> [[nodiscard]] constexpr IntegerType align_down(
IntegerType value,
std::type_identity_t<IntegerType> alignment) noexcept;Rounds value down to the previous multiple of alignment, which must be a power of two (asserted; a bad alignment returns value unchanged).
align_up
align_uptemplate <unsigned_integer IntegerType> [[nodiscard]] constexpr IntegerType align_up(
IntegerType value,
std::type_identity_t<IntegerType> alignment) noexcept;Rounds value up to the next multiple of alignment, which must be a power of two (asserted; a bad alignment returns value unchanged). When the rounded value does not fit the type (value within alignment - 1 of its maximum) the function asserts and returns value unchanged rather than wrapping to 0.
clamp
clampClamps value into [low, high]. Asserts when low > high.
degrees
degreesConverts radians to degrees.
divide_round_up
divide_round_uptemplate <unsigned_integer IntegerType> [[nodiscard]] constexpr IntegerType divide_round_up(
IntegerType numerator,
std::type_identity_t<IntegerType> denominator) noexcept;ceil(numerator / denominator) for integers; asserts and returns 0 when denominator is 0.
inverse_lerp
inverse_lerpThe t for which lerp(a, b, t) == value. Returns 0 when a == b exactly; any non-zero range, however narrow, is divided through.
is_aligned
is_alignedtemplate <unsigned_integer IntegerType> [[nodiscard]] constexpr bool is_aligned(
IntegerType value,
std::type_identity_t<IntegerType> alignment) noexcept;True when value is a multiple of alignment (a power of two; asserted, false otherwise).
[[nodiscard]] inline bool is_aligned(
const void *pointer,
usize alignment) noexcept;True when the pointer address is a multiple of alignment.
is_power_of_two
is_power_of_two[[nodiscard]] constexpr bool is_power_of_two(
u64 value) noexcept;True when value is a power of two. Zero is not a power of two.
lerp
lerpLinear interpolation from a (t = 0) to b (t = 1); t is not clamped.
log2_floor
log2_floortemplate <unsigned_integer IntegerType> [[nodiscard]] constexpr u32 log2_floor(
IntegerType value) noexcept;floor(log2(value)) for value > 0; asserts and returns 0 for zero.
nearly_equal
nearly_equalTrue when |a - b| is within epsilon (absolute tolerance). Exactly equal values compare equal even when infinite (inf - inf is NaN); NaN never compares equal.
True when |a - b| is within epsilon (absolute tolerance). Exactly equal values compare equal even when infinite (inf - inf is NaN); NaN never compares equal.
nearly_zero
nearly_zeroTrue when |value| is within epsilon of zero.
next_power_of_two
next_power_of_twotemplate <unsigned_integer IntegerType> [[nodiscard]] constexpr IntegerType next_power_of_two(
IntegerType value) noexcept;The smallest power of two that is >= value; 1 for zero. Returns 0 when the result does not fit the type (value above the largest representable power of two).
previous_power_of_two
previous_power_of_twotemplate <unsigned_integer IntegerType> [[nodiscard]] constexpr IntegerType previous_power_of_two(
IntegerType value) noexcept;The largest power of two that is <= value; 0 for zero.
radians
radiansConverts degrees to radians.
remap
remap[[nodiscard]] constexpr f32 remap(
f32 value,
f32 inLow,
f32 inHigh,
f32 outLow,
f32 outHigh) noexcept;Maps value from [inLow, inHigh] onto [outLow, outHigh] without clamping.
sign
sign-1, 0 or 1 depending on the sign of value (0 for zero and NaN).
smoothstep
smoothstepHermite smooth step: 0 at or below edge0, 1 at or above edge1, smooth in between.