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
//! Wrapping subtraction via the [`Sub`] trait.
use super::U384;
use core::ops::Sub;
/// Wrapping subtraction of two 384-bit integers, wrapping on underflow.
///
/// Delegates to [`U384::overflowing_sub`], returning only the 384-bit
/// result. The underflow flag is silently discarded, making this
/// modular `2^384` arithmetic.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u384::U384;
///
/// let a = U384::from_be_limbs([0, 0, 0, 0, 0, 10]);
/// let b = U384::from_be_limbs([0, 0, 0, 0, 0, 3]);
/// assert_eq!(a - b, U384::from_be_limbs([0, 0, 0, 0, 0, 7]));
/// ```
impl Sub for U384 {
type Output = U384;
#[inline]
fn sub(self, rhs: U384) -> U384 {
self.overflowing_sub(&rhs).0
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Small values subtract without underflow.
#[test]
fn small_sub() {
let a = U384::from_be_limbs([0, 0, 0, 0, 0, 10]);
let b = U384::from_be_limbs([0, 0, 0, 0, 0, 3]);
assert_eq!(a - b, U384::from_be_limbs([0, 0, 0, 0, 0, 7]));
}
/// Subtracting zero is identity.
#[test]
fn subtractive_identity() {
let a = U384::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1111, 0x2222]);
assert_eq!(a - U384::ZERO, a);
}
/// Self minus self is zero.
#[test]
fn self_cancellation() {
let a = U384::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 0x1111, 0x2222]);
assert_eq!(a - a, U384::ZERO);
}
/// 0 - 1 wraps to MAX.
#[test]
fn underflow_wraps() {
assert_eq!(U384::ZERO - U384::ONE, U384::MAX);
}
/// Borrow propagates across all limbs.
#[test]
fn borrow_propagation() {
let a = U384::from_be_limbs([1, 0, 0, 0, 0, 0]);
let b = U384::from_be_limbs([0, 0, 0, 0, 0, 1]);
assert_eq!(
a - b,
U384::from_be_limbs([0, u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX]),
);
}
}