Skip to main content

Crate magemath

Crate magemath 

Source
Expand description

Configurable f32 pow, exponential, logarithm, log1p, and cube-root approximations.

Configure operations with fluent const option builders — powf::PowfOptions, expf::ExpfOptions, logf::LogfOptions, log1pf::Log1pfOptions, and cbrtf::CbrtfOptions — ending in .bits() for compile-time specialization through powf::Powf, expf::Expf, logf::Logf, log1pf::Log1pf, cbrtf::Cbrtf, or inline on Magetypes SIMD vectors via SimdMath. Dynamic runtime configurations use .runtime() on the same option value.

use archmage::ScalarToken;
use magetypes::simd::generic::f32x4;
use magemath::{SimdMath, powf::{Powf, PowfDomain, PowfOptions}};

const GAMMA: u128 = PowfOptions::new().domain(PowfDomain::HdrGamma).midp().bits();
const SRGB_DEC: u128 = PowfOptions::new()
    .domain(PowfDomain::Unit)
    .const_exponent(2.4)
    .clamp_out_of_bounds()
    .bits();

let src = [0.0, 0.25, 0.5, 1.0];
let mut dst = [0.0; 4];
Powf::<GAMMA>::slice(&src, &mut dst, 2.2).unwrap();

let v = f32x4::from_array_t(ScalarToken, src);
let checked = v.try_powf_with::<GAMMA>(2.2).unwrap();
let decoded = v.powf_const::<SRGB_DEC>();
assert_eq!(checked.to_array(), dst);
assert_eq!(decoded.to_array()[3], 1.0);

Magemath depends on Magetypes and Archmage. It does not replace vector storage or primitive arithmetic, and Magetypes does not depend on Magemath.

Modules§

bf16
Lossless const bf16 (1 sign + 8 exp + 7 mantissa) packing and expansion for custom option range/clamp bounds inside const CFG: u128.
cbrtf
Configurable f32 cube-root (cbrtf) approximations with explicit portability, precision, and domain validation.
expf
Configurable f32 exponentials (exp2, exp) with explicit portability, precision, and validation, mirroring crate::powf.
log1pf
Configurable f32 ln(1 + x) (log1p) on (-1, +∞) with explicit portability, precision, subnormal policy, and validation.
logf
Configurable f32 logarithms (log2, ln, log10) with explicit portability, precision, subnormal policy, and validation, mirroring crate::powf.
powf
Configurable f32 power with explicit portability, precision, and validation.

Structs§

SliceError
Failure of a slice operation: the reason and the first input that caused it.

Enums§

Execution
Whole-operation feature selection, independent of numerical policy.
Portability
Whether accepted results must agree bit for bit across backends.
Subnormals
How nonzero subnormal inputs (0 < |x| < f32::MIN_POSITIVE) are handled.
Validation
How out-of-bounds inputs and intermediate ranges are handled at runtime.

Traits§

SimdMath
Configurable transcendental operations inline on Magetypes f32 vectors.
SimdVector
Sealed Magetypes f32 SIMD vector (f32x4, f32x8, and with w512, f32x16) supported by Magemath’s operator structs and SimdMath.