Skip to main content

malachite_base/num/arithmetic/
mod_power_of_2_add_mul.rs

1// Copyright © 2026 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::{ModPowerOf2AddMul, ModPowerOf2AddMulAssign};
10use crate::num::basic::unsigneds::PrimitiveUnsigned;
11
12fn assert_reduced<T: PrimitiveUnsigned>(x: T, y: T, z: T, pow: u64) {
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    assert!(
23        z.significant_bits() <= pow,
24        "z must be reduced mod 2^pow, but {z} >= 2^{pow}"
25    );
26}
27
28#[inline]
29fn mod_power_of_2_add_mul<T: PrimitiveUnsigned>(x: T, y: T, z: T, pow: u64) -> T {
30    assert_reduced(x, y, z, pow);
31    x.wrapping_add_mul(y, z).mod_power_of_2(pow)
32}
33
34#[inline]
35fn mod_power_of_2_add_mul_assign<T: PrimitiveUnsigned>(x: &mut T, y: T, z: T, pow: u64) {
36    assert_reduced(*x, y, z, pow);
37    x.wrapping_add_mul_assign(y, z);
38    x.mod_power_of_2_assign(pow);
39}
40
41macro_rules! impl_mod_power_of_2_add_mul {
42    ($t:ident) => {
43        impl ModPowerOf2AddMul<$t> for $t {
44            type Output = $t;
45
46            /// Adds a number and the product of two other numbers, modulo a fourth number $2^k$.
47            /// All three inputs must be already reduced modulo $2^k$.
48            ///
49            /// $f(x, y, z, k) = w$, where $x, y, z, w < 2^k$ and $x + yz \equiv w \mod 2^k$.
50            ///
51            /// # Worst-case complexity
52            /// Constant time and additional memory.
53            ///
54            /// # Panics
55            /// Panics if `pow` is greater than `Self::WIDTH` or if `self`, `y`, or `z` are greater
56            /// than or equal to $2^k$.
57            ///
58            /// # Examples
59            /// See [here](super::mod_power_of_2_add_mul#mod_power_of_2_add_mul).
60            #[inline]
61            fn mod_power_of_2_add_mul(self, y: $t, z: $t, pow: u64) -> $t {
62                mod_power_of_2_add_mul(self, y, z, pow)
63            }
64        }
65
66        impl ModPowerOf2AddMulAssign<$t> for $t {
67            /// Adds the product of two numbers to a third number, modulo a fourth number $2^k$, in
68            /// place. All three inputs must be already reduced modulo $2^k$.
69            ///
70            /// $x \gets w$, where $x, y, z, w < 2^k$ and $x + yz \equiv w \mod 2^k$.
71            ///
72            /// # Worst-case complexity
73            /// Constant time and additional memory.
74            ///
75            /// # Panics
76            /// Panics if `pow` is greater than `Self::WIDTH` or if `self`, `y`, or `z` are greater
77            /// than or equal to $2^k$.
78            ///
79            /// # Examples
80            /// See [here](super::mod_power_of_2_add_mul#mod_power_of_2_add_mul_assign).
81            #[inline]
82            fn mod_power_of_2_add_mul_assign(&mut self, y: $t, z: $t, pow: u64) {
83                mod_power_of_2_add_mul_assign(self, y, z, pow);
84            }
85        }
86    };
87}
88apply_to_unsigneds!(impl_mod_power_of_2_add_mul);