Skip to content

Commit

Permalink
Address review remarks, add benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
lleoha committed Oct 18, 2024
1 parent c53345f commit 863bd27
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
9 changes: 8 additions & 1 deletion benches/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,13 @@ fn bench_sub(c: &mut Criterion) {
group.finish();
}

criterion_group!(benches, bench_mul, bench_widening_mul, bench_div, bench_add, bench_sub,);
criterion_group!(
benches,
bench_mul,
bench_widening_mul,
bench_div,
bench_add,
bench_sub,
);

criterion_main!(benches);
20 changes: 8 additions & 12 deletions src/int/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<const LIMBS: usize> Int<LIMBS> {
pub fn checked_div(&self, rhs: &Self) -> CtOption<Self> {
NonZero::new(*rhs).and_then(|rhs| {
let (quotient, _, opposing_signs) = self.checked_div_mod(&rhs);
Self::new_from_sign_and_magnitude(opposing_signs, quotient).into()
Self::new_from_abs_sign(quotient, opposing_signs).into()
})
}

Expand Down Expand Up @@ -74,7 +74,7 @@ impl<const LIMBS: usize> Int<LIMBS> {
let quotient_sub_one = quotient.wrapping_add(&Uint::ONE);
let quotient = Uint::select(&quotient, &quotient_sub_one, increment_quotient);

Self::new_from_sign_and_magnitude(opposing_signs, quotient).into()
Self::new_from_abs_sign(quotient, opposing_signs).into()
})
}

Expand Down Expand Up @@ -103,8 +103,8 @@ impl<const LIMBS: usize> Int<LIMBS> {
/// );
/// ```
pub fn checked_div_mod_floor(&self, rhs: &Self) -> CtOption<(Self, Self)> {
let (lhs_sgn, lhs_mag) = self.sign_and_magnitude();
let (rhs_sgn, rhs_mag) = rhs.sign_and_magnitude();
let (lhs_mag, lhs_sgn) = self.abs_sign();
let (rhs_mag, rhs_sgn) = rhs.abs_sign();
let opposing_signs = lhs_sgn.xor(rhs_sgn);
NonZero::new(rhs_mag).and_then(|rhs_mag| {
let (quotient, remainder) = lhs_mag.div_rem(&rhs_mag);
Expand All @@ -120,14 +120,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
let inv_remainder = rhs_mag.wrapping_sub(&remainder);
let remainder = Uint::select(&remainder, &inv_remainder, modify);

CtOption::from(Int::new_from_sign_and_magnitude(opposing_signs, quotient)).and_then(
|quotient| {
CtOption::from(Int::new_from_sign_and_magnitude(opposing_signs, remainder))
.and_then(|remainder| {
CtOption::new((quotient, remainder), Choice::from(1u8))
})
},
)
CtOption::from(Int::new_from_abs_sign(quotient, opposing_signs)).and_then(|quotient| {
CtOption::from(Int::new_from_abs_sign(remainder, opposing_signs))
.and_then(|remainder| CtOption::new((quotient, remainder), Choice::from(1u8)))
})
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/int/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::ops::{Mul, MulAssign};

use subtle::CtOption;

use crate::{Checked, CheckedMul, ConcatMixed, ConstChoice, Int, Uint, WideningMul, Zero};
use crate::{Checked, CheckedMul, ConcatMixed, ConstChoice, Int, Uint, Zero};

impl<const LIMBS: usize> Int<LIMBS> {
/// Compute "wide" multiplication as a 3-tuple `(lo, hi, negate)`.
Expand Down
4 changes: 2 additions & 2 deletions src/int/sign.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{ConstChoice, ConstCtOption, Int, Uint};

impl<const LIMBS: usize> Int<LIMBS> {
/// Construct new [`Int`] from a sign and magnitude.
/// Returns `None` when the magnitude does not fit in an [`Int<LIMBS>`].
/// Construct new [`Int`] from an absolute value and sign.
/// Returns `None` when the absolute value does not fit in an [`Int<LIMBS>`].
pub const fn new_from_abs_sign(
abs: Uint<LIMBS>,
is_negative: ConstChoice,
Expand Down

0 comments on commit 863bd27

Please sign in to comment.