Refactor RNG code

This commit is contained in:
EnderIce2 2024-04-26 18:33:43 +03:00
parent 5a94744fd2
commit 6cedac03f5
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD

View File

@ -20,31 +20,47 @@
namespace Random namespace Random
{ {
static uint64_t Seed = 0xdeadbeef; uint64_t Seed = 0xdeadbeef;
bool RDRANDFlag, RDSEEDFlag;
__constructor void InitRandomSeed()
{
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) != 0)
{
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{
CPU::x86::AMD::CPUID0x00000007_ECX_0 cpuid;
RDRANDFlag = cpuid.EBX.RDSEED;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
CPU::x86::Intel::CPUID0x00000007_0 cpuid;
RDRANDFlag = cpuid.EBX.RDSEED;
}
}
else
RDRANDFlag = false;
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) != 0)
{
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{
CPU::x86::AMD::CPUID0x00000001 cpuid;
RDRANDFlag = cpuid.ECX.RDRAND;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
CPU::x86::Intel::CPUID0x00000001 cpuid;
RDRANDFlag = cpuid.ECX.RDRAND;
}
}
else
RDRANDFlag = 0;
}
uint16_t rand16() uint16_t rand16()
{ {
#if defined(a86) #if defined(a86)
static int RDRANDFlag = 0x1A1A;
if (unlikely(RDRANDFlag == 0x1A1A))
{
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) != 0)
{
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{
CPU::x86::AMD::CPUID0x00000001 cpuid;
RDRANDFlag = cpuid.ECX.RDRAND;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
CPU::x86::Intel::CPUID0x00000001 cpuid;
RDRANDFlag = cpuid.ECX.RDRAND;
}
}
else
RDRANDFlag = 0;
}
if (RDRANDFlag) if (RDRANDFlag)
{ {
uint16_t RDRANDValue = 0; uint16_t RDRANDValue = 0;
@ -62,26 +78,6 @@ namespace Random
uint32_t rand32() uint32_t rand32()
{ {
#if defined(a86) #if defined(a86)
static int RDRANDFlag = 0x1A1A;
if (unlikely(RDRANDFlag == 0x1A1A))
{
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) != 0)
{
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{
CPU::x86::AMD::CPUID0x00000001 cpuid;
RDRANDFlag = cpuid.ECX.RDRAND;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
CPU::x86::Intel::CPUID0x00000001 cpuid;
RDRANDFlag = cpuid.ECX.RDRAND;
}
}
else
RDRANDFlag = 0;
}
if (RDRANDFlag) if (RDRANDFlag)
{ {
uint32_t RDRANDValue = 0; uint32_t RDRANDValue = 0;
@ -99,26 +95,6 @@ namespace Random
uint64_t rand64() uint64_t rand64()
{ {
#if defined(a86) #if defined(a86)
static int RDRANDFlag = 0x1A1A;
if (unlikely(RDRANDFlag == 0x1A1A))
{
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) != 0)
{
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{
CPU::x86::AMD::CPUID0x00000001 cpuid;
RDRANDFlag = cpuid.ECX.RDRAND;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
CPU::x86::Intel::CPUID0x00000001 cpuid;
RDRANDFlag = cpuid.ECX.RDRAND;
}
}
else
RDRANDFlag = 0;
}
if (RDRANDFlag) if (RDRANDFlag)
{ {
uintptr_t RDRANDValue = 0; uintptr_t RDRANDValue = 0;