$ show rust-lang/compiler-builtins
This repository provides two core Rust libraries: `compiler-builtins` and `libm`. `compiler-builtins` is a pure-Rust port of LLVM's `compiler-rt` intrinsics, including integer arithmetic (division, multiplication, leading/trailing zeros), floating-point operations (add, sub, mul, div, pow, conversion, truncation, extension), memory operations (memcpy, memset, memcmp), and platform-specific atomics. It is used by the Rust compiler to link against required runtime symbols for bare-metal and embedded targets. `libm` is a pure-Rust implementation of the C standard math library, ported from musl, covering trigonometric, exponential, logarithmic, power, Bessel, error, gamma, and rounding functions for f32 and f64, with experimental f16/f128 support. Both crates are `#![no_std]` and rely only on `core`. The codebase exhibits strong engineering: extensive use of generic traits (Float, Int) to abstract over widths, macro-driven code generation (intrinsics!, division algorithm macros, benchmark macros), architecture-specific optimizations (x86 SSE/AVX, ARM NEON, AArch64 outline atomics, Wasm, LoongArch, Hexagon assembly), and rigorous correctness verification against MPFR (via rug) and musl. CI tests run across 20+ targets, including instruction-count and wall-time benchmarks. This code is foundational for the Rust ecosystem, particularly in embedded, WebAssembly, and systems programming.
The LLM identified 5 files worth highlighting for reuse:
Implements software floating-point division for f32, f64, and f128 using a Newton-Raphson iterative reciprocal refinement. The algorithm uses a half-width optimization (computing initial estimate with a narrower type to reduce costly widening multiplications) and encodes precise iteration counts and error bounds for each float width. The implementation is generic over the Float trait and can be extracted for other Newton-based operations (sqrt, invsqrt). It includes robust handling of subnormals, infinities, NaN, and zero, and compiles to ABI-aligned intrinsics like __divsf3.
Provides a macro `impl_binary_long!` that implements unsigned integer division using binary long division. The file documents multiple algorithmic variants (restoring, non-restoring, SWAR) with extensive comments on performance tradeoffs for different architectures (predicated vs branchy). The macro is parameterized over integer size, normalization shift function, and zero-divisor handler, making it reusable for any unsigned width. It is the foundation for u32/u64/u128 division implementations in the crate.
Implements parsing and formatting of IEEE 754 hexadecimal float literals (like %a in C) for exact representation of floating-point values. It supports multiple round-trip modes and edge cases (subnormals, infinities, NaN, overflow, underflow). This module is essential for the test infrastructure: test cases are stored as hex floats to allow precise reproduction and comparison across platforms. It is standalone and can be reused in any numeric testing framework.
Defines a test case generator (`BitPattern`) that produces inputs by iterating over biased bit patterns covering sign, exponent, and significand spaces. It focuses on boundary conditions (zero, subnormals, normals, infinities, NaN) and enumerates many exponent/significand combinations. This ensures thorough coverage of float edge cases without exhaustive search. The generator is parameterizable by float type and can be adapted for any IEEE float format (f16, f32, f64, f128).
Provides the `for_each_function!` proc macro that iterates over all defined math operations (from `api-list-common`) and expands to per-function code. This macro is used throughout the test and benchmark infrastructure to generate test harnesses, enums, and comparison code without manual boilerplate. It demonstrates a clean pattern for maintaining a central function registry and auto-generating repetitive validation code. The macro accepts custom expansion callbacks, making it flexible for different use cases (tests, benchmarks, API documentation).