A high-performance, no_std Rust math library aiming for glibc parity, speed, and strict accuracy (≤ 1.0 ULP).
- Glibc Parity: Implements core
libmfunctions with behavior matching the GNU C Library (NaNs, infinity, subnormals, and edge cases). - High Accuracy: Functions are verified against MPFR (Multiple Precision Floating-Point Reliably) to ensure an error of ≤ 1.0 ULP (Units in the Last Place).
- Optimized for Speed: Uses table-driven algorithms and hardware intrinsics (e.g., FMA, SSE2) where available to meet or exceed the performance of the system
libm. no_stdSupport: Designed for embedded, kernel, or other environments where the standard library is not available.- Zero External Dependencies: Self-contained implementation (does not link to the system
libm).
- Exponential:
exp,exp2,expm1,exp10 - Logarithmic:
ln/log,log2,log10,log1p - Trigonometric:
sin,cos,tan,atan,atan2,sincos - Power/Root:
pow,sqrt,cbrt,hypot
- Hyperbolic:
sinh,cosh,tanh - Inverse Hyperbolic:
asinh,acosh,atanh
- Gamma family:
lgamma,tgamma - Error functions:
erf,erfc
- Classification:
fpclassify,isfinite,isinf,isnan,signbit - Rounding:
rint,nearbyint,round,trunc,floor,ceil,lrint,llrint,lround,llround - Scaling:
frexp,ldexp,scalbn,scalbln - Min/Max/Delta:
fmin,fmax,fdim - Remainders:
fmod,remainder,remquo - Adjacency:
nextafter - Exponent access:
logb,ilogb - FMA:
fma - Decomposition:
modf
Accuracy is the primary goal of this project. Every function is tested using:
- Deterministic Tests: Checking specific edge cases and known difficult values.
- Property-based Testing: Using
proptestto verify thousands of random inputs across the entire floating-point range. - MPFR Verification: Results are compared against the "ground truth" provided by the MPFR library to guarantee ≤ 1.0 ULP accuracy.
Recent precision improvements:
acoshnear 1.0: uses a log1p-based path with compensated summation forx < 1.1192to keep errors within ≤ 1.0 ULP.
fastmaths is designed to be faster than the system glibc implementation. Benchmark results are machine- and toolchain-dependent, so run them locally for current numbers.
To run the full suite of benchmarks:
cargo benchNote: For maximum performance, compile with RUSTFLAGS="-C target-cpu=native" to enable hardware-specific optimizations like FMA and SSE2 instructions. On x86/x86_64, fastmaths assumes FMA at compile-time unless the soft-fma feature is enabled (use it for deterministic builds or legacy CPUs). For glibc comparisons, point the benches at a locally-built libm via FASTMATHS_GLIBC_LIBM or use ./build_libm.sh.
Add this to your Cargo.toml:
[dependencies]
fastmaths = "0.1.0"Example usage:
use fastmaths::exp;
let x = 2.0_f64;
let result = exp(x);
println!("e^{} = {}", x, result);Licensed under either of Apache License, Version 2.0 or MIT license at your option.