5 #include "tests/malloc.h"
7 typedef unsigned char UChar
;
8 typedef unsigned int UInt
;
9 typedef unsigned long int UWord
;
10 typedef unsigned long long int ULong
;
12 // What can we actually test here? The instructions take no input and
13 // produce output which is by definition totally random. So apart from
14 // not simply failing insn decode, there's nothing much to test.
16 // Get 10 values of each size, and check that they are not all the same
17 // (otherwise something's obviously wrong). Now, statistically, it's
18 // highly unlikely that they are all the same. For 10 16 bit ints, the
19 // probability of them being all the same is (I'd guess) (2^-16) ^ (10-1),
22 ULong
do_rdrand64 ( void )
28 "movabsq $0x5555555555555555, %%r11 ; "
34 : "=r"(res
), "=r"(cflag
) : : "r11", "r12"
42 ULong
do_rdrand32 ( void )
48 "movabsq $0x5555555555555555, %%r11 ; "
54 : "=r"(res
), "=r"(cflag
) : : "r11", "r12"
62 ULong
do_rdrand16 ( void )
68 "movabsq $0x5555555555555555, %%r11 ; "
74 : "=r"(res
), "=r"(cflag
) : : "r11", "r12"
82 void do_test ( ULong(*fn
)(void),
84 /* with 1s indicating the random bits in the result */ )
87 for (UInt i
= 0; i
< 10; i
++) {
91 // They really should all be different (to an extremely high probabilty.
93 int allSame
= 1/*true*/; // really, a Bool
94 for (UInt i
= 1; i
< 10; i
++) {
95 if (arr
[i
] != arr
[0]) {
102 // The 0/32/48 leading bits of the result should have a particular value,
103 // depending on the insn. So print them, with the random part masked out.
104 for (UInt i
= 0; i
< 10; i
++) {
105 printf("0x%016llx\n", arr
[i
] & ~mask
);
112 do_test( do_rdrand64
, 0xFFFFFFFFFFFFFFFFULL
);
113 do_test( do_rdrand32
, 0x00000000FFFFFFFFULL
);
114 do_test( do_rdrand16
, 0x000000000000FFFFULL
);