#division #integer #u64 #port #divide #reduce #partial

no-std fastdivide

Fastdivide is a partial port of libdivide. It makes it possible to reduce the cost of divisions.

6 releases (3 breaking)

0.4.2 Nov 1, 2024
0.4.1 Apr 1, 2024
0.4.0 Jan 27, 2022
0.3.0 Feb 22, 2021
0.1.0 Jun 13, 2017

#104 in Data structures

Download history 89852/week @ 2024-08-21 77943/week @ 2024-08-28 90433/week @ 2024-09-04 79246/week @ 2024-09-11 81145/week @ 2024-09-18 85793/week @ 2024-09-25 85614/week @ 2024-10-02 77392/week @ 2024-10-09 72373/week @ 2024-10-16 66250/week @ 2024-10-23 59961/week @ 2024-10-30 68294/week @ 2024-11-06 70949/week @ 2024-11-13 64065/week @ 2024-11-20 58138/week @ 2024-11-27 56321/week @ 2024-12-04

262,050 downloads per month
Used in 132 crates (13 directly)

zlib-acknowledgement OR MIT

13KB
211 lines

Build Status

FastDivide...

FastDivide is a simple/partial RUST port of libdivide by ridiculous_fish Libdivide is distributed under the zlib license, so does fast divide.

This port is only partial : It only include one implementation of division for u64 integers.

Yes, but what is it really ?

Division is a very costly operation for your CPU. You may have noticed that when the divisor is known at compile time, your compiler transforms the operations into a cryptic combination of a multiplication and bitshift.

The key idea is that, rather than computing

N / D

It is faster to compute (with k sufficiently large)

N * ( 2^k / D ) / (2^k)

If D is known in advance, (2^k / D) can be precomputed by the compiler.

Unfortunately if the divisor is unknown at compile time, the compiler cannot use this trick.

The point of fastdivide is to apply the same trick by letting you precompute a DivideU64 object.

When is it useful ?

If you do a lot (> 10) of division with the same divisor ; and this division is a bottleneck in your program.

This is for instance useful to compute histograms.

No runtime deps