malachite_base/num/arithmetic/
wrapping_sub_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::{WrappingSubMul, WrappingSubMulAssign};
10use crate::num::basic::integers::PrimitiveInt;
11
12fn wrapping_sub_mul<T: PrimitiveInt>(x: T, y: T, z: T) -> T {
13    x.wrapping_sub(y.wrapping_mul(z))
14}
15
16fn wrapping_sub_mul_assign<T: PrimitiveInt>(x: &mut T, y: T, z: T) {
17    x.wrapping_sub_assign(y.wrapping_mul(z));
18}
19
20macro_rules! impl_wrapping_sub_mul {
21    ($t:ident) => {
22        impl WrappingSubMul<$t> for $t {
23            type Output = $t;
24
25            /// Subtracts a number by the product of two other numbers, wrapping around at the
26            /// boundary of the type.
27            ///
28            /// $f(x, y, z) = w$, where $w \equiv x - yz \mod 2^W$ and $W$ is `Self::WIDTH`.
29            ///
30            /// # Worst-case complexity
31            /// Constant time and additional memory.
32            ///
33            /// # Examples
34            /// See [here](super::wrapping_sub_mul#wrapping_sub_mul).
35            #[inline]
36            fn wrapping_sub_mul(self, y: $t, z: $t) -> $t {
37                wrapping_sub_mul(self, y, z)
38            }
39        }
40
41        impl WrappingSubMulAssign<$t> for $t {
42            /// Subtracts a number by the product of two other numbers in place, wrapping around at
43            /// the boundary of the type.
44            ///
45            /// $x \gets w$, where $w \equiv x - yz \mod 2^W$ and $W$ is `Self::WIDTH`.
46            ///
47            /// # Worst-case complexity
48            /// Constant time and additional memory.
49            ///
50            /// # Examples
51            /// See [here](super::wrapping_sub_mul#wrapping_sub_mul_assign).
52            #[inline]
53            fn wrapping_sub_mul_assign(&mut self, y: $t, z: $t) {
54                wrapping_sub_mul_assign(self, y, z)
55            }
56        }
57    };
58}
59apply_to_primitive_ints!(impl_wrapping_sub_mul);