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

Optimizations for multiplication #199

Merged
merged 6 commits into from Mar 14, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion benches/bigint.rs
Expand Up @@ -39,7 +39,7 @@ fn factorial(n: usize) -> BigUint {
let mut f: BigUint = One::one();
for i in 1..=n {
let bu: BigUint = FromPrimitive::from_usize(i).unwrap();
f += bu;
f *= bu;
}
f
}
Expand Down Expand Up @@ -351,6 +351,21 @@ fn pow_bench_bigexp(b: &mut Bencher) {
});
}

#[bench]
fn pow_bench_1e1000(b: &mut Bencher) {
b.iter(|| BigUint::from(10u32).pow(1_000));
}

#[bench]
fn pow_bench_1e10000(b: &mut Bencher) {
b.iter(|| BigUint::from(10u32).pow(10_000));
}

#[bench]
fn pow_bench_1e100000(b: &mut Bencher) {
b.iter(|| BigUint::from(10u32).pow(100_000));
}

/// This modulus is the prime from the 2048-bit MODP DH group:
/// https://tools.ietf.org/html/rfc3526#section-3
const RFC3526_2048BIT_MODP_GROUP: &str = "\
Expand Down
57 changes: 41 additions & 16 deletions src/bigint/multiplication.rs
Expand Up @@ -21,24 +21,49 @@ impl Mul<Sign> for Sign {
}
}

forward_all_binop_to_ref_ref!(impl Mul for BigInt, mul);

impl<'a, 'b> Mul<&'b BigInt> for &'a BigInt {
type Output = BigInt;

#[inline]
fn mul(self, other: &BigInt) -> BigInt {
BigInt::from_biguint(self.sign * other.sign, &self.data * &other.data)
}
macro_rules! impl_mul {
($(impl<$($a:lifetime),*> Mul<$Other:ty> for $Self:ty;)*) => {$(
impl<$($a),*> Mul<$Other> for $Self {
type Output = BigInt;

#[inline]
fn mul(self, other: $Other) -> BigInt {
// automatically match value/ref
let BigInt { data: x, .. } = self;
let BigInt { data: y, .. } = other;
BigInt::from_biguint(self.sign * other.sign, x * y)
}
}
)*}
}
impl_mul! {
impl<> Mul<BigInt> for BigInt;
impl<'b> Mul<&'b BigInt> for BigInt;
impl<'a> Mul<BigInt> for &'a BigInt;
impl<'a, 'b> Mul<&'b BigInt> for &'a BigInt;
}

macro_rules! impl_mul_assign {
($(impl<$($a:lifetime),*> MulAssign<$Other:ty> for BigInt;)*) => {$(
impl<$($a),*> MulAssign<$Other> for BigInt {
#[inline]
fn mul_assign(&mut self, other: $Other) {
// automatically match value/ref
let BigInt { data: y, .. } = other;
self.data *= y;
if self.data.is_zero() {
self.sign = NoSign;
} else {
self.sign = self.sign * other.sign;
}
}
}
)*}
}

impl<'a> MulAssign<&'a BigInt> for BigInt {
#[inline]
fn mul_assign(&mut self, other: &BigInt) {
*self = &*self * other;
}
impl_mul_assign! {
impl<> MulAssign<BigInt> for BigInt;
impl<'a> MulAssign<&'a BigInt> for BigInt;
}
forward_val_assign!(impl MulAssign for BigInt, mul_assign);

promote_all_scalars!(impl Mul for BigInt, mul);
promote_all_scalars_assign!(impl MulAssign for BigInt, mul_assign);
Expand Down
5 changes: 3 additions & 2 deletions src/biguint.rs
Expand Up @@ -846,8 +846,9 @@ impl BigUint {
/// be nonzero.
#[inline]
fn normalize(&mut self) {
while let Some(&0) = self.data.last() {
self.data.pop();
if let Some(&0) = self.data.last() {
let len = self.data.iter().rposition(|&d| d != 0).map_or(0, |i| i + 1);
self.data.truncate(len);
}
if self.data.len() < self.data.capacity() / 4 {
self.data.shrink_to_fit();
Expand Down