alice-det-math
Cross-platform bit-exact f32 / f64 transcendentals — sin, cos, exp,
ln, atan2, asin, acos, tan, tanh, powf, cbrt, hypot, … — built
from IEEE 754 basic operations only, so every function is a pure function of
its input bits on x86_64, aarch64, wasm32 and every other IEEE target.
no_std, no alloc, no unsafe.
The platform libm is not: sin(x) differs in the last ulp between macOS,
glibc, MSVC and wasm. A lockstep simulation that calls it diverges across
peers; a signed-distance field evaluated on two machines disagrees about which
side of a surface a point is on. This crate is the transcendental layer of
alice-physics (128-bit deterministic
physics) and alice-sdf (SDF geometry),
extracted so both — and anything else — evaluate the same law.
Features
- Scalar
f32kernels (Cephessinf/cosf/expf, musllogf) andf64kernels (fdlibmatan/atan2/asin/acos/exp/log), ≤ 1–2 ulp of correctly rounded over the documented domains - SIMD (
simdfeature):wide::f32x8versions ofsin/cos/sin_cos/exp/ln/round/sqrt, lane-for-lane bit-identical to the scalar functions — same constants, same operation order, nomul_add round/sqrtwithout libm: musl'sx + 2^23 − 2^23rounding and a correctly rounded software square root, so the crate works on bare-metal targets and returns the same bits there- Canonical NaN: a NaN input never propagates through arithmetic (whose payload / sign rules are platform-specific); every function returns the canonical NaN or passes the input bits through unchanged
- Pinned:
tests/golden.rsholds a SHA-256 of every function over a fixed input grid; CI reproduces it on macOS ARM / Intel, Linux x86 / ARM, Windows, wasm32 (wasmtime), with the SSE2-only SIMD fallback, with AVX2 + FMA forced on, and withno_std(softwaresqrt)
Quick start
[]
= "0.1"
# alice-det-math = { version = "0.1", features = ["simd"] }
# alice-det-math = { version = "0.1", default-features = false } # no_std
use ;
let = sin_cos;
assert_eq!; // the same bits on every platform
assert_eq!;
assert_eq!;
let a = atan2; // π, fdlibm special cases
assert_eq!;
With simd:
use simd;
use f32x8;
let x = new;
let = sin_cos;
for in x.to_array.into_iter.enumerate
Policy for consumers
Add the platform libm methods to clippy.toml disallowed-methods and run
clippy with -D warnings, so a stray x.sin() on a float is a red gate:
= [
{ = "f32::sin", = "platform libm; use alice_det_math::sin" },
{ = "f32::cos", = "platform libm; use alice_det_math::cos" },
{ = "f32::exp", = "platform libm; use alice_det_math::exp" },
{ = "f32::ln", = "platform libm; use alice_det_math::ln" },
{ = "f32::atan2", = "platform libm; use alice_det_math::atan2" },
{ = "f32::mul_add", = "fused on FMA targets only; write a * b + c" },
]
Accuracy and guarantee scope
See the crate documentation (cargo doc --open) for the per-function error
table and the exact scope of the bit-exactness guarantee (IEEE 754 basic
operations on the target; x87 without SSE2 and fast-math builds are outside it).
License
MIT OR Apache-2.0. Coefficients and algorithms are from Cephes (Stephen L. Moshier), fdlibm (Sun Microsystems) and musl; see the source for attribution.