Skip to content

Commit c4d0535

Browse files
committed
fix: use AtomicUsize for random()
Signed-off-by: Yuuki Takano <ytakanoster@gmail.com>
1 parent 1e0e7d8 commit c4d0535

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

futures-util/src/async_await/random.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,22 @@ fn random() -> u64 {
5656

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

61-
static RNG: AtomicU64 = AtomicU64::new(1);
61+
static RNG: AtomicUsize = AtomicUsize::new(1);
6262

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

65-
x ^= x >> 12;
66-
x ^= x << 25;
67-
x ^= x >> 27;
65+
if core::mem::size_of::<usize>() == 4 {
66+
x ^= x << 13;
67+
x ^= x >> 17;
68+
x ^= x << 5;
69+
} else if core::mem::size_of::<usize>() == 8 {
70+
x ^= x >> 12;
71+
x ^= x << 25;
72+
x ^= x >> 27;
73+
}
74+
6875
let result = x.wrapping_mul(0x2545_f491_4f6c_dd1d) as u64;
6976
RNG.store(result, Ordering::Relaxed);
7077

0 commit comments

Comments
 (0)