softfpu
softfpu implements 32-, 64- and 80-bit floating-point arithmetic in safe Rust.
Addition, subtraction, multiplication, division and square root are correctly
rounded and report exception flags. The library uses only core and requires no
allocation or global state.
Intel x87 transcendentals. softfpu computes the results of all eight x87
transcendental instructions. Its result bits and status flags matched Intel
hardware on every operand tested, even when Intel's fsin differed from the
correctly rounded sine in the last bit.
Hardware evidence
Arithmetic throughput. In benchmarks of 64- and 80-bit addition, multiplication, division and square root, softfpu matched or beat Berkeley SoftFloat 3e in every native comparison and was faster in every wasm32 comparison under V8. For 80-bit multiplication, softfpu delivered 1.57–2.98× Berkeley's throughput natively and 1.11–2.37× in wasm32. Benchmarks
Basic arithmetic
Add the crate with cargo add softfpu. The
API documentation covers operand requirements, result
flags and examples.
use ;
let one = from_i32;
let ten = from_i32;
let quotient = divide;
assert_eq!;
assert_eq!;
math works with both Single and Double and rounds to nearest, ties to
even. The lower-level API lets you choose the rounding mode, tininess rule, and
NaN policy.
Features
- Bit-pattern formats.
Single,DoubleandExtendedstore raw encodings, not hostf32orf64values. They preserve NaN payloads, signed zeros and noncanonical 80-bit encodings: unnormals, pseudo-NaNs, pseudo-infinities, pseudo-denormals and MMX-aliased bit patterns. - Computation separated from rounding. A core operation returns either a finished value with flags or an intermediate the caller can round. The caller can round the same intermediate for masked and unmasked x87 exceptions.
- Complete rounding metadata. A
Roundedresult reports its value and its exception flags. It also reports whether rounding increased the magnitude, which the x87 status word needs forC1, and whether the result was tiny under the selectedTininessrule. - NaN policies. Core arithmetic methods take a
nan::NanPolicytype parameter. The policy supplies the default NaN, quiets signalling NaNs and selects the result when one or both operands are NaNs. - The
x86layer.- x86 NaN selection. SSE operations on 32 and 64 bits prefer the first operand. x87 operations on 80 bits choose by signalling state, significand and sign.
- Integer-indefinite results for failed conversions.
- x87 precision control at 24, 53 and 64 bits over the 15-bit extended exponent.
- The values of
fscale,fxtract,fprem,fprem1and the packed BCD instructions, including the progress of a partial remainder. - The x87 constants of
fld1,fldl2t,fldl2e,fldpi,fldlg2,fldln2andfldz, rounded under the rounding control. - x87 transcendentals:
sin(fsin),cos(fcos),sin_cos(fsincos),tan(fptan),atan2(fpatan),exp2m1(f2xm1),y_log2(fyl2x) andy_log2p1(fyl2xp1). - The SSE
rcpssandrsqrtssapproximations.
Usage
Masked and unmasked x87 rounding
Unrounded is Copy. A caller can inspect the masked result and, when an
overflow or underflow exception is unmasked, round the same intermediate again
with the x87 exponent adjustment.
use Computed;
use ;
let min_normal = Extended ;
let half = Extended ;
let Unfinished = multiply else ;
// Half the smallest normal number is an exact denormal. Masked rounding
// raises no underflow.
let masked = round;
assert!;
assert_eq!;
// With underflow unmasked, the result is normal and its exponent is adjusted
// by 24,576.
let unmasked = round_unmasked;
assert!;
assert_eq!;
x87 transcendentals
use ;
let one = from_i64;
// For a finite x with |x| >= 2^63, cos returns None. The instruction sets C2
// and preserves ST(0) in that case.
let computed = cos.expect;
let cos = computed.settle;
assert!;
The x87 transcendentals
The x87 algorithms were reconstructed from the Intel SDM, Intel's 2015 paper on trigonometric range reduction, Ken Shirriff's reading of the Pentium's constant ROM from die photographs, and measurements on hardware. Targeted comparisons distinguished many intermediate rounding steps, and several steps remain assumptions. Hardware evidence
The reconstruction reproduces Intel's quirks along with its values. For example,
Intel's f2xm1(+1) returns exactly 1 and still raises the precision exception
(PE), and so does softfpu's.
Implementation
- Design covers intermediate precision, cold returns, inlining attributes and NaN policies.
- Rounding derives the rounding decision, tininess and range responses.
- Addition and subtraction covers alignment, cancellation and sticky bits.
- Multiplication derives the product widths for each format.
- Division derives the quotient and remainder bounds for all three kernels, and covers the x87 partial remainder.
- Square root covers the seed table, refinement and final residual.
- Comparison and conversions covers numeric ordering, format changes and rounding at the units position.
- x87 transcendental functions covers the internal precision, range reduction, polynomial schedules and hardware evidence.
- SSE reciprocal approximations covers table lookup, special operands and the relative-error bound.
- Performance compares throughput with Berkeley SoftFloat 3e, natively and in wasm32.
Verification
| Command | Checks |
|---|---|
cargo test --doc |
Public API examples |
cargo test |
Crate tests, the integer rounding model, external NaN-policy tests, and a replay of 45,610 x87 transcendental results recorded on Intel hardware |
cargo test --release --test testfloat -- --ignored |
Generated cases against Berkeley TestFloat, with the reference library following x86's NaN rules. Requires testfloat_gen on PATH. |
tests/README.md explains how to build testfloat_gen and
describes the recorded results.
TestFloat checks values and exception flags under the masked response. The
crate's model tests check rounded_up, tiny and the unmasked responses
against exact integer arithmetic.
The schedules were first reconstructed in C. That version matched one Intel
processor across 13.93 billion executions, with exceptions masked and with
random exception masks. A second Intel processor, of a different core design,
reproduced the recorded observations. The Rust kernels matched the
reconstruction on worked examples, recorded observations and four million seeded
cases per instruction. cargo test replays the 45,610 recorded observations
that have every exception masked and valid operands.
Hardware evidence
License
softfpu is released under the MIT license. See LICENSE.