crypto_bigint/limb/
shr.rs

1//! Limb right bitshift
2
3use crate::{Limb, Word};
4use core::ops::{Shr, ShrAssign};
5
6impl Limb {
7    /// Computes `self >> rhs`.
8    #[inline(always)]
9    pub const fn shr(self, rhs: Self) -> Self {
10        Limb(self.0 >> rhs.0)
11    }
12}
13
14impl Shr for Limb {
15    type Output = Self;
16
17    #[inline(always)]
18    fn shr(self, rhs: Self) -> Self::Output {
19        self.shr(rhs)
20    }
21}
22
23impl Shr<usize> for Limb {
24    type Output = Self;
25
26    #[inline(always)]
27    fn shr(self, rhs: usize) -> Self::Output {
28        self.shr(Limb(rhs as Word))
29    }
30}
31
32impl ShrAssign for Limb {
33    #[inline(always)]
34    fn shr_assign(&mut self, other: Self) {
35        *self = self.shr(other);
36    }
37}
38
39impl ShrAssign<usize> for Limb {
40    #[inline(always)]
41    fn shr_assign(&mut self, other: usize) {
42        *self = self.shr(Limb(other as Word));
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use crate::Limb;
49
50    #[test]
51    fn shr1() {
52        assert_eq!(Limb(2) >> 1, Limb(1));
53    }
54
55    #[test]
56    fn shr2() {
57        assert_eq!(Limb(16) >> 2, Limb(4));
58    }
59
60    #[test]
61    fn shr_assign1() {
62        let mut l = Limb::ONE;
63        l >>= 1;
64        assert_eq!(l, Limb::ZERO);
65    }
66
67    #[test]
68    fn shr_assign2() {
69        let mut l = Limb(32);
70        l >>= 2;
71        assert_eq!(l, Limb(8));
72    }
73}