18 #include <sys/random.h>
27 #include <immintrin.h>
49 constexpr
unsigned flag_RDRAND = (1 << 30);
54 return ((ex[2] & flag_RDRAND) == flag_RDRAND);
56 unsigned eax, ebx, ecx, edx;
58 __get_cpuid(1, &eax, &ebx, &ecx, &edx);
59 return ((ecx & flag_RDRAND) == flag_RDRAND);
81 #if ENABLE_RNDFILE && !ENABLE_DEVRAND
85 static RandFile& getInstance()
87 static RandFile instance;
96 fSize = fread(&v,
sizeof(
unsigned), 1, dataFile);
98 _readNextRandDataFile();
105 RandFile() { _readNextRandDataFile(); }
115 void _readNextRandDataFile();
118 RandFile(RandFile
const&) =
delete;
119 void operator=(RandFile
const&) =
delete;
127 #if ENABLE_RDRAND || ENABLE_DEVRAND
128 constexpr
int max_rdrand_tries = 10;
129 for (
int i = 0; i < max_rdrand_tries; ++i) {
131 if (
sizeof(
unsigned) == getrandom(
reinterpret_cast<char*
>(pv),
sizeof(
unsigned), 0))
133 if (_rdrand32_step(pv))
145 #if ENABLE_RNDFILE && !ENABLE_DEVRAND
146 unsigned NextRaw() {
return RandFile::getInstance().NextRaw(); }
152 throw std::runtime_error(
"Random number generator failed up to retry limit.");
162 constexpr
double inc = 1.0 / (double)(1ULL << 32U);
Definition: rdrandwrapper.hpp:36
bool SupportsRDRAND()
Definition: rdrandwrapper.hpp:78
static bool CheckHardwareRDRANDSupport()
Definition: rdrandwrapper.hpp:46
RandSupportSingleton()
Definition: rdrandwrapper.hpp:40
RandSupportSingleton(RandSupportSingleton const &)=delete
bool isSupported
Definition: rdrandwrapper.hpp:38
static RandSupportSingleton & Instance()
Get a pointer to the Instance of the singleton. (The instance will be instantiated,...
Definition: rdrandwrapper.hpp:70
void operator=(RandSupportSingleton const &)=delete
Definition: rdrandwrapper.hpp:123
bool getRdRand(unsigned *pv)
Definition: rdrandwrapper.hpp:125
unsigned NextRaw()
Definition: rdrandwrapper.hpp:148
real1_f Next()
Definition: rdrandwrapper.hpp:159
bool SupportsRDRAND()
Definition: rdrandwrapper.hpp:143
GLOSSARY: bitLenInt - "bit-length integer" - unsigned integer ID of qubit position in register bitCap...
Definition: complex16x2simd.hpp:25
float real1_f
Definition: qrack_types.hpp:103