Skip to content

Commit 71b78bc

Browse files
authored
Merge pull request #308 from cuviper/less-wide
Avoid some widening operations
2 parents f511841 + 98bea13 commit 71b78bc

File tree

4 files changed

+17
-24
lines changed

4 files changed

+17
-24
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/monty.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::mem;
33
use core::ops::Shl;
44
use num_traits::One;
55

6-
use crate::big_digit::{self, BigDigit, DoubleBigDigit, SignedDoubleBigDigit};
6+
use crate::big_digit::{self, BigDigit, DoubleBigDigit};
77
use crate::biguint::BigUint;
88

99
struct MontyReducer {
@@ -15,16 +15,17 @@ struct MontyReducer {
1515
fn inv_mod_alt(b: BigDigit) -> BigDigit {
1616
assert_ne!(b & 1, 0);
1717

18-
let mut k0 = 2 - b as SignedDoubleBigDigit;
19-
let mut t = (b - 1) as SignedDoubleBigDigit;
18+
let mut k0 = BigDigit::wrapping_sub(2, b);
19+
let mut t = b - 1;
2020
let mut i = 1;
2121
while i < big_digit::BITS {
2222
t = t.wrapping_mul(t);
2323
k0 = k0.wrapping_mul(t + 1);
2424

2525
i <<= 1;
2626
}
27-
-k0 as BigDigit
27+
debug_assert_eq!(k0.wrapping_mul(b), 1);
28+
k0.wrapping_neg()
2829
}
2930

3031
impl MontyReducer {

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]) {

src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,6 @@ mod big_digit {
238238
pub(crate) type DoubleBigDigit = u128;
239239
);
240240

241-
// A [`SignedDoubleBigDigit`] is the signed version of [`DoubleBigDigit`].
242-
cfg_digit!(
243-
pub(crate) type SignedDoubleBigDigit = i64;
244-
pub(crate) type SignedDoubleBigDigit = i128;
245-
);
246-
247241
pub(crate) const BITS: u8 = BigDigit::BITS as u8;
248242
pub(crate) const HALF_BITS: u8 = BITS / 2;
249243
pub(crate) const HALF: BigDigit = (1 << HALF_BITS) - 1;

0 commit comments

Comments
 (0)