2 * PCG Random Number Generation for C.
4 * Copyright 2014-2019 Melissa O'Neill <oneill@pcg-random.org>,
5 * and the PCG Project contributors.
7 * SPDX-License-Identifier: (Apache-2.0 OR MIT)
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.
15 * Distributed on an "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, either
16 * express or implied. See your chosen license for details.
18 * For additional information about the PCG random number generation scheme,
19 * visit http://www.pcg-random.org/.
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
27 * Much of the derivation was performed mechanically. In particular, the
28 * output functions were generated by compiling the C++ output functions
29 * into LLVM bitcode and then transforming that using the LLVM C backend
30 * (from https://github.com/draperlaboratory/llvm-cbe), and then
31 * postprocessing and hand editing the output.
33 * Much of the remaining code was generated by C-preprocessor metaprogramming.
36 #ifndef PCG_VARIANTS_H_INCLUDED
37 #define PCG_VARIANTS_H_INCLUDED 1
42 typedef __uint128_t pcg128_t;
43 #define PCG_128BIT_CONSTANT(high,low) \
44 ((((pcg128_t)high) << 64) + low)
45 #define PCG_HAS_128BIT_OPS 1
48 #if __GNUC_GNU_INLINE__ && !defined(__cplusplus)
49 #error Nonstandard GNU inlining semantics. Compile with -std=c99 or better.
50 /* We could instead use macros PCG_INLINE and PCG_EXTERN_INLINE
51 but better to just reject ancient C code. */
59 * Rotate helper functions.
62 inline uint8_t pcg_rotr_8(uint8_t value, unsigned int rot)
64 /* Unfortunately, clang is kinda pathetic when it comes to properly
65 * recognizing idiomatic rotate code, so for clang we actually provide
66 * assembler directives (enabled with PCG_USE_INLINE_ASM). Boo, hiss.
68 #if PCG_USE_INLINE_ASM && __clang__ && (__x86_64__ || __i386__)
69 asm ("rorb %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
72 return (value >> rot) | (value << ((- rot) & 7));
76 inline uint16_t pcg_rotr_16(uint16_t value, unsigned int rot)
78 #if PCG_USE_INLINE_ASM && __clang__ && (__x86_64__ || __i386__)
79 asm ("rorw %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
82 return (value >> rot) | (value << ((- rot) & 15));
86 inline uint32_t pcg_rotr_32(uint32_t value, unsigned int rot)
88 #if PCG_USE_INLINE_ASM && __clang__ && (__x86_64__ || __i386__)
89 asm ("rorl %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
92 return (value >> rot) | (value << ((- rot) & 31));
96 inline uint64_t pcg_rotr_64(uint64_t value, unsigned int rot)
98 #if 0 && PCG_USE_INLINE_ASM && __clang__ && __x86_64__
99 /* For whatever reason, clang actually *does* generate rotq by
100 itself, so we don't need this code. */
101 asm ("rorq %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
104 return (value >> rot) | (value << ((- rot) & 63));
108 #if PCG_HAS_128BIT_OPS
109 inline pcg128_t pcg_rotr_128(pcg128_t value, unsigned int rot)
111 return (value >> rot) | (value << ((- rot) & 127));
116 * Output functions. These are the core of the PCG generation scheme.
121 inline uint8_t pcg_output_xsh_rs_16_8(uint16_t state)
123 return (uint8_t)(((state >> 7u) ^ state) >> ((state >> 14u) + 3u));
126 inline uint16_t pcg_output_xsh_rs_32_16(uint32_t state)
128 return (uint16_t)(((state >> 11u) ^ state) >> ((state >> 30u) + 11u));
131 inline uint32_t pcg_output_xsh_rs_64_32(uint64_t state)
134 return (uint32_t)(((state >> 22u) ^ state) >> ((state >> 61u) + 22u));
137 #if PCG_HAS_128BIT_OPS
138 inline uint64_t pcg_output_xsh_rs_128_64(pcg128_t state)
140 return (uint64_t)(((state >> 43u) ^ state) >> ((state >> 124u) + 45u));
146 inline uint8_t pcg_output_xsh_rr_16_8(uint16_t state)
148 return pcg_rotr_8(((state >> 5u) ^ state) >> 5u, state >> 13u);
151 inline uint16_t pcg_output_xsh_rr_32_16(uint32_t state)
153 return pcg_rotr_16(((state >> 10u) ^ state) >> 12u, state >> 28u);
156 inline uint32_t pcg_output_xsh_rr_64_32(uint64_t state)
158 return pcg_rotr_32(((state >> 18u) ^ state) >> 27u, state >> 59u);
161 #if PCG_HAS_128BIT_OPS
162 inline uint64_t pcg_output_xsh_rr_128_64(pcg128_t state)
164 return pcg_rotr_64(((state >> 35u) ^ state) >> 58u, state >> 122u);
170 inline uint8_t pcg_output_rxs_m_xs_8_8(uint8_t state)
172 uint8_t word = ((state >> ((state >> 6u) + 2u)) ^ state) * 217u;
173 return (word >> 6u) ^ word;
176 inline uint16_t pcg_output_rxs_m_xs_16_16(uint16_t state)
178 uint16_t word = ((state >> ((state >> 13u) + 3u)) ^ state) * 62169u;
179 return (word >> 11u) ^ word;
182 inline uint32_t pcg_output_rxs_m_xs_32_32(uint32_t state)
184 uint32_t word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
185 return (word >> 22u) ^ word;
188 inline uint64_t pcg_output_rxs_m_xs_64_64(uint64_t state)
190 uint64_t word = ((state >> ((state >> 59u) + 5u)) ^ state)
191 * 12605985483714917081ull;
192 return (word >> 43u) ^ word;
195 #if PCG_HAS_128BIT_OPS
196 inline pcg128_t pcg_output_rxs_m_xs_128_128(pcg128_t state)
198 pcg128_t word = ((state >> ((state >> 122u) + 6u)) ^ state)
199 * (PCG_128BIT_CONSTANT(17766728186571221404ULL,
200 12605985483714917081ULL));
201 /* 327738287884841127335028083622016905945 */
202 return (word >> 86u) ^ word;
208 inline uint8_t pcg_output_rxs_m_16_8(uint16_t state)
210 return (((state >> ((state >> 13u) + 3u)) ^ state) * 62169u) >> 8u;
213 inline uint16_t pcg_output_rxs_m_32_16(uint32_t state)
215 return (((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u) >> 16u;
218 inline uint32_t pcg_output_rxs_m_64_32(uint64_t state)
220 return (((state >> ((state >> 59u) + 5u)) ^ state)
221 * 12605985483714917081ull) >> 32u;
224 #if PCG_HAS_128BIT_OPS
225 inline uint64_t pcg_output_rxs_m_128_64(pcg128_t state)
227 return (((state >> ((state >> 122u) + 6u)) ^ state)
228 * (PCG_128BIT_CONSTANT(17766728186571221404ULL,
229 12605985483714917081ULL))) >> 64u;
230 /* 327738287884841127335028083622016905945 */
234 /* XSL RR (only defined for >= 64 bits) */
236 inline uint32_t pcg_output_xsl_rr_64_32(uint64_t state)
238 return pcg_rotr_32(((uint32_t)(state >> 32u)) ^ (uint32_t)state,
242 #if PCG_HAS_128BIT_OPS
243 inline uint64_t pcg_output_xsl_rr_128_64(pcg128_t state)
245 return pcg_rotr_64(((uint64_t)(state >> 64u)) ^ (uint64_t)state,
250 /* XSL RR RR (only defined for >= 64 bits) */
252 inline uint64_t pcg_output_xsl_rr_rr_64_64(uint64_t state)
254 uint32_t rot1 = (uint32_t)(state >> 59u);
255 uint32_t high = (uint32_t)(state >> 32u);
256 uint32_t low = (uint32_t)state;
257 uint32_t xored = high ^ low;
258 uint32_t newlow = pcg_rotr_32(xored, rot1);
259 uint32_t newhigh = pcg_rotr_32(high, newlow & 31u);
260 return (((uint64_t)newhigh) << 32u) | newlow;
263 #if PCG_HAS_128BIT_OPS
264 inline pcg128_t pcg_output_xsl_rr_rr_128_128(pcg128_t state)
266 uint32_t rot1 = (uint32_t)(state >> 122u);
267 uint64_t high = (uint64_t)(state >> 64u);
268 uint64_t low = (uint64_t)state;
269 uint64_t xored = high ^ low;
270 uint64_t newlow = pcg_rotr_64(xored, rot1);
271 uint64_t newhigh = pcg_rotr_64(high, newlow & 63u);
272 return (((pcg128_t)newhigh) << 64u) | newlow;
276 #define PCG_DEFAULT_MULTIPLIER_8 141U
277 #define PCG_DEFAULT_MULTIPLIER_16 12829U
278 #define PCG_DEFAULT_MULTIPLIER_32 747796405U
279 #define PCG_DEFAULT_MULTIPLIER_64 6364136223846793005ULL
281 #define PCG_DEFAULT_INCREMENT_8 77U
282 #define PCG_DEFAULT_INCREMENT_16 47989U
283 #define PCG_DEFAULT_INCREMENT_32 2891336453U
284 #define PCG_DEFAULT_INCREMENT_64 1442695040888963407ULL
286 #if PCG_HAS_128BIT_OPS
287 #define PCG_DEFAULT_MULTIPLIER_128 \
288 PCG_128BIT_CONSTANT(2549297995355413924ULL,4865540595714422341ULL)
289 #define PCG_DEFAULT_INCREMENT_128 \
290 PCG_128BIT_CONSTANT(6364136223846793005ULL,1442695040888963407ULL)
294 * Static initialization constants (if you can't call srandom for some
298 #define PCG_STATE_ONESEQ_8_INITIALIZER { 0xd7U }
299 #define PCG_STATE_ONESEQ_16_INITIALIZER { 0x20dfU }
300 #define PCG_STATE_ONESEQ_32_INITIALIZER { 0x46b56677U }
301 #define PCG_STATE_ONESEQ_64_INITIALIZER { 0x4d595df4d0f33173ULL }
302 #if PCG_HAS_128BIT_OPS
303 #define PCG_STATE_ONESEQ_128_INITIALIZER \
304 { PCG_128BIT_CONSTANT(0xb8dc10e158a92392ULL, 0x98046df007ec0a53ULL) }
307 #define PCG_STATE_UNIQUE_8_INITIALIZER PCG_STATE_ONESEQ_8_INITIALIZER
308 #define PCG_STATE_UNIQUE_16_INITIALIZER PCG_STATE_ONESEQ_16_INITIALIZER
309 #define PCG_STATE_UNIQUE_32_INITIALIZER PCG_STATE_ONESEQ_32_INITIALIZER
310 #define PCG_STATE_UNIQUE_64_INITIALIZER PCG_STATE_ONESEQ_64_INITIALIZER
311 #if PCG_HAS_128BIT_OPS
312 #define PCG_STATE_UNIQUE_128_INITIALIZER PCG_STATE_ONESEQ_128_INITIALIZER
315 #define PCG_STATE_MCG_8_INITIALIZER { 0xe5U }
316 #define PCG_STATE_MCG_16_INITIALIZER { 0xa5e5U }
317 #define PCG_STATE_MCG_32_INITIALIZER { 0xd15ea5e5U }
318 #define PCG_STATE_MCG_64_INITIALIZER { 0xcafef00dd15ea5e5ULL }
319 #if PCG_HAS_128BIT_OPS
320 #define PCG_STATE_MCG_128_INITIALIZER \
321 { PCG_128BIT_CONSTANT(0x0000000000000000ULL, 0xcafef00dd15ea5e5ULL) }
324 #define PCG_STATE_SETSEQ_8_INITIALIZER { 0x9bU, 0xdbU }
325 #define PCG_STATE_SETSEQ_16_INITIALIZER { 0xe39bU, 0x5bdbU }
326 #define PCG_STATE_SETSEQ_32_INITIALIZER { 0xec02d89bU, 0x94b95bdbU }
327 #define PCG_STATE_SETSEQ_64_INITIALIZER \
328 { 0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL }
329 #if PCG_HAS_128BIT_OPS
330 #define PCG_STATE_SETSEQ_128_INITIALIZER \
331 { PCG_128BIT_CONSTANT(0x979c9a98d8462005ULL, 0x7d3e9cb6cfe0549bULL), \
332 PCG_128BIT_CONSTANT(0x0000000000000001ULL, 0xda3e39cb94b95bdbULL) }
335 /* Representations for the oneseq, mcg, and unique variants */
341 struct pcg_state_16 {
345 struct pcg_state_32 {
349 struct pcg_state_64 {
353 #if PCG_HAS_128BIT_OPS
354 struct pcg_state_128 {
359 /* Representations setseq variants */
361 struct pcg_state_setseq_8 {
366 struct pcg_state_setseq_16 {
371 struct pcg_state_setseq_32 {
376 struct pcg_state_setseq_64 {
381 #if PCG_HAS_128BIT_OPS
382 struct pcg_state_setseq_128 {
388 /* Multi-step advance functions (jump-ahead, jump-back) */
390 extern uint8_t pcg_advance_lcg_8(uint8_t state, uint8_t delta, uint8_t cur_mult,
392 extern uint16_t pcg_advance_lcg_16(uint16_t state, uint16_t delta,
393 uint16_t cur_mult, uint16_t cur_plus);
394 extern uint32_t pcg_advance_lcg_32(uint32_t state, uint32_t delta,
395 uint32_t cur_mult, uint32_t cur_plus);
396 extern uint64_t pcg_advance_lcg_64(uint64_t state, uint64_t delta,
397 uint64_t cur_mult, uint64_t cur_plus);
399 #if PCG_HAS_128BIT_OPS
400 extern pcg128_t pcg_advance_lcg_128(pcg128_t state, pcg128_t delta,
401 pcg128_t cur_mult, pcg128_t cur_plus);
404 /* Functions to advance the underlying LCG, one version for each size and
405 * each style. These functions are considered semi-private. There is rarely
406 * a good reason to call them directly.
409 inline void pcg_oneseq_8_step_r(struct pcg_state_8* rng)
411 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8
412 + PCG_DEFAULT_INCREMENT_8;
415 inline void pcg_oneseq_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
417 rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
418 PCG_DEFAULT_INCREMENT_8);
421 inline void pcg_mcg_8_step_r(struct pcg_state_8* rng)
423 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8;
426 inline void pcg_mcg_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
429 = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8, 0u);
432 inline void pcg_unique_8_step_r(struct pcg_state_8* rng)
434 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8
435 + (uint8_t)(((intptr_t)rng) | 1u);
438 inline void pcg_unique_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
440 rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
441 (uint8_t)(((intptr_t)rng) | 1u));
444 inline void pcg_setseq_8_step_r(struct pcg_state_setseq_8* rng)
446 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8 + rng->inc;
449 inline void pcg_setseq_8_advance_r(struct pcg_state_setseq_8* rng,
452 rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
456 inline void pcg_oneseq_16_step_r(struct pcg_state_16* rng)
458 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16
459 + PCG_DEFAULT_INCREMENT_16;
462 inline void pcg_oneseq_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
464 rng->state = pcg_advance_lcg_16(
465 rng->state, delta, PCG_DEFAULT_MULTIPLIER_16, PCG_DEFAULT_INCREMENT_16);
468 inline void pcg_mcg_16_step_r(struct pcg_state_16* rng)
470 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16;
473 inline void pcg_mcg_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
476 = pcg_advance_lcg_16(rng->state, delta, PCG_DEFAULT_MULTIPLIER_16, 0u);
479 inline void pcg_unique_16_step_r(struct pcg_state_16* rng)
481 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16
482 + (uint16_t)(((intptr_t)rng) | 1u);
485 inline void pcg_unique_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
488 = pcg_advance_lcg_16(rng->state, delta, PCG_DEFAULT_MULTIPLIER_16,
489 (uint16_t)(((intptr_t)rng) | 1u));
492 inline void pcg_setseq_16_step_r(struct pcg_state_setseq_16* rng)
494 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16 + rng->inc;
497 inline void pcg_setseq_16_advance_r(struct pcg_state_setseq_16* rng,
500 rng->state = pcg_advance_lcg_16(rng->state, delta,
501 PCG_DEFAULT_MULTIPLIER_16, rng->inc);
504 inline void pcg_oneseq_32_step_r(struct pcg_state_32* rng)
506 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32
507 + PCG_DEFAULT_INCREMENT_32;
510 inline void pcg_oneseq_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
512 rng->state = pcg_advance_lcg_32(
513 rng->state, delta, PCG_DEFAULT_MULTIPLIER_32, PCG_DEFAULT_INCREMENT_32);
516 inline void pcg_mcg_32_step_r(struct pcg_state_32* rng)
518 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32;
521 inline void pcg_mcg_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
524 = pcg_advance_lcg_32(rng->state, delta, PCG_DEFAULT_MULTIPLIER_32, 0u);
527 inline void pcg_unique_32_step_r(struct pcg_state_32* rng)
529 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32
530 + (uint32_t)(((intptr_t)rng) | 1u);
533 inline void pcg_unique_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
536 = pcg_advance_lcg_32(rng->state, delta, PCG_DEFAULT_MULTIPLIER_32,
537 (uint32_t)(((intptr_t)rng) | 1u));
540 inline void pcg_setseq_32_step_r(struct pcg_state_setseq_32* rng)
542 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32 + rng->inc;
545 inline void pcg_setseq_32_advance_r(struct pcg_state_setseq_32* rng,
548 rng->state = pcg_advance_lcg_32(rng->state, delta,
549 PCG_DEFAULT_MULTIPLIER_32, rng->inc);
552 inline void pcg_oneseq_64_step_r(struct pcg_state_64* rng)
554 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64
555 + PCG_DEFAULT_INCREMENT_64;
558 inline void pcg_oneseq_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
560 rng->state = pcg_advance_lcg_64(
561 rng->state, delta, PCG_DEFAULT_MULTIPLIER_64, PCG_DEFAULT_INCREMENT_64);
564 inline void pcg_mcg_64_step_r(struct pcg_state_64* rng)
566 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64;
569 inline void pcg_mcg_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
572 = pcg_advance_lcg_64(rng->state, delta, PCG_DEFAULT_MULTIPLIER_64, 0u);
575 inline void pcg_unique_64_step_r(struct pcg_state_64* rng)
577 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64
578 + (uint64_t)(((intptr_t)rng) | 1u);
581 inline void pcg_unique_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
584 = pcg_advance_lcg_64(rng->state, delta, PCG_DEFAULT_MULTIPLIER_64,
585 (uint64_t)(((intptr_t)rng) | 1u));
588 inline void pcg_setseq_64_step_r(struct pcg_state_setseq_64* rng)
590 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64 + rng->inc;
593 inline void pcg_setseq_64_advance_r(struct pcg_state_setseq_64* rng,
596 rng->state = pcg_advance_lcg_64(rng->state, delta,
597 PCG_DEFAULT_MULTIPLIER_64, rng->inc);
600 #if PCG_HAS_128BIT_OPS
601 inline void pcg_oneseq_128_step_r(struct pcg_state_128* rng)
603 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128
604 + PCG_DEFAULT_INCREMENT_128;
608 #if PCG_HAS_128BIT_OPS
609 inline void pcg_oneseq_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
612 = pcg_advance_lcg_128(rng->state, delta, PCG_DEFAULT_MULTIPLIER_128,
613 PCG_DEFAULT_INCREMENT_128);
617 #if PCG_HAS_128BIT_OPS
618 inline void pcg_mcg_128_step_r(struct pcg_state_128* rng)
620 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128;
624 #if PCG_HAS_128BIT_OPS
625 inline void pcg_mcg_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
627 rng->state = pcg_advance_lcg_128(rng->state, delta,
628 PCG_DEFAULT_MULTIPLIER_128, 0u);
632 #if PCG_HAS_128BIT_OPS
633 inline void pcg_unique_128_step_r(struct pcg_state_128* rng)
635 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128
636 + (pcg128_t)(((intptr_t)rng) | 1u);
640 #if PCG_HAS_128BIT_OPS
641 inline void pcg_unique_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
644 = pcg_advance_lcg_128(rng->state, delta, PCG_DEFAULT_MULTIPLIER_128,
645 (pcg128_t)(((intptr_t)rng) | 1u));
649 #if PCG_HAS_128BIT_OPS
650 inline void pcg_setseq_128_step_r(struct pcg_state_setseq_128* rng)
652 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128 + rng->inc;
656 #if PCG_HAS_128BIT_OPS
657 inline void pcg_setseq_128_advance_r(struct pcg_state_setseq_128* rng,
660 rng->state = pcg_advance_lcg_128(rng->state, delta,
661 PCG_DEFAULT_MULTIPLIER_128, rng->inc);
665 /* Functions to seed the RNG state, one version for each size and each
666 * style. Unlike the step functions, regular users can and should call
670 inline void pcg_oneseq_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
673 pcg_oneseq_8_step_r(rng);
674 rng->state += initstate;
675 pcg_oneseq_8_step_r(rng);
678 inline void pcg_mcg_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
680 rng->state = initstate | 1u;
683 inline void pcg_unique_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
686 pcg_unique_8_step_r(rng);
687 rng->state += initstate;
688 pcg_unique_8_step_r(rng);
691 inline void pcg_setseq_8_srandom_r(struct pcg_state_setseq_8* rng,
692 uint8_t initstate, uint8_t initseq)
695 rng->inc = (initseq << 1u) | 1u;
696 pcg_setseq_8_step_r(rng);
697 rng->state += initstate;
698 pcg_setseq_8_step_r(rng);
701 inline void pcg_oneseq_16_srandom_r(struct pcg_state_16* rng,
705 pcg_oneseq_16_step_r(rng);
706 rng->state += initstate;
707 pcg_oneseq_16_step_r(rng);
710 inline void pcg_mcg_16_srandom_r(struct pcg_state_16* rng, uint16_t initstate)
712 rng->state = initstate | 1u;
715 inline void pcg_unique_16_srandom_r(struct pcg_state_16* rng,
719 pcg_unique_16_step_r(rng);
720 rng->state += initstate;
721 pcg_unique_16_step_r(rng);
724 inline void pcg_setseq_16_srandom_r(struct pcg_state_setseq_16* rng,
725 uint16_t initstate, uint16_t initseq)
728 rng->inc = (initseq << 1u) | 1u;
729 pcg_setseq_16_step_r(rng);
730 rng->state += initstate;
731 pcg_setseq_16_step_r(rng);
734 inline void pcg_oneseq_32_srandom_r(struct pcg_state_32* rng,
738 pcg_oneseq_32_step_r(rng);
739 rng->state += initstate;
740 pcg_oneseq_32_step_r(rng);
743 inline void pcg_mcg_32_srandom_r(struct pcg_state_32* rng, uint32_t initstate)
745 rng->state = initstate | 1u;
748 inline void pcg_unique_32_srandom_r(struct pcg_state_32* rng,
752 pcg_unique_32_step_r(rng);
753 rng->state += initstate;
754 pcg_unique_32_step_r(rng);
757 inline void pcg_setseq_32_srandom_r(struct pcg_state_setseq_32* rng,
758 uint32_t initstate, uint32_t initseq)
761 rng->inc = (initseq << 1u) | 1u;
762 pcg_setseq_32_step_r(rng);
763 rng->state += initstate;
764 pcg_setseq_32_step_r(rng);
767 inline void pcg_oneseq_64_srandom_r(struct pcg_state_64* rng,
771 pcg_oneseq_64_step_r(rng);
772 rng->state += initstate;
773 pcg_oneseq_64_step_r(rng);
776 inline void pcg_mcg_64_srandom_r(struct pcg_state_64* rng, uint64_t initstate)
778 rng->state = initstate | 1u;
781 inline void pcg_unique_64_srandom_r(struct pcg_state_64* rng,
785 pcg_unique_64_step_r(rng);
786 rng->state += initstate;
787 pcg_unique_64_step_r(rng);
790 inline void pcg_setseq_64_srandom_r(struct pcg_state_setseq_64* rng,
791 uint64_t initstate, uint64_t initseq)
794 rng->inc = (initseq << 1u) | 1u;
795 pcg_setseq_64_step_r(rng);
796 rng->state += initstate;
797 pcg_setseq_64_step_r(rng);
800 #if PCG_HAS_128BIT_OPS
801 inline void pcg_oneseq_128_srandom_r(struct pcg_state_128* rng,
805 pcg_oneseq_128_step_r(rng);
806 rng->state += initstate;
807 pcg_oneseq_128_step_r(rng);
811 #if PCG_HAS_128BIT_OPS
812 inline void pcg_mcg_128_srandom_r(struct pcg_state_128* rng, pcg128_t initstate)
814 rng->state = initstate | 1u;
818 #if PCG_HAS_128BIT_OPS
819 inline void pcg_unique_128_srandom_r(struct pcg_state_128* rng,
823 pcg_unique_128_step_r(rng);
824 rng->state += initstate;
825 pcg_unique_128_step_r(rng);
829 #if PCG_HAS_128BIT_OPS
830 inline void pcg_setseq_128_srandom_r(struct pcg_state_setseq_128* rng,
831 pcg128_t initstate, pcg128_t initseq)
834 rng->inc = (initseq << 1u) | 1u;
835 pcg_setseq_128_step_r(rng);
836 rng->state += initstate;
837 pcg_setseq_128_step_r(rng);
841 /* Now, finally we create each of the individual generators. We provide
842 * a random_r function that provides a random number of the appropriate
843 * type (using the full range of the type) and a boundedrand_r version
846 * Implementation notes for boundedrand_r:
848 * To avoid bias, we need to make the range of the RNG a multiple of
849 * bound, which we do by dropping output less than a threshold.
850 * Let's consider a 32-bit case... A naive scheme to calculate the
851 * threshold would be to do
853 * uint32_t threshold = 0x100000000ull % bound;
855 * but 64-bit div/mod is slower than 32-bit div/mod (especially on
856 * 32-bit platforms). In essence, we do
858 * uint32_t threshold = (0x100000000ull-bound) % bound;
860 * because this version will calculate the same modulus, but the LHS
861 * value is less than 2^32.
863 * (Note that using modulo is only wise for good RNGs, poorer RNGs
864 * such as raw LCGs do better using a technique based on division.)
865 * Empricical tests show that division is preferable to modulus for
866 * reducting the range of an RNG. It's faster, and sometimes it can
867 * even be statistically prefereable.
870 /* Generation functions for XSH RS */
872 inline uint8_t pcg_oneseq_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
874 uint16_t oldstate = rng->state;
875 pcg_oneseq_16_step_r(rng);
876 return pcg_output_xsh_rs_16_8(oldstate);
879 inline uint8_t pcg_oneseq_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
882 uint8_t threshold = ((uint8_t)(-bound)) % bound;
884 uint8_t r = pcg_oneseq_16_xsh_rs_8_random_r(rng);
890 inline uint16_t pcg_oneseq_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
892 uint32_t oldstate = rng->state;
893 pcg_oneseq_32_step_r(rng);
894 return pcg_output_xsh_rs_32_16(oldstate);
897 inline uint16_t pcg_oneseq_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
900 uint16_t threshold = ((uint16_t)(-bound)) % bound;
902 uint16_t r = pcg_oneseq_32_xsh_rs_16_random_r(rng);
908 inline uint32_t pcg_oneseq_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
910 uint64_t oldstate = rng->state;
911 pcg_oneseq_64_step_r(rng);
912 return pcg_output_xsh_rs_64_32(oldstate);
915 inline uint32_t pcg_oneseq_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
918 uint32_t threshold = -bound % bound;
920 uint32_t r = pcg_oneseq_64_xsh_rs_32_random_r(rng);
926 #if PCG_HAS_128BIT_OPS
927 inline uint64_t pcg_oneseq_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
929 pcg_oneseq_128_step_r(rng);
930 return pcg_output_xsh_rs_128_64(rng->state);
934 #if PCG_HAS_128BIT_OPS
936 pcg_oneseq_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
939 uint64_t threshold = -bound % bound;
941 uint64_t r = pcg_oneseq_128_xsh_rs_64_random_r(rng);
948 inline uint8_t pcg_unique_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
950 uint16_t oldstate = rng->state;
951 pcg_unique_16_step_r(rng);
952 return pcg_output_xsh_rs_16_8(oldstate);
955 inline uint8_t pcg_unique_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
958 uint8_t threshold = ((uint8_t)(-bound)) % bound;
960 uint8_t r = pcg_unique_16_xsh_rs_8_random_r(rng);
966 inline uint16_t pcg_unique_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
968 uint32_t oldstate = rng->state;
969 pcg_unique_32_step_r(rng);
970 return pcg_output_xsh_rs_32_16(oldstate);
973 inline uint16_t pcg_unique_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
976 uint16_t threshold = ((uint16_t)(-bound)) % bound;
978 uint16_t r = pcg_unique_32_xsh_rs_16_random_r(rng);
984 inline uint32_t pcg_unique_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
986 uint64_t oldstate = rng->state;
987 pcg_unique_64_step_r(rng);
988 return pcg_output_xsh_rs_64_32(oldstate);
991 inline uint32_t pcg_unique_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
994 uint32_t threshold = -bound % bound;
996 uint32_t r = pcg_unique_64_xsh_rs_32_random_r(rng);
1002 #if PCG_HAS_128BIT_OPS
1003 inline uint64_t pcg_unique_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
1005 pcg_unique_128_step_r(rng);
1006 return pcg_output_xsh_rs_128_64(rng->state);
1010 #if PCG_HAS_128BIT_OPS
1012 pcg_unique_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
1015 uint64_t threshold = -bound % bound;
1017 uint64_t r = pcg_unique_128_xsh_rs_64_random_r(rng);
1024 inline uint8_t pcg_setseq_16_xsh_rs_8_random_r(struct pcg_state_setseq_16* rng)
1026 uint16_t oldstate = rng->state;
1027 pcg_setseq_16_step_r(rng);
1028 return pcg_output_xsh_rs_16_8(oldstate);
1032 pcg_setseq_16_xsh_rs_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1035 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1037 uint8_t r = pcg_setseq_16_xsh_rs_8_random_r(rng);
1044 pcg_setseq_32_xsh_rs_16_random_r(struct pcg_state_setseq_32* rng)
1046 uint32_t oldstate = rng->state;
1047 pcg_setseq_32_step_r(rng);
1048 return pcg_output_xsh_rs_32_16(oldstate);
1052 pcg_setseq_32_xsh_rs_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1055 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1057 uint16_t r = pcg_setseq_32_xsh_rs_16_random_r(rng);
1064 pcg_setseq_64_xsh_rs_32_random_r(struct pcg_state_setseq_64* rng)
1066 uint64_t oldstate = rng->state;
1067 pcg_setseq_64_step_r(rng);
1068 return pcg_output_xsh_rs_64_32(oldstate);
1072 pcg_setseq_64_xsh_rs_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1075 uint32_t threshold = -bound % bound;
1077 uint32_t r = pcg_setseq_64_xsh_rs_32_random_r(rng);
1083 #if PCG_HAS_128BIT_OPS
1085 pcg_setseq_128_xsh_rs_64_random_r(struct pcg_state_setseq_128* rng)
1087 pcg_setseq_128_step_r(rng);
1088 return pcg_output_xsh_rs_128_64(rng->state);
1092 #if PCG_HAS_128BIT_OPS
1094 pcg_setseq_128_xsh_rs_64_boundedrand_r(struct pcg_state_setseq_128* rng,
1097 uint64_t threshold = -bound % bound;
1099 uint64_t r = pcg_setseq_128_xsh_rs_64_random_r(rng);
1106 inline uint8_t pcg_mcg_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
1108 uint16_t oldstate = rng->state;
1109 pcg_mcg_16_step_r(rng);
1110 return pcg_output_xsh_rs_16_8(oldstate);
1113 inline uint8_t pcg_mcg_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
1116 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1118 uint8_t r = pcg_mcg_16_xsh_rs_8_random_r(rng);
1124 inline uint16_t pcg_mcg_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
1126 uint32_t oldstate = rng->state;
1127 pcg_mcg_32_step_r(rng);
1128 return pcg_output_xsh_rs_32_16(oldstate);
1131 inline uint16_t pcg_mcg_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
1134 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1136 uint16_t r = pcg_mcg_32_xsh_rs_16_random_r(rng);
1142 inline uint32_t pcg_mcg_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
1144 uint64_t oldstate = rng->state;
1145 pcg_mcg_64_step_r(rng);
1146 return pcg_output_xsh_rs_64_32(oldstate);
1149 inline uint32_t pcg_mcg_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
1152 uint32_t threshold = -bound % bound;
1154 uint32_t r = pcg_mcg_64_xsh_rs_32_random_r(rng);
1160 #if PCG_HAS_128BIT_OPS
1161 inline uint64_t pcg_mcg_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
1163 pcg_mcg_128_step_r(rng);
1164 return pcg_output_xsh_rs_128_64(rng->state);
1168 #if PCG_HAS_128BIT_OPS
1169 inline uint64_t pcg_mcg_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
1172 uint64_t threshold = -bound % bound;
1174 uint64_t r = pcg_mcg_128_xsh_rs_64_random_r(rng);
1181 /* Generation functions for XSH RR */
1183 inline uint8_t pcg_oneseq_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1185 uint16_t oldstate = rng->state;
1186 pcg_oneseq_16_step_r(rng);
1187 return pcg_output_xsh_rr_16_8(oldstate);
1190 inline uint8_t pcg_oneseq_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1193 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1195 uint8_t r = pcg_oneseq_16_xsh_rr_8_random_r(rng);
1201 inline uint16_t pcg_oneseq_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1203 uint32_t oldstate = rng->state;
1204 pcg_oneseq_32_step_r(rng);
1205 return pcg_output_xsh_rr_32_16(oldstate);
1208 inline uint16_t pcg_oneseq_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1211 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1213 uint16_t r = pcg_oneseq_32_xsh_rr_16_random_r(rng);
1219 inline uint32_t pcg_oneseq_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1221 uint64_t oldstate = rng->state;
1222 pcg_oneseq_64_step_r(rng);
1223 return pcg_output_xsh_rr_64_32(oldstate);
1226 inline uint32_t pcg_oneseq_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1229 uint32_t threshold = -bound % bound;
1231 uint32_t r = pcg_oneseq_64_xsh_rr_32_random_r(rng);
1237 #if PCG_HAS_128BIT_OPS
1238 inline uint64_t pcg_oneseq_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1240 pcg_oneseq_128_step_r(rng);
1241 return pcg_output_xsh_rr_128_64(rng->state);
1245 #if PCG_HAS_128BIT_OPS
1247 pcg_oneseq_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1250 uint64_t threshold = -bound % bound;
1252 uint64_t r = pcg_oneseq_128_xsh_rr_64_random_r(rng);
1259 inline uint8_t pcg_unique_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1261 uint16_t oldstate = rng->state;
1262 pcg_unique_16_step_r(rng);
1263 return pcg_output_xsh_rr_16_8(oldstate);
1266 inline uint8_t pcg_unique_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1269 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1271 uint8_t r = pcg_unique_16_xsh_rr_8_random_r(rng);
1277 inline uint16_t pcg_unique_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1279 uint32_t oldstate = rng->state;
1280 pcg_unique_32_step_r(rng);
1281 return pcg_output_xsh_rr_32_16(oldstate);
1284 inline uint16_t pcg_unique_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1287 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1289 uint16_t r = pcg_unique_32_xsh_rr_16_random_r(rng);
1295 inline uint32_t pcg_unique_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1297 uint64_t oldstate = rng->state;
1298 pcg_unique_64_step_r(rng);
1299 return pcg_output_xsh_rr_64_32(oldstate);
1302 inline uint32_t pcg_unique_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1305 uint32_t threshold = -bound % bound;
1307 uint32_t r = pcg_unique_64_xsh_rr_32_random_r(rng);
1313 #if PCG_HAS_128BIT_OPS
1314 inline uint64_t pcg_unique_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1316 pcg_unique_128_step_r(rng);
1317 return pcg_output_xsh_rr_128_64(rng->state);
1321 #if PCG_HAS_128BIT_OPS
1323 pcg_unique_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1326 uint64_t threshold = -bound % bound;
1328 uint64_t r = pcg_unique_128_xsh_rr_64_random_r(rng);
1335 inline uint8_t pcg_setseq_16_xsh_rr_8_random_r(struct pcg_state_setseq_16* rng)
1337 uint16_t oldstate = rng->state;
1338 pcg_setseq_16_step_r(rng);
1339 return pcg_output_xsh_rr_16_8(oldstate);
1343 pcg_setseq_16_xsh_rr_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1346 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1348 uint8_t r = pcg_setseq_16_xsh_rr_8_random_r(rng);
1355 pcg_setseq_32_xsh_rr_16_random_r(struct pcg_state_setseq_32* rng)
1357 uint32_t oldstate = rng->state;
1358 pcg_setseq_32_step_r(rng);
1359 return pcg_output_xsh_rr_32_16(oldstate);
1363 pcg_setseq_32_xsh_rr_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1366 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1368 uint16_t r = pcg_setseq_32_xsh_rr_16_random_r(rng);
1375 pcg_setseq_64_xsh_rr_32_random_r(struct pcg_state_setseq_64* rng)
1377 uint64_t oldstate = rng->state;
1378 pcg_setseq_64_step_r(rng);
1379 return pcg_output_xsh_rr_64_32(oldstate);
1383 pcg_setseq_64_xsh_rr_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1386 uint32_t threshold = -bound % bound;
1388 uint32_t r = pcg_setseq_64_xsh_rr_32_random_r(rng);
1394 #if PCG_HAS_128BIT_OPS
1396 pcg_setseq_128_xsh_rr_64_random_r(struct pcg_state_setseq_128* rng)
1398 pcg_setseq_128_step_r(rng);
1399 return pcg_output_xsh_rr_128_64(rng->state);
1403 #if PCG_HAS_128BIT_OPS
1405 pcg_setseq_128_xsh_rr_64_boundedrand_r(struct pcg_state_setseq_128* rng,
1408 uint64_t threshold = -bound % bound;
1410 uint64_t r = pcg_setseq_128_xsh_rr_64_random_r(rng);
1417 inline uint8_t pcg_mcg_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1419 uint16_t oldstate = rng->state;
1420 pcg_mcg_16_step_r(rng);
1421 return pcg_output_xsh_rr_16_8(oldstate);
1424 inline uint8_t pcg_mcg_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1427 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1429 uint8_t r = pcg_mcg_16_xsh_rr_8_random_r(rng);
1435 inline uint16_t pcg_mcg_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1437 uint32_t oldstate = rng->state;
1438 pcg_mcg_32_step_r(rng);
1439 return pcg_output_xsh_rr_32_16(oldstate);
1442 inline uint16_t pcg_mcg_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1445 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1447 uint16_t r = pcg_mcg_32_xsh_rr_16_random_r(rng);
1453 inline uint32_t pcg_mcg_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1455 uint64_t oldstate = rng->state;
1456 pcg_mcg_64_step_r(rng);
1457 return pcg_output_xsh_rr_64_32(oldstate);
1460 inline uint32_t pcg_mcg_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1463 uint32_t threshold = -bound % bound;
1465 uint32_t r = pcg_mcg_64_xsh_rr_32_random_r(rng);
1471 #if PCG_HAS_128BIT_OPS
1472 inline uint64_t pcg_mcg_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1474 pcg_mcg_128_step_r(rng);
1475 return pcg_output_xsh_rr_128_64(rng->state);
1479 #if PCG_HAS_128BIT_OPS
1480 inline uint64_t pcg_mcg_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1483 uint64_t threshold = -bound % bound;
1485 uint64_t r = pcg_mcg_128_xsh_rr_64_random_r(rng);
1492 /* Generation functions for RXS M XS (no MCG versions because they
1493 * don't make sense when you want to use the entire state)
1496 inline uint8_t pcg_oneseq_8_rxs_m_xs_8_random_r(struct pcg_state_8* rng)
1498 uint8_t oldstate = rng->state;
1499 pcg_oneseq_8_step_r(rng);
1500 return pcg_output_rxs_m_xs_8_8(oldstate);
1503 inline uint8_t pcg_oneseq_8_rxs_m_xs_8_boundedrand_r(struct pcg_state_8* rng,
1506 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1508 uint8_t r = pcg_oneseq_8_rxs_m_xs_8_random_r(rng);
1514 inline uint16_t pcg_oneseq_16_rxs_m_xs_16_random_r(struct pcg_state_16* rng)
1516 uint16_t oldstate = rng->state;
1517 pcg_oneseq_16_step_r(rng);
1518 return pcg_output_rxs_m_xs_16_16(oldstate);
1522 pcg_oneseq_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_16* rng,
1525 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1527 uint16_t r = pcg_oneseq_16_rxs_m_xs_16_random_r(rng);
1533 inline uint32_t pcg_oneseq_32_rxs_m_xs_32_random_r(struct pcg_state_32* rng)
1535 uint32_t oldstate = rng->state;
1536 pcg_oneseq_32_step_r(rng);
1537 return pcg_output_rxs_m_xs_32_32(oldstate);
1541 pcg_oneseq_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_32* rng,
1544 uint32_t threshold = -bound % bound;
1546 uint32_t r = pcg_oneseq_32_rxs_m_xs_32_random_r(rng);
1552 inline uint64_t pcg_oneseq_64_rxs_m_xs_64_random_r(struct pcg_state_64* rng)
1554 uint64_t oldstate = rng->state;
1555 pcg_oneseq_64_step_r(rng);
1556 return pcg_output_rxs_m_xs_64_64(oldstate);
1560 pcg_oneseq_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_64* rng,
1563 uint64_t threshold = -bound % bound;
1565 uint64_t r = pcg_oneseq_64_rxs_m_xs_64_random_r(rng);
1571 #if PCG_HAS_128BIT_OPS
1572 inline pcg128_t pcg_oneseq_128_rxs_m_xs_128_random_r(struct pcg_state_128* rng)
1574 pcg_oneseq_128_step_r(rng);
1575 return pcg_output_rxs_m_xs_128_128(rng->state);
1579 #if PCG_HAS_128BIT_OPS
1581 pcg_oneseq_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_128* rng,
1584 pcg128_t threshold = -bound % bound;
1586 pcg128_t r = pcg_oneseq_128_rxs_m_xs_128_random_r(rng);
1593 inline uint16_t pcg_unique_16_rxs_m_xs_16_random_r(struct pcg_state_16* rng)
1595 uint16_t oldstate = rng->state;
1596 pcg_unique_16_step_r(rng);
1597 return pcg_output_rxs_m_xs_16_16(oldstate);
1601 pcg_unique_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_16* rng,
1604 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1606 uint16_t r = pcg_unique_16_rxs_m_xs_16_random_r(rng);
1612 inline uint32_t pcg_unique_32_rxs_m_xs_32_random_r(struct pcg_state_32* rng)
1614 uint32_t oldstate = rng->state;
1615 pcg_unique_32_step_r(rng);
1616 return pcg_output_rxs_m_xs_32_32(oldstate);
1620 pcg_unique_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_32* rng,
1623 uint32_t threshold = -bound % bound;
1625 uint32_t r = pcg_unique_32_rxs_m_xs_32_random_r(rng);
1631 inline uint64_t pcg_unique_64_rxs_m_xs_64_random_r(struct pcg_state_64* rng)
1633 uint64_t oldstate = rng->state;
1634 pcg_unique_64_step_r(rng);
1635 return pcg_output_rxs_m_xs_64_64(oldstate);
1639 pcg_unique_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_64* rng,
1642 uint64_t threshold = -bound % bound;
1644 uint64_t r = pcg_unique_64_rxs_m_xs_64_random_r(rng);
1650 #if PCG_HAS_128BIT_OPS
1651 inline pcg128_t pcg_unique_128_rxs_m_xs_128_random_r(struct pcg_state_128* rng)
1653 pcg_unique_128_step_r(rng);
1654 return pcg_output_rxs_m_xs_128_128(rng->state);
1658 #if PCG_HAS_128BIT_OPS
1660 pcg_unique_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_128* rng,
1663 pcg128_t threshold = -bound % bound;
1665 pcg128_t r = pcg_unique_128_rxs_m_xs_128_random_r(rng);
1672 inline uint8_t pcg_setseq_8_rxs_m_xs_8_random_r(struct pcg_state_setseq_8* rng)
1674 uint8_t oldstate = rng->state;
1675 pcg_setseq_8_step_r(rng);
1676 return pcg_output_rxs_m_xs_8_8(oldstate);
1680 pcg_setseq_8_rxs_m_xs_8_boundedrand_r(struct pcg_state_setseq_8* rng,
1683 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1685 uint8_t r = pcg_setseq_8_rxs_m_xs_8_random_r(rng);
1692 pcg_setseq_16_rxs_m_xs_16_random_r(struct pcg_state_setseq_16* rng)
1694 uint16_t oldstate = rng->state;
1695 pcg_setseq_16_step_r(rng);
1696 return pcg_output_rxs_m_xs_16_16(oldstate);
1700 pcg_setseq_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_setseq_16* rng,
1703 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1705 uint16_t r = pcg_setseq_16_rxs_m_xs_16_random_r(rng);
1712 pcg_setseq_32_rxs_m_xs_32_random_r(struct pcg_state_setseq_32* rng)
1714 uint32_t oldstate = rng->state;
1715 pcg_setseq_32_step_r(rng);
1716 return pcg_output_rxs_m_xs_32_32(oldstate);
1720 pcg_setseq_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_setseq_32* rng,
1723 uint32_t threshold = -bound % bound;
1725 uint32_t r = pcg_setseq_32_rxs_m_xs_32_random_r(rng);
1732 pcg_setseq_64_rxs_m_xs_64_random_r(struct pcg_state_setseq_64* rng)
1734 uint64_t oldstate = rng->state;
1735 pcg_setseq_64_step_r(rng);
1736 return pcg_output_rxs_m_xs_64_64(oldstate);
1740 pcg_setseq_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_setseq_64* rng,
1743 uint64_t threshold = -bound % bound;
1745 uint64_t r = pcg_setseq_64_rxs_m_xs_64_random_r(rng);
1751 #if PCG_HAS_128BIT_OPS
1753 pcg_setseq_128_rxs_m_xs_128_random_r(struct pcg_state_setseq_128* rng)
1755 pcg_setseq_128_step_r(rng);
1756 return pcg_output_rxs_m_xs_128_128(rng->state);
1760 #if PCG_HAS_128BIT_OPS
1762 pcg_setseq_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_setseq_128* rng,
1765 pcg128_t threshold = -bound % bound;
1767 pcg128_t r = pcg_setseq_128_rxs_m_xs_128_random_r(rng);
1774 /* Generation functions for RXS M */
1776 inline uint8_t pcg_oneseq_16_rxs_m_8_random_r(struct pcg_state_16* rng)
1778 uint16_t oldstate = rng->state;
1779 pcg_oneseq_16_step_r(rng);
1780 return pcg_output_rxs_m_16_8(oldstate);
1783 inline uint8_t pcg_oneseq_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
1786 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1788 uint8_t r = pcg_oneseq_16_rxs_m_8_random_r(rng);
1794 inline uint16_t pcg_oneseq_32_rxs_m_16_random_r(struct pcg_state_32* rng)
1796 uint32_t oldstate = rng->state;
1797 pcg_oneseq_32_step_r(rng);
1798 return pcg_output_rxs_m_32_16(oldstate);
1801 inline uint16_t pcg_oneseq_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
1804 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1806 uint16_t r = pcg_oneseq_32_rxs_m_16_random_r(rng);
1812 inline uint32_t pcg_oneseq_64_rxs_m_32_random_r(struct pcg_state_64* rng)
1814 uint64_t oldstate = rng->state;
1815 pcg_oneseq_64_step_r(rng);
1816 return pcg_output_rxs_m_64_32(oldstate);
1819 inline uint32_t pcg_oneseq_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
1822 uint32_t threshold = -bound % bound;
1824 uint32_t r = pcg_oneseq_64_rxs_m_32_random_r(rng);
1830 #if PCG_HAS_128BIT_OPS
1831 inline uint64_t pcg_oneseq_128_rxs_m_64_random_r(struct pcg_state_128* rng)
1833 pcg_oneseq_128_step_r(rng);
1834 return pcg_output_rxs_m_128_64(rng->state);
1838 #if PCG_HAS_128BIT_OPS
1839 inline uint64_t pcg_oneseq_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
1842 uint64_t threshold = -bound % bound;
1844 uint64_t r = pcg_oneseq_128_rxs_m_64_random_r(rng);
1851 inline uint8_t pcg_unique_16_rxs_m_8_random_r(struct pcg_state_16* rng)
1853 uint16_t oldstate = rng->state;
1854 pcg_unique_16_step_r(rng);
1855 return pcg_output_rxs_m_16_8(oldstate);
1858 inline uint8_t pcg_unique_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
1861 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1863 uint8_t r = pcg_unique_16_rxs_m_8_random_r(rng);
1869 inline uint16_t pcg_unique_32_rxs_m_16_random_r(struct pcg_state_32* rng)
1871 uint32_t oldstate = rng->state;
1872 pcg_unique_32_step_r(rng);
1873 return pcg_output_rxs_m_32_16(oldstate);
1876 inline uint16_t pcg_unique_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
1879 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1881 uint16_t r = pcg_unique_32_rxs_m_16_random_r(rng);
1887 inline uint32_t pcg_unique_64_rxs_m_32_random_r(struct pcg_state_64* rng)
1889 uint64_t oldstate = rng->state;
1890 pcg_unique_64_step_r(rng);
1891 return pcg_output_rxs_m_64_32(oldstate);
1894 inline uint32_t pcg_unique_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
1897 uint32_t threshold = -bound % bound;
1899 uint32_t r = pcg_unique_64_rxs_m_32_random_r(rng);
1905 #if PCG_HAS_128BIT_OPS
1906 inline uint64_t pcg_unique_128_rxs_m_64_random_r(struct pcg_state_128* rng)
1908 pcg_unique_128_step_r(rng);
1909 return pcg_output_rxs_m_128_64(rng->state);
1913 #if PCG_HAS_128BIT_OPS
1914 inline uint64_t pcg_unique_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
1917 uint64_t threshold = -bound % bound;
1919 uint64_t r = pcg_unique_128_rxs_m_64_random_r(rng);
1926 inline uint8_t pcg_setseq_16_rxs_m_8_random_r(struct pcg_state_setseq_16* rng)
1928 uint16_t oldstate = rng->state;
1929 pcg_setseq_16_step_r(rng);
1930 return pcg_output_rxs_m_16_8(oldstate);
1934 pcg_setseq_16_rxs_m_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1937 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1939 uint8_t r = pcg_setseq_16_rxs_m_8_random_r(rng);
1945 inline uint16_t pcg_setseq_32_rxs_m_16_random_r(struct pcg_state_setseq_32* rng)
1947 uint32_t oldstate = rng->state;
1948 pcg_setseq_32_step_r(rng);
1949 return pcg_output_rxs_m_32_16(oldstate);
1953 pcg_setseq_32_rxs_m_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1956 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1958 uint16_t r = pcg_setseq_32_rxs_m_16_random_r(rng);
1964 inline uint32_t pcg_setseq_64_rxs_m_32_random_r(struct pcg_state_setseq_64* rng)
1966 uint64_t oldstate = rng->state;
1967 pcg_setseq_64_step_r(rng);
1968 return pcg_output_rxs_m_64_32(oldstate);
1972 pcg_setseq_64_rxs_m_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1975 uint32_t threshold = -bound % bound;
1977 uint32_t r = pcg_setseq_64_rxs_m_32_random_r(rng);
1983 #if PCG_HAS_128BIT_OPS
1985 pcg_setseq_128_rxs_m_64_random_r(struct pcg_state_setseq_128* rng)
1987 pcg_setseq_128_step_r(rng);
1988 return pcg_output_rxs_m_128_64(rng->state);
1992 #if PCG_HAS_128BIT_OPS
1994 pcg_setseq_128_rxs_m_64_boundedrand_r(struct pcg_state_setseq_128* rng,
1997 uint64_t threshold = -bound % bound;
1999 uint64_t r = pcg_setseq_128_rxs_m_64_random_r(rng);
2006 inline uint8_t pcg_mcg_16_rxs_m_8_random_r(struct pcg_state_16* rng)
2008 uint16_t oldstate = rng->state;
2009 pcg_mcg_16_step_r(rng);
2010 return pcg_output_rxs_m_16_8(oldstate);
2013 inline uint8_t pcg_mcg_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
2016 uint8_t threshold = ((uint8_t)(-bound)) % bound;
2018 uint8_t r = pcg_mcg_16_rxs_m_8_random_r(rng);
2024 inline uint16_t pcg_mcg_32_rxs_m_16_random_r(struct pcg_state_32* rng)
2026 uint32_t oldstate = rng->state;
2027 pcg_mcg_32_step_r(rng);
2028 return pcg_output_rxs_m_32_16(oldstate);
2031 inline uint16_t pcg_mcg_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
2034 uint16_t threshold = ((uint16_t)(-bound)) % bound;
2036 uint16_t r = pcg_mcg_32_rxs_m_16_random_r(rng);
2042 inline uint32_t pcg_mcg_64_rxs_m_32_random_r(struct pcg_state_64* rng)
2044 uint64_t oldstate = rng->state;
2045 pcg_mcg_64_step_r(rng);
2046 return pcg_output_rxs_m_64_32(oldstate);
2049 inline uint32_t pcg_mcg_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
2052 uint32_t threshold = -bound % bound;
2054 uint32_t r = pcg_mcg_64_rxs_m_32_random_r(rng);
2060 #if PCG_HAS_128BIT_OPS
2061 inline uint64_t pcg_mcg_128_rxs_m_64_random_r(struct pcg_state_128* rng)
2063 pcg_mcg_128_step_r(rng);
2064 return pcg_output_rxs_m_128_64(rng->state);
2068 #if PCG_HAS_128BIT_OPS
2069 inline uint64_t pcg_mcg_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
2072 uint64_t threshold = -bound % bound;
2074 uint64_t r = pcg_mcg_128_rxs_m_64_random_r(rng);
2081 /* Generation functions for XSL RR (only defined for "large" types) */
2083 inline uint32_t pcg_oneseq_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2085 uint64_t oldstate = rng->state;
2086 pcg_oneseq_64_step_r(rng);
2087 return pcg_output_xsl_rr_64_32(oldstate);
2090 inline uint32_t pcg_oneseq_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2093 uint32_t threshold = -bound % bound;
2095 uint32_t r = pcg_oneseq_64_xsl_rr_32_random_r(rng);
2101 #if PCG_HAS_128BIT_OPS
2102 inline uint64_t pcg_oneseq_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2104 pcg_oneseq_128_step_r(rng);
2105 return pcg_output_xsl_rr_128_64(rng->state);
2109 #if PCG_HAS_128BIT_OPS
2111 pcg_oneseq_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2114 uint64_t threshold = -bound % bound;
2116 uint64_t r = pcg_oneseq_128_xsl_rr_64_random_r(rng);
2123 inline uint32_t pcg_unique_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2125 uint64_t oldstate = rng->state;
2126 pcg_unique_64_step_r(rng);
2127 return pcg_output_xsl_rr_64_32(oldstate);
2130 inline uint32_t pcg_unique_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2133 uint32_t threshold = -bound % bound;
2135 uint32_t r = pcg_unique_64_xsl_rr_32_random_r(rng);
2141 #if PCG_HAS_128BIT_OPS
2142 inline uint64_t pcg_unique_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2144 pcg_unique_128_step_r(rng);
2145 return pcg_output_xsl_rr_128_64(rng->state);
2149 #if PCG_HAS_128BIT_OPS
2151 pcg_unique_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2154 uint64_t threshold = -bound % bound;
2156 uint64_t r = pcg_unique_128_xsl_rr_64_random_r(rng);
2164 pcg_setseq_64_xsl_rr_32_random_r(struct pcg_state_setseq_64* rng)
2166 uint64_t oldstate = rng->state;
2167 pcg_setseq_64_step_r(rng);
2168 return pcg_output_xsl_rr_64_32(oldstate);
2172 pcg_setseq_64_xsl_rr_32_boundedrand_r(struct pcg_state_setseq_64* rng,
2175 uint32_t threshold = -bound % bound;
2177 uint32_t r = pcg_setseq_64_xsl_rr_32_random_r(rng);
2183 #if PCG_HAS_128BIT_OPS
2185 pcg_setseq_128_xsl_rr_64_random_r(struct pcg_state_setseq_128* rng)
2187 pcg_setseq_128_step_r(rng);
2188 return pcg_output_xsl_rr_128_64(rng->state);
2192 #if PCG_HAS_128BIT_OPS
2194 pcg_setseq_128_xsl_rr_64_boundedrand_r(struct pcg_state_setseq_128* rng,
2197 uint64_t threshold = -bound % bound;
2199 uint64_t r = pcg_setseq_128_xsl_rr_64_random_r(rng);
2206 inline uint32_t pcg_mcg_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2208 uint64_t oldstate = rng->state;
2209 pcg_mcg_64_step_r(rng);
2210 return pcg_output_xsl_rr_64_32(oldstate);
2213 inline uint32_t pcg_mcg_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2216 uint32_t threshold = -bound % bound;
2218 uint32_t r = pcg_mcg_64_xsl_rr_32_random_r(rng);
2224 #if PCG_HAS_128BIT_OPS
2225 inline uint64_t pcg_mcg_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2227 pcg_mcg_128_step_r(rng);
2228 return pcg_output_xsl_rr_128_64(rng->state);
2232 #if PCG_HAS_128BIT_OPS
2233 inline uint64_t pcg_mcg_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2236 uint64_t threshold = -bound % bound;
2238 uint64_t r = pcg_mcg_128_xsl_rr_64_random_r(rng);
2245 /* Generation functions for XSL RR RR (only defined for "large" types) */
2247 inline uint64_t pcg_oneseq_64_xsl_rr_rr_64_random_r(struct pcg_state_64* rng)
2249 uint64_t oldstate = rng->state;
2250 pcg_oneseq_64_step_r(rng);
2251 return pcg_output_xsl_rr_rr_64_64(oldstate);
2255 pcg_oneseq_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_64* rng,
2258 uint64_t threshold = -bound % bound;
2260 uint64_t r = pcg_oneseq_64_xsl_rr_rr_64_random_r(rng);
2266 #if PCG_HAS_128BIT_OPS
2267 inline pcg128_t pcg_oneseq_128_xsl_rr_rr_128_random_r(struct pcg_state_128* rng)
2269 pcg_oneseq_128_step_r(rng);
2270 return pcg_output_xsl_rr_rr_128_128(rng->state);
2274 #if PCG_HAS_128BIT_OPS
2276 pcg_oneseq_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_128* rng,
2279 pcg128_t threshold = -bound % bound;
2281 pcg128_t r = pcg_oneseq_128_xsl_rr_rr_128_random_r(rng);
2288 inline uint64_t pcg_unique_64_xsl_rr_rr_64_random_r(struct pcg_state_64* rng)
2290 uint64_t oldstate = rng->state;
2291 pcg_unique_64_step_r(rng);
2292 return pcg_output_xsl_rr_rr_64_64(oldstate);
2296 pcg_unique_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_64* rng,
2299 uint64_t threshold = -bound % bound;
2301 uint64_t r = pcg_unique_64_xsl_rr_rr_64_random_r(rng);
2307 #if PCG_HAS_128BIT_OPS
2308 inline pcg128_t pcg_unique_128_xsl_rr_rr_128_random_r(struct pcg_state_128* rng)
2310 pcg_unique_128_step_r(rng);
2311 return pcg_output_xsl_rr_rr_128_128(rng->state);
2315 #if PCG_HAS_128BIT_OPS
2317 pcg_unique_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_128* rng,
2320 pcg128_t threshold = -bound % bound;
2322 pcg128_t r = pcg_unique_128_xsl_rr_rr_128_random_r(rng);
2330 pcg_setseq_64_xsl_rr_rr_64_random_r(struct pcg_state_setseq_64* rng)
2332 uint64_t oldstate = rng->state;
2333 pcg_setseq_64_step_r(rng);
2334 return pcg_output_xsl_rr_rr_64_64(oldstate);
2338 pcg_setseq_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_setseq_64* rng,
2341 uint64_t threshold = -bound % bound;
2343 uint64_t r = pcg_setseq_64_xsl_rr_rr_64_random_r(rng);
2349 #if PCG_HAS_128BIT_OPS
2351 pcg_setseq_128_xsl_rr_rr_128_random_r(struct pcg_state_setseq_128* rng)
2353 pcg_setseq_128_step_r(rng);
2354 return pcg_output_xsl_rr_rr_128_128(rng->state);
2358 #if PCG_HAS_128BIT_OPS
2360 pcg_setseq_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_setseq_128* rng,
2363 pcg128_t threshold = -bound % bound;
2365 pcg128_t r = pcg_setseq_128_xsl_rr_rr_128_random_r(rng);
2373 typedef struct pcg_state_setseq_64 pcg32_random_t;
2374 typedef struct pcg_state_64 pcg32s_random_t;
2375 typedef struct pcg_state_64 pcg32u_random_t;
2376 typedef struct pcg_state_64 pcg32f_random_t;
2378 #define pcg32_random_r pcg_setseq_64_xsh_rr_32_random_r
2379 #define pcg32s_random_r pcg_oneseq_64_xsh_rr_32_random_r
2380 #define pcg32u_random_r pcg_unique_64_xsh_rr_32_random_r
2381 #define pcg32f_random_r pcg_mcg_64_xsh_rs_32_random_r
2382 /*** boundedrand_r */
2383 #define pcg32_boundedrand_r pcg_setseq_64_xsh_rr_32_boundedrand_r
2384 #define pcg32s_boundedrand_r pcg_oneseq_64_xsh_rr_32_boundedrand_r
2385 #define pcg32u_boundedrand_r pcg_unique_64_xsh_rr_32_boundedrand_r
2386 #define pcg32f_boundedrand_r pcg_mcg_64_xsh_rs_32_boundedrand_r
2388 #define pcg32_srandom_r pcg_setseq_64_srandom_r
2389 #define pcg32s_srandom_r pcg_oneseq_64_srandom_r
2390 #define pcg32u_srandom_r pcg_unique_64_srandom_r
2391 #define pcg32f_srandom_r pcg_mcg_64_srandom_r
2393 #define pcg32_advance_r pcg_setseq_64_advance_r
2394 #define pcg32s_advance_r pcg_oneseq_64_advance_r
2395 #define pcg32u_advance_r pcg_unique_64_advance_r
2396 #define pcg32f_advance_r pcg_mcg_64_advance_r
2398 #if PCG_HAS_128BIT_OPS
2400 typedef struct pcg_state_setseq_128 pcg64_random_t;
2401 typedef struct pcg_state_128 pcg64s_random_t;
2402 typedef struct pcg_state_128 pcg64u_random_t;
2403 typedef struct pcg_state_128 pcg64f_random_t;
2405 #define pcg64_random_r pcg_setseq_128_xsl_rr_64_random_r
2406 #define pcg64s_random_r pcg_oneseq_128_xsl_rr_64_random_r
2407 #define pcg64u_random_r pcg_unique_128_xsl_rr_64_random_r
2408 #define pcg64f_random_r pcg_mcg_128_xsl_rr_64_random_r
2409 /*** boundedrand_r */
2410 #define pcg64_boundedrand_r pcg_setseq_128_xsl_rr_64_boundedrand_r
2411 #define pcg64s_boundedrand_r pcg_oneseq_128_xsl_rr_64_boundedrand_r
2412 #define pcg64u_boundedrand_r pcg_unique_128_xsl_rr_64_boundedrand_r
2413 #define pcg64f_boundedrand_r pcg_mcg_128_xsl_rr_64_boundedrand_r
2415 #define pcg64_srandom_r pcg_setseq_128_srandom_r
2416 #define pcg64s_srandom_r pcg_oneseq_128_srandom_r
2417 #define pcg64u_srandom_r pcg_unique_128_srandom_r
2418 #define pcg64f_srandom_r pcg_mcg_128_srandom_r
2420 #define pcg64_advance_r pcg_setseq_128_advance_r
2421 #define pcg64s_advance_r pcg_oneseq_128_advance_r
2422 #define pcg64u_advance_r pcg_unique_128_advance_r
2423 #define pcg64f_advance_r pcg_mcg_128_advance_r
2427 typedef struct pcg_state_8 pcg8si_random_t;
2428 typedef struct pcg_state_16 pcg16si_random_t;
2429 typedef struct pcg_state_32 pcg32si_random_t;
2430 typedef struct pcg_state_64 pcg64si_random_t;
2432 #define pcg8si_random_r pcg_oneseq_8_rxs_m_xs_8_random_r
2433 #define pcg16si_random_r pcg_oneseq_16_rxs_m_xs_16_random_r
2434 #define pcg32si_random_r pcg_oneseq_32_rxs_m_xs_32_random_r
2435 #define pcg64si_random_r pcg_oneseq_64_rxs_m_xs_64_random_r
2436 /*** boundedrand_r */
2437 #define pcg8si_boundedrand_r pcg_oneseq_8_rxs_m_xs_8_boundedrand_r
2438 #define pcg16si_boundedrand_r pcg_oneseq_16_rxs_m_xs_16_boundedrand_r
2439 #define pcg32si_boundedrand_r pcg_oneseq_32_rxs_m_xs_32_boundedrand_r
2440 #define pcg64si_boundedrand_r pcg_oneseq_64_rxs_m_xs_64_boundedrand_r
2442 #define pcg8si_srandom_r pcg_oneseq_8_srandom_r
2443 #define pcg16si_srandom_r pcg_oneseq_16_srandom_r
2444 #define pcg32si_srandom_r pcg_oneseq_32_srandom_r
2445 #define pcg64si_srandom_r pcg_oneseq_64_srandom_r
2447 #define pcg8si_advance_r pcg_oneseq_8_advance_r
2448 #define pcg16si_advance_r pcg_oneseq_16_advance_r
2449 #define pcg32si_advance_r pcg_oneseq_32_advance_r
2450 #define pcg64si_advance_r pcg_oneseq_64_advance_r
2452 #if PCG_HAS_128BIT_OPS
2453 typedef struct pcg_state_128 pcg128si_random_t;
2454 #define pcg128si_random_r pcg_oneseq_128_rxs_m_xs_128_random_r
2455 #define pcg128si_boundedrand_r pcg_oneseq_128_rxs_m_xs_128_boundedrand_r
2456 #define pcg128si_srandom_r pcg_oneseq_128_srandom_r
2457 #define pcg128si_advance_r pcg_oneseq_128_advance_r
2461 typedef struct pcg_state_setseq_8 pcg8i_random_t;
2462 typedef struct pcg_state_setseq_16 pcg16i_random_t;
2463 typedef struct pcg_state_setseq_32 pcg32i_random_t;
2464 typedef struct pcg_state_setseq_64 pcg64i_random_t;
2466 #define pcg8i_random_r pcg_setseq_8_rxs_m_xs_8_random_r
2467 #define pcg16i_random_r pcg_setseq_16_rxs_m_xs_16_random_r
2468 #define pcg32i_random_r pcg_setseq_32_rxs_m_xs_32_random_r
2469 #define pcg64i_random_r pcg_setseq_64_rxs_m_xs_64_random_r
2470 /*** boundedrand_r */
2471 #define pcg8i_boundedrand_r pcg_setseq_8_rxs_m_xs_8_boundedrand_r
2472 #define pcg16i_boundedrand_r pcg_setseq_16_rxs_m_xs_16_boundedrand_r
2473 #define pcg32i_boundedrand_r pcg_setseq_32_rxs_m_xs_32_boundedrand_r
2474 #define pcg64i_boundedrand_r pcg_setseq_64_rxs_m_xs_64_boundedrand_r
2476 #define pcg8i_srandom_r pcg_setseq_8_srandom_r
2477 #define pcg16i_srandom_r pcg_setseq_16_srandom_r
2478 #define pcg32i_srandom_r pcg_setseq_32_srandom_r
2479 #define pcg64i_srandom_r pcg_setseq_64_srandom_r
2481 #define pcg8i_advance_r pcg_setseq_8_advance_r
2482 #define pcg16i_advance_r pcg_setseq_16_advance_r
2483 #define pcg32i_advance_r pcg_setseq_32_advance_r
2484 #define pcg64i_advance_r pcg_setseq_64_advance_r
2486 #if PCG_HAS_128BIT_OPS
2487 typedef struct pcg_state_setseq_128 pcg128i_random_t;
2488 #define pcg128i_random_r pcg_setseq_128_rxs_m_xs_128_random_r
2489 #define pcg128i_boundedrand_r pcg_setseq_128_rxs_m_xs_128_boundedrand_r
2490 #define pcg128i_srandom_r pcg_setseq_128_srandom_r
2491 #define pcg128i_advance_r pcg_setseq_128_advance_r
2494 extern uint32_t pcg32_random(void);
2495 extern uint32_t pcg32_boundedrand(uint32_t bound);
2496 extern void pcg32_srandom(uint64_t seed, uint64_t seq);
2497 extern void pcg32_advance(uint64_t delta);
2499 #if PCG_HAS_128BIT_OPS
2500 extern uint64_t pcg64_random(void);
2501 extern uint64_t pcg64_boundedrand(uint64_t bound);
2502 extern void pcg64_srandom(pcg128_t seed, pcg128_t seq);
2503 extern void pcg64_advance(pcg128_t delta);
2507 * Static initialization constants (if you can't call srandom for some
2511 #define PCG32_INITIALIZER PCG_STATE_SETSEQ_64_INITIALIZER
2512 #define PCG32U_INITIALIZER PCG_STATE_UNIQUE_64_INITIALIZER
2513 #define PCG32S_INITIALIZER PCG_STATE_ONESEQ_64_INITIALIZER
2514 #define PCG32F_INITIALIZER PCG_STATE_MCG_64_INITIALIZER
2516 #if PCG_HAS_128BIT_OPS
2517 #define PCG64_INITIALIZER PCG_STATE_SETSEQ_128_INITIALIZER
2518 #define PCG64U_INITIALIZER PCG_STATE_UNIQUE_128_INITIALIZER
2519 #define PCG64S_INITIALIZER PCG_STATE_ONESEQ_128_INITIALIZER
2520 #define PCG64F_INITIALIZER PCG_STATE_MCG_128_INITIALIZER
2523 #define PCG8SI_INITIALIZER PCG_STATE_ONESEQ_8_INITIALIZER
2524 #define PCG16SI_INITIALIZER PCG_STATE_ONESEQ_16_INITIALIZER
2525 #define PCG32SI_INITIALIZER PCG_STATE_ONESEQ_32_INITIALIZER
2526 #define PCG64SI_INITIALIZER PCG_STATE_ONESEQ_64_INITIALIZER
2527 #if PCG_HAS_128BIT_OPS
2528 #define PCG128SI_INITIALIZER PCG_STATE_ONESEQ_128_INITIALIZER
2531 #define PCG8I_INITIALIZER PCG_STATE_SETSEQ_8_INITIALIZER
2532 #define PCG16I_INITIALIZER PCG_STATE_SETSEQ_16_INITIALIZER
2533 #define PCG32I_INITIALIZER PCG_STATE_SETSEQ_32_INITIALIZER
2534 #define PCG64I_INITIALIZER PCG_STATE_SETSEQ_64_INITIALIZER
2535 #if PCG_HAS_128BIT_OPS
2536 #define PCG128I_INITIALIZER PCG_STATE_SETSEQ_128_INITIALIZER
2543 #endif /* PCG_VARIANTS_H_INCLUDED */