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
//! Checked 320-bit addition returning [`None`] on overflow.
use super::U320;
impl U320 {
/// Computes `self + other`, returning `None` if the result would
/// overflow 320 bits.
///
/// Delegates to [`U320::overflowing_add`] and converts the overflow
/// flag into an [`Option`].
///
/// # Examples
///
/// ```
/// use cnfy_uint::u320::U320;
///
/// let a = U320::from_be_limbs([0, 0, 0, 0, 10]);
/// let b = U320::from_be_limbs([0, 0, 0, 0, 20]);
/// assert_eq!(a.checked_add(&b), Some(U320::from_be_limbs([0, 0, 0, 0, 30])));
/// assert_eq!(U320::MAX.checked_add(&U320::ONE), None);
/// ```
#[inline]
pub const fn checked_add(&self, other: &U320) -> Option<U320> {
let (result, overflow) = self.overflowing_add(other);
if overflow {
None
} else {
Some(result)
}
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Non-overflowing addition returns Some.
#[test]
fn no_overflow() {
let a = U320::from_be_limbs([0, 0, 0, 0, 10]);
let b = U320::from_be_limbs([0, 0, 0, 0, 20]);
assert_eq!(a.checked_add(&b), Some(U320::from_be_limbs([0, 0, 0, 0, 30])));
}
/// Overflowing addition returns None.
#[test]
fn overflow_returns_none() {
assert_eq!(U320::MAX.checked_add(&U320::ONE), None);
}
/// Adding zero returns Some(self).
#[test]
fn add_zero() {
let a = U320::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1111]);
assert_eq!(a.checked_add(&U320::ZERO), Some(a));
}
/// MAX + MAX returns None.
#[test]
fn max_plus_max() {
assert_eq!(U320::MAX.checked_add(&U320::MAX), None);
}
/// Large non-overflowing addition.
#[test]
fn large_no_overflow() {
let a = U320::from_be_limbs([0, u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
let result = a.checked_add(&U320::ONE);
assert_eq!(result, Some(U320::from_be_limbs([1, 0, 0, 0, 0])));
}
}