#[derive(Copy, Clone, Debug)]
pub struct RdRand(());
impl RdRand {
#[inline]
pub fn new() -> Option<Self> {
let cpuid = unsafe { core::arch::x86_64::__cpuid(0x1) };
if cpuid.ecx & (1 << 30) != 0 {
Some(RdRand(()))
} else {
None
}
}
#[inline]
pub fn get_u64(self) -> Option<u64> {
let mut res: u64 = 0;
unsafe {
match core::arch::x86_64::_rdrand64_step(&mut res) {
1 => Some(res),
x => {
debug_assert_eq!(x, 0, "rdrand64 returned non-binary value");
None
}
}
}
}
#[inline]
pub fn get_u32(self) -> Option<u32> {
let mut res: u32 = 0;
unsafe {
match core::arch::x86_64::_rdrand32_step(&mut res) {
1 => Some(res),
x => {
debug_assert_eq!(x, 0, "rdrand32 returned non-binary value");
None
}
}
}
}
#[inline]
pub fn get_u16(self) -> Option<u16> {
let mut res: u16 = 0;
unsafe {
match core::arch::x86_64::_rdrand16_step(&mut res) {
1 => Some(res),
x => {
debug_assert_eq!(x, 0, "rdrand16 returned non-binary value");
None
}
}
}
}
}
#[cfg(all(test))]
mod tests {
use super::*;
#[test]
pub fn test_rdrand() {
let rand = RdRand::new();
if is_x86_feature_detected!("rdrand") {
let rand = rand.unwrap();
assert!(rand.get_u16().is_some());
assert!(rand.get_u32().is_some());
assert!(rand.get_u64().is_some());
} else {
assert!(rand.is_none());
}
}
}