Skip to content

Commit

Permalink
core: add some inlining hints to methods/fns in rand.
Browse files Browse the repository at this point in the history
  • Loading branch information
huonw committed Apr 29, 2013
1 parent 30266a7 commit d4b934b
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/libcore/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub trait Rand {
}

impl Rand for int {
#[inline]
fn rand<R: Rng>(rng: &R) -> int {
if int::bits == 32 {
rng.next() as int
Expand All @@ -63,30 +64,35 @@ impl Rand for int {
}

impl Rand for i8 {
#[inline]
fn rand<R: Rng>(rng: &R) -> i8 {
rng.next() as i8
}
}

impl Rand for i16 {
#[inline]
fn rand<R: Rng>(rng: &R) -> i16 {
rng.next() as i16
}
}

impl Rand for i32 {
#[inline]
fn rand<R: Rng>(rng: &R) -> i32 {
rng.next() as i32
}
}

impl Rand for i64 {
#[inline]
fn rand<R: Rng>(rng: &R) -> i64 {
(rng.next() as i64 << 32) | rng.next() as i64
}
}

impl Rand for uint {
#[inline]
fn rand<R: Rng>(rng: &R) -> uint {
if uint::bits == 32 {
rng.next() as uint
Expand All @@ -97,43 +103,50 @@ impl Rand for uint {
}

impl Rand for u8 {
#[inline]
fn rand<R: Rng>(rng: &R) -> u8 {
rng.next() as u8
}
}

impl Rand for u16 {
#[inline]
fn rand<R: Rng>(rng: &R) -> u16 {
rng.next() as u16
}
}

impl Rand for u32 {
#[inline]
fn rand<R: Rng>(rng: &R) -> u32 {
rng.next()
}
}

impl Rand for u64 {
#[inline]
fn rand<R: Rng>(rng: &R) -> u64 {
(rng.next() as u64 << 32) | rng.next() as u64
}
}

impl Rand for float {
#[inline]
fn rand<R: Rng>(rng: &R) -> float {
rng.gen::<f64>() as float
}
}

impl Rand for f32 {
#[inline]
fn rand<R: Rng>(rng: &R) -> f32 {
rng.gen::<f64>() as f32
}
}

static scale : f64 = (u32::max_value as f64) + 1.0f64;
impl Rand for f64 {
#[inline]
fn rand<R: Rng>(rng: &R) -> f64 {
let u1 = rng.next() as f64;
let u2 = rng.next() as f64;
Expand All @@ -144,12 +157,14 @@ impl Rand for f64 {
}

impl Rand for char {
#[inline]
fn rand<R: Rng>(rng: &R) -> char {
rng.next() as char
}
}

impl Rand for bool {
#[inline]
fn rand<R: Rng>(rng: &R) -> bool {
rng.next() & 1u32 == 1u32
}
Expand All @@ -163,6 +178,7 @@ macro_rules! tuple_impl {
$( $tyvar : Rand ),*
> Rand for ( $( $tyvar ),* , ) {

#[inline]
fn rand<R: Rng>(_rng: &R) -> ( $( $tyvar ),* , ) {
(
// use the $tyvar's to get the appropriate number of
Expand All @@ -177,7 +193,10 @@ macro_rules! tuple_impl {
}
}

impl Rand for () { fn rand<R: Rng>(_: &R) -> () { () } }
impl Rand for () {
#[inline]
fn rand<R: Rng>(_: &R) -> () { () }
}
tuple_impl!{A}
tuple_impl!{A, B}
tuple_impl!{A, B, C}
Expand All @@ -190,6 +209,7 @@ tuple_impl!{A, B, C, D, E, F, G, H, I}
tuple_impl!{A, B, C, D, E, F, G, H, I, J}

impl<T:Rand> Rand for Option<T> {
#[inline]
fn rand<R: Rng>(rng: &R) -> Option<T> {
if rng.gen() {
Some(rng.gen())
Expand All @@ -200,10 +220,12 @@ impl<T:Rand> Rand for Option<T> {
}

impl<T: Rand> Rand for ~T {
#[inline]
fn rand<R: Rng>(rng: &R) -> ~T { ~rng.gen() }
}

impl<T: Rand> Rand for @T {
#[inline]
fn rand<R: Rng>(rng: &R) -> @T { @rng.gen() }
}

Expand Down Expand Up @@ -413,6 +435,7 @@ pub trait RngUtil {
/// Extension methods for random number generators
impl<R: Rng> RngUtil for R {
/// Return a random value for a Rand type
#[inline(always)]
fn gen<T: Rand>(&self) -> T {
Rand::rand(self)
}
Expand Down Expand Up @@ -675,6 +698,7 @@ pub impl IsaacRng {
}

/// Refills the output buffer (`self.rsl`)
#[inline]
priv fn isaac(&self) {
self.c += 1;
// abbreviations
Expand Down Expand Up @@ -731,6 +755,9 @@ impl Rng for IsaacRng {
}
}

/// An [Xorshift random number
/// generator](http://en.wikipedia.org/wiki/Xorshift). Not suitable for
/// cryptographic purposes.
pub struct XorShiftRng {
priv mut x: u32,
priv mut y: u32,
Expand All @@ -739,6 +766,7 @@ pub struct XorShiftRng {
}

impl Rng for XorShiftRng {
#[inline]
pub fn next(&self) -> u32 {
let x = self.x;
let t = x ^ (x << 11);
Expand Down Expand Up @@ -788,6 +816,7 @@ fn tls_rng_state(_v: @IsaacRng) {}
* seeded by the system. Intended to be used in method chaining style, ie
* `task_rng().gen::<int>()`.
*/
#[inline]
pub fn task_rng() -> @IsaacRng {
let r : Option<@IsaacRng>;
unsafe {
Expand All @@ -807,13 +836,15 @@ pub fn task_rng() -> @IsaacRng {

// Allow direct chaining with `task_rng`
impl<R: Rng> Rng for @R {
#[inline(always)]
fn next(&self) -> u32 { (**self).next() }
}

/**
* Returns a random value of a Rand type, using the task's random number
* generator.
*/
#[inline]
pub fn random<T: Rand>() -> T {
task_rng().gen()
}
Expand Down

5 comments on commit d4b934b

@bors
Copy link
Contributor

@bors bors commented on d4b934b Apr 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at huonw@d4b934b

@bors
Copy link
Contributor

@bors bors commented on d4b934b Apr 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging huonw/rust/core-rust-isaac = d4b934b into auto

@bors
Copy link
Contributor

@bors bors commented on d4b934b Apr 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huonw/rust/core-rust-isaac = d4b934b merged ok, testing candidate = 868b7c1

@bors
Copy link
Contributor

@bors bors commented on d4b934b Apr 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on d4b934b Apr 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = 868b7c1

Please sign in to comment.