num_bigint/bigint/
shift.rs

1use super::BigInt;
2use super::Sign::NoSign;
3
4use core::ops::{Shl, ShlAssign, Shr, ShrAssign};
5use num_traits::{PrimInt, Signed, Zero};
6
7macro_rules! impl_shift {
8    (@ref $Shx:ident :: $shx:ident, $ShxAssign:ident :: $shx_assign:ident, $rhs:ty) => {
9        impl<'b> $Shx<&'b $rhs> for BigInt {
10            type Output = BigInt;
11
12            #[inline]
13            fn $shx(self, rhs: &'b $rhs) -> BigInt {
14                $Shx::$shx(self, *rhs)
15            }
16        }
17        impl<'a, 'b> $Shx<&'b $rhs> for &'a BigInt {
18            type Output = BigInt;
19
20            #[inline]
21            fn $shx(self, rhs: &'b $rhs) -> BigInt {
22                $Shx::$shx(self, *rhs)
23            }
24        }
25        impl<'b> $ShxAssign<&'b $rhs> for BigInt {
26            #[inline]
27            fn $shx_assign(&mut self, rhs: &'b $rhs) {
28                $ShxAssign::$shx_assign(self, *rhs);
29            }
30        }
31    };
32    ($($rhs:ty),+) => {$(
33        impl Shl<$rhs> for BigInt {
34            type Output = BigInt;
35
36            #[inline]
37            fn shl(self, rhs: $rhs) -> BigInt {
38                BigInt::from_biguint(self.sign, self.data << rhs)
39            }
40        }
41        impl<'a> Shl<$rhs> for &'a BigInt {
42            type Output = BigInt;
43
44            #[inline]
45            fn shl(self, rhs: $rhs) -> BigInt {
46                BigInt::from_biguint(self.sign, &self.data << rhs)
47            }
48        }
49        impl ShlAssign<$rhs> for BigInt {
50            #[inline]
51            fn shl_assign(&mut self, rhs: $rhs) {
52                self.data <<= rhs
53            }
54        }
55        impl_shift! { @ref Shl::shl, ShlAssign::shl_assign, $rhs }
56
57        impl Shr<$rhs> for BigInt {
58            type Output = BigInt;
59
60            #[inline]
61            fn shr(self, rhs: $rhs) -> BigInt {
62                let round_down = shr_round_down(&self, rhs);
63                let data = self.data >> rhs;
64                let data = if round_down { data + 1u8 } else { data };
65                BigInt::from_biguint(self.sign, data)
66            }
67        }
68        impl<'a> Shr<$rhs> for &'a BigInt {
69            type Output = BigInt;
70
71            #[inline]
72            fn shr(self, rhs: $rhs) -> BigInt {
73                let round_down = shr_round_down(self, rhs);
74                let data = &self.data >> rhs;
75                let data = if round_down { data + 1u8 } else { data };
76                BigInt::from_biguint(self.sign, data)
77            }
78        }
79        impl ShrAssign<$rhs> for BigInt {
80            #[inline]
81            fn shr_assign(&mut self, rhs: $rhs) {
82                let round_down = shr_round_down(self, rhs);
83                self.data >>= rhs;
84                if round_down {
85                    self.data += 1u8;
86                } else if self.data.is_zero() {
87                    self.sign = NoSign;
88                }
89            }
90        }
91        impl_shift! { @ref Shr::shr, ShrAssign::shr_assign, $rhs }
92    )*};
93}
94
95impl_shift! { u8, u16, u32, u64, u128, usize }
96impl_shift! { i8, i16, i32, i64, i128, isize }
97
98// Negative values need a rounding adjustment if there are any ones in the
99// bits that are getting shifted out.
100fn shr_round_down<T: PrimInt>(i: &BigInt, shift: T) -> bool {
101    if i.is_negative() {
102        let zeros = i.trailing_zeros().expect("negative values are non-zero");
103        shift > T::zero() && shift.to_u64().map(|shift| zeros < shift).unwrap_or(true)
104    } else {
105        false
106    }
107}