Optimize RNG functions

This commit is contained in:
Alex 2023-04-16 22:05:02 +03:00
parent 37f5b77421
commit b15f738e16
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
2 changed files with 68 additions and 67 deletions

View File

@ -24,7 +24,6 @@
#include <printf.h>
#include <cwalk.h>
#include <md5.h>
// #include <ini.h>
#include "../../kernel.h"
#include "../../DAPI.hpp"
@ -36,16 +35,6 @@ NewLock(DriverInterruptLock);
namespace Driver
{
const char *DriverTypesName[] = {
"Unknown",
"Generic",
"Display",
"Network",
"Storage",
"FileSystem",
"Input",
"Audio"};
void Driver::Panic()
{
#ifdef DEBUG

View File

@ -24,24 +24,27 @@ namespace Random
uint16_t rand16()
{
int RDRANDFlag = 0;
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{
CPU::x86::AMD::CPUID0x00000001 cpuid;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
CPU::x86::Intel::CPUID0x00000001 cpuid;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) == 0)
RDRANDFlag = 0;
#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;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
CPU::x86::Intel::CPUID0x00000001 cpuid;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
}
}
if (RDRANDFlag)
{
uint16_t RDRANDValue = 0;
@ -49,32 +52,36 @@ namespace Random
: "=r"(RDRANDValue));
return RDRANDValue;
}
#endif
Seed = Seed * 1103515245 + 12345;
return (uint16_t)(Seed / 65536) % __UINT16_MAX__;
#endif
return 0;
}
uint32_t rand32()
{
int RDRANDFlag = 0;
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{
CPU::x86::AMD::CPUID0x00000001 cpuid;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
CPU::x86::Intel::CPUID0x00000001 cpuid;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) == 0)
RDRANDFlag = 0;
#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;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
CPU::x86::Intel::CPUID0x00000001 cpuid;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
}
}
if (RDRANDFlag)
{
uint32_t RDRANDValue = 0;
@ -82,32 +89,36 @@ namespace Random
: "=r"(RDRANDValue));
return RDRANDValue;
}
#endif
Seed = Seed * 1103515245 + 12345;
return (uint32_t)(Seed / 65536) % __UINT16_MAX__;
return (uint32_t)(Seed / 65536) % __UINT32_MAX__;
#endif
return 0;
}
uint64_t rand64()
{
int RDRANDFlag = 0;
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{
CPU::x86::AMD::CPUID0x00000001 cpuid;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
CPU::x86::Intel::CPUID0x00000001 cpuid;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_TCG) == 0)
RDRANDFlag = 0;
#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;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
CPU::x86::Intel::CPUID0x00000001 cpuid;
cpuid.Get();
RDRANDFlag = cpuid.ECX.RDRAND;
}
}
}
if (RDRANDFlag)
{
uint64_t RDRANDValue = 0;
@ -115,10 +126,11 @@ namespace Random
: "=r"(RDRANDValue));
return RDRANDValue;
}
#endif
Seed = Seed * 1103515245 + 12345;
return (uint64_t)(Seed / 65536) % __UINT16_MAX__;
return (uint64_t)(Seed / 65536) % __UINT64_MAX__;
#endif
return 0;
}
void ChangeSeed(uint64_t CustomSeed) { Seed = CustomSeed; }