Skip to content

Commit d9fb6e6

Browse files
committed
Mimic the standard library for adc and sbb fallbacks
The intrinsic x86/x86_64 versions are still faster though.
1 parent f511841 commit d9fb6e6

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

src/biguint/addition.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ fn adc(carry: u8, a: u32, b: u32, out: &mut u32) -> u8 {
2525
}
2626

2727
// fallback for environments where we don't have an addcarry intrinsic
28+
// (copied from the standard library's `carrying_add`)
2829
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
2930
#[inline]
30-
fn adc(carry: u8, a: BigDigit, b: BigDigit, out: &mut BigDigit) -> u8 {
31-
use crate::big_digit::DoubleBigDigit;
32-
33-
let sum = DoubleBigDigit::from(a) + DoubleBigDigit::from(b) + DoubleBigDigit::from(carry);
34-
*out = sum as BigDigit;
35-
(sum >> big_digit::BITS) as u8
31+
fn adc(carry: u8, lhs: BigDigit, rhs: BigDigit, out: &mut BigDigit) -> u8 {
32+
let (a, b) = lhs.overflowing_add(rhs);
33+
let (c, d) = a.overflowing_add(carry as BigDigit);
34+
*out = c;
35+
u8::from(b || d)
3636
}
3737

3838
/// Two argument addition of raw slices, `a += b`, returning the carry.

src/biguint/subtraction.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ fn sbb(borrow: u8, a: u32, b: u32, out: &mut u32) -> u8 {
2525
}
2626

2727
// fallback for environments where we don't have a subborrow intrinsic
28+
// (copied from the standard library's `borrowing_sub`)
2829
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
2930
#[inline]
30-
fn sbb(borrow: u8, a: BigDigit, b: BigDigit, out: &mut BigDigit) -> u8 {
31-
use crate::big_digit::SignedDoubleBigDigit;
32-
33-
let difference = SignedDoubleBigDigit::from(a)
34-
- SignedDoubleBigDigit::from(b)
35-
- SignedDoubleBigDigit::from(borrow);
36-
*out = difference as BigDigit;
37-
u8::from(difference < 0)
31+
fn sbb(borrow: u8, lhs: BigDigit, rhs: BigDigit, out: &mut BigDigit) -> u8 {
32+
let (a, b) = lhs.overflowing_sub(rhs);
33+
let (c, d) = a.overflowing_sub(borrow as BigDigit);
34+
*out = c;
35+
u8::from(b || d)
3836
}
3937

4038
pub(super) fn sub2(a: &mut [BigDigit], b: &[BigDigit]) {

0 commit comments

Comments
 (0)