Skip to content

Commit 480a4da

Browse files
bors[bot]cuviper
andauthoredJan 14, 2020
Merge #129
129: Fix the bit shifts at the end of Toom-3 r=cuviper a=cuviper This fixes a regression from 64-bit `BigDigit` (#62). Co-authored-by: Josh Stone <cuviper@gmail.com>
2 parents 2e4c601 + bb33a77 commit 480a4da

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed
 

‎src/algorithms.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,12 @@ fn mac3(acc: &mut [BigDigit], b: &[BigDigit], c: &[BigDigit]) {
517517
// Recomposition. The coefficients of the polynomial are now known.
518518
//
519519
// Evaluate at w(t) where t is our given base to get the result.
520+
let bits = big_digit::BITS * i;
520521
let result = r0
521-
+ (comp1 << (32 * i))
522-
+ (comp2 << (2 * 32 * i))
523-
+ (comp3 << (3 * 32 * i))
524-
+ (r4 << (4 * 32 * i));
522+
+ (comp1 << bits)
523+
+ (comp2 << (2 * bits))
524+
+ (comp3 << (3 * bits))
525+
+ (r4 << (4 * bits));
525526
let result_pos = result.to_biguint().unwrap();
526527
add2(&mut acc[..], &result_pos.data);
527528
}

‎tests/torture.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ use num_bigint::RandBigInt;
88
use num_traits::Zero;
99
use rand::prelude::*;
1010

11+
fn get_rng() -> SmallRng {
12+
let seed = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
13+
SmallRng::from_seed(seed)
14+
}
15+
1116
fn test_mul_divide_torture_count(count: usize) {
1217
let bits_max = 1 << 12;
13-
let seed = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
14-
let mut rng = SmallRng::from_seed(seed);
18+
let mut rng = get_rng();
1519

1620
for _ in 0..count {
1721
// Test with numbers of random sizes:
@@ -33,11 +37,40 @@ fn test_mul_divide_torture_count(count: usize) {
3337

3438
#[test]
3539
fn test_mul_divide_torture() {
36-
test_mul_divide_torture_count(1000);
40+
test_mul_divide_torture_count(1_000);
3741
}
3842

3943
#[test]
4044
#[ignore]
4145
fn test_mul_divide_torture_long() {
42-
test_mul_divide_torture_count(1000000);
46+
test_mul_divide_torture_count(1_000_000);
47+
}
48+
49+
fn test_factored_mul_torture_count(count: usize) {
50+
let bits = 1 << 16;
51+
let mut rng = get_rng();
52+
53+
for _ in 0..count {
54+
let w = rng.gen_biguint(bits);
55+
let x = rng.gen_biguint(bits);
56+
let y = rng.gen_biguint(bits);
57+
let z = rng.gen_biguint(bits);
58+
59+
let prod1 = (&w * &x) * (&y * &z);
60+
let prod2 = (&w * &y) * (&x * &z);
61+
let prod3 = (&w * &z) * (&x * &y);
62+
assert_eq!(prod1, prod2);
63+
assert_eq!(prod2, prod3);
64+
}
65+
}
66+
67+
#[test]
68+
fn test_factored_mul_torture() {
69+
test_factored_mul_torture_count(50);
70+
}
71+
72+
#[test]
73+
#[ignore]
74+
fn test_factored_mul_torture_long() {
75+
test_factored_mul_torture_count(1_000);
4376
}

0 commit comments

Comments
 (0)