malachite_base/num/arithmetic/
mod_power_of_2_mul.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::num::arithmetic::traits::{ModPowerOf2Mul, ModPowerOf2MulAssign};
10use crate::num::basic::unsigneds::PrimitiveUnsigned;
11
12fn mod_power_of_2_mul<T: PrimitiveUnsigned>(x: T, y: T, pow: u64) -> T {
13    assert!(pow <= T::WIDTH);
14    assert!(
15        x.significant_bits() <= pow,
16        "x must be reduced mod 2^pow, but {x} >= 2^{pow}"
17    );
18    assert!(
19        y.significant_bits() <= pow,
20        "y must be reduced mod 2^pow, but {y} >= 2^{pow}"
21    );
22    x.wrapping_mul(y).mod_power_of_2(pow)
23}
24
25#[inline]
26fn mod_power_of_2_mul_assign<T: PrimitiveUnsigned>(x: &mut T, y: T, pow: u64) {
27    assert!(pow <= T::WIDTH);
28    assert!(
29        x.significant_bits() <= pow,
30        "x must be reduced mod 2^pow, but {x} >= 2^{pow}"
31    );
32    assert!(
33        y.significant_bits() <= pow,
34        "y must be reduced mod 2^pow, but {y} >= 2^{pow}"
35    );
36    x.wrapping_mul_assign(y);
37    x.mod_power_of_2_assign(pow);
38}
39
40macro_rules! impl_mod_power_of_2_mul {
41    ($t:ident) => {
42        impl ModPowerOf2Mul<$t> for $t {
43            type Output = $t;
44
45            /// Multiplies two numbers modulo a third number $2^k$. The inputs must be already
46            /// reduced modulo $2^k$.
47            ///
48            /// $f(x, y, k) = z$, where $x, y, z < 2^k$ and $xy \equiv z \mod 2^k$.
49            ///
50            /// # Worst-case complexity
51            /// Constant time and additional memory.
52            ///
53            /// # Panics
54            /// Panics if `pow` is greater than `Self::WIDTH` or if `self` or `other` are greater
55            /// than or equal to $2^k$.
56            ///
57            /// # Examples
58            /// See [here](super::mod_power_of_2_mul#mod_power_of_2_mul).
59            #[inline]
60            fn mod_power_of_2_mul(self, other: $t, pow: u64) -> $t {
61                mod_power_of_2_mul(self, other, pow)
62            }
63        }
64
65        impl ModPowerOf2MulAssign<$t> for $t {
66            /// Multiplies two numbers modulo a third number $2^k$, in place. The inputs must be
67            /// already reduced modulo $2^k$.
68            ///
69            /// $x \gets z$, where $x, y, z < 2^k$ and $xy \equiv z \mod 2^k$.
70            ///
71            /// # Worst-case complexity
72            /// Constant time and additional memory.
73            ///
74            /// # Panics
75            /// Panics if `pow` is greater than `Self::WIDTH` or if `self` or `other` are greater
76            /// than or equal to $2^k$.
77            ///
78            /// # Examples
79            /// See [here](super::mod_power_of_2_mul#mod_power_of_2_mul_assign).
80            #[inline]
81            fn mod_power_of_2_mul_assign(&mut self, other: $t, pow: u64) {
82                mod_power_of_2_mul_assign(self, other, pow)
83            }
84        }
85    };
86}
87apply_to_unsigneds!(impl_mod_power_of_2_mul);