num_modular/
preinv.rs

1use crate::{DivExact, ModularUnaryOps};
2
3/// Pre-computing the modular inverse for fast divisibility check.
4///
5/// This struct stores the modular inverse of a divisor, and a limit for divisibility check.
6/// See <https://math.stackexchange.com/a/1251328> for the explanation of the trick
7#[derive(Debug, Clone, Copy)]
8pub struct PreModInv<T> {
9    d_inv: T, // modular inverse of divisor
10    q_lim: T, // limit of residue
11}
12
13macro_rules! impl_preinv_for_prim_int {
14    ($t:ident, $ns:ident) => {
15        mod $ns {
16            use super::*;
17            use crate::word::$t::*;
18
19            impl PreModInv<$t> {
20                /// Construct the preinv instance with raw values.
21                ///
22                /// This function can be used to initialize preinv in a constant context, the divisor d
23                /// is required only for verification of d_inv and q_lim.
24                #[inline]
25                pub const fn new(d_inv: $t, q_lim: $t) -> Self {
26                    Self { d_inv, q_lim }
27                }
28
29                // check if the divisor is consistent in debug mode
30                #[inline]
31                fn debug_check(&self, d: $t) {
32                    debug_assert!(d % 2 != 0, "only odd divisors are supported");
33                    debug_assert!(d.wrapping_mul(self.d_inv) == 1);
34                    debug_assert!(self.q_lim * d > (<$t>::MAX - d));
35                }
36            }
37
38            impl From<$t> for PreModInv<$t> {
39                #[inline]
40                fn from(v: $t) -> Self {
41                    use crate::word::$t::*;
42
43                    debug_assert!(v % 2 != 0, "only odd divisors are supported");
44                    let d_inv = extend(v).invm(&merge(0, 1)).unwrap() as $t;
45                    let q_lim = <$t>::MAX / v;
46                    Self { d_inv, q_lim }
47                }
48            }
49
50            impl DivExact<$t, PreModInv<$t>> for $t {
51                type Output = $t;
52                #[inline]
53                fn div_exact(self, d: $t, pre: &PreModInv<$t>) -> Option<Self> {
54                    pre.debug_check(d);
55                    let q = self.wrapping_mul(pre.d_inv);
56                    if q <= pre.q_lim {
57                        Some(q)
58                    } else {
59                        None
60                    }
61                }
62            }
63
64            impl DivExact<$t, PreModInv<$t>> for DoubleWord {
65                type Output = DoubleWord;
66
67                #[inline]
68                fn div_exact(self, d: $t, pre: &PreModInv<$t>) -> Option<Self::Output> {
69                    pre.debug_check(d);
70
71                    // this implementation comes from GNU factor,
72                    // see https://math.stackexchange.com/q/4436380/815652 for explanation
73
74                    let (n0, n1) = split(self);
75                    let q0 = n0.wrapping_mul(pre.d_inv);
76                    let nr0 = wmul(q0, d);
77                    let nr0 = split(nr0).1;
78                    if nr0 > n1 {
79                        return None;
80                    }
81                    let nr1 = n1 - nr0;
82                    let q1 = nr1.wrapping_mul(pre.d_inv);
83                    if q1 > pre.q_lim {
84                        return None;
85                    }
86                    Some(merge(q0, q1))
87                }
88            }
89        }
90    };
91}
92impl_preinv_for_prim_int!(u8, u8_impl);
93impl_preinv_for_prim_int!(u16, u16_impl);
94impl_preinv_for_prim_int!(u32, u32_impl);
95impl_preinv_for_prim_int!(u64, u64_impl);
96impl_preinv_for_prim_int!(usize, usize_impl);
97
98// XXX: unchecked div_exact can be introduced by not checking the q_lim,
99//      investigate this after `exact_div` is introduced or removed from core lib
100//      https://github.com/rust-lang/rust/issues/85122
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105    use rand::random;
106
107    #[test]
108    fn div_exact_test() {
109        const N: u8 = 100;
110        for _ in 0..N {
111            // u8 test
112            let d = random::<u8>() | 1;
113            let pre: PreModInv<_> = d.into();
114
115            let n: u8 = random();
116            let expect = if n % d == 0 { Some(n / d) } else { None };
117            assert_eq!(n.div_exact(d, &pre), expect, "{} / {}", n, d);
118            let n: u16 = random();
119            let expect = if n % (d as u16) == 0 {
120                Some(n / (d as u16))
121            } else {
122                None
123            };
124            assert_eq!(n.div_exact(d, &pre), expect, "{} / {}", n, d);
125
126            // u16 test
127            let d = random::<u16>() | 1;
128            let pre: PreModInv<_> = d.into();
129
130            let n: u16 = random();
131            let expect = if n % d == 0 { Some(n / d) } else { None };
132            assert_eq!(n.div_exact(d, &pre), expect, "{} / {}", n, d);
133            let n: u32 = random();
134            let expect = if n % (d as u32) == 0 {
135                Some(n / (d as u32))
136            } else {
137                None
138            };
139            assert_eq!(n.div_exact(d, &pre), expect, "{} / {}", n, d);
140
141            // u32 test
142            let d = random::<u32>() | 1;
143            let pre: PreModInv<_> = d.into();
144
145            let n: u32 = random();
146            let expect = if n % d == 0 { Some(n / d) } else { None };
147            assert_eq!(n.div_exact(d, &pre), expect, "{} / {}", n, d);
148            let n: u64 = random();
149            let expect = if n % (d as u64) == 0 {
150                Some(n / (d as u64))
151            } else {
152                None
153            };
154            assert_eq!(n.div_exact(d, &pre), expect, "{} / {}", n, d);
155
156            // u64 test
157            let d = random::<u64>() | 1;
158            let pre: PreModInv<_> = d.into();
159
160            let n: u64 = random();
161            let expect = if n % d == 0 { Some(n / d) } else { None };
162            assert_eq!(n.div_exact(d, &pre), expect, "{} / {}", n, d);
163            let n: u128 = random();
164            let expect = if n % (d as u128) == 0 {
165                Some(n / (d as u128))
166            } else {
167                None
168            };
169            assert_eq!(n.div_exact(d, &pre), expect, "{} / {}", n, d);
170        }
171    }
172}