Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve add and sub performance by avoiding array::IntoIter #316

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
}
let mut carry = 0_u128;
#[allow(clippy::cast_possible_truncation)] // Intentional
for (lhs, rhs) in self.limbs.iter_mut().zip(rhs.limbs) {
for (lhs, &rhs) in self.limbs.iter_mut().zip(rhs.as_limbs()) {
carry += u128::from(*lhs) + u128::from(rhs);
*lhs = carry as u64;
carry >>= 64;
Expand Down Expand Up @@ -100,7 +100,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
let mut carry = 0_i128;
#[allow(clippy::cast_possible_truncation)] // Intentional
#[allow(clippy::cast_sign_loss)] // Intentional
for (lhs, rhs) in self.limbs.iter_mut().zip(rhs.limbs) {
for (lhs, &rhs) in self.limbs.iter_mut().zip(rhs.as_limbs()) {
carry += i128::from(*lhs) - i128::from(rhs);
*lhs = carry as u64;
carry >>= 64; // Sign extending shift
Expand Down
2 changes: 1 addition & 1 deletion src/base_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
}

// Add digit to result
let overflow = addmul_nx1(&mut result.limbs, &power.limbs, digit);
let overflow = addmul_nx1(&mut result.limbs, power.as_limbs(), digit);
if overflow != 0 || result.limbs[LIMBS - 1] > Self::MASK {
return Err(BaseConvertError::Overflow);
}
Expand Down
2 changes: 1 addition & 1 deletion src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ macro_rules! impl_bit_op {
for Uint<BITS, LIMBS>
{
fn $fn_assign(&mut self, rhs: &Uint<BITS, LIMBS>) {
for (limb, rhs) in self.limbs.iter_mut().zip(rhs.limbs) {
for (limb, &rhs) in self.limbs.iter_mut().zip(rhs.as_limbs()) {
u64::$fn_assign(limb, rhs);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/modular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
// Alternatively we could use `alloca`, but that is blocked on
// See <https://github.com/rust-lang/rust/issues/48055>
let mut product = vec![0; crate::nlimbs(2 * BITS)];
let overflow = algorithms::addmul(&mut product, &self.limbs, &rhs.limbs);
let overflow = algorithms::addmul(&mut product, self.as_limbs(), rhs.as_limbs());
debug_assert!(!overflow);

// Compute modulus using `div_rem`.
Expand Down Expand Up @@ -152,10 +152,10 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
assert_eq!(inv.wrapping_mul(modulus.limbs[0]), u64::MAX);
let mut result = Self::ZERO;
algorithms::mul_redc(
&self.limbs,
&other.limbs,
self.as_limbs(),
other.as_limbs(),
&mut result.limbs,
&modulus.limbs,
modulus.as_limbs(),
inv,
);
debug_assert!(result < modulus);
Expand Down
2 changes: 1 addition & 1 deletion src/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
assert_eq!(BITS_RES, BITS + BITS_RHS);
assert_eq!(LIMBS_RES, nlimbs(BITS_RES));
let mut result = Uint::<BITS_RES, LIMBS_RES>::ZERO;
algorithms::addmul(&mut result.limbs, &self.limbs, &rhs.limbs);
algorithms::addmul(&mut result.limbs, self.as_limbs(), rhs.as_limbs());
if LIMBS_RES > 0 {
debug_assert!(result.limbs[LIMBS_RES - 1] <= Uint::<BITS_RES, LIMBS_RES>::MASK);
}
Expand Down