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
39 #if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__
40 typedef __uint128_t pcg128_t;
41 #define PCG_128BIT_CONSTANT(high,low) \
42 ((((pcg128_t)high) << 64) + low)
43 #define PCG_HAS_128BIT_OPS 1
45 #define PCG_HAS_128BIT_OPS 0
53 * Rotate helper functions.
56 static inline uint8_t pcg_rotr_8(uint8_t value, unsigned int rot)
58 /* Unfortunately, clang is kinda pathetic when it comes to properly
59 * recognizing idiomatic rotate code, so for clang we actually provide
60 * assembler directives (enabled with PCG_USE_INLINE_ASM). Boo, hiss.
62 #if PCG_USE_INLINE_ASM && defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
63 __asm__ ("rorb %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
66 return (value >> rot) | (value << ((- rot) & 7));
70 static inline uint16_t pcg_rotr_16(uint16_t value, unsigned int rot)
72 #if PCG_USE_INLINE_ASM && defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
73 __asm__ ("rorw %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
76 return (value >> rot) | (value << ((- rot) & 15));
80 static inline uint32_t pcg_rotr_32(uint32_t value, unsigned int rot)
82 #if PCG_USE_INLINE_ASM && defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
83 __asm__ ("rorl %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
86 return (value >> rot) | (value << ((- rot) & 31));
90 static inline uint64_t pcg_rotr_64(uint64_t value, unsigned int rot)
92 #if 0 && PCG_USE_INLINE_ASM && defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
93 /* For whatever reason, clang actually *does* generate rotq by
94 itself, so we don't need this code. */
95 __asm__ ("rorq %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
98 return (value >> rot) | (value << ((- rot) & 63));
102 #if PCG_HAS_128BIT_OPS
103 static inline pcg128_t pcg_rotr_128(pcg128_t value, unsigned int rot)
105 return (value >> rot) | (value << ((- rot) & 127));
110 * Output functions. These are the core of the PCG generation scheme.
115 static inline uint8_t pcg_output_xsh_rs_16_8(uint16_t state)
117 return (uint8_t)(((state >> 7u) ^ state) >> ((state >> 14u) + 3u));
120 static inline uint16_t pcg_output_xsh_rs_32_16(uint32_t state)
122 return (uint16_t)(((state >> 11u) ^ state) >> ((state >> 30u) + 11u));
125 static inline uint32_t pcg_output_xsh_rs_64_32(uint64_t state)
128 return (uint32_t)(((state >> 22u) ^ state) >> ((state >> 61u) + 22u));
131 #if PCG_HAS_128BIT_OPS
132 static inline uint64_t pcg_output_xsh_rs_128_64(pcg128_t state)
134 return (uint64_t)(((state >> 43u) ^ state) >> ((state >> 124u) + 45u));
140 static inline uint8_t pcg_output_xsh_rr_16_8(uint16_t state)
142 return pcg_rotr_8(((state >> 5u) ^ state) >> 5u, state >> 13u);
145 static inline uint16_t pcg_output_xsh_rr_32_16(uint32_t state)
147 return pcg_rotr_16(((state >> 10u) ^ state) >> 12u, state >> 28u);
150 static inline uint32_t pcg_output_xsh_rr_64_32(uint64_t state)
152 return pcg_rotr_32(((state >> 18u) ^ state) >> 27u, state >> 59u);
155 #if PCG_HAS_128BIT_OPS
156 static inline uint64_t pcg_output_xsh_rr_128_64(pcg128_t state)
158 return pcg_rotr_64(((state >> 35u) ^ state) >> 58u, state >> 122u);
164 static inline uint8_t pcg_output_rxs_m_xs_8_8(uint8_t state)
166 uint8_t word = ((state >> ((state >> 6u) + 2u)) ^ state) * 217u;
167 return (word >> 6u) ^ word;
170 static inline uint16_t pcg_output_rxs_m_xs_16_16(uint16_t state)
172 uint16_t word = ((state >> ((state >> 13u) + 3u)) ^ state) * 62169u;
173 return (word >> 11u) ^ word;
176 static inline uint32_t pcg_output_rxs_m_xs_32_32(uint32_t state)
178 uint32_t word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
179 return (word >> 22u) ^ word;
182 static inline uint64_t pcg_output_rxs_m_xs_64_64(uint64_t state)
184 uint64_t word = ((state >> ((state >> 59u) + 5u)) ^ state)
185 * 12605985483714917081ull;
186 return (word >> 43u) ^ word;
189 #if PCG_HAS_128BIT_OPS
190 static inline pcg128_t pcg_output_rxs_m_xs_128_128(pcg128_t state)
192 pcg128_t word = ((state >> ((state >> 122u) + 6u)) ^ state)
193 * (PCG_128BIT_CONSTANT(17766728186571221404ULL,
194 12605985483714917081ULL));
195 /* 327738287884841127335028083622016905945 */
196 return (word >> 86u) ^ word;
202 static inline uint8_t pcg_output_rxs_m_16_8(uint16_t state)
204 return (((state >> ((state >> 13u) + 3u)) ^ state) * 62169u) >> 8u;
207 static inline uint16_t pcg_output_rxs_m_32_16(uint32_t state)
209 return (((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u) >> 16u;
212 static inline uint32_t pcg_output_rxs_m_64_32(uint64_t state)
214 return (((state >> ((state >> 59u) + 5u)) ^ state)
215 * 12605985483714917081ull) >> 32u;
218 #if PCG_HAS_128BIT_OPS
219 static inline uint64_t pcg_output_rxs_m_128_64(pcg128_t state)
221 return (((state >> ((state >> 122u) + 6u)) ^ state)
222 * (PCG_128BIT_CONSTANT(17766728186571221404ULL,
223 12605985483714917081ULL))) >> 64u;
224 /* 327738287884841127335028083622016905945 */
228 /* XSL RR (only defined for >= 64 bits) */
230 static inline uint32_t pcg_output_xsl_rr_64_32(uint64_t state)
232 return pcg_rotr_32(((uint32_t)(state >> 32u)) ^ (uint32_t)state,
236 #if PCG_HAS_128BIT_OPS
237 static inline uint64_t pcg_output_xsl_rr_128_64(pcg128_t state)
239 return pcg_rotr_64(((uint64_t)(state >> 64u)) ^ (uint64_t)state,
244 /* XSL RR RR (only defined for >= 64 bits) */
246 static inline uint64_t pcg_output_xsl_rr_rr_64_64(uint64_t state)
248 uint32_t rot1 = (uint32_t)(state >> 59u);
249 uint32_t high = (uint32_t)(state >> 32u);
250 uint32_t low = (uint32_t)state;
251 uint32_t xored = high ^ low;
252 uint32_t newlow = pcg_rotr_32(xored, rot1);
253 uint32_t newhigh = pcg_rotr_32(high, newlow & 31u);
254 return (((uint64_t)newhigh) << 32u) | newlow;
257 #if PCG_HAS_128BIT_OPS
258 static inline pcg128_t pcg_output_xsl_rr_rr_128_128(pcg128_t state)
260 uint32_t rot1 = (uint32_t)(state >> 122u);
261 uint64_t high = (uint64_t)(state >> 64u);
262 uint64_t low = (uint64_t)state;
263 uint64_t xored = high ^ low;
264 uint64_t newlow = pcg_rotr_64(xored, rot1);
265 uint64_t newhigh = pcg_rotr_64(high, newlow & 63u);
266 return (((pcg128_t)newhigh) << 64u) | newlow;
270 #define PCG_DEFAULT_MULTIPLIER_8 141U
271 #define PCG_DEFAULT_MULTIPLIER_16 12829U
272 #define PCG_DEFAULT_MULTIPLIER_32 747796405U
273 #define PCG_DEFAULT_MULTIPLIER_64 6364136223846793005ULL
275 #define PCG_DEFAULT_INCREMENT_8 77U
276 #define PCG_DEFAULT_INCREMENT_16 47989U
277 #define PCG_DEFAULT_INCREMENT_32 2891336453U
278 #define PCG_DEFAULT_INCREMENT_64 1442695040888963407ULL
280 #if PCG_HAS_128BIT_OPS
281 #define PCG_DEFAULT_MULTIPLIER_128 \
282 PCG_128BIT_CONSTANT(2549297995355413924ULL,4865540595714422341ULL)
283 #define PCG_DEFAULT_INCREMENT_128 \
284 PCG_128BIT_CONSTANT(6364136223846793005ULL,1442695040888963407ULL)
288 * Static initialization constants (if you can't call srandom for some
292 #define PCG_STATE_ONESEQ_8_INITIALIZER { 0xd7U }
293 #define PCG_STATE_ONESEQ_16_INITIALIZER { 0x20dfU }
294 #define PCG_STATE_ONESEQ_32_INITIALIZER { 0x46b56677U }
295 #define PCG_STATE_ONESEQ_64_INITIALIZER { 0x4d595df4d0f33173ULL }
296 #if PCG_HAS_128BIT_OPS
297 #define PCG_STATE_ONESEQ_128_INITIALIZER \
298 { PCG_128BIT_CONSTANT(0xb8dc10e158a92392ULL, 0x98046df007ec0a53ULL) }
301 #define PCG_STATE_UNIQUE_8_INITIALIZER PCG_STATE_ONESEQ_8_INITIALIZER
302 #define PCG_STATE_UNIQUE_16_INITIALIZER PCG_STATE_ONESEQ_16_INITIALIZER
303 #define PCG_STATE_UNIQUE_32_INITIALIZER PCG_STATE_ONESEQ_32_INITIALIZER
304 #define PCG_STATE_UNIQUE_64_INITIALIZER PCG_STATE_ONESEQ_64_INITIALIZER
305 #if PCG_HAS_128BIT_OPS
306 #define PCG_STATE_UNIQUE_128_INITIALIZER PCG_STATE_ONESEQ_128_INITIALIZER
309 #define PCG_STATE_MCG_8_INITIALIZER { 0xe5U }
310 #define PCG_STATE_MCG_16_INITIALIZER { 0xa5e5U }
311 #define PCG_STATE_MCG_32_INITIALIZER { 0xd15ea5e5U }
312 #define PCG_STATE_MCG_64_INITIALIZER { 0xcafef00dd15ea5e5ULL }
313 #if PCG_HAS_128BIT_OPS
314 #define PCG_STATE_MCG_128_INITIALIZER \
315 { PCG_128BIT_CONSTANT(0x0000000000000000ULL, 0xcafef00dd15ea5e5ULL) }
318 #define PCG_STATE_SETSEQ_8_INITIALIZER { 0x9bU, 0xdbU }
319 #define PCG_STATE_SETSEQ_16_INITIALIZER { 0xe39bU, 0x5bdbU }
320 #define PCG_STATE_SETSEQ_32_INITIALIZER { 0xec02d89bU, 0x94b95bdbU }
321 #define PCG_STATE_SETSEQ_64_INITIALIZER \
322 { 0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL }
323 #if PCG_HAS_128BIT_OPS
324 #define PCG_STATE_SETSEQ_128_INITIALIZER \
325 { PCG_128BIT_CONSTANT(0x979c9a98d8462005ULL, 0x7d3e9cb6cfe0549bULL), \
326 PCG_128BIT_CONSTANT(0x0000000000000001ULL, 0xda3e39cb94b95bdbULL) }
329 /* Representations for the oneseq, mcg, and unique variants */
335 struct pcg_state_16 {
339 struct pcg_state_32 {
343 struct pcg_state_64 {
347 #if PCG_HAS_128BIT_OPS
348 struct pcg_state_128 {
353 /* Representations setseq variants */
355 struct pcg_state_setseq_8 {
360 struct pcg_state_setseq_16 {
365 struct pcg_state_setseq_32 {
370 struct pcg_state_setseq_64 {
375 #if PCG_HAS_128BIT_OPS
376 struct pcg_state_setseq_128 {
382 /* Multi-step advance functions (jump-ahead, jump-back) */
384 extern uint8_t pcg_advance_lcg_8(uint8_t state, uint8_t delta, uint8_t cur_mult,
386 extern uint16_t pcg_advance_lcg_16(uint16_t state, uint16_t delta,
387 uint16_t cur_mult, uint16_t cur_plus);
388 extern uint32_t pcg_advance_lcg_32(uint32_t state, uint32_t delta,
389 uint32_t cur_mult, uint32_t cur_plus);
390 extern uint64_t pcg_advance_lcg_64(uint64_t state, uint64_t delta,
391 uint64_t cur_mult, uint64_t cur_plus);
393 #if PCG_HAS_128BIT_OPS
394 extern pcg128_t pcg_advance_lcg_128(pcg128_t state, pcg128_t delta,
395 pcg128_t cur_mult, pcg128_t cur_plus);
398 /* Functions to advance the underlying LCG, one version for each size and
399 * each style. These functions are considered semi-private. There is rarely
400 * a good reason to call them directly.
403 static inline void pcg_oneseq_8_step_r(struct pcg_state_8* rng)
405 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8
406 + PCG_DEFAULT_INCREMENT_8;
409 static inline void pcg_oneseq_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
411 rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
412 PCG_DEFAULT_INCREMENT_8);
415 static inline void pcg_mcg_8_step_r(struct pcg_state_8* rng)
417 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8;
420 static inline void pcg_mcg_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
423 = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8, 0u);
426 static inline void pcg_unique_8_step_r(struct pcg_state_8* rng)
428 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8
429 + (uint8_t)(((intptr_t)rng) | 1u);
432 static inline void pcg_unique_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
434 rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
435 (uint8_t)(((intptr_t)rng) | 1u));
438 static inline void pcg_setseq_8_step_r(struct pcg_state_setseq_8* rng)
440 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8 + rng->inc;
443 static inline void pcg_setseq_8_advance_r(struct pcg_state_setseq_8* rng,
446 rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
450 static inline void pcg_oneseq_16_step_r(struct pcg_state_16* rng)
452 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16
453 + PCG_DEFAULT_INCREMENT_16;
456 static inline void pcg_oneseq_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
458 rng->state = pcg_advance_lcg_16(
459 rng->state, delta, PCG_DEFAULT_MULTIPLIER_16, PCG_DEFAULT_INCREMENT_16);
462 static inline void pcg_mcg_16_step_r(struct pcg_state_16* rng)
464 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16;
467 static inline void pcg_mcg_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
470 = pcg_advance_lcg_16(rng->state, delta, PCG_DEFAULT_MULTIPLIER_16, 0u);
473 static inline void pcg_unique_16_step_r(struct pcg_state_16* rng)
475 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16
476 + (uint16_t)(((intptr_t)rng) | 1u);
479 static inline void pcg_unique_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
482 = pcg_advance_lcg_16(rng->state, delta, PCG_DEFAULT_MULTIPLIER_16,
483 (uint16_t)(((intptr_t)rng) | 1u));
486 static inline void pcg_setseq_16_step_r(struct pcg_state_setseq_16* rng)
488 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16 + rng->inc;
491 static inline void pcg_setseq_16_advance_r(struct pcg_state_setseq_16* rng,
494 rng->state = pcg_advance_lcg_16(rng->state, delta,
495 PCG_DEFAULT_MULTIPLIER_16, rng->inc);
498 static inline void pcg_oneseq_32_step_r(struct pcg_state_32* rng)
500 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32
501 + PCG_DEFAULT_INCREMENT_32;
504 static inline void pcg_oneseq_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
506 rng->state = pcg_advance_lcg_32(
507 rng->state, delta, PCG_DEFAULT_MULTIPLIER_32, PCG_DEFAULT_INCREMENT_32);
510 static inline void pcg_mcg_32_step_r(struct pcg_state_32* rng)
512 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32;
515 static inline void pcg_mcg_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
518 = pcg_advance_lcg_32(rng->state, delta, PCG_DEFAULT_MULTIPLIER_32, 0u);
521 static inline void pcg_unique_32_step_r(struct pcg_state_32* rng)
523 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32
524 + (uint32_t)(((intptr_t)rng) | 1u);
527 static inline void pcg_unique_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
530 = pcg_advance_lcg_32(rng->state, delta, PCG_DEFAULT_MULTIPLIER_32,
531 (uint32_t)(((intptr_t)rng) | 1u));
534 static inline void pcg_setseq_32_step_r(struct pcg_state_setseq_32* rng)
536 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32 + rng->inc;
539 static inline void pcg_setseq_32_advance_r(struct pcg_state_setseq_32* rng,
542 rng->state = pcg_advance_lcg_32(rng->state, delta,
543 PCG_DEFAULT_MULTIPLIER_32, rng->inc);
546 static inline void pcg_oneseq_64_step_r(struct pcg_state_64* rng)
548 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64
549 + PCG_DEFAULT_INCREMENT_64;
552 static inline void pcg_oneseq_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
554 rng->state = pcg_advance_lcg_64(
555 rng->state, delta, PCG_DEFAULT_MULTIPLIER_64, PCG_DEFAULT_INCREMENT_64);
558 static inline void pcg_mcg_64_step_r(struct pcg_state_64* rng)
560 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64;
563 static inline void pcg_mcg_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
566 = pcg_advance_lcg_64(rng->state, delta, PCG_DEFAULT_MULTIPLIER_64, 0u);
569 static inline void pcg_unique_64_step_r(struct pcg_state_64* rng)
571 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64
572 + (uint64_t)(((intptr_t)rng) | 1u);
575 static inline void pcg_unique_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
578 = pcg_advance_lcg_64(rng->state, delta, PCG_DEFAULT_MULTIPLIER_64,
579 (uint64_t)(((intptr_t)rng) | 1u));
582 static inline void pcg_setseq_64_step_r(struct pcg_state_setseq_64* rng)
584 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64 + rng->inc;
587 static inline void pcg_setseq_64_advance_r(struct pcg_state_setseq_64* rng,
590 rng->state = pcg_advance_lcg_64(rng->state, delta,
591 PCG_DEFAULT_MULTIPLIER_64, rng->inc);
594 #if PCG_HAS_128BIT_OPS
595 static inline void pcg_oneseq_128_step_r(struct pcg_state_128* rng)
597 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128
598 + PCG_DEFAULT_INCREMENT_128;
602 #if PCG_HAS_128BIT_OPS
603 static inline void pcg_oneseq_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
606 = pcg_advance_lcg_128(rng->state, delta, PCG_DEFAULT_MULTIPLIER_128,
607 PCG_DEFAULT_INCREMENT_128);
611 #if PCG_HAS_128BIT_OPS
612 static inline void pcg_mcg_128_step_r(struct pcg_state_128* rng)
614 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128;
618 #if PCG_HAS_128BIT_OPS
619 static inline void pcg_mcg_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
621 rng->state = pcg_advance_lcg_128(rng->state, delta,
622 PCG_DEFAULT_MULTIPLIER_128, 0u);
626 #if PCG_HAS_128BIT_OPS
627 static inline void pcg_unique_128_step_r(struct pcg_state_128* rng)
629 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128
630 + (pcg128_t)(((intptr_t)rng) | 1u);
634 #if PCG_HAS_128BIT_OPS
635 static inline void pcg_unique_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
638 = pcg_advance_lcg_128(rng->state, delta, PCG_DEFAULT_MULTIPLIER_128,
639 (pcg128_t)(((intptr_t)rng) | 1u));
643 #if PCG_HAS_128BIT_OPS
644 static inline void pcg_setseq_128_step_r(struct pcg_state_setseq_128* rng)
646 rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128 + rng->inc;
650 #if PCG_HAS_128BIT_OPS
651 static inline void pcg_setseq_128_advance_r(struct pcg_state_setseq_128* rng,
654 rng->state = pcg_advance_lcg_128(rng->state, delta,
655 PCG_DEFAULT_MULTIPLIER_128, rng->inc);
659 /* Functions to seed the RNG state, one version for each size and each
660 * style. Unlike the step functions, regular users can and should call
664 static inline void pcg_oneseq_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
667 pcg_oneseq_8_step_r(rng);
668 rng->state += initstate;
669 pcg_oneseq_8_step_r(rng);
672 static inline void pcg_mcg_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
674 rng->state = initstate | 1u;
677 static inline void pcg_unique_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
680 pcg_unique_8_step_r(rng);
681 rng->state += initstate;
682 pcg_unique_8_step_r(rng);
685 static inline void pcg_setseq_8_srandom_r(struct pcg_state_setseq_8* rng,
686 uint8_t initstate, uint8_t initseq)
689 rng->inc = (initseq << 1u) | 1u;
690 pcg_setseq_8_step_r(rng);
691 rng->state += initstate;
692 pcg_setseq_8_step_r(rng);
695 static inline void pcg_oneseq_16_srandom_r(struct pcg_state_16* rng,
699 pcg_oneseq_16_step_r(rng);
700 rng->state += initstate;
701 pcg_oneseq_16_step_r(rng);
704 static inline void pcg_mcg_16_srandom_r(struct pcg_state_16* rng, uint16_t initstate)
706 rng->state = initstate | 1u;
709 static inline void pcg_unique_16_srandom_r(struct pcg_state_16* rng,
713 pcg_unique_16_step_r(rng);
714 rng->state += initstate;
715 pcg_unique_16_step_r(rng);
718 static inline void pcg_setseq_16_srandom_r(struct pcg_state_setseq_16* rng,
719 uint16_t initstate, uint16_t initseq)
722 rng->inc = (initseq << 1u) | 1u;
723 pcg_setseq_16_step_r(rng);
724 rng->state += initstate;
725 pcg_setseq_16_step_r(rng);
728 static inline void pcg_oneseq_32_srandom_r(struct pcg_state_32* rng,
732 pcg_oneseq_32_step_r(rng);
733 rng->state += initstate;
734 pcg_oneseq_32_step_r(rng);
737 static inline void pcg_mcg_32_srandom_r(struct pcg_state_32* rng, uint32_t initstate)
739 rng->state = initstate | 1u;
742 static inline void pcg_unique_32_srandom_r(struct pcg_state_32* rng,
746 pcg_unique_32_step_r(rng);
747 rng->state += initstate;
748 pcg_unique_32_step_r(rng);
751 static inline void pcg_setseq_32_srandom_r(struct pcg_state_setseq_32* rng,
752 uint32_t initstate, uint32_t initseq)
755 rng->inc = (initseq << 1u) | 1u;
756 pcg_setseq_32_step_r(rng);
757 rng->state += initstate;
758 pcg_setseq_32_step_r(rng);
761 static inline void pcg_oneseq_64_srandom_r(struct pcg_state_64* rng,
765 pcg_oneseq_64_step_r(rng);
766 rng->state += initstate;
767 pcg_oneseq_64_step_r(rng);
770 static inline void pcg_mcg_64_srandom_r(struct pcg_state_64* rng, uint64_t initstate)
772 rng->state = initstate | 1u;
775 static inline void pcg_unique_64_srandom_r(struct pcg_state_64* rng,
779 pcg_unique_64_step_r(rng);
780 rng->state += initstate;
781 pcg_unique_64_step_r(rng);
784 static inline void pcg_setseq_64_srandom_r(struct pcg_state_setseq_64* rng,
785 uint64_t initstate, uint64_t initseq)
788 rng->inc = (initseq << 1u) | 1u;
789 pcg_setseq_64_step_r(rng);
790 rng->state += initstate;
791 pcg_setseq_64_step_r(rng);
794 #if PCG_HAS_128BIT_OPS
795 static inline void pcg_oneseq_128_srandom_r(struct pcg_state_128* rng,
799 pcg_oneseq_128_step_r(rng);
800 rng->state += initstate;
801 pcg_oneseq_128_step_r(rng);
805 #if PCG_HAS_128BIT_OPS
806 static inline void pcg_mcg_128_srandom_r(struct pcg_state_128* rng, pcg128_t initstate)
808 rng->state = initstate | 1u;
812 #if PCG_HAS_128BIT_OPS
813 static inline void pcg_unique_128_srandom_r(struct pcg_state_128* rng,
817 pcg_unique_128_step_r(rng);
818 rng->state += initstate;
819 pcg_unique_128_step_r(rng);
823 #if PCG_HAS_128BIT_OPS
824 static inline void pcg_setseq_128_srandom_r(struct pcg_state_setseq_128* rng,
825 pcg128_t initstate, pcg128_t initseq)
828 rng->inc = (initseq << 1u) | 1u;
829 pcg_setseq_128_step_r(rng);
830 rng->state += initstate;
831 pcg_setseq_128_step_r(rng);
835 /* Now, finally we create each of the individual generators. We provide
836 * a random_r function that provides a random number of the appropriate
837 * type (using the full range of the type) and a boundedrand_r version
840 * Implementation notes for boundedrand_r:
842 * To avoid bias, we need to make the range of the RNG a multiple of
843 * bound, which we do by dropping output less than a threshold.
844 * Let's consider a 32-bit case... A naive scheme to calculate the
845 * threshold would be to do
847 * uint32_t threshold = 0x100000000ull % bound;
849 * but 64-bit div/mod is slower than 32-bit div/mod (especially on
850 * 32-bit platforms). In essence, we do
852 * uint32_t threshold = (0x100000000ull-bound) % bound;
854 * because this version will calculate the same modulus, but the LHS
855 * value is less than 2^32.
857 * (Note that using modulo is only wise for good RNGs, poorer RNGs
858 * such as raw LCGs do better using a technique based on division.)
859 * Empricical tests show that division is preferable to modulus for
860 * reducting the range of an RNG. It's faster, and sometimes it can
861 * even be statistically prefereable.
864 /* Generation functions for XSH RS */
866 static inline uint8_t pcg_oneseq_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
868 uint16_t oldstate = rng->state;
869 pcg_oneseq_16_step_r(rng);
870 return pcg_output_xsh_rs_16_8(oldstate);
873 static inline uint8_t pcg_oneseq_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
876 uint8_t threshold = ((uint8_t)(-bound)) % bound;
878 uint8_t r = pcg_oneseq_16_xsh_rs_8_random_r(rng);
884 static inline uint16_t pcg_oneseq_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
886 uint32_t oldstate = rng->state;
887 pcg_oneseq_32_step_r(rng);
888 return pcg_output_xsh_rs_32_16(oldstate);
891 static inline uint16_t pcg_oneseq_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
894 uint16_t threshold = ((uint16_t)(-bound)) % bound;
896 uint16_t r = pcg_oneseq_32_xsh_rs_16_random_r(rng);
902 static inline uint32_t pcg_oneseq_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
904 uint64_t oldstate = rng->state;
905 pcg_oneseq_64_step_r(rng);
906 return pcg_output_xsh_rs_64_32(oldstate);
909 static inline uint32_t pcg_oneseq_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
912 uint32_t threshold = -bound % bound;
914 uint32_t r = pcg_oneseq_64_xsh_rs_32_random_r(rng);
920 #if PCG_HAS_128BIT_OPS
921 static inline uint64_t pcg_oneseq_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
923 pcg_oneseq_128_step_r(rng);
924 return pcg_output_xsh_rs_128_64(rng->state);
928 #if PCG_HAS_128BIT_OPS
929 static inline uint64_t
930 pcg_oneseq_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
933 uint64_t threshold = -bound % bound;
935 uint64_t r = pcg_oneseq_128_xsh_rs_64_random_r(rng);
942 static inline uint8_t pcg_unique_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
944 uint16_t oldstate = rng->state;
945 pcg_unique_16_step_r(rng);
946 return pcg_output_xsh_rs_16_8(oldstate);
949 static inline uint8_t pcg_unique_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
952 uint8_t threshold = ((uint8_t)(-bound)) % bound;
954 uint8_t r = pcg_unique_16_xsh_rs_8_random_r(rng);
960 static inline uint16_t pcg_unique_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
962 uint32_t oldstate = rng->state;
963 pcg_unique_32_step_r(rng);
964 return pcg_output_xsh_rs_32_16(oldstate);
967 static inline uint16_t pcg_unique_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
970 uint16_t threshold = ((uint16_t)(-bound)) % bound;
972 uint16_t r = pcg_unique_32_xsh_rs_16_random_r(rng);
978 static inline uint32_t pcg_unique_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
980 uint64_t oldstate = rng->state;
981 pcg_unique_64_step_r(rng);
982 return pcg_output_xsh_rs_64_32(oldstate);
985 static inline uint32_t pcg_unique_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
988 uint32_t threshold = -bound % bound;
990 uint32_t r = pcg_unique_64_xsh_rs_32_random_r(rng);
996 #if PCG_HAS_128BIT_OPS
997 static inline uint64_t pcg_unique_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
999 pcg_unique_128_step_r(rng);
1000 return pcg_output_xsh_rs_128_64(rng->state);
1004 #if PCG_HAS_128BIT_OPS
1005 static inline uint64_t
1006 pcg_unique_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
1009 uint64_t threshold = -bound % bound;
1011 uint64_t r = pcg_unique_128_xsh_rs_64_random_r(rng);
1018 static inline uint8_t pcg_setseq_16_xsh_rs_8_random_r(struct pcg_state_setseq_16* rng)
1020 uint16_t oldstate = rng->state;
1021 pcg_setseq_16_step_r(rng);
1022 return pcg_output_xsh_rs_16_8(oldstate);
1025 static inline uint8_t
1026 pcg_setseq_16_xsh_rs_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1029 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1031 uint8_t r = pcg_setseq_16_xsh_rs_8_random_r(rng);
1037 static inline uint16_t
1038 pcg_setseq_32_xsh_rs_16_random_r(struct pcg_state_setseq_32* rng)
1040 uint32_t oldstate = rng->state;
1041 pcg_setseq_32_step_r(rng);
1042 return pcg_output_xsh_rs_32_16(oldstate);
1045 static inline uint16_t
1046 pcg_setseq_32_xsh_rs_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1049 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1051 uint16_t r = pcg_setseq_32_xsh_rs_16_random_r(rng);
1057 static inline uint32_t
1058 pcg_setseq_64_xsh_rs_32_random_r(struct pcg_state_setseq_64* rng)
1060 uint64_t oldstate = rng->state;
1061 pcg_setseq_64_step_r(rng);
1062 return pcg_output_xsh_rs_64_32(oldstate);
1065 static inline uint32_t
1066 pcg_setseq_64_xsh_rs_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1069 uint32_t threshold = -bound % bound;
1071 uint32_t r = pcg_setseq_64_xsh_rs_32_random_r(rng);
1077 #if PCG_HAS_128BIT_OPS
1078 static inline uint64_t
1079 pcg_setseq_128_xsh_rs_64_random_r(struct pcg_state_setseq_128* rng)
1081 pcg_setseq_128_step_r(rng);
1082 return pcg_output_xsh_rs_128_64(rng->state);
1086 #if PCG_HAS_128BIT_OPS
1087 static inline uint64_t
1088 pcg_setseq_128_xsh_rs_64_boundedrand_r(struct pcg_state_setseq_128* rng,
1091 uint64_t threshold = -bound % bound;
1093 uint64_t r = pcg_setseq_128_xsh_rs_64_random_r(rng);
1100 static inline uint8_t pcg_mcg_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
1102 uint16_t oldstate = rng->state;
1103 pcg_mcg_16_step_r(rng);
1104 return pcg_output_xsh_rs_16_8(oldstate);
1107 static inline uint8_t pcg_mcg_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
1110 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1112 uint8_t r = pcg_mcg_16_xsh_rs_8_random_r(rng);
1118 static inline uint16_t pcg_mcg_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
1120 uint32_t oldstate = rng->state;
1121 pcg_mcg_32_step_r(rng);
1122 return pcg_output_xsh_rs_32_16(oldstate);
1125 static inline uint16_t pcg_mcg_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
1128 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1130 uint16_t r = pcg_mcg_32_xsh_rs_16_random_r(rng);
1136 static inline uint32_t pcg_mcg_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
1138 uint64_t oldstate = rng->state;
1139 pcg_mcg_64_step_r(rng);
1140 return pcg_output_xsh_rs_64_32(oldstate);
1143 static inline uint32_t pcg_mcg_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
1146 uint32_t threshold = -bound % bound;
1148 uint32_t r = pcg_mcg_64_xsh_rs_32_random_r(rng);
1154 #if PCG_HAS_128BIT_OPS
1155 static inline uint64_t pcg_mcg_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
1157 pcg_mcg_128_step_r(rng);
1158 return pcg_output_xsh_rs_128_64(rng->state);
1162 #if PCG_HAS_128BIT_OPS
1163 static inline uint64_t pcg_mcg_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
1166 uint64_t threshold = -bound % bound;
1168 uint64_t r = pcg_mcg_128_xsh_rs_64_random_r(rng);
1175 /* Generation functions for XSH RR */
1177 static inline uint8_t pcg_oneseq_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1179 uint16_t oldstate = rng->state;
1180 pcg_oneseq_16_step_r(rng);
1181 return pcg_output_xsh_rr_16_8(oldstate);
1184 static inline uint8_t pcg_oneseq_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1187 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1189 uint8_t r = pcg_oneseq_16_xsh_rr_8_random_r(rng);
1195 static inline uint16_t pcg_oneseq_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1197 uint32_t oldstate = rng->state;
1198 pcg_oneseq_32_step_r(rng);
1199 return pcg_output_xsh_rr_32_16(oldstate);
1202 static inline uint16_t pcg_oneseq_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1205 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1207 uint16_t r = pcg_oneseq_32_xsh_rr_16_random_r(rng);
1213 static inline uint32_t pcg_oneseq_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1215 uint64_t oldstate = rng->state;
1216 pcg_oneseq_64_step_r(rng);
1217 return pcg_output_xsh_rr_64_32(oldstate);
1220 static inline uint32_t pcg_oneseq_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1223 uint32_t threshold = -bound % bound;
1225 uint32_t r = pcg_oneseq_64_xsh_rr_32_random_r(rng);
1231 #if PCG_HAS_128BIT_OPS
1232 static inline uint64_t pcg_oneseq_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1234 pcg_oneseq_128_step_r(rng);
1235 return pcg_output_xsh_rr_128_64(rng->state);
1239 #if PCG_HAS_128BIT_OPS
1240 static inline uint64_t
1241 pcg_oneseq_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1244 uint64_t threshold = -bound % bound;
1246 uint64_t r = pcg_oneseq_128_xsh_rr_64_random_r(rng);
1253 static inline uint8_t pcg_unique_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1255 uint16_t oldstate = rng->state;
1256 pcg_unique_16_step_r(rng);
1257 return pcg_output_xsh_rr_16_8(oldstate);
1260 static inline uint8_t pcg_unique_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1263 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1265 uint8_t r = pcg_unique_16_xsh_rr_8_random_r(rng);
1271 static inline uint16_t pcg_unique_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1273 uint32_t oldstate = rng->state;
1274 pcg_unique_32_step_r(rng);
1275 return pcg_output_xsh_rr_32_16(oldstate);
1278 static inline uint16_t pcg_unique_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1281 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1283 uint16_t r = pcg_unique_32_xsh_rr_16_random_r(rng);
1289 static inline uint32_t pcg_unique_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1291 uint64_t oldstate = rng->state;
1292 pcg_unique_64_step_r(rng);
1293 return pcg_output_xsh_rr_64_32(oldstate);
1296 static inline uint32_t pcg_unique_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1299 uint32_t threshold = -bound % bound;
1301 uint32_t r = pcg_unique_64_xsh_rr_32_random_r(rng);
1307 #if PCG_HAS_128BIT_OPS
1308 static inline uint64_t pcg_unique_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1310 pcg_unique_128_step_r(rng);
1311 return pcg_output_xsh_rr_128_64(rng->state);
1315 #if PCG_HAS_128BIT_OPS
1316 static inline uint64_t
1317 pcg_unique_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1320 uint64_t threshold = -bound % bound;
1322 uint64_t r = pcg_unique_128_xsh_rr_64_random_r(rng);
1329 static inline uint8_t pcg_setseq_16_xsh_rr_8_random_r(struct pcg_state_setseq_16* rng)
1331 uint16_t oldstate = rng->state;
1332 pcg_setseq_16_step_r(rng);
1333 return pcg_output_xsh_rr_16_8(oldstate);
1336 static inline uint8_t
1337 pcg_setseq_16_xsh_rr_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1340 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1342 uint8_t r = pcg_setseq_16_xsh_rr_8_random_r(rng);
1348 static inline uint16_t
1349 pcg_setseq_32_xsh_rr_16_random_r(struct pcg_state_setseq_32* rng)
1351 uint32_t oldstate = rng->state;
1352 pcg_setseq_32_step_r(rng);
1353 return pcg_output_xsh_rr_32_16(oldstate);
1356 static inline uint16_t
1357 pcg_setseq_32_xsh_rr_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1360 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1362 uint16_t r = pcg_setseq_32_xsh_rr_16_random_r(rng);
1368 static inline uint32_t
1369 pcg_setseq_64_xsh_rr_32_random_r(struct pcg_state_setseq_64* rng)
1371 uint64_t oldstate = rng->state;
1372 pcg_setseq_64_step_r(rng);
1373 return pcg_output_xsh_rr_64_32(oldstate);
1376 static inline uint32_t
1377 pcg_setseq_64_xsh_rr_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1380 uint32_t threshold = -bound % bound;
1382 uint32_t r = pcg_setseq_64_xsh_rr_32_random_r(rng);
1388 #if PCG_HAS_128BIT_OPS
1389 static inline uint64_t
1390 pcg_setseq_128_xsh_rr_64_random_r(struct pcg_state_setseq_128* rng)
1392 pcg_setseq_128_step_r(rng);
1393 return pcg_output_xsh_rr_128_64(rng->state);
1397 #if PCG_HAS_128BIT_OPS
1398 static inline uint64_t
1399 pcg_setseq_128_xsh_rr_64_boundedrand_r(struct pcg_state_setseq_128* rng,
1402 uint64_t threshold = -bound % bound;
1404 uint64_t r = pcg_setseq_128_xsh_rr_64_random_r(rng);
1411 static inline uint8_t pcg_mcg_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1413 uint16_t oldstate = rng->state;
1414 pcg_mcg_16_step_r(rng);
1415 return pcg_output_xsh_rr_16_8(oldstate);
1418 static inline uint8_t pcg_mcg_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1421 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1423 uint8_t r = pcg_mcg_16_xsh_rr_8_random_r(rng);
1429 static inline uint16_t pcg_mcg_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1431 uint32_t oldstate = rng->state;
1432 pcg_mcg_32_step_r(rng);
1433 return pcg_output_xsh_rr_32_16(oldstate);
1436 static inline uint16_t pcg_mcg_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1439 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1441 uint16_t r = pcg_mcg_32_xsh_rr_16_random_r(rng);
1447 static inline uint32_t pcg_mcg_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1449 uint64_t oldstate = rng->state;
1450 pcg_mcg_64_step_r(rng);
1451 return pcg_output_xsh_rr_64_32(oldstate);
1454 static inline uint32_t pcg_mcg_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1457 uint32_t threshold = -bound % bound;
1459 uint32_t r = pcg_mcg_64_xsh_rr_32_random_r(rng);
1465 #if PCG_HAS_128BIT_OPS
1466 static inline uint64_t pcg_mcg_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1468 pcg_mcg_128_step_r(rng);
1469 return pcg_output_xsh_rr_128_64(rng->state);
1473 #if PCG_HAS_128BIT_OPS
1474 static inline uint64_t pcg_mcg_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1477 uint64_t threshold = -bound % bound;
1479 uint64_t r = pcg_mcg_128_xsh_rr_64_random_r(rng);
1486 /* Generation functions for RXS M XS (no MCG versions because they
1487 * don't make sense when you want to use the entire state)
1490 static inline uint8_t pcg_oneseq_8_rxs_m_xs_8_random_r(struct pcg_state_8* rng)
1492 uint8_t oldstate = rng->state;
1493 pcg_oneseq_8_step_r(rng);
1494 return pcg_output_rxs_m_xs_8_8(oldstate);
1497 static inline uint8_t pcg_oneseq_8_rxs_m_xs_8_boundedrand_r(struct pcg_state_8* rng,
1500 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1502 uint8_t r = pcg_oneseq_8_rxs_m_xs_8_random_r(rng);
1508 static inline uint16_t pcg_oneseq_16_rxs_m_xs_16_random_r(struct pcg_state_16* rng)
1510 uint16_t oldstate = rng->state;
1511 pcg_oneseq_16_step_r(rng);
1512 return pcg_output_rxs_m_xs_16_16(oldstate);
1515 static inline uint16_t
1516 pcg_oneseq_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_16* rng,
1519 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1521 uint16_t r = pcg_oneseq_16_rxs_m_xs_16_random_r(rng);
1527 static inline uint32_t pcg_oneseq_32_rxs_m_xs_32_random_r(struct pcg_state_32* rng)
1529 uint32_t oldstate = rng->state;
1530 pcg_oneseq_32_step_r(rng);
1531 return pcg_output_rxs_m_xs_32_32(oldstate);
1534 static inline uint32_t
1535 pcg_oneseq_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_32* rng,
1538 uint32_t threshold = -bound % bound;
1540 uint32_t r = pcg_oneseq_32_rxs_m_xs_32_random_r(rng);
1546 static inline uint64_t pcg_oneseq_64_rxs_m_xs_64_random_r(struct pcg_state_64* rng)
1548 uint64_t oldstate = rng->state;
1549 pcg_oneseq_64_step_r(rng);
1550 return pcg_output_rxs_m_xs_64_64(oldstate);
1553 static inline uint64_t
1554 pcg_oneseq_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_64* rng,
1557 uint64_t threshold = -bound % bound;
1559 uint64_t r = pcg_oneseq_64_rxs_m_xs_64_random_r(rng);
1565 #if PCG_HAS_128BIT_OPS
1566 static inline pcg128_t pcg_oneseq_128_rxs_m_xs_128_random_r(struct pcg_state_128* rng)
1568 pcg_oneseq_128_step_r(rng);
1569 return pcg_output_rxs_m_xs_128_128(rng->state);
1573 #if PCG_HAS_128BIT_OPS
1574 static inline pcg128_t
1575 pcg_oneseq_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_128* rng,
1578 pcg128_t threshold = -bound % bound;
1580 pcg128_t r = pcg_oneseq_128_rxs_m_xs_128_random_r(rng);
1587 static inline uint16_t pcg_unique_16_rxs_m_xs_16_random_r(struct pcg_state_16* rng)
1589 uint16_t oldstate = rng->state;
1590 pcg_unique_16_step_r(rng);
1591 return pcg_output_rxs_m_xs_16_16(oldstate);
1594 static inline uint16_t
1595 pcg_unique_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_16* rng,
1598 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1600 uint16_t r = pcg_unique_16_rxs_m_xs_16_random_r(rng);
1606 static inline uint32_t pcg_unique_32_rxs_m_xs_32_random_r(struct pcg_state_32* rng)
1608 uint32_t oldstate = rng->state;
1609 pcg_unique_32_step_r(rng);
1610 return pcg_output_rxs_m_xs_32_32(oldstate);
1613 static inline uint32_t
1614 pcg_unique_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_32* rng,
1617 uint32_t threshold = -bound % bound;
1619 uint32_t r = pcg_unique_32_rxs_m_xs_32_random_r(rng);
1625 static inline uint64_t pcg_unique_64_rxs_m_xs_64_random_r(struct pcg_state_64* rng)
1627 uint64_t oldstate = rng->state;
1628 pcg_unique_64_step_r(rng);
1629 return pcg_output_rxs_m_xs_64_64(oldstate);
1632 static inline uint64_t
1633 pcg_unique_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_64* rng,
1636 uint64_t threshold = -bound % bound;
1638 uint64_t r = pcg_unique_64_rxs_m_xs_64_random_r(rng);
1644 #if PCG_HAS_128BIT_OPS
1645 static inline pcg128_t pcg_unique_128_rxs_m_xs_128_random_r(struct pcg_state_128* rng)
1647 pcg_unique_128_step_r(rng);
1648 return pcg_output_rxs_m_xs_128_128(rng->state);
1652 #if PCG_HAS_128BIT_OPS
1653 static inline pcg128_t
1654 pcg_unique_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_128* rng,
1657 pcg128_t threshold = -bound % bound;
1659 pcg128_t r = pcg_unique_128_rxs_m_xs_128_random_r(rng);
1666 static inline uint8_t pcg_setseq_8_rxs_m_xs_8_random_r(struct pcg_state_setseq_8* rng)
1668 uint8_t oldstate = rng->state;
1669 pcg_setseq_8_step_r(rng);
1670 return pcg_output_rxs_m_xs_8_8(oldstate);
1673 static inline uint8_t
1674 pcg_setseq_8_rxs_m_xs_8_boundedrand_r(struct pcg_state_setseq_8* rng,
1677 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1679 uint8_t r = pcg_setseq_8_rxs_m_xs_8_random_r(rng);
1685 static inline uint16_t
1686 pcg_setseq_16_rxs_m_xs_16_random_r(struct pcg_state_setseq_16* rng)
1688 uint16_t oldstate = rng->state;
1689 pcg_setseq_16_step_r(rng);
1690 return pcg_output_rxs_m_xs_16_16(oldstate);
1693 static inline uint16_t
1694 pcg_setseq_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_setseq_16* rng,
1697 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1699 uint16_t r = pcg_setseq_16_rxs_m_xs_16_random_r(rng);
1705 static inline uint32_t
1706 pcg_setseq_32_rxs_m_xs_32_random_r(struct pcg_state_setseq_32* rng)
1708 uint32_t oldstate = rng->state;
1709 pcg_setseq_32_step_r(rng);
1710 return pcg_output_rxs_m_xs_32_32(oldstate);
1713 static inline uint32_t
1714 pcg_setseq_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_setseq_32* rng,
1717 uint32_t threshold = -bound % bound;
1719 uint32_t r = pcg_setseq_32_rxs_m_xs_32_random_r(rng);
1725 static inline uint64_t
1726 pcg_setseq_64_rxs_m_xs_64_random_r(struct pcg_state_setseq_64* rng)
1728 uint64_t oldstate = rng->state;
1729 pcg_setseq_64_step_r(rng);
1730 return pcg_output_rxs_m_xs_64_64(oldstate);
1733 static inline uint64_t
1734 pcg_setseq_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_setseq_64* rng,
1737 uint64_t threshold = -bound % bound;
1739 uint64_t r = pcg_setseq_64_rxs_m_xs_64_random_r(rng);
1745 #if PCG_HAS_128BIT_OPS
1746 static inline pcg128_t
1747 pcg_setseq_128_rxs_m_xs_128_random_r(struct pcg_state_setseq_128* rng)
1749 pcg_setseq_128_step_r(rng);
1750 return pcg_output_rxs_m_xs_128_128(rng->state);
1754 #if PCG_HAS_128BIT_OPS
1755 static inline pcg128_t
1756 pcg_setseq_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_setseq_128* rng,
1759 pcg128_t threshold = -bound % bound;
1761 pcg128_t r = pcg_setseq_128_rxs_m_xs_128_random_r(rng);
1768 /* Generation functions for RXS M */
1770 static inline uint8_t pcg_oneseq_16_rxs_m_8_random_r(struct pcg_state_16* rng)
1772 uint16_t oldstate = rng->state;
1773 pcg_oneseq_16_step_r(rng);
1774 return pcg_output_rxs_m_16_8(oldstate);
1777 static inline uint8_t pcg_oneseq_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
1780 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1782 uint8_t r = pcg_oneseq_16_rxs_m_8_random_r(rng);
1788 static inline uint16_t pcg_oneseq_32_rxs_m_16_random_r(struct pcg_state_32* rng)
1790 uint32_t oldstate = rng->state;
1791 pcg_oneseq_32_step_r(rng);
1792 return pcg_output_rxs_m_32_16(oldstate);
1795 static inline uint16_t pcg_oneseq_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
1798 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1800 uint16_t r = pcg_oneseq_32_rxs_m_16_random_r(rng);
1806 static inline uint32_t pcg_oneseq_64_rxs_m_32_random_r(struct pcg_state_64* rng)
1808 uint64_t oldstate = rng->state;
1809 pcg_oneseq_64_step_r(rng);
1810 return pcg_output_rxs_m_64_32(oldstate);
1813 static inline uint32_t pcg_oneseq_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
1816 uint32_t threshold = -bound % bound;
1818 uint32_t r = pcg_oneseq_64_rxs_m_32_random_r(rng);
1824 #if PCG_HAS_128BIT_OPS
1825 static inline uint64_t pcg_oneseq_128_rxs_m_64_random_r(struct pcg_state_128* rng)
1827 pcg_oneseq_128_step_r(rng);
1828 return pcg_output_rxs_m_128_64(rng->state);
1832 #if PCG_HAS_128BIT_OPS
1833 static inline uint64_t pcg_oneseq_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
1836 uint64_t threshold = -bound % bound;
1838 uint64_t r = pcg_oneseq_128_rxs_m_64_random_r(rng);
1845 static inline uint8_t pcg_unique_16_rxs_m_8_random_r(struct pcg_state_16* rng)
1847 uint16_t oldstate = rng->state;
1848 pcg_unique_16_step_r(rng);
1849 return pcg_output_rxs_m_16_8(oldstate);
1852 static inline uint8_t pcg_unique_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
1855 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1857 uint8_t r = pcg_unique_16_rxs_m_8_random_r(rng);
1863 static inline uint16_t pcg_unique_32_rxs_m_16_random_r(struct pcg_state_32* rng)
1865 uint32_t oldstate = rng->state;
1866 pcg_unique_32_step_r(rng);
1867 return pcg_output_rxs_m_32_16(oldstate);
1870 static inline uint16_t pcg_unique_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
1873 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1875 uint16_t r = pcg_unique_32_rxs_m_16_random_r(rng);
1881 static inline uint32_t pcg_unique_64_rxs_m_32_random_r(struct pcg_state_64* rng)
1883 uint64_t oldstate = rng->state;
1884 pcg_unique_64_step_r(rng);
1885 return pcg_output_rxs_m_64_32(oldstate);
1888 static inline uint32_t pcg_unique_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
1891 uint32_t threshold = -bound % bound;
1893 uint32_t r = pcg_unique_64_rxs_m_32_random_r(rng);
1899 #if PCG_HAS_128BIT_OPS
1900 static inline uint64_t pcg_unique_128_rxs_m_64_random_r(struct pcg_state_128* rng)
1902 pcg_unique_128_step_r(rng);
1903 return pcg_output_rxs_m_128_64(rng->state);
1907 #if PCG_HAS_128BIT_OPS
1908 static inline uint64_t pcg_unique_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
1911 uint64_t threshold = -bound % bound;
1913 uint64_t r = pcg_unique_128_rxs_m_64_random_r(rng);
1920 static inline uint8_t pcg_setseq_16_rxs_m_8_random_r(struct pcg_state_setseq_16* rng)
1922 uint16_t oldstate = rng->state;
1923 pcg_setseq_16_step_r(rng);
1924 return pcg_output_rxs_m_16_8(oldstate);
1927 static inline uint8_t
1928 pcg_setseq_16_rxs_m_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1931 uint8_t threshold = ((uint8_t)(-bound)) % bound;
1933 uint8_t r = pcg_setseq_16_rxs_m_8_random_r(rng);
1939 static inline uint16_t pcg_setseq_32_rxs_m_16_random_r(struct pcg_state_setseq_32* rng)
1941 uint32_t oldstate = rng->state;
1942 pcg_setseq_32_step_r(rng);
1943 return pcg_output_rxs_m_32_16(oldstate);
1946 static inline uint16_t
1947 pcg_setseq_32_rxs_m_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1950 uint16_t threshold = ((uint16_t)(-bound)) % bound;
1952 uint16_t r = pcg_setseq_32_rxs_m_16_random_r(rng);
1958 static inline uint32_t pcg_setseq_64_rxs_m_32_random_r(struct pcg_state_setseq_64* rng)
1960 uint64_t oldstate = rng->state;
1961 pcg_setseq_64_step_r(rng);
1962 return pcg_output_rxs_m_64_32(oldstate);
1965 static inline uint32_t
1966 pcg_setseq_64_rxs_m_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1969 uint32_t threshold = -bound % bound;
1971 uint32_t r = pcg_setseq_64_rxs_m_32_random_r(rng);
1977 #if PCG_HAS_128BIT_OPS
1978 static inline uint64_t
1979 pcg_setseq_128_rxs_m_64_random_r(struct pcg_state_setseq_128* rng)
1981 pcg_setseq_128_step_r(rng);
1982 return pcg_output_rxs_m_128_64(rng->state);
1986 #if PCG_HAS_128BIT_OPS
1987 static inline uint64_t
1988 pcg_setseq_128_rxs_m_64_boundedrand_r(struct pcg_state_setseq_128* rng,
1991 uint64_t threshold = -bound % bound;
1993 uint64_t r = pcg_setseq_128_rxs_m_64_random_r(rng);
2000 static inline uint8_t pcg_mcg_16_rxs_m_8_random_r(struct pcg_state_16* rng)
2002 uint16_t oldstate = rng->state;
2003 pcg_mcg_16_step_r(rng);
2004 return pcg_output_rxs_m_16_8(oldstate);
2007 static inline uint8_t pcg_mcg_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
2010 uint8_t threshold = ((uint8_t)(-bound)) % bound;
2012 uint8_t r = pcg_mcg_16_rxs_m_8_random_r(rng);
2018 static inline uint16_t pcg_mcg_32_rxs_m_16_random_r(struct pcg_state_32* rng)
2020 uint32_t oldstate = rng->state;
2021 pcg_mcg_32_step_r(rng);
2022 return pcg_output_rxs_m_32_16(oldstate);
2025 static inline uint16_t pcg_mcg_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
2028 uint16_t threshold = ((uint16_t)(-bound)) % bound;
2030 uint16_t r = pcg_mcg_32_rxs_m_16_random_r(rng);
2036 static inline uint32_t pcg_mcg_64_rxs_m_32_random_r(struct pcg_state_64* rng)
2038 uint64_t oldstate = rng->state;
2039 pcg_mcg_64_step_r(rng);
2040 return pcg_output_rxs_m_64_32(oldstate);
2043 static inline uint32_t pcg_mcg_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
2046 uint32_t threshold = -bound % bound;
2048 uint32_t r = pcg_mcg_64_rxs_m_32_random_r(rng);
2054 #if PCG_HAS_128BIT_OPS
2055 static inline uint64_t pcg_mcg_128_rxs_m_64_random_r(struct pcg_state_128* rng)
2057 pcg_mcg_128_step_r(rng);
2058 return pcg_output_rxs_m_128_64(rng->state);
2062 #if PCG_HAS_128BIT_OPS
2063 static inline uint64_t pcg_mcg_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
2066 uint64_t threshold = -bound % bound;
2068 uint64_t r = pcg_mcg_128_rxs_m_64_random_r(rng);
2075 /* Generation functions for XSL RR (only defined for "large" types) */
2077 static inline uint32_t pcg_oneseq_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2079 uint64_t oldstate = rng->state;
2080 pcg_oneseq_64_step_r(rng);
2081 return pcg_output_xsl_rr_64_32(oldstate);
2084 static inline uint32_t pcg_oneseq_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2087 uint32_t threshold = -bound % bound;
2089 uint32_t r = pcg_oneseq_64_xsl_rr_32_random_r(rng);
2095 #if PCG_HAS_128BIT_OPS
2096 static inline uint64_t pcg_oneseq_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2098 pcg_oneseq_128_step_r(rng);
2099 return pcg_output_xsl_rr_128_64(rng->state);
2103 #if PCG_HAS_128BIT_OPS
2104 static inline uint64_t
2105 pcg_oneseq_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2108 uint64_t threshold = -bound % bound;
2110 uint64_t r = pcg_oneseq_128_xsl_rr_64_random_r(rng);
2117 static inline uint32_t pcg_unique_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2119 uint64_t oldstate = rng->state;
2120 pcg_unique_64_step_r(rng);
2121 return pcg_output_xsl_rr_64_32(oldstate);
2124 static inline uint32_t pcg_unique_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2127 uint32_t threshold = -bound % bound;
2129 uint32_t r = pcg_unique_64_xsl_rr_32_random_r(rng);
2135 #if PCG_HAS_128BIT_OPS
2136 static inline uint64_t pcg_unique_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2138 pcg_unique_128_step_r(rng);
2139 return pcg_output_xsl_rr_128_64(rng->state);
2143 #if PCG_HAS_128BIT_OPS
2144 static inline uint64_t
2145 pcg_unique_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2148 uint64_t threshold = -bound % bound;
2150 uint64_t r = pcg_unique_128_xsl_rr_64_random_r(rng);
2157 static inline uint32_t
2158 pcg_setseq_64_xsl_rr_32_random_r(struct pcg_state_setseq_64* rng)
2160 uint64_t oldstate = rng->state;
2161 pcg_setseq_64_step_r(rng);
2162 return pcg_output_xsl_rr_64_32(oldstate);
2165 static inline uint32_t
2166 pcg_setseq_64_xsl_rr_32_boundedrand_r(struct pcg_state_setseq_64* rng,
2169 uint32_t threshold = -bound % bound;
2171 uint32_t r = pcg_setseq_64_xsl_rr_32_random_r(rng);
2177 #if PCG_HAS_128BIT_OPS
2178 static inline uint64_t
2179 pcg_setseq_128_xsl_rr_64_random_r(struct pcg_state_setseq_128* rng)
2181 pcg_setseq_128_step_r(rng);
2182 return pcg_output_xsl_rr_128_64(rng->state);
2186 #if PCG_HAS_128BIT_OPS
2187 static inline uint64_t
2188 pcg_setseq_128_xsl_rr_64_boundedrand_r(struct pcg_state_setseq_128* rng,
2191 uint64_t threshold = -bound % bound;
2193 uint64_t r = pcg_setseq_128_xsl_rr_64_random_r(rng);
2200 static inline uint32_t pcg_mcg_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2202 uint64_t oldstate = rng->state;
2203 pcg_mcg_64_step_r(rng);
2204 return pcg_output_xsl_rr_64_32(oldstate);
2207 static inline uint32_t pcg_mcg_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2210 uint32_t threshold = -bound % bound;
2212 uint32_t r = pcg_mcg_64_xsl_rr_32_random_r(rng);
2218 #if PCG_HAS_128BIT_OPS
2219 static inline uint64_t pcg_mcg_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2221 pcg_mcg_128_step_r(rng);
2222 return pcg_output_xsl_rr_128_64(rng->state);
2226 #if PCG_HAS_128BIT_OPS
2227 static inline uint64_t pcg_mcg_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2230 uint64_t threshold = -bound % bound;
2232 uint64_t r = pcg_mcg_128_xsl_rr_64_random_r(rng);
2239 /* Generation functions for XSL RR RR (only defined for "large" types) */
2241 static inline uint64_t pcg_oneseq_64_xsl_rr_rr_64_random_r(struct pcg_state_64* rng)
2243 uint64_t oldstate = rng->state;
2244 pcg_oneseq_64_step_r(rng);
2245 return pcg_output_xsl_rr_rr_64_64(oldstate);
2248 static inline uint64_t
2249 pcg_oneseq_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_64* rng,
2252 uint64_t threshold = -bound % bound;
2254 uint64_t r = pcg_oneseq_64_xsl_rr_rr_64_random_r(rng);
2260 #if PCG_HAS_128BIT_OPS
2261 static inline pcg128_t pcg_oneseq_128_xsl_rr_rr_128_random_r(struct pcg_state_128* rng)
2263 pcg_oneseq_128_step_r(rng);
2264 return pcg_output_xsl_rr_rr_128_128(rng->state);
2268 #if PCG_HAS_128BIT_OPS
2269 static inline pcg128_t
2270 pcg_oneseq_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_128* rng,
2273 pcg128_t threshold = -bound % bound;
2275 pcg128_t r = pcg_oneseq_128_xsl_rr_rr_128_random_r(rng);
2282 static inline uint64_t pcg_unique_64_xsl_rr_rr_64_random_r(struct pcg_state_64* rng)
2284 uint64_t oldstate = rng->state;
2285 pcg_unique_64_step_r(rng);
2286 return pcg_output_xsl_rr_rr_64_64(oldstate);
2289 static inline uint64_t
2290 pcg_unique_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_64* rng,
2293 uint64_t threshold = -bound % bound;
2295 uint64_t r = pcg_unique_64_xsl_rr_rr_64_random_r(rng);
2301 #if PCG_HAS_128BIT_OPS
2302 static inline pcg128_t pcg_unique_128_xsl_rr_rr_128_random_r(struct pcg_state_128* rng)
2304 pcg_unique_128_step_r(rng);
2305 return pcg_output_xsl_rr_rr_128_128(rng->state);
2309 #if PCG_HAS_128BIT_OPS
2310 static inline pcg128_t
2311 pcg_unique_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_128* rng,
2314 pcg128_t threshold = -bound % bound;
2316 pcg128_t r = pcg_unique_128_xsl_rr_rr_128_random_r(rng);
2323 static inline uint64_t
2324 pcg_setseq_64_xsl_rr_rr_64_random_r(struct pcg_state_setseq_64* rng)
2326 uint64_t oldstate = rng->state;
2327 pcg_setseq_64_step_r(rng);
2328 return pcg_output_xsl_rr_rr_64_64(oldstate);
2331 static inline uint64_t
2332 pcg_setseq_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_setseq_64* rng,
2335 uint64_t threshold = -bound % bound;
2337 uint64_t r = pcg_setseq_64_xsl_rr_rr_64_random_r(rng);
2343 #if PCG_HAS_128BIT_OPS
2344 static inline pcg128_t
2345 pcg_setseq_128_xsl_rr_rr_128_random_r(struct pcg_state_setseq_128* rng)
2347 pcg_setseq_128_step_r(rng);
2348 return pcg_output_xsl_rr_rr_128_128(rng->state);
2352 #if PCG_HAS_128BIT_OPS
2353 static inline pcg128_t
2354 pcg_setseq_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_setseq_128* rng,
2357 pcg128_t threshold = -bound % bound;
2359 pcg128_t r = pcg_setseq_128_xsl_rr_rr_128_random_r(rng);
2367 typedef struct pcg_state_setseq_64 pcg32_random_t;
2368 typedef struct pcg_state_64 pcg32s_random_t;
2369 typedef struct pcg_state_64 pcg32u_random_t;
2370 typedef struct pcg_state_64 pcg32f_random_t;
2372 #define pcg32_random_r pcg_setseq_64_xsh_rr_32_random_r
2373 #define pcg32s_random_r pcg_oneseq_64_xsh_rr_32_random_r
2374 #define pcg32u_random_r pcg_unique_64_xsh_rr_32_random_r
2375 #define pcg32f_random_r pcg_mcg_64_xsh_rs_32_random_r
2376 /*** boundedrand_r */
2377 #define pcg32_boundedrand_r pcg_setseq_64_xsh_rr_32_boundedrand_r
2378 #define pcg32s_boundedrand_r pcg_oneseq_64_xsh_rr_32_boundedrand_r
2379 #define pcg32u_boundedrand_r pcg_unique_64_xsh_rr_32_boundedrand_r
2380 #define pcg32f_boundedrand_r pcg_mcg_64_xsh_rs_32_boundedrand_r
2382 #define pcg32_srandom_r pcg_setseq_64_srandom_r
2383 #define pcg32s_srandom_r pcg_oneseq_64_srandom_r
2384 #define pcg32u_srandom_r pcg_unique_64_srandom_r
2385 #define pcg32f_srandom_r pcg_mcg_64_srandom_r
2387 #define pcg32_advance_r pcg_setseq_64_advance_r
2388 #define pcg32s_advance_r pcg_oneseq_64_advance_r
2389 #define pcg32u_advance_r pcg_unique_64_advance_r
2390 #define pcg32f_advance_r pcg_mcg_64_advance_r
2392 #if PCG_HAS_128BIT_OPS
2394 typedef struct pcg_state_setseq_128 pcg64_random_t;
2395 typedef struct pcg_state_128 pcg64s_random_t;
2396 typedef struct pcg_state_128 pcg64u_random_t;
2397 typedef struct pcg_state_128 pcg64f_random_t;
2399 #define pcg64_random_r pcg_setseq_128_xsl_rr_64_random_r
2400 #define pcg64s_random_r pcg_oneseq_128_xsl_rr_64_random_r
2401 #define pcg64u_random_r pcg_unique_128_xsl_rr_64_random_r
2402 #define pcg64f_random_r pcg_mcg_128_xsl_rr_64_random_r
2403 /*** boundedrand_r */
2404 #define pcg64_boundedrand_r pcg_setseq_128_xsl_rr_64_boundedrand_r
2405 #define pcg64s_boundedrand_r pcg_oneseq_128_xsl_rr_64_boundedrand_r
2406 #define pcg64u_boundedrand_r pcg_unique_128_xsl_rr_64_boundedrand_r
2407 #define pcg64f_boundedrand_r pcg_mcg_128_xsl_rr_64_boundedrand_r
2409 #define pcg64_srandom_r pcg_setseq_128_srandom_r
2410 #define pcg64s_srandom_r pcg_oneseq_128_srandom_r
2411 #define pcg64u_srandom_r pcg_unique_128_srandom_r
2412 #define pcg64f_srandom_r pcg_mcg_128_srandom_r
2414 #define pcg64_advance_r pcg_setseq_128_advance_r
2415 #define pcg64s_advance_r pcg_oneseq_128_advance_r
2416 #define pcg64u_advance_r pcg_unique_128_advance_r
2417 #define pcg64f_advance_r pcg_mcg_128_advance_r
2421 typedef struct pcg_state_8 pcg8si_random_t;
2422 typedef struct pcg_state_16 pcg16si_random_t;
2423 typedef struct pcg_state_32 pcg32si_random_t;
2424 typedef struct pcg_state_64 pcg64si_random_t;
2426 #define pcg8si_random_r pcg_oneseq_8_rxs_m_xs_8_random_r
2427 #define pcg16si_random_r pcg_oneseq_16_rxs_m_xs_16_random_r
2428 #define pcg32si_random_r pcg_oneseq_32_rxs_m_xs_32_random_r
2429 #define pcg64si_random_r pcg_oneseq_64_rxs_m_xs_64_random_r
2430 /*** boundedrand_r */
2431 #define pcg8si_boundedrand_r pcg_oneseq_8_rxs_m_xs_8_boundedrand_r
2432 #define pcg16si_boundedrand_r pcg_oneseq_16_rxs_m_xs_16_boundedrand_r
2433 #define pcg32si_boundedrand_r pcg_oneseq_32_rxs_m_xs_32_boundedrand_r
2434 #define pcg64si_boundedrand_r pcg_oneseq_64_rxs_m_xs_64_boundedrand_r
2436 #define pcg8si_srandom_r pcg_oneseq_8_srandom_r
2437 #define pcg16si_srandom_r pcg_oneseq_16_srandom_r
2438 #define pcg32si_srandom_r pcg_oneseq_32_srandom_r
2439 #define pcg64si_srandom_r pcg_oneseq_64_srandom_r
2441 #define pcg8si_advance_r pcg_oneseq_8_advance_r
2442 #define pcg16si_advance_r pcg_oneseq_16_advance_r
2443 #define pcg32si_advance_r pcg_oneseq_32_advance_r
2444 #define pcg64si_advance_r pcg_oneseq_64_advance_r
2446 #if PCG_HAS_128BIT_OPS
2447 typedef struct pcg_state_128 pcg128si_random_t;
2448 #define pcg128si_random_r pcg_oneseq_128_rxs_m_xs_128_random_r
2449 #define pcg128si_boundedrand_r pcg_oneseq_128_rxs_m_xs_128_boundedrand_r
2450 #define pcg128si_srandom_r pcg_oneseq_128_srandom_r
2451 #define pcg128si_advance_r pcg_oneseq_128_advance_r
2455 typedef struct pcg_state_setseq_8 pcg8i_random_t;
2456 typedef struct pcg_state_setseq_16 pcg16i_random_t;
2457 typedef struct pcg_state_setseq_32 pcg32i_random_t;
2458 typedef struct pcg_state_setseq_64 pcg64i_random_t;
2460 #define pcg8i_random_r pcg_setseq_8_rxs_m_xs_8_random_r
2461 #define pcg16i_random_r pcg_setseq_16_rxs_m_xs_16_random_r
2462 #define pcg32i_random_r pcg_setseq_32_rxs_m_xs_32_random_r
2463 #define pcg64i_random_r pcg_setseq_64_rxs_m_xs_64_random_r
2464 /*** boundedrand_r */
2465 #define pcg8i_boundedrand_r pcg_setseq_8_rxs_m_xs_8_boundedrand_r
2466 #define pcg16i_boundedrand_r pcg_setseq_16_rxs_m_xs_16_boundedrand_r
2467 #define pcg32i_boundedrand_r pcg_setseq_32_rxs_m_xs_32_boundedrand_r
2468 #define pcg64i_boundedrand_r pcg_setseq_64_rxs_m_xs_64_boundedrand_r
2470 #define pcg8i_srandom_r pcg_setseq_8_srandom_r
2471 #define pcg16i_srandom_r pcg_setseq_16_srandom_r
2472 #define pcg32i_srandom_r pcg_setseq_32_srandom_r
2473 #define pcg64i_srandom_r pcg_setseq_64_srandom_r
2475 #define pcg8i_advance_r pcg_setseq_8_advance_r
2476 #define pcg16i_advance_r pcg_setseq_16_advance_r
2477 #define pcg32i_advance_r pcg_setseq_32_advance_r
2478 #define pcg64i_advance_r pcg_setseq_64_advance_r
2480 #if PCG_HAS_128BIT_OPS
2481 typedef struct pcg_state_setseq_128 pcg128i_random_t;
2482 #define pcg128i_random_r pcg_setseq_128_rxs_m_xs_128_random_r
2483 #define pcg128i_boundedrand_r pcg_setseq_128_rxs_m_xs_128_boundedrand_r
2484 #define pcg128i_srandom_r pcg_setseq_128_srandom_r
2485 #define pcg128i_advance_r pcg_setseq_128_advance_r
2489 * Static initialization constants (if you can't call srandom for some
2493 #define PCG32_INITIALIZER PCG_STATE_SETSEQ_64_INITIALIZER
2494 #define PCG32U_INITIALIZER PCG_STATE_UNIQUE_64_INITIALIZER
2495 #define PCG32S_INITIALIZER PCG_STATE_ONESEQ_64_INITIALIZER
2496 #define PCG32F_INITIALIZER PCG_STATE_MCG_64_INITIALIZER
2498 #if PCG_HAS_128BIT_OPS
2499 #define PCG64_INITIALIZER PCG_STATE_SETSEQ_128_INITIALIZER
2500 #define PCG64U_INITIALIZER PCG_STATE_UNIQUE_128_INITIALIZER
2501 #define PCG64S_INITIALIZER PCG_STATE_ONESEQ_128_INITIALIZER
2502 #define PCG64F_INITIALIZER PCG_STATE_MCG_128_INITIALIZER
2505 #define PCG8SI_INITIALIZER PCG_STATE_ONESEQ_8_INITIALIZER
2506 #define PCG16SI_INITIALIZER PCG_STATE_ONESEQ_16_INITIALIZER
2507 #define PCG32SI_INITIALIZER PCG_STATE_ONESEQ_32_INITIALIZER
2508 #define PCG64SI_INITIALIZER PCG_STATE_ONESEQ_64_INITIALIZER
2509 #if PCG_HAS_128BIT_OPS
2510 #define PCG128SI_INITIALIZER PCG_STATE_ONESEQ_128_INITIALIZER
2513 #define PCG8I_INITIALIZER PCG_STATE_SETSEQ_8_INITIALIZER
2514 #define PCG16I_INITIALIZER PCG_STATE_SETSEQ_16_INITIALIZER
2515 #define PCG32I_INITIALIZER PCG_STATE_SETSEQ_32_INITIALIZER
2516 #define PCG64I_INITIALIZER PCG_STATE_SETSEQ_64_INITIALIZER
2517 #if PCG_HAS_128BIT_OPS
2518 #define PCG128I_INITIALIZER PCG_STATE_SETSEQ_128_INITIALIZER
2525 #endif /* PCG_VARIANTS_H_INCLUDED */