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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#pragma once
/**
@file
@brief XorShift
@author MITSUNARI Shigeo(@herumi)
@author MITSUNARI Shigeo
*/
#include <cybozu/inttype.hpp>
namespace cybozu {
class XorShift {
uint32_t x_, y_, z_, w_;
public:
explicit XorShift(uint32_t x = 0, uint32_t y = 0, uint32_t z = 0, uint32_t w = 0)
{
init(x, y, z, w);
}
void init(uint32_t x = 0, uint32_t y = 0, uint32_t z = 0, uint32_t w = 0)
{
x_ = x ? x : 123456789;
y_ = y ? y : 362436069;
z_ = z ? z : 521288629;
w_ = w ? w : 88675123;
}
uint32_t get32()
{
unsigned int t = x_ ^ (x_ << 11);
x_ = y_; y_ = z_; z_ = w_;
return w_ = (w_ ^ (w_ >> 19)) ^ (t ^ (t >> 8));
}
uint32_t operator()() { return get32(); }
uint64_t get64()
{
uint32_t a = get32();
uint32_t b = get32();
return (uint64_t(a) << 32) | b;
}
template<class T>
void read(T *x, size_t n)
{
const size_t size = sizeof(T) * n;
uint8_t *p8 = static_cast<uint8_t*>(x);
for (size_t i = 0; i < size; i++) {
p8[i] = static_cast<uint8_t>(get32());
}
}
void read(uint32_t *x, size_t n)
{
for (size_t i = 0; i < n; i++) {
x[i] = get32();
}
}
void read(uint64_t *x, size_t n)
{
for (size_t i = 0; i < n; i++) {
x[i] = get64();
}
}
};
// see http://xorshift.di.unimi.it/xorshift128plus.c
class XorShift128Plus {
uint64_t s_[2];
static const uint64_t seed0 = 123456789;
static const uint64_t seed1 = 987654321;
public:
explicit XorShift128Plus(uint64_t s0 = seed0, uint64_t s1 = seed1)
{
init(s0, s1);
}
void init(uint64_t s0 = seed0, uint64_t s1 = seed1)
{
s_[0] = s0;
s_[1] = s1;
}
uint32_t get32()
{
return static_cast<uint32_t>(get64());
}
uint64_t operator()() { return get64(); }
uint64_t get64()
{
uint64_t s1 = s_[0];
const uint64_t s0 = s_[1];
s_[0] = s0;
s1 ^= s1 << 23;
s_[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
return s_[1] + s0;
}
template<class T>
void read(T *x, size_t n)
{
const size_t size = sizeof(T) * n;
uint8_t *p8 = static_cast<uint8_t*>(x);
for (size_t i = 0; i < size; i++) {
p8[i] = static_cast<uint8_t>(get32());
}
}
void read(uint32_t *x, size_t n)
{
for (size_t i = 0; i < n; i++) {
x[i] = get32();
}
}
void read(uint64_t *x, size_t n)
{
for (size_t i = 0; i < n; i++) {
x[i] = get64();
}
}
};
// see http://xoroshiro.di.unimi.it/xoroshiro128plus.c
class Xoroshiro128Plus {
uint64_t s_[2];
static const uint64_t seed0 = 123456789;
static const uint64_t seed1 = 987654321;
uint64_t rotl(uint64_t x, unsigned int k) const
{
return (x << k) | (x >> (64 - k));
}
public:
explicit Xoroshiro128Plus(uint64_t s0 = seed0, uint64_t s1 = seed1)
{
init(s0, s1);
}
void init(uint64_t s0 = seed0, uint64_t s1 = seed1)
{
s_[0] = s0;
s_[1] = s1;
}
uint32_t get32()
{
return static_cast<uint32_t>(get64());
}
uint64_t operator()() { return get64(); }
uint64_t get64()
{
uint64_t s0 = s_[0];
uint64_t s1 = s_[1];
uint64_t result = s0 + s1;
s1 ^= s0;
s_[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14);
s_[1] = rotl(s1, 36);
return result;
}
template<class T>
void read(T *x, size_t n)
{
const size_t size = sizeof(T) * n;
uint8_t *p8 = static_cast<uint8_t*>(x);
for (size_t i = 0; i < size; i++) {
p8[i] = static_cast<uint8_t>(get32());
}
}
void read(uint32_t *x, size_t n)
{
for (size_t i = 0; i < n; i++) {
x[i] = get32();
}
}
void read(uint64_t *x, size_t n)
{
for (size_t i = 0; i < n; i++) {
x[i] = get64();
}
}
};
} // cybozu
|