From 093fce980f79e6720fd0bf5fa0b93fe1000f69ec Mon Sep 17 00:00:00 2001 From: Jens Reidel Date: Tue, 5 Jan 2021 19:00:28 +0100 Subject: [PATCH 1/6] Bump rand to 0.8 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 76c374cc..ffebdb4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,7 @@ features = ["i128"] [dependencies.rand] optional = true -version = "0.7" +version = "0.8" default-features = false [dependencies.serde] From 28176b444aaee3031a66c1baf4bdf2f46c4e83aa Mon Sep 17 00:00:00 2001 From: Jens Reidel Date: Tue, 5 Jan 2021 22:57:41 +0100 Subject: [PATCH 2/6] Fix big_rand ci --- ci/big_rand/Cargo.toml | 8 ++++---- ci/big_rand/src/lib.rs | 4 ++-- ci/big_rand/src/torture.rs | 9 ++++++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ci/big_rand/Cargo.toml b/ci/big_rand/Cargo.toml index 199c022e..33dd20bc 100644 --- a/ci/big_rand/Cargo.toml +++ b/ci/big_rand/Cargo.toml @@ -6,9 +6,9 @@ edition = "2018" [dependencies] num-traits = "0.2.11" -rand_chacha = "0.2" -rand_isaac = "0.2" -rand_xorshift = "0.2" +rand_chacha = "0.3" +rand_isaac = "0.3" +rand_xorshift = "0.3" [dependencies.num-bigint] features = ["rand"] @@ -16,4 +16,4 @@ path = "../.." [dependencies.rand] features = ["small_rng"] -version = "0.7" +version = "0.8" diff --git a/ci/big_rand/src/lib.rs b/ci/big_rand/src/lib.rs index 1bea5750..69728777 100644 --- a/ci/big_rand/src/lib.rs +++ b/ci/big_rand/src/lib.rs @@ -11,7 +11,7 @@ mod torture; mod biguint { use num_bigint::{BigUint, RandBigInt, RandomBits}; use num_traits::Zero; - use rand::distributions::Uniform; + use rand::distributions::{Distribution, Uniform}; use rand::thread_rng; use rand::{Rng, SeedableRng}; @@ -192,7 +192,7 @@ mod biguint { let mut rng = thread_rng(); let bit_range = Uniform::new(0, 2048); - let sample_bits: Vec<_> = rng.sample_iter(&bit_range).take(100).collect(); + let sample_bits: Vec<_> = bit_range.sample_iter(&mut rng).take(100).collect(); for bits in sample_bits { let x = rng.gen_biguint(bits); for n in 2..11 { diff --git a/ci/big_rand/src/torture.rs b/ci/big_rand/src/torture.rs index 746128de..eac8c222 100644 --- a/ci/big_rand/src/torture.rs +++ b/ci/big_rand/src/torture.rs @@ -4,7 +4,10 @@ use rand::prelude::*; use rand::rngs::SmallRng; fn get_rng() -> SmallRng { - let seed = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; + let seed = [ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 26, 27, 28, 29, 30, 31, 32, + ]; SmallRng::from_seed(seed) } @@ -14,8 +17,8 @@ fn test_mul_divide_torture_count(count: usize) { for _ in 0..count { // Test with numbers of random sizes: - let xbits = rng.gen_range(0, bits_max); - let ybits = rng.gen_range(0, bits_max); + let xbits = rng.gen_range(0..bits_max); + let ybits = rng.gen_range(0..bits_max); let x = rng.gen_biguint(xbits); let y = rng.gen_biguint(ybits); From 63a89672f6e76f9eceddc968c685bb51514aa1dd Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 4 Mar 2021 19:31:03 -0800 Subject: [PATCH 3/6] Update for rand's MSRV 1.36 --- .github/workflows/ci.yaml | 3 +-- README.md | 4 ++-- bors.toml | 1 - ci/rustup.sh | 2 +- ci/test_full.sh | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9a1eab14..c6680623 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -14,9 +14,8 @@ jobs: matrix: rust: [ 1.31.0, # 2018! - 1.32.0, # rand 1.34.0, # quickcheck, has_try_from - 1.36.0, # alloc + 1.36.0, # alloc, rand stable, beta, nightly diff --git a/README.md b/README.md index d9df19bc..3311b7a8 100644 --- a/README.md +++ b/README.md @@ -29,12 +29,12 @@ if your compiler is not new enough. feature is enabled. To enable it include rand as ```toml -rand = "0.7" +rand = "0.8" num-bigint = { version = "0.3", features = ["rand"] } ``` Note that you must use the version of `rand` that `num-bigint` is compatible -with: `0.7`. +with: `0.8`. ## Releases diff --git a/bors.toml b/bors.toml index 5ef58889..1a5421b0 100644 --- a/bors.toml +++ b/bors.toml @@ -1,6 +1,5 @@ status = [ "Test (1.31.0)", - "Test (1.32.0)", "Test (1.34.0)", "Test (1.36.0)", "Test (stable)", diff --git a/ci/rustup.sh b/ci/rustup.sh index ad98c34a..ef75c700 100755 --- a/ci/rustup.sh +++ b/ci/rustup.sh @@ -5,6 +5,6 @@ set -ex ci=$(dirname $0) -for version in 1.31.0 1.32.0 1.34.0 1.36.0 stable beta nightly; do +for version in 1.31.0 1.34.0 1.36.0 stable beta nightly; do rustup run "$version" "$ci/test_full.sh" done diff --git a/ci/test_full.sh b/ci/test_full.sh index 3b3b472d..c3e2785f 100755 --- a/ci/test_full.sh +++ b/ci/test_full.sh @@ -28,8 +28,8 @@ if ! check_version $MSRV ; then fi STD_FEATURES=(serde) -check_version 1.32 && STD_FEATURES+=(rand) check_version 1.34 && STD_FEATURES+=(quickcheck) +check_version 1.36 && STD_FEATURES+=(rand) check_version 1.36 && NO_STD_FEATURES=(serde rand) check_version 1.40 && STD_FEATURES+=(arbitrary) echo "Testing supported features: ${STD_FEATURES[*]}" From 46aa21283a76c076f36c41bba82c8c35b68cf460 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 4 Mar 2021 19:31:29 -0800 Subject: [PATCH 4/6] Bump to 0.4.0-pre --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index ffebdb4c..c7947220 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,8 @@ categories = [ "algorithms", "data-structures", "science" ] license = "MIT OR Apache-2.0" name = "num-bigint" repository = "https://github.com/rust-num/num-bigint" -version = "0.3.2" +version = "0.4.0-pre" +publish = false readme = "README.md" build = "build.rs" exclude = ["/bors.toml", "/ci/*", "/.github/*"] From 998d4cb559c82cb58fc885a11487da5a31845b7a Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 5 Mar 2021 08:56:28 -0800 Subject: [PATCH 5/6] seed tests with u64 for platform consistency --- Cargo.toml | 11 +++++++---- benches/bigint.rs | 12 ++++-------- benches/gcd.rs | 12 ++++-------- benches/roots.rs | 12 ++++-------- ci/big_rand/Cargo.toml | 5 +---- ci/big_rand/src/torture.rs | 10 +++------- 6 files changed, 23 insertions(+), 39 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c7947220..c6aadb7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,10 @@ build = "build.rs" exclude = ["/bors.toml", "/ci/*", "/.github/*"] edition = "2018" +[features] +default = ["std"] +std = ["num-integer/std", "num-traits/std"] + [package.metadata.docs.rs] features = ["std", "serde", "rand", "quickcheck", "arbitrary"] @@ -66,9 +70,8 @@ optional = true version = "0.4" default-features = false -[features] -default = ["std"] -std = ["num-integer/std", "num-traits/std"] - [build-dependencies] autocfg = "1" + +[dev-dependencies] +rand_xorshift = "0.3" diff --git a/benches/bigint.rs b/benches/bigint.rs index c2f7bd4d..8163aa64 100644 --- a/benches/bigint.rs +++ b/benches/bigint.rs @@ -5,17 +5,13 @@ extern crate test; use num_bigint::{BigInt, BigUint, RandBigInt}; use num_traits::{FromPrimitive, Num, One, Zero}; -use rand::rngs::StdRng; -use rand::SeedableRng; +use rand::prelude::*; +use rand_xorshift::XorShiftRng; use std::mem::replace; use test::Bencher; -fn get_rng() -> StdRng { - let mut seed = [0; 32]; - for i in 1..32 { - seed[usize::from(i)] = i; - } - SeedableRng::from_seed(seed) +fn get_rng() -> impl Rng { + XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0) } fn multiply_bench(b: &mut Bencher, xbits: u64, ybits: u64) { diff --git a/benches/gcd.rs b/benches/gcd.rs index 1a65654c..61028a7d 100644 --- a/benches/gcd.rs +++ b/benches/gcd.rs @@ -6,16 +6,12 @@ extern crate test; use num_bigint::{BigUint, RandBigInt}; use num_integer::Integer; use num_traits::Zero; -use rand::rngs::StdRng; -use rand::SeedableRng; +use rand::prelude::*; +use rand_xorshift::XorShiftRng; use test::Bencher; -fn get_rng() -> StdRng { - let mut seed = [0; 32]; - for i in 1..32 { - seed[usize::from(i)] = i; - } - SeedableRng::from_seed(seed) +fn get_rng() -> impl Rng { + XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0) } fn bench(b: &mut Bencher, bits: u64, gcd: fn(&BigUint, &BigUint) -> BigUint) { diff --git a/benches/roots.rs b/benches/roots.rs index 2433cf0a..0f365ada 100644 --- a/benches/roots.rs +++ b/benches/roots.rs @@ -4,8 +4,8 @@ extern crate test; use num_bigint::{BigUint, RandBigInt}; -use rand::rngs::StdRng; -use rand::SeedableRng; +use rand::prelude::*; +use rand_xorshift::XorShiftRng; use test::Bencher; // The `big64` cases demonstrate the speed of cases where the value @@ -16,12 +16,8 @@ use test::Bencher; // // The `big2k` and `big4k` cases are too big for `f64`, and use a simpler guess. -fn get_rng() -> StdRng { - let mut seed = [0; 32]; - for i in 1..32 { - seed[usize::from(i)] = i; - } - SeedableRng::from_seed(seed) +fn get_rng() -> impl Rng { + XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0) } fn check(x: &BigUint, n: u32) { diff --git a/ci/big_rand/Cargo.toml b/ci/big_rand/Cargo.toml index 33dd20bc..d2bfe353 100644 --- a/ci/big_rand/Cargo.toml +++ b/ci/big_rand/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [dependencies] num-traits = "0.2.11" +rand = "0.8" rand_chacha = "0.3" rand_isaac = "0.3" rand_xorshift = "0.3" @@ -13,7 +14,3 @@ rand_xorshift = "0.3" [dependencies.num-bigint] features = ["rand"] path = "../.." - -[dependencies.rand] -features = ["small_rng"] -version = "0.8" diff --git a/ci/big_rand/src/torture.rs b/ci/big_rand/src/torture.rs index eac8c222..58400883 100644 --- a/ci/big_rand/src/torture.rs +++ b/ci/big_rand/src/torture.rs @@ -1,14 +1,10 @@ use num_bigint::RandBigInt; use num_traits::Zero; use rand::prelude::*; -use rand::rngs::SmallRng; +use rand_xorshift::XorShiftRng; -fn get_rng() -> SmallRng { - let seed = [ - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, - 26, 27, 28, 29, 30, 31, 32, - ]; - SmallRng::from_seed(seed) +fn get_rng() -> impl Rng { + XorShiftRng::seed_from_u64(0x1234_5678_9abc_def0) } fn test_mul_divide_torture_count(count: usize) { From 4247abe6014593bdda0a4a8035c60cffeb19d993 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 5 Mar 2021 09:46:19 -0800 Subject: [PATCH 6/6] implement xorshift* for benchmarking --- Cargo.toml | 3 --- benches/bigint.rs | 7 ++----- benches/gcd.rs | 7 ++----- benches/rng/mod.rs | 38 ++++++++++++++++++++++++++++++++++++++ benches/roots.rs | 9 +++------ ci/big_rand/src/torture.rs | 2 +- 6 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 benches/rng/mod.rs diff --git a/Cargo.toml b/Cargo.toml index c6aadb7a..6e54ee32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,6 +72,3 @@ default-features = false [build-dependencies] autocfg = "1" - -[dev-dependencies] -rand_xorshift = "0.3" diff --git a/benches/bigint.rs b/benches/bigint.rs index 8163aa64..b7f5fd21 100644 --- a/benches/bigint.rs +++ b/benches/bigint.rs @@ -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(); diff --git a/benches/gcd.rs b/benches/gcd.rs index 61028a7d..c211b6ef 100644 --- a/benches/gcd.rs +++ b/benches/gcd.rs @@ -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(); diff --git a/benches/rng/mod.rs b/benches/rng/mod.rs new file mode 100644 index 00000000..33e4f0fa --- /dev/null +++ b/benches/rng/mod.rs @@ -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)) + } +} diff --git a/benches/roots.rs b/benches/roots.rs index 0f365ada..7afc4f76 100644 --- a/benches/roots.rs +++ b/benches/roots.rs @@ -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. // @@ -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 { diff --git a/ci/big_rand/src/torture.rs b/ci/big_rand/src/torture.rs index 58400883..72059ef2 100644 --- a/ci/big_rand/src/torture.rs +++ b/ci/big_rand/src/torture.rs @@ -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) }