Skip to content

Commit

Permalink
implement xorshift* for benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Mar 5, 2021
1 parent 998d4cb commit 4247abe
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,3 @@ default-features = false

[build-dependencies]
autocfg = "1"

[dev-dependencies]
rand_xorshift = "0.3"
7 changes: 2 additions & 5 deletions benches/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ extern crate test;

use num_bigint::{BigInt, BigUint, RandBigInt};
use num_traits::{FromPrimitive, Num, One, Zero};
use rand::prelude::*;
use rand_xorshift::XorShiftRng;
use std::mem::replace;
use test::Bencher;

fn get_rng() -> impl Rng {
XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0)
}
mod rng;
use rng::get_rng;

fn multiply_bench(b: &mut Bencher, xbits: u64, ybits: u64) {
let mut rng = get_rng();
Expand Down
7 changes: 2 additions & 5 deletions benches/gcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ extern crate test;
use num_bigint::{BigUint, RandBigInt};
use num_integer::Integer;
use num_traits::Zero;
use rand::prelude::*;
use rand_xorshift::XorShiftRng;
use test::Bencher;

fn get_rng() -> impl Rng {
XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0)
}
mod rng;
use rng::get_rng;

fn bench(b: &mut Bencher, bits: u64, gcd: fn(&BigUint, &BigUint) -> BigUint) {
let mut rng = get_rng();
Expand Down
38 changes: 38 additions & 0 deletions benches/rng/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use rand::RngCore;

pub(crate) fn get_rng() -> impl RngCore {
XorShiftStar {
a: 0x0123_4567_89AB_CDEF,
}
}

/// Simple `Rng` for benchmarking without additional dependencies
struct XorShiftStar {
a: u64,
}

impl RngCore for XorShiftStar {
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}

fn next_u64(&mut self) -> u64 {
// https://en.wikipedia.org/wiki/Xorshift#xorshift*
self.a ^= self.a >> 12;
self.a ^= self.a << 25;
self.a ^= self.a >> 27;
self.a.wrapping_mul(0x2545_F491_4F6C_DD1D)
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
for chunk in dest.chunks_mut(8) {
let bytes = self.next_u64().to_le_bytes();
let slice = &bytes[..chunk.len()];
chunk.copy_from_slice(slice)
}
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
Ok(self.fill_bytes(dest))
}
}
9 changes: 3 additions & 6 deletions benches/roots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
extern crate test;

use num_bigint::{BigUint, RandBigInt};
use rand::prelude::*;
use rand_xorshift::XorShiftRng;
use test::Bencher;

mod rng;
use rng::get_rng;

// The `big64` cases demonstrate the speed of cases where the value
// can be converted to a `u64` primitive for faster calculation.
//
Expand All @@ -16,10 +17,6 @@ use test::Bencher;
//
// The `big2k` and `big4k` cases are too big for `f64`, and use a simpler guess.

fn get_rng() -> impl Rng {
XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0)
}

fn check(x: &BigUint, n: u32) {
let root = x.nth_root(n);
if n == 2 {
Expand Down
2 changes: 1 addition & 1 deletion ci/big_rand/src/torture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use num_traits::Zero;
use rand::prelude::*;
use rand_xorshift::XorShiftRng;

fn get_rng() -> impl Rng {
fn get_rng() -> XorShiftRng {
XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0)
}

Expand Down

0 comments on commit 4247abe

Please sign in to comment.