Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions futures-util/src/async_await/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ mod stream_select_mod;
#[cfg(feature = "async-await-macro")]
pub use self::stream_select_mod::*;

#[cfg(feature = "std")]
#[cfg(feature = "async-await-macro")]
mod random;
#[cfg(feature = "std")]
#[cfg(feature = "async-await-macro")]
pub use self::random::*;

Expand Down
40 changes: 32 additions & 8 deletions futures-util/src/async_await/random.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
use std::{
cell::Cell,
collections::hash_map::DefaultHasher,
hash::Hasher,
num::Wrapping,
sync::atomic::{AtomicUsize, Ordering},
};

// Based on [Fisher–Yates shuffle].
//
// [Fisher–Yates shuffle]: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
Expand All @@ -24,7 +16,16 @@ fn gen_index(n: usize) -> usize {
/// Pseudorandom number generator based on [xorshift*].
///
/// [xorshift*]: https://en.wikipedia.org/wiki/Xorshift#xorshift*
#[cfg(feature = "std")]
fn random() -> u64 {
use std::{
cell::Cell,
collections::hash_map::DefaultHasher,
hash::Hasher,
num::Wrapping,
sync::atomic::{AtomicUsize, Ordering},
};

std::thread_local! {
static RNG: Cell<Wrapping<u64>> = Cell::new(Wrapping(prng_seed()));
}
Expand Down Expand Up @@ -52,3 +53,26 @@ fn random() -> u64 {
x.0.wrapping_mul(0x2545_f491_4f6c_dd1d)
})
}

#[cfg(not(feature = "std"))]
fn random() -> u64 {
use core::sync::atomic::{AtomicUsize, Ordering};

static RNG: AtomicUsize = AtomicUsize::new(1);

let mut x = RNG.load(Ordering::Relaxed);

if core::mem::size_of::<usize>() == 4 {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
} else if core::mem::size_of::<usize>() == 8 {
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
x = x.wrapping_mul(0x2545_f491_4f6c_dd1d);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like repeated code, would be nice if we could refactor this somehow.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your review.
I prepared xorshift64star and xorshift32 functions.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ytakano thanks, I should have mentioned that I am just an outside contributor, hope we can get a review from @taiki-e


RNG.store(x, Ordering::Relaxed);
x as u64
}
2 changes: 0 additions & 2 deletions futures-util/src/async_await/select_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,13 @@ macro_rules! document_select_macro {
};
}

#[cfg(feature = "std")]
#[doc(hidden)]
pub use futures_macro::select_internal;

#[doc(hidden)]
pub use futures_macro::select_biased_internal;

document_select_macro! {
#[cfg(feature = "std")]
#[macro_export]
macro_rules! select {
($($tokens:tt)*) => {{
Expand Down
1 change: 0 additions & 1 deletion futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ pub use futures_util::{AsyncBufReadExt, AsyncReadExt, AsyncSeekExt, AsyncWriteEx
// Macro reexports
pub use futures_core::ready; // Readiness propagation
pub use futures_util::pin_mut;
#[cfg(feature = "std")]
#[cfg(feature = "async-await")]
pub use futures_util::select;
#[cfg(feature = "async-await")]
Expand Down
Loading