Skip to content

Commit c528be1

Browse files
committed
Skip trailing zeros in multiplication
1 parent 42c9c9a commit c528be1

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/biguint/multiplication.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,20 @@ fn bigint_from_slice(slice: &[BigDigit]) -> BigInt {
6666
/// Three argument multiply accumulate:
6767
/// acc += b * c
6868
#[allow(clippy::many_single_char_names)]
69-
fn mac3(acc: &mut [BigDigit], b: &[BigDigit], c: &[BigDigit]) {
69+
fn mac3(mut acc: &mut [BigDigit], mut b: &[BigDigit], mut c: &[BigDigit]) {
70+
// Least-significant zeros have no effect on the output.
71+
if let Some(&0) = b.first() {
72+
let nz = b.iter().position(|&d| d != 0).unwrap();
73+
b = &b[nz..];
74+
acc = &mut acc[nz..];
75+
}
76+
if let Some(&0) = c.first() {
77+
let nz = c.iter().position(|&d| d != 0).unwrap();
78+
c = &c[nz..];
79+
acc = &mut acc[nz..];
80+
}
81+
82+
let acc = acc;
7083
let (x, y) = if b.len() < c.len() { (b, c) } else { (c, b) };
7184

7285
// We use three algorithms for different input sizes.

0 commit comments

Comments
 (0)