Crate fast_posit

Crate fast_posit 

Source
Expand description

This crate provides a correct, clean, flexible, and 🚀 fast software implementation of Posit arithmetic.

§Introduction

Posits are an alternative floating point format proposed by John Gustafson in 2017, with the first published standard in 2022. They have several interesting features that make them an excellent replacement for traditional IEEE754 floats, in domains such as neural networks or HPC.

The following references are useful if you are not yet familiar with posits:

This crate aims to implement the entire posit standard and beyond, including features such as arbitrary posit and quire sizes beyond those prescribed by the standard. Versions prior to 1.0.0, however, may be incomplete. Correctness is ensured via extensive testing against an oracle.

§Usage

// Use standard posit types, or define your own.
use fast_posit::{p8, p16, p32, p64};  // Standard: n bits, 2 exponent bits
type MyPosit = Posit<24, 3, i32>;  // Non-standard: 24 bits, 3 exponent bits

// Create posits from ints, IEEE floats, strings, constants, or a raw bit representation.
let a = p32::round_from(2.71_f64);
let b = p32::round_from(42_i32);
let c = p32::from_bits(0x7f001337);
let d = p32::MIN_POSITIVE;

// Perform basic arithmetic and comparisons with the usual operators.
assert!(p16::round_from(2.14_f32) + p16::ONE == 3.14_f32.round_into());
assert!(p16::MIN_POSITIVE < 1e-15_f32.round_into());
// # use core::f32
// assert!(p16::from  p16::round_from(f32::consts::PI));

// Convert posits back to ints, IEEE floats, strings, or a raw bit representation.
assert_eq!(p8::ONE.to_bits(), 0b01000000)

§Performance

In terms of performance, you can expect as a very rough estimate 50 to 80 Mops/s (corresponding to about a 10–20× slowdown relative to native hw FPU operations) on an 11th gen Intel x86 core at 2.80GHz. This is, as far as we’re aware, faster (or at least as fast) as any freely available software implementation of posit arithmetic.

Needless to say, both absolute performance and relative performance vs the FPU varies depending on your system.

This crate includes benchmarks; run them with cargo bench -F bench.

Structs§

Posit
A Posit floating point number with N bits and ES exponent bits, using Int as its underlying type.

Traits§

Int
The trait for the underlying machine integer types that can be used to represent a posit (only satisfied by i8, i16, i32, i64, and i128).
RoundFrom
Used to do value-to-value conversions that consume the input and round according to the posit standard. It is the reciprocal of RoundInto.
RoundInto
Used to do value-to-value conversions that consume the input and round according to the posit standard. It is the reciprocal of RoundFrom.

Type Aliases§

p8
Standard-defined 8-bit posit (with 2-bit exponent).
p16
Standard-defined 16-bit posit (with 2-bit exponent).
p32
Standard-defined 32-bit posit (with 2-bit exponent).
p64
Standard-defined 64-bit posit (with 2-bit exponent).