devela/num/int/divisor/
mod.rs

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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// devela::num::int::divisor

#[allow(unused_imports)]
use crate::{
    _core::{fmt, hash, ops},
    compile, iif, isize_up, paste, usize_up,
};

/// Faster divisor for division and modulo operations.
///
/// # Features
/// It's implemented for the integer primitives enabled by the corresponding
/// [capability features][crate:_info::features#capability-features]:
/// [`_int_i8`][Self#impl-Divisor<i8>],
/// [`_int_i16`][Self#impl-Divisor<i16>],
/// [`_int_i32`][Self#impl-Divisor<i32>],
/// [`_int_i64`][Self#impl-Divisor<i64>],
/// [`_int_i128`][Self#impl-Divisor<i128>],
/// [`_int_isize`][Self#impl-Divisor<isize>],
/// [`_int_u8`][Self#impl-Divisor<u8>],
/// [`_int_u16`][Self#impl-Divisor<u16>],
/// [`_int_u32`][Self#impl-Divisor<u32>],
/// [`_int_u64`][Self#impl-Divisor<u64>],
/// [`_int_u128`][Self#impl-Divisor<u128>],
/// [`_int_usize`][Self#impl-Divisor<usize>].
///
/// # Derived Work
#[doc = include_str!("./MODIFICATIONS.md")]
#[must_use]
#[derive(Clone, Copy)]
pub struct Divisor<T> {
    inner: DivisorInner<T>,
}

/// Inner representation of [`Divisor`].
#[derive(Clone, Copy)]
enum DivisorInner<T> {
    Shift(T, u8),
    MultiplyShift(T, T, u8),
    MultiplyAddShift(T, T, u8),
    /// *Variant only used for signed numbers.*
    #[allow(dead_code)]
    ShiftAndNegate(T, u8),
    /// *Variant only used for signed numbers.*
    #[allow(dead_code)]
    MultiplyAddShiftNegate(T, T, u8),
}

/// Implements [`Divisor`]`<T>` for each enabled integer primitive.
macro_rules! impl_divisor {
    () => {
        impl_divisor![signed
            i8|u8|i16|u16:Y:"_int_i8", i16|u16|i32|u32:Y:"_int_i16", i32|u32|i64|u64:Y:"_int_i32",
            i64|u64|i128|u128:PW:"_int_i64", i128|u128|i128|u128:N:"_int_i128",
            isize|usize|isize_up|usize_up:Y:"_int_isize"
        ];
        impl_divisor![unsigned
            u8|u16:Y:"_int_u8", u16|u32:Y:"_int_u16", u32|u64:Y:"_int_u32",
            u64|u128:PW:"_int_u64", u128|u128:N:"_int_u128", usize|usize_up:Y:"_int_usize"
        ];
    };

    (
    // (un)signed entry arms
    //
    // # Arguments:
    // $t:     the type. E.g. i8.
    // $un:    the unsigned type of the same size. E.g. u8. (only for signed)
    // $up:    the upcasted type. E.g. i16.
    // $unup:  the unsigned upcasted type. E.g. u16. (only for signed)
    // $is_up: upcasted behavior. Y:upcasted | N:not upcasted | PW depends on pointer width == 64
    // $cap:   the capability feature that enables the current implementation.
     signed $( $t:ty | $un:ty | $up:ty | $unup:ty : $is_up:ident : $cap:literal),+) => {
        $( impl_divisor![@signed $t|$un|$up|$unup:$is_up:$cap]; )+
    };
    (unsigned $( $t:ty | $up:ty : $is_up:ident : $cap:literal),+) => {
        $( impl_divisor![@unsigned $t|$up:$is_up:$cap]; )+
    };
    (
    /* inner arms */
     @signed $t:ty | $un:ty | $up:ty | $unup:ty : $is_up:ident : $cap:literal) => {
        #[cfg(feature = $cap )]
        impl_divisor![@traits $t];

        #[cfg(feature = $cap )]
        #[doc = crate::doc_availability!(feature = $cap)]
        // #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $cap)))]
        impl Divisor<$t> {
            impl_divisor![@shared $t|$un|$up|$unup:$is_up:$cap]; // shared methods

            /// Returns the absolute value of the signed primitive as its unsigned equivalent.
            #[must_use]
            const fn abs(n: $t) -> $un {
                iif![n < 0; ((-1i8) as $un).wrapping_mul(n as $un); n as $un]
            }

            /// Creates a divisor which can be used for faster computation
            /// of division and modulo by `d`.
            ///
            /// Returns `None` if `d` equals zero.
            ///
            /// # Examples
            /// ```
            /// # use devela::Divisor;
            #[doc = concat!["let d = Divisor::<", stringify![$t], ">::new(-21).unwrap();"]]
            /// ```
            #[must_use]
            pub const fn new(d: $t) -> Option<Divisor<$t>> {
                if d == 0 {
                    Self::cold_0_divisor()
                } else {
                    let ud = Self::abs(d);
                    let shift = ud.ilog2() as u8;
                    let inner = if ud.is_power_of_two() {
                        iif![d > 0; DivisorInner::Shift(d, shift); DivisorInner::ShiftAndNegate(d, shift)]
                    } else {
                        let (mut magic, rem) = Self::div_rem_wide_by_base(1 << (shift - 1), ud);
                        let e = ud - rem;
                        if e < 1 << shift {
                            DivisorInner::MultiplyShift(d, d.signum() * (magic as $t + 1), shift - 1)
                        } else {
                            magic *= 2;
                            let (doubled_rem, overflowed) = rem.overflowing_mul(2);
                            iif![doubled_rem >= ud || overflowed; magic += 1];
                            magic += 1;
                            if d > 0 {
                                DivisorInner::MultiplyAddShift(d, magic as $t, shift)
                            } else {
                                DivisorInner::MultiplyAddShiftNegate(d, -(magic as $t), shift)
                            }
                        }
                    };

                    Some(Self { inner })
                }
            }

            /// Returns the value that was used to construct this divisor as a primitive type.
            ///
            /// # Examples
            /// ```
            /// # use devela::Divisor;
            #[doc = concat!["let d = Divisor::<", stringify![$t], ">::new(-15).unwrap();"]]
            /// assert_eq!(d.get(), -15);
            /// ```
            #[must_use]
            pub const fn get(&self) -> $t {
                match self.inner {
                    DivisorInner::Shift(d, _) => d,
                    DivisorInner::ShiftAndNegate(d, _) => d,
                    DivisorInner::MultiplyShift(d, _, _) => d,
                    DivisorInner::MultiplyAddShift(d, _, _) => d,
                    DivisorInner::MultiplyAddShiftNegate(d, _, _) => d,
                }
            }

            /// Returns `true` if `n` is divisible by `self`.
            ///
            /// We take `0` to be divisible by all non-zero numbers.
            ///
            /// # Examples
            /// ```
            /// # use devela::Divisor;
            #[doc = concat!["let d = Divisor::<", stringify![$t], ">::new(-9).unwrap();"]]
            /// assert!(d.divides(27));
            /// ```
            #[must_use]
            pub const fn divides(&self, n: $t) -> bool {
                self.rem_of(n) == 0
            }

            /// Returns the remainder of dividing `n` by `self`.
            ///
            /// # Examples
            /// ```
            /// # use devela::Divisor;
            #[doc = concat!["let d = Divisor::<", stringify![$t], ">::new(21).unwrap();"]]
            /// let rem = d.rem_of(-30);
            /// assert_eq!(rem, -9);
            /// ```
            #[must_use]
            pub const fn rem_of(&self, n: $t) -> $t {
                n.wrapping_add((self.get().wrapping_mul(self.div_of(n))).wrapping_mul(-1))
            }

            /// Returns the result of dividing `n` by `self`.
            ///
            /// This will perform a wrapping division, i.e.
            #[doc = concat!("`Divisor::<", stringify!($t), ">::new(-1).unwrap().div_of(",
                stringify!($t) ,"::MIN)`")]
            /// will always silently return
            #[doc = concat!("`", stringify!($t) ,"::MIN`")]
            /// whether the program was compiled with `overflow-checks` turned off or not.
            ///
            /// # Examples
            /// ```
            /// # use devela::Divisor;
            #[doc = concat!["let d = Divisor::<", stringify![$t], ">::new(13).unwrap();"]]
            /// let div = d.div_of(-30);
            /// assert_eq!(div, -2);
            /// ```
            #[must_use]
            pub const fn div_of(&self, n: $t) -> $t {
                match self.inner {
                    DivisorInner::Shift(_, shift) => {
                        let mask = (1 as $t << shift).wrapping_sub(1);
                        let b = (n >> (<$t>::BITS - 1)) & mask;
                        n.wrapping_add(b) >> shift
                    },
                    DivisorInner::ShiftAndNegate(_, shift) => {
                        let mask = (1 as $t << shift).wrapping_sub(1);
                        let b = (n >> (<$t>::BITS - 1)) & mask;
                        let t = n.wrapping_add(b) >> shift;
                        t.wrapping_mul(-1)
                    },
                    DivisorInner::MultiplyShift(_, magic, shift) => {
                        let q = Self::mulh(magic, n) >> shift;
                        iif![q < 0; q + 1; q]
                    },
                    DivisorInner::MultiplyAddShift(_, magic, shift) => {
                        let q = Self::mulh(magic, n);
                        let t = q.wrapping_add(n) >> shift;
                        iif![t < 0; t + 1; t]
                    },
                    DivisorInner::MultiplyAddShiftNegate(_, magic, shift) => {
                        let q = Self::mulh(magic, n);
                        let t = q.wrapping_add(n.wrapping_mul(-1)) >> shift;
                        iif![t < 0; t + 1; t]
                    }
                }
            }
        }
    };

    (@unsigned $t:ty | $up:ty : $is_up:ident : $cap:literal) => {
        #[cfg(feature = $cap )]
        impl_divisor![@traits $t];

        #[cfg(feature = $cap )]
        #[doc = crate::doc_availability!(feature = $cap)]
        // #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $cap)))]
        impl Divisor<$t> {
            impl_divisor![@shared $t|$t|$up|$up:$is_up:$cap]; // shared methods

            /// Creates a divisor which can be used for faster computation
            /// of division and modulo by `d`.
            ///
            /// Returns `None` if `d` equals zero.
            ///
            /// # Examples
            /// ```
            /// # use devela::Divisor;
            #[doc = concat!["let _d = Divisor::<", stringify![$t], ">::new(5);"]]
            /// ```
            #[must_use]
            pub const fn new(d: $t) -> Option<Divisor<$t>> {
                if d == 0 {
                    Self::cold_0_divisor()
                } else {
                    let shift = d.ilog2() as u8;
                    let inner = if d.is_power_of_two() {
                        DivisorInner::Shift(d, shift)
                    } else {
                        let (mut magic, rem) = Self::div_rem_wide_by_base(1 << shift, d);
                        let e = d - rem;
                        if e < 1 << shift {
                            DivisorInner::MultiplyShift(d, magic + 1, shift)
                        } else {
                            magic = magic.wrapping_mul(2);
                            let (doubled_rem, overflowed) = rem.overflowing_mul(2);
                            if doubled_rem >= d || overflowed { magic += 1; }
                            DivisorInner::MultiplyAddShift(d, magic + 1, shift)
                        }
                    };
                    Some(Self { inner })
                }
            }

            /// Returns the value that was used to construct this divisor as a primitive type.
            ///
            /// # Examples
            /// ```
            /// # use devela::Divisor;
            #[doc = concat!["let d = Divisor::<", stringify![$t], ">::new(7).unwrap();"]]
            /// assert_eq!(d.get(), 7);
            /// ```
            #[must_use]
            pub const fn get(&self) -> $t {
                match self.inner {
                    DivisorInner::Shift(d, _) => d,
                    DivisorInner::MultiplyShift(d, _, _) => d,
                    DivisorInner::MultiplyAddShift(d, _, _) => d,
                    _ => unreachable![],
                }
            }

            /// Returns `true` if `n` is divisible by `self`.
            ///
            /// We take `0` to be divisible by all non-zero numbers.
            ///
            /// # Examples
            /// ```
            /// # use devela::Divisor;
            #[doc = concat!["let d = Divisor::<", stringify![$t], ">::new(17).unwrap();"]]
            /// assert!(d.divides(34));
            /// ```
            #[must_use]
            pub const fn divides(&self, n: $t) -> bool {
                self.rem_of(n) == 0
            }

            /// Returns the remainder of dividing `n` by `self`.
            ///
            /// # Examples
            /// ```
            /// # use devela::Divisor;
            #[doc = concat!["let d = Divisor::<", stringify![$t], ">::new(11).unwrap();"]]
            /// let rem = d.rem_of(30);
            /// assert_eq!(rem, 8);
            /// ```
            #[must_use]
            pub const fn rem_of(&self, n: $t) -> $t {
                n - self.get() * self.div_of(n)
            }

            /// Returns the result of dividing `n` by `self`.
            ///
            /// # Examples
            /// ```
            /// # use devela::Divisor;
            #[doc = concat!["let d = Divisor::<", stringify![$t], ">::new(17).unwrap();"]]
            /// let div = d.div_of(34);
            /// assert_eq!(div, 2);
            /// ```
            #[must_use]
            pub const fn div_of(&self, n: $t) -> $t {
                match self.inner {
                    DivisorInner::Shift(_, shift) => n >> shift,
                    DivisorInner::MultiplyShift(_, magic, shift) => Self::mulh(magic, n) >> shift,
                    DivisorInner::MultiplyAddShift(_, magic, shift) => {
                        let q = Self::mulh(magic, n);
                        let t = ((n - q) >> 1) + q;
                        t >> shift
                    },
                    _ => unreachable![], // the remaining arms are only for signed
                }
            }
        }
    };

    (@shared $t:ty | $un:ty | $up:ty | $unup:ty : $is_up:ident : $cap:literal) => {
        paste!{
            /// Alias of [`new`][Self::new] with a unique name that helps type inference.
            pub const fn [<new_ $t>](d: $t) -> Option<Divisor<$t>> { Self::new(d) }
        }

        /// Helper function to be called from the cold path branch when divisor == 0.
        #[cold] #[inline(never)]
        const fn cold_0_divisor() -> Option<Self> { None }

        /// Multiply two words together, returning only the top half of the product.
        ///
        /// Works by extending the factors to 2N-bits, using the built-in 2N-by-2N-bit
        /// multiplication and shifting right to the top half only.
        #[compile(any(same($is_up, Y), all(same($is_up, PW), pointer_width_eq(64))))]
        const fn mulh(x: $t, y: $t) -> $t {
            (((x as $up) * (y as $up)) >> <$t>::BITS) as $t
        }
        /// Non-upcasting version, adapted from Figure 8-2 in Hacker's Delight, 2nd Ed.
        #[compile(any(same($is_up, N), all(same($is_up, PW), not(pointer_width_eq(64)))))]
        const fn mulh(x: $t, y: $t) -> $t {
            const HALF_WIDTH_BITS: u32 = <$t>::BITS / 2;
            const LOWER_HALF_MASK: $t = (1 << HALF_WIDTH_BITS) - 1;

            let x_low = x & LOWER_HALF_MASK;
            let y_low = y & LOWER_HALF_MASK;
            let t = x_low.wrapping_mul(y_low);
            let k = t >> HALF_WIDTH_BITS;

            let x_high = x >> HALF_WIDTH_BITS;
            let t = x_high.wrapping_mul(y_low) + k;
            let k = t & LOWER_HALF_MASK;
            let w1 = t >> HALF_WIDTH_BITS;

            let y_high = y >> HALF_WIDTH_BITS;
            let t = x_low.wrapping_mul(y_high) + k;
            let k = t >> HALF_WIDTH_BITS;

            x_high.wrapping_mul(y_high) + w1 + k
        }

        /// Divide a 2N-bit dividend by an N-bit divisor with remainder, assuming
        /// that the result fits into N bits and that the lower half of bits of the
        /// dividend are all zero.
        ///
        /// Works by extending the dividend to 2N-bits and then using the built-in
        /// 2N-by-2N-bit division method.
        #[compile(any(same($is_up, Y), all(same($is_up, PW), pointer_width_eq(64))))]
        const fn div_rem_wide_by_base(top_half: $un, d: $un) -> ($un, $un) {
            let n = (top_half as $unup) << <$un>::BITS;
            let quot = (n / (d as $unup)) as $un;
            let rem = (n % (d as $unup)) as $un;
            (quot, rem)
        }
        /// Non-upcasting version, adapted from Figure 9-3 in Hacker's Delight, 2nd Ed.
        #[compile(any(same($is_up, N), all(same($is_up, PW), not(pointer_width_eq(64)))))]
        const fn div_rem_wide_by_base(top_half: $un, d: $un) -> ($un, $un) {
            const HALF_WORD_BITS: u32 = <$un>::BITS / 2;
            const BASE: $un = 1 << HALF_WORD_BITS;
            let s = d.leading_zeros();
            let v = d << s;
            let vn1 = v >> HALF_WORD_BITS;
            let vn0 = v & (BASE - 1);
            let un32 = top_half << s;
            let mut q1 = un32 / vn1;
            let mut rhat = un32 - q1 * vn1;
            loop {
                if q1 >= BASE || q1 * vn0 > (rhat << HALF_WORD_BITS) {
                    q1 -= 1;
                    rhat += vn1;
                    iif![rhat < BASE; continue];
                }
                break;
            }
            let un21 = (un32 << HALF_WORD_BITS).wrapping_sub(q1.wrapping_mul(v));
            let mut q0 = un21 / vn1;
            rhat = un21 - q0 * vn1;
            loop {
                if q0 >= BASE || q0 * vn0 > (rhat << HALF_WORD_BITS) {
                    q0 -= 1;
                    rhat += vn1;
                    iif![rhat < BASE; continue];
                }
                break;
            }
            let r = ((un21 << HALF_WORD_BITS).wrapping_sub(q0.wrapping_mul(v))) >> s;
            ((q1 << HALF_WORD_BITS) + q0, r)
        }
    };

    (@traits $t:ty) => {
        impl PartialEq for Divisor<$t> {
            fn eq(&self, other: &Self) -> bool { self.get() == other.get() }
        }
        impl Eq for Divisor<$t> {}
        impl fmt::Debug for Divisor<$t> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.get()) }
        }
        impl fmt::Display for Divisor<$t> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.get()) }
        }
        impl hash::Hash for Divisor<$t> {
            fn hash<H: hash::Hasher>(&self, state: &mut H) { self.get().hash(state); }
        }
        impl ops::Div<Divisor<$t>> for $t {
            type Output = $t;
            fn div(self, rhs: Divisor<$t>) -> Self::Output { rhs.div_of(self) }
        }
        impl ops::DivAssign<Divisor<$t>> for $t {
            fn div_assign(&mut self, rhs: Divisor<$t>) { *self = rhs.div_of(*self) }
        }
        impl ops::Rem<Divisor<$t>> for $t {
            type Output = $t;
            fn rem(self, rhs: Divisor<$t>) -> Self::Output { rhs.rem_of(self) }
        }
        impl ops::RemAssign<Divisor<$t>> for $t {
            fn rem_assign(&mut self, rhs: Divisor<$t>) { *self = rhs.rem_of(*self) }
        }
    };
}
impl_divisor!();