-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
rdrand.cpp
76 lines (70 loc) · 1.33 KB
/
rdrand.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef _MSC_VER
#include <intrin.h>
#else
#include <x86intrin.h>
#endif
#include <cybozu/xorshift.hpp>
#include <cybozu/random_generator.hpp>
#include <cybozu/benchmark.hpp>
#if CYBOZU_CPP_VERSION == CYBOZU_CPP_VERSION_CPP11
#include <random>
#define USE_RANDOM
#endif
#define XBYAK_NO_OP_NAMES
#include <xbyak/xbyak_util.h>
#if defined(_MSC_VER) || defined(__RDRND__)
#define USE_RDRAND
uint32_t use_rdrand()
{
for (int i = 0; i < 10; i++) {
uint32_t x;
if (_rdrand32_step(&x)) return x;
}
fprintf(stderr, "_rdrand32_step err\n");
exit(1);
}
#endif
uint32_t use_xorshift()
{
static cybozu::XorShift rg;
return rg.get32();
}
uint32_t use_urandom()
{
static cybozu::RandomGenerator rg;
return rg.get32();
}
#ifdef USE_RANDOM
uint32_t use_mt()
{
static std::mt19937 mt;
return mt();
}
uint32_t use_random_device()
{
static std::random_device rd;
// static std::random_device rd("/dev/urandom");
return rd();
}
#endif
int main()
{
#ifdef USE_RDRAND
Xbyak::util::Cpu cpu;
if (cpu.has(Xbyak::util::Cpu::tRDRAND)) {
CYBOZU_BENCH("rdrand", use_rdrand);
} else
#endif
{
puts("no rdrand");
}
CYBOZU_BENCH("xorshift", use_xorshift);
CYBOZU_BENCH("urandom", use_urandom);
#ifdef USE_RANDOM
CYBOZU_BENCH("mt19937", use_mt);
CYBOZU_BENCH("random_device", use_random_device);
#endif
}