1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! Left shift via the [`Shl`] trait.
use super::U320;
use core::ops::Shl;
/// Shifts the value left by `n` bits, zero-filling the low bits and
/// discarding bits shifted past position 319.
///
/// Delegates to [`U320::shl_bits`]. For shifts of 320 or more, the
/// result is zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u320::U320;
///
/// let a = U320::from_be_limbs([0, 0, 0, 0, 1]);
/// assert_eq!(a << 3, U320::from_be_limbs([0, 0, 0, 0, 8]));
/// ```
impl Shl<u32> for U320 {
type Output = U320;
#[inline]
fn shl(self, rhs: u32) -> U320 {
self.shl_bits(rhs)
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Shift by zero is identity.
#[test]
fn identity() {
let a = U320::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1111]);
assert_eq!(a << 0, a);
}
/// Shift by 1 doubles the value.
#[test]
fn shift_one() {
assert_eq!(
U320::from_be_limbs([0, 0, 0, 0, 4]) << 1,
U320::from_be_limbs([0, 0, 0, 0, 8]),
);
}
/// Shift by 64 moves one limb.
#[test]
fn one_limb() {
let a = U320::from_be_limbs([0, 0, 0, 0, 1]);
assert_eq!(a << 64, U320::from_be_limbs([0, 0, 0, 1, 0]));
}
/// Shift by 320 or more produces zero.
#[test]
fn full_shift() {
assert_eq!(U320::MAX << 320, U320::ZERO);
assert_eq!(U320::MAX << 400, U320::ZERO);
}
/// Matches shl_bits behavior.
#[test]
fn matches_shl_bits() {
let a = U320::from_be_limbs([0, 0x1234, 0x5678, 0x9ABC, 0xDEF0]);
assert_eq!(a << 17, a.shl_bits(17));
}
}