Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: select! macro for no-std environments #2903

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
70 changes: 56 additions & 14 deletions futures-util/src/async_await/random.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
use std::{
cell::Cell,
collections::hash_map::DefaultHasher,
hash::Hasher,
num::Wrapping,
sync::atomic::{AtomicUsize, Ordering},
};
use core::num::Wrapping;

// Based on [Fisher–Yates shuffle].
//
Expand All @@ -24,7 +18,15 @@ 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,
sync::atomic::{AtomicUsize, Ordering},
};

std::thread_local! {
static RNG: Cell<Wrapping<u64>> = Cell::new(Wrapping(prng_seed()));
}
Expand All @@ -43,12 +45,52 @@ fn random() -> u64 {
}

RNG.with(|rng| {
let mut x = rng.get();
debug_assert_ne!(x.0, 0);
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
rng.set(x);
x.0.wrapping_mul(0x2545_f491_4f6c_dd1d)
let x = rng.get();
let (next, result) = xorshift64star(x);
rng.set(next);
result
})
}

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

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

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

if core::mem::size_of::<usize>() == 4 {
let next = xorshift32(x as u32);
RNG.store(next as usize, Ordering::Relaxed);
next as u64
} else if core::mem::size_of::<usize>() == 8 {
let (next, result) = xorshift64star(Wrapping(x as u64));
RNG.store(next.0 as usize, Ordering::Relaxed);
result
} else {
panic!("random() function is not supported on this platform");
}
}

/// Xorshift64* algorithm.
/// Returns the next state and the random number; `(next_state, random_number)`.
#[inline]
fn xorshift64star(mut x: Wrapping<u64>) -> (Wrapping<u64>, u64) {
debug_assert_ne!(x.0, 0);
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
(x, x.0.wrapping_mul(0x2545_f491_4f6c_dd1d))
}

/// Xorshift32 algorithm.
#[cfg(not(feature = "std"))]
#[inline]
fn xorshift32(mut x: u32) -> u32 {
debug_assert_ne!(x, 0);
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
x
}
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