Hi! I've been following the development of posit arithmetic on and off for the past 2 years or so. A couple months ago I decided to try my hand at writing a software implementation. I believe it might be an interesting contribution to the posit ecosystem, for a few different reasons.
`
fast-posit` is a software implementation of posit arithmetic, written in Rust (bindings to C are coming soon). You can find the source code and README at
https://github.com/andrepd/posit-rust. It has three goals in mind:
- Correctness: all functions are correct as defined in the standard (i.e. they give the correct results, for all inputs, with the correct rounding).
- Performance: this library aims to be faster than, or at least as fast as, any freely available software implementation of posits.
- Readability: notwithstanding a fast implementation being quite byzantine and difficult to understand at first glance, the code tries to be well structured and _extensively_ documented.
I believe it achieves all three. First of all, its correctness is extensively tested via checking against an oracle (which uses arbitrary precision arithmetic to calculate the correct unrounded result), exhaustively where possible, and probabilistically where we cannot enumerate all inputs. Furthermore, there is only
one single implementation for each function, parametrised by the values of N and ES, rather than a separate one for each N; this gives us more confidence that the exhaustive test coverage for the smaller sizes transfers to the instantiations for the larger sizes.
Also, it's very fast! Comparing it to Cerlane Leong's SoftPosit (the most mature software impl as I understand it), it's
~2–10 times faster, depending on the operation! Some of the tricks I used to make it go fast may or may not be already known; I confess I did not do much prior lit review besides reading some of the code for Cerlane's library. Benchmarks are included and are easy to run, so feel free to give it a test on your machines.
Finally, it's easy to use (the README includes example code that showcases its usage, please do give it a read), and it's well documented, both the user-facing API as well as the implementation details. The user-facing API documentation is live here:
https://docs.rs/fast_posit.
While still being very fast, I believe the implementations are still reasonably understandable for anyone who is interested. I took care to document all the non-trivial algorithms (decode a posit, add two posits, round quire to posit, etc.) in great detail and, as mentioned above, there is only one implementation for each function, which is parametrised at compile-time on the size N and exponent size ES of the posit type. See e.g.
decode.rs. My hope is that if you are interested in learning more about posits, or about software implementation of floating point formats in general, you might benefit from reading through this code!
-----
This library allows the user to define posit types with arbitrary N and ES (up to 128); the standard p8, p16, p32, p64 are already defined. Rust allows operator overloading, so you can write code like `a + b.ceil() < -c`, just as you would with native types. Conversions to/from signed ints and IEEE floats are included. Conversions between posit types are too, duly optimised when the ES are the same (as is the case with the standard posits with ES=2). Quire arithmetic is there, but I have ideas on how to optimise it (the current implementation is quite ugly and kinda slow).
It is not yet feature complete as of v0.1.3: the main thing missing are all the elementary functions (apart from + - × ÷). There's a checklist on the bottom of the readme detailing what exactly is left to be done.
Anyways, thanks for reading, and any feedback would be greatly appreciated! I'd be happy to answer any questions you might have.
For those who aren't familiar with Rust, don't be discouraged! There's a "tour" of the main features in the readme, and I'm confident that someone who hasn't read a line of Rust can still pretty much understand everything. C bindings are in the to-do list, so you will be able to call into this fast implementation from C, C++, or any language that can do ffi into a C API.