1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#[cfg(all(target_feature = "rdrnd", target_arch = "x86"))]
#[inline(always)]
pub fn generate_hyper_thread_safe_random_usize() -> usize
{
extern "platform-intrinsic"
{
#[inline(always)]
fn x86_rdrand32_step() -> (u32, i32);
}
#[target_feature(enable = "rdrnd")]
unsafe fn generate_hyper_thread_safe_random_usize_target_feature() -> usize
{
loop
{
let (random_value, success) = x86_rdrand32_step();
if success != 0
{
return random_value as usize
}
}
}
unsafe { generate_hyper_thread_safe_random_usize_target_feature() }
}
#[cfg(all(target_feature = "rdrnd", target_arch = "x86_64"))]
#[inline(always)]
pub fn generate_hyper_thread_safe_random_usize() -> usize
{
extern "platform-intrinsic"
{
#[inline(always)]
fn x86_rdrand64_step() -> (u64, i32);
}
#[target_feature(enable = "rdrnd")]
unsafe fn generate_hyper_thread_safe_random_usize_target_feature() -> usize
{
loop
{
let (random_value, success) = x86_rdrand64_step();
if success != 0
{
return random_value as usize
}
}
}
unsafe { generate_hyper_thread_safe_random_usize_target_feature() }
}
#[cfg(all(target_pointer_width = "32", not(all(target_feature = "rdrnd", target_arch = "x86"))))]
pub fn generate_hyper_thread_safe_random_usize() -> usize
{
use ::rand::Rng;
use ::rand::thread_rng;
thread_rng().next_u32() as usize
}
#[cfg(all(target_pointer_width = "64", not(all(target_feature = "rdrnd", target_arch = "x86_64"))))]
pub fn generate_hyper_thread_safe_random_usize() -> usize
{
use ::rand::Rng;
use ::rand::thread_rng;
thread_rng().next_u64() as usize
}