Skip to main content

malachite_base/num/arithmetic/
wrapping_mul_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::{WrappingMulAddMul, WrappingMulAddMulAssign};
10
11macro_rules! impl_wrapping_mul_add_mul {
12    ($t:ident) => {
13        impl WrappingMulAddMul for $t {
14            type Output = $t;
15
16            /// Adds the products of two pairs of numbers, wrapping around at the boundary of the
17            /// type.
18            ///
19            /// $f(x, y, z, w) = z$, where $z \\equiv xy + zw \\mod 2^W$ and $W$ is `Self::WIDTH`.
20            ///
21            /// # Worst-case complexity
22            /// Constant time and additional memory.
23            ///
24            /// # Examples
25            /// See [here](super::wrapping_mul_add_mul#wrapping_mul_add_mul).
26            #[inline]
27            fn wrapping_mul_add_mul(self, y: $t, z: $t, w: $t) -> $t {
28                self.wrapping_mul(y).wrapping_add(z.wrapping_mul(w))
29            }
30        }
31
32        impl WrappingMulAddMulAssign for $t {
33            /// Adds the products of two pairs of numbers, in place, wrapping around at the boundary
34            /// of the type.
35            ///
36            /// $x \\gets z$, where $z \\equiv xy + zw \\mod 2^W$ and $W$ is `Self::WIDTH`.
37            ///
38            /// # Worst-case complexity
39            /// Constant time and additional memory.
40            ///
41            /// # Examples
42            /// See [here](super::wrapping_mul_add_mul#wrapping_mul_add_mul_assign).
43            #[inline]
44            fn wrapping_mul_add_mul_assign(&mut self, y: $t, z: $t, w: $t) {
45                *self = self.wrapping_mul(y).wrapping_add(z.wrapping_mul(w));
46            }
47        }
48    };
49}
50apply_to_primitive_ints!(impl_wrapping_mul_add_mul);