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
71
72
73
74
//! Checked 320-bit subtraction returning [`None`] on underflow.
use super::U320;
impl U320 {
/// Computes `self - other`, returning `None` if the result would
/// underflow (i.e., `other > self`).
///
/// Delegates to [`U320::overflowing_sub`] and converts the underflow
/// 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, 3]);
/// assert_eq!(a.checked_sub(&b), Some(U320::from_be_limbs([0, 0, 0, 0, 7])));
/// assert_eq!(U320::ZERO.checked_sub(&U320::ONE), None);
/// ```
#[inline]
pub const fn checked_sub(&self, other: &U320) -> Option<U320> {
let (result, underflow) = self.overflowing_sub(other);
if underflow {
None
} else {
Some(result)
}
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Non-underflowing subtraction returns Some.
#[test]
fn no_underflow() {
let a = U320::from_be_limbs([0, 0, 0, 0, 10]);
let b = U320::from_be_limbs([0, 0, 0, 0, 3]);
assert_eq!(a.checked_sub(&b), Some(U320::from_be_limbs([0, 0, 0, 0, 7])));
}
/// Underflowing subtraction returns None.
#[test]
fn underflow_returns_none() {
assert_eq!(U320::ZERO.checked_sub(&U320::ONE), None);
}
/// Subtracting zero returns Some(self).
#[test]
fn sub_zero() {
let a = U320::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1111]);
assert_eq!(a.checked_sub(&U320::ZERO), Some(a));
}
/// Subtracting self returns Some(ZERO).
#[test]
fn self_cancellation() {
let a = U320::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1111]);
assert_eq!(a.checked_sub(&a), Some(U320::ZERO));
}
/// Larger minus smaller across limbs.
#[test]
fn cross_limb() {
let a = U320::from_be_limbs([1, 0, 0, 0, 0]);
let b = U320::ONE;
assert_eq!(
a.checked_sub(&b),
Some(U320::from_be_limbs([0, u64::MAX, u64::MAX, u64::MAX, u64::MAX])),
);
}
}