diff options
Diffstat (limited to 'libs/libc/random.c')
-rw-r--r-- | libs/libc/random.c | 48 |
1 files changed, 19 insertions, 29 deletions
diff --git a/libs/libc/random.c b/libs/libc/random.c index 6296407..983357f 100644 --- a/libs/libc/random.c +++ b/libs/libc/random.c @@ -15,46 +15,36 @@ void srand(u32 seed) g_seed = seed; } -u32 rdrand(void) +static u32 default_rand(void) { -#ifdef KERNEL - if (!(cpu_features.ecx & CPUID_FEAT_ECX_RDRND)) - return rand(); - - u32 rd; - __asm__ volatile("1:\n" - "rdrand %0\n" - "jnc 1b\n" - : "=r"(rd)); - return rd; -#else - return rand(); -#endif + g_seed = g_seed * 1103515245 + 12345; + return (g_seed >> 16) & 0x7FFF; } -u32 rdseed(void) +u32 rand(void) { #ifdef KERNEL - if (!(cpu_extended_features.ebx & CPUID_EXT_FEAT_EBX_RDSEED)) - return rand(); - u32 rd; - __asm__ volatile("1:\n" - "rdseed %0\n" - "jnc 1b\n" - : "=r"(rd)); + if (cpu_extended_features.ebx & CPUID_EXT_FEAT_EBX_RDSEED) { + __asm__ volatile("1:\n" + "rdseed %0\n" + "jnc 1b\n" + : "=r"(rd)); + } else if (cpu_features.ecx & CPUID_FEAT_ECX_RDRND) { + __asm__ volatile("1:\n" + "rdrand %0\n" + "jnc 1b\n" + : "=r"(rd)); + } else { + rd = default_rand(); + } + return rd; #else - return rand(); + return default_rand(); #endif } -u32 rand(void) -{ - g_seed = g_seed * 1103515245 + 12345; - return (g_seed >> 16) & 0x7FFF; -} - char *randstr(u32 size) { if (!size) |