From 6cedac03f54607d7ae343e295783d487b42cbabc Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Fri, 26 Apr 2024 18:33:43 +0300 Subject: [PATCH] Refactor RNG code --- core/random.cpp | 98 +++++++++++++++++++------------------------------ 1 file changed, 37 insertions(+), 61 deletions(-) diff --git a/core/random.cpp b/core/random.cpp index bcbd9a2..e31df9d 100644 --- a/core/random.cpp +++ b/core/random.cpp @@ -20,31 +20,47 @@ 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() { #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) { uint16_t RDRANDValue = 0; @@ -62,26 +78,6 @@ namespace Random uint32_t rand32() { #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) { uint32_t RDRANDValue = 0; @@ -99,26 +95,6 @@ namespace Random uint64_t rand64() { #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) { uintptr_t RDRANDValue = 0;