crypto_bigint/limb/
shl.rs1use crate::Limb;
4use core::ops::{Shl, ShlAssign};
5use num_traits::WrappingShl;
6
7impl Limb {
8 #[inline(always)]
11 pub const fn shl(self, shift: u32) -> Self {
12 Limb(self.0 << shift)
13 }
14
15 #[inline(always)]
17 pub(crate) const fn shl1(self) -> (Self, Self) {
18 (Self(self.0 << 1), Self(self.0 >> Self::HI_BIT))
19 }
20}
21
22macro_rules! impl_shl {
23 ($($shift:ty),+) => {
24 $(
25 impl Shl<$shift> for Limb {
26 type Output = Limb;
27
28 #[inline]
29 fn shl(self, shift: $shift) -> Limb {
30 Self::shl(self, u32::try_from(shift).expect("invalid shift"))
31 }
32 }
33
34 impl Shl<$shift> for &Limb {
35 type Output = Limb;
36
37 #[inline]
38 fn shl(self, shift: $shift) -> Limb {
39 *self << shift
40 }
41 }
42
43 impl ShlAssign<$shift> for Limb {
44 #[inline]
45 fn shl_assign(&mut self, shift: $shift) {
46 *self = *self << shift;
47 }
48 }
49 )+
50 };
51}
52
53impl_shl!(i32, u32, usize);
54
55impl WrappingShl for Limb {
56 #[inline]
57 fn wrapping_shl(&self, shift: u32) -> Limb {
58 Self(self.0.wrapping_shl(shift))
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use crate::Limb;
65
66 #[test]
67 fn shl1() {
68 assert_eq!(Limb(1) << 1, Limb(2));
69 }
70
71 #[test]
72 fn shl2() {
73 assert_eq!(Limb(1) << 2, Limb(4));
74 }
75
76 #[test]
77 fn shl_assign1() {
78 let mut l = Limb(1);
79 l <<= 1;
80 assert_eq!(l, Limb(2));
81 }
82
83 #[test]
84 fn shl_assign2() {
85 let mut l = Limb(1);
86 l <<= 2;
87 assert_eq!(l, Limb(4));
88 }
89}