| Crates.io | reciprocal |
| lib.rs | reciprocal |
| version | 0.1.2 |
| created_at | 2021-05-12 01:23:54.006918+00 |
| updated_at | 2021-05-28 12:25:57.420466+00 |
| description | Baseline implementation of integer division by constants |
| homepage | |
| repository | https://github.com/pkhuong/reciprocal |
| max_upload_size | |
| id | 396378 |
| size | 30,140 |
When dividing integers by compile-time constants, compilers (LLVM) can be trusted to convert those to a sequence of multiplication and shift.
That doesn't work so well when the constant isn't known until runtime.
This crate implements a simple strategy with reliable performance. Does reliable imply good? For this application, it does.
The basic PartialReciprocal should be compiled to a constant-time
fast path, and can handle every divisor except 0, 1, and u64::MAX.
The slightly more complex Reciprocal can also divide by 1 and
u64::MAX, at the expense of one more u64 field, and a slightly
more complex (one more load, maybe one more integer arithmetic
instruction) fast path.