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
//! Branchless zero check for [`U320`].
use super::U320;
impl U320 {
/// Returns `true` if all five limbs are zero.
///
/// Uses bitwise OR to combine all limbs into a single comparison,
/// producing a branchless check that avoids short-circuit evaluation.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u320::U320;
///
/// assert!(U320::from_be_limbs([0, 0, 0, 0, 0]).is_zero());
/// assert!(!U320::from_be_limbs([0, 0, 0, 0, 1]).is_zero());
/// ```
#[inline]
pub const fn is_zero(&self) -> bool {
(self.0[0] | self.0[1] | self.0[2] | self.0[3] | self.0[4]) == 0
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Zero is zero.
#[test]
fn zero_is_zero() {
assert!(U320::ZERO.is_zero());
}
/// One is not zero.
#[test]
fn one_is_not_zero() {
assert!(!U320::ONE.is_zero());
}
/// Max is not zero.
#[test]
fn max_is_not_zero() {
assert!(!U320::MAX.is_zero());
}
/// Only the MSB limb set is not zero.
#[test]
fn msb_only() {
assert!(!U320::from_be_limbs([1, 0, 0, 0, 0]).is_zero());
}
/// Default is zero.
#[test]
fn default_is_zero() {
assert!(U320::default().is_zero());
}
}