mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-03 09:49:31 +02:00
Change hand-picked RNGs back to SmallRng
While `Xoshiro256+` is faster on 64-bit, it has low linear complexity in the lower three bits, which *are* used when generating dither. Also, while `Xoshiro128StarStar` access one less variable from the heap, multiplication is generally slower than addition in hardware.
This commit is contained in:
parent
4c89a721ee
commit
ff3648434b
3 changed files with 7 additions and 34 deletions
|
@ -1,3 +1,4 @@
|
|||
use rand::rngs::SmallRng;
|
||||
use rand::SeedableRng;
|
||||
use rand_distr::{Distribution, Normal, Triangular, Uniform};
|
||||
use std::fmt;
|
||||
|
@ -41,29 +42,12 @@ impl fmt::Display for dyn Ditherer {
|
|||
}
|
||||
}
|
||||
|
||||
// `SmallRng` is 33% faster than `ThreadRng`, but we can do even better.
|
||||
// `SmallRng` defaults to `Xoshiro256PlusPlus` on 64-bit platforms and
|
||||
// `Xoshiro128PlusPlus` on 32-bit platforms. These are excellent for the
|
||||
// general case. In our case of just 64-bit floating points, we can make
|
||||
// some optimizations. Compared to `SmallRng`, these hand-picked generators
|
||||
// improve performance by another 9% on 64-bit platforms and 2% on 32-bit
|
||||
// platforms.
|
||||
//
|
||||
// For reference, see https://prng.di.unimi.it. Note that we do not use
|
||||
// `Xoroshiro128Plus` or `Xoshiro128Plus` because they display low linear
|
||||
// complexity in the lower four bits, which is not what we want:
|
||||
// linearization is the very point of dithering.
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
type Rng = rand_xoshiro::Xoshiro256Plus;
|
||||
#[cfg(not(target_pointer_width = "64"))]
|
||||
type Rng = rand_xoshiro::Xoshiro128StarStar;
|
||||
|
||||
fn create_rng() -> Rng {
|
||||
Rng::from_entropy()
|
||||
fn create_rng() -> SmallRng {
|
||||
SmallRng::from_entropy()
|
||||
}
|
||||
|
||||
pub struct TriangularDitherer {
|
||||
cached_rng: Rng,
|
||||
cached_rng: SmallRng,
|
||||
distribution: Triangular<f64>,
|
||||
}
|
||||
|
||||
|
@ -90,7 +74,7 @@ impl TriangularDitherer {
|
|||
}
|
||||
|
||||
pub struct GaussianDitherer {
|
||||
cached_rng: Rng,
|
||||
cached_rng: SmallRng,
|
||||
distribution: Normal<f64>,
|
||||
}
|
||||
|
||||
|
@ -119,7 +103,7 @@ impl GaussianDitherer {
|
|||
pub struct HighPassDitherer {
|
||||
active_channel: usize,
|
||||
previous_noises: [f64; NUM_CHANNELS],
|
||||
cached_rng: Rng,
|
||||
cached_rng: SmallRng,
|
||||
distribution: Uniform<f64>,
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue