Unverified Commit 219d5e40 authored by PhoeniX's avatar PhoeniX

RNG support

parent d0528578
......@@ -10,3 +10,4 @@
#include "kernlib/ports.hpp"
#include "kernlib/printf.hpp"
#include "kernlib/std.hpp"
#include "kernlib/rand.hpp"
// PhoeniX OS Kernel library standard functions
// Copyright © 2017 Yury Popov a.k.a. PhoeniX
#pragma once
#include <stdint.h>
class RAND {
private:
static uint64_t _get64();
public:
template<typename T> static inline T get() { return static_cast<T>(_get64()); }
};
// PhoeniX OS Kernel library printf functions
// Copyright © 2017 Yury Popov a.k.a. PhoeniX
#include "kernlib.hpp"
#include "kernlib/rand.hpp"
static inline bool check_rdrand() {
uint32_t cpuid;
asm volatile("cpuid":"=c"(cpuid):"a"(uint32_t(1)), "c"(uint32_t(0)):"ebx", "edx");
return cpuid & (1 << 30);
}
static uint64_t _genseed() {
if (check_rdrand()) {
uint64_t val;
asm volatile("rdrandq %q0":"=r"(val));
return val;
}
return rdtsc();
}
static uint64_t seed = _genseed();
uint64_t RAND::_get64() {
uint64_t x = seed;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
seed = x;
return x * 0x2545f4914f6cdd1dllu;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment