malachite_base/num/arithmetic/
add_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::{AddMul, AddMulAssign, WrappingAddMul, WrappingAddMulAssign};
10
11macro_rules! impl_add_mul_primitive_int {
12    ($t:ident) => {
13        impl AddMul for $t {
14            type Output = $t;
15
16            /// Adds a number and the product of two other numbers.
17            ///
18            /// $f(x, y, z) = x + yz$.
19            ///
20            /// # Worst-case complexity
21            /// Constant time and additional memory.
22            ///
23            /// # Examples
24            /// See [here](super::add_mul#add_mul).
25            #[inline]
26            fn add_mul(self, y: $t, z: $t) -> $t {
27                self.wrapping_add_mul(y, z)
28            }
29        }
30
31        impl AddMulAssign<$t> for $t {
32            /// Adds the product of two other numbers to a number in place.
33            ///
34            /// $x \gets x + yz$.
35            ///
36            /// # Worst-case complexity
37            /// Constant time and additional memory.
38            ///
39            /// # Examples
40            /// See [here](super::add_mul#add_mul_assign).
41            #[inline]
42            fn add_mul_assign(&mut self, y: $t, z: $t) {
43                self.wrapping_add_mul_assign(y, z)
44            }
45        }
46    };
47}
48apply_to_primitive_ints!(impl_add_mul_primitive_int);
49
50macro_rules! impl_add_mul_primitive_float {
51    ($t:ident) => {
52        impl AddMul for $t {
53            type Output = $t;
54
55            /// Adds a number and the product of two other numbers.
56            ///
57            /// $f(x, y, z) = x + yz$.
58            ///
59            /// # Worst-case complexity
60            /// Constant time and additional memory.
61            ///
62            /// # Examples
63            /// See [here](super::add_mul#add_mul).
64            #[inline]
65            fn add_mul(self, y: $t, z: $t) -> $t {
66                self + y * z
67            }
68        }
69
70        impl AddMulAssign<$t> for $t {
71            /// Adds the product of two other numbers to a number in place.
72            ///
73            /// $x \gets x + yz$.
74            ///
75            /// # Worst-case complexity
76            /// Constant time and additional memory.
77            ///
78            /// # Examples
79            /// See [here](super::add_mul#add_mul_assign).
80            #[inline]
81            fn add_mul_assign(&mut self, y: $t, z: $t) {
82                *self += y * z;
83            }
84        }
85    };
86}
87apply_to_primitive_floats!(impl_add_mul_primitive_float);