Skip to content

Engine → Core

math_utils.h

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::numeric_limits<f32>::infinity()
F32_MAX inline constexpr f32 std::numeric_limits<f32>::max()
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

[[nodiscard]] constexpr f32 absolute(
    f32 value) noexcept;

Absolute value usable in constant expressions.

align_down

template <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

template <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

[[nodiscard]] constexpr f32 clamp(
    f32 value,
    f32 low,
    f32 high) noexcept;

Clamps value into [low, high]. Asserts when low > high.

degrees

[[nodiscard]] constexpr f32 degrees(
    f32 angleRadians) noexcept;

Converts radians to degrees.

divide_round_up

template <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

[[nodiscard]] constexpr f32 inverse_lerp(
    f32 a,
    f32 b,
    f32 value) noexcept;

The 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

template <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

[[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

[[nodiscard]] constexpr f32 lerp(
    f32 a,
    f32 b,
    f32 t) noexcept;

Linear interpolation from a (t = 0) to b (t = 1); t is not clamped.

log2_floor

template <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

[[nodiscard]] constexpr bool nearly_equal(
    f32 a,
    f32 b,
    f32 epsilon = EPSILON) noexcept;

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.

[[nodiscard]] constexpr bool nearly_equal(
    f64 a,
    f64 b,
    f64 epsilon = EPSILON_F64) noexcept;

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

[[nodiscard]] constexpr bool nearly_zero(
    f32 value,
    f32 epsilon = EPSILON) noexcept;

True when |value| is within epsilon of zero.

next_power_of_two

template <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

template <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

[[nodiscard]] constexpr f32 radians(
    f32 angleDegrees) noexcept;

Converts degrees to radians.

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.

saturate

[[nodiscard]] constexpr f32 saturate(
    f32 value) noexcept;

Clamps value into [0, 1].

sign

[[nodiscard]] constexpr f32 sign(
    f32 value) noexcept;

-1, 0 or 1 depending on the sign of value (0 for zero and NaN).

smoothstep

[[nodiscard]] constexpr f32 smoothstep(
    f32 edge0,
    f32 edge1,
    f32 value) noexcept;

Hermite smooth step: 0 at or below edge0, 1 at or above edge1, smooth in between.

square

[[nodiscard]] constexpr f32 square(
    f32 value) noexcept;

value * value.

wrap_angle

[[nodiscard]] inline f32 wrap_angle(
    f32 angleRadians) noexcept;

Wraps an angle in radians into the half-open range [-PI, PI).