Cache RDSEED and RDRAND presence

This commit is contained in:
apio 2022-09-18 15:22:43 +02:00
parent 183c3236d4
commit 48fae4a971

View File

@ -10,6 +10,8 @@
static uint64_t state = 0xf5026f5ae96319e9; static uint64_t state = 0xf5026f5ae96319e9;
extern "C" int _test_rdseed(); extern "C" int _test_rdseed();
static bool has_rdrand = false;
static bool has_rdseed = false;
static uint64_t rdtsc() static uint64_t rdtsc()
{ {
@ -37,6 +39,9 @@ void Mersenne::init()
{ {
kdbgln("Preparing random number generator"); kdbgln("Preparing random number generator");
has_rdrand = CPU::has_feature(CPU::Features::RDRAND);
has_rdseed = _test_rdseed();
state ^= (0x45fe1024UL + MOON_MAJOR) * (MOON_MINOR ^ 200UL); state ^= (0x45fe1024UL + MOON_MAJOR) * (MOON_MINOR ^ 200UL);
state ^= 0xe0e4f5332ea75b; state ^= 0xe0e4f5332ea75b;
@ -54,9 +59,9 @@ void Mersenne::reseed()
{ {
state ^= rdtsc(); state ^= rdtsc();
if (CPU::has_feature(CPU::Features::RDRAND)) { state ^= rdrand(); } if (has_rdrand) { state ^= rdrand(); }
if (_test_rdseed()) { state ^= rdseed(); } if (has_rdseed) { state ^= rdseed(); }
state ^= rdtsc(); state ^= rdtsc();