ruint/algorithms/
add.rs

1/// ⚠️ Add with carry.
2#[doc = crate::algorithms::unstable_warning!()]
3/// Helper while [Rust#85532](https://github.com/rust-lang/rust/issues/85532) stabilizes.
4#[inline]
5#[must_use]
6pub const fn carrying_add(lhs: u64, rhs: u64, carry: bool) -> (u64, bool) {
7    #[cfg(feature = "nightly")]
8    {
9        lhs.carrying_add(rhs, carry)
10    }
11    #[cfg(not(feature = "nightly"))]
12    {
13        let (result, carry_1) = lhs.overflowing_add(rhs);
14        let (result, carry_2) = result.overflowing_add(carry as u64);
15        (result, carry_1 | carry_2)
16    }
17}
18
19/// ⚠️ Sub with borrow.
20#[doc = crate::algorithms::unstable_warning!()]
21/// Helper while [Rust#85532](https://github.com/rust-lang/rust/issues/85532) stabilizes.
22#[inline]
23#[must_use]
24pub const fn borrowing_sub(lhs: u64, rhs: u64, borrow: bool) -> (u64, bool) {
25    #[cfg(feature = "nightly")]
26    {
27        lhs.borrowing_sub(rhs, borrow)
28    }
29    #[cfg(not(feature = "nightly"))]
30    {
31        let (result, borrow_1) = lhs.overflowing_sub(rhs);
32        let (result, borrow_2) = result.overflowing_sub(borrow as u64);
33        (result, borrow_1 | borrow_2)
34    }
35}
36
37/// ⚠️ `lhs += rhs + carry`
38#[doc = crate::algorithms::unstable_warning!()]
39#[inline(always)]
40pub fn carrying_add_n(lhs: &mut [u64], rhs: &[u64], mut carry: bool) -> bool {
41    debug_assert!(lhs.len() == rhs.len());
42    for i in 0..lhs.len() {
43        (lhs[i], carry) = carrying_add(lhs[i], rhs[i], carry);
44    }
45    carry
46}
47
48/// ⚠️ `lhs -= rhs - borrow`
49#[doc = crate::algorithms::unstable_warning!()]
50#[inline(always)]
51pub fn borrowing_sub_n(lhs: &mut [u64], rhs: &[u64], mut borrow: bool) -> bool {
52    debug_assert!(lhs.len() == rhs.len());
53    for i in 0..lhs.len() {
54        (lhs[i], borrow) = borrowing_sub(lhs[i], rhs[i], borrow);
55    }
56    borrow
57}