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
69
70
//! Byte-level leading zero count for [`U320`].
use super::U320;
impl U320 {
/// Returns the number of leading zero bytes in the 320-bit value.
///
/// Equivalent to `leading_zeros() / 8`, counting complete zero bytes
/// from the most significant position. Returns 0-40.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u320::U320;
///
/// assert_eq!(U320::ZERO.leading_zero_bytes(), 40);
/// assert_eq!(U320::ONE.leading_zero_bytes(), 39);
/// assert_eq!(U320::MAX.leading_zero_bytes(), 0);
/// ```
#[inline]
pub const fn leading_zero_bytes(&self) -> u32 {
self.leading_zeros() / 8
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Zero has 40 leading zero bytes.
#[test]
fn zero() {
assert_eq!(U320::ZERO.leading_zero_bytes(), 40);
}
/// One has 39 leading zero bytes.
#[test]
fn one() {
assert_eq!(U320::ONE.leading_zero_bytes(), 39);
}
/// MAX has 0 leading zero bytes.
#[test]
fn max() {
assert_eq!(U320::MAX.leading_zero_bytes(), 0);
}
/// 0xFF in the LSB has 39 leading zero bytes.
#[test]
fn single_byte() {
assert_eq!(U320::from_be_limbs([0, 0, 0, 0, 0xFF]).leading_zero_bytes(), 39);
}
/// 0x100 spans into a second byte, so 38 leading zero bytes.
#[test]
fn two_byte_value() {
assert_eq!(U320::from_be_limbs([0, 0, 0, 0, 0x100]).leading_zero_bytes(), 38);
}
/// Full first limb means 32 leading zero bytes.
#[test]
fn full_lsb_limb() {
assert_eq!(U320::from_be_limbs([0, 0, 0, 0, u64::MAX]).leading_zero_bytes(), 32);
}
/// Value in the MSB limb.
#[test]
fn msb_limb() {
assert_eq!(U320::from_be_limbs([1, 0, 0, 0, 0]).leading_zero_bytes(), 7);
}
}