malachite_base/num/arithmetic/
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::{SubMul, SubMulAssign, WrappingSubMul, WrappingSubMulAssign};
10
11macro_rules! impl_sub_mul_primitive_int {
12    ($t:ident) => {
13        impl SubMul<$t> for $t {
14            type Output = $t;
15
16            /// Subtracts a number by 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::sub_mul#sub_mul).
25            #[inline]
26            fn sub_mul(self, y: $t, z: $t) -> $t {
27                self.wrapping_sub_mul(y, z)
28            }
29        }
30
31        impl SubMulAssign<$t> for $t {
32            /// Subtracts a number by the product of two other numbers 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::sub_mul#sub_mul_assign).
41            #[inline]
42            fn sub_mul_assign(&mut self, y: $t, z: $t) {
43                self.wrapping_sub_mul_assign(y, z);
44            }
45        }
46    };
47}
48apply_to_primitive_ints!(impl_sub_mul_primitive_int);
49
50macro_rules! impl_sub_mul_primitive_float {
51    ($t:ident) => {
52        impl SubMul for $t {
53            type Output = $t;
54
55            /// Subtracts a number by 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::sub_mul#sub_mul).
64            #[inline]
65            fn sub_mul(self, y: $t, z: $t) -> $t {
66                self - y * z
67            }
68        }
69
70        impl SubMulAssign<$t> for $t {
71            /// Subtracts a number by the product of two other numbers 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::sub_mul#sub_mul_assign).
80            #[inline]
81            fn sub_mul_assign(&mut self, y: $t, z: $t) {
82                *self -= y * z;
83            }
84        }
85    };
86}
87apply_to_primitive_floats!(impl_sub_mul_primitive_float);