-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand64-hw.c
47 lines (41 loc) · 1001 Bytes
/
rand64-hw.c
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
#include <cpuid.h>
#include <immintrin.h>
/* Description of the current CPU. */
struct cpuid { unsigned eax, ebx, ecx, edx; };
/* Return information about the CPU. See <http://wiki.osdev.org/CPUID>. */
struct cpuid
cpuid (unsigned int leaf, unsigned int subleaf)
{
struct cpuid result;
asm ("cpuid"
: "=a" (result.eax), "=b" (result.ebx),
"=c" (result.ecx), "=d" (result.edx)
: "a" (leaf), "c" (subleaf));
return result;
}
/* Return true if the CPU supports the RDRAND instruction. */
_Bool
rdrand_supported (void)
{
struct cpuid extended = cpuid (1, 0);
return (extended.ecx & bit_RDRND) != 0;
}
/* Initialize the hardware rand64 implementation. */
void
hardware_rand64_init (void)
{
}
/* Return a random value, using hardware operations. */
unsigned long long
hardware_rand64 (void)
{
unsigned long long int x;
while (! _rdrand64_step (&x))
continue;
return x;
}
/* Finalize the hardware rand64 implementation. */
void
hardware_rand64_fini (void)
{
}