| 1 | /* |
| 2 | * PCG Random Number Generation for C++ |
| 3 | * |
| 4 | * Copyright 2014-2019 Melissa O'Neill <oneill@pcg-random.org>, |
| 5 | * and the PCG Project contributors. |
| 6 | * |
| 7 | * SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (provided in |
| 10 | * LICENSE-APACHE.txt and at http://www.apache.org/licenses/LICENSE-2.0) |
| 11 | * or under the MIT license (provided in LICENSE-MIT.txt and at |
| 12 | * http://opensource.org/licenses/MIT), at your option. This file may not |
| 13 | * be copied, modified, or distributed except according to those terms. |
| 14 | * |
| 15 | * Distributed on an "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, either |
| 16 | * express or implied. See your chosen license for details. |
| 17 | * |
| 18 | * For additional information about the PCG random number generation scheme, |
| 19 | * visit http://www.pcg-random.org/. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * This code provides the reference implementation of the PCG family of |
| 24 | * random number generators. The code is complex because it implements |
| 25 | * |
| 26 | * - several members of the PCG family, specifically members corresponding |
| 27 | * to the output functions: |
| 28 | * - XSH RR (good for 64-bit state, 32-bit output) |
| 29 | * - XSH RS (good for 64-bit state, 32-bit output) |
| 30 | * - XSL RR (good for 128-bit state, 64-bit output) |
| 31 | * - RXS M XS (statistically most powerful generator) |
| 32 | * - XSL RR RR (good for 128-bit state, 128-bit output) |
| 33 | * - and RXS, RXS M, XSH, XSL (mostly for testing) |
| 34 | * - at potentially *arbitrary* bit sizes |
| 35 | * - with four different techniques for random streams (MCG, one-stream |
| 36 | * LCG, settable-stream LCG, unique-stream LCG) |
| 37 | * - and the extended generation schemes allowing arbitrary periods |
| 38 | * - with all features of C++11 random number generation (and more), |
| 39 | * some of which are somewhat painful, including |
| 40 | * - initializing with a SeedSequence which writes 32-bit values |
| 41 | * to memory, even though the state of the generator may not |
| 42 | * use 32-bit values (it might use smaller or larger integers) |
| 43 | * - I/O for RNGs and a prescribed format, which needs to handle |
| 44 | * the issue that 8-bit and 128-bit integers don't have working |
| 45 | * I/O routines (e.g., normally 8-bit = char, not integer) |
| 46 | * - equality and inequality for RNGs |
| 47 | * - and a number of convenience typedefs to mask all the complexity |
| 48 | * |
| 49 | * The code employes a fairly heavy level of abstraction, and has to deal |
| 50 | * with various C++ minutia. If you're looking to learn about how the PCG |
| 51 | * scheme works, you're probably best of starting with one of the other |
| 52 | * codebases (see www.pcg-random.org). But if you're curious about the |
| 53 | * constants for the various output functions used in those other, simpler, |
| 54 | * codebases, this code shows how they are calculated. |
| 55 | * |
| 56 | * On the positive side, at least there are convenience typedefs so that you |
| 57 | * can say |
| 58 | * |
| 59 | * pcg32 myRNG; |
| 60 | * |
| 61 | * rather than: |
| 62 | * |
| 63 | * pcg_detail::engine< |
| 64 | * uint32_t, // Output Type |
| 65 | * uint64_t, // State Type |
| 66 | * pcg_detail::xsh_rr_mixin<uint32_t, uint64_t>, true, // Output Func |
| 67 | * pcg_detail::specific_stream<uint64_t>, // Stream Kind |
| 68 | * pcg_detail::default_multiplier<uint64_t> // LCG Mult |
| 69 | * > myRNG; |
| 70 | * |
| 71 | */ |
| 72 | |
| 73 | #ifndef PCG_RAND_HPP_INCLUDED |
| 74 | #define PCG_RAND_HPP_INCLUDED 1 |
| 75 | |
| 76 | #include <algorithm> |
| 77 | #include <cinttypes> |
| 78 | #include <cstddef> |
| 79 | #include <cstdlib> |
| 80 | #include <cstring> |
| 81 | #include <cassert> |
| 82 | #include <limits> |
| 83 | #include <iostream> |
| 84 | #include <iterator> |
| 85 | #include <type_traits> |
| 86 | #include <utility> |
| 87 | #include <locale> |
| 88 | #include <new> |
| 89 | #include <stdexcept> |
| 90 | |
| 91 | #ifdef _MSC_VER |
| 92 | #pragma warning(disable:4146) |
| 93 | #endif |
| 94 | |
| 95 | #ifdef _MSC_VER |
| 96 | #define PCG_ALWAYS_INLINE __forceinline |
| 97 | #elif __GNUC__ |
| 98 | #define PCG_ALWAYS_INLINE __attribute__((always_inline)) |
| 99 | #else |
| 100 | #define PCG_ALWAYS_INLINE inline |
| 101 | #endif |
| 102 | |
| 103 | #ifdef min |
| 104 | #undef min |
| 105 | #endif |
| 106 | |
| 107 | #ifdef max |
| 108 | #undef max |
| 109 | #endif |
| 110 | |
| 111 | /* |
| 112 | * The pcg_extras namespace contains some support code that is likley to |
| 113 | * be useful for a variety of RNGs, including: |
| 114 | * - 128-bit int support for platforms where it isn't available natively |
| 115 | * - bit twiddling operations |
| 116 | * - I/O of 128-bit and 8-bit integers |
| 117 | * - Handling the evilness of SeedSeq |
| 118 | * - Support for efficiently producing random numbers less than a given |
| 119 | * bound |
| 120 | */ |
| 121 | |
| 122 | #include "pcg_extras.hpp" |
| 123 | |
| 124 | namespace pcg_detail { |
| 125 | |
| 126 | using namespace pcg_extras; |
| 127 | |
| 128 | /* |
| 129 | * The LCG generators need some constants to function. This code lets you |
| 130 | * look up the constant by *type*. For example |
| 131 | * |
| 132 | * default_multiplier<uint32_t>::multiplier() |
| 133 | * |
| 134 | * gives you the default multipler for 32-bit integers. We use the name |
| 135 | * of the constant and not a generic word like value to allow these classes |
| 136 | * to be used as mixins. |
| 137 | */ |
| 138 | |
| 139 | template <typename T> |
| 140 | struct default_multiplier { |
| 141 | // Not defined for an arbitrary type |
| 142 | }; |
| 143 | |
| 144 | template <typename T> |
| 145 | struct default_increment { |
| 146 | // Not defined for an arbitrary type |
| 147 | }; |
| 148 | |
| 149 | #define PCG_DEFINE_CONSTANT(type, what, kind, constant) \ |
| 150 | template <> \ |
| 151 | struct what ## _ ## kind<type> { \ |
| 152 | static constexpr type kind() { \ |
| 153 | return constant; \ |
| 154 | } \ |
| 155 | }; |
| 156 | |
| 157 | PCG_DEFINE_CONSTANT(uint8_t, default, multiplier, 141U) |
| 158 | PCG_DEFINE_CONSTANT(uint8_t, default, increment, 77U) |
| 159 | |
| 160 | PCG_DEFINE_CONSTANT(uint16_t, default, multiplier, 12829U) |
| 161 | PCG_DEFINE_CONSTANT(uint16_t, default, increment, 47989U) |
| 162 | |
| 163 | PCG_DEFINE_CONSTANT(uint32_t, default, multiplier, 747796405U) |
| 164 | PCG_DEFINE_CONSTANT(uint32_t, default, increment, 2891336453U) |
| 165 | |
| 166 | PCG_DEFINE_CONSTANT(uint64_t, default, multiplier, 6364136223846793005ULL) |
| 167 | PCG_DEFINE_CONSTANT(uint64_t, default, increment, 1442695040888963407ULL) |
| 168 | |
| 169 | PCG_DEFINE_CONSTANT(pcg128_t, default, multiplier, |
| 170 | PCG_128BIT_CONSTANT(2549297995355413924ULL,4865540595714422341ULL)) |
| 171 | PCG_DEFINE_CONSTANT(pcg128_t, default, increment, |
| 172 | PCG_128BIT_CONSTANT(6364136223846793005ULL,1442695040888963407ULL)) |
| 173 | |
| 174 | /* Alternative (cheaper) multipliers for 128-bit */ |
| 175 | |
| 176 | template <typename T> |
| 177 | struct cheap_multiplier : public default_multiplier<T> { |
| 178 | // For most types just use the default. |
| 179 | }; |
| 180 | |
| 181 | template <> |
| 182 | struct cheap_multiplier<pcg128_t> { |
| 183 | static constexpr uint64_t multiplier() { |
| 184 | return 0xda942042e4dd58b5ULL; |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | |
| 189 | /* |
| 190 | * Each PCG generator is available in four variants, based on how it applies |
| 191 | * the additive constant for its underlying LCG; the variations are: |
| 192 | * |
| 193 | * single stream - all instances use the same fixed constant, thus |
| 194 | * the RNG always somewhere in same sequence |
| 195 | * mcg - adds zero, resulting in a single stream and reduced |
| 196 | * period |
| 197 | * specific stream - the constant can be changed at any time, selecting |
| 198 | * a different random sequence |
| 199 | * unique stream - the constant is based on the memory address of the |
| 200 | * object, thus every RNG has its own unique sequence |
| 201 | * |
| 202 | * This variation is provided though mixin classes which define a function |
| 203 | * value called increment() that returns the nesessary additive constant. |
| 204 | */ |
| 205 | |
| 206 | |
| 207 | |
| 208 | /* |
| 209 | * unique stream |
| 210 | */ |
| 211 | |
| 212 | |
| 213 | template <typename itype> |
| 214 | class unique_stream { |
| 215 | protected: |
| 216 | static constexpr bool is_mcg = false; |
| 217 | |
| 218 | // Is never called, but is provided for symmetry with specific_stream |
| 219 | void set_stream(...) |
| 220 | { |
| 221 | abort(); |
| 222 | } |
| 223 | |
| 224 | public: |
| 225 | typedef itype state_type; |
| 226 | |
| 227 | constexpr itype increment() const { |
| 228 | return itype(reinterpret_cast<uintptr_t>(this) | 1); |
| 229 | } |
| 230 | |
| 231 | constexpr itype stream() const |
| 232 | { |
| 233 | return increment() >> 1; |
| 234 | } |
| 235 | |
| 236 | static constexpr bool can_specify_stream = false; |
| 237 | |
| 238 | static constexpr size_t streams_pow2() |
| 239 | { |
| 240 | return (sizeof(itype) < sizeof(size_t) ? sizeof(itype) |
| 241 | : sizeof(size_t))*8 - 1u; |
| 242 | } |
| 243 | |
| 244 | protected: |
| 245 | constexpr unique_stream() = default; |
| 246 | }; |
| 247 | |
| 248 | |
| 249 | /* |
| 250 | * no stream (mcg) |
| 251 | */ |
| 252 | |
| 253 | template <typename itype> |
| 254 | class no_stream { |
| 255 | protected: |
| 256 | static constexpr bool is_mcg = true; |
| 257 | |
| 258 | // Is never called, but is provided for symmetry with specific_stream |
| 259 | void set_stream(...) |
| 260 | { |
| 261 | abort(); |
| 262 | } |
| 263 | |
| 264 | public: |
| 265 | typedef itype state_type; |
| 266 | |
| 267 | static constexpr itype increment() { |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static constexpr bool can_specify_stream = false; |
| 272 | |
| 273 | static constexpr size_t streams_pow2() |
| 274 | { |
| 275 | return 0u; |
| 276 | } |
| 277 | |
| 278 | protected: |
| 279 | constexpr no_stream() = default; |
| 280 | }; |
| 281 | |
| 282 | |
| 283 | /* |
| 284 | * single stream/sequence (oneseq) |
| 285 | */ |
| 286 | |
| 287 | template <typename itype> |
| 288 | class oneseq_stream : public default_increment<itype> { |
| 289 | protected: |
| 290 | static constexpr bool is_mcg = false; |
| 291 | |
| 292 | // Is never called, but is provided for symmetry with specific_stream |
| 293 | void set_stream(...) |
| 294 | { |
| 295 | abort(); |
| 296 | } |
| 297 | |
| 298 | public: |
| 299 | typedef itype state_type; |
| 300 | |
| 301 | static constexpr itype stream() |
| 302 | { |
| 303 | return default_increment<itype>::increment() >> 1; |
| 304 | } |
| 305 | |
| 306 | static constexpr bool can_specify_stream = false; |
| 307 | |
| 308 | static constexpr size_t streams_pow2() |
| 309 | { |
| 310 | return 0u; |
| 311 | } |
| 312 | |
| 313 | protected: |
| 314 | constexpr oneseq_stream() = default; |
| 315 | }; |
| 316 | |
| 317 | |
| 318 | /* |
| 319 | * specific stream |
| 320 | */ |
| 321 | |
| 322 | template <typename itype> |
| 323 | class specific_stream { |
| 324 | protected: |
| 325 | static constexpr bool is_mcg = false; |
| 326 | |
| 327 | itype inc_ = default_increment<itype>::increment(); |
| 328 | |
| 329 | public: |
| 330 | typedef itype state_type; |
| 331 | typedef itype stream_state; |
| 332 | |
| 333 | constexpr itype increment() const { |
| 334 | return inc_; |
| 335 | } |
| 336 | |
| 337 | itype stream() |
| 338 | { |
| 339 | return inc_ >> 1; |
| 340 | } |
| 341 | |
| 342 | void set_stream(itype specific_seq) |
| 343 | { |
| 344 | inc_ = (specific_seq << 1) | 1; |
| 345 | } |
| 346 | |
| 347 | static constexpr bool can_specify_stream = true; |
| 348 | |
| 349 | static constexpr size_t streams_pow2() |
| 350 | { |
| 351 | return (sizeof(itype)*8) - 1u; |
| 352 | } |
| 353 | |
| 354 | protected: |
| 355 | specific_stream() = default; |
| 356 | |
| 357 | specific_stream(itype specific_seq) |
| 358 | : inc_(itype(specific_seq << 1) | itype(1U)) |
| 359 | { |
| 360 | // Nothing (else) to do. |
| 361 | } |
| 362 | }; |
| 363 | |
| 364 | |
| 365 | /* |
| 366 | * This is where it all comes together. This function joins together three |
| 367 | * mixin classes which define |
| 368 | * - the LCG additive constant (the stream) |
| 369 | * - the LCG multiplier |
| 370 | * - the output function |
| 371 | * in addition, we specify the type of the LCG state, and the result type, |
| 372 | * and whether to use the pre-advance version of the state for the output |
| 373 | * (increasing instruction-level parallelism) or the post-advance version |
| 374 | * (reducing register pressure). |
| 375 | * |
| 376 | * Given the high level of parameterization, the code has to use some |
| 377 | * template-metaprogramming tricks to handle some of the suble variations |
| 378 | * involved. |
| 379 | */ |
| 380 | |
| 381 | template <typename xtype, typename itype, |
| 382 | typename output_mixin, |
| 383 | bool output_previous = true, |
| 384 | typename stream_mixin = oneseq_stream<itype>, |
| 385 | typename multiplier_mixin = default_multiplier<itype> > |
| 386 | class engine : protected output_mixin, |
| 387 | public stream_mixin, |
| 388 | protected multiplier_mixin { |
| 389 | protected: |
| 390 | itype state_; |
| 391 | |
| 392 | struct can_specify_stream_tag {}; |
| 393 | struct no_specifiable_stream_tag {}; |
| 394 | |
| 395 | using stream_mixin::increment; |
| 396 | using multiplier_mixin::multiplier; |
| 397 | |
| 398 | public: |
| 399 | typedef xtype result_type; |
| 400 | typedef itype state_type; |
| 401 | |
| 402 | static constexpr size_t period_pow2() |
| 403 | { |
| 404 | return sizeof(state_type)*8 - 2*stream_mixin::is_mcg; |
| 405 | } |
| 406 | |
| 407 | // It would be nice to use std::numeric_limits for these, but |
| 408 | // we can't be sure that it'd be defined for the 128-bit types. |
| 409 | |
| 410 | static constexpr result_type min() |
| 411 | { |
| 412 | return result_type(0UL); |
| 413 | } |
| 414 | |
| 415 | static constexpr result_type max() |
| 416 | { |
| 417 | return result_type(~result_type(0UL)); |
| 418 | } |
| 419 | |
| 420 | protected: |
| 421 | itype bump(itype state) |
| 422 | { |
| 423 | return state * multiplier() + increment(); |
| 424 | } |
| 425 | |
| 426 | itype base_generate() |
| 427 | { |
| 428 | return state_ = bump(state: state_); |
| 429 | } |
| 430 | |
| 431 | itype base_generate0() |
| 432 | { |
| 433 | itype old_state = state_; |
| 434 | state_ = bump(state: state_); |
| 435 | return old_state; |
| 436 | } |
| 437 | |
| 438 | public: |
| 439 | result_type operator()() |
| 440 | { |
| 441 | if (output_previous) |
| 442 | return this->output(base_generate0()); |
| 443 | else |
| 444 | return this->output(base_generate()); |
| 445 | } |
| 446 | |
| 447 | result_type operator()(result_type upper_bound) |
| 448 | { |
| 449 | return bounded_rand(*this, upper_bound); |
| 450 | } |
| 451 | |
| 452 | protected: |
| 453 | static itype advance(itype state, itype delta, |
| 454 | itype cur_mult, itype cur_plus); |
| 455 | |
| 456 | static itype distance(itype cur_state, itype newstate, itype cur_mult, |
| 457 | itype cur_plus, itype mask = ~itype(0U)); |
| 458 | |
| 459 | itype distance(itype newstate, itype mask = itype(~itype(0U))) const |
| 460 | { |
| 461 | return distance(state_, newstate, multiplier(), increment(), mask); |
| 462 | } |
| 463 | |
| 464 | public: |
| 465 | void advance(itype delta) |
| 466 | { |
| 467 | state_ = advance(state_, delta, this->multiplier(), this->increment()); |
| 468 | } |
| 469 | |
| 470 | void backstep(itype delta) |
| 471 | { |
| 472 | advance(-delta); |
| 473 | } |
| 474 | |
| 475 | void discard(itype delta) |
| 476 | { |
| 477 | advance(delta); |
| 478 | } |
| 479 | |
| 480 | bool wrapped() |
| 481 | { |
| 482 | if (stream_mixin::is_mcg) { |
| 483 | // For MCGs, the low order two bits never change. In this |
| 484 | // implementation, we keep them fixed at 3 to make this test |
| 485 | // easier. |
| 486 | return state_ == 3; |
| 487 | } else { |
| 488 | return state_ == 0; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | engine(itype state = itype(0xcafef00dd15ea5e5ULL)) |
| 493 | : state_(this->is_mcg ? state|state_type(3U) |
| 494 | : bump(state: state + this->increment())) |
| 495 | { |
| 496 | // Nothing else to do. |
| 497 | } |
| 498 | |
| 499 | // This function may or may not exist. It thus has to be a template |
| 500 | // to use SFINAE; users don't have to worry about its template-ness. |
| 501 | |
| 502 | template <typename sm = stream_mixin> |
| 503 | engine(itype state, typename sm::stream_state stream_seed) |
| 504 | : stream_mixin(stream_seed), |
| 505 | state_(this->is_mcg ? state|state_type(3U) |
| 506 | : bump(state: state + this->increment())) |
| 507 | { |
| 508 | // Nothing else to do. |
| 509 | } |
| 510 | |
| 511 | template<typename SeedSeq> |
| 512 | engine(SeedSeq&& seedSeq, typename std::enable_if< |
| 513 | !stream_mixin::can_specify_stream |
| 514 | && !std::is_convertible<SeedSeq, itype>::value |
| 515 | && !std::is_convertible<SeedSeq, engine>::value, |
| 516 | no_specifiable_stream_tag>::type = {}) |
| 517 | : engine(generate_one<itype>(std::forward<SeedSeq>(seedSeq))) |
| 518 | { |
| 519 | // Nothing else to do. |
| 520 | } |
| 521 | |
| 522 | template<typename SeedSeq> |
| 523 | engine(SeedSeq&& seedSeq, typename std::enable_if< |
| 524 | stream_mixin::can_specify_stream |
| 525 | && !std::is_convertible<SeedSeq, itype>::value |
| 526 | && !std::is_convertible<SeedSeq, engine>::value, |
| 527 | can_specify_stream_tag>::type = {}) |
| 528 | : engine(generate_one<itype,1,2>(seedSeq), |
| 529 | generate_one<itype,0,2>(seedSeq)) |
| 530 | { |
| 531 | // Nothing else to do. |
| 532 | } |
| 533 | |
| 534 | |
| 535 | template<typename... Args> |
| 536 | void seed(Args&&... args) |
| 537 | { |
| 538 | new (this) engine(std::forward<Args>(args)...); |
| 539 | } |
| 540 | |
| 541 | template <typename xtype1, typename itype1, |
| 542 | typename output_mixin1, bool output_previous1, |
| 543 | typename stream_mixin_lhs, typename multiplier_mixin_lhs, |
| 544 | typename stream_mixin_rhs, typename multiplier_mixin_rhs> |
| 545 | friend bool operator==(const engine<xtype1,itype1, |
| 546 | output_mixin1,output_previous1, |
| 547 | stream_mixin_lhs, multiplier_mixin_lhs>&, |
| 548 | const engine<xtype1,itype1, |
| 549 | output_mixin1,output_previous1, |
| 550 | stream_mixin_rhs, multiplier_mixin_rhs>&); |
| 551 | |
| 552 | template <typename xtype1, typename itype1, |
| 553 | typename output_mixin1, bool output_previous1, |
| 554 | typename stream_mixin_lhs, typename multiplier_mixin_lhs, |
| 555 | typename stream_mixin_rhs, typename multiplier_mixin_rhs> |
| 556 | friend itype1 operator-(const engine<xtype1,itype1, |
| 557 | output_mixin1,output_previous1, |
| 558 | stream_mixin_lhs, multiplier_mixin_lhs>&, |
| 559 | const engine<xtype1,itype1, |
| 560 | output_mixin1,output_previous1, |
| 561 | stream_mixin_rhs, multiplier_mixin_rhs>&); |
| 562 | |
| 563 | template <typename CharT, typename Traits, |
| 564 | typename xtype1, typename itype1, |
| 565 | typename output_mixin1, bool output_previous1, |
| 566 | typename stream_mixin1, typename multiplier_mixin1> |
| 567 | friend std::basic_ostream<CharT,Traits>& |
| 568 | operator<<(std::basic_ostream<CharT,Traits>& out, |
| 569 | const engine<xtype1,itype1, |
| 570 | output_mixin1,output_previous1, |
| 571 | stream_mixin1, multiplier_mixin1>&); |
| 572 | |
| 573 | template <typename CharT, typename Traits, |
| 574 | typename xtype1, typename itype1, |
| 575 | typename output_mixin1, bool output_previous1, |
| 576 | typename stream_mixin1, typename multiplier_mixin1> |
| 577 | friend std::basic_istream<CharT,Traits>& |
| 578 | operator>>(std::basic_istream<CharT,Traits>& in, |
| 579 | engine<xtype1, itype1, |
| 580 | output_mixin1, output_previous1, |
| 581 | stream_mixin1, multiplier_mixin1>& rng); |
| 582 | }; |
| 583 | |
| 584 | template <typename CharT, typename Traits, |
| 585 | typename xtype, typename itype, |
| 586 | typename output_mixin, bool output_previous, |
| 587 | typename stream_mixin, typename multiplier_mixin> |
| 588 | std::basic_ostream<CharT,Traits>& |
| 589 | operator<<(std::basic_ostream<CharT,Traits>& out, |
| 590 | const engine<xtype,itype, |
| 591 | output_mixin,output_previous, |
| 592 | stream_mixin, multiplier_mixin>& rng) |
| 593 | { |
| 594 | using pcg_extras::operator<<; |
| 595 | |
| 596 | auto orig_flags = out.flags(std::ios_base::dec | std::ios_base::left); |
| 597 | auto space = out.widen(' '); |
| 598 | auto orig_fill = out.fill(); |
| 599 | |
| 600 | out << rng.multiplier() << space |
| 601 | << rng.increment() << space |
| 602 | << rng.state_; |
| 603 | |
| 604 | out.flags(orig_flags); |
| 605 | out.fill(orig_fill); |
| 606 | return out; |
| 607 | } |
| 608 | |
| 609 | |
| 610 | template <typename CharT, typename Traits, |
| 611 | typename xtype, typename itype, |
| 612 | typename output_mixin, bool output_previous, |
| 613 | typename stream_mixin, typename multiplier_mixin> |
| 614 | std::basic_istream<CharT,Traits>& |
| 615 | operator>>(std::basic_istream<CharT,Traits>& in, |
| 616 | engine<xtype,itype, |
| 617 | output_mixin,output_previous, |
| 618 | stream_mixin, multiplier_mixin>& rng) |
| 619 | { |
| 620 | using pcg_extras::operator>>; |
| 621 | |
| 622 | auto orig_flags = in.flags(std::ios_base::dec | std::ios_base::skipws); |
| 623 | |
| 624 | itype multiplier, increment, state; |
| 625 | in >> multiplier >> increment >> state; |
| 626 | |
| 627 | if (!in.fail()) { |
| 628 | bool good = true; |
| 629 | if (multiplier != rng.multiplier()) { |
| 630 | good = false; |
| 631 | } else if (rng.can_specify_stream) { |
| 632 | rng.set_stream(increment >> 1); |
| 633 | } else if (increment != rng.increment()) { |
| 634 | good = false; |
| 635 | } |
| 636 | if (good) { |
| 637 | rng.state_ = state; |
| 638 | } else { |
| 639 | in.clear(std::ios::failbit); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | in.flags(orig_flags); |
| 644 | return in; |
| 645 | } |
| 646 | |
| 647 | |
| 648 | template <typename xtype, typename itype, |
| 649 | typename output_mixin, bool output_previous, |
| 650 | typename stream_mixin, typename multiplier_mixin> |
| 651 | itype engine<xtype,itype,output_mixin,output_previous,stream_mixin, |
| 652 | multiplier_mixin>::advance( |
| 653 | itype state, itype delta, itype cur_mult, itype cur_plus) |
| 654 | { |
| 655 | // The method used here is based on Brown, "Random Number Generation |
| 656 | // with Arbitrary Stride,", Transactions of the American Nuclear |
| 657 | // Society (Nov. 1994). The algorithm is very similar to fast |
| 658 | // exponentiation. |
| 659 | // |
| 660 | // Even though delta is an unsigned integer, we can pass a |
| 661 | // signed integer to go backwards, it just goes "the long way round". |
| 662 | |
| 663 | constexpr itype ZERO = 0u; // itype may be a non-trivial types, so |
| 664 | constexpr itype ONE = 1u; // we define some ugly constants. |
| 665 | itype acc_mult = 1; |
| 666 | itype acc_plus = 0; |
| 667 | while (delta > ZERO) { |
| 668 | if (delta & ONE) { |
| 669 | acc_mult *= cur_mult; |
| 670 | acc_plus = acc_plus*cur_mult + cur_plus; |
| 671 | } |
| 672 | cur_plus = (cur_mult+ONE)*cur_plus; |
| 673 | cur_mult *= cur_mult; |
| 674 | delta >>= 1; |
| 675 | } |
| 676 | return acc_mult * state + acc_plus; |
| 677 | } |
| 678 | |
| 679 | template <typename xtype, typename itype, |
| 680 | typename output_mixin, bool output_previous, |
| 681 | typename stream_mixin, typename multiplier_mixin> |
| 682 | itype engine<xtype,itype,output_mixin,output_previous,stream_mixin, |
| 683 | multiplier_mixin>::distance( |
| 684 | itype cur_state, itype newstate, itype cur_mult, itype cur_plus, itype mask) |
| 685 | { |
| 686 | constexpr itype ONE = 1u; // itype could be weird, so use constant |
| 687 | bool is_mcg = cur_plus == itype(0); |
| 688 | itype the_bit = is_mcg ? itype(4u) : itype(1u); |
| 689 | itype distance = 0u; |
| 690 | while ((cur_state & mask) != (newstate & mask)) { |
| 691 | if ((cur_state & the_bit) != (newstate & the_bit)) { |
| 692 | cur_state = cur_state * cur_mult + cur_plus; |
| 693 | distance |= the_bit; |
| 694 | } |
| 695 | assert((cur_state & the_bit) == (newstate & the_bit)); |
| 696 | the_bit <<= 1; |
| 697 | cur_plus = (cur_mult+ONE)*cur_plus; |
| 698 | cur_mult *= cur_mult; |
| 699 | } |
| 700 | return is_mcg ? distance >> 2 : distance; |
| 701 | } |
| 702 | |
| 703 | template <typename xtype, typename itype, |
| 704 | typename output_mixin, bool output_previous, |
| 705 | typename stream_mixin_lhs, typename multiplier_mixin_lhs, |
| 706 | typename stream_mixin_rhs, typename multiplier_mixin_rhs> |
| 707 | itype operator-(const engine<xtype,itype, |
| 708 | output_mixin,output_previous, |
| 709 | stream_mixin_lhs, multiplier_mixin_lhs>& lhs, |
| 710 | const engine<xtype,itype, |
| 711 | output_mixin,output_previous, |
| 712 | stream_mixin_rhs, multiplier_mixin_rhs>& rhs) |
| 713 | { |
| 714 | static_assert( |
| 715 | std::is_same<stream_mixin_lhs, stream_mixin_rhs>::value && |
| 716 | std::is_same<multiplier_mixin_lhs, multiplier_mixin_rhs>::value, |
| 717 | "Incomparable generators" ); |
| 718 | if (lhs.increment() == rhs.increment()) { |
| 719 | return rhs.distance(lhs.state_); |
| 720 | } else { |
| 721 | constexpr itype ONE = 1u; |
| 722 | itype lhs_diff = lhs.increment() + (lhs.multiplier()-ONE) * lhs.state_; |
| 723 | itype rhs_diff = rhs.increment() + (rhs.multiplier()-ONE) * rhs.state_; |
| 724 | if ((lhs_diff & itype(3u)) != (rhs_diff & itype(3u))) { |
| 725 | rhs_diff = -rhs_diff; |
| 726 | } |
| 727 | return rhs.distance(rhs_diff, lhs_diff, rhs.multiplier(), itype(0u)); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | |
| 732 | template <typename xtype, typename itype, |
| 733 | typename output_mixin, bool output_previous, |
| 734 | typename stream_mixin_lhs, typename multiplier_mixin_lhs, |
| 735 | typename stream_mixin_rhs, typename multiplier_mixin_rhs> |
| 736 | bool operator==(const engine<xtype,itype, |
| 737 | output_mixin,output_previous, |
| 738 | stream_mixin_lhs, multiplier_mixin_lhs>& lhs, |
| 739 | const engine<xtype,itype, |
| 740 | output_mixin,output_previous, |
| 741 | stream_mixin_rhs, multiplier_mixin_rhs>& rhs) |
| 742 | { |
| 743 | return (lhs.multiplier() == rhs.multiplier()) |
| 744 | && (lhs.increment() == rhs.increment()) |
| 745 | && (lhs.state_ == rhs.state_); |
| 746 | } |
| 747 | |
| 748 | template <typename xtype, typename itype, |
| 749 | typename output_mixin, bool output_previous, |
| 750 | typename stream_mixin_lhs, typename multiplier_mixin_lhs, |
| 751 | typename stream_mixin_rhs, typename multiplier_mixin_rhs> |
| 752 | inline bool operator!=(const engine<xtype,itype, |
| 753 | output_mixin,output_previous, |
| 754 | stream_mixin_lhs, multiplier_mixin_lhs>& lhs, |
| 755 | const engine<xtype,itype, |
| 756 | output_mixin,output_previous, |
| 757 | stream_mixin_rhs, multiplier_mixin_rhs>& rhs) |
| 758 | { |
| 759 | return !operator==(lhs,rhs); |
| 760 | } |
| 761 | |
| 762 | |
| 763 | template <typename xtype, typename itype, |
| 764 | template<typename XT,typename IT> class output_mixin, |
| 765 | bool output_previous = (sizeof(itype) <= 8), |
| 766 | template<typename IT> class multiplier_mixin = default_multiplier> |
| 767 | using oneseq_base = engine<xtype, itype, |
| 768 | output_mixin<xtype, itype>, output_previous, |
| 769 | oneseq_stream<itype>, |
| 770 | multiplier_mixin<itype> >; |
| 771 | |
| 772 | template <typename xtype, typename itype, |
| 773 | template<typename XT,typename IT> class output_mixin, |
| 774 | bool output_previous = (sizeof(itype) <= 8), |
| 775 | template<typename IT> class multiplier_mixin = default_multiplier> |
| 776 | using unique_base = engine<xtype, itype, |
| 777 | output_mixin<xtype, itype>, output_previous, |
| 778 | unique_stream<itype>, |
| 779 | multiplier_mixin<itype> >; |
| 780 | |
| 781 | template <typename xtype, typename itype, |
| 782 | template<typename XT,typename IT> class output_mixin, |
| 783 | bool output_previous = (sizeof(itype) <= 8), |
| 784 | template<typename IT> class multiplier_mixin = default_multiplier> |
| 785 | using setseq_base = engine<xtype, itype, |
| 786 | output_mixin<xtype, itype>, output_previous, |
| 787 | specific_stream<itype>, |
| 788 | multiplier_mixin<itype> >; |
| 789 | |
| 790 | template <typename xtype, typename itype, |
| 791 | template<typename XT,typename IT> class output_mixin, |
| 792 | bool output_previous = (sizeof(itype) <= 8), |
| 793 | template<typename IT> class multiplier_mixin = default_multiplier> |
| 794 | using mcg_base = engine<xtype, itype, |
| 795 | output_mixin<xtype, itype>, output_previous, |
| 796 | no_stream<itype>, |
| 797 | multiplier_mixin<itype> >; |
| 798 | |
| 799 | /* |
| 800 | * OUTPUT FUNCTIONS. |
| 801 | * |
| 802 | * These are the core of the PCG generation scheme. They specify how to |
| 803 | * turn the base LCG's internal state into the output value of the final |
| 804 | * generator. |
| 805 | * |
| 806 | * They're implemented as mixin classes. |
| 807 | * |
| 808 | * All of the classes have code that is written to allow it to be applied |
| 809 | * at *arbitrary* bit sizes, although in practice they'll only be used at |
| 810 | * standard sizes supported by C++. |
| 811 | */ |
| 812 | |
| 813 | /* |
| 814 | * XSH RS -- high xorshift, followed by a random shift |
| 815 | * |
| 816 | * Fast. A good performer. |
| 817 | */ |
| 818 | |
| 819 | template <typename xtype, typename itype> |
| 820 | struct xsh_rs_mixin { |
| 821 | static xtype output(itype internal) |
| 822 | { |
| 823 | constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8); |
| 824 | constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8); |
| 825 | constexpr bitcount_t sparebits = bits - xtypebits; |
| 826 | constexpr bitcount_t opbits = |
| 827 | sparebits-5 >= 64 ? 5 |
| 828 | : sparebits-4 >= 32 ? 4 |
| 829 | : sparebits-3 >= 16 ? 3 |
| 830 | : sparebits-2 >= 4 ? 2 |
| 831 | : sparebits-1 >= 1 ? 1 |
| 832 | : 0; |
| 833 | constexpr bitcount_t mask = (1 << opbits) - 1; |
| 834 | constexpr bitcount_t maxrandshift = mask; |
| 835 | constexpr bitcount_t topspare = opbits; |
| 836 | constexpr bitcount_t bottomspare = sparebits - topspare; |
| 837 | constexpr bitcount_t xshift = topspare + (xtypebits+maxrandshift)/2; |
| 838 | bitcount_t rshift = |
| 839 | opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0; |
| 840 | internal ^= internal >> xshift; |
| 841 | xtype result = xtype(internal >> (bottomspare - maxrandshift + rshift)); |
| 842 | return result; |
| 843 | } |
| 844 | }; |
| 845 | |
| 846 | /* |
| 847 | * XSH RR -- high xorshift, followed by a random rotate |
| 848 | * |
| 849 | * Fast. A good performer. Slightly better statistically than XSH RS. |
| 850 | */ |
| 851 | |
| 852 | template <typename xtype, typename itype> |
| 853 | struct xsh_rr_mixin { |
| 854 | static xtype output(itype internal) |
| 855 | { |
| 856 | constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8); |
| 857 | constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype)*8); |
| 858 | constexpr bitcount_t sparebits = bits - xtypebits; |
| 859 | constexpr bitcount_t wantedopbits = |
| 860 | xtypebits >= 128 ? 7 |
| 861 | : xtypebits >= 64 ? 6 |
| 862 | : xtypebits >= 32 ? 5 |
| 863 | : xtypebits >= 16 ? 4 |
| 864 | : 3; |
| 865 | constexpr bitcount_t opbits = |
| 866 | sparebits >= wantedopbits ? wantedopbits |
| 867 | : sparebits; |
| 868 | constexpr bitcount_t amplifier = wantedopbits - opbits; |
| 869 | constexpr bitcount_t mask = (1 << opbits) - 1; |
| 870 | constexpr bitcount_t topspare = opbits; |
| 871 | constexpr bitcount_t bottomspare = sparebits - topspare; |
| 872 | constexpr bitcount_t xshift = (topspare + xtypebits)/2; |
| 873 | bitcount_t rot = opbits ? bitcount_t(internal >> (bits - opbits)) & mask |
| 874 | : 0; |
| 875 | bitcount_t amprot = (rot << amplifier) & mask; |
| 876 | internal ^= internal >> xshift; |
| 877 | xtype result = xtype(internal >> bottomspare); |
| 878 | result = rotr(result, amprot); |
| 879 | return result; |
| 880 | } |
| 881 | }; |
| 882 | |
| 883 | /* |
| 884 | * RXS -- random xorshift |
| 885 | */ |
| 886 | |
| 887 | template <typename xtype, typename itype> |
| 888 | struct rxs_mixin { |
| 889 | static xtype output_rxs(itype internal) |
| 890 | { |
| 891 | constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8); |
| 892 | constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype)*8); |
| 893 | constexpr bitcount_t shift = bits - xtypebits; |
| 894 | constexpr bitcount_t = (xtypebits - shift)/2; |
| 895 | bitcount_t rshift = shift > 64+8 ? (internal >> (bits - 6)) & 63 |
| 896 | : shift > 32+4 ? (internal >> (bits - 5)) & 31 |
| 897 | : shift > 16+2 ? (internal >> (bits - 4)) & 15 |
| 898 | : shift > 8+1 ? (internal >> (bits - 3)) & 7 |
| 899 | : shift > 4+1 ? (internal >> (bits - 2)) & 3 |
| 900 | : shift > 2+1 ? (internal >> (bits - 1)) & 1 |
| 901 | : 0; |
| 902 | internal ^= internal >> (shift + extrashift - rshift); |
| 903 | xtype result = internal >> rshift; |
| 904 | return result; |
| 905 | } |
| 906 | }; |
| 907 | |
| 908 | /* |
| 909 | * RXS M XS -- random xorshift, mcg multiply, fixed xorshift |
| 910 | * |
| 911 | * The most statistically powerful generator, but all those steps |
| 912 | * make it slower than some of the others. We give it the rottenest jobs. |
| 913 | * |
| 914 | * Because it's usually used in contexts where the state type and the |
| 915 | * result type are the same, it is a permutation and is thus invertable. |
| 916 | * We thus provide a function to invert it. This function is used to |
| 917 | * for the "inside out" generator used by the extended generator. |
| 918 | */ |
| 919 | |
| 920 | /* Defined type-based concepts for the multiplication step. They're actually |
| 921 | * all derived by truncating the 128-bit, which was computed to be a good |
| 922 | * "universal" constant. |
| 923 | */ |
| 924 | |
| 925 | template <typename T> |
| 926 | struct mcg_multiplier { |
| 927 | // Not defined for an arbitrary type |
| 928 | }; |
| 929 | |
| 930 | template <typename T> |
| 931 | struct mcg_unmultiplier { |
| 932 | // Not defined for an arbitrary type |
| 933 | }; |
| 934 | |
| 935 | PCG_DEFINE_CONSTANT(uint8_t, mcg, multiplier, 217U) |
| 936 | PCG_DEFINE_CONSTANT(uint8_t, mcg, unmultiplier, 105U) |
| 937 | |
| 938 | PCG_DEFINE_CONSTANT(uint16_t, mcg, multiplier, 62169U) |
| 939 | PCG_DEFINE_CONSTANT(uint16_t, mcg, unmultiplier, 28009U) |
| 940 | |
| 941 | PCG_DEFINE_CONSTANT(uint32_t, mcg, multiplier, 277803737U) |
| 942 | PCG_DEFINE_CONSTANT(uint32_t, mcg, unmultiplier, 2897767785U) |
| 943 | |
| 944 | PCG_DEFINE_CONSTANT(uint64_t, mcg, multiplier, 12605985483714917081ULL) |
| 945 | PCG_DEFINE_CONSTANT(uint64_t, mcg, unmultiplier, 15009553638781119849ULL) |
| 946 | |
| 947 | PCG_DEFINE_CONSTANT(pcg128_t, mcg, multiplier, |
| 948 | PCG_128BIT_CONSTANT(17766728186571221404ULL, 12605985483714917081ULL)) |
| 949 | PCG_DEFINE_CONSTANT(pcg128_t, mcg, unmultiplier, |
| 950 | PCG_128BIT_CONSTANT(14422606686972528997ULL, 15009553638781119849ULL)) |
| 951 | |
| 952 | |
| 953 | template <typename xtype, typename itype> |
| 954 | struct rxs_m_xs_mixin { |
| 955 | static xtype output(itype internal) |
| 956 | { |
| 957 | constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8); |
| 958 | constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8); |
| 959 | constexpr bitcount_t opbits = xtypebits >= 128 ? 6 |
| 960 | : xtypebits >= 64 ? 5 |
| 961 | : xtypebits >= 32 ? 4 |
| 962 | : xtypebits >= 16 ? 3 |
| 963 | : 2; |
| 964 | constexpr bitcount_t shift = bits - xtypebits; |
| 965 | constexpr bitcount_t mask = (1 << opbits) - 1; |
| 966 | bitcount_t rshift = |
| 967 | opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0; |
| 968 | internal ^= internal >> (opbits + rshift); |
| 969 | internal *= mcg_multiplier<itype>::multiplier(); |
| 970 | xtype result = internal >> shift; |
| 971 | result ^= result >> ((2U*xtypebits+2U)/3U); |
| 972 | return result; |
| 973 | } |
| 974 | |
| 975 | static itype unoutput(itype internal) |
| 976 | { |
| 977 | constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8); |
| 978 | constexpr bitcount_t opbits = bits >= 128 ? 6 |
| 979 | : bits >= 64 ? 5 |
| 980 | : bits >= 32 ? 4 |
| 981 | : bits >= 16 ? 3 |
| 982 | : 2; |
| 983 | constexpr bitcount_t mask = (1 << opbits) - 1; |
| 984 | |
| 985 | internal = unxorshift(internal, bits, (2U*bits+2U)/3U); |
| 986 | |
| 987 | internal *= mcg_unmultiplier<itype>::unmultiplier(); |
| 988 | |
| 989 | bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0; |
| 990 | internal = unxorshift(internal, bits, opbits + rshift); |
| 991 | |
| 992 | return internal; |
| 993 | } |
| 994 | }; |
| 995 | |
| 996 | |
| 997 | /* |
| 998 | * RXS M -- random xorshift, mcg multiply |
| 999 | */ |
| 1000 | |
| 1001 | template <typename xtype, typename itype> |
| 1002 | struct rxs_m_mixin { |
| 1003 | static xtype output(itype internal) |
| 1004 | { |
| 1005 | constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8); |
| 1006 | constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8); |
| 1007 | constexpr bitcount_t opbits = xtypebits >= 128 ? 6 |
| 1008 | : xtypebits >= 64 ? 5 |
| 1009 | : xtypebits >= 32 ? 4 |
| 1010 | : xtypebits >= 16 ? 3 |
| 1011 | : 2; |
| 1012 | constexpr bitcount_t shift = bits - xtypebits; |
| 1013 | constexpr bitcount_t mask = (1 << opbits) - 1; |
| 1014 | bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0; |
| 1015 | internal ^= internal >> (opbits + rshift); |
| 1016 | internal *= mcg_multiplier<itype>::multiplier(); |
| 1017 | xtype result = internal >> shift; |
| 1018 | return result; |
| 1019 | } |
| 1020 | }; |
| 1021 | |
| 1022 | |
| 1023 | /* |
| 1024 | * DXSM -- double xorshift multiply |
| 1025 | * |
| 1026 | * This is a new, more powerful output permutation (added in 2019). It's |
| 1027 | * a more comprehensive scrambling than RXS M, but runs faster on 128-bit |
| 1028 | * types. Although primarily intended for use at large sizes, also works |
| 1029 | * at smaller sizes as well. |
| 1030 | * |
| 1031 | * This permutation is similar to xorshift multiply hash functions, except |
| 1032 | * that one of the multipliers is the LCG multiplier (to avoid needing to |
| 1033 | * have a second constant) and the other is based on the low-order bits. |
| 1034 | * This latter aspect means that the scrambling applied to the high bits |
| 1035 | * depends on the low bits, and makes it (to my eye) impractical to back |
| 1036 | * out the permutation without having the low-order bits. |
| 1037 | */ |
| 1038 | |
| 1039 | template <typename xtype, typename itype> |
| 1040 | struct dxsm_mixin { |
| 1041 | inline xtype output(itype internal) |
| 1042 | { |
| 1043 | constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8); |
| 1044 | constexpr bitcount_t itypebits = bitcount_t(sizeof(itype) * 8); |
| 1045 | static_assert(xtypebits <= itypebits/2, |
| 1046 | "Output type must be half the size of the state type." ); |
| 1047 | |
| 1048 | xtype hi = xtype(internal >> (itypebits - xtypebits)); |
| 1049 | xtype lo = xtype(internal); |
| 1050 | |
| 1051 | lo |= 1; |
| 1052 | hi ^= hi >> (xtypebits/2); |
| 1053 | hi *= xtype(cheap_multiplier<itype>::multiplier()); |
| 1054 | hi ^= hi >> (3*(xtypebits/4)); |
| 1055 | hi *= lo; |
| 1056 | return hi; |
| 1057 | } |
| 1058 | }; |
| 1059 | |
| 1060 | |
| 1061 | /* |
| 1062 | * XSL RR -- fixed xorshift (to low bits), random rotate |
| 1063 | * |
| 1064 | * Useful for 128-bit types that are split across two CPU registers. |
| 1065 | */ |
| 1066 | |
| 1067 | template <typename xtype, typename itype> |
| 1068 | struct xsl_rr_mixin { |
| 1069 | static xtype output(itype internal) |
| 1070 | { |
| 1071 | constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8); |
| 1072 | constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8); |
| 1073 | constexpr bitcount_t sparebits = bits - xtypebits; |
| 1074 | constexpr bitcount_t wantedopbits = xtypebits >= 128 ? 7 |
| 1075 | : xtypebits >= 64 ? 6 |
| 1076 | : xtypebits >= 32 ? 5 |
| 1077 | : xtypebits >= 16 ? 4 |
| 1078 | : 3; |
| 1079 | constexpr bitcount_t opbits = sparebits >= wantedopbits ? wantedopbits |
| 1080 | : sparebits; |
| 1081 | constexpr bitcount_t amplifier = wantedopbits - opbits; |
| 1082 | constexpr bitcount_t mask = (1 << opbits) - 1; |
| 1083 | constexpr bitcount_t topspare = sparebits; |
| 1084 | constexpr bitcount_t bottomspare = sparebits - topspare; |
| 1085 | constexpr bitcount_t xshift = (topspare + xtypebits) / 2; |
| 1086 | |
| 1087 | bitcount_t rot = |
| 1088 | opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0; |
| 1089 | bitcount_t amprot = (rot << amplifier) & mask; |
| 1090 | internal ^= internal >> xshift; |
| 1091 | xtype result = xtype(internal >> bottomspare); |
| 1092 | result = rotr(result, amprot); |
| 1093 | return result; |
| 1094 | } |
| 1095 | }; |
| 1096 | |
| 1097 | |
| 1098 | /* |
| 1099 | * XSL RR RR -- fixed xorshift (to low bits), random rotate (both parts) |
| 1100 | * |
| 1101 | * Useful for 128-bit types that are split across two CPU registers. |
| 1102 | * If you really want an invertable 128-bit RNG, I guess this is the one. |
| 1103 | */ |
| 1104 | |
| 1105 | template <typename T> struct halfsize_trait {}; |
| 1106 | template <> struct halfsize_trait<pcg128_t> { typedef uint64_t type; }; |
| 1107 | template <> struct halfsize_trait<uint64_t> { typedef uint32_t type; }; |
| 1108 | template <> struct halfsize_trait<uint32_t> { typedef uint16_t type; }; |
| 1109 | template <> struct halfsize_trait<uint16_t> { typedef uint8_t type; }; |
| 1110 | |
| 1111 | template <typename xtype, typename itype> |
| 1112 | struct xsl_rr_rr_mixin { |
| 1113 | typedef typename halfsize_trait<itype>::type htype; |
| 1114 | |
| 1115 | static itype output(itype internal) |
| 1116 | { |
| 1117 | constexpr bitcount_t htypebits = bitcount_t(sizeof(htype) * 8); |
| 1118 | constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8); |
| 1119 | constexpr bitcount_t sparebits = bits - htypebits; |
| 1120 | constexpr bitcount_t wantedopbits = htypebits >= 128 ? 7 |
| 1121 | : htypebits >= 64 ? 6 |
| 1122 | : htypebits >= 32 ? 5 |
| 1123 | : htypebits >= 16 ? 4 |
| 1124 | : 3; |
| 1125 | constexpr bitcount_t opbits = sparebits >= wantedopbits ? wantedopbits |
| 1126 | : sparebits; |
| 1127 | constexpr bitcount_t amplifier = wantedopbits - opbits; |
| 1128 | constexpr bitcount_t mask = (1 << opbits) - 1; |
| 1129 | constexpr bitcount_t topspare = sparebits; |
| 1130 | constexpr bitcount_t xshift = (topspare + htypebits) / 2; |
| 1131 | |
| 1132 | bitcount_t rot = |
| 1133 | opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0; |
| 1134 | bitcount_t amprot = (rot << amplifier) & mask; |
| 1135 | internal ^= internal >> xshift; |
| 1136 | htype lowbits = htype(internal); |
| 1137 | lowbits = rotr(lowbits, amprot); |
| 1138 | htype highbits = htype(internal >> topspare); |
| 1139 | bitcount_t rot2 = lowbits & mask; |
| 1140 | bitcount_t amprot2 = (rot2 << amplifier) & mask; |
| 1141 | highbits = rotr(highbits, amprot2); |
| 1142 | return (itype(highbits) << topspare) ^ itype(lowbits); |
| 1143 | } |
| 1144 | }; |
| 1145 | |
| 1146 | |
| 1147 | /* |
| 1148 | * XSH -- fixed xorshift (to high bits) |
| 1149 | * |
| 1150 | * You shouldn't use this at 64-bits or less. |
| 1151 | */ |
| 1152 | |
| 1153 | template <typename xtype, typename itype> |
| 1154 | struct xsh_mixin { |
| 1155 | static xtype output(itype internal) |
| 1156 | { |
| 1157 | constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8); |
| 1158 | constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8); |
| 1159 | constexpr bitcount_t sparebits = bits - xtypebits; |
| 1160 | constexpr bitcount_t topspare = 0; |
| 1161 | constexpr bitcount_t bottomspare = sparebits - topspare; |
| 1162 | constexpr bitcount_t xshift = (topspare + xtypebits) / 2; |
| 1163 | |
| 1164 | internal ^= internal >> xshift; |
| 1165 | xtype result = internal >> bottomspare; |
| 1166 | return result; |
| 1167 | } |
| 1168 | }; |
| 1169 | |
| 1170 | /* |
| 1171 | * XSL -- fixed xorshift (to low bits) |
| 1172 | * |
| 1173 | * You shouldn't use this at 64-bits or less. |
| 1174 | */ |
| 1175 | |
| 1176 | template <typename xtype, typename itype> |
| 1177 | struct xsl_mixin { |
| 1178 | inline xtype output(itype internal) |
| 1179 | { |
| 1180 | constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8); |
| 1181 | constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8); |
| 1182 | constexpr bitcount_t sparebits = bits - xtypebits; |
| 1183 | constexpr bitcount_t topspare = sparebits; |
| 1184 | constexpr bitcount_t bottomspare = sparebits - topspare; |
| 1185 | constexpr bitcount_t xshift = (topspare + xtypebits) / 2; |
| 1186 | |
| 1187 | internal ^= internal >> xshift; |
| 1188 | xtype result = internal >> bottomspare; |
| 1189 | return result; |
| 1190 | } |
| 1191 | }; |
| 1192 | |
| 1193 | |
| 1194 | /* ---- End of Output Functions ---- */ |
| 1195 | |
| 1196 | |
| 1197 | template <typename baseclass> |
| 1198 | struct inside_out : private baseclass { |
| 1199 | inside_out() = delete; |
| 1200 | |
| 1201 | typedef typename baseclass::result_type result_type; |
| 1202 | typedef typename baseclass::state_type state_type; |
| 1203 | static_assert(sizeof(result_type) == sizeof(state_type), |
| 1204 | "Require a RNG whose output function is a permutation" ); |
| 1205 | |
| 1206 | static bool external_step(result_type& randval, size_t i) |
| 1207 | { |
| 1208 | state_type state = baseclass::unoutput(randval); |
| 1209 | state = state * baseclass::multiplier() + baseclass::increment() |
| 1210 | + state_type(i*2); |
| 1211 | result_type result = baseclass::output(state); |
| 1212 | randval = result; |
| 1213 | state_type zero = |
| 1214 | baseclass::is_mcg ? state & state_type(3U) : state_type(0U); |
| 1215 | return result == zero; |
| 1216 | } |
| 1217 | |
| 1218 | static bool external_advance(result_type& randval, size_t i, |
| 1219 | result_type delta, bool forwards = true) |
| 1220 | { |
| 1221 | state_type state = baseclass::unoutput(randval); |
| 1222 | state_type mult = baseclass::multiplier(); |
| 1223 | state_type inc = baseclass::increment() + state_type(i*2); |
| 1224 | state_type zero = |
| 1225 | baseclass::is_mcg ? state & state_type(3U) : state_type(0U); |
| 1226 | state_type dist_to_zero = baseclass::distance(state, zero, mult, inc); |
| 1227 | bool crosses_zero = |
| 1228 | forwards ? dist_to_zero <= delta |
| 1229 | : (-dist_to_zero) <= delta; |
| 1230 | if (!forwards) |
| 1231 | delta = -delta; |
| 1232 | state = baseclass::advance(state, delta, mult, inc); |
| 1233 | randval = baseclass::output(state); |
| 1234 | return crosses_zero; |
| 1235 | } |
| 1236 | }; |
| 1237 | |
| 1238 | |
| 1239 | template <bitcount_t table_pow2, bitcount_t advance_pow2, typename baseclass, typename extvalclass, bool kdd = true> |
| 1240 | class pcg_extended : public baseclass { |
| 1241 | public: |
| 1242 | typedef typename baseclass::state_type state_type; |
| 1243 | typedef typename baseclass::result_type result_type; |
| 1244 | typedef inside_out<extvalclass> insideout; |
| 1245 | |
| 1246 | private: |
| 1247 | static constexpr bitcount_t rtypebits = sizeof(result_type)*8; |
| 1248 | static constexpr bitcount_t stypebits = sizeof(state_type)*8; |
| 1249 | |
| 1250 | static constexpr bitcount_t tick_limit_pow2 = 64U; |
| 1251 | |
| 1252 | static constexpr size_t table_size = 1UL << table_pow2; |
| 1253 | static constexpr size_t table_shift = stypebits - table_pow2; |
| 1254 | static constexpr state_type table_mask = |
| 1255 | (state_type(1U) << table_pow2) - state_type(1U); |
| 1256 | |
| 1257 | static constexpr bool may_tick = |
| 1258 | (advance_pow2 < stypebits) && (advance_pow2 < tick_limit_pow2); |
| 1259 | static constexpr size_t tick_shift = stypebits - advance_pow2; |
| 1260 | static constexpr state_type tick_mask = |
| 1261 | may_tick ? state_type( |
| 1262 | (uint64_t(1) << (advance_pow2*may_tick)) - 1) |
| 1263 | // ^-- stupidity to appease GCC warnings |
| 1264 | : ~state_type(0U); |
| 1265 | |
| 1266 | static constexpr bool may_tock = stypebits < tick_limit_pow2; |
| 1267 | |
| 1268 | result_type data_[table_size]; |
| 1269 | |
| 1270 | PCG_NOINLINE void advance_table(); |
| 1271 | |
| 1272 | PCG_NOINLINE void advance_table(state_type delta, bool isForwards = true); |
| 1273 | |
| 1274 | result_type& get_extended_value() |
| 1275 | { |
| 1276 | state_type state = this->state_; |
| 1277 | if (kdd && baseclass::is_mcg) { |
| 1278 | // The low order bits of an MCG are constant, so drop them. |
| 1279 | state >>= 2; |
| 1280 | } |
| 1281 | size_t index = kdd ? state & table_mask |
| 1282 | : state >> table_shift; |
| 1283 | |
| 1284 | if (may_tick) { |
| 1285 | bool tick = kdd ? (state & tick_mask) == state_type(0u) |
| 1286 | : (state >> tick_shift) == state_type(0u); |
| 1287 | if (tick) |
| 1288 | advance_table(); |
| 1289 | } |
| 1290 | if (may_tock) { |
| 1291 | bool tock = state == state_type(0u); |
| 1292 | if (tock) |
| 1293 | advance_table(); |
| 1294 | } |
| 1295 | return data_[index]; |
| 1296 | } |
| 1297 | |
| 1298 | public: |
| 1299 | static constexpr size_t period_pow2() |
| 1300 | { |
| 1301 | return baseclass::period_pow2() + table_size*extvalclass::period_pow2(); |
| 1302 | } |
| 1303 | |
| 1304 | PCG_ALWAYS_INLINE result_type operator()() |
| 1305 | { |
| 1306 | result_type rhs = get_extended_value(); |
| 1307 | result_type lhs = this->baseclass::operator()(); |
| 1308 | return lhs ^ rhs; |
| 1309 | } |
| 1310 | |
| 1311 | result_type operator()(result_type upper_bound) |
| 1312 | { |
| 1313 | return bounded_rand(*this, upper_bound); |
| 1314 | } |
| 1315 | |
| 1316 | void set(result_type wanted) |
| 1317 | { |
| 1318 | result_type& rhs = get_extended_value(); |
| 1319 | result_type lhs = this->baseclass::operator()(); |
| 1320 | rhs = lhs ^ wanted; |
| 1321 | } |
| 1322 | |
| 1323 | void advance(state_type distance, bool forwards = true); |
| 1324 | |
| 1325 | void backstep(state_type distance) |
| 1326 | { |
| 1327 | advance(distance, forwards: false); |
| 1328 | } |
| 1329 | |
| 1330 | pcg_extended(const result_type* data) |
| 1331 | : baseclass() |
| 1332 | { |
| 1333 | datainit(data); |
| 1334 | } |
| 1335 | |
| 1336 | pcg_extended(const result_type* data, state_type seed) |
| 1337 | : baseclass(seed) |
| 1338 | { |
| 1339 | datainit(data); |
| 1340 | } |
| 1341 | |
| 1342 | // This function may or may not exist. It thus has to be a template |
| 1343 | // to use SFINAE; users don't have to worry about its template-ness. |
| 1344 | |
| 1345 | template <typename bc = baseclass> |
| 1346 | pcg_extended(const result_type* data, state_type seed, |
| 1347 | typename bc::stream_state stream_seed) |
| 1348 | : baseclass(seed, stream_seed) |
| 1349 | { |
| 1350 | datainit(data); |
| 1351 | } |
| 1352 | |
| 1353 | pcg_extended() |
| 1354 | : baseclass() |
| 1355 | { |
| 1356 | selfinit(); |
| 1357 | } |
| 1358 | |
| 1359 | pcg_extended(state_type seed) |
| 1360 | : baseclass(seed) |
| 1361 | { |
| 1362 | selfinit(); |
| 1363 | } |
| 1364 | |
| 1365 | // This function may or may not exist. It thus has to be a template |
| 1366 | // to use SFINAE; users don't have to worry about its template-ness. |
| 1367 | |
| 1368 | template <typename bc = baseclass> |
| 1369 | pcg_extended(state_type seed, typename bc::stream_state stream_seed) |
| 1370 | : baseclass(seed, stream_seed) |
| 1371 | { |
| 1372 | selfinit(); |
| 1373 | } |
| 1374 | |
| 1375 | private: |
| 1376 | void selfinit(); |
| 1377 | void datainit(const result_type* data); |
| 1378 | |
| 1379 | public: |
| 1380 | |
| 1381 | template<typename SeedSeq, typename = typename std::enable_if< |
| 1382 | !std::is_convertible<SeedSeq, result_type>::value |
| 1383 | && !std::is_convertible<SeedSeq, pcg_extended>::value>::type> |
| 1384 | pcg_extended(SeedSeq&& seedSeq) |
| 1385 | : baseclass(seedSeq) |
| 1386 | { |
| 1387 | generate_to<table_size>(seedSeq, data_); |
| 1388 | } |
| 1389 | |
| 1390 | template<typename... Args> |
| 1391 | void seed(Args&&... args) |
| 1392 | { |
| 1393 | new (this) pcg_extended(std::forward<Args>(args)...); |
| 1394 | } |
| 1395 | |
| 1396 | template <bitcount_t table_pow2_, bitcount_t advance_pow2_, |
| 1397 | typename baseclass_, typename extvalclass_, bool kdd_> |
| 1398 | friend bool operator==(const pcg_extended<table_pow2_, advance_pow2_, |
| 1399 | baseclass_, extvalclass_, kdd_>&, |
| 1400 | const pcg_extended<table_pow2_, advance_pow2_, |
| 1401 | baseclass_, extvalclass_, kdd_>&); |
| 1402 | |
| 1403 | template <typename CharT, typename Traits, |
| 1404 | bitcount_t table_pow2_, bitcount_t advance_pow2_, |
| 1405 | typename baseclass_, typename extvalclass_, bool kdd_> |
| 1406 | friend std::basic_ostream<CharT,Traits>& |
| 1407 | operator<<(std::basic_ostream<CharT,Traits>& out, |
| 1408 | const pcg_extended<table_pow2_, advance_pow2_, |
| 1409 | baseclass_, extvalclass_, kdd_>&); |
| 1410 | |
| 1411 | template <typename CharT, typename Traits, |
| 1412 | bitcount_t table_pow2_, bitcount_t advance_pow2_, |
| 1413 | typename baseclass_, typename extvalclass_, bool kdd_> |
| 1414 | friend std::basic_istream<CharT,Traits>& |
| 1415 | operator>>(std::basic_istream<CharT,Traits>& in, |
| 1416 | pcg_extended<table_pow2_, advance_pow2_, |
| 1417 | baseclass_, extvalclass_, kdd_>&); |
| 1418 | |
| 1419 | }; |
| 1420 | |
| 1421 | |
| 1422 | template <bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1423 | typename baseclass, typename extvalclass, bool kdd> |
| 1424 | void pcg_extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::datainit( |
| 1425 | const result_type* data) |
| 1426 | { |
| 1427 | for (size_t i = 0; i < table_size; ++i) |
| 1428 | data_[i] = data[i]; |
| 1429 | } |
| 1430 | |
| 1431 | template <bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1432 | typename baseclass, typename extvalclass, bool kdd> |
| 1433 | void pcg_extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::selfinit() |
| 1434 | { |
| 1435 | // We need to fill the extended table with something, and we have |
| 1436 | // very little provided data, so we use the base generator to |
| 1437 | // produce values. Although not ideal (use a seed sequence, folks!), |
| 1438 | // unexpected correlations are mitigated by |
| 1439 | // - using XOR differences rather than the number directly |
| 1440 | // - the way the table is accessed, its values *won't* be accessed |
| 1441 | // in the same order the were written. |
| 1442 | // - any strange correlations would only be apparent if we |
| 1443 | // were to backstep the generator so that the base generator |
| 1444 | // was generating the same values again |
| 1445 | result_type lhs = baseclass::operator()(); |
| 1446 | result_type rhs = baseclass::operator()(); |
| 1447 | result_type xdiff = lhs - rhs; |
| 1448 | for (size_t i = 0; i < table_size; ++i) { |
| 1449 | data_[i] = baseclass::operator()() ^ xdiff; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | template <bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1454 | typename baseclass, typename extvalclass, bool kdd> |
| 1455 | bool operator==(const pcg_extended<table_pow2, advance_pow2, |
| 1456 | baseclass, extvalclass, kdd>& lhs, |
| 1457 | const pcg_extended<table_pow2, advance_pow2, |
| 1458 | baseclass, extvalclass, kdd>& rhs) |
| 1459 | { |
| 1460 | auto& base_lhs = static_cast<const baseclass&>(lhs); |
| 1461 | auto& base_rhs = static_cast<const baseclass&>(rhs); |
| 1462 | return base_lhs == base_rhs |
| 1463 | && std::equal( |
| 1464 | std::begin(lhs.data_), std::end(lhs.data_), |
| 1465 | std::begin(rhs.data_) |
| 1466 | ); |
| 1467 | } |
| 1468 | |
| 1469 | template <bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1470 | typename baseclass, typename extvalclass, bool kdd> |
| 1471 | inline bool operator!=(const pcg_extended<table_pow2, advance_pow2, |
| 1472 | baseclass, extvalclass, kdd>& lhs, |
| 1473 | const pcg_extended<table_pow2, advance_pow2, |
| 1474 | baseclass, extvalclass, kdd>& rhs) |
| 1475 | { |
| 1476 | return !operator==(lhs, rhs); |
| 1477 | } |
| 1478 | |
| 1479 | template <typename CharT, typename Traits, |
| 1480 | bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1481 | typename baseclass, typename extvalclass, bool kdd> |
| 1482 | std::basic_ostream<CharT,Traits>& |
| 1483 | operator<<(std::basic_ostream<CharT,Traits>& out, |
| 1484 | const pcg_extended<table_pow2, advance_pow2, |
| 1485 | baseclass, extvalclass, kdd>& rng) |
| 1486 | { |
| 1487 | auto orig_flags = out.flags(std::ios_base::dec | std::ios_base::left); |
| 1488 | auto space = out.widen(' '); |
| 1489 | auto orig_fill = out.fill(); |
| 1490 | |
| 1491 | out << rng.multiplier() << space |
| 1492 | << rng.increment() << space |
| 1493 | << rng.state_; |
| 1494 | |
| 1495 | for (const auto& datum : rng.data_) |
| 1496 | out << space << datum; |
| 1497 | |
| 1498 | out.flags(orig_flags); |
| 1499 | out.fill(orig_fill); |
| 1500 | return out; |
| 1501 | } |
| 1502 | |
| 1503 | template <typename CharT, typename Traits, |
| 1504 | bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1505 | typename baseclass, typename extvalclass, bool kdd> |
| 1506 | std::basic_istream<CharT,Traits>& |
| 1507 | operator>>(std::basic_istream<CharT,Traits>& in, |
| 1508 | pcg_extended<table_pow2, advance_pow2, |
| 1509 | baseclass, extvalclass, kdd>& rng) |
| 1510 | { |
| 1511 | pcg_extended<table_pow2, advance_pow2, baseclass, extvalclass> new_rng; |
| 1512 | auto& base_rng = static_cast<baseclass&>(new_rng); |
| 1513 | in >> base_rng; |
| 1514 | |
| 1515 | if (in.fail()) |
| 1516 | return in; |
| 1517 | |
| 1518 | auto orig_flags = in.flags(std::ios_base::dec | std::ios_base::skipws); |
| 1519 | |
| 1520 | for (auto& datum : new_rng.data_) { |
| 1521 | in >> datum; |
| 1522 | if (in.fail()) |
| 1523 | goto bail; |
| 1524 | } |
| 1525 | |
| 1526 | rng = new_rng; |
| 1527 | |
| 1528 | bail: |
| 1529 | in.flags(orig_flags); |
| 1530 | return in; |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | |
| 1535 | template <bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1536 | typename baseclass, typename extvalclass, bool kdd> |
| 1537 | void |
| 1538 | pcg_extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance_table() |
| 1539 | { |
| 1540 | bool carry = false; |
| 1541 | for (size_t i = 0; i < table_size; ++i) { |
| 1542 | if (carry) { |
| 1543 | carry = insideout::external_step(data_[i],i+1); |
| 1544 | } |
| 1545 | bool carry2 = insideout::external_step(data_[i],i+1); |
| 1546 | carry = carry || carry2; |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | template <bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1551 | typename baseclass, typename extvalclass, bool kdd> |
| 1552 | void |
| 1553 | pcg_extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance_table( |
| 1554 | state_type delta, bool isForwards) |
| 1555 | { |
| 1556 | typedef typename baseclass::state_type base_state_t; |
| 1557 | typedef typename extvalclass::state_type ext_state_t; |
| 1558 | constexpr bitcount_t basebits = sizeof(base_state_t)*8; |
| 1559 | constexpr bitcount_t extbits = sizeof(ext_state_t)*8; |
| 1560 | static_assert(basebits <= extbits || advance_pow2 > 0, |
| 1561 | "Current implementation might overflow its carry" ); |
| 1562 | |
| 1563 | base_state_t carry = 0; |
| 1564 | for (size_t i = 0; i < table_size; ++i) { |
| 1565 | base_state_t total_delta = carry + delta; |
| 1566 | ext_state_t trunc_delta = ext_state_t(total_delta); |
| 1567 | if (basebits > extbits) { |
| 1568 | carry = total_delta >> extbits; |
| 1569 | } else { |
| 1570 | carry = 0; |
| 1571 | } |
| 1572 | carry += |
| 1573 | insideout::external_advance(data_[i],i+1, trunc_delta, isForwards); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | template <bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1578 | typename baseclass, typename extvalclass, bool kdd> |
| 1579 | void pcg_extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance( |
| 1580 | state_type distance, bool forwards) |
| 1581 | { |
| 1582 | static_assert(kdd, |
| 1583 | "Efficient advance is too hard for non-kdd extension. " |
| 1584 | "For a weak advance, cast to base class" ); |
| 1585 | state_type zero = |
| 1586 | baseclass::is_mcg ? this->state_ & state_type(3U) : state_type(0U); |
| 1587 | if (may_tick) { |
| 1588 | state_type ticks = distance >> (advance_pow2*may_tick); |
| 1589 | // ^-- stupidity to appease GCC |
| 1590 | // warnings |
| 1591 | state_type adv_mask = |
| 1592 | baseclass::is_mcg ? tick_mask << 2 : tick_mask; |
| 1593 | state_type next_advance_distance = this->distance(zero, adv_mask); |
| 1594 | if (!forwards) |
| 1595 | next_advance_distance = (-next_advance_distance) & tick_mask; |
| 1596 | if (next_advance_distance < (distance & tick_mask)) { |
| 1597 | ++ticks; |
| 1598 | } |
| 1599 | if (ticks) |
| 1600 | advance_table(ticks, forwards); |
| 1601 | } |
| 1602 | if (forwards) { |
| 1603 | if (may_tock && this->distance(zero) <= distance) |
| 1604 | advance_table(); |
| 1605 | baseclass::advance(distance); |
| 1606 | } else { |
| 1607 | if (may_tock && -(this->distance(zero)) <= distance) |
| 1608 | advance_table(state_type(1U), false); |
| 1609 | baseclass::advance(-distance); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | } // namespace pcg_detail |
| 1614 | |
| 1615 | namespace pcg_engines { |
| 1616 | |
| 1617 | using namespace pcg_detail; |
| 1618 | |
| 1619 | /* Predefined types for XSH RS */ |
| 1620 | |
| 1621 | typedef oneseq_base<uint8_t, uint16_t, xsh_rs_mixin> oneseq_xsh_rs_16_8; |
| 1622 | typedef oneseq_base<uint16_t, uint32_t, xsh_rs_mixin> oneseq_xsh_rs_32_16; |
| 1623 | typedef oneseq_base<uint32_t, uint64_t, xsh_rs_mixin> oneseq_xsh_rs_64_32; |
| 1624 | typedef oneseq_base<uint64_t, pcg128_t, xsh_rs_mixin> oneseq_xsh_rs_128_64; |
| 1625 | typedef oneseq_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier> |
| 1626 | cm_oneseq_xsh_rs_128_64; |
| 1627 | |
| 1628 | typedef unique_base<uint8_t, uint16_t, xsh_rs_mixin> unique_xsh_rs_16_8; |
| 1629 | typedef unique_base<uint16_t, uint32_t, xsh_rs_mixin> unique_xsh_rs_32_16; |
| 1630 | typedef unique_base<uint32_t, uint64_t, xsh_rs_mixin> unique_xsh_rs_64_32; |
| 1631 | typedef unique_base<uint64_t, pcg128_t, xsh_rs_mixin> unique_xsh_rs_128_64; |
| 1632 | typedef unique_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier> |
| 1633 | cm_unique_xsh_rs_128_64; |
| 1634 | |
| 1635 | typedef setseq_base<uint8_t, uint16_t, xsh_rs_mixin> setseq_xsh_rs_16_8; |
| 1636 | typedef setseq_base<uint16_t, uint32_t, xsh_rs_mixin> setseq_xsh_rs_32_16; |
| 1637 | typedef setseq_base<uint32_t, uint64_t, xsh_rs_mixin> setseq_xsh_rs_64_32; |
| 1638 | typedef setseq_base<uint64_t, pcg128_t, xsh_rs_mixin> setseq_xsh_rs_128_64; |
| 1639 | typedef setseq_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier> |
| 1640 | cm_setseq_xsh_rs_128_64; |
| 1641 | |
| 1642 | typedef mcg_base<uint8_t, uint16_t, xsh_rs_mixin> mcg_xsh_rs_16_8; |
| 1643 | typedef mcg_base<uint16_t, uint32_t, xsh_rs_mixin> mcg_xsh_rs_32_16; |
| 1644 | typedef mcg_base<uint32_t, uint64_t, xsh_rs_mixin> mcg_xsh_rs_64_32; |
| 1645 | typedef mcg_base<uint64_t, pcg128_t, xsh_rs_mixin> mcg_xsh_rs_128_64; |
| 1646 | typedef mcg_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier> |
| 1647 | cm_mcg_xsh_rs_128_64; |
| 1648 | |
| 1649 | /* Predefined types for XSH RR */ |
| 1650 | |
| 1651 | typedef oneseq_base<uint8_t, uint16_t, xsh_rr_mixin> oneseq_xsh_rr_16_8; |
| 1652 | typedef oneseq_base<uint16_t, uint32_t, xsh_rr_mixin> oneseq_xsh_rr_32_16; |
| 1653 | typedef oneseq_base<uint32_t, uint64_t, xsh_rr_mixin> oneseq_xsh_rr_64_32; |
| 1654 | typedef oneseq_base<uint64_t, pcg128_t, xsh_rr_mixin> oneseq_xsh_rr_128_64; |
| 1655 | typedef oneseq_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier> |
| 1656 | cm_oneseq_xsh_rr_128_64; |
| 1657 | |
| 1658 | typedef unique_base<uint8_t, uint16_t, xsh_rr_mixin> unique_xsh_rr_16_8; |
| 1659 | typedef unique_base<uint16_t, uint32_t, xsh_rr_mixin> unique_xsh_rr_32_16; |
| 1660 | typedef unique_base<uint32_t, uint64_t, xsh_rr_mixin> unique_xsh_rr_64_32; |
| 1661 | typedef unique_base<uint64_t, pcg128_t, xsh_rr_mixin> unique_xsh_rr_128_64; |
| 1662 | typedef unique_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier> |
| 1663 | cm_unique_xsh_rr_128_64; |
| 1664 | |
| 1665 | typedef setseq_base<uint8_t, uint16_t, xsh_rr_mixin> setseq_xsh_rr_16_8; |
| 1666 | typedef setseq_base<uint16_t, uint32_t, xsh_rr_mixin> setseq_xsh_rr_32_16; |
| 1667 | typedef setseq_base<uint32_t, uint64_t, xsh_rr_mixin> setseq_xsh_rr_64_32; |
| 1668 | typedef setseq_base<uint64_t, pcg128_t, xsh_rr_mixin> setseq_xsh_rr_128_64; |
| 1669 | typedef setseq_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier> |
| 1670 | cm_setseq_xsh_rr_128_64; |
| 1671 | |
| 1672 | typedef mcg_base<uint8_t, uint16_t, xsh_rr_mixin> mcg_xsh_rr_16_8; |
| 1673 | typedef mcg_base<uint16_t, uint32_t, xsh_rr_mixin> mcg_xsh_rr_32_16; |
| 1674 | typedef mcg_base<uint32_t, uint64_t, xsh_rr_mixin> mcg_xsh_rr_64_32; |
| 1675 | typedef mcg_base<uint64_t, pcg128_t, xsh_rr_mixin> mcg_xsh_rr_128_64; |
| 1676 | typedef mcg_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier> |
| 1677 | cm_mcg_xsh_rr_128_64; |
| 1678 | |
| 1679 | |
| 1680 | /* Predefined types for RXS M XS */ |
| 1681 | |
| 1682 | typedef oneseq_base<uint8_t, uint8_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_8_8; |
| 1683 | typedef oneseq_base<uint16_t, uint16_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_16_16; |
| 1684 | typedef oneseq_base<uint32_t, uint32_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_32_32; |
| 1685 | typedef oneseq_base<uint64_t, uint64_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_64_64; |
| 1686 | typedef oneseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> |
| 1687 | oneseq_rxs_m_xs_128_128; |
| 1688 | typedef oneseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin, true, cheap_multiplier> |
| 1689 | cm_oneseq_rxs_m_xs_128_128; |
| 1690 | |
| 1691 | typedef unique_base<uint8_t, uint8_t, rxs_m_xs_mixin> unique_rxs_m_xs_8_8; |
| 1692 | typedef unique_base<uint16_t, uint16_t, rxs_m_xs_mixin> unique_rxs_m_xs_16_16; |
| 1693 | typedef unique_base<uint32_t, uint32_t, rxs_m_xs_mixin> unique_rxs_m_xs_32_32; |
| 1694 | typedef unique_base<uint64_t, uint64_t, rxs_m_xs_mixin> unique_rxs_m_xs_64_64; |
| 1695 | typedef unique_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> unique_rxs_m_xs_128_128; |
| 1696 | typedef unique_base<pcg128_t, pcg128_t, rxs_m_xs_mixin, true, cheap_multiplier> |
| 1697 | cm_unique_rxs_m_xs_128_128; |
| 1698 | |
| 1699 | typedef setseq_base<uint8_t, uint8_t, rxs_m_xs_mixin> setseq_rxs_m_xs_8_8; |
| 1700 | typedef setseq_base<uint16_t, uint16_t, rxs_m_xs_mixin> setseq_rxs_m_xs_16_16; |
| 1701 | typedef setseq_base<uint32_t, uint32_t, rxs_m_xs_mixin> setseq_rxs_m_xs_32_32; |
| 1702 | typedef setseq_base<uint64_t, uint64_t, rxs_m_xs_mixin> setseq_rxs_m_xs_64_64; |
| 1703 | typedef setseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> setseq_rxs_m_xs_128_128; |
| 1704 | typedef setseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin, true, cheap_multiplier> |
| 1705 | cm_setseq_rxs_m_xs_128_128; |
| 1706 | |
| 1707 | // MCG versions don't make sense here, so aren't defined. |
| 1708 | |
| 1709 | /* Predefined types for RXS M */ |
| 1710 | |
| 1711 | typedef oneseq_base<uint8_t, uint16_t, rxs_m_mixin> oneseq_rxs_m_16_8; |
| 1712 | typedef oneseq_base<uint16_t, uint32_t, rxs_m_mixin> oneseq_rxs_m_32_16; |
| 1713 | typedef oneseq_base<uint32_t, uint64_t, rxs_m_mixin> oneseq_rxs_m_64_32; |
| 1714 | typedef oneseq_base<uint64_t, pcg128_t, rxs_m_mixin> oneseq_rxs_m_128_64; |
| 1715 | typedef oneseq_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier> |
| 1716 | cm_oneseq_rxs_m_128_64; |
| 1717 | |
| 1718 | typedef unique_base<uint8_t, uint16_t, rxs_m_mixin> unique_rxs_m_16_8; |
| 1719 | typedef unique_base<uint16_t, uint32_t, rxs_m_mixin> unique_rxs_m_32_16; |
| 1720 | typedef unique_base<uint32_t, uint64_t, rxs_m_mixin> unique_rxs_m_64_32; |
| 1721 | typedef unique_base<uint64_t, pcg128_t, rxs_m_mixin> unique_rxs_m_128_64; |
| 1722 | typedef unique_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier> |
| 1723 | cm_unique_rxs_m_128_64; |
| 1724 | |
| 1725 | typedef setseq_base<uint8_t, uint16_t, rxs_m_mixin> setseq_rxs_m_16_8; |
| 1726 | typedef setseq_base<uint16_t, uint32_t, rxs_m_mixin> setseq_rxs_m_32_16; |
| 1727 | typedef setseq_base<uint32_t, uint64_t, rxs_m_mixin> setseq_rxs_m_64_32; |
| 1728 | typedef setseq_base<uint64_t, pcg128_t, rxs_m_mixin> setseq_rxs_m_128_64; |
| 1729 | typedef setseq_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier> |
| 1730 | cm_setseq_rxs_m_128_64; |
| 1731 | |
| 1732 | typedef mcg_base<uint8_t, uint16_t, rxs_m_mixin> mcg_rxs_m_16_8; |
| 1733 | typedef mcg_base<uint16_t, uint32_t, rxs_m_mixin> mcg_rxs_m_32_16; |
| 1734 | typedef mcg_base<uint32_t, uint64_t, rxs_m_mixin> mcg_rxs_m_64_32; |
| 1735 | typedef mcg_base<uint64_t, pcg128_t, rxs_m_mixin> mcg_rxs_m_128_64; |
| 1736 | typedef mcg_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier> |
| 1737 | cm_mcg_rxs_m_128_64; |
| 1738 | |
| 1739 | /* Predefined types for DXSM */ |
| 1740 | |
| 1741 | typedef oneseq_base<uint8_t, uint16_t, dxsm_mixin> oneseq_dxsm_16_8; |
| 1742 | typedef oneseq_base<uint16_t, uint32_t, dxsm_mixin> oneseq_dxsm_32_16; |
| 1743 | typedef oneseq_base<uint32_t, uint64_t, dxsm_mixin> oneseq_dxsm_64_32; |
| 1744 | typedef oneseq_base<uint64_t, pcg128_t, dxsm_mixin> oneseq_dxsm_128_64; |
| 1745 | typedef oneseq_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier> |
| 1746 | cm_oneseq_dxsm_128_64; |
| 1747 | |
| 1748 | typedef unique_base<uint8_t, uint16_t, dxsm_mixin> unique_dxsm_16_8; |
| 1749 | typedef unique_base<uint16_t, uint32_t, dxsm_mixin> unique_dxsm_32_16; |
| 1750 | typedef unique_base<uint32_t, uint64_t, dxsm_mixin> unique_dxsm_64_32; |
| 1751 | typedef unique_base<uint64_t, pcg128_t, dxsm_mixin> unique_dxsm_128_64; |
| 1752 | typedef unique_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier> |
| 1753 | cm_unique_dxsm_128_64; |
| 1754 | |
| 1755 | typedef setseq_base<uint8_t, uint16_t, dxsm_mixin> setseq_dxsm_16_8; |
| 1756 | typedef setseq_base<uint16_t, uint32_t, dxsm_mixin> setseq_dxsm_32_16; |
| 1757 | typedef setseq_base<uint32_t, uint64_t, dxsm_mixin> setseq_dxsm_64_32; |
| 1758 | typedef setseq_base<uint64_t, pcg128_t, dxsm_mixin> setseq_dxsm_128_64; |
| 1759 | typedef setseq_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier> |
| 1760 | cm_setseq_dxsm_128_64; |
| 1761 | |
| 1762 | typedef mcg_base<uint8_t, uint16_t, dxsm_mixin> mcg_dxsm_16_8; |
| 1763 | typedef mcg_base<uint16_t, uint32_t, dxsm_mixin> mcg_dxsm_32_16; |
| 1764 | typedef mcg_base<uint32_t, uint64_t, dxsm_mixin> mcg_dxsm_64_32; |
| 1765 | typedef mcg_base<uint64_t, pcg128_t, dxsm_mixin> mcg_dxsm_128_64; |
| 1766 | typedef mcg_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier> |
| 1767 | cm_mcg_dxsm_128_64; |
| 1768 | |
| 1769 | /* Predefined types for XSL RR (only defined for "large" types) */ |
| 1770 | |
| 1771 | typedef oneseq_base<uint32_t, uint64_t, xsl_rr_mixin> oneseq_xsl_rr_64_32; |
| 1772 | typedef oneseq_base<uint64_t, pcg128_t, xsl_rr_mixin> oneseq_xsl_rr_128_64; |
| 1773 | typedef oneseq_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier> |
| 1774 | cm_oneseq_xsl_rr_128_64; |
| 1775 | |
| 1776 | typedef unique_base<uint32_t, uint64_t, xsl_rr_mixin> unique_xsl_rr_64_32; |
| 1777 | typedef unique_base<uint64_t, pcg128_t, xsl_rr_mixin> unique_xsl_rr_128_64; |
| 1778 | typedef unique_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier> |
| 1779 | cm_unique_xsl_rr_128_64; |
| 1780 | |
| 1781 | typedef setseq_base<uint32_t, uint64_t, xsl_rr_mixin> setseq_xsl_rr_64_32; |
| 1782 | typedef setseq_base<uint64_t, pcg128_t, xsl_rr_mixin> setseq_xsl_rr_128_64; |
| 1783 | typedef setseq_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier> |
| 1784 | cm_setseq_xsl_rr_128_64; |
| 1785 | |
| 1786 | typedef mcg_base<uint32_t, uint64_t, xsl_rr_mixin> mcg_xsl_rr_64_32; |
| 1787 | typedef mcg_base<uint64_t, pcg128_t, xsl_rr_mixin> mcg_xsl_rr_128_64; |
| 1788 | typedef mcg_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier> |
| 1789 | cm_mcg_xsl_rr_128_64; |
| 1790 | |
| 1791 | |
| 1792 | /* Predefined types for XSL RR RR (only defined for "large" types) */ |
| 1793 | |
| 1794 | typedef oneseq_base<uint64_t, uint64_t, xsl_rr_rr_mixin> |
| 1795 | oneseq_xsl_rr_rr_64_64; |
| 1796 | typedef oneseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin> |
| 1797 | oneseq_xsl_rr_rr_128_128; |
| 1798 | typedef oneseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin, true, cheap_multiplier> |
| 1799 | cm_oneseq_xsl_rr_rr_128_128; |
| 1800 | |
| 1801 | typedef unique_base<uint64_t, uint64_t, xsl_rr_rr_mixin> |
| 1802 | unique_xsl_rr_rr_64_64; |
| 1803 | typedef unique_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin> |
| 1804 | unique_xsl_rr_rr_128_128; |
| 1805 | typedef unique_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin, true, cheap_multiplier> |
| 1806 | cm_unique_xsl_rr_rr_128_128; |
| 1807 | |
| 1808 | typedef setseq_base<uint64_t, uint64_t, xsl_rr_rr_mixin> |
| 1809 | setseq_xsl_rr_rr_64_64; |
| 1810 | typedef setseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin> |
| 1811 | setseq_xsl_rr_rr_128_128; |
| 1812 | typedef setseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin, true, cheap_multiplier> |
| 1813 | cm_setseq_xsl_rr_rr_128_128; |
| 1814 | |
| 1815 | // MCG versions don't make sense here, so aren't defined. |
| 1816 | |
| 1817 | /* Extended generators */ |
| 1818 | |
| 1819 | template <bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1820 | typename BaseRNG, bool kdd = true> |
| 1821 | using ext_std8 = pcg_extended<table_pow2, advance_pow2, BaseRNG, |
| 1822 | oneseq_rxs_m_xs_8_8, kdd>; |
| 1823 | |
| 1824 | template <bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1825 | typename BaseRNG, bool kdd = true> |
| 1826 | using ext_std16 = pcg_extended<table_pow2, advance_pow2, BaseRNG, |
| 1827 | oneseq_rxs_m_xs_16_16, kdd>; |
| 1828 | |
| 1829 | template <bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1830 | typename BaseRNG, bool kdd = true> |
| 1831 | using ext_std32 = pcg_extended<table_pow2, advance_pow2, BaseRNG, |
| 1832 | oneseq_rxs_m_xs_32_32, kdd>; |
| 1833 | |
| 1834 | template <bitcount_t table_pow2, bitcount_t advance_pow2, |
| 1835 | typename BaseRNG, bool kdd = true> |
| 1836 | using ext_std64 = pcg_extended<table_pow2, advance_pow2, BaseRNG, |
| 1837 | oneseq_rxs_m_xs_64_64, kdd>; |
| 1838 | |
| 1839 | |
| 1840 | template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true> |
| 1841 | using ext_oneseq_rxs_m_xs_32_32 = |
| 1842 | ext_std32<table_pow2, advance_pow2, oneseq_rxs_m_xs_32_32, kdd>; |
| 1843 | |
| 1844 | template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true> |
| 1845 | using ext_mcg_xsh_rs_64_32 = |
| 1846 | ext_std32<table_pow2, advance_pow2, mcg_xsh_rs_64_32, kdd>; |
| 1847 | |
| 1848 | template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true> |
| 1849 | using ext_oneseq_xsh_rs_64_32 = |
| 1850 | ext_std32<table_pow2, advance_pow2, oneseq_xsh_rs_64_32, kdd>; |
| 1851 | |
| 1852 | template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true> |
| 1853 | using ext_setseq_xsh_rr_64_32 = |
| 1854 | ext_std32<table_pow2, advance_pow2, setseq_xsh_rr_64_32, kdd>; |
| 1855 | |
| 1856 | template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true> |
| 1857 | using ext_mcg_xsl_rr_128_64 = |
| 1858 | ext_std64<table_pow2, advance_pow2, mcg_xsl_rr_128_64, kdd>; |
| 1859 | |
| 1860 | template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true> |
| 1861 | using ext_oneseq_xsl_rr_128_64 = |
| 1862 | ext_std64<table_pow2, advance_pow2, oneseq_xsl_rr_128_64, kdd>; |
| 1863 | |
| 1864 | template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true> |
| 1865 | using ext_setseq_xsl_rr_128_64 = |
| 1866 | ext_std64<table_pow2, advance_pow2, setseq_xsl_rr_128_64, kdd>; |
| 1867 | |
| 1868 | } // namespace pcg_engines |
| 1869 | |
| 1870 | typedef pcg_engines::setseq_xsh_rr_64_32 pcg32; |
| 1871 | typedef pcg_engines::oneseq_xsh_rr_64_32 pcg32_oneseq; |
| 1872 | typedef pcg_engines::unique_xsh_rr_64_32 pcg32_unique; |
| 1873 | typedef pcg_engines::mcg_xsh_rs_64_32 pcg32_fast; |
| 1874 | |
| 1875 | typedef pcg_engines::setseq_xsl_rr_128_64 pcg64; |
| 1876 | typedef pcg_engines::oneseq_xsl_rr_128_64 pcg64_oneseq; |
| 1877 | typedef pcg_engines::unique_xsl_rr_128_64 pcg64_unique; |
| 1878 | typedef pcg_engines::mcg_xsl_rr_128_64 pcg64_fast; |
| 1879 | |
| 1880 | typedef pcg_engines::setseq_rxs_m_xs_8_8 pcg8_once_insecure; |
| 1881 | typedef pcg_engines::setseq_rxs_m_xs_16_16 pcg16_once_insecure; |
| 1882 | typedef pcg_engines::setseq_rxs_m_xs_32_32 pcg32_once_insecure; |
| 1883 | typedef pcg_engines::setseq_rxs_m_xs_64_64 pcg64_once_insecure; |
| 1884 | typedef pcg_engines::setseq_xsl_rr_rr_128_128 pcg128_once_insecure; |
| 1885 | |
| 1886 | typedef pcg_engines::oneseq_rxs_m_xs_8_8 pcg8_oneseq_once_insecure; |
| 1887 | typedef pcg_engines::oneseq_rxs_m_xs_16_16 pcg16_oneseq_once_insecure; |
| 1888 | typedef pcg_engines::oneseq_rxs_m_xs_32_32 pcg32_oneseq_once_insecure; |
| 1889 | typedef pcg_engines::oneseq_rxs_m_xs_64_64 pcg64_oneseq_once_insecure; |
| 1890 | typedef pcg_engines::oneseq_xsl_rr_rr_128_128 pcg128_oneseq_once_insecure; |
| 1891 | |
| 1892 | |
| 1893 | // These two extended RNGs provide two-dimensionally equidistributed |
| 1894 | // 32-bit generators. pcg32_k2_fast occupies the same space as pcg64, |
| 1895 | // and can be called twice to generate 64 bits, but does not required |
| 1896 | // 128-bit math; on 32-bit systems, it's faster than pcg64 as well. |
| 1897 | |
| 1898 | typedef pcg_engines::ext_setseq_xsh_rr_64_32<1,16,true> pcg32_k2; |
| 1899 | typedef pcg_engines::ext_oneseq_xsh_rs_64_32<1,32,true> pcg32_k2_fast; |
| 1900 | |
| 1901 | // These eight extended RNGs have about as much state as arc4random |
| 1902 | // |
| 1903 | // - the k variants are k-dimensionally equidistributed |
| 1904 | // - the c variants offer better crypographic security |
| 1905 | // |
| 1906 | // (just how good the cryptographic security is is an open question) |
| 1907 | |
| 1908 | typedef pcg_engines::ext_setseq_xsh_rr_64_32<6,16,true> pcg32_k64; |
| 1909 | typedef pcg_engines::ext_mcg_xsh_rs_64_32<6,32,true> pcg32_k64_oneseq; |
| 1910 | typedef pcg_engines::ext_oneseq_xsh_rs_64_32<6,32,true> pcg32_k64_fast; |
| 1911 | |
| 1912 | typedef pcg_engines::ext_setseq_xsh_rr_64_32<6,16,false> pcg32_c64; |
| 1913 | typedef pcg_engines::ext_oneseq_xsh_rs_64_32<6,32,false> pcg32_c64_oneseq; |
| 1914 | typedef pcg_engines::ext_mcg_xsh_rs_64_32<6,32,false> pcg32_c64_fast; |
| 1915 | |
| 1916 | typedef pcg_engines::ext_setseq_xsl_rr_128_64<5,16,true> pcg64_k32; |
| 1917 | typedef pcg_engines::ext_oneseq_xsl_rr_128_64<5,128,true> pcg64_k32_oneseq; |
| 1918 | typedef pcg_engines::ext_mcg_xsl_rr_128_64<5,128,true> pcg64_k32_fast; |
| 1919 | |
| 1920 | typedef pcg_engines::ext_setseq_xsl_rr_128_64<5,16,false> pcg64_c32; |
| 1921 | typedef pcg_engines::ext_oneseq_xsl_rr_128_64<5,128,false> pcg64_c32_oneseq; |
| 1922 | typedef pcg_engines::ext_mcg_xsl_rr_128_64<5,128,false> pcg64_c32_fast; |
| 1923 | |
| 1924 | // These eight extended RNGs have more state than the Mersenne twister |
| 1925 | // |
| 1926 | // - the k variants are k-dimensionally equidistributed |
| 1927 | // - the c variants offer better crypographic security |
| 1928 | // |
| 1929 | // (just how good the cryptographic security is is an open question) |
| 1930 | |
| 1931 | typedef pcg_engines::ext_setseq_xsh_rr_64_32<10,16,true> pcg32_k1024; |
| 1932 | typedef pcg_engines::ext_oneseq_xsh_rs_64_32<10,32,true> pcg32_k1024_fast; |
| 1933 | |
| 1934 | typedef pcg_engines::ext_setseq_xsh_rr_64_32<10,16,false> pcg32_c1024; |
| 1935 | typedef pcg_engines::ext_oneseq_xsh_rs_64_32<10,32,false> pcg32_c1024_fast; |
| 1936 | |
| 1937 | typedef pcg_engines::ext_setseq_xsl_rr_128_64<10,16,true> pcg64_k1024; |
| 1938 | typedef pcg_engines::ext_oneseq_xsl_rr_128_64<10,128,true> pcg64_k1024_fast; |
| 1939 | |
| 1940 | typedef pcg_engines::ext_setseq_xsl_rr_128_64<10,16,false> pcg64_c1024; |
| 1941 | typedef pcg_engines::ext_oneseq_xsl_rr_128_64<10,128,false> pcg64_c1024_fast; |
| 1942 | |
| 1943 | // These generators have an insanely huge period (2^524352), and is suitable |
| 1944 | // for silly party tricks, such as dumping out 64 KB ZIP files at an arbitrary |
| 1945 | // point in the future. [Actually, over the full period of the generator, it |
| 1946 | // will produce every 64 KB ZIP file 2^64 times!] |
| 1947 | |
| 1948 | typedef pcg_engines::ext_setseq_xsh_rr_64_32<14,16,true> pcg32_k16384; |
| 1949 | typedef pcg_engines::ext_oneseq_xsh_rs_64_32<14,32,true> pcg32_k16384_fast; |
| 1950 | |
| 1951 | #ifdef _MSC_VER |
| 1952 | #pragma warning(default:4146) |
| 1953 | #endif |
| 1954 | |
| 1955 | #endif // PCG_RAND_HPP_INCLUDED |
| 1956 | |