Skip to content

Commit 4247abe

Browse files
committed
implement xorshift* for benchmarking
1 parent 998d4cb commit 4247abe

File tree

6 files changed

+46
-20
lines changed

6 files changed

+46
-20
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,3 @@ default-features = false
7272

7373
[build-dependencies]
7474
autocfg = "1"
75-
76-
[dev-dependencies]
77-
rand_xorshift = "0.3"

benches/bigint.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ extern crate test;
55

66
use num_bigint::{BigInt, BigUint, RandBigInt};
77
use num_traits::{FromPrimitive, Num, One, Zero};
8-
use rand::prelude::*;
9-
use rand_xorshift::XorShiftRng;
108
use std::mem::replace;
119
use test::Bencher;
1210

13-
fn get_rng() -> impl Rng {
14-
XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0)
15-
}
11+
mod rng;
12+
use rng::get_rng;
1613

1714
fn multiply_bench(b: &mut Bencher, xbits: u64, ybits: u64) {
1815
let mut rng = get_rng();

benches/gcd.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ extern crate test;
66
use num_bigint::{BigUint, RandBigInt};
77
use num_integer::Integer;
88
use num_traits::Zero;
9-
use rand::prelude::*;
10-
use rand_xorshift::XorShiftRng;
119
use test::Bencher;
1210

13-
fn get_rng() -> impl Rng {
14-
XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0)
15-
}
11+
mod rng;
12+
use rng::get_rng;
1613

1714
fn bench(b: &mut Bencher, bits: u64, gcd: fn(&BigUint, &BigUint) -> BigUint) {
1815
let mut rng = get_rng();

benches/rng/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use rand::RngCore;
2+
3+
pub(crate) fn get_rng() -> impl RngCore {
4+
XorShiftStar {
5+
a: 0x0123_4567_89AB_CDEF,
6+
}
7+
}
8+
9+
/// Simple `Rng` for benchmarking without additional dependencies
10+
struct XorShiftStar {
11+
a: u64,
12+
}
13+
14+
impl RngCore for XorShiftStar {
15+
fn next_u32(&mut self) -> u32 {
16+
self.next_u64() as u32
17+
}
18+
19+
fn next_u64(&mut self) -> u64 {
20+
// https://en.wikipedia.org/wiki/Xorshift#xorshift*
21+
self.a ^= self.a >> 12;
22+
self.a ^= self.a << 25;
23+
self.a ^= self.a >> 27;
24+
self.a.wrapping_mul(0x2545_F491_4F6C_DD1D)
25+
}
26+
27+
fn fill_bytes(&mut self, dest: &mut [u8]) {
28+
for chunk in dest.chunks_mut(8) {
29+
let bytes = self.next_u64().to_le_bytes();
30+
let slice = &bytes[..chunk.len()];
31+
chunk.copy_from_slice(slice)
32+
}
33+
}
34+
35+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
36+
Ok(self.fill_bytes(dest))
37+
}
38+
}

benches/roots.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
extern crate test;
55

66
use num_bigint::{BigUint, RandBigInt};
7-
use rand::prelude::*;
8-
use rand_xorshift::XorShiftRng;
97
use test::Bencher;
108

9+
mod rng;
10+
use rng::get_rng;
11+
1112
// The `big64` cases demonstrate the speed of cases where the value
1213
// can be converted to a `u64` primitive for faster calculation.
1314
//
@@ -16,10 +17,6 @@ use test::Bencher;
1617
//
1718
// The `big2k` and `big4k` cases are too big for `f64`, and use a simpler guess.
1819

19-
fn get_rng() -> impl Rng {
20-
XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0)
21-
}
22-
2320
fn check(x: &BigUint, n: u32) {
2421
let root = x.nth_root(n);
2522
if n == 2 {

ci/big_rand/src/torture.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use num_traits::Zero;
33
use rand::prelude::*;
44
use rand_xorshift::XorShiftRng;
55

6-
fn get_rng() -> impl Rng {
6+
fn get_rng() -> XorShiftRng {
77
XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0)
88
}
99

0 commit comments

Comments
 (0)