/rust/registry/src/index.crates.io-1949cf8c6b5b557f/num-bigint-0.2.6/src/bigint.rs
Line | Count | Source |
1 | | #[allow(deprecated, unused_imports)] |
2 | | use std::ascii::AsciiExt; |
3 | | use std::cmp::Ordering::{self, Equal, Greater, Less}; |
4 | | use std::default::Default; |
5 | | use std::fmt; |
6 | | use std::iter::{Product, Sum}; |
7 | | use std::mem; |
8 | | use std::ops::{ |
9 | | Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, |
10 | | Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign, |
11 | | }; |
12 | | use std::str::{self, FromStr}; |
13 | | #[cfg(has_i128)] |
14 | | use std::{i128, u128}; |
15 | | use std::{i64, u64}; |
16 | | |
17 | | #[cfg(feature = "serde")] |
18 | | use serde; |
19 | | |
20 | | use integer::{Integer, Roots}; |
21 | | use traits::{ |
22 | | CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Pow, Signed, |
23 | | ToPrimitive, Zero, |
24 | | }; |
25 | | |
26 | | use self::Sign::{Minus, NoSign, Plus}; |
27 | | |
28 | | use super::ParseBigIntError; |
29 | | use big_digit::{self, BigDigit, DoubleBigDigit}; |
30 | | use biguint; |
31 | | use biguint::to_str_radix_reversed; |
32 | | use biguint::{BigUint, IntDigits}; |
33 | | |
34 | | use IsizePromotion; |
35 | | use UsizePromotion; |
36 | | |
37 | | #[cfg(feature = "quickcheck")] |
38 | | use quickcheck::{Arbitrary, Gen}; |
39 | | |
40 | | /// A Sign is a `BigInt`'s composing element. |
41 | | #[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Debug, Hash)] |
42 | | pub enum Sign { |
43 | | Minus, |
44 | | NoSign, |
45 | | Plus, |
46 | | } |
47 | | |
48 | | impl Neg for Sign { |
49 | | type Output = Sign; |
50 | | |
51 | | /// Negate Sign value. |
52 | | #[inline] |
53 | 0 | fn neg(self) -> Sign { |
54 | 0 | match self { |
55 | 0 | Minus => Plus, |
56 | 0 | NoSign => NoSign, |
57 | 0 | Plus => Minus, |
58 | | } |
59 | 0 | } Unexecuted instantiation: <num_bigint::bigint::Sign as core::ops::arith::Neg>::neg Unexecuted instantiation: <num_bigint::bigint::Sign as core::ops::arith::Neg>::neg |
60 | | } |
61 | | |
62 | | impl Mul<Sign> for Sign { |
63 | | type Output = Sign; |
64 | | |
65 | | #[inline] |
66 | 0 | fn mul(self, other: Sign) -> Sign { |
67 | 0 | match (self, other) { |
68 | 0 | (NoSign, _) | (_, NoSign) => NoSign, |
69 | 0 | (Plus, Plus) | (Minus, Minus) => Plus, |
70 | 0 | (Plus, Minus) | (Minus, Plus) => Minus, |
71 | | } |
72 | 0 | } Unexecuted instantiation: <num_bigint::bigint::Sign as core::ops::arith::Mul>::mul Unexecuted instantiation: <num_bigint::bigint::Sign as core::ops::arith::Mul>::mul |
73 | | } |
74 | | |
75 | | #[cfg(feature = "serde")] |
76 | | impl serde::Serialize for Sign { |
77 | | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
78 | | where |
79 | | S: serde::Serializer, |
80 | | { |
81 | | // Note: do not change the serialization format, or it may break |
82 | | // forward and backward compatibility of serialized data! |
83 | | match *self { |
84 | | Sign::Minus => (-1i8).serialize(serializer), |
85 | | Sign::NoSign => 0i8.serialize(serializer), |
86 | | Sign::Plus => 1i8.serialize(serializer), |
87 | | } |
88 | | } |
89 | | } |
90 | | |
91 | | #[cfg(feature = "serde")] |
92 | | impl<'de> serde::Deserialize<'de> for Sign { |
93 | | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
94 | | where |
95 | | D: serde::Deserializer<'de>, |
96 | | { |
97 | | use serde::de::Error; |
98 | | use serde::de::Unexpected; |
99 | | |
100 | | let sign: i8 = serde::Deserialize::deserialize(deserializer)?; |
101 | | match sign { |
102 | | -1 => Ok(Sign::Minus), |
103 | | 0 => Ok(Sign::NoSign), |
104 | | 1 => Ok(Sign::Plus), |
105 | | _ => Err(D::Error::invalid_value( |
106 | | Unexpected::Signed(sign.into()), |
107 | | &"a sign of -1, 0, or 1", |
108 | | )), |
109 | | } |
110 | | } |
111 | | } |
112 | | |
113 | | /// A big signed integer type. |
114 | | #[derive(Clone, Debug, Hash)] |
115 | | pub struct BigInt { |
116 | | sign: Sign, |
117 | | data: BigUint, |
118 | | } |
119 | | |
120 | | #[cfg(feature = "quickcheck")] |
121 | | impl Arbitrary for BigInt { |
122 | | fn arbitrary<G: Gen>(g: &mut G) -> Self { |
123 | | let positive = bool::arbitrary(g); |
124 | | let sign = if positive { Sign::Plus } else { Sign::Minus }; |
125 | | Self::from_biguint(sign, BigUint::arbitrary(g)) |
126 | | } |
127 | | |
128 | | #[allow(bare_trait_objects)] // `dyn` needs Rust 1.27 to parse, even when cfg-disabled |
129 | | fn shrink(&self) -> Box<Iterator<Item = Self>> { |
130 | | let sign = self.sign(); |
131 | | let unsigned_shrink = self.data.shrink(); |
132 | | Box::new(unsigned_shrink.map(move |x| BigInt::from_biguint(sign, x))) |
133 | | } |
134 | | } |
135 | | |
136 | | /// Return the magnitude of a `BigInt`. |
137 | | /// |
138 | | /// This is in a private module, pseudo pub(crate) |
139 | | #[cfg(feature = "rand")] |
140 | | pub fn magnitude(i: &BigInt) -> &BigUint { |
141 | | &i.data |
142 | | } |
143 | | |
144 | | /// Return the owned magnitude of a `BigInt`. |
145 | | /// |
146 | | /// This is in a private module, pseudo pub(crate) |
147 | | #[cfg(feature = "rand")] |
148 | | pub fn into_magnitude(i: BigInt) -> BigUint { |
149 | | i.data |
150 | | } |
151 | | |
152 | | impl PartialEq for BigInt { |
153 | | #[inline] |
154 | 0 | fn eq(&self, other: &BigInt) -> bool { |
155 | 0 | self.cmp(other) == Equal |
156 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::cmp::PartialEq>::eq Unexecuted instantiation: <num_bigint::bigint::BigInt as core::cmp::PartialEq>::eq |
157 | | } |
158 | | |
159 | | impl Eq for BigInt {} |
160 | | |
161 | | impl PartialOrd for BigInt { |
162 | | #[inline] |
163 | 0 | fn partial_cmp(&self, other: &BigInt) -> Option<Ordering> { |
164 | 0 | Some(self.cmp(other)) |
165 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::cmp::PartialOrd>::partial_cmp Unexecuted instantiation: <num_bigint::bigint::BigInt as core::cmp::PartialOrd>::partial_cmp |
166 | | } |
167 | | |
168 | | impl Ord for BigInt { |
169 | | #[inline] |
170 | 0 | fn cmp(&self, other: &BigInt) -> Ordering { |
171 | 0 | let scmp = self.sign.cmp(&other.sign); |
172 | 0 | if scmp != Equal { |
173 | 0 | return scmp; |
174 | 0 | } |
175 | | |
176 | 0 | match self.sign { |
177 | 0 | NoSign => Equal, |
178 | 0 | Plus => self.data.cmp(&other.data), |
179 | 0 | Minus => other.data.cmp(&self.data), |
180 | | } |
181 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::cmp::Ord>::cmp Unexecuted instantiation: <num_bigint::bigint::BigInt as core::cmp::Ord>::cmp |
182 | | } |
183 | | |
184 | | impl Default for BigInt { |
185 | | #[inline] |
186 | 0 | fn default() -> BigInt { |
187 | 0 | Zero::zero() |
188 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::default::Default>::default Unexecuted instantiation: <num_bigint::bigint::BigInt as core::default::Default>::default |
189 | | } |
190 | | |
191 | | impl fmt::Display for BigInt { |
192 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
193 | 0 | f.pad_integral(!self.is_negative(), "", &self.data.to_str_radix(10)) |
194 | 0 | } |
195 | | } |
196 | | |
197 | | impl fmt::Binary for BigInt { |
198 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
199 | 0 | f.pad_integral(!self.is_negative(), "0b", &self.data.to_str_radix(2)) |
200 | 0 | } |
201 | | } |
202 | | |
203 | | impl fmt::Octal for BigInt { |
204 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
205 | 0 | f.pad_integral(!self.is_negative(), "0o", &self.data.to_str_radix(8)) |
206 | 0 | } |
207 | | } |
208 | | |
209 | | impl fmt::LowerHex for BigInt { |
210 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
211 | 0 | f.pad_integral(!self.is_negative(), "0x", &self.data.to_str_radix(16)) |
212 | 0 | } |
213 | | } |
214 | | |
215 | | impl fmt::UpperHex for BigInt { |
216 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
217 | 0 | let mut s = self.data.to_str_radix(16); |
218 | 0 | s.make_ascii_uppercase(); |
219 | 0 | f.pad_integral(!self.is_negative(), "0x", &s) |
220 | 0 | } |
221 | | } |
222 | | |
223 | | // Negation in two's complement. |
224 | | // acc must be initialized as 1 for least-significant digit. |
225 | | // |
226 | | // When negating, a carry (acc == 1) means that all the digits |
227 | | // considered to this point were zero. This means that if all the |
228 | | // digits of a negative BigInt have been considered, carry must be |
229 | | // zero as we cannot have negative zero. |
230 | | // |
231 | | // 01 -> ...f ff |
232 | | // ff -> ...f 01 |
233 | | // 01 00 -> ...f ff 00 |
234 | | // 01 01 -> ...f fe ff |
235 | | // 01 ff -> ...f fe 01 |
236 | | // ff 00 -> ...f 01 00 |
237 | | // ff 01 -> ...f 00 ff |
238 | | // ff ff -> ...f 00 01 |
239 | | #[inline] |
240 | 0 | fn negate_carry(a: BigDigit, acc: &mut DoubleBigDigit) -> BigDigit { |
241 | 0 | *acc += DoubleBigDigit::from(!a); |
242 | 0 | let lo = *acc as BigDigit; |
243 | 0 | *acc >>= big_digit::BITS; |
244 | 0 | lo |
245 | 0 | } Unexecuted instantiation: num_bigint::bigint::negate_carry Unexecuted instantiation: num_bigint::bigint::negate_carry |
246 | | |
247 | | // !-2 = !...f fe = ...0 01 = +1 |
248 | | // !-1 = !...f ff = ...0 00 = 0 |
249 | | // ! 0 = !...0 00 = ...f ff = -1 |
250 | | // !+1 = !...0 01 = ...f fe = -2 |
251 | | impl Not for BigInt { |
252 | | type Output = BigInt; |
253 | | |
254 | 0 | fn not(mut self) -> BigInt { |
255 | 0 | match self.sign { |
256 | 0 | NoSign | Plus => { |
257 | 0 | self.data += 1u32; |
258 | 0 | self.sign = Minus; |
259 | 0 | } |
260 | | Minus => { |
261 | 0 | self.data -= 1u32; |
262 | 0 | self.sign = if self.data.is_zero() { NoSign } else { Plus }; |
263 | | } |
264 | | } |
265 | 0 | self |
266 | 0 | } |
267 | | } |
268 | | |
269 | | impl<'a> Not for &'a BigInt { |
270 | | type Output = BigInt; |
271 | | |
272 | 0 | fn not(self) -> BigInt { |
273 | 0 | match self.sign { |
274 | 0 | NoSign | Plus => BigInt::from_biguint(Minus, &self.data + 1u32), |
275 | 0 | Minus => BigInt::from_biguint(Plus, &self.data - 1u32), |
276 | | } |
277 | 0 | } |
278 | | } |
279 | | |
280 | | // + 1 & -ff = ...0 01 & ...f 01 = ...0 01 = + 1 |
281 | | // +ff & - 1 = ...0 ff & ...f ff = ...0 ff = +ff |
282 | | // answer is pos, has length of a |
283 | 0 | fn bitand_pos_neg(a: &mut Vec<BigDigit>, b: &[BigDigit]) { |
284 | 0 | let mut carry_b = 1; |
285 | 0 | for (ai, &bi) in a.iter_mut().zip(b.iter()) { |
286 | 0 | let twos_b = negate_carry(bi, &mut carry_b); |
287 | 0 | *ai &= twos_b; |
288 | 0 | } |
289 | 0 | debug_assert!(b.len() > a.len() || carry_b == 0); |
290 | 0 | } |
291 | | |
292 | | // - 1 & +ff = ...f ff & ...0 ff = ...0 ff = +ff |
293 | | // -ff & + 1 = ...f 01 & ...0 01 = ...0 01 = + 1 |
294 | | // answer is pos, has length of b |
295 | 0 | fn bitand_neg_pos(a: &mut Vec<BigDigit>, b: &[BigDigit]) { |
296 | 0 | let mut carry_a = 1; |
297 | 0 | for (ai, &bi) in a.iter_mut().zip(b.iter()) { |
298 | 0 | let twos_a = negate_carry(*ai, &mut carry_a); |
299 | 0 | *ai = twos_a & bi; |
300 | 0 | } |
301 | 0 | debug_assert!(a.len() > b.len() || carry_a == 0); |
302 | 0 | if a.len() > b.len() { |
303 | 0 | a.truncate(b.len()); |
304 | 0 | } else if b.len() > a.len() { |
305 | 0 | let extra = &b[a.len()..]; |
306 | 0 | a.extend(extra.iter().cloned()); |
307 | 0 | } |
308 | 0 | } |
309 | | |
310 | | // - 1 & -ff = ...f ff & ...f 01 = ...f 01 = - ff |
311 | | // -ff & - 1 = ...f 01 & ...f ff = ...f 01 = - ff |
312 | | // -ff & -fe = ...f 01 & ...f 02 = ...f 00 = -100 |
313 | | // answer is neg, has length of longest with a possible carry |
314 | 0 | fn bitand_neg_neg(a: &mut Vec<BigDigit>, b: &[BigDigit]) { |
315 | 0 | let mut carry_a = 1; |
316 | 0 | let mut carry_b = 1; |
317 | 0 | let mut carry_and = 1; |
318 | 0 | for (ai, &bi) in a.iter_mut().zip(b.iter()) { |
319 | 0 | let twos_a = negate_carry(*ai, &mut carry_a); |
320 | 0 | let twos_b = negate_carry(bi, &mut carry_b); |
321 | 0 | *ai = negate_carry(twos_a & twos_b, &mut carry_and); |
322 | 0 | } |
323 | 0 | debug_assert!(a.len() > b.len() || carry_a == 0); |
324 | 0 | debug_assert!(b.len() > a.len() || carry_b == 0); |
325 | 0 | if a.len() > b.len() { |
326 | 0 | for ai in a[b.len()..].iter_mut() { |
327 | 0 | let twos_a = negate_carry(*ai, &mut carry_a); |
328 | 0 | *ai = negate_carry(twos_a, &mut carry_and); |
329 | 0 | } |
330 | 0 | debug_assert!(carry_a == 0); |
331 | 0 | } else if b.len() > a.len() { |
332 | 0 | let extra = &b[a.len()..]; |
333 | 0 | a.extend(extra.iter().map(|&bi| { |
334 | 0 | let twos_b = negate_carry(bi, &mut carry_b); |
335 | 0 | negate_carry(twos_b, &mut carry_and) |
336 | 0 | })); Unexecuted instantiation: num_bigint::bigint::bitand_neg_neg::{closure#0}Unexecuted instantiation: num_bigint::bigint::bitand_neg_neg::{closure#0} |
337 | 0 | debug_assert!(carry_b == 0); |
338 | 0 | } |
339 | 0 | if carry_and != 0 { |
340 | 0 | a.push(1); |
341 | 0 | } |
342 | 0 | } |
343 | | |
344 | | forward_val_val_binop!(impl BitAnd for BigInt, bitand); |
345 | | forward_ref_val_binop!(impl BitAnd for BigInt, bitand); |
346 | | |
347 | | // do not use forward_ref_ref_binop_commutative! for bitand so that we can |
348 | | // clone as needed, avoiding over-allocation |
349 | | impl<'a, 'b> BitAnd<&'b BigInt> for &'a BigInt { |
350 | | type Output = BigInt; |
351 | | |
352 | | #[inline] |
353 | 0 | fn bitand(self, other: &BigInt) -> BigInt { |
354 | 0 | match (self.sign, other.sign) { |
355 | 0 | (NoSign, _) | (_, NoSign) => BigInt::from_slice(NoSign, &[]), |
356 | 0 | (Plus, Plus) => BigInt::from_biguint(Plus, &self.data & &other.data), |
357 | 0 | (Plus, Minus) => self.clone() & other, |
358 | 0 | (Minus, Plus) => other.clone() & self, |
359 | | (Minus, Minus) => { |
360 | | // forward to val-ref, choosing the larger to clone |
361 | 0 | if self.len() >= other.len() { |
362 | 0 | self.clone() & other |
363 | | } else { |
364 | 0 | other.clone() & self |
365 | | } |
366 | | } |
367 | | } |
368 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::bit::BitAnd>::bitand Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::bit::BitAnd>::bitand |
369 | | } |
370 | | |
371 | | impl<'a> BitAnd<&'a BigInt> for BigInt { |
372 | | type Output = BigInt; |
373 | | |
374 | | #[inline] |
375 | 0 | fn bitand(mut self, other: &BigInt) -> BigInt { |
376 | 0 | self &= other; |
377 | 0 | self |
378 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::BitAnd<&num_bigint::bigint::BigInt>>::bitand Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::BitAnd<&num_bigint::bigint::BigInt>>::bitand |
379 | | } |
380 | | |
381 | | forward_val_assign!(impl BitAndAssign for BigInt, bitand_assign); |
382 | | |
383 | | impl<'a> BitAndAssign<&'a BigInt> for BigInt { |
384 | 0 | fn bitand_assign(&mut self, other: &BigInt) { |
385 | 0 | match (self.sign, other.sign) { |
386 | 0 | (NoSign, _) => {} |
387 | 0 | (_, NoSign) => self.assign_from_slice(NoSign, &[]), |
388 | | (Plus, Plus) => { |
389 | 0 | self.data &= &other.data; |
390 | 0 | if self.data.is_zero() { |
391 | 0 | self.sign = NoSign; |
392 | 0 | } |
393 | | } |
394 | 0 | (Plus, Minus) => { |
395 | 0 | bitand_pos_neg(self.digits_mut(), other.digits()); |
396 | 0 | self.normalize(); |
397 | 0 | } |
398 | 0 | (Minus, Plus) => { |
399 | 0 | bitand_neg_pos(self.digits_mut(), other.digits()); |
400 | 0 | self.sign = Plus; |
401 | 0 | self.normalize(); |
402 | 0 | } |
403 | 0 | (Minus, Minus) => { |
404 | 0 | bitand_neg_neg(self.digits_mut(), other.digits()); |
405 | 0 | self.normalize(); |
406 | 0 | } |
407 | | } |
408 | 0 | } |
409 | | } |
410 | | |
411 | | // + 1 | -ff = ...0 01 | ...f 01 = ...f 01 = -ff |
412 | | // +ff | - 1 = ...0 ff | ...f ff = ...f ff = - 1 |
413 | | // answer is neg, has length of b |
414 | 0 | fn bitor_pos_neg(a: &mut Vec<BigDigit>, b: &[BigDigit]) { |
415 | 0 | let mut carry_b = 1; |
416 | 0 | let mut carry_or = 1; |
417 | 0 | for (ai, &bi) in a.iter_mut().zip(b.iter()) { |
418 | 0 | let twos_b = negate_carry(bi, &mut carry_b); |
419 | 0 | *ai = negate_carry(*ai | twos_b, &mut carry_or); |
420 | 0 | } |
421 | 0 | debug_assert!(b.len() > a.len() || carry_b == 0); |
422 | 0 | if a.len() > b.len() { |
423 | 0 | a.truncate(b.len()); |
424 | 0 | } else if b.len() > a.len() { |
425 | 0 | let extra = &b[a.len()..]; |
426 | 0 | a.extend(extra.iter().map(|&bi| { |
427 | 0 | let twos_b = negate_carry(bi, &mut carry_b); |
428 | 0 | negate_carry(twos_b, &mut carry_or) |
429 | 0 | })); Unexecuted instantiation: num_bigint::bigint::bitor_pos_neg::{closure#0}Unexecuted instantiation: num_bigint::bigint::bitor_pos_neg::{closure#0} |
430 | 0 | debug_assert!(carry_b == 0); |
431 | 0 | } |
432 | | // for carry_or to be non-zero, we would need twos_b == 0 |
433 | 0 | debug_assert!(carry_or == 0); |
434 | 0 | } |
435 | | |
436 | | // - 1 | +ff = ...f ff | ...0 ff = ...f ff = - 1 |
437 | | // -ff | + 1 = ...f 01 | ...0 01 = ...f 01 = -ff |
438 | | // answer is neg, has length of a |
439 | 0 | fn bitor_neg_pos(a: &mut Vec<BigDigit>, b: &[BigDigit]) { |
440 | 0 | let mut carry_a = 1; |
441 | 0 | let mut carry_or = 1; |
442 | 0 | for (ai, &bi) in a.iter_mut().zip(b.iter()) { |
443 | 0 | let twos_a = negate_carry(*ai, &mut carry_a); |
444 | 0 | *ai = negate_carry(twos_a | bi, &mut carry_or); |
445 | 0 | } |
446 | 0 | debug_assert!(a.len() > b.len() || carry_a == 0); |
447 | 0 | if a.len() > b.len() { |
448 | 0 | for ai in a[b.len()..].iter_mut() { |
449 | 0 | let twos_a = negate_carry(*ai, &mut carry_a); |
450 | 0 | *ai = negate_carry(twos_a, &mut carry_or); |
451 | 0 | } |
452 | 0 | debug_assert!(carry_a == 0); |
453 | 0 | } |
454 | | // for carry_or to be non-zero, we would need twos_a == 0 |
455 | 0 | debug_assert!(carry_or == 0); |
456 | 0 | } |
457 | | |
458 | | // - 1 | -ff = ...f ff | ...f 01 = ...f ff = -1 |
459 | | // -ff | - 1 = ...f 01 | ...f ff = ...f ff = -1 |
460 | | // answer is neg, has length of shortest |
461 | 0 | fn bitor_neg_neg(a: &mut Vec<BigDigit>, b: &[BigDigit]) { |
462 | 0 | let mut carry_a = 1; |
463 | 0 | let mut carry_b = 1; |
464 | 0 | let mut carry_or = 1; |
465 | 0 | for (ai, &bi) in a.iter_mut().zip(b.iter()) { |
466 | 0 | let twos_a = negate_carry(*ai, &mut carry_a); |
467 | 0 | let twos_b = negate_carry(bi, &mut carry_b); |
468 | 0 | *ai = negate_carry(twos_a | twos_b, &mut carry_or); |
469 | 0 | } |
470 | 0 | debug_assert!(a.len() > b.len() || carry_a == 0); |
471 | 0 | debug_assert!(b.len() > a.len() || carry_b == 0); |
472 | 0 | if a.len() > b.len() { |
473 | 0 | a.truncate(b.len()); |
474 | 0 | } |
475 | | // for carry_or to be non-zero, we would need twos_a == 0 or twos_b == 0 |
476 | 0 | debug_assert!(carry_or == 0); |
477 | 0 | } |
478 | | |
479 | | forward_val_val_binop!(impl BitOr for BigInt, bitor); |
480 | | forward_ref_val_binop!(impl BitOr for BigInt, bitor); |
481 | | |
482 | | // do not use forward_ref_ref_binop_commutative! for bitor so that we can |
483 | | // clone as needed, avoiding over-allocation |
484 | | impl<'a, 'b> BitOr<&'b BigInt> for &'a BigInt { |
485 | | type Output = BigInt; |
486 | | |
487 | | #[inline] |
488 | 0 | fn bitor(self, other: &BigInt) -> BigInt { |
489 | 0 | match (self.sign, other.sign) { |
490 | 0 | (NoSign, _) => other.clone(), |
491 | 0 | (_, NoSign) => self.clone(), |
492 | 0 | (Plus, Plus) => BigInt::from_biguint(Plus, &self.data | &other.data), |
493 | 0 | (Plus, Minus) => other.clone() | self, |
494 | 0 | (Minus, Plus) => self.clone() | other, |
495 | | (Minus, Minus) => { |
496 | | // forward to val-ref, choosing the smaller to clone |
497 | 0 | if self.len() <= other.len() { |
498 | 0 | self.clone() | other |
499 | | } else { |
500 | 0 | other.clone() | self |
501 | | } |
502 | | } |
503 | | } |
504 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::bit::BitOr>::bitor Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::bit::BitOr>::bitor |
505 | | } |
506 | | |
507 | | impl<'a> BitOr<&'a BigInt> for BigInt { |
508 | | type Output = BigInt; |
509 | | |
510 | | #[inline] |
511 | 0 | fn bitor(mut self, other: &BigInt) -> BigInt { |
512 | 0 | self |= other; |
513 | 0 | self |
514 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::BitOr<&num_bigint::bigint::BigInt>>::bitor Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::BitOr<&num_bigint::bigint::BigInt>>::bitor |
515 | | } |
516 | | |
517 | | forward_val_assign!(impl BitOrAssign for BigInt, bitor_assign); |
518 | | |
519 | | impl<'a> BitOrAssign<&'a BigInt> for BigInt { |
520 | 0 | fn bitor_assign(&mut self, other: &BigInt) { |
521 | 0 | match (self.sign, other.sign) { |
522 | 0 | (_, NoSign) => {} |
523 | 0 | (NoSign, _) => self.assign_from_slice(other.sign, other.digits()), |
524 | 0 | (Plus, Plus) => self.data |= &other.data, |
525 | 0 | (Plus, Minus) => { |
526 | 0 | bitor_pos_neg(self.digits_mut(), other.digits()); |
527 | 0 | self.sign = Minus; |
528 | 0 | self.normalize(); |
529 | 0 | } |
530 | 0 | (Minus, Plus) => { |
531 | 0 | bitor_neg_pos(self.digits_mut(), other.digits()); |
532 | 0 | self.normalize(); |
533 | 0 | } |
534 | 0 | (Minus, Minus) => { |
535 | 0 | bitor_neg_neg(self.digits_mut(), other.digits()); |
536 | 0 | self.normalize(); |
537 | 0 | } |
538 | | } |
539 | 0 | } |
540 | | } |
541 | | |
542 | | // + 1 ^ -ff = ...0 01 ^ ...f 01 = ...f 00 = -100 |
543 | | // +ff ^ - 1 = ...0 ff ^ ...f ff = ...f 00 = -100 |
544 | | // answer is neg, has length of longest with a possible carry |
545 | 0 | fn bitxor_pos_neg(a: &mut Vec<BigDigit>, b: &[BigDigit]) { |
546 | 0 | let mut carry_b = 1; |
547 | 0 | let mut carry_xor = 1; |
548 | 0 | for (ai, &bi) in a.iter_mut().zip(b.iter()) { |
549 | 0 | let twos_b = negate_carry(bi, &mut carry_b); |
550 | 0 | *ai = negate_carry(*ai ^ twos_b, &mut carry_xor); |
551 | 0 | } |
552 | 0 | debug_assert!(b.len() > a.len() || carry_b == 0); |
553 | 0 | if a.len() > b.len() { |
554 | 0 | for ai in a[b.len()..].iter_mut() { |
555 | 0 | let twos_b = !0; |
556 | 0 | *ai = negate_carry(*ai ^ twos_b, &mut carry_xor); |
557 | 0 | } |
558 | 0 | } else if b.len() > a.len() { |
559 | 0 | let extra = &b[a.len()..]; |
560 | 0 | a.extend(extra.iter().map(|&bi| { |
561 | 0 | let twos_b = negate_carry(bi, &mut carry_b); |
562 | 0 | negate_carry(twos_b, &mut carry_xor) |
563 | 0 | })); Unexecuted instantiation: num_bigint::bigint::bitxor_pos_neg::{closure#0}Unexecuted instantiation: num_bigint::bigint::bitxor_pos_neg::{closure#0} |
564 | 0 | debug_assert!(carry_b == 0); |
565 | 0 | } |
566 | 0 | if carry_xor != 0 { |
567 | 0 | a.push(1); |
568 | 0 | } |
569 | 0 | } |
570 | | |
571 | | // - 1 ^ +ff = ...f ff ^ ...0 ff = ...f 00 = -100 |
572 | | // -ff ^ + 1 = ...f 01 ^ ...0 01 = ...f 00 = -100 |
573 | | // answer is neg, has length of longest with a possible carry |
574 | 0 | fn bitxor_neg_pos(a: &mut Vec<BigDigit>, b: &[BigDigit]) { |
575 | 0 | let mut carry_a = 1; |
576 | 0 | let mut carry_xor = 1; |
577 | 0 | for (ai, &bi) in a.iter_mut().zip(b.iter()) { |
578 | 0 | let twos_a = negate_carry(*ai, &mut carry_a); |
579 | 0 | *ai = negate_carry(twos_a ^ bi, &mut carry_xor); |
580 | 0 | } |
581 | 0 | debug_assert!(a.len() > b.len() || carry_a == 0); |
582 | 0 | if a.len() > b.len() { |
583 | 0 | for ai in a[b.len()..].iter_mut() { |
584 | 0 | let twos_a = negate_carry(*ai, &mut carry_a); |
585 | 0 | *ai = negate_carry(twos_a, &mut carry_xor); |
586 | 0 | } |
587 | 0 | debug_assert!(carry_a == 0); |
588 | 0 | } else if b.len() > a.len() { |
589 | 0 | let extra = &b[a.len()..]; |
590 | 0 | a.extend(extra.iter().map(|&bi| { |
591 | 0 | let twos_a = !0; |
592 | 0 | negate_carry(twos_a ^ bi, &mut carry_xor) |
593 | 0 | })); Unexecuted instantiation: num_bigint::bigint::bitxor_neg_pos::{closure#0}Unexecuted instantiation: num_bigint::bigint::bitxor_neg_pos::{closure#0} |
594 | 0 | } |
595 | 0 | if carry_xor != 0 { |
596 | 0 | a.push(1); |
597 | 0 | } |
598 | 0 | } |
599 | | |
600 | | // - 1 ^ -ff = ...f ff ^ ...f 01 = ...0 fe = +fe |
601 | | // -ff & - 1 = ...f 01 ^ ...f ff = ...0 fe = +fe |
602 | | // answer is pos, has length of longest |
603 | 0 | fn bitxor_neg_neg(a: &mut Vec<BigDigit>, b: &[BigDigit]) { |
604 | 0 | let mut carry_a = 1; |
605 | 0 | let mut carry_b = 1; |
606 | 0 | for (ai, &bi) in a.iter_mut().zip(b.iter()) { |
607 | 0 | let twos_a = negate_carry(*ai, &mut carry_a); |
608 | 0 | let twos_b = negate_carry(bi, &mut carry_b); |
609 | 0 | *ai = twos_a ^ twos_b; |
610 | 0 | } |
611 | 0 | debug_assert!(a.len() > b.len() || carry_a == 0); |
612 | 0 | debug_assert!(b.len() > a.len() || carry_b == 0); |
613 | 0 | if a.len() > b.len() { |
614 | 0 | for ai in a[b.len()..].iter_mut() { |
615 | 0 | let twos_a = negate_carry(*ai, &mut carry_a); |
616 | 0 | let twos_b = !0; |
617 | 0 | *ai = twos_a ^ twos_b; |
618 | 0 | } |
619 | 0 | debug_assert!(carry_a == 0); |
620 | 0 | } else if b.len() > a.len() { |
621 | 0 | let extra = &b[a.len()..]; |
622 | 0 | a.extend(extra.iter().map(|&bi| { |
623 | 0 | let twos_a = !0; |
624 | 0 | let twos_b = negate_carry(bi, &mut carry_b); |
625 | 0 | twos_a ^ twos_b |
626 | 0 | })); Unexecuted instantiation: num_bigint::bigint::bitxor_neg_neg::{closure#0}Unexecuted instantiation: num_bigint::bigint::bitxor_neg_neg::{closure#0} |
627 | 0 | debug_assert!(carry_b == 0); |
628 | 0 | } |
629 | 0 | } |
630 | | |
631 | | forward_all_binop_to_val_ref_commutative!(impl BitXor for BigInt, bitxor); |
632 | | |
633 | | impl<'a> BitXor<&'a BigInt> for BigInt { |
634 | | type Output = BigInt; |
635 | | |
636 | | #[inline] |
637 | 0 | fn bitxor(mut self, other: &BigInt) -> BigInt { |
638 | 0 | self ^= other; |
639 | 0 | self |
640 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::BitXor<&num_bigint::bigint::BigInt>>::bitxor Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::BitXor<&num_bigint::bigint::BigInt>>::bitxor |
641 | | } |
642 | | |
643 | | forward_val_assign!(impl BitXorAssign for BigInt, bitxor_assign); |
644 | | |
645 | | impl<'a> BitXorAssign<&'a BigInt> for BigInt { |
646 | 0 | fn bitxor_assign(&mut self, other: &BigInt) { |
647 | 0 | match (self.sign, other.sign) { |
648 | 0 | (_, NoSign) => {} |
649 | 0 | (NoSign, _) => self.assign_from_slice(other.sign, other.digits()), |
650 | | (Plus, Plus) => { |
651 | 0 | self.data ^= &other.data; |
652 | 0 | if self.data.is_zero() { |
653 | 0 | self.sign = NoSign; |
654 | 0 | } |
655 | | } |
656 | 0 | (Plus, Minus) => { |
657 | 0 | bitxor_pos_neg(self.digits_mut(), other.digits()); |
658 | 0 | self.sign = Minus; |
659 | 0 | self.normalize(); |
660 | 0 | } |
661 | 0 | (Minus, Plus) => { |
662 | 0 | bitxor_neg_pos(self.digits_mut(), other.digits()); |
663 | 0 | self.normalize(); |
664 | 0 | } |
665 | 0 | (Minus, Minus) => { |
666 | 0 | bitxor_neg_neg(self.digits_mut(), other.digits()); |
667 | 0 | self.sign = Plus; |
668 | 0 | self.normalize(); |
669 | 0 | } |
670 | | } |
671 | 0 | } |
672 | | } |
673 | | |
674 | | impl FromStr for BigInt { |
675 | | type Err = ParseBigIntError; |
676 | | |
677 | | #[inline] |
678 | 0 | fn from_str(s: &str) -> Result<BigInt, ParseBigIntError> { |
679 | 0 | BigInt::from_str_radix(s, 10) |
680 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::str::traits::FromStr>::from_str Unexecuted instantiation: <num_bigint::bigint::BigInt as core::str::traits::FromStr>::from_str |
681 | | } |
682 | | |
683 | | impl Num for BigInt { |
684 | | type FromStrRadixErr = ParseBigIntError; |
685 | | |
686 | | /// Creates and initializes a BigInt. |
687 | | #[inline] |
688 | 0 | fn from_str_radix(mut s: &str, radix: u32) -> Result<BigInt, ParseBigIntError> { |
689 | 0 | let sign = if s.starts_with('-') { |
690 | 0 | let tail = &s[1..]; |
691 | 0 | if !tail.starts_with('+') { |
692 | 0 | s = tail |
693 | 0 | } |
694 | 0 | Minus |
695 | | } else { |
696 | 0 | Plus |
697 | | }; |
698 | 0 | let bu = BigUint::from_str_radix(s, radix)?; |
699 | 0 | Ok(BigInt::from_biguint(sign, bu)) |
700 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::Num>::from_str_radix Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::Num>::from_str_radix |
701 | | } |
702 | | |
703 | | impl Shl<usize> for BigInt { |
704 | | type Output = BigInt; |
705 | | |
706 | | #[inline] |
707 | 0 | fn shl(mut self, rhs: usize) -> BigInt { |
708 | 0 | self <<= rhs; |
709 | 0 | self |
710 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::Shl<usize>>::shl Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::Shl<usize>>::shl |
711 | | } |
712 | | |
713 | | impl<'a> Shl<usize> for &'a BigInt { |
714 | | type Output = BigInt; |
715 | | |
716 | | #[inline] |
717 | 0 | fn shl(self, rhs: usize) -> BigInt { |
718 | 0 | BigInt::from_biguint(self.sign, &self.data << rhs) |
719 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::bit::Shl<usize>>::shl Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::bit::Shl<usize>>::shl |
720 | | } |
721 | | |
722 | | impl ShlAssign<usize> for BigInt { |
723 | | #[inline] |
724 | 0 | fn shl_assign(&mut self, rhs: usize) { |
725 | 0 | self.data <<= rhs; |
726 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::ShlAssign<usize>>::shl_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::ShlAssign<usize>>::shl_assign |
727 | | } |
728 | | |
729 | | // Negative values need a rounding adjustment if there are any ones in the |
730 | | // bits that are getting shifted out. |
731 | 0 | fn shr_round_down(i: &BigInt, rhs: usize) -> bool { |
732 | 0 | i.is_negative() |
733 | 0 | && biguint::trailing_zeros(&i.data) |
734 | 0 | .map(|n| n < rhs) |
735 | 0 | .unwrap_or(false) |
736 | 0 | } |
737 | | |
738 | | impl Shr<usize> for BigInt { |
739 | | type Output = BigInt; |
740 | | |
741 | | #[inline] |
742 | 0 | fn shr(mut self, rhs: usize) -> BigInt { |
743 | 0 | self >>= rhs; |
744 | 0 | self |
745 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::Shr<usize>>::shr Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::Shr<usize>>::shr |
746 | | } |
747 | | |
748 | | impl<'a> Shr<usize> for &'a BigInt { |
749 | | type Output = BigInt; |
750 | | |
751 | | #[inline] |
752 | 0 | fn shr(self, rhs: usize) -> BigInt { |
753 | 0 | let round_down = shr_round_down(self, rhs); |
754 | 0 | let data = &self.data >> rhs; |
755 | 0 | BigInt::from_biguint(self.sign, if round_down { data + 1u8 } else { data }) |
756 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::bit::Shr<usize>>::shr Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::bit::Shr<usize>>::shr |
757 | | } |
758 | | |
759 | | impl ShrAssign<usize> for BigInt { |
760 | | #[inline] |
761 | 0 | fn shr_assign(&mut self, rhs: usize) { |
762 | 0 | let round_down = shr_round_down(self, rhs); |
763 | 0 | self.data >>= rhs; |
764 | 0 | if round_down { |
765 | 0 | self.data += 1u8; |
766 | 0 | } else if self.data.is_zero() { |
767 | 0 | self.sign = NoSign; |
768 | 0 | } |
769 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::ShrAssign<usize>>::shr_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::bit::ShrAssign<usize>>::shr_assign |
770 | | } |
771 | | |
772 | | impl Zero for BigInt { |
773 | | #[inline] |
774 | 0 | fn zero() -> BigInt { |
775 | 0 | BigInt::from_biguint(NoSign, Zero::zero()) |
776 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::Zero>::zero Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::Zero>::zero |
777 | | |
778 | | #[inline] |
779 | 0 | fn set_zero(&mut self) { |
780 | 0 | self.data.set_zero(); |
781 | 0 | self.sign = NoSign; |
782 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::Zero>::set_zero Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::Zero>::set_zero |
783 | | |
784 | | #[inline] |
785 | 0 | fn is_zero(&self) -> bool { |
786 | 0 | self.sign == NoSign |
787 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::Zero>::is_zero Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::Zero>::is_zero |
788 | | } |
789 | | |
790 | | impl One for BigInt { |
791 | | #[inline] |
792 | 0 | fn one() -> BigInt { |
793 | 0 | BigInt::from_biguint(Plus, One::one()) |
794 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::One>::one Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::One>::one |
795 | | |
796 | | #[inline] |
797 | 0 | fn set_one(&mut self) { |
798 | 0 | self.data.set_one(); |
799 | 0 | self.sign = Plus; |
800 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::One>::set_one Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::One>::set_one |
801 | | |
802 | | #[inline] |
803 | 0 | fn is_one(&self) -> bool { |
804 | 0 | self.sign == Plus && self.data.is_one() |
805 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::One>::is_one Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::identities::One>::is_one |
806 | | } |
807 | | |
808 | | impl Signed for BigInt { |
809 | | #[inline] |
810 | 0 | fn abs(&self) -> BigInt { |
811 | 0 | match self.sign { |
812 | 0 | Plus | NoSign => self.clone(), |
813 | 0 | Minus => BigInt::from_biguint(Plus, self.data.clone()), |
814 | | } |
815 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::sign::Signed>::abs Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::sign::Signed>::abs |
816 | | |
817 | | #[inline] |
818 | 0 | fn abs_sub(&self, other: &BigInt) -> BigInt { |
819 | 0 | if *self <= *other { |
820 | 0 | Zero::zero() |
821 | | } else { |
822 | 0 | self - other |
823 | | } |
824 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::sign::Signed>::abs_sub Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::sign::Signed>::abs_sub |
825 | | |
826 | | #[inline] |
827 | 0 | fn signum(&self) -> BigInt { |
828 | 0 | match self.sign { |
829 | 0 | Plus => BigInt::from_biguint(Plus, One::one()), |
830 | 0 | Minus => BigInt::from_biguint(Minus, One::one()), |
831 | 0 | NoSign => Zero::zero(), |
832 | | } |
833 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::sign::Signed>::signum Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::sign::Signed>::signum |
834 | | |
835 | | #[inline] |
836 | 0 | fn is_positive(&self) -> bool { |
837 | 0 | self.sign == Plus |
838 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::sign::Signed>::is_positive Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::sign::Signed>::is_positive |
839 | | |
840 | | #[inline] |
841 | 0 | fn is_negative(&self) -> bool { |
842 | 0 | self.sign == Minus |
843 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::sign::Signed>::is_negative Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::sign::Signed>::is_negative |
844 | | } |
845 | | |
846 | | /// Help function for pow |
847 | | /// |
848 | | /// Computes the effect of the exponent on the sign. |
849 | | #[inline] |
850 | 0 | fn powsign<T: Integer>(sign: Sign, other: &T) -> Sign { |
851 | 0 | if other.is_zero() { |
852 | 0 | Plus |
853 | 0 | } else if sign != Minus { |
854 | 0 | sign |
855 | 0 | } else if other.is_odd() { |
856 | 0 | sign |
857 | | } else { |
858 | 0 | -sign |
859 | | } |
860 | 0 | } Unexecuted instantiation: num_bigint::bigint::powsign::<_> Unexecuted instantiation: num_bigint::bigint::powsign::<_> |
861 | | |
862 | | macro_rules! pow_impl { |
863 | | ($T:ty) => { |
864 | | impl<'a> Pow<$T> for &'a BigInt { |
865 | | type Output = BigInt; |
866 | | |
867 | | #[inline] |
868 | 0 | fn pow(self, rhs: $T) -> BigInt { |
869 | 0 | BigInt::from_biguint(powsign(self.sign, &rhs), (&self.data).pow(rhs)) |
870 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<u8>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<u16>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<u32>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<u64>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<usize>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<u128>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<num_bigint::biguint::BigUint>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<u8>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<u16>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<u32>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<u64>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<usize>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<u128>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<num_bigint::biguint::BigUint>>::pow |
871 | | } |
872 | | |
873 | | impl<'a, 'b> Pow<&'b $T> for &'a BigInt { |
874 | | type Output = BigInt; |
875 | | |
876 | | #[inline] |
877 | 0 | fn pow(self, rhs: &$T) -> BigInt { |
878 | 0 | BigInt::from_biguint(powsign(self.sign, rhs), (&self.data).pow(rhs)) |
879 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&u8>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&u16>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&u32>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&u64>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&usize>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&u128>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&num_bigint::biguint::BigUint>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&u8>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&u16>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&u32>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&u64>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&usize>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&u128>>::pow Unexecuted instantiation: <&num_bigint::bigint::BigInt as num_traits::pow::Pow<&num_bigint::biguint::BigUint>>::pow |
880 | | } |
881 | | }; |
882 | | } |
883 | | |
884 | | pow_impl!(u8); |
885 | | pow_impl!(u16); |
886 | | pow_impl!(u32); |
887 | | pow_impl!(u64); |
888 | | pow_impl!(usize); |
889 | | #[cfg(has_i128)] |
890 | | pow_impl!(u128); |
891 | | pow_impl!(BigUint); |
892 | | |
893 | | // A convenience method for getting the absolute value of an i32 in a u32. |
894 | | #[inline] |
895 | 0 | fn i32_abs_as_u32(a: i32) -> u32 { |
896 | 0 | if a == i32::min_value() { |
897 | 0 | a as u32 |
898 | | } else { |
899 | 0 | a.abs() as u32 |
900 | | } |
901 | 0 | } Unexecuted instantiation: num_bigint::bigint::i32_abs_as_u32 Unexecuted instantiation: num_bigint::bigint::i32_abs_as_u32 |
902 | | |
903 | | // A convenience method for getting the absolute value of an i64 in a u64. |
904 | | #[inline] |
905 | 0 | fn i64_abs_as_u64(a: i64) -> u64 { |
906 | 0 | if a == i64::min_value() { |
907 | 0 | a as u64 |
908 | | } else { |
909 | 0 | a.abs() as u64 |
910 | | } |
911 | 0 | } Unexecuted instantiation: num_bigint::bigint::i64_abs_as_u64 Unexecuted instantiation: num_bigint::bigint::i64_abs_as_u64 |
912 | | |
913 | | // A convenience method for getting the absolute value of an i128 in a u128. |
914 | | #[cfg(has_i128)] |
915 | | #[inline] |
916 | 0 | fn i128_abs_as_u128(a: i128) -> u128 { |
917 | 0 | if a == i128::min_value() { |
918 | 0 | a as u128 |
919 | | } else { |
920 | 0 | a.abs() as u128 |
921 | | } |
922 | 0 | } Unexecuted instantiation: num_bigint::bigint::i128_abs_as_u128 Unexecuted instantiation: num_bigint::bigint::i128_abs_as_u128 |
923 | | |
924 | | // We want to forward to BigUint::add, but it's not clear how that will go until |
925 | | // we compare both sign and magnitude. So we duplicate this body for every |
926 | | // val/ref combination, deferring that decision to BigUint's own forwarding. |
927 | | macro_rules! bigint_add { |
928 | | ($a:expr, $a_owned:expr, $a_data:expr, $b:expr, $b_owned:expr, $b_data:expr) => { |
929 | | match ($a.sign, $b.sign) { |
930 | | (_, NoSign) => $a_owned, |
931 | | (NoSign, _) => $b_owned, |
932 | | // same sign => keep the sign with the sum of magnitudes |
933 | | (Plus, Plus) | (Minus, Minus) => BigInt::from_biguint($a.sign, $a_data + $b_data), |
934 | | // opposite signs => keep the sign of the larger with the difference of magnitudes |
935 | | (Plus, Minus) | (Minus, Plus) => match $a.data.cmp(&$b.data) { |
936 | | Less => BigInt::from_biguint($b.sign, $b_data - $a_data), |
937 | | Greater => BigInt::from_biguint($a.sign, $a_data - $b_data), |
938 | | Equal => Zero::zero(), |
939 | | }, |
940 | | } |
941 | | }; |
942 | | } |
943 | | |
944 | | impl<'a, 'b> Add<&'b BigInt> for &'a BigInt { |
945 | | type Output = BigInt; |
946 | | |
947 | | #[inline] |
948 | 0 | fn add(self, other: &BigInt) -> BigInt { |
949 | 0 | bigint_add!( |
950 | | self, |
951 | 0 | self.clone(), |
952 | 0 | &self.data, |
953 | | other, |
954 | 0 | other.clone(), |
955 | 0 | &other.data |
956 | | ) |
957 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Add>::add Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Add>::add |
958 | | } |
959 | | |
960 | | impl<'a> Add<BigInt> for &'a BigInt { |
961 | | type Output = BigInt; |
962 | | |
963 | | #[inline] |
964 | 0 | fn add(self, other: BigInt) -> BigInt { |
965 | 0 | bigint_add!(self, self.clone(), &self.data, other, other, other.data) |
966 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Add<num_bigint::bigint::BigInt>>::add Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Add<num_bigint::bigint::BigInt>>::add |
967 | | } |
968 | | |
969 | | impl<'a> Add<&'a BigInt> for BigInt { |
970 | | type Output = BigInt; |
971 | | |
972 | | #[inline] |
973 | 0 | fn add(self, other: &BigInt) -> BigInt { |
974 | 0 | bigint_add!(self, self, self.data, other, other.clone(), &other.data) |
975 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<&num_bigint::bigint::BigInt>>::add Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<&num_bigint::bigint::BigInt>>::add |
976 | | } |
977 | | |
978 | | impl Add<BigInt> for BigInt { |
979 | | type Output = BigInt; |
980 | | |
981 | | #[inline] |
982 | 0 | fn add(self, other: BigInt) -> BigInt { |
983 | 0 | bigint_add!(self, self, self.data, other, other, other.data) |
984 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add>::add Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add>::add |
985 | | } |
986 | | |
987 | | impl<'a> AddAssign<&'a BigInt> for BigInt { |
988 | | #[inline] |
989 | 0 | fn add_assign(&mut self, other: &BigInt) { |
990 | 0 | let n = mem::replace(self, BigInt::zero()); |
991 | 0 | *self = n + other; |
992 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<&num_bigint::bigint::BigInt>>::add_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<&num_bigint::bigint::BigInt>>::add_assign |
993 | | } |
994 | | forward_val_assign!(impl AddAssign for BigInt, add_assign); |
995 | | |
996 | | promote_all_scalars!(impl Add for BigInt, add); |
997 | | promote_all_scalars_assign!(impl AddAssign for BigInt, add_assign); |
998 | | forward_all_scalar_binop_to_val_val_commutative!(impl Add<u32> for BigInt, add); |
999 | | forward_all_scalar_binop_to_val_val_commutative!(impl Add<u64> for BigInt, add); |
1000 | | #[cfg(has_i128)] |
1001 | | forward_all_scalar_binop_to_val_val_commutative!(impl Add<u128> for BigInt, add); |
1002 | | |
1003 | | impl Add<u32> for BigInt { |
1004 | | type Output = BigInt; |
1005 | | |
1006 | | #[inline] |
1007 | 0 | fn add(self, other: u32) -> BigInt { |
1008 | 0 | match self.sign { |
1009 | 0 | NoSign => From::from(other), |
1010 | 0 | Plus => BigInt::from_biguint(Plus, self.data + other), |
1011 | 0 | Minus => match self.data.cmp(&From::from(other)) { |
1012 | 0 | Equal => Zero::zero(), |
1013 | 0 | Less => BigInt::from_biguint(Plus, other - self.data), |
1014 | 0 | Greater => BigInt::from_biguint(Minus, self.data - other), |
1015 | | }, |
1016 | | } |
1017 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<u32>>::add Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<u32>>::add |
1018 | | } |
1019 | | impl AddAssign<u32> for BigInt { |
1020 | | #[inline] |
1021 | 0 | fn add_assign(&mut self, other: u32) { |
1022 | 0 | let n = mem::replace(self, BigInt::zero()); |
1023 | 0 | *self = n + other; |
1024 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<u32>>::add_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<u32>>::add_assign |
1025 | | } |
1026 | | |
1027 | | impl Add<u64> for BigInt { |
1028 | | type Output = BigInt; |
1029 | | |
1030 | | #[inline] |
1031 | 0 | fn add(self, other: u64) -> BigInt { |
1032 | 0 | match self.sign { |
1033 | 0 | NoSign => From::from(other), |
1034 | 0 | Plus => BigInt::from_biguint(Plus, self.data + other), |
1035 | 0 | Minus => match self.data.cmp(&From::from(other)) { |
1036 | 0 | Equal => Zero::zero(), |
1037 | 0 | Less => BigInt::from_biguint(Plus, other - self.data), |
1038 | 0 | Greater => BigInt::from_biguint(Minus, self.data - other), |
1039 | | }, |
1040 | | } |
1041 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<u64>>::add Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<u64>>::add |
1042 | | } |
1043 | | impl AddAssign<u64> for BigInt { |
1044 | | #[inline] |
1045 | 0 | fn add_assign(&mut self, other: u64) { |
1046 | 0 | let n = mem::replace(self, BigInt::zero()); |
1047 | 0 | *self = n + other; |
1048 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<u64>>::add_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<u64>>::add_assign |
1049 | | } |
1050 | | |
1051 | | #[cfg(has_i128)] |
1052 | | impl Add<u128> for BigInt { |
1053 | | type Output = BigInt; |
1054 | | |
1055 | | #[inline] |
1056 | 0 | fn add(self, other: u128) -> BigInt { |
1057 | 0 | match self.sign { |
1058 | 0 | NoSign => From::from(other), |
1059 | 0 | Plus => BigInt::from_biguint(Plus, self.data + other), |
1060 | 0 | Minus => match self.data.cmp(&From::from(other)) { |
1061 | 0 | Equal => Zero::zero(), |
1062 | 0 | Less => BigInt::from_biguint(Plus, other - self.data), |
1063 | 0 | Greater => BigInt::from_biguint(Minus, self.data - other), |
1064 | | }, |
1065 | | } |
1066 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<u128>>::add Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<u128>>::add |
1067 | | } |
1068 | | #[cfg(has_i128)] |
1069 | | impl AddAssign<u128> for BigInt { |
1070 | | #[inline] |
1071 | 0 | fn add_assign(&mut self, other: u128) { |
1072 | 0 | let n = mem::replace(self, BigInt::zero()); |
1073 | 0 | *self = n + other; |
1074 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<u128>>::add_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<u128>>::add_assign |
1075 | | } |
1076 | | |
1077 | | forward_all_scalar_binop_to_val_val_commutative!(impl Add<i32> for BigInt, add); |
1078 | | forward_all_scalar_binop_to_val_val_commutative!(impl Add<i64> for BigInt, add); |
1079 | | #[cfg(has_i128)] |
1080 | | forward_all_scalar_binop_to_val_val_commutative!(impl Add<i128> for BigInt, add); |
1081 | | |
1082 | | impl Add<i32> for BigInt { |
1083 | | type Output = BigInt; |
1084 | | |
1085 | | #[inline] |
1086 | 0 | fn add(self, other: i32) -> BigInt { |
1087 | 0 | if other >= 0 { |
1088 | 0 | self + other as u32 |
1089 | | } else { |
1090 | 0 | self - i32_abs_as_u32(other) |
1091 | | } |
1092 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<i32>>::add Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<i32>>::add |
1093 | | } |
1094 | | impl AddAssign<i32> for BigInt { |
1095 | | #[inline] |
1096 | 0 | fn add_assign(&mut self, other: i32) { |
1097 | 0 | if other >= 0 { |
1098 | 0 | *self += other as u32; |
1099 | 0 | } else { |
1100 | 0 | *self -= i32_abs_as_u32(other); |
1101 | 0 | } |
1102 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<i32>>::add_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<i32>>::add_assign |
1103 | | } |
1104 | | |
1105 | | impl Add<i64> for BigInt { |
1106 | | type Output = BigInt; |
1107 | | |
1108 | | #[inline] |
1109 | 0 | fn add(self, other: i64) -> BigInt { |
1110 | 0 | if other >= 0 { |
1111 | 0 | self + other as u64 |
1112 | | } else { |
1113 | 0 | self - i64_abs_as_u64(other) |
1114 | | } |
1115 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<i64>>::add Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<i64>>::add |
1116 | | } |
1117 | | impl AddAssign<i64> for BigInt { |
1118 | | #[inline] |
1119 | 0 | fn add_assign(&mut self, other: i64) { |
1120 | 0 | if other >= 0 { |
1121 | 0 | *self += other as u64; |
1122 | 0 | } else { |
1123 | 0 | *self -= i64_abs_as_u64(other); |
1124 | 0 | } |
1125 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<i64>>::add_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<i64>>::add_assign |
1126 | | } |
1127 | | |
1128 | | #[cfg(has_i128)] |
1129 | | impl Add<i128> for BigInt { |
1130 | | type Output = BigInt; |
1131 | | |
1132 | | #[inline] |
1133 | 0 | fn add(self, other: i128) -> BigInt { |
1134 | 0 | if other >= 0 { |
1135 | 0 | self + other as u128 |
1136 | | } else { |
1137 | 0 | self - i128_abs_as_u128(other) |
1138 | | } |
1139 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<i128>>::add Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Add<i128>>::add |
1140 | | } |
1141 | | #[cfg(has_i128)] |
1142 | | impl AddAssign<i128> for BigInt { |
1143 | | #[inline] |
1144 | 0 | fn add_assign(&mut self, other: i128) { |
1145 | 0 | if other >= 0 { |
1146 | 0 | *self += other as u128; |
1147 | 0 | } else { |
1148 | 0 | *self -= i128_abs_as_u128(other); |
1149 | 0 | } |
1150 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<i128>>::add_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::AddAssign<i128>>::add_assign |
1151 | | } |
1152 | | |
1153 | | // We want to forward to BigUint::sub, but it's not clear how that will go until |
1154 | | // we compare both sign and magnitude. So we duplicate this body for every |
1155 | | // val/ref combination, deferring that decision to BigUint's own forwarding. |
1156 | | macro_rules! bigint_sub { |
1157 | | ($a:expr, $a_owned:expr, $a_data:expr, $b:expr, $b_owned:expr, $b_data:expr) => { |
1158 | | match ($a.sign, $b.sign) { |
1159 | | (_, NoSign) => $a_owned, |
1160 | | (NoSign, _) => -$b_owned, |
1161 | | // opposite signs => keep the sign of the left with the sum of magnitudes |
1162 | | (Plus, Minus) | (Minus, Plus) => BigInt::from_biguint($a.sign, $a_data + $b_data), |
1163 | | // same sign => keep or toggle the sign of the left with the difference of magnitudes |
1164 | | (Plus, Plus) | (Minus, Minus) => match $a.data.cmp(&$b.data) { |
1165 | | Less => BigInt::from_biguint(-$a.sign, $b_data - $a_data), |
1166 | | Greater => BigInt::from_biguint($a.sign, $a_data - $b_data), |
1167 | | Equal => Zero::zero(), |
1168 | | }, |
1169 | | } |
1170 | | }; |
1171 | | } |
1172 | | |
1173 | | impl<'a, 'b> Sub<&'b BigInt> for &'a BigInt { |
1174 | | type Output = BigInt; |
1175 | | |
1176 | | #[inline] |
1177 | 0 | fn sub(self, other: &BigInt) -> BigInt { |
1178 | 0 | bigint_sub!( |
1179 | | self, |
1180 | 0 | self.clone(), |
1181 | 0 | &self.data, |
1182 | | other, |
1183 | 0 | other.clone(), |
1184 | 0 | &other.data |
1185 | | ) |
1186 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Sub>::sub Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Sub>::sub |
1187 | | } |
1188 | | |
1189 | | impl<'a> Sub<BigInt> for &'a BigInt { |
1190 | | type Output = BigInt; |
1191 | | |
1192 | | #[inline] |
1193 | 0 | fn sub(self, other: BigInt) -> BigInt { |
1194 | 0 | bigint_sub!(self, self.clone(), &self.data, other, other, other.data) |
1195 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub |
1196 | | } |
1197 | | |
1198 | | impl<'a> Sub<&'a BigInt> for BigInt { |
1199 | | type Output = BigInt; |
1200 | | |
1201 | | #[inline] |
1202 | 0 | fn sub(self, other: &BigInt) -> BigInt { |
1203 | 0 | bigint_sub!(self, self, self.data, other, other.clone(), &other.data) |
1204 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<&num_bigint::bigint::BigInt>>::sub Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<&num_bigint::bigint::BigInt>>::sub |
1205 | | } |
1206 | | |
1207 | | impl Sub<BigInt> for BigInt { |
1208 | | type Output = BigInt; |
1209 | | |
1210 | | #[inline] |
1211 | 0 | fn sub(self, other: BigInt) -> BigInt { |
1212 | 0 | bigint_sub!(self, self, self.data, other, other, other.data) |
1213 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub>::sub Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub>::sub |
1214 | | } |
1215 | | |
1216 | | impl<'a> SubAssign<&'a BigInt> for BigInt { |
1217 | | #[inline] |
1218 | 0 | fn sub_assign(&mut self, other: &BigInt) { |
1219 | 0 | let n = mem::replace(self, BigInt::zero()); |
1220 | 0 | *self = n - other; |
1221 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<&num_bigint::bigint::BigInt>>::sub_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<&num_bigint::bigint::BigInt>>::sub_assign |
1222 | | } |
1223 | | forward_val_assign!(impl SubAssign for BigInt, sub_assign); |
1224 | | |
1225 | | promote_all_scalars!(impl Sub for BigInt, sub); |
1226 | | promote_all_scalars_assign!(impl SubAssign for BigInt, sub_assign); |
1227 | | forward_all_scalar_binop_to_val_val!(impl Sub<u32> for BigInt, sub); |
1228 | | forward_all_scalar_binop_to_val_val!(impl Sub<u64> for BigInt, sub); |
1229 | | #[cfg(has_i128)] |
1230 | | forward_all_scalar_binop_to_val_val!(impl Sub<u128> for BigInt, sub); |
1231 | | |
1232 | | impl Sub<u32> for BigInt { |
1233 | | type Output = BigInt; |
1234 | | |
1235 | | #[inline] |
1236 | 0 | fn sub(self, other: u32) -> BigInt { |
1237 | 0 | match self.sign { |
1238 | 0 | NoSign => BigInt::from_biguint(Minus, From::from(other)), |
1239 | 0 | Minus => BigInt::from_biguint(Minus, self.data + other), |
1240 | 0 | Plus => match self.data.cmp(&From::from(other)) { |
1241 | 0 | Equal => Zero::zero(), |
1242 | 0 | Greater => BigInt::from_biguint(Plus, self.data - other), |
1243 | 0 | Less => BigInt::from_biguint(Minus, other - self.data), |
1244 | | }, |
1245 | | } |
1246 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<u32>>::sub Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<u32>>::sub |
1247 | | } |
1248 | | impl SubAssign<u32> for BigInt { |
1249 | | #[inline] |
1250 | 0 | fn sub_assign(&mut self, other: u32) { |
1251 | 0 | let n = mem::replace(self, BigInt::zero()); |
1252 | 0 | *self = n - other; |
1253 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<u32>>::sub_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<u32>>::sub_assign |
1254 | | } |
1255 | | |
1256 | | impl Sub<BigInt> for u32 { |
1257 | | type Output = BigInt; |
1258 | | |
1259 | | #[inline] |
1260 | 0 | fn sub(self, other: BigInt) -> BigInt { |
1261 | 0 | -(other - self) |
1262 | 0 | } Unexecuted instantiation: <u32 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub Unexecuted instantiation: <u32 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub |
1263 | | } |
1264 | | |
1265 | | impl Sub<BigInt> for u64 { |
1266 | | type Output = BigInt; |
1267 | | |
1268 | | #[inline] |
1269 | 0 | fn sub(self, other: BigInt) -> BigInt { |
1270 | 0 | -(other - self) |
1271 | 0 | } Unexecuted instantiation: <u64 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub Unexecuted instantiation: <u64 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub |
1272 | | } |
1273 | | #[cfg(has_i128)] |
1274 | | impl Sub<BigInt> for u128 { |
1275 | | type Output = BigInt; |
1276 | | |
1277 | | #[inline] |
1278 | 0 | fn sub(self, other: BigInt) -> BigInt { |
1279 | 0 | -(other - self) |
1280 | 0 | } Unexecuted instantiation: <u128 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub Unexecuted instantiation: <u128 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub |
1281 | | } |
1282 | | |
1283 | | impl Sub<u64> for BigInt { |
1284 | | type Output = BigInt; |
1285 | | |
1286 | | #[inline] |
1287 | 0 | fn sub(self, other: u64) -> BigInt { |
1288 | 0 | match self.sign { |
1289 | 0 | NoSign => BigInt::from_biguint(Minus, From::from(other)), |
1290 | 0 | Minus => BigInt::from_biguint(Minus, self.data + other), |
1291 | 0 | Plus => match self.data.cmp(&From::from(other)) { |
1292 | 0 | Equal => Zero::zero(), |
1293 | 0 | Greater => BigInt::from_biguint(Plus, self.data - other), |
1294 | 0 | Less => BigInt::from_biguint(Minus, other - self.data), |
1295 | | }, |
1296 | | } |
1297 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<u64>>::sub Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<u64>>::sub |
1298 | | } |
1299 | | impl SubAssign<u64> for BigInt { |
1300 | | #[inline] |
1301 | 0 | fn sub_assign(&mut self, other: u64) { |
1302 | 0 | let n = mem::replace(self, BigInt::zero()); |
1303 | 0 | *self = n - other; |
1304 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<u64>>::sub_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<u64>>::sub_assign |
1305 | | } |
1306 | | |
1307 | | #[cfg(has_i128)] |
1308 | | impl Sub<u128> for BigInt { |
1309 | | type Output = BigInt; |
1310 | | |
1311 | | #[inline] |
1312 | 0 | fn sub(self, other: u128) -> BigInt { |
1313 | 0 | match self.sign { |
1314 | 0 | NoSign => BigInt::from_biguint(Minus, From::from(other)), |
1315 | 0 | Minus => BigInt::from_biguint(Minus, self.data + other), |
1316 | 0 | Plus => match self.data.cmp(&From::from(other)) { |
1317 | 0 | Equal => Zero::zero(), |
1318 | 0 | Greater => BigInt::from_biguint(Plus, self.data - other), |
1319 | 0 | Less => BigInt::from_biguint(Minus, other - self.data), |
1320 | | }, |
1321 | | } |
1322 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<u128>>::sub Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<u128>>::sub |
1323 | | } |
1324 | | #[cfg(has_i128)] |
1325 | | impl SubAssign<u128> for BigInt { |
1326 | | #[inline] |
1327 | 0 | fn sub_assign(&mut self, other: u128) { |
1328 | 0 | let n = mem::replace(self, BigInt::zero()); |
1329 | 0 | *self = n - other; |
1330 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<u128>>::sub_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<u128>>::sub_assign |
1331 | | } |
1332 | | |
1333 | | forward_all_scalar_binop_to_val_val!(impl Sub<i32> for BigInt, sub); |
1334 | | forward_all_scalar_binop_to_val_val!(impl Sub<i64> for BigInt, sub); |
1335 | | #[cfg(has_i128)] |
1336 | | forward_all_scalar_binop_to_val_val!(impl Sub<i128> for BigInt, sub); |
1337 | | |
1338 | | impl Sub<i32> for BigInt { |
1339 | | type Output = BigInt; |
1340 | | |
1341 | | #[inline] |
1342 | 0 | fn sub(self, other: i32) -> BigInt { |
1343 | 0 | if other >= 0 { |
1344 | 0 | self - other as u32 |
1345 | | } else { |
1346 | 0 | self + i32_abs_as_u32(other) |
1347 | | } |
1348 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<i32>>::sub Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<i32>>::sub |
1349 | | } |
1350 | | impl SubAssign<i32> for BigInt { |
1351 | | #[inline] |
1352 | 0 | fn sub_assign(&mut self, other: i32) { |
1353 | 0 | if other >= 0 { |
1354 | 0 | *self -= other as u32; |
1355 | 0 | } else { |
1356 | 0 | *self += i32_abs_as_u32(other); |
1357 | 0 | } |
1358 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<i32>>::sub_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<i32>>::sub_assign |
1359 | | } |
1360 | | |
1361 | | impl Sub<BigInt> for i32 { |
1362 | | type Output = BigInt; |
1363 | | |
1364 | | #[inline] |
1365 | 0 | fn sub(self, other: BigInt) -> BigInt { |
1366 | 0 | if self >= 0 { |
1367 | 0 | self as u32 - other |
1368 | | } else { |
1369 | 0 | -other - i32_abs_as_u32(self) |
1370 | | } |
1371 | 0 | } Unexecuted instantiation: <i32 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub Unexecuted instantiation: <i32 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub |
1372 | | } |
1373 | | |
1374 | | impl Sub<i64> for BigInt { |
1375 | | type Output = BigInt; |
1376 | | |
1377 | | #[inline] |
1378 | 0 | fn sub(self, other: i64) -> BigInt { |
1379 | 0 | if other >= 0 { |
1380 | 0 | self - other as u64 |
1381 | | } else { |
1382 | 0 | self + i64_abs_as_u64(other) |
1383 | | } |
1384 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<i64>>::sub Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<i64>>::sub |
1385 | | } |
1386 | | impl SubAssign<i64> for BigInt { |
1387 | | #[inline] |
1388 | 0 | fn sub_assign(&mut self, other: i64) { |
1389 | 0 | if other >= 0 { |
1390 | 0 | *self -= other as u64; |
1391 | 0 | } else { |
1392 | 0 | *self += i64_abs_as_u64(other); |
1393 | 0 | } |
1394 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<i64>>::sub_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<i64>>::sub_assign |
1395 | | } |
1396 | | |
1397 | | impl Sub<BigInt> for i64 { |
1398 | | type Output = BigInt; |
1399 | | |
1400 | | #[inline] |
1401 | 0 | fn sub(self, other: BigInt) -> BigInt { |
1402 | 0 | if self >= 0 { |
1403 | 0 | self as u64 - other |
1404 | | } else { |
1405 | 0 | -other - i64_abs_as_u64(self) |
1406 | | } |
1407 | 0 | } Unexecuted instantiation: <i64 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub Unexecuted instantiation: <i64 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub |
1408 | | } |
1409 | | |
1410 | | #[cfg(has_i128)] |
1411 | | impl Sub<i128> for BigInt { |
1412 | | type Output = BigInt; |
1413 | | |
1414 | | #[inline] |
1415 | 0 | fn sub(self, other: i128) -> BigInt { |
1416 | 0 | if other >= 0 { |
1417 | 0 | self - other as u128 |
1418 | | } else { |
1419 | 0 | self + i128_abs_as_u128(other) |
1420 | | } |
1421 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<i128>>::sub Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Sub<i128>>::sub |
1422 | | } |
1423 | | #[cfg(has_i128)] |
1424 | | impl SubAssign<i128> for BigInt { |
1425 | | #[inline] |
1426 | 0 | fn sub_assign(&mut self, other: i128) { |
1427 | 0 | if other >= 0 { |
1428 | 0 | *self -= other as u128; |
1429 | 0 | } else { |
1430 | 0 | *self += i128_abs_as_u128(other); |
1431 | 0 | } |
1432 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<i128>>::sub_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::SubAssign<i128>>::sub_assign |
1433 | | } |
1434 | | #[cfg(has_i128)] |
1435 | | impl Sub<BigInt> for i128 { |
1436 | | type Output = BigInt; |
1437 | | |
1438 | | #[inline] |
1439 | 0 | fn sub(self, other: BigInt) -> BigInt { |
1440 | 0 | if self >= 0 { |
1441 | 0 | self as u128 - other |
1442 | | } else { |
1443 | 0 | -other - i128_abs_as_u128(self) |
1444 | | } |
1445 | 0 | } Unexecuted instantiation: <i128 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub Unexecuted instantiation: <i128 as core::ops::arith::Sub<num_bigint::bigint::BigInt>>::sub |
1446 | | } |
1447 | | |
1448 | | forward_all_binop_to_ref_ref!(impl Mul for BigInt, mul); |
1449 | | |
1450 | | impl<'a, 'b> Mul<&'b BigInt> for &'a BigInt { |
1451 | | type Output = BigInt; |
1452 | | |
1453 | | #[inline] |
1454 | 0 | fn mul(self, other: &BigInt) -> BigInt { |
1455 | 0 | BigInt::from_biguint(self.sign * other.sign, &self.data * &other.data) |
1456 | 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 |
1457 | | } |
1458 | | |
1459 | | impl<'a> MulAssign<&'a BigInt> for BigInt { |
1460 | | #[inline] |
1461 | 0 | fn mul_assign(&mut self, other: &BigInt) { |
1462 | 0 | *self = &*self * other; |
1463 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<&num_bigint::bigint::BigInt>>::mul_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<&num_bigint::bigint::BigInt>>::mul_assign |
1464 | | } |
1465 | | forward_val_assign!(impl MulAssign for BigInt, mul_assign); |
1466 | | |
1467 | | promote_all_scalars!(impl Mul for BigInt, mul); |
1468 | | promote_all_scalars_assign!(impl MulAssign for BigInt, mul_assign); |
1469 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<u32> for BigInt, mul); |
1470 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<u64> for BigInt, mul); |
1471 | | #[cfg(has_i128)] |
1472 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<u128> for BigInt, mul); |
1473 | | |
1474 | | impl Mul<u32> for BigInt { |
1475 | | type Output = BigInt; |
1476 | | |
1477 | | #[inline] |
1478 | 0 | fn mul(self, other: u32) -> BigInt { |
1479 | 0 | BigInt::from_biguint(self.sign, self.data * other) |
1480 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<u32>>::mul Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<u32>>::mul |
1481 | | } |
1482 | | |
1483 | | impl MulAssign<u32> for BigInt { |
1484 | | #[inline] |
1485 | 0 | fn mul_assign(&mut self, other: u32) { |
1486 | 0 | self.data *= other; |
1487 | 0 | if self.data.is_zero() { |
1488 | 0 | self.sign = NoSign; |
1489 | 0 | } |
1490 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<u32>>::mul_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<u32>>::mul_assign |
1491 | | } |
1492 | | |
1493 | | impl Mul<u64> for BigInt { |
1494 | | type Output = BigInt; |
1495 | | |
1496 | | #[inline] |
1497 | 0 | fn mul(self, other: u64) -> BigInt { |
1498 | 0 | BigInt::from_biguint(self.sign, self.data * other) |
1499 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<u64>>::mul Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<u64>>::mul |
1500 | | } |
1501 | | |
1502 | | impl MulAssign<u64> for BigInt { |
1503 | | #[inline] |
1504 | 0 | fn mul_assign(&mut self, other: u64) { |
1505 | 0 | self.data *= other; |
1506 | 0 | if self.data.is_zero() { |
1507 | 0 | self.sign = NoSign; |
1508 | 0 | } |
1509 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<u64>>::mul_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<u64>>::mul_assign |
1510 | | } |
1511 | | #[cfg(has_i128)] |
1512 | | impl Mul<u128> for BigInt { |
1513 | | type Output = BigInt; |
1514 | | |
1515 | | #[inline] |
1516 | 0 | fn mul(self, other: u128) -> BigInt { |
1517 | 0 | BigInt::from_biguint(self.sign, self.data * other) |
1518 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<u128>>::mul Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<u128>>::mul |
1519 | | } |
1520 | | #[cfg(has_i128)] |
1521 | | impl MulAssign<u128> for BigInt { |
1522 | | #[inline] |
1523 | 0 | fn mul_assign(&mut self, other: u128) { |
1524 | 0 | self.data *= other; |
1525 | 0 | if self.data.is_zero() { |
1526 | 0 | self.sign = NoSign; |
1527 | 0 | } |
1528 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<u128>>::mul_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<u128>>::mul_assign |
1529 | | } |
1530 | | |
1531 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<i32> for BigInt, mul); |
1532 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<i64> for BigInt, mul); |
1533 | | #[cfg(has_i128)] |
1534 | | forward_all_scalar_binop_to_val_val_commutative!(impl Mul<i128> for BigInt, mul); |
1535 | | |
1536 | | impl Mul<i32> for BigInt { |
1537 | | type Output = BigInt; |
1538 | | |
1539 | | #[inline] |
1540 | 0 | fn mul(self, other: i32) -> BigInt { |
1541 | 0 | if other >= 0 { |
1542 | 0 | self * other as u32 |
1543 | | } else { |
1544 | 0 | -(self * i32_abs_as_u32(other)) |
1545 | | } |
1546 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<i32>>::mul Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<i32>>::mul |
1547 | | } |
1548 | | |
1549 | | impl MulAssign<i32> for BigInt { |
1550 | | #[inline] |
1551 | 0 | fn mul_assign(&mut self, other: i32) { |
1552 | 0 | if other >= 0 { |
1553 | 0 | *self *= other as u32; |
1554 | 0 | } else { |
1555 | 0 | self.sign = -self.sign; |
1556 | 0 | *self *= i32_abs_as_u32(other); |
1557 | 0 | } |
1558 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<i32>>::mul_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<i32>>::mul_assign |
1559 | | } |
1560 | | |
1561 | | impl Mul<i64> for BigInt { |
1562 | | type Output = BigInt; |
1563 | | |
1564 | | #[inline] |
1565 | 0 | fn mul(self, other: i64) -> BigInt { |
1566 | 0 | if other >= 0 { |
1567 | 0 | self * other as u64 |
1568 | | } else { |
1569 | 0 | -(self * i64_abs_as_u64(other)) |
1570 | | } |
1571 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<i64>>::mul Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<i64>>::mul |
1572 | | } |
1573 | | |
1574 | | impl MulAssign<i64> for BigInt { |
1575 | | #[inline] |
1576 | 0 | fn mul_assign(&mut self, other: i64) { |
1577 | 0 | if other >= 0 { |
1578 | 0 | *self *= other as u64; |
1579 | 0 | } else { |
1580 | 0 | self.sign = -self.sign; |
1581 | 0 | *self *= i64_abs_as_u64(other); |
1582 | 0 | } |
1583 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<i64>>::mul_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<i64>>::mul_assign |
1584 | | } |
1585 | | #[cfg(has_i128)] |
1586 | | impl Mul<i128> for BigInt { |
1587 | | type Output = BigInt; |
1588 | | |
1589 | | #[inline] |
1590 | 0 | fn mul(self, other: i128) -> BigInt { |
1591 | 0 | if other >= 0 { |
1592 | 0 | self * other as u128 |
1593 | | } else { |
1594 | 0 | -(self * i128_abs_as_u128(other)) |
1595 | | } |
1596 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<i128>>::mul Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Mul<i128>>::mul |
1597 | | } |
1598 | | #[cfg(has_i128)] |
1599 | | impl MulAssign<i128> for BigInt { |
1600 | | #[inline] |
1601 | 0 | fn mul_assign(&mut self, other: i128) { |
1602 | 0 | if other >= 0 { |
1603 | 0 | *self *= other as u128; |
1604 | 0 | } else { |
1605 | 0 | self.sign = -self.sign; |
1606 | 0 | *self *= i128_abs_as_u128(other); |
1607 | 0 | } |
1608 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<i128>>::mul_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::MulAssign<i128>>::mul_assign |
1609 | | } |
1610 | | |
1611 | | forward_all_binop_to_ref_ref!(impl Div for BigInt, div); |
1612 | | |
1613 | | impl<'a, 'b> Div<&'b BigInt> for &'a BigInt { |
1614 | | type Output = BigInt; |
1615 | | |
1616 | | #[inline] |
1617 | 0 | fn div(self, other: &BigInt) -> BigInt { |
1618 | 0 | let (q, _) = self.div_rem(other); |
1619 | 0 | q |
1620 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Div>::div Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Div>::div |
1621 | | } |
1622 | | |
1623 | | impl<'a> DivAssign<&'a BigInt> for BigInt { |
1624 | | #[inline] |
1625 | 0 | fn div_assign(&mut self, other: &BigInt) { |
1626 | 0 | *self = &*self / other; |
1627 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<&num_bigint::bigint::BigInt>>::div_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<&num_bigint::bigint::BigInt>>::div_assign |
1628 | | } |
1629 | | forward_val_assign!(impl DivAssign for BigInt, div_assign); |
1630 | | |
1631 | | promote_all_scalars!(impl Div for BigInt, div); |
1632 | | promote_all_scalars_assign!(impl DivAssign for BigInt, div_assign); |
1633 | | forward_all_scalar_binop_to_val_val!(impl Div<u32> for BigInt, div); |
1634 | | forward_all_scalar_binop_to_val_val!(impl Div<u64> for BigInt, div); |
1635 | | #[cfg(has_i128)] |
1636 | | forward_all_scalar_binop_to_val_val!(impl Div<u128> for BigInt, div); |
1637 | | |
1638 | | impl Div<u32> for BigInt { |
1639 | | type Output = BigInt; |
1640 | | |
1641 | | #[inline] |
1642 | 0 | fn div(self, other: u32) -> BigInt { |
1643 | 0 | BigInt::from_biguint(self.sign, self.data / other) |
1644 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<u32>>::div Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<u32>>::div |
1645 | | } |
1646 | | |
1647 | | impl DivAssign<u32> for BigInt { |
1648 | | #[inline] |
1649 | 0 | fn div_assign(&mut self, other: u32) { |
1650 | 0 | self.data /= other; |
1651 | 0 | if self.data.is_zero() { |
1652 | 0 | self.sign = NoSign; |
1653 | 0 | } |
1654 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<u32>>::div_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<u32>>::div_assign |
1655 | | } |
1656 | | |
1657 | | impl Div<BigInt> for u32 { |
1658 | | type Output = BigInt; |
1659 | | |
1660 | | #[inline] |
1661 | 0 | fn div(self, other: BigInt) -> BigInt { |
1662 | 0 | BigInt::from_biguint(other.sign, self / other.data) |
1663 | 0 | } Unexecuted instantiation: <u32 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div Unexecuted instantiation: <u32 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div |
1664 | | } |
1665 | | |
1666 | | impl Div<u64> for BigInt { |
1667 | | type Output = BigInt; |
1668 | | |
1669 | | #[inline] |
1670 | 0 | fn div(self, other: u64) -> BigInt { |
1671 | 0 | BigInt::from_biguint(self.sign, self.data / other) |
1672 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<u64>>::div Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<u64>>::div |
1673 | | } |
1674 | | |
1675 | | impl DivAssign<u64> for BigInt { |
1676 | | #[inline] |
1677 | 0 | fn div_assign(&mut self, other: u64) { |
1678 | 0 | self.data /= other; |
1679 | 0 | if self.data.is_zero() { |
1680 | 0 | self.sign = NoSign; |
1681 | 0 | } |
1682 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<u64>>::div_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<u64>>::div_assign |
1683 | | } |
1684 | | |
1685 | | impl Div<BigInt> for u64 { |
1686 | | type Output = BigInt; |
1687 | | |
1688 | | #[inline] |
1689 | 0 | fn div(self, other: BigInt) -> BigInt { |
1690 | 0 | BigInt::from_biguint(other.sign, self / other.data) |
1691 | 0 | } Unexecuted instantiation: <u64 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div Unexecuted instantiation: <u64 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div |
1692 | | } |
1693 | | |
1694 | | #[cfg(has_i128)] |
1695 | | impl Div<u128> for BigInt { |
1696 | | type Output = BigInt; |
1697 | | |
1698 | | #[inline] |
1699 | 0 | fn div(self, other: u128) -> BigInt { |
1700 | 0 | BigInt::from_biguint(self.sign, self.data / other) |
1701 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<u128>>::div Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<u128>>::div |
1702 | | } |
1703 | | |
1704 | | #[cfg(has_i128)] |
1705 | | impl DivAssign<u128> for BigInt { |
1706 | | #[inline] |
1707 | 0 | fn div_assign(&mut self, other: u128) { |
1708 | 0 | self.data /= other; |
1709 | 0 | if self.data.is_zero() { |
1710 | 0 | self.sign = NoSign; |
1711 | 0 | } |
1712 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<u128>>::div_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<u128>>::div_assign |
1713 | | } |
1714 | | |
1715 | | #[cfg(has_i128)] |
1716 | | impl Div<BigInt> for u128 { |
1717 | | type Output = BigInt; |
1718 | | |
1719 | | #[inline] |
1720 | 0 | fn div(self, other: BigInt) -> BigInt { |
1721 | 0 | BigInt::from_biguint(other.sign, self / other.data) |
1722 | 0 | } Unexecuted instantiation: <u128 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div Unexecuted instantiation: <u128 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div |
1723 | | } |
1724 | | |
1725 | | forward_all_scalar_binop_to_val_val!(impl Div<i32> for BigInt, div); |
1726 | | forward_all_scalar_binop_to_val_val!(impl Div<i64> for BigInt, div); |
1727 | | #[cfg(has_i128)] |
1728 | | forward_all_scalar_binop_to_val_val!(impl Div<i128> for BigInt, div); |
1729 | | |
1730 | | impl Div<i32> for BigInt { |
1731 | | type Output = BigInt; |
1732 | | |
1733 | | #[inline] |
1734 | 0 | fn div(self, other: i32) -> BigInt { |
1735 | 0 | if other >= 0 { |
1736 | 0 | self / other as u32 |
1737 | | } else { |
1738 | 0 | -(self / i32_abs_as_u32(other)) |
1739 | | } |
1740 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<i32>>::div Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<i32>>::div |
1741 | | } |
1742 | | |
1743 | | impl DivAssign<i32> for BigInt { |
1744 | | #[inline] |
1745 | 0 | fn div_assign(&mut self, other: i32) { |
1746 | 0 | if other >= 0 { |
1747 | 0 | *self /= other as u32; |
1748 | 0 | } else { |
1749 | 0 | self.sign = -self.sign; |
1750 | 0 | *self /= i32_abs_as_u32(other); |
1751 | 0 | } |
1752 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<i32>>::div_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<i32>>::div_assign |
1753 | | } |
1754 | | |
1755 | | impl Div<BigInt> for i32 { |
1756 | | type Output = BigInt; |
1757 | | |
1758 | | #[inline] |
1759 | 0 | fn div(self, other: BigInt) -> BigInt { |
1760 | 0 | if self >= 0 { |
1761 | 0 | self as u32 / other |
1762 | | } else { |
1763 | 0 | -(i32_abs_as_u32(self) / other) |
1764 | | } |
1765 | 0 | } Unexecuted instantiation: <i32 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div Unexecuted instantiation: <i32 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div |
1766 | | } |
1767 | | |
1768 | | impl Div<i64> for BigInt { |
1769 | | type Output = BigInt; |
1770 | | |
1771 | | #[inline] |
1772 | 0 | fn div(self, other: i64) -> BigInt { |
1773 | 0 | if other >= 0 { |
1774 | 0 | self / other as u64 |
1775 | | } else { |
1776 | 0 | -(self / i64_abs_as_u64(other)) |
1777 | | } |
1778 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<i64>>::div Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<i64>>::div |
1779 | | } |
1780 | | |
1781 | | impl DivAssign<i64> for BigInt { |
1782 | | #[inline] |
1783 | 0 | fn div_assign(&mut self, other: i64) { |
1784 | 0 | if other >= 0 { |
1785 | 0 | *self /= other as u64; |
1786 | 0 | } else { |
1787 | 0 | self.sign = -self.sign; |
1788 | 0 | *self /= i64_abs_as_u64(other); |
1789 | 0 | } |
1790 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<i64>>::div_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<i64>>::div_assign |
1791 | | } |
1792 | | |
1793 | | impl Div<BigInt> for i64 { |
1794 | | type Output = BigInt; |
1795 | | |
1796 | | #[inline] |
1797 | 0 | fn div(self, other: BigInt) -> BigInt { |
1798 | 0 | if self >= 0 { |
1799 | 0 | self as u64 / other |
1800 | | } else { |
1801 | 0 | -(i64_abs_as_u64(self) / other) |
1802 | | } |
1803 | 0 | } Unexecuted instantiation: <i64 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div Unexecuted instantiation: <i64 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div |
1804 | | } |
1805 | | |
1806 | | #[cfg(has_i128)] |
1807 | | impl Div<i128> for BigInt { |
1808 | | type Output = BigInt; |
1809 | | |
1810 | | #[inline] |
1811 | 0 | fn div(self, other: i128) -> BigInt { |
1812 | 0 | if other >= 0 { |
1813 | 0 | self / other as u128 |
1814 | | } else { |
1815 | 0 | -(self / i128_abs_as_u128(other)) |
1816 | | } |
1817 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<i128>>::div Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Div<i128>>::div |
1818 | | } |
1819 | | |
1820 | | #[cfg(has_i128)] |
1821 | | impl DivAssign<i128> for BigInt { |
1822 | | #[inline] |
1823 | 0 | fn div_assign(&mut self, other: i128) { |
1824 | 0 | if other >= 0 { |
1825 | 0 | *self /= other as u128; |
1826 | 0 | } else { |
1827 | 0 | self.sign = -self.sign; |
1828 | 0 | *self /= i128_abs_as_u128(other); |
1829 | 0 | } |
1830 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<i128>>::div_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::DivAssign<i128>>::div_assign |
1831 | | } |
1832 | | |
1833 | | #[cfg(has_i128)] |
1834 | | impl Div<BigInt> for i128 { |
1835 | | type Output = BigInt; |
1836 | | |
1837 | | #[inline] |
1838 | 0 | fn div(self, other: BigInt) -> BigInt { |
1839 | 0 | if self >= 0 { |
1840 | 0 | self as u128 / other |
1841 | | } else { |
1842 | 0 | -(i128_abs_as_u128(self) / other) |
1843 | | } |
1844 | 0 | } Unexecuted instantiation: <i128 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div Unexecuted instantiation: <i128 as core::ops::arith::Div<num_bigint::bigint::BigInt>>::div |
1845 | | } |
1846 | | |
1847 | | forward_all_binop_to_ref_ref!(impl Rem for BigInt, rem); |
1848 | | |
1849 | | impl<'a, 'b> Rem<&'b BigInt> for &'a BigInt { |
1850 | | type Output = BigInt; |
1851 | | |
1852 | | #[inline] |
1853 | 0 | fn rem(self, other: &BigInt) -> BigInt { |
1854 | 0 | let (_, r) = self.div_rem(other); |
1855 | 0 | r |
1856 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Rem>::rem Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Rem>::rem |
1857 | | } |
1858 | | |
1859 | | impl<'a> RemAssign<&'a BigInt> for BigInt { |
1860 | | #[inline] |
1861 | 0 | fn rem_assign(&mut self, other: &BigInt) { |
1862 | 0 | *self = &*self % other; |
1863 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<&num_bigint::bigint::BigInt>>::rem_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<&num_bigint::bigint::BigInt>>::rem_assign |
1864 | | } |
1865 | | forward_val_assign!(impl RemAssign for BigInt, rem_assign); |
1866 | | |
1867 | | promote_all_scalars!(impl Rem for BigInt, rem); |
1868 | | promote_all_scalars_assign!(impl RemAssign for BigInt, rem_assign); |
1869 | | forward_all_scalar_binop_to_val_val!(impl Rem<u32> for BigInt, rem); |
1870 | | forward_all_scalar_binop_to_val_val!(impl Rem<u64> for BigInt, rem); |
1871 | | #[cfg(has_i128)] |
1872 | | forward_all_scalar_binop_to_val_val!(impl Rem<u128> for BigInt, rem); |
1873 | | |
1874 | | impl Rem<u32> for BigInt { |
1875 | | type Output = BigInt; |
1876 | | |
1877 | | #[inline] |
1878 | 0 | fn rem(self, other: u32) -> BigInt { |
1879 | 0 | BigInt::from_biguint(self.sign, self.data % other) |
1880 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<u32>>::rem Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<u32>>::rem |
1881 | | } |
1882 | | |
1883 | | impl RemAssign<u32> for BigInt { |
1884 | | #[inline] |
1885 | 0 | fn rem_assign(&mut self, other: u32) { |
1886 | 0 | self.data %= other; |
1887 | 0 | if self.data.is_zero() { |
1888 | 0 | self.sign = NoSign; |
1889 | 0 | } |
1890 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<u32>>::rem_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<u32>>::rem_assign |
1891 | | } |
1892 | | |
1893 | | impl Rem<BigInt> for u32 { |
1894 | | type Output = BigInt; |
1895 | | |
1896 | | #[inline] |
1897 | 0 | fn rem(self, other: BigInt) -> BigInt { |
1898 | 0 | BigInt::from_biguint(Plus, self % other.data) |
1899 | 0 | } Unexecuted instantiation: <u32 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem Unexecuted instantiation: <u32 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem |
1900 | | } |
1901 | | |
1902 | | impl Rem<u64> for BigInt { |
1903 | | type Output = BigInt; |
1904 | | |
1905 | | #[inline] |
1906 | 0 | fn rem(self, other: u64) -> BigInt { |
1907 | 0 | BigInt::from_biguint(self.sign, self.data % other) |
1908 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<u64>>::rem Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<u64>>::rem |
1909 | | } |
1910 | | |
1911 | | impl RemAssign<u64> for BigInt { |
1912 | | #[inline] |
1913 | 0 | fn rem_assign(&mut self, other: u64) { |
1914 | 0 | self.data %= other; |
1915 | 0 | if self.data.is_zero() { |
1916 | 0 | self.sign = NoSign; |
1917 | 0 | } |
1918 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<u64>>::rem_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<u64>>::rem_assign |
1919 | | } |
1920 | | |
1921 | | impl Rem<BigInt> for u64 { |
1922 | | type Output = BigInt; |
1923 | | |
1924 | | #[inline] |
1925 | 0 | fn rem(self, other: BigInt) -> BigInt { |
1926 | 0 | BigInt::from_biguint(Plus, self % other.data) |
1927 | 0 | } Unexecuted instantiation: <u64 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem Unexecuted instantiation: <u64 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem |
1928 | | } |
1929 | | |
1930 | | #[cfg(has_i128)] |
1931 | | impl Rem<u128> for BigInt { |
1932 | | type Output = BigInt; |
1933 | | |
1934 | | #[inline] |
1935 | 0 | fn rem(self, other: u128) -> BigInt { |
1936 | 0 | BigInt::from_biguint(self.sign, self.data % other) |
1937 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<u128>>::rem Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<u128>>::rem |
1938 | | } |
1939 | | |
1940 | | #[cfg(has_i128)] |
1941 | | impl RemAssign<u128> for BigInt { |
1942 | | #[inline] |
1943 | 0 | fn rem_assign(&mut self, other: u128) { |
1944 | 0 | self.data %= other; |
1945 | 0 | if self.data.is_zero() { |
1946 | 0 | self.sign = NoSign; |
1947 | 0 | } |
1948 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<u128>>::rem_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<u128>>::rem_assign |
1949 | | } |
1950 | | |
1951 | | #[cfg(has_i128)] |
1952 | | impl Rem<BigInt> for u128 { |
1953 | | type Output = BigInt; |
1954 | | |
1955 | | #[inline] |
1956 | 0 | fn rem(self, other: BigInt) -> BigInt { |
1957 | 0 | BigInt::from_biguint(Plus, self % other.data) |
1958 | 0 | } Unexecuted instantiation: <u128 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem Unexecuted instantiation: <u128 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem |
1959 | | } |
1960 | | |
1961 | | forward_all_scalar_binop_to_val_val!(impl Rem<i32> for BigInt, rem); |
1962 | | forward_all_scalar_binop_to_val_val!(impl Rem<i64> for BigInt, rem); |
1963 | | #[cfg(has_i128)] |
1964 | | forward_all_scalar_binop_to_val_val!(impl Rem<i128> for BigInt, rem); |
1965 | | |
1966 | | impl Rem<i32> for BigInt { |
1967 | | type Output = BigInt; |
1968 | | |
1969 | | #[inline] |
1970 | 0 | fn rem(self, other: i32) -> BigInt { |
1971 | 0 | if other >= 0 { |
1972 | 0 | self % other as u32 |
1973 | | } else { |
1974 | 0 | self % i32_abs_as_u32(other) |
1975 | | } |
1976 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<i32>>::rem Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<i32>>::rem |
1977 | | } |
1978 | | |
1979 | | impl RemAssign<i32> for BigInt { |
1980 | | #[inline] |
1981 | 0 | fn rem_assign(&mut self, other: i32) { |
1982 | 0 | if other >= 0 { |
1983 | 0 | *self %= other as u32; |
1984 | 0 | } else { |
1985 | 0 | *self %= i32_abs_as_u32(other); |
1986 | 0 | } |
1987 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<i32>>::rem_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<i32>>::rem_assign |
1988 | | } |
1989 | | |
1990 | | impl Rem<BigInt> for i32 { |
1991 | | type Output = BigInt; |
1992 | | |
1993 | | #[inline] |
1994 | 0 | fn rem(self, other: BigInt) -> BigInt { |
1995 | 0 | if self >= 0 { |
1996 | 0 | self as u32 % other |
1997 | | } else { |
1998 | 0 | -(i32_abs_as_u32(self) % other) |
1999 | | } |
2000 | 0 | } Unexecuted instantiation: <i32 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem Unexecuted instantiation: <i32 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem |
2001 | | } |
2002 | | |
2003 | | impl Rem<i64> for BigInt { |
2004 | | type Output = BigInt; |
2005 | | |
2006 | | #[inline] |
2007 | 0 | fn rem(self, other: i64) -> BigInt { |
2008 | 0 | if other >= 0 { |
2009 | 0 | self % other as u64 |
2010 | | } else { |
2011 | 0 | self % i64_abs_as_u64(other) |
2012 | | } |
2013 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<i64>>::rem Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<i64>>::rem |
2014 | | } |
2015 | | |
2016 | | impl RemAssign<i64> for BigInt { |
2017 | | #[inline] |
2018 | 0 | fn rem_assign(&mut self, other: i64) { |
2019 | 0 | if other >= 0 { |
2020 | 0 | *self %= other as u64; |
2021 | 0 | } else { |
2022 | 0 | *self %= i64_abs_as_u64(other); |
2023 | 0 | } |
2024 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<i64>>::rem_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<i64>>::rem_assign |
2025 | | } |
2026 | | |
2027 | | impl Rem<BigInt> for i64 { |
2028 | | type Output = BigInt; |
2029 | | |
2030 | | #[inline] |
2031 | 0 | fn rem(self, other: BigInt) -> BigInt { |
2032 | 0 | if self >= 0 { |
2033 | 0 | self as u64 % other |
2034 | | } else { |
2035 | 0 | -(i64_abs_as_u64(self) % other) |
2036 | | } |
2037 | 0 | } Unexecuted instantiation: <i64 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem Unexecuted instantiation: <i64 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem |
2038 | | } |
2039 | | |
2040 | | #[cfg(has_i128)] |
2041 | | impl Rem<i128> for BigInt { |
2042 | | type Output = BigInt; |
2043 | | |
2044 | | #[inline] |
2045 | 0 | fn rem(self, other: i128) -> BigInt { |
2046 | 0 | if other >= 0 { |
2047 | 0 | self % other as u128 |
2048 | | } else { |
2049 | 0 | self % i128_abs_as_u128(other) |
2050 | | } |
2051 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<i128>>::rem Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Rem<i128>>::rem |
2052 | | } |
2053 | | #[cfg(has_i128)] |
2054 | | impl RemAssign<i128> for BigInt { |
2055 | | #[inline] |
2056 | 0 | fn rem_assign(&mut self, other: i128) { |
2057 | 0 | if other >= 0 { |
2058 | 0 | *self %= other as u128; |
2059 | 0 | } else { |
2060 | 0 | *self %= i128_abs_as_u128(other); |
2061 | 0 | } |
2062 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<i128>>::rem_assign Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::RemAssign<i128>>::rem_assign |
2063 | | } |
2064 | | #[cfg(has_i128)] |
2065 | | impl Rem<BigInt> for i128 { |
2066 | | type Output = BigInt; |
2067 | | |
2068 | | #[inline] |
2069 | 0 | fn rem(self, other: BigInt) -> BigInt { |
2070 | 0 | if self >= 0 { |
2071 | 0 | self as u128 % other |
2072 | | } else { |
2073 | 0 | -(i128_abs_as_u128(self) % other) |
2074 | | } |
2075 | 0 | } Unexecuted instantiation: <i128 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem Unexecuted instantiation: <i128 as core::ops::arith::Rem<num_bigint::bigint::BigInt>>::rem |
2076 | | } |
2077 | | |
2078 | | impl Neg for BigInt { |
2079 | | type Output = BigInt; |
2080 | | |
2081 | | #[inline] |
2082 | 0 | fn neg(mut self) -> BigInt { |
2083 | 0 | self.sign = -self.sign; |
2084 | 0 | self |
2085 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Neg>::neg Unexecuted instantiation: <num_bigint::bigint::BigInt as core::ops::arith::Neg>::neg |
2086 | | } |
2087 | | |
2088 | | impl<'a> Neg for &'a BigInt { |
2089 | | type Output = BigInt; |
2090 | | |
2091 | | #[inline] |
2092 | 0 | fn neg(self) -> BigInt { |
2093 | 0 | -self.clone() |
2094 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Neg>::neg Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Neg>::neg |
2095 | | } |
2096 | | |
2097 | | impl CheckedAdd for BigInt { |
2098 | | #[inline] |
2099 | 0 | fn checked_add(&self, v: &BigInt) -> Option<BigInt> { |
2100 | 0 | Some(self.add(v)) |
2101 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::ops::checked::CheckedAdd>::checked_add Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::ops::checked::CheckedAdd>::checked_add |
2102 | | } |
2103 | | |
2104 | | impl CheckedSub for BigInt { |
2105 | | #[inline] |
2106 | 0 | fn checked_sub(&self, v: &BigInt) -> Option<BigInt> { |
2107 | 0 | Some(self.sub(v)) |
2108 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::ops::checked::CheckedSub>::checked_sub Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::ops::checked::CheckedSub>::checked_sub |
2109 | | } |
2110 | | |
2111 | | impl CheckedMul for BigInt { |
2112 | | #[inline] |
2113 | 0 | fn checked_mul(&self, v: &BigInt) -> Option<BigInt> { |
2114 | 0 | Some(self.mul(v)) |
2115 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::ops::checked::CheckedMul>::checked_mul Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::ops::checked::CheckedMul>::checked_mul |
2116 | | } |
2117 | | |
2118 | | impl CheckedDiv for BigInt { |
2119 | | #[inline] |
2120 | 0 | fn checked_div(&self, v: &BigInt) -> Option<BigInt> { |
2121 | 0 | if v.is_zero() { |
2122 | 0 | return None; |
2123 | 0 | } |
2124 | 0 | Some(self.div(v)) |
2125 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::ops::checked::CheckedDiv>::checked_div Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::ops::checked::CheckedDiv>::checked_div |
2126 | | } |
2127 | | |
2128 | | impl Integer for BigInt { |
2129 | | #[inline] |
2130 | 0 | fn div_rem(&self, other: &BigInt) -> (BigInt, BigInt) { |
2131 | | // r.sign == self.sign |
2132 | 0 | let (d_ui, r_ui) = self.data.div_mod_floor(&other.data); |
2133 | 0 | let d = BigInt::from_biguint(self.sign, d_ui); |
2134 | 0 | let r = BigInt::from_biguint(self.sign, r_ui); |
2135 | 0 | if other.is_negative() { |
2136 | 0 | (-d, r) |
2137 | | } else { |
2138 | 0 | (d, r) |
2139 | | } |
2140 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::div_rem Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::div_rem |
2141 | | |
2142 | | #[inline] |
2143 | 0 | fn div_floor(&self, other: &BigInt) -> BigInt { |
2144 | 0 | let (d, _) = self.div_mod_floor(other); |
2145 | 0 | d |
2146 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::div_floor Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::div_floor |
2147 | | |
2148 | | #[inline] |
2149 | 0 | fn mod_floor(&self, other: &BigInt) -> BigInt { |
2150 | 0 | let (_, m) = self.div_mod_floor(other); |
2151 | 0 | m |
2152 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::mod_floor Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::mod_floor |
2153 | | |
2154 | 0 | fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt) { |
2155 | | // m.sign == other.sign |
2156 | 0 | let (d_ui, m_ui) = self.data.div_rem(&other.data); |
2157 | 0 | let d = BigInt::from_biguint(Plus, d_ui); |
2158 | 0 | let m = BigInt::from_biguint(Plus, m_ui); |
2159 | 0 | let one: BigInt = One::one(); |
2160 | 0 | match (self.sign, other.sign) { |
2161 | 0 | (_, NoSign) => panic!(), |
2162 | 0 | (Plus, Plus) | (NoSign, Plus) => (d, m), |
2163 | | (Plus, Minus) | (NoSign, Minus) => { |
2164 | 0 | if m.is_zero() { |
2165 | 0 | (-d, Zero::zero()) |
2166 | | } else { |
2167 | 0 | (-d - one, m + other) |
2168 | | } |
2169 | | } |
2170 | | (Minus, Plus) => { |
2171 | 0 | if m.is_zero() { |
2172 | 0 | (-d, Zero::zero()) |
2173 | | } else { |
2174 | 0 | (-d - one, other - m) |
2175 | | } |
2176 | | } |
2177 | 0 | (Minus, Minus) => (d, -m), |
2178 | | } |
2179 | 0 | } |
2180 | | |
2181 | | /// Calculates the Greatest Common Divisor (GCD) of the number and `other`. |
2182 | | /// |
2183 | | /// The result is always positive. |
2184 | | #[inline] |
2185 | 0 | fn gcd(&self, other: &BigInt) -> BigInt { |
2186 | 0 | BigInt::from_biguint(Plus, self.data.gcd(&other.data)) |
2187 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::gcd Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::gcd |
2188 | | |
2189 | | /// Calculates the Lowest Common Multiple (LCM) of the number and `other`. |
2190 | | #[inline] |
2191 | 0 | fn lcm(&self, other: &BigInt) -> BigInt { |
2192 | 0 | BigInt::from_biguint(Plus, self.data.lcm(&other.data)) |
2193 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::lcm Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::lcm |
2194 | | |
2195 | | /// Deprecated, use `is_multiple_of` instead. |
2196 | | #[inline] |
2197 | 0 | fn divides(&self, other: &BigInt) -> bool { |
2198 | 0 | self.is_multiple_of(other) |
2199 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::divides Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::divides |
2200 | | |
2201 | | /// Returns `true` if the number is a multiple of `other`. |
2202 | | #[inline] |
2203 | 0 | fn is_multiple_of(&self, other: &BigInt) -> bool { |
2204 | 0 | self.data.is_multiple_of(&other.data) |
2205 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::is_multiple_of Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::is_multiple_of |
2206 | | |
2207 | | /// Returns `true` if the number is divisible by `2`. |
2208 | | #[inline] |
2209 | 0 | fn is_even(&self) -> bool { |
2210 | 0 | self.data.is_even() |
2211 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::is_even Unexecuted instantiation: <num_bigint::bigint::BigInt as num_integer::Integer>::is_even |
2212 | | |
2213 | | /// Returns `true` if the number is not divisible by `2`. |
2214 | | #[inline] |
2215 | 0 | fn is_odd(&self) -> bool { |
2216 | 0 | self.data.is_odd() |
2217 | 0 | } |
2218 | | } |
2219 | | |
2220 | | impl Roots for BigInt { |
2221 | 0 | fn nth_root(&self, n: u32) -> Self { |
2222 | 0 | assert!( |
2223 | 0 | !(self.is_negative() && n.is_even()), |
2224 | 0 | "root of degree {} is imaginary", |
2225 | | n |
2226 | | ); |
2227 | | |
2228 | 0 | BigInt::from_biguint(self.sign, self.data.nth_root(n)) |
2229 | 0 | } |
2230 | | |
2231 | 0 | fn sqrt(&self) -> Self { |
2232 | 0 | assert!(!self.is_negative(), "square root is imaginary"); |
2233 | | |
2234 | 0 | BigInt::from_biguint(self.sign, self.data.sqrt()) |
2235 | 0 | } |
2236 | | |
2237 | 0 | fn cbrt(&self) -> Self { |
2238 | 0 | BigInt::from_biguint(self.sign, self.data.cbrt()) |
2239 | 0 | } |
2240 | | } |
2241 | | |
2242 | | impl ToPrimitive for BigInt { |
2243 | | #[inline] |
2244 | 0 | fn to_i64(&self) -> Option<i64> { |
2245 | 0 | match self.sign { |
2246 | 0 | Plus => self.data.to_i64(), |
2247 | 0 | NoSign => Some(0), |
2248 | 0 | Minus => self.data.to_u64().and_then(|n| { |
2249 | 0 | let m: u64 = 1 << 63; |
2250 | 0 | if n < m { |
2251 | 0 | Some(-(n as i64)) |
2252 | 0 | } else if n == m { |
2253 | 0 | Some(i64::MIN) |
2254 | | } else { |
2255 | 0 | None |
2256 | | } |
2257 | 0 | }), Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_i64::{closure#0}Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_i64::{closure#0} |
2258 | | } |
2259 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_i64 Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_i64 |
2260 | | |
2261 | | #[inline] |
2262 | | #[cfg(has_i128)] |
2263 | 0 | fn to_i128(&self) -> Option<i128> { |
2264 | 0 | match self.sign { |
2265 | 0 | Plus => self.data.to_i128(), |
2266 | 0 | NoSign => Some(0), |
2267 | 0 | Minus => self.data.to_u128().and_then(|n| { |
2268 | 0 | let m: u128 = 1 << 127; |
2269 | 0 | if n < m { |
2270 | 0 | Some(-(n as i128)) |
2271 | 0 | } else if n == m { |
2272 | 0 | Some(i128::MIN) |
2273 | | } else { |
2274 | 0 | None |
2275 | | } |
2276 | 0 | }), Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_i128::{closure#0}Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_i128::{closure#0} |
2277 | | } |
2278 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_i128 Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_i128 |
2279 | | |
2280 | | #[inline] |
2281 | 0 | fn to_u64(&self) -> Option<u64> { |
2282 | 0 | match self.sign { |
2283 | 0 | Plus => self.data.to_u64(), |
2284 | 0 | NoSign => Some(0), |
2285 | 0 | Minus => None, |
2286 | | } |
2287 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_u64 Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_u64 |
2288 | | |
2289 | | #[inline] |
2290 | | #[cfg(has_i128)] |
2291 | 0 | fn to_u128(&self) -> Option<u128> { |
2292 | 0 | match self.sign { |
2293 | 0 | Plus => self.data.to_u128(), |
2294 | 0 | NoSign => Some(0), |
2295 | 0 | Minus => None, |
2296 | | } |
2297 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_u128 Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_u128 |
2298 | | |
2299 | | #[inline] |
2300 | 0 | fn to_f32(&self) -> Option<f32> { |
2301 | 0 | self.data |
2302 | 0 | .to_f32() |
2303 | 0 | .map(|n| if self.sign == Minus { -n } else { n })Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_f32::{closure#0}Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_f32::{closure#0} |
2304 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_f32 Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_f32 |
2305 | | |
2306 | | #[inline] |
2307 | 0 | fn to_f64(&self) -> Option<f64> { |
2308 | 0 | self.data |
2309 | 0 | .to_f64() |
2310 | 0 | .map(|n| if self.sign == Minus { -n } else { n })Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_f64::{closure#0}Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_f64::{closure#0} |
2311 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_f64 Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::ToPrimitive>::to_f64 |
2312 | | } |
2313 | | |
2314 | | impl FromPrimitive for BigInt { |
2315 | | #[inline] |
2316 | 0 | fn from_i64(n: i64) -> Option<BigInt> { |
2317 | 0 | Some(BigInt::from(n)) |
2318 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_i64 Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_i64 |
2319 | | |
2320 | | #[inline] |
2321 | | #[cfg(has_i128)] |
2322 | 0 | fn from_i128(n: i128) -> Option<BigInt> { |
2323 | 0 | Some(BigInt::from(n)) |
2324 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_i128 Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_i128 |
2325 | | |
2326 | | #[inline] |
2327 | 0 | fn from_u64(n: u64) -> Option<BigInt> { |
2328 | 0 | Some(BigInt::from(n)) |
2329 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_u64 Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_u64 |
2330 | | |
2331 | | #[inline] |
2332 | | #[cfg(has_i128)] |
2333 | 0 | fn from_u128(n: u128) -> Option<BigInt> { |
2334 | 0 | Some(BigInt::from(n)) |
2335 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_u128 Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_u128 |
2336 | | |
2337 | | #[inline] |
2338 | 0 | fn from_f64(n: f64) -> Option<BigInt> { |
2339 | 0 | if n >= 0.0 { |
2340 | 0 | BigUint::from_f64(n).map(|x| BigInt::from_biguint(Plus, x)) Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_f64::{closure#0}Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_f64::{closure#0} |
2341 | | } else { |
2342 | 0 | BigUint::from_f64(-n).map(|x| BigInt::from_biguint(Minus, x)) Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_f64::{closure#1}Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_f64::{closure#1} |
2343 | | } |
2344 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_f64 Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::cast::FromPrimitive>::from_f64 |
2345 | | } |
2346 | | |
2347 | | impl From<i64> for BigInt { |
2348 | | #[inline] |
2349 | 0 | fn from(n: i64) -> Self { |
2350 | 0 | if n >= 0 { |
2351 | 0 | BigInt::from(n as u64) |
2352 | | } else { |
2353 | 0 | let u = u64::MAX - (n as u64) + 1; |
2354 | 0 | BigInt { |
2355 | 0 | sign: Minus, |
2356 | 0 | data: BigUint::from(u), |
2357 | 0 | } |
2358 | | } |
2359 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i64>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i64>>::from |
2360 | | } |
2361 | | |
2362 | | #[cfg(has_i128)] |
2363 | | impl From<i128> for BigInt { |
2364 | | #[inline] |
2365 | 0 | fn from(n: i128) -> Self { |
2366 | 0 | if n >= 0 { |
2367 | 0 | BigInt::from(n as u128) |
2368 | | } else { |
2369 | 0 | let u = u128::MAX - (n as u128) + 1; |
2370 | 0 | BigInt { |
2371 | 0 | sign: Minus, |
2372 | 0 | data: BigUint::from(u), |
2373 | 0 | } |
2374 | | } |
2375 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i128>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i128>>::from |
2376 | | } |
2377 | | |
2378 | | macro_rules! impl_bigint_from_int { |
2379 | | ($T:ty) => { |
2380 | | impl From<$T> for BigInt { |
2381 | | #[inline] |
2382 | 0 | fn from(n: $T) -> Self { |
2383 | 0 | BigInt::from(n as i64) |
2384 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i8>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i16>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i32>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<isize>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i8>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i16>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i32>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<isize>>::from |
2385 | | } |
2386 | | }; |
2387 | | } |
2388 | | |
2389 | | impl_bigint_from_int!(i8); |
2390 | | impl_bigint_from_int!(i16); |
2391 | | impl_bigint_from_int!(i32); |
2392 | | impl_bigint_from_int!(isize); |
2393 | | |
2394 | | impl From<u64> for BigInt { |
2395 | | #[inline] |
2396 | 0 | fn from(n: u64) -> Self { |
2397 | 0 | if n > 0 { |
2398 | 0 | BigInt { |
2399 | 0 | sign: Plus, |
2400 | 0 | data: BigUint::from(n), |
2401 | 0 | } |
2402 | | } else { |
2403 | 0 | BigInt::zero() |
2404 | | } |
2405 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u64>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u64>>::from |
2406 | | } |
2407 | | |
2408 | | #[cfg(has_i128)] |
2409 | | impl From<u128> for BigInt { |
2410 | | #[inline] |
2411 | 0 | fn from(n: u128) -> Self { |
2412 | 0 | if n > 0 { |
2413 | 0 | BigInt { |
2414 | 0 | sign: Plus, |
2415 | 0 | data: BigUint::from(n), |
2416 | 0 | } |
2417 | | } else { |
2418 | 0 | BigInt::zero() |
2419 | | } |
2420 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u128>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u128>>::from |
2421 | | } |
2422 | | |
2423 | | macro_rules! impl_bigint_from_uint { |
2424 | | ($T:ty) => { |
2425 | | impl From<$T> for BigInt { |
2426 | | #[inline] |
2427 | 0 | fn from(n: $T) -> Self { |
2428 | 0 | BigInt::from(n as u64) |
2429 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u8>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u16>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u32>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<usize>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u8>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u16>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u32>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<usize>>::from |
2430 | | } |
2431 | | }; |
2432 | | } |
2433 | | |
2434 | | impl_bigint_from_uint!(u8); |
2435 | | impl_bigint_from_uint!(u16); |
2436 | | impl_bigint_from_uint!(u32); |
2437 | | impl_bigint_from_uint!(usize); |
2438 | | |
2439 | | impl From<BigUint> for BigInt { |
2440 | | #[inline] |
2441 | 0 | fn from(n: BigUint) -> Self { |
2442 | 0 | if n.is_zero() { |
2443 | 0 | BigInt::zero() |
2444 | | } else { |
2445 | 0 | BigInt { |
2446 | 0 | sign: Plus, |
2447 | 0 | data: n, |
2448 | 0 | } |
2449 | | } |
2450 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<num_bigint::biguint::BigUint>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<num_bigint::biguint::BigUint>>::from |
2451 | | } |
2452 | | |
2453 | | impl IntDigits for BigInt { |
2454 | | #[inline] |
2455 | 0 | fn digits(&self) -> &[BigDigit] { |
2456 | 0 | self.data.digits() |
2457 | 0 | } |
2458 | | #[inline] |
2459 | 0 | fn digits_mut(&mut self) -> &mut Vec<BigDigit> { |
2460 | 0 | self.data.digits_mut() |
2461 | 0 | } |
2462 | | #[inline] |
2463 | 0 | fn normalize(&mut self) { |
2464 | 0 | self.data.normalize(); |
2465 | 0 | if self.data.is_zero() { |
2466 | 0 | self.sign = NoSign; |
2467 | 0 | } |
2468 | 0 | } |
2469 | | #[inline] |
2470 | 0 | fn capacity(&self) -> usize { |
2471 | 0 | self.data.capacity() |
2472 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_bigint::biguint::IntDigits>::capacity Unexecuted instantiation: <num_bigint::bigint::BigInt as num_bigint::biguint::IntDigits>::capacity |
2473 | | #[inline] |
2474 | 0 | fn len(&self) -> usize { |
2475 | 0 | self.data.len() |
2476 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_bigint::biguint::IntDigits>::len Unexecuted instantiation: <num_bigint::bigint::BigInt as num_bigint::biguint::IntDigits>::len |
2477 | | } |
2478 | | |
2479 | | #[cfg(feature = "serde")] |
2480 | | impl serde::Serialize for BigInt { |
2481 | | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
2482 | | where |
2483 | | S: serde::Serializer, |
2484 | | { |
2485 | | // Note: do not change the serialization format, or it may break |
2486 | | // forward and backward compatibility of serialized data! |
2487 | | (self.sign, &self.data).serialize(serializer) |
2488 | | } |
2489 | | } |
2490 | | |
2491 | | #[cfg(feature = "serde")] |
2492 | | impl<'de> serde::Deserialize<'de> for BigInt { |
2493 | | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
2494 | | where |
2495 | | D: serde::Deserializer<'de>, |
2496 | | { |
2497 | | let (sign, data) = serde::Deserialize::deserialize(deserializer)?; |
2498 | | Ok(BigInt::from_biguint(sign, data)) |
2499 | | } |
2500 | | } |
2501 | | |
2502 | | /// A generic trait for converting a value to a `BigInt`. This may return |
2503 | | /// `None` when converting from `f32` or `f64`, and will always succeed |
2504 | | /// when converting from any integer or unsigned primitive, or `BigUint`. |
2505 | | pub trait ToBigInt { |
2506 | | /// Converts the value of `self` to a `BigInt`. |
2507 | | fn to_bigint(&self) -> Option<BigInt>; |
2508 | | } |
2509 | | |
2510 | | impl ToBigInt for BigInt { |
2511 | | #[inline] |
2512 | 0 | fn to_bigint(&self) -> Option<BigInt> { |
2513 | 0 | Some(self.clone()) |
2514 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <num_bigint::bigint::BigInt as num_bigint::bigint::ToBigInt>::to_bigint |
2515 | | } |
2516 | | |
2517 | | impl ToBigInt for BigUint { |
2518 | | #[inline] |
2519 | 0 | fn to_bigint(&self) -> Option<BigInt> { |
2520 | 0 | if self.is_zero() { |
2521 | 0 | Some(Zero::zero()) |
2522 | | } else { |
2523 | 0 | Some(BigInt { |
2524 | 0 | sign: Plus, |
2525 | 0 | data: self.clone(), |
2526 | 0 | }) |
2527 | | } |
2528 | 0 | } Unexecuted instantiation: <num_bigint::biguint::BigUint as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <num_bigint::biguint::BigUint as num_bigint::bigint::ToBigInt>::to_bigint |
2529 | | } |
2530 | | |
2531 | | impl biguint::ToBigUint for BigInt { |
2532 | | #[inline] |
2533 | 0 | fn to_biguint(&self) -> Option<BigUint> { |
2534 | 0 | match self.sign() { |
2535 | 0 | Plus => Some(self.data.clone()), |
2536 | 0 | NoSign => Some(Zero::zero()), |
2537 | 0 | Minus => None, |
2538 | | } |
2539 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as num_bigint::biguint::ToBigUint>::to_biguint Unexecuted instantiation: <num_bigint::bigint::BigInt as num_bigint::biguint::ToBigUint>::to_biguint |
2540 | | } |
2541 | | |
2542 | | macro_rules! impl_to_bigint { |
2543 | | ($T:ty, $from_ty:path) => { |
2544 | | impl ToBigInt for $T { |
2545 | | #[inline] |
2546 | 0 | fn to_bigint(&self) -> Option<BigInt> { |
2547 | 0 | $from_ty(*self) |
2548 | 0 | } Unexecuted instantiation: <isize as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i8 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i16 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i32 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i64 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i128 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <usize as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u8 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u16 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u32 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u64 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u128 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <f32 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <f64 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <isize as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i8 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i16 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i32 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i64 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i128 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <usize as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u8 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u16 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u32 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u64 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u128 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <f32 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <f64 as num_bigint::bigint::ToBigInt>::to_bigint |
2549 | | } |
2550 | | }; |
2551 | | } |
2552 | | |
2553 | | impl_to_bigint!(isize, FromPrimitive::from_isize); |
2554 | | impl_to_bigint!(i8, FromPrimitive::from_i8); |
2555 | | impl_to_bigint!(i16, FromPrimitive::from_i16); |
2556 | | impl_to_bigint!(i32, FromPrimitive::from_i32); |
2557 | | impl_to_bigint!(i64, FromPrimitive::from_i64); |
2558 | | #[cfg(has_i128)] |
2559 | | impl_to_bigint!(i128, FromPrimitive::from_i128); |
2560 | | |
2561 | | impl_to_bigint!(usize, FromPrimitive::from_usize); |
2562 | | impl_to_bigint!(u8, FromPrimitive::from_u8); |
2563 | | impl_to_bigint!(u16, FromPrimitive::from_u16); |
2564 | | impl_to_bigint!(u32, FromPrimitive::from_u32); |
2565 | | impl_to_bigint!(u64, FromPrimitive::from_u64); |
2566 | | #[cfg(has_i128)] |
2567 | | impl_to_bigint!(u128, FromPrimitive::from_u128); |
2568 | | |
2569 | | impl_to_bigint!(f32, FromPrimitive::from_f32); |
2570 | | impl_to_bigint!(f64, FromPrimitive::from_f64); |
2571 | | |
2572 | | impl BigInt { |
2573 | | /// Creates and initializes a BigInt. |
2574 | | /// |
2575 | | /// The base 2<sup>32</sup> digits are ordered least significant digit first. |
2576 | | #[inline] |
2577 | 0 | pub fn new(sign: Sign, digits: Vec<u32>) -> BigInt { |
2578 | 0 | BigInt::from_biguint(sign, BigUint::new(digits)) |
2579 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::new Unexecuted instantiation: <num_bigint::bigint::BigInt>::new |
2580 | | |
2581 | | /// Creates and initializes a `BigInt`. |
2582 | | /// |
2583 | | /// The base 2<sup>32</sup> digits are ordered least significant digit first. |
2584 | | #[inline] |
2585 | 0 | pub fn from_biguint(mut sign: Sign, mut data: BigUint) -> BigInt { |
2586 | 0 | if sign == NoSign { |
2587 | 0 | data.assign_from_slice(&[]); |
2588 | 0 | } else if data.is_zero() { |
2589 | 0 | sign = NoSign; |
2590 | 0 | } |
2591 | | |
2592 | 0 | BigInt { |
2593 | 0 | sign: sign, |
2594 | 0 | data: data, |
2595 | 0 | } |
2596 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_biguint Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_biguint |
2597 | | |
2598 | | /// Creates and initializes a `BigInt`. |
2599 | | /// |
2600 | | /// The base 2<sup>32</sup> digits are ordered least significant digit first. |
2601 | | #[inline] |
2602 | 0 | pub fn from_slice(sign: Sign, slice: &[u32]) -> BigInt { |
2603 | 0 | BigInt::from_biguint(sign, BigUint::from_slice(slice)) |
2604 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_slice Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_slice |
2605 | | |
2606 | | /// Reinitializes a `BigInt`. |
2607 | | /// |
2608 | | /// The base 2<sup>32</sup> digits are ordered least significant digit first. |
2609 | | #[inline] |
2610 | 0 | pub fn assign_from_slice(&mut self, sign: Sign, slice: &[u32]) { |
2611 | 0 | if sign == NoSign { |
2612 | 0 | self.data.assign_from_slice(&[]); |
2613 | 0 | self.sign = NoSign; |
2614 | 0 | } else { |
2615 | 0 | self.data.assign_from_slice(slice); |
2616 | 0 | self.sign = match self.data.is_zero() { |
2617 | 0 | true => NoSign, |
2618 | 0 | false => sign, |
2619 | | } |
2620 | | } |
2621 | 0 | } |
2622 | | |
2623 | | /// Creates and initializes a `BigInt`. |
2624 | | /// |
2625 | | /// The bytes are in big-endian byte order. |
2626 | | /// |
2627 | | /// # Examples |
2628 | | /// |
2629 | | /// ``` |
2630 | | /// use num_bigint::{BigInt, Sign}; |
2631 | | /// |
2632 | | /// assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"A"), |
2633 | | /// BigInt::parse_bytes(b"65", 10).unwrap()); |
2634 | | /// assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"AA"), |
2635 | | /// BigInt::parse_bytes(b"16705", 10).unwrap()); |
2636 | | /// assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"AB"), |
2637 | | /// BigInt::parse_bytes(b"16706", 10).unwrap()); |
2638 | | /// assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"Hello world!"), |
2639 | | /// BigInt::parse_bytes(b"22405534230753963835153736737", 10).unwrap()); |
2640 | | /// ``` |
2641 | | #[inline] |
2642 | 0 | pub fn from_bytes_be(sign: Sign, bytes: &[u8]) -> BigInt { |
2643 | 0 | BigInt::from_biguint(sign, BigUint::from_bytes_be(bytes)) |
2644 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_bytes_be Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_bytes_be |
2645 | | |
2646 | | /// Creates and initializes a `BigInt`. |
2647 | | /// |
2648 | | /// The bytes are in little-endian byte order. |
2649 | | #[inline] |
2650 | 0 | pub fn from_bytes_le(sign: Sign, bytes: &[u8]) -> BigInt { |
2651 | 0 | BigInt::from_biguint(sign, BigUint::from_bytes_le(bytes)) |
2652 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_bytes_le Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_bytes_le |
2653 | | |
2654 | | /// Creates and initializes a `BigInt` from an array of bytes in |
2655 | | /// two's complement binary representation. |
2656 | | /// |
2657 | | /// The digits are in big-endian base 2<sup>8</sup>. |
2658 | | #[inline] |
2659 | 0 | pub fn from_signed_bytes_be(digits: &[u8]) -> BigInt { |
2660 | 0 | let sign = match digits.first() { |
2661 | 0 | Some(v) if *v > 0x7f => Sign::Minus, |
2662 | 0 | Some(_) => Sign::Plus, |
2663 | 0 | None => return BigInt::zero(), |
2664 | | }; |
2665 | | |
2666 | 0 | if sign == Sign::Minus { |
2667 | | // two's-complement the content to retrieve the magnitude |
2668 | 0 | let mut digits = Vec::from(digits); |
2669 | 0 | twos_complement_be(&mut digits); |
2670 | 0 | BigInt::from_biguint(sign, BigUint::from_bytes_be(&*digits)) |
2671 | | } else { |
2672 | 0 | BigInt::from_biguint(sign, BigUint::from_bytes_be(digits)) |
2673 | | } |
2674 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_signed_bytes_be Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_signed_bytes_be |
2675 | | |
2676 | | /// Creates and initializes a `BigInt` from an array of bytes in two's complement. |
2677 | | /// |
2678 | | /// The digits are in little-endian base 2<sup>8</sup>. |
2679 | | #[inline] |
2680 | 0 | pub fn from_signed_bytes_le(digits: &[u8]) -> BigInt { |
2681 | 0 | let sign = match digits.last() { |
2682 | 0 | Some(v) if *v > 0x7f => Sign::Minus, |
2683 | 0 | Some(_) => Sign::Plus, |
2684 | 0 | None => return BigInt::zero(), |
2685 | | }; |
2686 | | |
2687 | 0 | if sign == Sign::Minus { |
2688 | | // two's-complement the content to retrieve the magnitude |
2689 | 0 | let mut digits = Vec::from(digits); |
2690 | 0 | twos_complement_le(&mut digits); |
2691 | 0 | BigInt::from_biguint(sign, BigUint::from_bytes_le(&*digits)) |
2692 | | } else { |
2693 | 0 | BigInt::from_biguint(sign, BigUint::from_bytes_le(digits)) |
2694 | | } |
2695 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_signed_bytes_le Unexecuted instantiation: <num_bigint::bigint::BigInt>::from_signed_bytes_le |
2696 | | |
2697 | | /// Creates and initializes a `BigInt`. |
2698 | | /// |
2699 | | /// # Examples |
2700 | | /// |
2701 | | /// ``` |
2702 | | /// use num_bigint::{BigInt, ToBigInt}; |
2703 | | /// |
2704 | | /// assert_eq!(BigInt::parse_bytes(b"1234", 10), ToBigInt::to_bigint(&1234)); |
2705 | | /// assert_eq!(BigInt::parse_bytes(b"ABCD", 16), ToBigInt::to_bigint(&0xABCD)); |
2706 | | /// assert_eq!(BigInt::parse_bytes(b"G", 16), None); |
2707 | | /// ``` |
2708 | | #[inline] |
2709 | 0 | pub fn parse_bytes(buf: &[u8], radix: u32) -> Option<BigInt> { |
2710 | 0 | str::from_utf8(buf) |
2711 | 0 | .ok() |
2712 | 0 | .and_then(|s| BigInt::from_str_radix(s, radix).ok()) Unexecuted instantiation: <num_bigint::bigint::BigInt>::parse_bytes::{closure#0}Unexecuted instantiation: <num_bigint::bigint::BigInt>::parse_bytes::{closure#0} |
2713 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::parse_bytes Unexecuted instantiation: <num_bigint::bigint::BigInt>::parse_bytes |
2714 | | |
2715 | | /// Creates and initializes a `BigInt`. Each u8 of the input slice is |
2716 | | /// interpreted as one digit of the number |
2717 | | /// and must therefore be less than `radix`. |
2718 | | /// |
2719 | | /// The bytes are in big-endian byte order. |
2720 | | /// `radix` must be in the range `2...256`. |
2721 | | /// |
2722 | | /// # Examples |
2723 | | /// |
2724 | | /// ``` |
2725 | | /// use num_bigint::{BigInt, Sign}; |
2726 | | /// |
2727 | | /// let inbase190 = vec![15, 33, 125, 12, 14]; |
2728 | | /// let a = BigInt::from_radix_be(Sign::Minus, &inbase190, 190).unwrap(); |
2729 | | /// assert_eq!(a.to_radix_be(190), (Sign:: Minus, inbase190)); |
2730 | | /// ``` |
2731 | 0 | pub fn from_radix_be(sign: Sign, buf: &[u8], radix: u32) -> Option<BigInt> { |
2732 | 0 | BigUint::from_radix_be(buf, radix).map(|u| BigInt::from_biguint(sign, u)) |
2733 | 0 | } |
2734 | | |
2735 | | /// Creates and initializes a `BigInt`. Each u8 of the input slice is |
2736 | | /// interpreted as one digit of the number |
2737 | | /// and must therefore be less than `radix`. |
2738 | | /// |
2739 | | /// The bytes are in little-endian byte order. |
2740 | | /// `radix` must be in the range `2...256`. |
2741 | | /// |
2742 | | /// # Examples |
2743 | | /// |
2744 | | /// ``` |
2745 | | /// use num_bigint::{BigInt, Sign}; |
2746 | | /// |
2747 | | /// let inbase190 = vec![14, 12, 125, 33, 15]; |
2748 | | /// let a = BigInt::from_radix_be(Sign::Minus, &inbase190, 190).unwrap(); |
2749 | | /// assert_eq!(a.to_radix_be(190), (Sign::Minus, inbase190)); |
2750 | | /// ``` |
2751 | 0 | pub fn from_radix_le(sign: Sign, buf: &[u8], radix: u32) -> Option<BigInt> { |
2752 | 0 | BigUint::from_radix_le(buf, radix).map(|u| BigInt::from_biguint(sign, u)) |
2753 | 0 | } |
2754 | | |
2755 | | /// Returns the sign and the byte representation of the `BigInt` in big-endian byte order. |
2756 | | /// |
2757 | | /// # Examples |
2758 | | /// |
2759 | | /// ``` |
2760 | | /// use num_bigint::{ToBigInt, Sign}; |
2761 | | /// |
2762 | | /// let i = -1125.to_bigint().unwrap(); |
2763 | | /// assert_eq!(i.to_bytes_be(), (Sign::Minus, vec![4, 101])); |
2764 | | /// ``` |
2765 | | #[inline] |
2766 | 0 | pub fn to_bytes_be(&self) -> (Sign, Vec<u8>) { |
2767 | 0 | (self.sign, self.data.to_bytes_be()) |
2768 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_bytes_be Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_bytes_be |
2769 | | |
2770 | | /// Returns the sign and the byte representation of the `BigInt` in little-endian byte order. |
2771 | | /// |
2772 | | /// # Examples |
2773 | | /// |
2774 | | /// ``` |
2775 | | /// use num_bigint::{ToBigInt, Sign}; |
2776 | | /// |
2777 | | /// let i = -1125.to_bigint().unwrap(); |
2778 | | /// assert_eq!(i.to_bytes_le(), (Sign::Minus, vec![101, 4])); |
2779 | | /// ``` |
2780 | | #[inline] |
2781 | 0 | pub fn to_bytes_le(&self) -> (Sign, Vec<u8>) { |
2782 | 0 | (self.sign, self.data.to_bytes_le()) |
2783 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_bytes_le Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_bytes_le |
2784 | | |
2785 | | /// Returns the sign and the `u32` digits representation of the `BigInt` ordered least |
2786 | | /// significant digit first. |
2787 | | /// |
2788 | | /// # Examples |
2789 | | /// |
2790 | | /// ``` |
2791 | | /// use num_bigint::{BigInt, Sign}; |
2792 | | /// |
2793 | | /// assert_eq!(BigInt::from(-1125).to_u32_digits(), (Sign::Minus, vec![1125])); |
2794 | | /// assert_eq!(BigInt::from(4294967295u32).to_u32_digits(), (Sign::Plus, vec![4294967295])); |
2795 | | /// assert_eq!(BigInt::from(4294967296u64).to_u32_digits(), (Sign::Plus, vec![0, 1])); |
2796 | | /// assert_eq!(BigInt::from(-112500000000i64).to_u32_digits(), (Sign::Minus, vec![830850304, 26])); |
2797 | | /// assert_eq!(BigInt::from(112500000000i64).to_u32_digits(), (Sign::Plus, vec![830850304, 26])); |
2798 | | /// ``` |
2799 | | #[inline] |
2800 | 0 | pub fn to_u32_digits(&self) -> (Sign, Vec<u32>) { |
2801 | 0 | (self.sign, self.data.to_u32_digits()) |
2802 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_u32_digits Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_u32_digits |
2803 | | |
2804 | | /// Returns the two's-complement byte representation of the `BigInt` in big-endian byte order. |
2805 | | /// |
2806 | | /// # Examples |
2807 | | /// |
2808 | | /// ``` |
2809 | | /// use num_bigint::ToBigInt; |
2810 | | /// |
2811 | | /// let i = -1125.to_bigint().unwrap(); |
2812 | | /// assert_eq!(i.to_signed_bytes_be(), vec![251, 155]); |
2813 | | /// ``` |
2814 | | #[inline] |
2815 | 0 | pub fn to_signed_bytes_be(&self) -> Vec<u8> { |
2816 | 0 | let mut bytes = self.data.to_bytes_be(); |
2817 | 0 | let first_byte = bytes.first().cloned().unwrap_or(0); |
2818 | 0 | if first_byte > 0x7f |
2819 | 0 | && !(first_byte == 0x80 |
2820 | 0 | && bytes.iter().skip(1).all(Zero::is_zero) |
2821 | 0 | && self.sign == Sign::Minus) |
2822 | 0 | { |
2823 | 0 | // msb used by magnitude, extend by 1 byte |
2824 | 0 | bytes.insert(0, 0); |
2825 | 0 | } |
2826 | 0 | if self.sign == Sign::Minus { |
2827 | 0 | twos_complement_be(&mut bytes); |
2828 | 0 | } |
2829 | 0 | bytes |
2830 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_signed_bytes_be Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_signed_bytes_be |
2831 | | |
2832 | | /// Returns the two's-complement byte representation of the `BigInt` in little-endian byte order. |
2833 | | /// |
2834 | | /// # Examples |
2835 | | /// |
2836 | | /// ``` |
2837 | | /// use num_bigint::ToBigInt; |
2838 | | /// |
2839 | | /// let i = -1125.to_bigint().unwrap(); |
2840 | | /// assert_eq!(i.to_signed_bytes_le(), vec![155, 251]); |
2841 | | /// ``` |
2842 | | #[inline] |
2843 | 0 | pub fn to_signed_bytes_le(&self) -> Vec<u8> { |
2844 | 0 | let mut bytes = self.data.to_bytes_le(); |
2845 | 0 | let last_byte = bytes.last().cloned().unwrap_or(0); |
2846 | 0 | if last_byte > 0x7f |
2847 | 0 | && !(last_byte == 0x80 |
2848 | 0 | && bytes.iter().rev().skip(1).all(Zero::is_zero) |
2849 | 0 | && self.sign == Sign::Minus) |
2850 | 0 | { |
2851 | 0 | // msb used by magnitude, extend by 1 byte |
2852 | 0 | bytes.push(0); |
2853 | 0 | } |
2854 | 0 | if self.sign == Sign::Minus { |
2855 | 0 | twos_complement_le(&mut bytes); |
2856 | 0 | } |
2857 | 0 | bytes |
2858 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_signed_bytes_le Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_signed_bytes_le |
2859 | | |
2860 | | /// Returns the integer formatted as a string in the given radix. |
2861 | | /// `radix` must be in the range `2...36`. |
2862 | | /// |
2863 | | /// # Examples |
2864 | | /// |
2865 | | /// ``` |
2866 | | /// use num_bigint::BigInt; |
2867 | | /// |
2868 | | /// let i = BigInt::parse_bytes(b"ff", 16).unwrap(); |
2869 | | /// assert_eq!(i.to_str_radix(16), "ff"); |
2870 | | /// ``` |
2871 | | #[inline] |
2872 | 0 | pub fn to_str_radix(&self, radix: u32) -> String { |
2873 | 0 | let mut v = to_str_radix_reversed(&self.data, radix); |
2874 | | |
2875 | 0 | if self.is_negative() { |
2876 | 0 | v.push(b'-'); |
2877 | 0 | } |
2878 | | |
2879 | 0 | v.reverse(); |
2880 | 0 | unsafe { String::from_utf8_unchecked(v) } |
2881 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_str_radix Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_str_radix |
2882 | | |
2883 | | /// Returns the integer in the requested base in big-endian digit order. |
2884 | | /// The output is not given in a human readable alphabet but as a zero |
2885 | | /// based u8 number. |
2886 | | /// `radix` must be in the range `2...256`. |
2887 | | /// |
2888 | | /// # Examples |
2889 | | /// |
2890 | | /// ``` |
2891 | | /// use num_bigint::{BigInt, Sign}; |
2892 | | /// |
2893 | | /// assert_eq!(BigInt::from(-0xFFFFi64).to_radix_be(159), |
2894 | | /// (Sign::Minus, vec![2, 94, 27])); |
2895 | | /// // 0xFFFF = 65535 = 2*(159^2) + 94*159 + 27 |
2896 | | /// ``` |
2897 | | #[inline] |
2898 | 0 | pub fn to_radix_be(&self, radix: u32) -> (Sign, Vec<u8>) { |
2899 | 0 | (self.sign, self.data.to_radix_be(radix)) |
2900 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_radix_be Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_radix_be |
2901 | | |
2902 | | /// Returns the integer in the requested base in little-endian digit order. |
2903 | | /// The output is not given in a human readable alphabet but as a zero |
2904 | | /// based u8 number. |
2905 | | /// `radix` must be in the range `2...256`. |
2906 | | /// |
2907 | | /// # Examples |
2908 | | /// |
2909 | | /// ``` |
2910 | | /// use num_bigint::{BigInt, Sign}; |
2911 | | /// |
2912 | | /// assert_eq!(BigInt::from(-0xFFFFi64).to_radix_le(159), |
2913 | | /// (Sign::Minus, vec![27, 94, 2])); |
2914 | | /// // 0xFFFF = 65535 = 27 + 94*159 + 2*(159^2) |
2915 | | /// ``` |
2916 | | #[inline] |
2917 | 0 | pub fn to_radix_le(&self, radix: u32) -> (Sign, Vec<u8>) { |
2918 | 0 | (self.sign, self.data.to_radix_le(radix)) |
2919 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_radix_le Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_radix_le |
2920 | | |
2921 | | /// Returns the sign of the `BigInt` as a `Sign`. |
2922 | | /// |
2923 | | /// # Examples |
2924 | | /// |
2925 | | /// ``` |
2926 | | /// use num_bigint::{ToBigInt, Sign}; |
2927 | | /// |
2928 | | /// assert_eq!(ToBigInt::to_bigint(&1234).unwrap().sign(), Sign::Plus); |
2929 | | /// assert_eq!(ToBigInt::to_bigint(&-4321).unwrap().sign(), Sign::Minus); |
2930 | | /// assert_eq!(ToBigInt::to_bigint(&0).unwrap().sign(), Sign::NoSign); |
2931 | | /// ``` |
2932 | | #[inline] |
2933 | 0 | pub fn sign(&self) -> Sign { |
2934 | 0 | self.sign |
2935 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::sign Unexecuted instantiation: <num_bigint::bigint::BigInt>::sign |
2936 | | |
2937 | | /// Determines the fewest bits necessary to express the `BigInt`, |
2938 | | /// not including the sign. |
2939 | | #[inline] |
2940 | 0 | pub fn bits(&self) -> usize { |
2941 | 0 | self.data.bits() |
2942 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::bits Unexecuted instantiation: <num_bigint::bigint::BigInt>::bits |
2943 | | |
2944 | | /// Converts this `BigInt` into a `BigUint`, if it's not negative. |
2945 | | #[inline] |
2946 | 0 | pub fn to_biguint(&self) -> Option<BigUint> { |
2947 | 0 | match self.sign { |
2948 | 0 | Plus => Some(self.data.clone()), |
2949 | 0 | NoSign => Some(Zero::zero()), |
2950 | 0 | Minus => None, |
2951 | | } |
2952 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_biguint Unexecuted instantiation: <num_bigint::bigint::BigInt>::to_biguint |
2953 | | |
2954 | | #[inline] |
2955 | 0 | pub fn checked_add(&self, v: &BigInt) -> Option<BigInt> { |
2956 | 0 | Some(self.add(v)) |
2957 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::checked_add Unexecuted instantiation: <num_bigint::bigint::BigInt>::checked_add |
2958 | | |
2959 | | #[inline] |
2960 | 0 | pub fn checked_sub(&self, v: &BigInt) -> Option<BigInt> { |
2961 | 0 | Some(self.sub(v)) |
2962 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::checked_sub Unexecuted instantiation: <num_bigint::bigint::BigInt>::checked_sub |
2963 | | |
2964 | | #[inline] |
2965 | 0 | pub fn checked_mul(&self, v: &BigInt) -> Option<BigInt> { |
2966 | 0 | Some(self.mul(v)) |
2967 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::checked_mul Unexecuted instantiation: <num_bigint::bigint::BigInt>::checked_mul |
2968 | | |
2969 | | #[inline] |
2970 | 0 | pub fn checked_div(&self, v: &BigInt) -> Option<BigInt> { |
2971 | 0 | if v.is_zero() { |
2972 | 0 | return None; |
2973 | 0 | } |
2974 | 0 | Some(self.div(v)) |
2975 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt>::checked_div Unexecuted instantiation: <num_bigint::bigint::BigInt>::checked_div |
2976 | | |
2977 | | /// Returns `(self ^ exponent) mod modulus` |
2978 | | /// |
2979 | | /// Note that this rounds like `mod_floor`, not like the `%` operator, |
2980 | | /// which makes a difference when given a negative `self` or `modulus`. |
2981 | | /// The result will be in the interval `[0, modulus)` for `modulus > 0`, |
2982 | | /// or in the interval `(modulus, 0]` for `modulus < 0` |
2983 | | /// |
2984 | | /// Panics if the exponent is negative or the modulus is zero. |
2985 | 0 | pub fn modpow(&self, exponent: &Self, modulus: &Self) -> Self { |
2986 | 0 | assert!( |
2987 | 0 | !exponent.is_negative(), |
2988 | | "negative exponentiation is not supported!" |
2989 | | ); |
2990 | 0 | assert!(!modulus.is_zero(), "divide by zero!"); |
2991 | | |
2992 | 0 | let result = self.data.modpow(&exponent.data, &modulus.data); |
2993 | 0 | if result.is_zero() { |
2994 | 0 | return BigInt::zero(); |
2995 | 0 | } |
2996 | | |
2997 | | // The sign of the result follows the modulus, like `mod_floor`. |
2998 | 0 | let (sign, mag) = match ( |
2999 | 0 | self.is_negative() && exponent.is_odd(), |
3000 | 0 | modulus.is_negative(), |
3001 | | ) { |
3002 | 0 | (false, false) => (Plus, result), |
3003 | 0 | (true, false) => (Plus, &modulus.data - result), |
3004 | 0 | (false, true) => (Minus, &modulus.data - result), |
3005 | 0 | (true, true) => (Minus, result), |
3006 | | }; |
3007 | 0 | BigInt::from_biguint(sign, mag) |
3008 | 0 | } |
3009 | | |
3010 | | /// Returns the truncated principal square root of `self` -- |
3011 | | /// see [Roots::sqrt](https://docs.rs/num-integer/0.1/num_integer/trait.Roots.html#method.sqrt). |
3012 | 0 | pub fn sqrt(&self) -> Self { |
3013 | 0 | Roots::sqrt(self) |
3014 | 0 | } |
3015 | | |
3016 | | /// Returns the truncated principal cube root of `self` -- |
3017 | | /// see [Roots::cbrt](https://docs.rs/num-integer/0.1/num_integer/trait.Roots.html#method.cbrt). |
3018 | 0 | pub fn cbrt(&self) -> Self { |
3019 | 0 | Roots::cbrt(self) |
3020 | 0 | } |
3021 | | |
3022 | | /// Returns the truncated principal `n`th root of `self` -- |
3023 | | /// See [Roots::nth_root](https://docs.rs/num-integer/0.1/num_integer/trait.Roots.html#tymethod.nth_root). |
3024 | 0 | pub fn nth_root(&self, n: u32) -> Self { |
3025 | 0 | Roots::nth_root(self, n) |
3026 | 0 | } |
3027 | | } |
3028 | | |
3029 | | impl_sum_iter_type!(BigInt); |
3030 | | impl_product_iter_type!(BigInt); |
3031 | | |
3032 | | /// Perform in-place two's complement of the given binary representation, |
3033 | | /// in little-endian byte order. |
3034 | | #[inline] |
3035 | 0 | fn twos_complement_le(digits: &mut [u8]) { |
3036 | 0 | twos_complement(digits) |
3037 | 0 | } Unexecuted instantiation: num_bigint::bigint::twos_complement_le Unexecuted instantiation: num_bigint::bigint::twos_complement_le |
3038 | | |
3039 | | /// Perform in-place two's complement of the given binary representation |
3040 | | /// in big-endian byte order. |
3041 | | #[inline] |
3042 | 0 | fn twos_complement_be(digits: &mut [u8]) { |
3043 | 0 | twos_complement(digits.iter_mut().rev()) |
3044 | 0 | } Unexecuted instantiation: num_bigint::bigint::twos_complement_be Unexecuted instantiation: num_bigint::bigint::twos_complement_be |
3045 | | |
3046 | | /// Perform in-place two's complement of the given digit iterator |
3047 | | /// starting from the least significant byte. |
3048 | | #[inline] |
3049 | 0 | fn twos_complement<'a, I>(digits: I) |
3050 | 0 | where |
3051 | 0 | I: IntoIterator<Item = &'a mut u8>, |
3052 | | { |
3053 | 0 | let mut carry = true; |
3054 | 0 | for d in digits { |
3055 | 0 | *d = d.not(); |
3056 | 0 | if carry { |
3057 | 0 | *d = d.wrapping_add(1); |
3058 | 0 | carry = d.is_zero(); |
3059 | 0 | } |
3060 | | } |
3061 | 0 | } Unexecuted instantiation: num_bigint::bigint::twos_complement::<_> Unexecuted instantiation: num_bigint::bigint::twos_complement::<_> |
3062 | | |
3063 | | #[test] |
3064 | | fn test_from_biguint() { |
3065 | | fn check(inp_s: Sign, inp_n: usize, ans_s: Sign, ans_n: usize) { |
3066 | | let inp = BigInt::from_biguint(inp_s, FromPrimitive::from_usize(inp_n).unwrap()); |
3067 | | let ans = BigInt { |
3068 | | sign: ans_s, |
3069 | | data: FromPrimitive::from_usize(ans_n).unwrap(), |
3070 | | }; |
3071 | | assert_eq!(inp, ans); |
3072 | | } |
3073 | | check(Plus, 1, Plus, 1); |
3074 | | check(Plus, 0, NoSign, 0); |
3075 | | check(Minus, 1, Minus, 1); |
3076 | | check(NoSign, 1, NoSign, 0); |
3077 | | } |
3078 | | |
3079 | | #[test] |
3080 | | fn test_from_slice() { |
3081 | | fn check(inp_s: Sign, inp_n: u32, ans_s: Sign, ans_n: u32) { |
3082 | | let inp = BigInt::from_slice(inp_s, &[inp_n]); |
3083 | | let ans = BigInt { |
3084 | | sign: ans_s, |
3085 | | data: FromPrimitive::from_u32(ans_n).unwrap(), |
3086 | | }; |
3087 | | assert_eq!(inp, ans); |
3088 | | } |
3089 | | check(Plus, 1, Plus, 1); |
3090 | | check(Plus, 0, NoSign, 0); |
3091 | | check(Minus, 1, Minus, 1); |
3092 | | check(NoSign, 1, NoSign, 0); |
3093 | | } |
3094 | | |
3095 | | #[test] |
3096 | | fn test_assign_from_slice() { |
3097 | | fn check(inp_s: Sign, inp_n: u32, ans_s: Sign, ans_n: u32) { |
3098 | | let mut inp = BigInt::from_slice(Minus, &[2627_u32, 0_u32, 9182_u32, 42_u32]); |
3099 | | inp.assign_from_slice(inp_s, &[inp_n]); |
3100 | | let ans = BigInt { |
3101 | | sign: ans_s, |
3102 | | data: FromPrimitive::from_u32(ans_n).unwrap(), |
3103 | | }; |
3104 | | assert_eq!(inp, ans); |
3105 | | } |
3106 | | check(Plus, 1, Plus, 1); |
3107 | | check(Plus, 0, NoSign, 0); |
3108 | | check(Minus, 1, Minus, 1); |
3109 | | check(NoSign, 1, NoSign, 0); |
3110 | | } |