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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* Copyright Matt Borland 2022 - 2025.
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See http://www.boost.org for most recent version including documentation.
*
* $Id$
*/
#ifndef BOOST_RANDOM_XOSHIRO_HPP
#define BOOST_RANDOM_XOSHIRO_HPP
#include <boost/random/detail/config.hpp>
#include <boost/random/detail/xoshiro_base.hpp>
#include <boost/core/bit.hpp>
#include <array>
#include <cstdint>
namespace boost {
namespace random {
/**
* This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators.
* It has excellent (sub-ns) speed, a state (256 bits) that is large
* enough for any parallel application, and it passes all tests we are
* aware of.
*
* For generating just floating-point numbers, xoshiro256+ is even faster.
*/
class xoshiro256pp final : public detail::xoshiro_base<xoshiro256pp, 4>
{
private:
using Base = detail::xoshiro_base<xoshiro256pp, 4>;
public:
using Base::Base;
inline result_type next() noexcept
{
const std::uint64_t result = boost::core::rotl(state_[0] + state_[3], 23) + state_[0];
const std::uint64_t t = state_[1] << 17;
state_[2] ^= state_[0];
state_[3] ^= state_[1];
state_[1] ^= state_[2];
state_[0] ^= state_[3];
state_[2] ^= t;
state_[3] = boost::core::rotl(state_[3], 45);
return result;
}
};
/**
* This is xoshiro256+ 1.0, our best and fastest generator for floating-point
* numbers. We suggest to use its upper bits for floating-point
* generation, as it is slightly faster than xoshiro256++/xoshiro256**. It
* passes all tests we are aware of except for the lowest three bits,
* which might fail linearity tests (and just those), so if low linear
* complexity is not considered an issue (as it is usually the case) it
* can be used to generate 64-bit outputs, too.
*/
class xoshiro256d final : public detail::xoshiro_base<xoshiro256d, 4, double>
{
private:
using Base = detail::xoshiro_base<xoshiro256d, 4, double>;
public:
using Base::Base;
inline std::uint64_t next_int() noexcept
{
const std::uint64_t result = state_[0] + state_[3];
const std::uint64_t t = state_[1] << 17;
state_[2] ^= state_[0];
state_[3] ^= state_[1];
state_[1] ^= state_[2];
state_[0] ^= state_[3];
state_[2] ^= t;
state_[3] = boost::core::rotl(state_[3], 45);
return result;
}
inline result_type next() noexcept
{
#ifdef BOOST_RANDOM_HAS_HEX_FLOAT
return static_cast<double>((next_int() >> 11)) * 0x1.0p-53;
#else
return static_cast<double>((next_int() >> 11)) * 1.11022302462515654e-16;
#endif
}
static constexpr result_type (min)() noexcept
{
#ifdef BOOST_RANDOM_HAS_HEX_FLOAT
return static_cast<double>((std::numeric_limits<std::uint64_t>::min)() >> 11) * 0x1.0p-53;
#else
return static_cast<double>((std::numeric_limits<std::uint64_t>::min)() >> 11) * 1.11022302462515654e-16;
#endif
}
static constexpr result_type (max)() noexcept
{
#ifdef BOOST_RANDOM_HAS_HEX_FLOAT
return static_cast<double>((std::numeric_limits<std::uint64_t>::max)()) * 0x1.0p-53;
#else
return static_cast<double>((std::numeric_limits<std::uint64_t>::max)()) * 1.11022302462515654e-16;
#endif
}
};
/**
* This is xoshiro256** 1.0, one of our all-purpose, rock-solid
* generators. It has excellent (sub-ns) speed, a state (256 bits) that is
* large enough for any parallel application, and it passes all tests we
* are aware of.
*
* For generating just floating-point numbers, xoshiro256+ is even faster.
*/
class xoshiro256mm final : public detail::xoshiro_base<xoshiro256mm, 4>
{
private:
using Base = detail::xoshiro_base<xoshiro256mm, 4>;
public:
using Base::Base;
inline result_type next() noexcept
{
const std::uint64_t result = boost::core::rotl(state_[1] * 5, 7) * 9U;
const std::uint64_t t = state_[1] << 17;
state_[2] ^= state_[0];
state_[3] ^= state_[1];
state_[1] ^= state_[2];
state_[0] ^= state_[3];
state_[2] ^= t;
state_[3] = boost::core::rotl(state_[3], 45);
return result;
}
};
/**
* This is xoshiro512++ 1.0, one of our all-purpose, rock-solid
* generators. It has excellent (about 1ns) speed, a state (512 bits) that
* is large enough for any parallel application, and it passes all tests
* we are aware of.
*
* For generating just floating-point numbers, xoshiro512+ is even faster.
*
* The state must be seeded so that it is not everywhere zero. If you have
* a 64-bit seed, we suggest to seed a splitmix64 generator and use its
* output to fill s.
*/
class xoshiro512pp final : public detail::xoshiro_base<xoshiro512pp, 8>
{
private:
using Base = detail::xoshiro_base<xoshiro512pp, 8>;
public:
using Base::Base;
inline result_type next() noexcept
{
const std::uint64_t result = boost::core::rotl(state_[0] + state_[2], 17) + state_[2];
const std::uint64_t t = state_[1] << 11;
state_[2] ^= state_[0];
state_[5] ^= state_[1];
state_[1] ^= state_[2];
state_[7] ^= state_[3];
state_[3] ^= state_[4];
state_[4] ^= state_[5];
state_[0] ^= state_[6];
state_[6] ^= state_[7];
state_[6] ^= t;
state_[7] = boost::core::rotl(state_[7], 21);
return result;
}
};
/**
* This is xoshiro512** 1.0, one of our all-purpose, rock-solid generators
* with increased state size. It has excellent (about 1ns) speed, a state
* (512 bits) that is large enough for any parallel application, and it
* passes all tests we are aware of.
*
* For generating just floating-point numbers, xoshiro512+ is even faster.
*
* The state must be seeded so that it is not everywhere zero. If you have
* a 64-bit seed, we suggest to seed a splitmix64 generator and use its
* output to fill s.
*/
class xoshiro512mm final : public detail::xoshiro_base<xoshiro512mm, 8>
{
private:
using Base = detail::xoshiro_base<xoshiro512mm, 8>;
public:
using Base::Base;
inline result_type next() noexcept
{
const std::uint64_t result = boost::core::rotl(state_[1] * 5, 7) * 9;
const std::uint64_t t = state_[1] << 11;
state_[2] ^= state_[0];
state_[5] ^= state_[1];
state_[1] ^= state_[2];
state_[7] ^= state_[3];
state_[3] ^= state_[4];
state_[4] ^= state_[5];
state_[0] ^= state_[6];
state_[6] ^= state_[7];
state_[6] ^= t;
state_[7] = boost::core::rotl(state_[7], 21);
return result;
}
};
/**
* This is xoshiro512+ 1.0, our generator for floating-point numbers with
* increased state size. We suggest to use its upper bits for
* floating-point generation, as it is slightly faster than xoshiro512**.
* It passes all tests we are aware of except for the lowest three bits,
* which might fail linearity tests (and just those), so if low linear
* complexity is not considered an issue (as it is usually the case) it
* can be used to generate 64-bit outputs, too.
*
* We suggest to use a sign test to extract a random Boolean value, and
* right shifts to extract subsets of bits.
*
* The state must be seeded so that it is not everywhere zero. If you have
* a 64-bit seed, we suggest to seed a splitmix64 generator and use its
* output to fill s.
*/
class xoshiro512d final : public detail::xoshiro_base<xoshiro512d, 8, double>
{
private:
using Base = detail::xoshiro_base<xoshiro512d, 8, double>;
public:
using Base::Base;
inline std::uint64_t next_int() noexcept
{
const std::uint64_t result = state_[0] + state_[2];
const std::uint64_t t = state_[1] << 11;
state_[2] ^= state_[0];
state_[5] ^= state_[1];
state_[1] ^= state_[2];
state_[7] ^= state_[3];
state_[3] ^= state_[4];
state_[4] ^= state_[5];
state_[0] ^= state_[6];
state_[6] ^= state_[7];
state_[6] ^= t;
state_[7] = boost::core::rotl(state_[7], 21);
return result;
}
inline result_type next() noexcept
{
#ifdef BOOST_RANDOM_HAS_HEX_FLOAT
return static_cast<double>((next_int() >> 11)) * 0x1.0p-53;
#else
return static_cast<double>((next_int() >> 11)) * 1.11022302462515654e-16;
#endif
}
static constexpr result_type (min)() noexcept
{
#ifdef BOOST_RANDOM_HAS_HEX_FLOAT
return static_cast<double>((std::numeric_limits<std::uint64_t>::min)() >> 11) * 0x1.0p-53;
#else
return static_cast<double>((std::numeric_limits<std::uint64_t>::min)() >> 11) * 1.11022302462515654e-16;
#endif
}
static constexpr result_type (max)() noexcept
{
#ifdef BOOST_RANDOM_HAS_HEX_FLOAT
return static_cast<double>((std::numeric_limits<std::uint64_t>::max)() >> 11) * 0x1.0p-53;
#else
return static_cast<double>((std::numeric_limits<std::uint64_t>::max)() >> 11) * 1.11022302462515654e-16;
#endif
}
};
/**
* This is xoshiro128++ 1.0, one of our 32-bit all-purpose, rock-solid
* generators. It has excellent speed, a state size (128 bits) that is
* large enough for mild parallelism, and it passes all tests we are aware
* of.
*
* For generating just single-precision (i.e., 32-bit) floating-point
* numbers, xoshiro128+ is even faster.
*
* The state must be seeded so that it is not everywhere zero.
*/
class xoshiro128pp final : public detail::xoshiro_base<xoshiro128pp, 4, std::uint32_t, std::uint32_t>
{
private:
using Base = detail::xoshiro_base<xoshiro128pp, 4, std::uint32_t, std::uint32_t>;
public:
using Base::Base;
inline result_type next() noexcept
{
const std::uint32_t result = boost::core::rotl(state_[0] + state_[3], 7) + state_[0];
const std::uint32_t t = state_[1] << 9;
state_[2] ^= state_[0];
state_[3] ^= state_[1];
state_[1] ^= state_[2];
state_[0] ^= state_[3];
state_[2] ^= t;
state_[3] = boost::core::rotl(state_[3], 11);
return result;
}
};
/**
* This is xoshiro128** 1.1, one of our 32-bit all-purpose, rock-solid
* generators. It has excellent speed, a state size (128 bits) that is
* large enough for mild parallelism, and it passes all tests we are aware
* of.
*
* Note that version 1.0 had mistakenly state_[0] instead of state_[1] as state
* word passed to the scrambler.
*
* For generating just single-precision (i.e., 32-bit) floating-point
* numbers, xoshiro128+ is even faster.
*
* The state must be seeded so that it is not everywhere zero.
*/
class xoshiro128mm final : public detail::xoshiro_base<xoshiro128mm, 4, std::uint32_t, std::uint32_t>
{
private:
using Base = detail::xoshiro_base<xoshiro128mm, 4, std::uint32_t, std::uint32_t>;
public:
using Base::Base;
inline result_type next() noexcept
{
const std::uint32_t result = boost::core::rotl(state_[1] * 5, 7) * 9;
const std::uint32_t t = state_[1] << 9;
state_[2] ^= state_[0];
state_[3] ^= state_[1];
state_[1] ^= state_[2];
state_[0] ^= state_[3];
state_[2] ^= t;
state_[3] = boost::core::rotl(state_[3], 11);
return result;
}
};
/**
* This is xoshiro128+ 1.0, our best and fastest 32-bit generator for 32-bit
* floating-point numbers. We suggest to use its upper bits for
* floating-point generation, as it is slightly faster than xoshiro128**.
* It passes all tests we are aware of except for
* linearity tests, as the lowest four bits have low linear complexity, so
* if low linear complexity is not considered an issue (as it is usually
* the case) it can be used to generate 32-bit outputs, too.
*
* We suggest to use a sign test to extract a random Boolean value, and
* right shifts to extract subsets of bits.
*
* The state must be seeded so that it is not everywhere zero.
*/
class xoshiro128f final : public detail::xoshiro_base<xoshiro128f, 4, float, std::uint32_t>
{
private:
using Base = detail::xoshiro_base<xoshiro128f, 4, float, std::uint32_t>;
public:
using Base::Base;
inline std::uint32_t next_int() noexcept
{
const std::uint32_t result = state_[0] + state_[3];
const std::uint32_t t = state_[1] << 9;
state_[2] ^= state_[0];
state_[3] ^= state_[1];
state_[1] ^= state_[2];
state_[0] ^= state_[3];
state_[2] ^= t;
state_[3] = boost::core::rotl(state_[3], 11);
return result;
}
inline result_type next() noexcept
{
#ifdef BOOST_RANDOM_HAS_HEX_FLOAT
return static_cast<float>((next_int() >> 8)) * 0x1.0p-24f;
#else
return static_cast<float>((next_int() >> 8)) * 5.9604645e-08f;
#endif
}
static constexpr result_type (min)() noexcept
{
#ifdef BOOST_RANDOM_HAS_HEX_FLOAT
return static_cast<float>((std::numeric_limits<std::uint32_t>::min)() >> 8) * 0x1.0p-24f;
#else
return static_cast<float>((std::numeric_limits<std::uint64_t>::min)() >> 8) * 5.9604645e-08f;
#endif
}
static constexpr result_type (max)() noexcept
{
#ifdef BOOST_RANDOM_HAS_HEX_FLOAT
return static_cast<float>((std::numeric_limits<std::uint32_t>::max)() >> 8) * 0x1.0p-24f;
#else
return static_cast<float>((std::numeric_limits<std::uint64_t>::max)() >> 8) * 5.9604645e-08f;
#endif
}
};
} // namespace random
} // namespace boost
#endif //BOOST_RANDOM_XOSHIRO_HPP