1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
/*!
# Yes, but what is it really ?
Division is a very costly operation for your CPU (probably between 10
and 40 cycles).
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.
Fastdivide is about doing the same trick your compiler uses but
when the divisor is unknown at compile time.
Of course, it requires preprocessing a datastructure that is
specific to your divisor, and using it only makes sense if
this preprocessing is amortized by a high number of division (with the
same divisor).
# When is it useful ?
You should probably use `fastdivide`, if you do a lot (> 10) of division with the same divisor ;
and these divisions are a bottleneck in your program.
This is for instance useful to compute histograms.
# Example
```rust
use fastdivide::DividerU64;
fn histogram(vals: &[u64], min: u64, interval: u64, output: &mut [usize]) {
// Preprocessing a datastructure dedicated
// to dividing `u64` by `interval`.
//
// This preprocessing is not cheap.
let divide = DividerU64::divide_by(interval);
// We reuse the same `Divider` for all of the
// values in vals.
for &val in vals {
if val < min {
continue;
}
let bucket_id = divide.divide(val - min) as usize;
if bucket_id < output.len() {
output[bucket_id as usize] += 1;
}
}
}
# let mut output = vec![0; 3];
# histogram(&[0u64, 1u64, 4u64, 36u64, 2u64, 1u64], 1u64, 3u64, &mut output[..]);
# assert_eq!(output[0], 3);
# assert_eq!(output[1], 1);
# assert_eq!(output[2], 0);
```
*/
#![no_std]
#[cfg(feature = "std")]
extern crate std;
#[cfg(test)]
#[macro_use]
extern crate std;
// ported from libdivide.h by ridiculous_fish
//
// This file is not the original library, it is an attempt to port part
// of it to rust.
// This algorithm is described in https://ridiculousfish.com/blog/posts/labor-of-division-episode-i.html
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DividerU64 {
Fast { magic: u64, shift: u8 },
BitShift(u8),
General { magic_low: u64, shift: u8 },
}
#[inline(always)]
fn libdivide_mullhi_u64(x: u64, y: u64) -> u64 {
let xl = x as u128;
let yl = y as u128;
((xl * yl) >> 64) as u64
}
#[inline(always)]
fn floor_log2(n: u64) -> u8 {
assert_ne!(n, 0);
63u8 - (n.leading_zeros() as u8)
}
impl DividerU64 {
fn power_of_2_division(divisor: u64) -> Option<DividerU64> {
if divisor == 0 {
return None;
}
if !divisor.is_power_of_two() {
return None;
}
// Divisor is a power of 2. We can just do a bit shift.
Some(DividerU64::BitShift(floor_log2(divisor)))
}
fn fast_path(divisor: u64) -> Option<DividerU64> {
if divisor.is_power_of_two() {
return None;
}
let floor_log_2_d: u8 = floor_log2(divisor);
let u = 1u128 << (floor_log_2_d + 64);
let proposed_magic_number: u128 = u / divisor as u128;
let reminder: u64 = (u - proposed_magic_number * (divisor as u128)) as u64;
assert!(reminder > 0 && reminder < divisor);
let e: u64 = divisor - reminder;
// This is a sufficient condition for our 64-bits magic number
// condition to work as described in
// See https://ridiculousfish.com/blog/posts/labor-of-division-episode-i.html
if e >= (1u64 << floor_log_2_d) {
return None;
}
Some(DividerU64::Fast {
magic: (proposed_magic_number as u64) + 1u64,
shift: floor_log_2_d,
})
}
fn general_path(divisor: u64) -> DividerU64 {
// p=⌈log2d⌉
let p: u8 = 64u8 - (divisor.leading_zeros() as u8);
// m=⌈2^{64+p} / d⌉. This is a 33 bit number, so keep only the low 32 bits.
// we do a little dance to avoid the overflow if p = 64.
let e = 1u128 << (63 + p);
let m = 2 + (e + (e - divisor as u128)) / divisor as u128;
DividerU64::General {
magic_low: m as u64,
shift: p - 1,
}
}
pub fn divide_by(divisor: u64) -> DividerU64 {
assert!(divisor > 0u64);
Self::power_of_2_division(divisor)
.or_else(|| DividerU64::fast_path(divisor))
.unwrap_or_else(|| DividerU64::general_path(divisor))
}
#[inline(always)]
pub fn divide(&self, n: u64) -> u64 {
match *self {
DividerU64::Fast { magic, shift } => {
// The divisor has a magic number that is lower than 32 bits.
// We get away with a multiplication and a bit-shift.
libdivide_mullhi_u64(magic, n) >> shift
}
DividerU64::BitShift(d) => n >> d,
DividerU64::General { magic_low, shift } => {
// magic only contains the low 64 bits of our actual magic number which actually has a 65 bits.
// The following dance computes n * (magic + 2^64) >> shift
let q = libdivide_mullhi_u64(magic_low, n);
let t = ((n - q) >> 1).wrapping_add(q);
t >> shift
}
}
}
}
impl core::ops::Div<DividerU64> for u64 {
type Output = u64;
#[inline(always)]
fn div(self, denom: DividerU64) -> Self::Output {
denom.divide(self)
}
}
#[cfg(test)]
mod tests {
use super::DividerU64;
use proptest::prelude::*;
#[test]
fn test_divide_op() {
let divider = DividerU64::divide_by(2);
let res = 4u64 / divider;
assert_eq!(res, 2);
let res = 8u64 / divider;
assert_eq!(res, 4);
}
#[test]
fn test_divide_by_4() {
let divider = DividerU64::divide_by(4);
assert!(matches!(divider, DividerU64::BitShift(2)));
}
#[test]
fn test_divide_by_7() {
let divider = DividerU64::divide_by(7);
assert!(matches!(divider, DividerU64::General { .. }));
}
#[test]
fn test_divide_by_11() {
let divider = DividerU64::divide_by(11);
assert_eq!(
divider,
DividerU64::Fast {
magic: 13415813871788764812,
shift: 3
}
);
}
#[test]
fn test_floor_log2() {
for i in [1, 2, 3, 4, 10, 15, 16, 31, 32, 33, u64::MAX] {
let log_i = super::floor_log2(i);
let lower_bound = 1 << log_i;
let upper_bound = lower_bound - 1 + lower_bound;
assert!(lower_bound <= i);
assert!(upper_bound >= i);
}
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(100000))]
#[test]
fn test_proptest(n in 0..u64::MAX, d in 1..u64::MAX) {
let divider = DividerU64::divide_by(d);
let quotient = divider.divide(n);
assert_eq!(quotient, n / d);
}
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(100000))]
#[test]
fn test_proptest_divide_by_7(n in 0..u64::MAX) {
let divider = DividerU64::divide_by(7);
let quotient = divider.divide(n);
assert_eq!(quotient, n / 7);
}
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(10000))]
#[test]
fn test_proptest_divide_by_11(n in 0..u64::MAX) {
let divider = DividerU64::divide_by(11);
let quotient = divider.divide(n);
assert_eq!(quotient, n / 11);
}
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(10000))]
#[test]
fn test_proptest_divide_by_any(d in 1..u64::MAX) {
DividerU64::divide_by(d);
}
}
#[test]
fn test_libdivide() {
for d in (1u64..100u64)
.chain(vec![2048, 234234131223u64].into_iter())
.chain((5..63).map(|i| 1 << i))
{
let divider = DividerU64::divide_by(d);
for i in (0u64..10_000).chain(vec![2048, 234234131223u64, 1 << 43, 1 << 43 + 1]) {
assert_eq!(divider.divide(i), i / d);
}
}
}
}