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