From a43e4f1593d10d7517e21458ce17f1789e6a1d42 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 20 Dec 2022 06:55:34 +0200 Subject: [PATCH] Updated the random number generator --- Core/Random.cpp | 139 +++++++++++++++++++++++++++++++++++++++++++++-- include/rand.hpp | 2 +- 2 files changed, 135 insertions(+), 6 deletions(-) diff --git a/Core/Random.cpp b/Core/Random.cpp index 8e05863..53621c0 100644 --- a/Core/Random.cpp +++ b/Core/Random.cpp @@ -1,4 +1,5 @@ #include +#include namespace Random { @@ -6,22 +7,150 @@ namespace Random uint16_t rand16() { + int RDRANDFlag = 0; + if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0) + { +#if defined(__amd64__) + CPU::x64::AMD::CPUID0x1 cpuid1amd; +#elif defined(__i386__) + CPU::x32::AMD::CPUID0x1 cpuid1amd; +#endif +#if defined(__amd64__) || defined(__i386__) + asmv("cpuid" + : "=a"(cpuid1amd.EAX.raw), "=b"(cpuid1amd.EBX.raw), "=c"(cpuid1amd.ECX.raw), "=d"(cpuid1amd.EDX.raw) + : "a"(0x1)); +#endif + RDRANDFlag = cpuid1amd.ECX.RDRAND; + } + if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0) + { +#if defined(__amd64__) + CPU::x64::Intel::CPUID0x1 cpuid1intel; +#elif defined(__i386__) + CPU::x32::Intel::CPUID0x1 cpuid1intel; +#endif +#if defined(__amd64__) || defined(__i386__) + asmv("cpuid" + : "=a"(cpuid1intel.EAX.raw), "=b"(cpuid1intel.EBX.raw), "=c"(cpuid1intel.ECX.raw), "=d"(cpuid1intel.EDX.raw) + : "a"(0x1)); +#endif + RDRANDFlag = cpuid1intel.ECX.RDRAND; + } + + if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) == 0) + RDRANDFlag = 0; + +#if defined(__amd64__) || defined(__i386__) + if (RDRANDFlag) + { + uint16_t RDRANDValue = 0; + asmv("1: rdrand %0; jnc 1b" + : "=r"(RDRANDValue)); + return RDRANDValue; + } +#endif + Seed = Seed * 1103515245 + 12345; return (uint16_t)(Seed / 65536) % __UINT16_MAX__; } uint32_t rand32() { + int RDRANDFlag = 0; + if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0) + { +#if defined(__amd64__) + CPU::x64::AMD::CPUID0x1 cpuid1amd; +#elif defined(__i386__) + CPU::x32::AMD::CPUID0x1 cpuid1amd; +#endif +#if defined(__amd64__) || defined(__i386__) + asmv("cpuid" + : "=a"(cpuid1amd.EAX.raw), "=b"(cpuid1amd.EBX.raw), "=c"(cpuid1amd.ECX.raw), "=d"(cpuid1amd.EDX.raw) + : "a"(0x1)); +#endif + RDRANDFlag = cpuid1amd.ECX.RDRAND; + } + if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0) + { +#if defined(__amd64__) + CPU::x64::Intel::CPUID0x1 cpuid1intel; +#elif defined(__i386__) + CPU::x32::Intel::CPUID0x1 cpuid1intel; +#endif +#if defined(__amd64__) || defined(__i386__) + asmv("cpuid" + : "=a"(cpuid1intel.EAX.raw), "=b"(cpuid1intel.EBX.raw), "=c"(cpuid1intel.ECX.raw), "=d"(cpuid1intel.EDX.raw) + : "a"(0x1)); +#endif + RDRANDFlag = cpuid1intel.ECX.RDRAND; + } + + if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) == 0) + RDRANDFlag = 0; + +#if defined(__amd64__) || defined(__i386__) + if (RDRANDFlag) + { + uint32_t RDRANDValue = 0; + asmv("1: rdrand %0; jnc 1b" + : "=r"(RDRANDValue)); + return RDRANDValue; + } +#endif + Seed = Seed * 1103515245 + 12345; - return (uint32_t)(Seed / 65536) % __UINT32_MAX__; + return (uint32_t)(Seed / 65536) % __UINT16_MAX__; } uint64_t rand64() { + int RDRANDFlag = 0; + if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0) + { +#if defined(__amd64__) + CPU::x64::AMD::CPUID0x1 cpuid1amd; +#elif defined(__i386__) + CPU::x32::AMD::CPUID0x1 cpuid1amd; +#endif +#if defined(__amd64__) || defined(__i386__) + asmv("cpuid" + : "=a"(cpuid1amd.EAX.raw), "=b"(cpuid1amd.EBX.raw), "=c"(cpuid1amd.ECX.raw), "=d"(cpuid1amd.EDX.raw) + : "a"(0x1)); +#endif + RDRANDFlag = cpuid1amd.ECX.RDRAND; + } + if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0) + { +#if defined(__amd64__) + CPU::x64::Intel::CPUID0x1 cpuid1intel; +#elif defined(__i386__) + CPU::x32::Intel::CPUID0x1 cpuid1intel; +#endif +#if defined(__amd64__) || defined(__i386__) + asmv("cpuid" + : "=a"(cpuid1intel.EAX.raw), "=b"(cpuid1intel.EBX.raw), "=c"(cpuid1intel.ECX.raw), "=d"(cpuid1intel.EDX.raw) + : "a"(0x1)); +#endif + RDRANDFlag = cpuid1intel.ECX.RDRAND; + } + + if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) == 0) + RDRANDFlag = 0; + +#if defined(__amd64__) || defined(__i386__) + if (RDRANDFlag) + { + uint64_t RDRANDValue = 0; + asmv("1: rdrand %0; jnc 1b" + : "=r"(RDRANDValue)); + return RDRANDValue; + } +#endif + Seed = Seed * 1103515245 + 12345; - return (uint64_t)(Seed / 65536) % __UINT64_MAX__; + return (uint64_t)(Seed / 65536) % __UINT16_MAX__; } - void changeseed(uint64_t CustomSeed) { Seed = CustomSeed; } - -} \ No newline at end of file + void ChangeSeed(uint64_t CustomSeed) { Seed = CustomSeed; } +} diff --git a/include/rand.hpp b/include/rand.hpp index 81cff9e..715851c 100644 --- a/include/rand.hpp +++ b/include/rand.hpp @@ -8,7 +8,7 @@ namespace Random uint16_t rand16(); uint32_t rand32(); uint64_t rand64(); - void changeseed(uint64_t CustomSeed); + void ChangeSeed(uint64_t CustomSeed); } #endif // !__FENNIX_KERNEL_RANDOM_H__