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
//! Branchless zero check for `U512`.
use super::U512;
impl U512 {
/// Returns `true` if all eight 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::u512::U512;
///
/// assert!(U512::from_be_limbs([0; 8]).is_zero());
/// assert!(!U512::from_be_limbs([0, 0, 0, 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]
| self.0[5]
| self.0[6]
| self.0[7])
== 0
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Zero is zero.
#[test]
fn zero_is_zero() {
assert!(U512::from_be_limbs([0; 8]).is_zero());
}
/// One is not zero.
#[test]
fn one_is_not_zero() {
assert!(!U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 1]).is_zero());
}
/// Max is not zero.
#[test]
fn max_is_not_zero() {
assert!(!U512::from_be_limbs([u64::MAX; 8]).is_zero());
}
/// Only the MSB limb set is not zero.
#[test]
fn msb_only() {
assert!(!U512::from_be_limbs([1, 0, 0, 0, 0, 0, 0, 0]).is_zero());
}
}