-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand64-hw.c
50 lines (42 loc) · 1.01 KB
/
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
48
49
50
#include "rand64-hw.h"
/* Hardware implementation. */
/* 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;
/* Work around GCC bug 107565
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107565>. */
x = 0;
while (! _rdrand64_step (&x))
continue;
return x;
}
/* Finalize the hardware rand64 implementation. */
void
hardware_rand64_fini (void)
{
}