Skip to content

Software implementation of half-precision floating point numbers according to IEEE 754.

License

Notifications You must be signed in to change notification settings

jakobj/psoftfloat-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 

Repository files navigation

psoftfloat-rs

Software implementation of half-precision floating point numbers according to IEEE 754. Not all operations are supported. Only round to nearest, ties to even is implemented. Note that this implementation does not generate floating-point exception flags for underflow etc.

WARNING

This implementation is for educational purposes only. It is explicitly not optimized for speed, but readability.

Usage

use soft_float::SoftFloat16;

fn main() {
    let x = SoftFloat16::from_bits(0x4198); // 2.796875
    let y = SoftFloat16::from_bits(0xb180); // -0.171875
    let z = x * y;

    assert!(SoftFloat16::to_bits(z) == 0xb7b1);

    println!("{} * {} = {}", f32::from(x), f32::from(y), f32::from(z)); // print via `f32`
}

Testing

You can execute a couple of special test cases via

$ cd soft_float
$ cargo test

This is particularly helpful during development, but doesn't exactly proof correctness of the implementation.

For half-precision floats, we can actually test them all. By default, these exhaustive tests are ignored, but you can execute them via

$ cd soft_float
$ cargo test -- --ignored

Furthermore, we use the Berkeley TestFloat programs to verify the implementation. Exception flags are merely copied over from the testfloat_gen output. You can execute the tests by

$ cd tests
$ make

Resources

About

Software implementation of half-precision floating point numbers according to IEEE 754.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published