Skip to content

Commit

Permalink
Adjust inlining
Browse files Browse the repository at this point in the history
See #817
  • Loading branch information
dhardy committed Jun 6, 2019
1 parent e1b61a0 commit 7c8284f
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions rand_core/CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.5.0] - 2019-06-06
- Enable testing with Miri and fix incorrect pointer usages (#779, #780, #781, #783, #784)
- Rewrite `Error` type and adjust API (#800)
- Adjust usage of `#[inline]` for `BlockRng` and `BlockRng64`

## [0.4.0] - 2019-01-24
- Disable the `std` feature by default (#702)
Expand Down
29 changes: 23 additions & 6 deletions rand_core/src/block.rs
Expand Up @@ -131,6 +131,7 @@ impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng<R> {
impl<R: BlockRngCore> BlockRng<R> {
/// Create a new `BlockRng` from an existing RNG implementing
/// `BlockRngCore`. Results will be generated on first use.
#[inline]
pub fn new(core: R) -> BlockRng<R>{
let results_empty = R::Results::default();
BlockRng {
Expand All @@ -145,18 +146,21 @@ impl<R: BlockRngCore> BlockRng<R> {
/// If this is equal to or larger than the size of the result buffer then
/// the buffer is "empty" and `generate()` must be called to produce new
/// results.
#[inline(always)]
pub fn index(&self) -> usize {
self.index
}

/// Reset the number of available results.
/// This will force a new set of results to be generated on next use.
#[inline]
pub fn reset(&mut self) {
self.index = self.results.as_ref().len();
}

/// Generate a new set of results immediately, setting the index to the
/// given value.
#[inline]
pub fn generate_and_set(&mut self, index: usize) {
assert!(index < self.results.as_ref().len());
self.core.generate(&mut self.results);
Expand All @@ -167,7 +171,7 @@ impl<R: BlockRngCore> BlockRng<R> {
impl<R: BlockRngCore<Item=u32>> RngCore for BlockRng<R>
where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
{
#[inline(always)]
#[inline]
fn next_u32(&mut self) -> u32 {
if self.index >= self.results.as_ref().len() {
self.generate_and_set(0);
Expand All @@ -178,7 +182,7 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
value
}

#[inline(always)]
#[inline]
fn next_u64(&mut self) -> u64 {
let read_u64 = |results: &[u32], index| {
if cfg!(any(target_endian = "little")) {
Expand Down Expand Up @@ -210,6 +214,7 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
}
}

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut read_len = 0;
while read_len < dest.len() {
Expand All @@ -225,23 +230,26 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
}
}

#[inline(always)]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
Ok(self.fill_bytes(dest))
}
}

impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
type Seed = R::Seed;

#[inline(always)]
fn from_seed(seed: Self::Seed) -> Self {
Self::new(R::from_seed(seed))
}

#[inline(always)]
fn seed_from_u64(seed: u64) -> Self {
Self::new(R::seed_from_u64(seed))
}

#[inline(always)]
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
Ok(Self::new(R::from_rng(rng)?))
}
Expand Down Expand Up @@ -296,6 +304,7 @@ impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng64<R> {
impl<R: BlockRngCore> BlockRng64<R> {
/// Create a new `BlockRng` from an existing RNG implementing
/// `BlockRngCore`. Results will be generated on first use.
#[inline]
pub fn new(core: R) -> BlockRng64<R>{
let results_empty = R::Results::default();
BlockRng64 {
Expand All @@ -311,19 +320,22 @@ impl<R: BlockRngCore> BlockRng64<R> {
/// If this is equal to or larger than the size of the result buffer then
/// the buffer is "empty" and `generate()` must be called to produce new
/// results.
#[inline(always)]
pub fn index(&self) -> usize {
self.index
}

/// Reset the number of available results.
/// This will force a new set of results to be generated on next use.
#[inline]
pub fn reset(&mut self) {
self.index = self.results.as_ref().len();
self.half_used = false;
}

/// Generate a new set of results immediately, setting the index to the
/// given value.
#[inline]
pub fn generate_and_set(&mut self, index: usize) {
assert!(index < self.results.as_ref().len());
self.core.generate(&mut self.results);
Expand All @@ -335,7 +347,7 @@ impl<R: BlockRngCore> BlockRng64<R> {
impl<R: BlockRngCore<Item=u64>> RngCore for BlockRng64<R>
where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
{
#[inline(always)]
#[inline]
fn next_u32(&mut self) -> u32 {
let mut index = self.index * 2 - self.half_used as usize;
if index >= self.results.as_ref().len() * 2 {
Expand All @@ -361,7 +373,7 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
}
}

#[inline(always)]
#[inline]
fn next_u64(&mut self) -> u64 {
if self.index >= self.results.as_ref().len() {
self.core.generate(&mut self.results);
Expand All @@ -374,6 +386,7 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
value
}

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut read_len = 0;
self.half_used = false;
Expand All @@ -392,6 +405,7 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
}
}

#[inline(always)]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}
Expand All @@ -400,14 +414,17 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {
type Seed = R::Seed;

#[inline(always)]
fn from_seed(seed: Self::Seed) -> Self {
Self::new(R::from_seed(seed))
}

#[inline(always)]
fn seed_from_u64(seed: u64) -> Self {
Self::new(R::seed_from_u64(seed))
}

#[inline(always)]
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
Ok(Self::new(R::from_rng(rng)?))
}
Expand Down
1 change: 1 addition & 0 deletions rand_hc/CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.1.1] - 2019-06-06
- Bump `rand_core` version
- Adjust usage of `#[inline]`

## [0.1.0] - 2018-10-17
- Pulled out of the Rand crate
9 changes: 7 additions & 2 deletions rand_hc/src/hc128.rs
Expand Up @@ -67,20 +67,22 @@ const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv
pub struct Hc128Rng(BlockRng<Hc128Core>);

impl RngCore for Hc128Rng {
#[inline(always)]
#[inline]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}

#[inline(always)]
#[inline]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest)
}

#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}
Expand All @@ -89,10 +91,12 @@ impl RngCore for Hc128Rng {
impl SeedableRng for Hc128Rng {
type Seed = <Hc128Core as SeedableRng>::Seed;

#[inline]
fn from_seed(seed: Self::Seed) -> Self {
Hc128Rng(BlockRng::<Hc128Core>::from_seed(seed))
}

#[inline]
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
BlockRng::<Hc128Core>::from_rng(rng).map(Hc128Rng)
}
Expand Down Expand Up @@ -268,6 +272,7 @@ impl Hc128Core {
// Initialize an HC-128 random number generator. The seed has to be
// 256 bits in length (`[u32; 8]`), matching the 128 bit `key` followed by
// 128 bit `iv` when HC-128 where to be used as a stream cipher.
#[inline(always)] // single use: SeedableRng::from_seed
fn init(seed: [u32; SEED_WORDS]) -> Self {
#[inline]
fn f1(x: u32) -> u32 {
Expand Down
1 change: 1 addition & 0 deletions rand_isaac/CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.1.2] - 2019-06-06
- Bump `rand_core` version
- Remove deprecated code
- Adjust usage of `#[inline]`

## [0.1.1] - 2018-11-26
- Fix `rand_core` version requirement
Expand Down
9 changes: 7 additions & 2 deletions rand_isaac/src/isaac.rs
Expand Up @@ -92,20 +92,22 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
pub struct IsaacRng(BlockRng<IsaacCore>);

impl RngCore for IsaacRng {
#[inline(always)]
#[inline]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}

#[inline(always)]
#[inline]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest)
}

#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}
Expand All @@ -114,17 +116,20 @@ impl RngCore for IsaacRng {
impl SeedableRng for IsaacRng {
type Seed = <IsaacCore as SeedableRng>::Seed;

#[inline]
fn from_seed(seed: Self::Seed) -> Self {
IsaacRng(BlockRng::<IsaacCore>::from_seed(seed))
}

/// Create an ISAAC random number generator using an `u64` as seed.
/// If `seed == 0` this will produce the same stream of random numbers as
/// the reference implementation when used unseeded.
#[inline]
fn seed_from_u64(seed: u64) -> Self {
IsaacRng(BlockRng::<IsaacCore>::seed_from_u64(seed))
}

#[inline]
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
BlockRng::<IsaacCore>::from_rng(rng).map(|rng| IsaacRng(rng))
}
Expand Down
9 changes: 7 additions & 2 deletions rand_isaac/src/isaac64.rs
Expand Up @@ -83,20 +83,22 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
pub struct Isaac64Rng(BlockRng64<Isaac64Core>);

impl RngCore for Isaac64Rng {
#[inline(always)]
#[inline]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}

#[inline(always)]
#[inline]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest)
}

#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}
Expand All @@ -105,17 +107,20 @@ impl RngCore for Isaac64Rng {
impl SeedableRng for Isaac64Rng {
type Seed = <Isaac64Core as SeedableRng>::Seed;

#[inline]
fn from_seed(seed: Self::Seed) -> Self {
Isaac64Rng(BlockRng64::<Isaac64Core>::from_seed(seed))
}

/// Create an ISAAC random number generator using an `u64` as seed.
/// If `seed == 0` this will produce the same stream of random numbers as
/// the reference implementation when used unseeded.
#[inline]
fn seed_from_u64(seed: u64) -> Self {
Isaac64Rng(BlockRng64::<Isaac64Core>::seed_from_u64(seed))
}

#[inline]
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
BlockRng64::<Isaac64Core>::from_rng(rng).map(|rng| Isaac64Rng(rng))
}
Expand Down
4 changes: 4 additions & 0 deletions src/rngs/small.rs
Expand Up @@ -89,10 +89,12 @@ impl RngCore for SmallRng {
self.0.next_u64()
}

#[inline(always)]
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest);
}

#[inline(always)]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}
Expand All @@ -101,10 +103,12 @@ impl RngCore for SmallRng {
impl SeedableRng for SmallRng {
type Seed = <Rng as SeedableRng>::Seed;

#[inline(always)]
fn from_seed(seed: Self::Seed) -> Self {
SmallRng(Rng::from_seed(seed))
}

#[inline(always)]
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
Rng::from_rng(rng).map(SmallRng)
}
Expand Down
4 changes: 4 additions & 0 deletions src/rngs/std.rs
Expand Up @@ -44,10 +44,12 @@ impl RngCore for StdRng {
self.0.next_u64()
}

#[inline(always)]
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest);
}

#[inline(always)]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}
Expand All @@ -56,10 +58,12 @@ impl RngCore for StdRng {
impl SeedableRng for StdRng {
type Seed = <Rng as SeedableRng>::Seed;

#[inline(always)]
fn from_seed(seed: Self::Seed) -> Self {
StdRng(Rng::from_seed(seed))
}

#[inline(always)]
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
Rng::from_rng(rng).map(StdRng)
}
Expand Down

0 comments on commit 7c8284f

Please sign in to comment.