crypto_bigint/limb/
shl.rs

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