Skip to content

Commit dce75e1

Browse files
committed
Avoid allocation in the Toom-3 finalization
1 parent bc967f6 commit dce75e1

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/biguint/multiplication.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::addition::{__add2, add2};
22
use super::subtraction::sub2;
33
#[cfg(not(u64_digit))]
44
use super::u32_from_u128;
5-
use super::{biguint_from_vec, cmp_slice, BigUint};
5+
use super::{biguint_from_vec, cmp_slice, BigUint, IntDigits};
66

77
use crate::big_digit::{self, BigDigit, DoubleBigDigit};
88
use crate::Sign::{self, Minus, NoSign, Plus};
@@ -322,14 +322,24 @@ fn mac3(mut acc: &mut [BigDigit], mut b: &[BigDigit], mut c: &[BigDigit]) {
322322
// Recomposition. The coefficients of the polynomial are now known.
323323
//
324324
// Evaluate at w(t) where t is our given base to get the result.
325-
let bits = u64::from(big_digit::BITS) * i as u64;
326-
let result = r0
327-
+ (comp1 << bits)
328-
+ (comp2 << (2 * bits))
329-
+ (comp3 << (3 * bits))
330-
+ (r4 << (4 * bits));
331-
let result_pos = result.to_biguint().unwrap();
332-
add2(&mut acc[..], &result_pos.data);
325+
//
326+
// let bits = u64::from(big_digit::BITS) * i as u64;
327+
// let result = r0
328+
// + (comp1 << bits)
329+
// + (comp2 << (2 * bits))
330+
// + (comp3 << (3 * bits))
331+
// + (r4 << (4 * bits));
332+
// let result_pos = result.to_biguint().unwrap();
333+
// add2(&mut acc[..], &result_pos.data);
334+
//
335+
// But with less intermediate copying:
336+
for (j, result) in [&r0, &comp1, &comp2, &comp3, &r4].iter().enumerate().rev() {
337+
match result.sign() {
338+
Plus => add2(&mut acc[i * j..], result.digits()),
339+
Minus => sub2(&mut acc[i * j..], result.digits()),
340+
NoSign => {}
341+
}
342+
}
333343
}
334344
}
335345

0 commit comments

Comments
 (0)