]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/pcg_variants.h
Import PCG-C master, 2019-07-18 (83252d9c23df9c82ecb42210afed61a7b42402d7)
[FreeBSD/FreeBSD.git] / include / pcg_variants.h
1 /*
2  * PCG Random Number Generation for C.
3  *
4  * Copyright 2014-2019 Melissa O'Neill <oneill@pcg-random.org>,
5  *                     and the PCG Project contributors.
6  *
7  * SPDX-License-Identifier: (Apache-2.0 OR MIT)
8  *
9  * Licensed under the Apache License, Version 2.0 (provided in
10  * LICENSE-APACHE.txt and at http://www.apache.org/licenses/LICENSE-2.0)
11  * or under the MIT license (provided in LICENSE-MIT.txt and at
12  * http://opensource.org/licenses/MIT), at your option. This file may not
13  * be copied, modified, or distributed except according to those terms.
14  *
15  * Distributed on an "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied.  See your chosen license for details.
17  *
18  * For additional information about the PCG random number generation scheme,
19  * visit http://www.pcg-random.org/.
20  */
21
22 /*
23  * This code is derived from the canonical C++ PCG implementation, which
24  * has many additional features and is preferable if you can use C++ in
25  * your project.
26  *
27  * 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.
32  *
33  * Much of the remaining code was generated by C-preprocessor metaprogramming.
34  */
35
36 #ifndef PCG_VARIANTS_H_INCLUDED
37 #define PCG_VARIANTS_H_INCLUDED 1
38
39 #include <inttypes.h>
40
41 #if __SIZEOF_INT128__
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
46 #endif
47
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. */
52 #endif
53
54 #if __cplusplus
55 extern "C" {
56 #endif
57
58 /*
59  * Rotate helper functions.
60  */
61
62 inline uint8_t pcg_rotr_8(uint8_t value, unsigned int rot)
63 {
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.
67  */
68 #if PCG_USE_INLINE_ASM && __clang__ && (__x86_64__  || __i386__)
69     asm ("rorb   %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
70     return value;
71 #else
72     return (value >> rot) | (value << ((- rot) & 7));
73 #endif
74 }
75
76 inline uint16_t pcg_rotr_16(uint16_t value, unsigned int rot)
77 {
78 #if PCG_USE_INLINE_ASM && __clang__ && (__x86_64__  || __i386__)
79     asm ("rorw   %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
80     return value;
81 #else
82     return (value >> rot) | (value << ((- rot) & 15));
83 #endif
84 }
85
86 inline uint32_t pcg_rotr_32(uint32_t value, unsigned int rot)
87 {
88 #if PCG_USE_INLINE_ASM && __clang__ && (__x86_64__  || __i386__)
89     asm ("rorl   %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
90     return value;
91 #else
92     return (value >> rot) | (value << ((- rot) & 31));
93 #endif
94 }
95
96 inline uint64_t pcg_rotr_64(uint64_t value, unsigned int rot)
97 {
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));
102     return value;
103 #else
104     return (value >> rot) | (value << ((- rot) & 63));
105 #endif
106 }
107
108 #if PCG_HAS_128BIT_OPS
109 inline pcg128_t pcg_rotr_128(pcg128_t value, unsigned int rot)
110 {
111     return (value >> rot) | (value << ((- rot) & 127));
112 }
113 #endif
114
115 /*
116  * Output functions.  These are the core of the PCG generation scheme.
117  */
118
119 /* XSH RS */
120
121 inline uint8_t pcg_output_xsh_rs_16_8(uint16_t state)
122 {
123     return (uint8_t)(((state >> 7u) ^ state) >> ((state >> 14u) + 3u));
124 }
125
126 inline uint16_t pcg_output_xsh_rs_32_16(uint32_t state)
127 {
128     return (uint16_t)(((state >> 11u) ^ state) >> ((state >> 30u) + 11u));
129 }
130
131 inline uint32_t pcg_output_xsh_rs_64_32(uint64_t state)
132 {
133
134     return (uint32_t)(((state >> 22u) ^ state) >> ((state >> 61u) + 22u));
135 }
136
137 #if PCG_HAS_128BIT_OPS
138 inline uint64_t pcg_output_xsh_rs_128_64(pcg128_t state)
139 {
140     return (uint64_t)(((state >> 43u) ^ state) >> ((state >> 124u) + 45u));
141 }
142 #endif
143
144 /* XSH RR */
145
146 inline uint8_t pcg_output_xsh_rr_16_8(uint16_t state)
147 {
148     return pcg_rotr_8(((state >> 5u) ^ state) >> 5u, state >> 13u);
149 }
150
151 inline uint16_t pcg_output_xsh_rr_32_16(uint32_t state)
152 {
153     return pcg_rotr_16(((state >> 10u) ^ state) >> 12u, state >> 28u);
154 }
155
156 inline uint32_t pcg_output_xsh_rr_64_32(uint64_t state)
157 {
158     return pcg_rotr_32(((state >> 18u) ^ state) >> 27u, state >> 59u);
159 }
160
161 #if PCG_HAS_128BIT_OPS
162 inline uint64_t pcg_output_xsh_rr_128_64(pcg128_t state)
163 {
164     return pcg_rotr_64(((state >> 35u) ^ state) >> 58u, state >> 122u);
165 }
166 #endif
167
168 /* RXS M XS */
169
170 inline uint8_t pcg_output_rxs_m_xs_8_8(uint8_t state)
171 {
172     uint8_t word = ((state >> ((state >> 6u) + 2u)) ^ state) * 217u;
173     return (word >> 6u) ^ word;
174 }
175
176 inline uint16_t pcg_output_rxs_m_xs_16_16(uint16_t state)
177 {
178     uint16_t word = ((state >> ((state >> 13u) + 3u)) ^ state) * 62169u;
179     return (word >> 11u) ^ word;
180 }
181
182 inline uint32_t pcg_output_rxs_m_xs_32_32(uint32_t state)
183 {
184     uint32_t word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
185     return (word >> 22u) ^ word;
186 }
187
188 inline uint64_t pcg_output_rxs_m_xs_64_64(uint64_t state)
189 {
190     uint64_t word = ((state >> ((state >> 59u) + 5u)) ^ state)
191                     * 12605985483714917081ull;
192     return (word >> 43u) ^ word;
193 }
194
195 #if PCG_HAS_128BIT_OPS
196 inline pcg128_t pcg_output_rxs_m_xs_128_128(pcg128_t state)
197 {
198     pcg128_t word = ((state >> ((state >> 122u) + 6u)) ^ state)
199                        * (PCG_128BIT_CONSTANT(17766728186571221404ULL,
200                                               12605985483714917081ULL));
201     /* 327738287884841127335028083622016905945 */
202     return (word >> 86u) ^ word;
203 }
204 #endif
205
206 /* RXS M */
207
208 inline uint8_t pcg_output_rxs_m_16_8(uint16_t state)
209 {
210     return (((state >> ((state >> 13u) + 3u)) ^ state) * 62169u) >> 8u;
211 }
212
213 inline uint16_t pcg_output_rxs_m_32_16(uint32_t state)
214 {
215     return (((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u) >> 16u;
216 }
217
218 inline uint32_t pcg_output_rxs_m_64_32(uint64_t state)
219 {
220     return (((state >> ((state >> 59u) + 5u)) ^ state)
221                * 12605985483714917081ull) >> 32u;
222 }
223
224 #if PCG_HAS_128BIT_OPS
225 inline uint64_t pcg_output_rxs_m_128_64(pcg128_t state)
226 {
227     return (((state >> ((state >> 122u) + 6u)) ^ state)
228                * (PCG_128BIT_CONSTANT(17766728186571221404ULL,
229                                       12605985483714917081ULL))) >> 64u;
230     /* 327738287884841127335028083622016905945 */
231 }
232 #endif
233
234 /* XSL RR (only defined for >= 64 bits) */
235
236 inline uint32_t pcg_output_xsl_rr_64_32(uint64_t state)
237 {
238     return pcg_rotr_32(((uint32_t)(state >> 32u)) ^ (uint32_t)state,
239                        state >> 59u);
240 }
241
242 #if PCG_HAS_128BIT_OPS
243 inline uint64_t pcg_output_xsl_rr_128_64(pcg128_t state)
244 {
245     return pcg_rotr_64(((uint64_t)(state >> 64u)) ^ (uint64_t)state,
246                        state >> 122u);
247 }
248 #endif
249
250 /* XSL RR RR (only defined for >= 64 bits) */
251
252 inline uint64_t pcg_output_xsl_rr_rr_64_64(uint64_t state)
253 {
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;
261 }
262
263 #if PCG_HAS_128BIT_OPS
264 inline pcg128_t pcg_output_xsl_rr_rr_128_128(pcg128_t state)
265 {
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;
273 }
274 #endif
275
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
280
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
285
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)
291 #endif
292
293 /*
294  * Static initialization constants (if you can't call srandom for some
295  * bizarre reason).
296  */
297
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) }
305 #endif
306
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
313 #endif
314
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) }
322 #endif
323
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) }
333 #endif
334
335 /* Representations for the oneseq, mcg, and unique variants */
336
337 struct pcg_state_8 {
338     uint8_t state;
339 };
340
341 struct pcg_state_16 {
342     uint16_t state;
343 };
344
345 struct pcg_state_32 {
346     uint32_t state;
347 };
348
349 struct pcg_state_64 {
350     uint64_t state;
351 };
352
353 #if PCG_HAS_128BIT_OPS
354 struct pcg_state_128 {
355     pcg128_t state;
356 };
357 #endif
358
359 /* Representations setseq variants */
360
361 struct pcg_state_setseq_8 {
362     uint8_t state;
363     uint8_t inc;
364 };
365
366 struct pcg_state_setseq_16 {
367     uint16_t state;
368     uint16_t inc;
369 };
370
371 struct pcg_state_setseq_32 {
372     uint32_t state;
373     uint32_t inc;
374 };
375
376 struct pcg_state_setseq_64 {
377     uint64_t state;
378     uint64_t inc;
379 };
380
381 #if PCG_HAS_128BIT_OPS
382 struct pcg_state_setseq_128 {
383     pcg128_t state;
384     pcg128_t inc;
385 };
386 #endif
387
388 /* Multi-step advance functions (jump-ahead, jump-back) */
389
390 extern uint8_t pcg_advance_lcg_8(uint8_t state, uint8_t delta, uint8_t cur_mult,
391                                  uint8_t cur_plus);
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);
398
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);
402 #endif
403
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.
407  */
408
409 inline void pcg_oneseq_8_step_r(struct pcg_state_8* rng)
410 {
411     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8
412                  + PCG_DEFAULT_INCREMENT_8;
413 }
414
415 inline void pcg_oneseq_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
416 {
417     rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
418                                    PCG_DEFAULT_INCREMENT_8);
419 }
420
421 inline void pcg_mcg_8_step_r(struct pcg_state_8* rng)
422 {
423     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8;
424 }
425
426 inline void pcg_mcg_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
427 {
428     rng->state
429         = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8, 0u);
430 }
431
432 inline void pcg_unique_8_step_r(struct pcg_state_8* rng)
433 {
434     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8
435                  + (uint8_t)(((intptr_t)rng) | 1u);
436 }
437
438 inline void pcg_unique_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
439 {
440     rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
441                                    (uint8_t)(((intptr_t)rng) | 1u));
442 }
443
444 inline void pcg_setseq_8_step_r(struct pcg_state_setseq_8* rng)
445 {
446     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8 + rng->inc;
447 }
448
449 inline void pcg_setseq_8_advance_r(struct pcg_state_setseq_8* rng,
450                                    uint8_t delta)
451 {
452     rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
453                                    rng->inc);
454 }
455
456 inline void pcg_oneseq_16_step_r(struct pcg_state_16* rng)
457 {
458     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16
459                  + PCG_DEFAULT_INCREMENT_16;
460 }
461
462 inline void pcg_oneseq_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
463 {
464     rng->state = pcg_advance_lcg_16(
465         rng->state, delta, PCG_DEFAULT_MULTIPLIER_16, PCG_DEFAULT_INCREMENT_16);
466 }
467
468 inline void pcg_mcg_16_step_r(struct pcg_state_16* rng)
469 {
470     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16;
471 }
472
473 inline void pcg_mcg_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
474 {
475     rng->state
476         = pcg_advance_lcg_16(rng->state, delta, PCG_DEFAULT_MULTIPLIER_16, 0u);
477 }
478
479 inline void pcg_unique_16_step_r(struct pcg_state_16* rng)
480 {
481     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16
482                  + (uint16_t)(((intptr_t)rng) | 1u);
483 }
484
485 inline void pcg_unique_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
486 {
487     rng->state
488         = pcg_advance_lcg_16(rng->state, delta, PCG_DEFAULT_MULTIPLIER_16,
489                              (uint16_t)(((intptr_t)rng) | 1u));
490 }
491
492 inline void pcg_setseq_16_step_r(struct pcg_state_setseq_16* rng)
493 {
494     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16 + rng->inc;
495 }
496
497 inline void pcg_setseq_16_advance_r(struct pcg_state_setseq_16* rng,
498                                     uint16_t delta)
499 {
500     rng->state = pcg_advance_lcg_16(rng->state, delta,
501                                     PCG_DEFAULT_MULTIPLIER_16, rng->inc);
502 }
503
504 inline void pcg_oneseq_32_step_r(struct pcg_state_32* rng)
505 {
506     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32
507                  + PCG_DEFAULT_INCREMENT_32;
508 }
509
510 inline void pcg_oneseq_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
511 {
512     rng->state = pcg_advance_lcg_32(
513         rng->state, delta, PCG_DEFAULT_MULTIPLIER_32, PCG_DEFAULT_INCREMENT_32);
514 }
515
516 inline void pcg_mcg_32_step_r(struct pcg_state_32* rng)
517 {
518     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32;
519 }
520
521 inline void pcg_mcg_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
522 {
523     rng->state
524         = pcg_advance_lcg_32(rng->state, delta, PCG_DEFAULT_MULTIPLIER_32, 0u);
525 }
526
527 inline void pcg_unique_32_step_r(struct pcg_state_32* rng)
528 {
529     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32
530                  + (uint32_t)(((intptr_t)rng) | 1u);
531 }
532
533 inline void pcg_unique_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
534 {
535     rng->state
536         = pcg_advance_lcg_32(rng->state, delta, PCG_DEFAULT_MULTIPLIER_32,
537                              (uint32_t)(((intptr_t)rng) | 1u));
538 }
539
540 inline void pcg_setseq_32_step_r(struct pcg_state_setseq_32* rng)
541 {
542     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32 + rng->inc;
543 }
544
545 inline void pcg_setseq_32_advance_r(struct pcg_state_setseq_32* rng,
546                                     uint32_t delta)
547 {
548     rng->state = pcg_advance_lcg_32(rng->state, delta,
549                                     PCG_DEFAULT_MULTIPLIER_32, rng->inc);
550 }
551
552 inline void pcg_oneseq_64_step_r(struct pcg_state_64* rng)
553 {
554     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64
555                  + PCG_DEFAULT_INCREMENT_64;
556 }
557
558 inline void pcg_oneseq_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
559 {
560     rng->state = pcg_advance_lcg_64(
561         rng->state, delta, PCG_DEFAULT_MULTIPLIER_64, PCG_DEFAULT_INCREMENT_64);
562 }
563
564 inline void pcg_mcg_64_step_r(struct pcg_state_64* rng)
565 {
566     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64;
567 }
568
569 inline void pcg_mcg_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
570 {
571     rng->state
572         = pcg_advance_lcg_64(rng->state, delta, PCG_DEFAULT_MULTIPLIER_64, 0u);
573 }
574
575 inline void pcg_unique_64_step_r(struct pcg_state_64* rng)
576 {
577     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64
578                  + (uint64_t)(((intptr_t)rng) | 1u);
579 }
580
581 inline void pcg_unique_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
582 {
583     rng->state
584         = pcg_advance_lcg_64(rng->state, delta, PCG_DEFAULT_MULTIPLIER_64,
585                              (uint64_t)(((intptr_t)rng) | 1u));
586 }
587
588 inline void pcg_setseq_64_step_r(struct pcg_state_setseq_64* rng)
589 {
590     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64 + rng->inc;
591 }
592
593 inline void pcg_setseq_64_advance_r(struct pcg_state_setseq_64* rng,
594                                     uint64_t delta)
595 {
596     rng->state = pcg_advance_lcg_64(rng->state, delta,
597                                     PCG_DEFAULT_MULTIPLIER_64, rng->inc);
598 }
599
600 #if PCG_HAS_128BIT_OPS
601 inline void pcg_oneseq_128_step_r(struct pcg_state_128* rng)
602 {
603     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128
604                  + PCG_DEFAULT_INCREMENT_128;
605 }
606 #endif
607
608 #if PCG_HAS_128BIT_OPS
609 inline void pcg_oneseq_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
610 {
611     rng->state
612         = pcg_advance_lcg_128(rng->state, delta, PCG_DEFAULT_MULTIPLIER_128,
613                               PCG_DEFAULT_INCREMENT_128);
614 }
615 #endif
616
617 #if PCG_HAS_128BIT_OPS
618 inline void pcg_mcg_128_step_r(struct pcg_state_128* rng)
619 {
620     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128;
621 }
622 #endif
623
624 #if PCG_HAS_128BIT_OPS
625 inline void pcg_mcg_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
626 {
627     rng->state = pcg_advance_lcg_128(rng->state, delta,
628                                      PCG_DEFAULT_MULTIPLIER_128, 0u);
629 }
630 #endif
631
632 #if PCG_HAS_128BIT_OPS
633 inline void pcg_unique_128_step_r(struct pcg_state_128* rng)
634 {
635     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128
636                  + (pcg128_t)(((intptr_t)rng) | 1u);
637 }
638 #endif
639
640 #if PCG_HAS_128BIT_OPS
641 inline void pcg_unique_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
642 {
643     rng->state
644         = pcg_advance_lcg_128(rng->state, delta, PCG_DEFAULT_MULTIPLIER_128,
645                               (pcg128_t)(((intptr_t)rng) | 1u));
646 }
647 #endif
648
649 #if PCG_HAS_128BIT_OPS
650 inline void pcg_setseq_128_step_r(struct pcg_state_setseq_128* rng)
651 {
652     rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128 + rng->inc;
653 }
654 #endif
655
656 #if PCG_HAS_128BIT_OPS
657 inline void pcg_setseq_128_advance_r(struct pcg_state_setseq_128* rng,
658                                      pcg128_t delta)
659 {
660     rng->state = pcg_advance_lcg_128(rng->state, delta,
661                                      PCG_DEFAULT_MULTIPLIER_128, rng->inc);
662 }
663 #endif
664
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
667  * these functions.
668  */
669
670 inline void pcg_oneseq_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
671 {
672     rng->state = 0U;
673     pcg_oneseq_8_step_r(rng);
674     rng->state += initstate;
675     pcg_oneseq_8_step_r(rng);
676 }
677
678 inline void pcg_mcg_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
679 {
680     rng->state = initstate | 1u;
681 }
682
683 inline void pcg_unique_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
684 {
685     rng->state = 0U;
686     pcg_unique_8_step_r(rng);
687     rng->state += initstate;
688     pcg_unique_8_step_r(rng);
689 }
690
691 inline void pcg_setseq_8_srandom_r(struct pcg_state_setseq_8* rng,
692                                    uint8_t initstate, uint8_t initseq)
693 {
694     rng->state = 0U;
695     rng->inc = (initseq << 1u) | 1u;
696     pcg_setseq_8_step_r(rng);
697     rng->state += initstate;
698     pcg_setseq_8_step_r(rng);
699 }
700
701 inline void pcg_oneseq_16_srandom_r(struct pcg_state_16* rng,
702                                     uint16_t initstate)
703 {
704     rng->state = 0U;
705     pcg_oneseq_16_step_r(rng);
706     rng->state += initstate;
707     pcg_oneseq_16_step_r(rng);
708 }
709
710 inline void pcg_mcg_16_srandom_r(struct pcg_state_16* rng, uint16_t initstate)
711 {
712     rng->state = initstate | 1u;
713 }
714
715 inline void pcg_unique_16_srandom_r(struct pcg_state_16* rng,
716                                     uint16_t initstate)
717 {
718     rng->state = 0U;
719     pcg_unique_16_step_r(rng);
720     rng->state += initstate;
721     pcg_unique_16_step_r(rng);
722 }
723
724 inline void pcg_setseq_16_srandom_r(struct pcg_state_setseq_16* rng,
725                                     uint16_t initstate, uint16_t initseq)
726 {
727     rng->state = 0U;
728     rng->inc = (initseq << 1u) | 1u;
729     pcg_setseq_16_step_r(rng);
730     rng->state += initstate;
731     pcg_setseq_16_step_r(rng);
732 }
733
734 inline void pcg_oneseq_32_srandom_r(struct pcg_state_32* rng,
735                                     uint32_t initstate)
736 {
737     rng->state = 0U;
738     pcg_oneseq_32_step_r(rng);
739     rng->state += initstate;
740     pcg_oneseq_32_step_r(rng);
741 }
742
743 inline void pcg_mcg_32_srandom_r(struct pcg_state_32* rng, uint32_t initstate)
744 {
745     rng->state = initstate | 1u;
746 }
747
748 inline void pcg_unique_32_srandom_r(struct pcg_state_32* rng,
749                                     uint32_t initstate)
750 {
751     rng->state = 0U;
752     pcg_unique_32_step_r(rng);
753     rng->state += initstate;
754     pcg_unique_32_step_r(rng);
755 }
756
757 inline void pcg_setseq_32_srandom_r(struct pcg_state_setseq_32* rng,
758                                     uint32_t initstate, uint32_t initseq)
759 {
760     rng->state = 0U;
761     rng->inc = (initseq << 1u) | 1u;
762     pcg_setseq_32_step_r(rng);
763     rng->state += initstate;
764     pcg_setseq_32_step_r(rng);
765 }
766
767 inline void pcg_oneseq_64_srandom_r(struct pcg_state_64* rng,
768                                     uint64_t initstate)
769 {
770     rng->state = 0U;
771     pcg_oneseq_64_step_r(rng);
772     rng->state += initstate;
773     pcg_oneseq_64_step_r(rng);
774 }
775
776 inline void pcg_mcg_64_srandom_r(struct pcg_state_64* rng, uint64_t initstate)
777 {
778     rng->state = initstate | 1u;
779 }
780
781 inline void pcg_unique_64_srandom_r(struct pcg_state_64* rng,
782                                     uint64_t initstate)
783 {
784     rng->state = 0U;
785     pcg_unique_64_step_r(rng);
786     rng->state += initstate;
787     pcg_unique_64_step_r(rng);
788 }
789
790 inline void pcg_setseq_64_srandom_r(struct pcg_state_setseq_64* rng,
791                                     uint64_t initstate, uint64_t initseq)
792 {
793     rng->state = 0U;
794     rng->inc = (initseq << 1u) | 1u;
795     pcg_setseq_64_step_r(rng);
796     rng->state += initstate;
797     pcg_setseq_64_step_r(rng);
798 }
799
800 #if PCG_HAS_128BIT_OPS
801 inline void pcg_oneseq_128_srandom_r(struct pcg_state_128* rng,
802                                      pcg128_t initstate)
803 {
804     rng->state = 0U;
805     pcg_oneseq_128_step_r(rng);
806     rng->state += initstate;
807     pcg_oneseq_128_step_r(rng);
808 }
809 #endif
810
811 #if PCG_HAS_128BIT_OPS
812 inline void pcg_mcg_128_srandom_r(struct pcg_state_128* rng, pcg128_t initstate)
813 {
814     rng->state = initstate | 1u;
815 }
816 #endif
817
818 #if PCG_HAS_128BIT_OPS
819 inline void pcg_unique_128_srandom_r(struct pcg_state_128* rng,
820                                      pcg128_t initstate)
821 {
822     rng->state = 0U;
823     pcg_unique_128_step_r(rng);
824     rng->state += initstate;
825     pcg_unique_128_step_r(rng);
826 }
827 #endif
828
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)
832 {
833     rng->state = 0U;
834     rng->inc = (initseq << 1u) | 1u;
835     pcg_setseq_128_step_r(rng);
836     rng->state += initstate;
837     pcg_setseq_128_step_r(rng);
838 }
839 #endif
840
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
844  * that provides
845  *
846  * Implementation notes for boundedrand_r:
847  *
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
852  *
853  *         uint32_t threshold = 0x100000000ull % bound;
854  *
855  *     but 64-bit div/mod is slower than 32-bit div/mod (especially on
856  *     32-bit platforms).  In essence, we do
857  *
858  *         uint32_t threshold = (0x100000000ull-bound) % bound;
859  *
860  *     because this version will calculate the same modulus, but the LHS
861  *     value is less than 2^32.
862  *
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.
868  */
869
870 /* Generation functions for XSH RS */
871
872 inline uint8_t pcg_oneseq_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
873 {
874     uint16_t oldstate = rng->state;
875     pcg_oneseq_16_step_r(rng);
876     return pcg_output_xsh_rs_16_8(oldstate);
877 }
878
879 inline uint8_t pcg_oneseq_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
880                                                     uint8_t bound)
881 {
882     uint8_t threshold = ((uint8_t)(-bound)) % bound;
883     for (;;) {
884         uint8_t r = pcg_oneseq_16_xsh_rs_8_random_r(rng);
885         if (r >= threshold)
886             return r % bound;
887     }
888 }
889
890 inline uint16_t pcg_oneseq_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
891 {
892     uint32_t oldstate = rng->state;
893     pcg_oneseq_32_step_r(rng);
894     return pcg_output_xsh_rs_32_16(oldstate);
895 }
896
897 inline uint16_t pcg_oneseq_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
898                                                       uint16_t bound)
899 {
900     uint16_t threshold = ((uint16_t)(-bound)) % bound;
901     for (;;) {
902         uint16_t r = pcg_oneseq_32_xsh_rs_16_random_r(rng);
903         if (r >= threshold)
904             return r % bound;
905     }
906 }
907
908 inline uint32_t pcg_oneseq_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
909 {
910     uint64_t oldstate = rng->state;
911     pcg_oneseq_64_step_r(rng);
912     return pcg_output_xsh_rs_64_32(oldstate);
913 }
914
915 inline uint32_t pcg_oneseq_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
916                                                       uint32_t bound)
917 {
918     uint32_t threshold = -bound % bound;
919     for (;;) {
920         uint32_t r = pcg_oneseq_64_xsh_rs_32_random_r(rng);
921         if (r >= threshold)
922             return r % bound;
923     }
924 }
925
926 #if PCG_HAS_128BIT_OPS
927 inline uint64_t pcg_oneseq_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
928 {
929     pcg_oneseq_128_step_r(rng);
930     return pcg_output_xsh_rs_128_64(rng->state);
931 }
932 #endif
933
934 #if PCG_HAS_128BIT_OPS
935 inline uint64_t
936 pcg_oneseq_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
937                                        uint64_t bound)
938 {
939     uint64_t threshold = -bound % bound;
940     for (;;) {
941         uint64_t r = pcg_oneseq_128_xsh_rs_64_random_r(rng);
942         if (r >= threshold)
943             return r % bound;
944     }
945 }
946 #endif
947
948 inline uint8_t pcg_unique_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
949 {
950     uint16_t oldstate = rng->state;
951     pcg_unique_16_step_r(rng);
952     return pcg_output_xsh_rs_16_8(oldstate);
953 }
954
955 inline uint8_t pcg_unique_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
956                                                     uint8_t bound)
957 {
958     uint8_t threshold = ((uint8_t)(-bound)) % bound;
959     for (;;) {
960         uint8_t r = pcg_unique_16_xsh_rs_8_random_r(rng);
961         if (r >= threshold)
962             return r % bound;
963     }
964 }
965
966 inline uint16_t pcg_unique_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
967 {
968     uint32_t oldstate = rng->state;
969     pcg_unique_32_step_r(rng);
970     return pcg_output_xsh_rs_32_16(oldstate);
971 }
972
973 inline uint16_t pcg_unique_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
974                                                       uint16_t bound)
975 {
976     uint16_t threshold = ((uint16_t)(-bound)) % bound;
977     for (;;) {
978         uint16_t r = pcg_unique_32_xsh_rs_16_random_r(rng);
979         if (r >= threshold)
980             return r % bound;
981     }
982 }
983
984 inline uint32_t pcg_unique_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
985 {
986     uint64_t oldstate = rng->state;
987     pcg_unique_64_step_r(rng);
988     return pcg_output_xsh_rs_64_32(oldstate);
989 }
990
991 inline uint32_t pcg_unique_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
992                                                       uint32_t bound)
993 {
994     uint32_t threshold = -bound % bound;
995     for (;;) {
996         uint32_t r = pcg_unique_64_xsh_rs_32_random_r(rng);
997         if (r >= threshold)
998             return r % bound;
999     }
1000 }
1001
1002 #if PCG_HAS_128BIT_OPS
1003 inline uint64_t pcg_unique_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
1004 {
1005     pcg_unique_128_step_r(rng);
1006     return pcg_output_xsh_rs_128_64(rng->state);
1007 }
1008 #endif
1009
1010 #if PCG_HAS_128BIT_OPS
1011 inline uint64_t
1012 pcg_unique_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
1013                                        uint64_t bound)
1014 {
1015     uint64_t threshold = -bound % bound;
1016     for (;;) {
1017         uint64_t r = pcg_unique_128_xsh_rs_64_random_r(rng);
1018         if (r >= threshold)
1019             return r % bound;
1020     }
1021 }
1022 #endif
1023
1024 inline uint8_t pcg_setseq_16_xsh_rs_8_random_r(struct pcg_state_setseq_16* rng)
1025 {
1026     uint16_t oldstate = rng->state;
1027     pcg_setseq_16_step_r(rng);
1028     return pcg_output_xsh_rs_16_8(oldstate);
1029 }
1030
1031 inline uint8_t
1032 pcg_setseq_16_xsh_rs_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1033                                      uint8_t bound)
1034 {
1035     uint8_t threshold = ((uint8_t)(-bound)) % bound;
1036     for (;;) {
1037         uint8_t r = pcg_setseq_16_xsh_rs_8_random_r(rng);
1038         if (r >= threshold)
1039             return r % bound;
1040     }
1041 }
1042
1043 inline uint16_t
1044 pcg_setseq_32_xsh_rs_16_random_r(struct pcg_state_setseq_32* rng)
1045 {
1046     uint32_t oldstate = rng->state;
1047     pcg_setseq_32_step_r(rng);
1048     return pcg_output_xsh_rs_32_16(oldstate);
1049 }
1050
1051 inline uint16_t
1052 pcg_setseq_32_xsh_rs_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1053                                       uint16_t bound)
1054 {
1055     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1056     for (;;) {
1057         uint16_t r = pcg_setseq_32_xsh_rs_16_random_r(rng);
1058         if (r >= threshold)
1059             return r % bound;
1060     }
1061 }
1062
1063 inline uint32_t
1064 pcg_setseq_64_xsh_rs_32_random_r(struct pcg_state_setseq_64* rng)
1065 {
1066     uint64_t oldstate = rng->state;
1067     pcg_setseq_64_step_r(rng);
1068     return pcg_output_xsh_rs_64_32(oldstate);
1069 }
1070
1071 inline uint32_t
1072 pcg_setseq_64_xsh_rs_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1073                                       uint32_t bound)
1074 {
1075     uint32_t threshold = -bound % bound;
1076     for (;;) {
1077         uint32_t r = pcg_setseq_64_xsh_rs_32_random_r(rng);
1078         if (r >= threshold)
1079             return r % bound;
1080     }
1081 }
1082
1083 #if PCG_HAS_128BIT_OPS
1084 inline uint64_t
1085 pcg_setseq_128_xsh_rs_64_random_r(struct pcg_state_setseq_128* rng)
1086 {
1087     pcg_setseq_128_step_r(rng);
1088     return pcg_output_xsh_rs_128_64(rng->state);
1089 }
1090 #endif
1091
1092 #if PCG_HAS_128BIT_OPS
1093 inline uint64_t
1094 pcg_setseq_128_xsh_rs_64_boundedrand_r(struct pcg_state_setseq_128* rng,
1095                                        uint64_t bound)
1096 {
1097     uint64_t threshold = -bound % bound;
1098     for (;;) {
1099         uint64_t r = pcg_setseq_128_xsh_rs_64_random_r(rng);
1100         if (r >= threshold)
1101             return r % bound;
1102     }
1103 }
1104 #endif
1105
1106 inline uint8_t pcg_mcg_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
1107 {
1108     uint16_t oldstate = rng->state;
1109     pcg_mcg_16_step_r(rng);
1110     return pcg_output_xsh_rs_16_8(oldstate);
1111 }
1112
1113 inline uint8_t pcg_mcg_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
1114                                                  uint8_t bound)
1115 {
1116     uint8_t threshold = ((uint8_t)(-bound)) % bound;
1117     for (;;) {
1118         uint8_t r = pcg_mcg_16_xsh_rs_8_random_r(rng);
1119         if (r >= threshold)
1120             return r % bound;
1121     }
1122 }
1123
1124 inline uint16_t pcg_mcg_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
1125 {
1126     uint32_t oldstate = rng->state;
1127     pcg_mcg_32_step_r(rng);
1128     return pcg_output_xsh_rs_32_16(oldstate);
1129 }
1130
1131 inline uint16_t pcg_mcg_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
1132                                                    uint16_t bound)
1133 {
1134     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1135     for (;;) {
1136         uint16_t r = pcg_mcg_32_xsh_rs_16_random_r(rng);
1137         if (r >= threshold)
1138             return r % bound;
1139     }
1140 }
1141
1142 inline uint32_t pcg_mcg_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
1143 {
1144     uint64_t oldstate = rng->state;
1145     pcg_mcg_64_step_r(rng);
1146     return pcg_output_xsh_rs_64_32(oldstate);
1147 }
1148
1149 inline uint32_t pcg_mcg_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
1150                                                    uint32_t bound)
1151 {
1152     uint32_t threshold = -bound % bound;
1153     for (;;) {
1154         uint32_t r = pcg_mcg_64_xsh_rs_32_random_r(rng);
1155         if (r >= threshold)
1156             return r % bound;
1157     }
1158 }
1159
1160 #if PCG_HAS_128BIT_OPS
1161 inline uint64_t pcg_mcg_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
1162 {
1163     pcg_mcg_128_step_r(rng);
1164     return pcg_output_xsh_rs_128_64(rng->state);
1165 }
1166 #endif
1167
1168 #if PCG_HAS_128BIT_OPS
1169 inline uint64_t pcg_mcg_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
1170                                                     uint64_t bound)
1171 {
1172     uint64_t threshold = -bound % bound;
1173     for (;;) {
1174         uint64_t r = pcg_mcg_128_xsh_rs_64_random_r(rng);
1175         if (r >= threshold)
1176             return r % bound;
1177     }
1178 }
1179 #endif
1180
1181 /* Generation functions for XSH RR */
1182
1183 inline uint8_t pcg_oneseq_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1184 {
1185     uint16_t oldstate = rng->state;
1186     pcg_oneseq_16_step_r(rng);
1187     return pcg_output_xsh_rr_16_8(oldstate);
1188 }
1189
1190 inline uint8_t pcg_oneseq_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1191                                                     uint8_t bound)
1192 {
1193     uint8_t threshold = ((uint8_t)(-bound)) % bound;
1194     for (;;) {
1195         uint8_t r = pcg_oneseq_16_xsh_rr_8_random_r(rng);
1196         if (r >= threshold)
1197             return r % bound;
1198     }
1199 }
1200
1201 inline uint16_t pcg_oneseq_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1202 {
1203     uint32_t oldstate = rng->state;
1204     pcg_oneseq_32_step_r(rng);
1205     return pcg_output_xsh_rr_32_16(oldstate);
1206 }
1207
1208 inline uint16_t pcg_oneseq_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1209                                                       uint16_t bound)
1210 {
1211     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1212     for (;;) {
1213         uint16_t r = pcg_oneseq_32_xsh_rr_16_random_r(rng);
1214         if (r >= threshold)
1215             return r % bound;
1216     }
1217 }
1218
1219 inline uint32_t pcg_oneseq_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1220 {
1221     uint64_t oldstate = rng->state;
1222     pcg_oneseq_64_step_r(rng);
1223     return pcg_output_xsh_rr_64_32(oldstate);
1224 }
1225
1226 inline uint32_t pcg_oneseq_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1227                                                       uint32_t bound)
1228 {
1229     uint32_t threshold = -bound % bound;
1230     for (;;) {
1231         uint32_t r = pcg_oneseq_64_xsh_rr_32_random_r(rng);
1232         if (r >= threshold)
1233             return r % bound;
1234     }
1235 }
1236
1237 #if PCG_HAS_128BIT_OPS
1238 inline uint64_t pcg_oneseq_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1239 {
1240     pcg_oneseq_128_step_r(rng);
1241     return pcg_output_xsh_rr_128_64(rng->state);
1242 }
1243 #endif
1244
1245 #if PCG_HAS_128BIT_OPS
1246 inline uint64_t
1247 pcg_oneseq_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1248                                        uint64_t bound)
1249 {
1250     uint64_t threshold = -bound % bound;
1251     for (;;) {
1252         uint64_t r = pcg_oneseq_128_xsh_rr_64_random_r(rng);
1253         if (r >= threshold)
1254             return r % bound;
1255     }
1256 }
1257 #endif
1258
1259 inline uint8_t pcg_unique_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1260 {
1261     uint16_t oldstate = rng->state;
1262     pcg_unique_16_step_r(rng);
1263     return pcg_output_xsh_rr_16_8(oldstate);
1264 }
1265
1266 inline uint8_t pcg_unique_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1267                                                     uint8_t bound)
1268 {
1269     uint8_t threshold = ((uint8_t)(-bound)) % bound;
1270     for (;;) {
1271         uint8_t r = pcg_unique_16_xsh_rr_8_random_r(rng);
1272         if (r >= threshold)
1273             return r % bound;
1274     }
1275 }
1276
1277 inline uint16_t pcg_unique_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1278 {
1279     uint32_t oldstate = rng->state;
1280     pcg_unique_32_step_r(rng);
1281     return pcg_output_xsh_rr_32_16(oldstate);
1282 }
1283
1284 inline uint16_t pcg_unique_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1285                                                       uint16_t bound)
1286 {
1287     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1288     for (;;) {
1289         uint16_t r = pcg_unique_32_xsh_rr_16_random_r(rng);
1290         if (r >= threshold)
1291             return r % bound;
1292     }
1293 }
1294
1295 inline uint32_t pcg_unique_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1296 {
1297     uint64_t oldstate = rng->state;
1298     pcg_unique_64_step_r(rng);
1299     return pcg_output_xsh_rr_64_32(oldstate);
1300 }
1301
1302 inline uint32_t pcg_unique_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1303                                                       uint32_t bound)
1304 {
1305     uint32_t threshold = -bound % bound;
1306     for (;;) {
1307         uint32_t r = pcg_unique_64_xsh_rr_32_random_r(rng);
1308         if (r >= threshold)
1309             return r % bound;
1310     }
1311 }
1312
1313 #if PCG_HAS_128BIT_OPS
1314 inline uint64_t pcg_unique_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1315 {
1316     pcg_unique_128_step_r(rng);
1317     return pcg_output_xsh_rr_128_64(rng->state);
1318 }
1319 #endif
1320
1321 #if PCG_HAS_128BIT_OPS
1322 inline uint64_t
1323 pcg_unique_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1324                                        uint64_t bound)
1325 {
1326     uint64_t threshold = -bound % bound;
1327     for (;;) {
1328         uint64_t r = pcg_unique_128_xsh_rr_64_random_r(rng);
1329         if (r >= threshold)
1330             return r % bound;
1331     }
1332 }
1333 #endif
1334
1335 inline uint8_t pcg_setseq_16_xsh_rr_8_random_r(struct pcg_state_setseq_16* rng)
1336 {
1337     uint16_t oldstate = rng->state;
1338     pcg_setseq_16_step_r(rng);
1339     return pcg_output_xsh_rr_16_8(oldstate);
1340 }
1341
1342 inline uint8_t
1343 pcg_setseq_16_xsh_rr_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1344                                      uint8_t bound)
1345 {
1346     uint8_t threshold = ((uint8_t)(-bound)) % bound;
1347     for (;;) {
1348         uint8_t r = pcg_setseq_16_xsh_rr_8_random_r(rng);
1349         if (r >= threshold)
1350             return r % bound;
1351     }
1352 }
1353
1354 inline uint16_t
1355 pcg_setseq_32_xsh_rr_16_random_r(struct pcg_state_setseq_32* rng)
1356 {
1357     uint32_t oldstate = rng->state;
1358     pcg_setseq_32_step_r(rng);
1359     return pcg_output_xsh_rr_32_16(oldstate);
1360 }
1361
1362 inline uint16_t
1363 pcg_setseq_32_xsh_rr_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1364                                       uint16_t bound)
1365 {
1366     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1367     for (;;) {
1368         uint16_t r = pcg_setseq_32_xsh_rr_16_random_r(rng);
1369         if (r >= threshold)
1370             return r % bound;
1371     }
1372 }
1373
1374 inline uint32_t
1375 pcg_setseq_64_xsh_rr_32_random_r(struct pcg_state_setseq_64* rng)
1376 {
1377     uint64_t oldstate = rng->state;
1378     pcg_setseq_64_step_r(rng);
1379     return pcg_output_xsh_rr_64_32(oldstate);
1380 }
1381
1382 inline uint32_t
1383 pcg_setseq_64_xsh_rr_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1384                                       uint32_t bound)
1385 {
1386     uint32_t threshold = -bound % bound;
1387     for (;;) {
1388         uint32_t r = pcg_setseq_64_xsh_rr_32_random_r(rng);
1389         if (r >= threshold)
1390             return r % bound;
1391     }
1392 }
1393
1394 #if PCG_HAS_128BIT_OPS
1395 inline uint64_t
1396 pcg_setseq_128_xsh_rr_64_random_r(struct pcg_state_setseq_128* rng)
1397 {
1398     pcg_setseq_128_step_r(rng);
1399     return pcg_output_xsh_rr_128_64(rng->state);
1400 }
1401 #endif
1402
1403 #if PCG_HAS_128BIT_OPS
1404 inline uint64_t
1405 pcg_setseq_128_xsh_rr_64_boundedrand_r(struct pcg_state_setseq_128* rng,
1406                                        uint64_t bound)
1407 {
1408     uint64_t threshold = -bound % bound;
1409     for (;;) {
1410         uint64_t r = pcg_setseq_128_xsh_rr_64_random_r(rng);
1411         if (r >= threshold)
1412             return r % bound;
1413     }
1414 }
1415 #endif
1416
1417 inline uint8_t pcg_mcg_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1418 {
1419     uint16_t oldstate = rng->state;
1420     pcg_mcg_16_step_r(rng);
1421     return pcg_output_xsh_rr_16_8(oldstate);
1422 }
1423
1424 inline uint8_t pcg_mcg_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1425                                                  uint8_t bound)
1426 {
1427     uint8_t threshold = ((uint8_t)(-bound)) % bound;
1428     for (;;) {
1429         uint8_t r = pcg_mcg_16_xsh_rr_8_random_r(rng);
1430         if (r >= threshold)
1431             return r % bound;
1432     }
1433 }
1434
1435 inline uint16_t pcg_mcg_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1436 {
1437     uint32_t oldstate = rng->state;
1438     pcg_mcg_32_step_r(rng);
1439     return pcg_output_xsh_rr_32_16(oldstate);
1440 }
1441
1442 inline uint16_t pcg_mcg_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1443                                                    uint16_t bound)
1444 {
1445     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1446     for (;;) {
1447         uint16_t r = pcg_mcg_32_xsh_rr_16_random_r(rng);
1448         if (r >= threshold)
1449             return r % bound;
1450     }
1451 }
1452
1453 inline uint32_t pcg_mcg_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1454 {
1455     uint64_t oldstate = rng->state;
1456     pcg_mcg_64_step_r(rng);
1457     return pcg_output_xsh_rr_64_32(oldstate);
1458 }
1459
1460 inline uint32_t pcg_mcg_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1461                                                    uint32_t bound)
1462 {
1463     uint32_t threshold = -bound % bound;
1464     for (;;) {
1465         uint32_t r = pcg_mcg_64_xsh_rr_32_random_r(rng);
1466         if (r >= threshold)
1467             return r % bound;
1468     }
1469 }
1470
1471 #if PCG_HAS_128BIT_OPS
1472 inline uint64_t pcg_mcg_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1473 {
1474     pcg_mcg_128_step_r(rng);
1475     return pcg_output_xsh_rr_128_64(rng->state);
1476 }
1477 #endif
1478
1479 #if PCG_HAS_128BIT_OPS
1480 inline uint64_t pcg_mcg_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1481                                                     uint64_t bound)
1482 {
1483     uint64_t threshold = -bound % bound;
1484     for (;;) {
1485         uint64_t r = pcg_mcg_128_xsh_rr_64_random_r(rng);
1486         if (r >= threshold)
1487             return r % bound;
1488     }
1489 }
1490 #endif
1491
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)
1494  */
1495
1496 inline uint8_t pcg_oneseq_8_rxs_m_xs_8_random_r(struct pcg_state_8* rng)
1497 {
1498     uint8_t oldstate = rng->state;
1499     pcg_oneseq_8_step_r(rng);
1500     return pcg_output_rxs_m_xs_8_8(oldstate);
1501 }
1502
1503 inline uint8_t pcg_oneseq_8_rxs_m_xs_8_boundedrand_r(struct pcg_state_8* rng,
1504                                                      uint8_t bound)
1505 {
1506     uint8_t threshold = ((uint8_t)(-bound)) % bound;
1507     for (;;) {
1508         uint8_t r = pcg_oneseq_8_rxs_m_xs_8_random_r(rng);
1509         if (r >= threshold)
1510             return r % bound;
1511     }
1512 }
1513
1514 inline uint16_t pcg_oneseq_16_rxs_m_xs_16_random_r(struct pcg_state_16* rng)
1515 {
1516     uint16_t oldstate = rng->state;
1517     pcg_oneseq_16_step_r(rng);
1518     return pcg_output_rxs_m_xs_16_16(oldstate);
1519 }
1520
1521 inline uint16_t
1522 pcg_oneseq_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_16* rng,
1523                                         uint16_t bound)
1524 {
1525     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1526     for (;;) {
1527         uint16_t r = pcg_oneseq_16_rxs_m_xs_16_random_r(rng);
1528         if (r >= threshold)
1529             return r % bound;
1530     }
1531 }
1532
1533 inline uint32_t pcg_oneseq_32_rxs_m_xs_32_random_r(struct pcg_state_32* rng)
1534 {
1535     uint32_t oldstate = rng->state;
1536     pcg_oneseq_32_step_r(rng);
1537     return pcg_output_rxs_m_xs_32_32(oldstate);
1538 }
1539
1540 inline uint32_t
1541 pcg_oneseq_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_32* rng,
1542                                         uint32_t bound)
1543 {
1544     uint32_t threshold = -bound % bound;
1545     for (;;) {
1546         uint32_t r = pcg_oneseq_32_rxs_m_xs_32_random_r(rng);
1547         if (r >= threshold)
1548             return r % bound;
1549     }
1550 }
1551
1552 inline uint64_t pcg_oneseq_64_rxs_m_xs_64_random_r(struct pcg_state_64* rng)
1553 {
1554     uint64_t oldstate = rng->state;
1555     pcg_oneseq_64_step_r(rng);
1556     return pcg_output_rxs_m_xs_64_64(oldstate);
1557 }
1558
1559 inline uint64_t
1560 pcg_oneseq_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_64* rng,
1561                                         uint64_t bound)
1562 {
1563     uint64_t threshold = -bound % bound;
1564     for (;;) {
1565         uint64_t r = pcg_oneseq_64_rxs_m_xs_64_random_r(rng);
1566         if (r >= threshold)
1567             return r % bound;
1568     }
1569 }
1570
1571 #if PCG_HAS_128BIT_OPS
1572 inline pcg128_t pcg_oneseq_128_rxs_m_xs_128_random_r(struct pcg_state_128* rng)
1573 {
1574     pcg_oneseq_128_step_r(rng);
1575     return pcg_output_rxs_m_xs_128_128(rng->state);
1576 }
1577 #endif
1578
1579 #if PCG_HAS_128BIT_OPS
1580 inline pcg128_t
1581 pcg_oneseq_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_128* rng,
1582                                           pcg128_t bound)
1583 {
1584     pcg128_t threshold = -bound % bound;
1585     for (;;) {
1586         pcg128_t r = pcg_oneseq_128_rxs_m_xs_128_random_r(rng);
1587         if (r >= threshold)
1588             return r % bound;
1589     }
1590 }
1591 #endif
1592
1593 inline uint16_t pcg_unique_16_rxs_m_xs_16_random_r(struct pcg_state_16* rng)
1594 {
1595     uint16_t oldstate = rng->state;
1596     pcg_unique_16_step_r(rng);
1597     return pcg_output_rxs_m_xs_16_16(oldstate);
1598 }
1599
1600 inline uint16_t
1601 pcg_unique_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_16* rng,
1602                                         uint16_t bound)
1603 {
1604     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1605     for (;;) {
1606         uint16_t r = pcg_unique_16_rxs_m_xs_16_random_r(rng);
1607         if (r >= threshold)
1608             return r % bound;
1609     }
1610 }
1611
1612 inline uint32_t pcg_unique_32_rxs_m_xs_32_random_r(struct pcg_state_32* rng)
1613 {
1614     uint32_t oldstate = rng->state;
1615     pcg_unique_32_step_r(rng);
1616     return pcg_output_rxs_m_xs_32_32(oldstate);
1617 }
1618
1619 inline uint32_t
1620 pcg_unique_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_32* rng,
1621                                         uint32_t bound)
1622 {
1623     uint32_t threshold = -bound % bound;
1624     for (;;) {
1625         uint32_t r = pcg_unique_32_rxs_m_xs_32_random_r(rng);
1626         if (r >= threshold)
1627             return r % bound;
1628     }
1629 }
1630
1631 inline uint64_t pcg_unique_64_rxs_m_xs_64_random_r(struct pcg_state_64* rng)
1632 {
1633     uint64_t oldstate = rng->state;
1634     pcg_unique_64_step_r(rng);
1635     return pcg_output_rxs_m_xs_64_64(oldstate);
1636 }
1637
1638 inline uint64_t
1639 pcg_unique_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_64* rng,
1640                                         uint64_t bound)
1641 {
1642     uint64_t threshold = -bound % bound;
1643     for (;;) {
1644         uint64_t r = pcg_unique_64_rxs_m_xs_64_random_r(rng);
1645         if (r >= threshold)
1646             return r % bound;
1647     }
1648 }
1649
1650 #if PCG_HAS_128BIT_OPS
1651 inline pcg128_t pcg_unique_128_rxs_m_xs_128_random_r(struct pcg_state_128* rng)
1652 {
1653     pcg_unique_128_step_r(rng);
1654     return pcg_output_rxs_m_xs_128_128(rng->state);
1655 }
1656 #endif
1657
1658 #if PCG_HAS_128BIT_OPS
1659 inline pcg128_t
1660 pcg_unique_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_128* rng,
1661                                           pcg128_t bound)
1662 {
1663     pcg128_t threshold = -bound % bound;
1664     for (;;) {
1665         pcg128_t r = pcg_unique_128_rxs_m_xs_128_random_r(rng);
1666         if (r >= threshold)
1667             return r % bound;
1668     }
1669 }
1670 #endif
1671
1672 inline uint8_t pcg_setseq_8_rxs_m_xs_8_random_r(struct pcg_state_setseq_8* rng)
1673 {
1674     uint8_t oldstate = rng->state;
1675     pcg_setseq_8_step_r(rng);
1676     return pcg_output_rxs_m_xs_8_8(oldstate);
1677 }
1678
1679 inline uint8_t
1680 pcg_setseq_8_rxs_m_xs_8_boundedrand_r(struct pcg_state_setseq_8* rng,
1681                                       uint8_t bound)
1682 {
1683     uint8_t threshold = ((uint8_t)(-bound)) % bound;
1684     for (;;) {
1685         uint8_t r = pcg_setseq_8_rxs_m_xs_8_random_r(rng);
1686         if (r >= threshold)
1687             return r % bound;
1688     }
1689 }
1690
1691 inline uint16_t
1692 pcg_setseq_16_rxs_m_xs_16_random_r(struct pcg_state_setseq_16* rng)
1693 {
1694     uint16_t oldstate = rng->state;
1695     pcg_setseq_16_step_r(rng);
1696     return pcg_output_rxs_m_xs_16_16(oldstate);
1697 }
1698
1699 inline uint16_t
1700 pcg_setseq_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_setseq_16* rng,
1701                                         uint16_t bound)
1702 {
1703     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1704     for (;;) {
1705         uint16_t r = pcg_setseq_16_rxs_m_xs_16_random_r(rng);
1706         if (r >= threshold)
1707             return r % bound;
1708     }
1709 }
1710
1711 inline uint32_t
1712 pcg_setseq_32_rxs_m_xs_32_random_r(struct pcg_state_setseq_32* rng)
1713 {
1714     uint32_t oldstate = rng->state;
1715     pcg_setseq_32_step_r(rng);
1716     return pcg_output_rxs_m_xs_32_32(oldstate);
1717 }
1718
1719 inline uint32_t
1720 pcg_setseq_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_setseq_32* rng,
1721                                         uint32_t bound)
1722 {
1723     uint32_t threshold = -bound % bound;
1724     for (;;) {
1725         uint32_t r = pcg_setseq_32_rxs_m_xs_32_random_r(rng);
1726         if (r >= threshold)
1727             return r % bound;
1728     }
1729 }
1730
1731 inline uint64_t
1732 pcg_setseq_64_rxs_m_xs_64_random_r(struct pcg_state_setseq_64* rng)
1733 {
1734     uint64_t oldstate = rng->state;
1735     pcg_setseq_64_step_r(rng);
1736     return pcg_output_rxs_m_xs_64_64(oldstate);
1737 }
1738
1739 inline uint64_t
1740 pcg_setseq_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_setseq_64* rng,
1741                                         uint64_t bound)
1742 {
1743     uint64_t threshold = -bound % bound;
1744     for (;;) {
1745         uint64_t r = pcg_setseq_64_rxs_m_xs_64_random_r(rng);
1746         if (r >= threshold)
1747             return r % bound;
1748     }
1749 }
1750
1751 #if PCG_HAS_128BIT_OPS
1752 inline pcg128_t
1753 pcg_setseq_128_rxs_m_xs_128_random_r(struct pcg_state_setseq_128* rng)
1754 {
1755     pcg_setseq_128_step_r(rng);
1756     return pcg_output_rxs_m_xs_128_128(rng->state);
1757 }
1758 #endif
1759
1760 #if PCG_HAS_128BIT_OPS
1761 inline pcg128_t
1762 pcg_setseq_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_setseq_128* rng,
1763                                           pcg128_t bound)
1764 {
1765     pcg128_t threshold = -bound % bound;
1766     for (;;) {
1767         pcg128_t r = pcg_setseq_128_rxs_m_xs_128_random_r(rng);
1768         if (r >= threshold)
1769             return r % bound;
1770     }
1771 }
1772 #endif
1773
1774 /* Generation functions for RXS M */
1775
1776 inline uint8_t pcg_oneseq_16_rxs_m_8_random_r(struct pcg_state_16* rng)
1777 {
1778     uint16_t oldstate = rng->state;
1779     pcg_oneseq_16_step_r(rng);
1780     return pcg_output_rxs_m_16_8(oldstate);
1781 }
1782
1783 inline uint8_t pcg_oneseq_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
1784                                                    uint8_t bound)
1785 {
1786     uint8_t threshold = ((uint8_t)(-bound)) % bound;
1787     for (;;) {
1788         uint8_t r = pcg_oneseq_16_rxs_m_8_random_r(rng);
1789         if (r >= threshold)
1790             return r % bound;
1791     }
1792 }
1793
1794 inline uint16_t pcg_oneseq_32_rxs_m_16_random_r(struct pcg_state_32* rng)
1795 {
1796     uint32_t oldstate = rng->state;
1797     pcg_oneseq_32_step_r(rng);
1798     return pcg_output_rxs_m_32_16(oldstate);
1799 }
1800
1801 inline uint16_t pcg_oneseq_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
1802                                                      uint16_t bound)
1803 {
1804     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1805     for (;;) {
1806         uint16_t r = pcg_oneseq_32_rxs_m_16_random_r(rng);
1807         if (r >= threshold)
1808             return r % bound;
1809     }
1810 }
1811
1812 inline uint32_t pcg_oneseq_64_rxs_m_32_random_r(struct pcg_state_64* rng)
1813 {
1814     uint64_t oldstate = rng->state;
1815     pcg_oneseq_64_step_r(rng);
1816     return pcg_output_rxs_m_64_32(oldstate);
1817 }
1818
1819 inline uint32_t pcg_oneseq_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
1820                                                      uint32_t bound)
1821 {
1822     uint32_t threshold = -bound % bound;
1823     for (;;) {
1824         uint32_t r = pcg_oneseq_64_rxs_m_32_random_r(rng);
1825         if (r >= threshold)
1826             return r % bound;
1827     }
1828 }
1829
1830 #if PCG_HAS_128BIT_OPS
1831 inline uint64_t pcg_oneseq_128_rxs_m_64_random_r(struct pcg_state_128* rng)
1832 {
1833     pcg_oneseq_128_step_r(rng);
1834     return pcg_output_rxs_m_128_64(rng->state);
1835 }
1836 #endif
1837
1838 #if PCG_HAS_128BIT_OPS
1839 inline uint64_t pcg_oneseq_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
1840                                                       uint64_t bound)
1841 {
1842     uint64_t threshold = -bound % bound;
1843     for (;;) {
1844         uint64_t r = pcg_oneseq_128_rxs_m_64_random_r(rng);
1845         if (r >= threshold)
1846             return r % bound;
1847     }
1848 }
1849 #endif
1850
1851 inline uint8_t pcg_unique_16_rxs_m_8_random_r(struct pcg_state_16* rng)
1852 {
1853     uint16_t oldstate = rng->state;
1854     pcg_unique_16_step_r(rng);
1855     return pcg_output_rxs_m_16_8(oldstate);
1856 }
1857
1858 inline uint8_t pcg_unique_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
1859                                                    uint8_t bound)
1860 {
1861     uint8_t threshold = ((uint8_t)(-bound)) % bound;
1862     for (;;) {
1863         uint8_t r = pcg_unique_16_rxs_m_8_random_r(rng);
1864         if (r >= threshold)
1865             return r % bound;
1866     }
1867 }
1868
1869 inline uint16_t pcg_unique_32_rxs_m_16_random_r(struct pcg_state_32* rng)
1870 {
1871     uint32_t oldstate = rng->state;
1872     pcg_unique_32_step_r(rng);
1873     return pcg_output_rxs_m_32_16(oldstate);
1874 }
1875
1876 inline uint16_t pcg_unique_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
1877                                                      uint16_t bound)
1878 {
1879     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1880     for (;;) {
1881         uint16_t r = pcg_unique_32_rxs_m_16_random_r(rng);
1882         if (r >= threshold)
1883             return r % bound;
1884     }
1885 }
1886
1887 inline uint32_t pcg_unique_64_rxs_m_32_random_r(struct pcg_state_64* rng)
1888 {
1889     uint64_t oldstate = rng->state;
1890     pcg_unique_64_step_r(rng);
1891     return pcg_output_rxs_m_64_32(oldstate);
1892 }
1893
1894 inline uint32_t pcg_unique_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
1895                                                      uint32_t bound)
1896 {
1897     uint32_t threshold = -bound % bound;
1898     for (;;) {
1899         uint32_t r = pcg_unique_64_rxs_m_32_random_r(rng);
1900         if (r >= threshold)
1901             return r % bound;
1902     }
1903 }
1904
1905 #if PCG_HAS_128BIT_OPS
1906 inline uint64_t pcg_unique_128_rxs_m_64_random_r(struct pcg_state_128* rng)
1907 {
1908     pcg_unique_128_step_r(rng);
1909     return pcg_output_rxs_m_128_64(rng->state);
1910 }
1911 #endif
1912
1913 #if PCG_HAS_128BIT_OPS
1914 inline uint64_t pcg_unique_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
1915                                                       uint64_t bound)
1916 {
1917     uint64_t threshold = -bound % bound;
1918     for (;;) {
1919         uint64_t r = pcg_unique_128_rxs_m_64_random_r(rng);
1920         if (r >= threshold)
1921             return r % bound;
1922     }
1923 }
1924 #endif
1925
1926 inline uint8_t pcg_setseq_16_rxs_m_8_random_r(struct pcg_state_setseq_16* rng)
1927 {
1928     uint16_t oldstate = rng->state;
1929     pcg_setseq_16_step_r(rng);
1930     return pcg_output_rxs_m_16_8(oldstate);
1931 }
1932
1933 inline uint8_t
1934 pcg_setseq_16_rxs_m_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1935                                     uint8_t bound)
1936 {
1937     uint8_t threshold = ((uint8_t)(-bound)) % bound;
1938     for (;;) {
1939         uint8_t r = pcg_setseq_16_rxs_m_8_random_r(rng);
1940         if (r >= threshold)
1941             return r % bound;
1942     }
1943 }
1944
1945 inline uint16_t pcg_setseq_32_rxs_m_16_random_r(struct pcg_state_setseq_32* rng)
1946 {
1947     uint32_t oldstate = rng->state;
1948     pcg_setseq_32_step_r(rng);
1949     return pcg_output_rxs_m_32_16(oldstate);
1950 }
1951
1952 inline uint16_t
1953 pcg_setseq_32_rxs_m_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1954                                      uint16_t bound)
1955 {
1956     uint16_t threshold = ((uint16_t)(-bound)) % bound;
1957     for (;;) {
1958         uint16_t r = pcg_setseq_32_rxs_m_16_random_r(rng);
1959         if (r >= threshold)
1960             return r % bound;
1961     }
1962 }
1963
1964 inline uint32_t pcg_setseq_64_rxs_m_32_random_r(struct pcg_state_setseq_64* rng)
1965 {
1966     uint64_t oldstate = rng->state;
1967     pcg_setseq_64_step_r(rng);
1968     return pcg_output_rxs_m_64_32(oldstate);
1969 }
1970
1971 inline uint32_t
1972 pcg_setseq_64_rxs_m_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1973                                      uint32_t bound)
1974 {
1975     uint32_t threshold = -bound % bound;
1976     for (;;) {
1977         uint32_t r = pcg_setseq_64_rxs_m_32_random_r(rng);
1978         if (r >= threshold)
1979             return r % bound;
1980     }
1981 }
1982
1983 #if PCG_HAS_128BIT_OPS
1984 inline uint64_t
1985 pcg_setseq_128_rxs_m_64_random_r(struct pcg_state_setseq_128* rng)
1986 {
1987     pcg_setseq_128_step_r(rng);
1988     return pcg_output_rxs_m_128_64(rng->state);
1989 }
1990 #endif
1991
1992 #if PCG_HAS_128BIT_OPS
1993 inline uint64_t
1994 pcg_setseq_128_rxs_m_64_boundedrand_r(struct pcg_state_setseq_128* rng,
1995                                       uint64_t bound)
1996 {
1997     uint64_t threshold = -bound % bound;
1998     for (;;) {
1999         uint64_t r = pcg_setseq_128_rxs_m_64_random_r(rng);
2000         if (r >= threshold)
2001             return r % bound;
2002     }
2003 }
2004 #endif
2005
2006 inline uint8_t pcg_mcg_16_rxs_m_8_random_r(struct pcg_state_16* rng)
2007 {
2008     uint16_t oldstate = rng->state;
2009     pcg_mcg_16_step_r(rng);
2010     return pcg_output_rxs_m_16_8(oldstate);
2011 }
2012
2013 inline uint8_t pcg_mcg_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
2014                                                 uint8_t bound)
2015 {
2016     uint8_t threshold = ((uint8_t)(-bound)) % bound;
2017     for (;;) {
2018         uint8_t r = pcg_mcg_16_rxs_m_8_random_r(rng);
2019         if (r >= threshold)
2020             return r % bound;
2021     }
2022 }
2023
2024 inline uint16_t pcg_mcg_32_rxs_m_16_random_r(struct pcg_state_32* rng)
2025 {
2026     uint32_t oldstate = rng->state;
2027     pcg_mcg_32_step_r(rng);
2028     return pcg_output_rxs_m_32_16(oldstate);
2029 }
2030
2031 inline uint16_t pcg_mcg_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
2032                                                   uint16_t bound)
2033 {
2034     uint16_t threshold = ((uint16_t)(-bound)) % bound;
2035     for (;;) {
2036         uint16_t r = pcg_mcg_32_rxs_m_16_random_r(rng);
2037         if (r >= threshold)
2038             return r % bound;
2039     }
2040 }
2041
2042 inline uint32_t pcg_mcg_64_rxs_m_32_random_r(struct pcg_state_64* rng)
2043 {
2044     uint64_t oldstate = rng->state;
2045     pcg_mcg_64_step_r(rng);
2046     return pcg_output_rxs_m_64_32(oldstate);
2047 }
2048
2049 inline uint32_t pcg_mcg_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
2050                                                   uint32_t bound)
2051 {
2052     uint32_t threshold = -bound % bound;
2053     for (;;) {
2054         uint32_t r = pcg_mcg_64_rxs_m_32_random_r(rng);
2055         if (r >= threshold)
2056             return r % bound;
2057     }
2058 }
2059
2060 #if PCG_HAS_128BIT_OPS
2061 inline uint64_t pcg_mcg_128_rxs_m_64_random_r(struct pcg_state_128* rng)
2062 {
2063     pcg_mcg_128_step_r(rng);
2064     return pcg_output_rxs_m_128_64(rng->state);
2065 }
2066 #endif
2067
2068 #if PCG_HAS_128BIT_OPS
2069 inline uint64_t pcg_mcg_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
2070                                                    uint64_t bound)
2071 {
2072     uint64_t threshold = -bound % bound;
2073     for (;;) {
2074         uint64_t r = pcg_mcg_128_rxs_m_64_random_r(rng);
2075         if (r >= threshold)
2076             return r % bound;
2077     }
2078 }
2079 #endif
2080
2081 /* Generation functions for XSL RR (only defined for "large" types) */
2082
2083 inline uint32_t pcg_oneseq_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2084 {
2085     uint64_t oldstate = rng->state;
2086     pcg_oneseq_64_step_r(rng);
2087     return pcg_output_xsl_rr_64_32(oldstate);
2088 }
2089
2090 inline uint32_t pcg_oneseq_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2091                                                       uint32_t bound)
2092 {
2093     uint32_t threshold = -bound % bound;
2094     for (;;) {
2095         uint32_t r = pcg_oneseq_64_xsl_rr_32_random_r(rng);
2096         if (r >= threshold)
2097             return r % bound;
2098     }
2099 }
2100
2101 #if PCG_HAS_128BIT_OPS
2102 inline uint64_t pcg_oneseq_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2103 {
2104     pcg_oneseq_128_step_r(rng);
2105     return pcg_output_xsl_rr_128_64(rng->state);
2106 }
2107 #endif
2108
2109 #if PCG_HAS_128BIT_OPS
2110 inline uint64_t
2111 pcg_oneseq_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2112                                        uint64_t bound)
2113 {
2114     uint64_t threshold = -bound % bound;
2115     for (;;) {
2116         uint64_t r = pcg_oneseq_128_xsl_rr_64_random_r(rng);
2117         if (r >= threshold)
2118             return r % bound;
2119     }
2120 }
2121 #endif
2122
2123 inline uint32_t pcg_unique_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2124 {
2125     uint64_t oldstate = rng->state;
2126     pcg_unique_64_step_r(rng);
2127     return pcg_output_xsl_rr_64_32(oldstate);
2128 }
2129
2130 inline uint32_t pcg_unique_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2131                                                       uint32_t bound)
2132 {
2133     uint32_t threshold = -bound % bound;
2134     for (;;) {
2135         uint32_t r = pcg_unique_64_xsl_rr_32_random_r(rng);
2136         if (r >= threshold)
2137             return r % bound;
2138     }
2139 }
2140
2141 #if PCG_HAS_128BIT_OPS
2142 inline uint64_t pcg_unique_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2143 {
2144     pcg_unique_128_step_r(rng);
2145     return pcg_output_xsl_rr_128_64(rng->state);
2146 }
2147 #endif
2148
2149 #if PCG_HAS_128BIT_OPS
2150 inline uint64_t
2151 pcg_unique_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2152                                        uint64_t bound)
2153 {
2154     uint64_t threshold = -bound % bound;
2155     for (;;) {
2156         uint64_t r = pcg_unique_128_xsl_rr_64_random_r(rng);
2157         if (r >= threshold)
2158             return r % bound;
2159     }
2160 }
2161 #endif
2162
2163 inline uint32_t
2164 pcg_setseq_64_xsl_rr_32_random_r(struct pcg_state_setseq_64* rng)
2165 {
2166     uint64_t oldstate = rng->state;
2167     pcg_setseq_64_step_r(rng);
2168     return pcg_output_xsl_rr_64_32(oldstate);
2169 }
2170
2171 inline uint32_t
2172 pcg_setseq_64_xsl_rr_32_boundedrand_r(struct pcg_state_setseq_64* rng,
2173                                       uint32_t bound)
2174 {
2175     uint32_t threshold = -bound % bound;
2176     for (;;) {
2177         uint32_t r = pcg_setseq_64_xsl_rr_32_random_r(rng);
2178         if (r >= threshold)
2179             return r % bound;
2180     }
2181 }
2182
2183 #if PCG_HAS_128BIT_OPS
2184 inline uint64_t
2185 pcg_setseq_128_xsl_rr_64_random_r(struct pcg_state_setseq_128* rng)
2186 {
2187     pcg_setseq_128_step_r(rng);
2188     return pcg_output_xsl_rr_128_64(rng->state);
2189 }
2190 #endif
2191
2192 #if PCG_HAS_128BIT_OPS
2193 inline uint64_t
2194 pcg_setseq_128_xsl_rr_64_boundedrand_r(struct pcg_state_setseq_128* rng,
2195                                        uint64_t bound)
2196 {
2197     uint64_t threshold = -bound % bound;
2198     for (;;) {
2199         uint64_t r = pcg_setseq_128_xsl_rr_64_random_r(rng);
2200         if (r >= threshold)
2201             return r % bound;
2202     }
2203 }
2204 #endif
2205
2206 inline uint32_t pcg_mcg_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2207 {
2208     uint64_t oldstate = rng->state;
2209     pcg_mcg_64_step_r(rng);
2210     return pcg_output_xsl_rr_64_32(oldstate);
2211 }
2212
2213 inline uint32_t pcg_mcg_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2214                                                    uint32_t bound)
2215 {
2216     uint32_t threshold = -bound % bound;
2217     for (;;) {
2218         uint32_t r = pcg_mcg_64_xsl_rr_32_random_r(rng);
2219         if (r >= threshold)
2220             return r % bound;
2221     }
2222 }
2223
2224 #if PCG_HAS_128BIT_OPS
2225 inline uint64_t pcg_mcg_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2226 {
2227     pcg_mcg_128_step_r(rng);
2228     return pcg_output_xsl_rr_128_64(rng->state);
2229 }
2230 #endif
2231
2232 #if PCG_HAS_128BIT_OPS
2233 inline uint64_t pcg_mcg_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2234                                                     uint64_t bound)
2235 {
2236     uint64_t threshold = -bound % bound;
2237     for (;;) {
2238         uint64_t r = pcg_mcg_128_xsl_rr_64_random_r(rng);
2239         if (r >= threshold)
2240             return r % bound;
2241     }
2242 }
2243 #endif
2244
2245 /* Generation functions for XSL RR RR (only defined for "large" types) */
2246
2247 inline uint64_t pcg_oneseq_64_xsl_rr_rr_64_random_r(struct pcg_state_64* rng)
2248 {
2249     uint64_t oldstate = rng->state;
2250     pcg_oneseq_64_step_r(rng);
2251     return pcg_output_xsl_rr_rr_64_64(oldstate);
2252 }
2253
2254 inline uint64_t
2255 pcg_oneseq_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_64* rng,
2256                                          uint64_t bound)
2257 {
2258     uint64_t threshold = -bound % bound;
2259     for (;;) {
2260         uint64_t r = pcg_oneseq_64_xsl_rr_rr_64_random_r(rng);
2261         if (r >= threshold)
2262             return r % bound;
2263     }
2264 }
2265
2266 #if PCG_HAS_128BIT_OPS
2267 inline pcg128_t pcg_oneseq_128_xsl_rr_rr_128_random_r(struct pcg_state_128* rng)
2268 {
2269     pcg_oneseq_128_step_r(rng);
2270     return pcg_output_xsl_rr_rr_128_128(rng->state);
2271 }
2272 #endif
2273
2274 #if PCG_HAS_128BIT_OPS
2275 inline pcg128_t
2276 pcg_oneseq_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_128* rng,
2277                                            pcg128_t bound)
2278 {
2279     pcg128_t threshold = -bound % bound;
2280     for (;;) {
2281         pcg128_t r = pcg_oneseq_128_xsl_rr_rr_128_random_r(rng);
2282         if (r >= threshold)
2283             return r % bound;
2284     }
2285 }
2286 #endif
2287
2288 inline uint64_t pcg_unique_64_xsl_rr_rr_64_random_r(struct pcg_state_64* rng)
2289 {
2290     uint64_t oldstate = rng->state;
2291     pcg_unique_64_step_r(rng);
2292     return pcg_output_xsl_rr_rr_64_64(oldstate);
2293 }
2294
2295 inline uint64_t
2296 pcg_unique_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_64* rng,
2297                                          uint64_t bound)
2298 {
2299     uint64_t threshold = -bound % bound;
2300     for (;;) {
2301         uint64_t r = pcg_unique_64_xsl_rr_rr_64_random_r(rng);
2302         if (r >= threshold)
2303             return r % bound;
2304     }
2305 }
2306
2307 #if PCG_HAS_128BIT_OPS
2308 inline pcg128_t pcg_unique_128_xsl_rr_rr_128_random_r(struct pcg_state_128* rng)
2309 {
2310     pcg_unique_128_step_r(rng);
2311     return pcg_output_xsl_rr_rr_128_128(rng->state);
2312 }
2313 #endif
2314
2315 #if PCG_HAS_128BIT_OPS
2316 inline pcg128_t
2317 pcg_unique_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_128* rng,
2318                                            pcg128_t bound)
2319 {
2320     pcg128_t threshold = -bound % bound;
2321     for (;;) {
2322         pcg128_t r = pcg_unique_128_xsl_rr_rr_128_random_r(rng);
2323         if (r >= threshold)
2324             return r % bound;
2325     }
2326 }
2327 #endif
2328
2329 inline uint64_t
2330 pcg_setseq_64_xsl_rr_rr_64_random_r(struct pcg_state_setseq_64* rng)
2331 {
2332     uint64_t oldstate = rng->state;
2333     pcg_setseq_64_step_r(rng);
2334     return pcg_output_xsl_rr_rr_64_64(oldstate);
2335 }
2336
2337 inline uint64_t
2338 pcg_setseq_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_setseq_64* rng,
2339                                          uint64_t bound)
2340 {
2341     uint64_t threshold = -bound % bound;
2342     for (;;) {
2343         uint64_t r = pcg_setseq_64_xsl_rr_rr_64_random_r(rng);
2344         if (r >= threshold)
2345             return r % bound;
2346     }
2347 }
2348
2349 #if PCG_HAS_128BIT_OPS
2350 inline pcg128_t
2351 pcg_setseq_128_xsl_rr_rr_128_random_r(struct pcg_state_setseq_128* rng)
2352 {
2353     pcg_setseq_128_step_r(rng);
2354     return pcg_output_xsl_rr_rr_128_128(rng->state);
2355 }
2356 #endif
2357
2358 #if PCG_HAS_128BIT_OPS
2359 inline pcg128_t
2360 pcg_setseq_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_setseq_128* rng,
2361                                            pcg128_t bound)
2362 {
2363     pcg128_t threshold = -bound % bound;
2364     for (;;) {
2365         pcg128_t r = pcg_setseq_128_xsl_rr_rr_128_random_r(rng);
2366         if (r >= threshold)
2367             return r % bound;
2368     }
2369 }
2370 #endif
2371
2372 /*** Typedefs */
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;
2377 /*** random_r */
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
2387 /*** srandom_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
2392 /*** advance_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
2397
2398 #if PCG_HAS_128BIT_OPS
2399 /*** Typedefs */
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;
2404 /*** random_r */
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
2414 /*** srandom_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
2419 /*** advance_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
2424 #endif
2425
2426 /*** Typedefs */
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;
2431 /*** random_r */
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
2441 /*** srandom_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
2446 /*** advance_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
2451
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
2458 #endif
2459
2460 /*** Typedefs */
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;
2465 /*** random_r */
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
2475 /*** srandom_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
2480 /*** advance_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
2485
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
2492 #endif
2493
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);
2498
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);
2504 #endif
2505
2506 /*
2507  * Static initialization constants (if you can't call srandom for some
2508  * bizarre reason).
2509  */
2510
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
2515
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
2521 #endif
2522
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
2529 #endif
2530
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
2537 #endif
2538
2539 #if __cplusplus
2540 }
2541 #endif
2542
2543 #endif /* PCG_VARIANTS_H_INCLUDED */
2544