| 1 | /* |
| 2 | * PCG Random Number Generation for C. |
| 3 | * |
| 4 | * Copyright 2014-2019 Melissa O'Neill <[email protected]>, |
| 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 is derived from the canonical C++ PCG implementation, which |
| 24 | * has many additional features and is preferable if you can use C++ in |
| 25 | * your project. |
| 26 | * |
| 27 | * Repetative C code is derived using C preprocessor metaprogramming |
| 28 | * techniques. |
| 29 | */ |
| 30 | |
| 31 | #include "pcg_variants.h"cxx . . $ `<file>/vendor/pcg/pcg_variants.h`/ |
| 32 | |
| 33 | /* Multi-step advance functions (jump-ahead, jump-back) |
| 34 | * |
| 35 | * The method used here is based on Brown, "Random Number Generation |
| 36 | * with Arbitrary Stride,", Transactions of the American Nuclear |
| 37 | * Society (Nov. 1994). The algorithm is very similar to fast |
| 38 | * exponentiation. |
| 39 | * |
| 40 | * Even though delta is an unsigned integer, we can pass a |
| 41 | * signed integer to go backwards, it just goes "the long way round". |
| 42 | */ |
| 43 | |
| 44 | uint64_tcxx . . $ uint64_t# pcg_advance_lcg_64cxx . . $ pcg_advance_lcg_64(8f424044f20ee7cf).(uint64_tcxx . . $ uint64_t# state, uint64_tcxx . . $ uint64_t# delta, uint64_tcxx . . $ uint64_t# cur_mult, |
| 45 | uint64_tcxx . . $ uint64_t# cur_plus) |
| 46 | { |
| 47 | uint64_tcxx . . $ uint64_t# acc_mult = 1u; |
| 48 | uint64_tcxx . . $ uint64_t# acc_plus = 0u; |
| 49 | while (delta > 0) { |
| 50 | if (delta & 1) { |
| 51 | acc_mult *= cur_mult; |
| 52 | acc_plus = acc_plus * cur_mult + cur_plus; |
| 53 | } |
| 54 | cur_plus = (cur_mult + 1) * cur_plus; |
| 55 | cur_mult *= cur_mult; |
| 56 | delta /= 2; |
| 57 | } |
| 58 | return acc_mult * state + acc_plus; |
| 59 | } |