/rust/registry/src/index.crates.io-6f17d22bba15001f/num-bigint-0.4.6/src/bigint/multiplication.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::CheckedUnsignedAbs::{Negative, Positive}; |
2 | | use super::Sign::{self, Minus, NoSign, Plus}; |
3 | | use super::{BigInt, UnsignedAbs}; |
4 | | |
5 | | use crate::{IsizePromotion, UsizePromotion}; |
6 | | |
7 | | use core::iter::Product; |
8 | | use core::ops::{Mul, MulAssign}; |
9 | | use num_traits::{CheckedMul, One, Zero}; |
10 | | |
11 | | impl Mul<Sign> for Sign { |
12 | | type Output = Sign; |
13 | | |
14 | | #[inline] |
15 | 0 | fn mul(self, other: Sign) -> Sign { |
16 | 0 | match (self, other) { |
17 | 0 | (NoSign, _) | (_, NoSign) => NoSign, |
18 | 0 | (Plus, Plus) | (Minus, Minus) => Plus, |
19 | 0 | (Plus, Minus) | (Minus, Plus) => Minus, |
20 | | } |
21 | 0 | } |
22 | | } |
23 | | |
24 | | macro_rules! impl_mul { |
25 | | ($(impl Mul<$Other:ty> for $Self:ty;)*) => {$( |
26 | | impl Mul<$Other> for $Self { |
27 | | type Output = BigInt; |
28 | | |
29 | | #[inline] |
30 | 0 | fn mul(self, other: $Other) -> BigInt { |
31 | 0 | // automatically match value/ref |
32 | 0 | let BigInt { data: x, .. } = self; |
33 | 0 | let BigInt { data: y, .. } = other; |
34 | 0 | BigInt::from_biguint(self.sign * other.sign, x * y) |
35 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul>::mul Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Mul>::mul Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Mul<num_bigint::bigint::BigInt>>::mul Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<&num_bigint::bigint::BigInt>>::mul |
36 | | } |
37 | | )*} |
38 | | } |
39 | | impl_mul! { |
40 | | impl Mul<BigInt> for BigInt; |
41 | | impl Mul<BigInt> for &BigInt; |
42 | | impl Mul<&BigInt> for BigInt; |
43 | | impl Mul<&BigInt> for &BigInt; |
44 | | } |
45 | | |
46 | | macro_rules! impl_mul_assign { |
47 | | ($(impl MulAssign<$Other:ty> for BigInt;)*) => {$( |
48 | | impl MulAssign<$Other> for BigInt { |
49 | | #[inline] |
50 | 0 | fn mul_assign(&mut self, other: $Other) { |
51 | 0 | // automatically match value/ref |
52 | 0 | let BigInt { data: y, .. } = other; |
53 | 0 | self.data *= y; |
54 | 0 | if self.data.is_zero() { |
55 | 0 | self.sign = NoSign; |
56 | 0 | } else { |
57 | 0 | self.sign = self.sign * other.sign; |
58 | 0 | } |
59 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign>::mul_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<&num_bigint::bigint::BigInt>>::mul_assign |
60 | | } |
61 | | )*} |
62 | | } |
63 | | impl_mul_assign! { |
64 | | impl MulAssign<BigInt> for BigInt; |
65 | | impl MulAssign<&BigInt> for BigInt; |
66 | | } |
67 | | |
68 | | promote_all_scalars!(impl Mul for BigInt, mul); |
69 | | promote_all_scalars_assign!(impl MulAssign for BigInt, mul_assign); |
70 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<u32> for BigInt, mul); |
71 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<u64> for BigInt, mul); |
72 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<u128> for BigInt, mul); |
73 | | |
74 | | impl Mul<u32> for BigInt { |
75 | | type Output = BigInt; |
76 | | |
77 | | #[inline] |
78 | 0 | fn mul(self, other: u32) -> BigInt { |
79 | 0 | BigInt::from_biguint(self.sign, self.data * other) |
80 | 0 | } |
81 | | } |
82 | | |
83 | | impl MulAssign<u32> for BigInt { |
84 | | #[inline] |
85 | 0 | fn mul_assign(&mut self, other: u32) { |
86 | 0 | self.data *= other; |
87 | 0 | if self.data.is_zero() { |
88 | 0 | self.sign = NoSign; |
89 | 0 | } |
90 | 0 | } |
91 | | } |
92 | | |
93 | | impl Mul<u64> for BigInt { |
94 | | type Output = BigInt; |
95 | | |
96 | | #[inline] |
97 | 0 | fn mul(self, other: u64) -> BigInt { |
98 | 0 | BigInt::from_biguint(self.sign, self.data * other) |
99 | 0 | } |
100 | | } |
101 | | |
102 | | impl MulAssign<u64> for BigInt { |
103 | | #[inline] |
104 | 0 | fn mul_assign(&mut self, other: u64) { |
105 | 0 | self.data *= other; |
106 | 0 | if self.data.is_zero() { |
107 | 0 | self.sign = NoSign; |
108 | 0 | } |
109 | 0 | } |
110 | | } |
111 | | |
112 | | impl Mul<u128> for BigInt { |
113 | | type Output = BigInt; |
114 | | |
115 | | #[inline] |
116 | 0 | fn mul(self, other: u128) -> BigInt { |
117 | 0 | BigInt::from_biguint(self.sign, self.data * other) |
118 | 0 | } |
119 | | } |
120 | | |
121 | | impl MulAssign<u128> for BigInt { |
122 | | #[inline] |
123 | 0 | fn mul_assign(&mut self, other: u128) { |
124 | 0 | self.data *= other; |
125 | 0 | if self.data.is_zero() { |
126 | 0 | self.sign = NoSign; |
127 | 0 | } |
128 | 0 | } |
129 | | } |
130 | | |
131 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<i32> for BigInt, mul); |
132 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<i64> for BigInt, mul); |
133 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<i128> for BigInt, mul); |
134 | | |
135 | | impl Mul<i32> for BigInt { |
136 | | type Output = BigInt; |
137 | | |
138 | | #[inline] |
139 | 0 | fn mul(self, other: i32) -> BigInt { |
140 | 0 | match other.checked_uabs() { |
141 | 0 | Positive(u) => self * u, |
142 | 0 | Negative(u) => -self * u, |
143 | | } |
144 | 0 | } |
145 | | } |
146 | | |
147 | | impl MulAssign<i32> for BigInt { |
148 | | #[inline] |
149 | 0 | fn mul_assign(&mut self, other: i32) { |
150 | 0 | match other.checked_uabs() { |
151 | 0 | Positive(u) => *self *= u, |
152 | 0 | Negative(u) => { |
153 | 0 | self.sign = -self.sign; |
154 | 0 | self.data *= u; |
155 | 0 | } |
156 | | } |
157 | 0 | } |
158 | | } |
159 | | |
160 | | impl Mul<i64> for BigInt { |
161 | | type Output = BigInt; |
162 | | |
163 | | #[inline] |
164 | 0 | fn mul(self, other: i64) -> BigInt { |
165 | 0 | match other.checked_uabs() { |
166 | 0 | Positive(u) => self * u, |
167 | 0 | Negative(u) => -self * u, |
168 | | } |
169 | 0 | } |
170 | | } |
171 | | |
172 | | impl MulAssign<i64> for BigInt { |
173 | | #[inline] |
174 | 0 | fn mul_assign(&mut self, other: i64) { |
175 | 0 | match other.checked_uabs() { |
176 | 0 | Positive(u) => *self *= u, |
177 | 0 | Negative(u) => { |
178 | 0 | self.sign = -self.sign; |
179 | 0 | self.data *= u; |
180 | 0 | } |
181 | | } |
182 | 0 | } |
183 | | } |
184 | | |
185 | | impl Mul<i128> for BigInt { |
186 | | type Output = BigInt; |
187 | | |
188 | | #[inline] |
189 | 0 | fn mul(self, other: i128) -> BigInt { |
190 | 0 | match other.checked_uabs() { |
191 | 0 | Positive(u) => self * u, |
192 | 0 | Negative(u) => -self * u, |
193 | | } |
194 | 0 | } |
195 | | } |
196 | | |
197 | | impl MulAssign<i128> for BigInt { |
198 | | #[inline] |
199 | 0 | fn mul_assign(&mut self, other: i128) { |
200 | 0 | match other.checked_uabs() { |
201 | 0 | Positive(u) => *self *= u, |
202 | 0 | Negative(u) => { |
203 | 0 | self.sign = -self.sign; |
204 | 0 | self.data *= u; |
205 | 0 | } |
206 | | } |
207 | 0 | } |
208 | | } |
209 | | |
210 | | impl CheckedMul for BigInt { |
211 | | #[inline] |
212 | 0 | fn checked_mul(&self, v: &BigInt) -> Option<BigInt> { |
213 | 0 | Some(self.mul(v)) |
214 | 0 | } |
215 | | } |
216 | | |
217 | | impl_product_iter_type!(BigInt); |