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
//! Leading zero byte count for [`U384`].
use super::U384;
impl U384 {
/// Returns the number of leading zero bytes in the 48-byte big-endian
/// representation.
///
/// Computed as `leading_zeros() / 8`. Returns 48 for the zero value
/// and 0 when the most significant byte is non-zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u384::U384;
///
/// assert_eq!(U384::ZERO.leading_zero_bytes(), 48);
/// assert_eq!(U384::ONE.leading_zero_bytes(), 47);
/// assert_eq!(U384::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 48 leading zero bytes.
#[test]
fn zero() {
assert_eq!(U384::ZERO.leading_zero_bytes(), 48);
}
/// One has 47 leading zero bytes.
#[test]
fn one() {
assert_eq!(U384::ONE.leading_zero_bytes(), 47);
}
/// MAX has 0 leading zero bytes.
#[test]
fn max() {
assert_eq!(U384::MAX.leading_zero_bytes(), 0);
}
/// Value 0xFF has 47 leading zero bytes.
#[test]
fn byte_value() {
let v = U384::from_be_limbs([0, 0, 0, 0, 0, 0xFF]);
assert_eq!(v.leading_zero_bytes(), 47);
}
/// Value 0x100 has 46 leading zero bytes.
#[test]
fn two_byte_value() {
let v = U384::from_be_limbs([0, 0, 0, 0, 0, 0x100]);
assert_eq!(v.leading_zero_bytes(), 46);
}
}