libm
A pure-Rust, no_std implementation of the C math library for float functions.
Repository Health
Technical Analysis
libm is a pure-Rust implementation of the C math library (libm). It provides portable, dependency-free implementations of floating-point math functions — trigonometry, exponentials, logarithms, roots, rounding, and more — for both f32 and f64, without linking against a system C library.
Because it is written entirely in Rust and is no_std compatible, libm is the fallback that powers Rust’s core float math on targets that lack a platform libm, and it can be used directly in embedded and bare-metal projects that need math routines where the standard library is unavailable.
What You Get
- Pure-Rust implementations of the full C math function set for f32 and f64
- A no_std, dependency-free crate suitable for embedded and bare-metal targets
- The fallback math implementations used by Rust’s core float methods
- Optional architecture-specific features for SIMD and assembly-accelerated routines
- A stable, plain-function API mirroring familiar libm names like sinf and pow
Common Use Cases
- Providing math functions in no_std and embedded Rust projects
- Running float math on targets without a platform-provided libm
- Ensuring bit-reproducible math results independent of the system C library
- Backing Rust core float methods where software implementations are required
Under The Hood
Architecture — libm is one crate within the rust-lang/compiler-builtins Cargo workspace. Its public surface (libm/src/lib.rs, libm/src/libm_helper.rs) re-exports a large module of individual function implementations under libm/src/math, where each routine lives in its own file (for example sin.rs, acos.rs, pow.rs) with paired f32/f64 variants. Architecture-specific optimizations and approximation helpers are isolated in math/arch and math/approx, gated behind features so the crate stays portable by default. A build.rs and configure.rs handle target and feature detection at compile time.
Tech Stack — Written in Rust (2021 edition, MSRV 1.63/1.67), fully no_std with no runtime dependencies. Optional nightly-gated features (unstable, unstable-intrinsics, unstable-float) enable core::intrinsics calls and f16/f128 support. Development relies on no-panic to assert routines never panic, plus an extensive companion libm-test crate for accuracy checks.
Code Quality — The code is organized one function per file with rigorous testing: the sibling libm-test crate validates results against reference values and checks accuracy bounds, and no-panic guarantees are enforced. As part of the rust-lang organization the project is heavily reviewed, actively maintained, and held to a high correctness bar.
API Design — The API is deliberately flat and familiar: free functions named after their C counterparts, so anyone who has used libm in C can call libm::sqrt or libm::sinf immediately with no setup. There is no state, no builder, and no initialization; adding the crate and calling functions is all that is required, which makes it trivial to drop into constrained environments.