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