magemath

Guide · Tuning & Subnormals · Magemath API
magemath is a configurable scalar and SIMD f32 transcendental approximation crate (powf, exp2, exp, log2, ln, log10, log1p, cbrt), built on Magetypes vectors and Archmage capability proofs. #![no_std] and #![forbid(unsafe_code)] by default; enable std for runtime CPU detection and avx512 for 16-lane AVX-512 slice widening.
Two opposite configurations: 1.52 µs vs 666.5 ns
Stating your actual input domain and exponent removes subnormal rescaling, runtime exponent validation, and internal range clamps at compile time:
use ScalarToken;
use f32x8;
use SimdMath;
use ;
// 1. Broad defensive config: unranged [0, MAX], runtime exponent validation,
// * 2^23 subnormal normalization, internal y*log2(x) range check, error reporting.
// Zen 5 (Ryzen 9 9950X3D, 16-lane AVX-512 slice): 1.52 µs ± 2 ns / 4,096 f32
const DEFENSIVE: u128 = new
.domain
.admit_subnormals
.bits;
// 2. Tight domain-proven config: [0, 1] unit domain, compile-time constant 1/2.4
// (proving output range-check elision at compile time), unchecked in-bounds.
// Zen 5 (Ryzen 9 9950X3D, 16-lane AVX-512 slice): 666.5 ns ± 2 ns / 4,096 f32 (2.28x faster)
const FAST: u128 = new
.domain
.const_exponent
.assume_in_bounds
.bits;
let src: = .map.collect;
let mut wide_out = vec!;
let mut fast_out = vec!;
slice.unwrap;
slice_const.unwrap;
assert_eq!;
let chunk = from_slice_t;
assert_eq!;
Keeping Validation::Checked on [0, 1] with .const_exponent(1.0 / 2.4).reject_subnormals() runs in 827.1 ns ± 3 ns (1.84x faster while rejecting < 0, > 1, ±inf, or NaN with exact index), and .const_exponent(0.5) lowers to vsqrtps in 251.8 ns ± 1 ns (6.0x faster).
Measured whole-slice throughput (4,096 f32 values, AMD Ryzen 9 9950X3D Zen 5, single core pinned via taskset -c 8, release build without -C target-cpu, avx512 enabled; methodology and full matrix in benchmarks/magemath_slices_2026-11-17/RESULTS.md):
| Family | Broad / IEEE configuration | Time (4,096 f32) |
Tight / unchecked configuration | Time (4,096 f32) |
Speedup |
|---|---|---|---|---|---|
powf (x^(1/2.4)) |
NonNegative + admit_subnormals() |
1.52 µs ± 2 ns |
Unit + const_exponent(1.0 / 2.4) + assume_in_bounds() |
666.5 ns ± 2 ns |
2.28x |
exp2 (2^x) |
Finite (464.3 ns) / .input_range(-126.0, 127.5) (570.3 ns) |
464.3–570.3 ns |
SingleOctave + assume_in_bounds() |
181.0 ns ± 2 ns |
2.56x–3.15x |
log2 (log2(x)) |
PositiveNormals + admit_subnormals() |
575.2 ns ± 3 ns |
SingleOctave + assume_in_bounds() |
217.1 ns ± 1 ns |
2.65x |
log1p (ln(1 + x)) |
GreaterThanNegOne |
849.7 ns ± 4 ns |
AroundZero + assume_in_bounds() |
347.5 ns ± 1 ns |
2.45x |
cbrt (cbrt(x)) |
AllFloats |
1.10 µs ± 2 ns |
SignedUnit + assume_in_bounds() |
640.3 ns ± 5 ns |
1.72x |
Quick start
[]
= "=0.9.32"
= "=0.9.32"
= { = "0.1", = ["std"] }
use ;
const GAMMA: u128 = new.gamma.midp.bits;
let result = array.unwrap;
assert_eq!;
assert_eq!;
let src = ;
let mut dst = ;
slice.unwrap;
// Default positive-normal Midp convenience helpers:
let mut pos_dst = ;
try_powf_slice.unwrap;
Each family has one fluent const options builder (PowfOptions, ExpfOptions, LogfOptions, Log1pfOptions, CbrtfOptions) with discrete #[non_exhaustive] *Domain presets, explicit Subnormals policies (FlushOrClamp default, Reject via .reject_subnormals(), opt-in Normalize via .admit_subnormals()), outward-rounded bf16 custom range/clamp bounds, and (for powf) compile-time .const_exponent(n) specialization — ending in .bits() for const CFG: u128 (Powf<CFG>, Expf<CFG>, Logf<CFG>, Log1pf<CFG>, Cbrtf<CFG>, and inline vector methods via SimdMath) or .runtime() for dynamic options.
Inline SIMD kernels and matching scalar tails
Inside an #[attune(_*_t, define(f32x8))] kernel, import SimdMath to call configured operations inline on any Magetypes f32 vector (self.token() is reused automatically), and pair .caller() with *_with_token so scalar tails produce identical bits:
use *;
use SimdMath;
use ;
const GAMMA_CALLER: u128 = new.gamma.midp.caller.bits;
let src = ;
let mut dst = ;
powf_slice.unwrap;
assert_eq!;
Limitations and when to use an alternative
- Bounded approximations, not universal
< 1 ULPlibm:MidpandLowptrade full-range< 1 ULProunding for deterministic SIMD polynomials and Halley steps (PowfPrecision::Highpadds a binary64 table path orstdf32::powfdelegation forpowf). When you need< 1 ULPIEEE-754 libm behavior across arbitraryf32orf64inputs, usestd::f32/libmorsleef. f32only:f64transcendentals are not implemented here, and hardwaresqrtstays directly on Magetypes.- Flat Magetypes methods remain supported: if you only need an inline unchecked polynomial inside an existing
magetypesloop without cross-ISA bit identity,const_exponentlowering, or scalar tail helpers, the flat methods onmagetypes(pow_midp,exp2_lowp,cbrt_midp, ...) are unchanged and not deprecated. See choosing and migrating and Magetypes Fast Paths.
License
MIT OR Apache-2.0