Skip to content

Commit 6d127b4

Browse files
authored
Merge pull request #868 from roderickvd/smallrng-dither
Change hand-picked RNGs back to `SmallRng`
2 parents 4c89a72 + ff36484 commit 6d127b4

3 files changed

Lines changed: 7 additions & 34 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

playback/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ lewton = "0.10"
4747
ogg = "0.8"
4848

4949
# Dithering
50-
rand = "0.8"
50+
rand = { version = "0.8", features = ["small_rng"] }
5151
rand_distr = "0.4"
52-
rand_xoshiro = "0.6"
5352

5453
[features]
5554
alsa-backend = ["alsa"]

playback/src/dither.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rand::rngs::SmallRng;
12
use rand::SeedableRng;
23
use rand_distr::{Distribution, Normal, Triangular, Uniform};
34
use std::fmt;
@@ -41,29 +42,12 @@ impl fmt::Display for dyn Ditherer {
4142
}
4243
}
4344

44-
// `SmallRng` is 33% faster than `ThreadRng`, but we can do even better.
45-
// `SmallRng` defaults to `Xoshiro256PlusPlus` on 64-bit platforms and
46-
// `Xoshiro128PlusPlus` on 32-bit platforms. These are excellent for the
47-
// general case. In our case of just 64-bit floating points, we can make
48-
// some optimizations. Compared to `SmallRng`, these hand-picked generators
49-
// improve performance by another 9% on 64-bit platforms and 2% on 32-bit
50-
// platforms.
51-
//
52-
// For reference, see https://prng.di.unimi.it. Note that we do not use
53-
// `Xoroshiro128Plus` or `Xoshiro128Plus` because they display low linear
54-
// complexity in the lower four bits, which is not what we want:
55-
// linearization is the very point of dithering.
56-
#[cfg(target_pointer_width = "64")]
57-
type Rng = rand_xoshiro::Xoshiro256Plus;
58-
#[cfg(not(target_pointer_width = "64"))]
59-
type Rng = rand_xoshiro::Xoshiro128StarStar;
60-
61-
fn create_rng() -> Rng {
62-
Rng::from_entropy()
45+
fn create_rng() -> SmallRng {
46+
SmallRng::from_entropy()
6347
}
6448

6549
pub struct TriangularDitherer {
66-
cached_rng: Rng,
50+
cached_rng: SmallRng,
6751
distribution: Triangular<f64>,
6852
}
6953

@@ -90,7 +74,7 @@ impl TriangularDitherer {
9074
}
9175

9276
pub struct GaussianDitherer {
93-
cached_rng: Rng,
77+
cached_rng: SmallRng,
9478
distribution: Normal<f64>,
9579
}
9680

@@ -119,7 +103,7 @@ impl GaussianDitherer {
119103
pub struct HighPassDitherer {
120104
active_channel: usize,
121105
previous_noises: [f64; NUM_CHANNELS],
122-
cached_rng: Rng,
106+
cached_rng: SmallRng,
123107
distribution: Uniform<f64>,
124108
}
125109

0 commit comments

Comments
 (0)