Coverage Report

Created: 2023-09-25 06:05

/src/igraph/vendor/pcg/pcg_variants.h
Line
Count
Source (jump to first uncovered line)
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
#ifdef _MSC_VER
42
#pragma warning(push)
43
#pragma warning(disable:4146) /* "unary minus operator applied to unsigned type, result still unsigned" */
44
#endif
45
46
#if __SIZEOF_INT128__
47
    typedef __uint128_t pcg128_t;
48
    #define PCG_128BIT_CONSTANT(high,low) \
49
            ((((pcg128_t)high) << 64) + low)
50
    #define PCG_HAS_128BIT_OPS 1
51
#endif
52
53
/* Checking for !__GNUC_STDC_INLINE__ is a hack to work around a bug in the
54
 * Intel compiler where it defined both __GNUC_GNU_INLINE__ and __GNUC_STDC_INLINE__
55
 * to 1 when using -std=gnu99. igraph is always compiled with -std=gnu99.
56
 *
57
 * Tested with icc (ICC) 2021.3.0 20210609 on Linux */
58
#if __GNUC_GNU_INLINE__  &&  !__GNUC_STDC_INLINE__ && !defined(__cplusplus)
59
    #error Nonstandard GNU inlining semantics. Compile with -std=c99 or better.
60
    /* We could instead use macros PCG_INLINE and PCG_EXTERN_INLINE
61
       but better to just reject ancient C code. */
62
#endif
63
64
#if __cplusplus
65
extern "C" {
66
#endif
67
68
/*
69
 * Rotate helper functions.
70
 */
71
72
inline uint8_t pcg_rotr_8(uint8_t value, unsigned int rot)
73
0
{
74
0
/* Unfortunately, clang is kinda pathetic when it comes to properly
75
0
 * recognizing idiomatic rotate code, so for clang we actually provide
76
0
 * assembler directives (enabled with PCG_USE_INLINE_ASM).  Boo, hiss.
77
0
 */
78
0
#if PCG_USE_INLINE_ASM && __clang__ && (__x86_64__  || __i386__)
79
0
    asm ("rorb   %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
80
0
    return value;
81
0
#else
82
0
    return (value >> rot) | (value << ((- rot) & 7));
83
0
#endif
84
0
}
85
86
inline uint16_t pcg_rotr_16(uint16_t value, unsigned int rot)
87
0
{
88
0
#if PCG_USE_INLINE_ASM && __clang__ && (__x86_64__  || __i386__)
89
0
    asm ("rorw   %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
90
0
    return value;
91
0
#else
92
0
    return (value >> rot) | (value << ((- rot) & 15));
93
0
#endif
94
0
}
95
96
inline uint32_t pcg_rotr_32(uint32_t value, unsigned int rot)
97
0
{
98
#if PCG_USE_INLINE_ASM && __clang__ && (__x86_64__  || __i386__)
99
    asm ("rorl   %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
100
    return value;
101
#else
102
0
    return (value >> rot) | (value << ((- rot) & 31));
103
0
#endif
104
0
}
105
106
inline uint64_t pcg_rotr_64(uint64_t value, unsigned int rot)
107
0
{
108
0
#if 0 && PCG_USE_INLINE_ASM && __clang__ && __x86_64__
109
0
    /* For whatever reason, clang actually *does* generate rotq by
110
0
       itself, so we don't need this code. */
111
0
    asm ("rorq   %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
112
0
    return value;
113
0
#else
114
0
    return (value >> rot) | (value << ((- rot) & 63));
115
0
#endif
116
0
}
117
118
#if PCG_HAS_128BIT_OPS
119
inline pcg128_t pcg_rotr_128(pcg128_t value, unsigned int rot)
120
0
{
121
0
    return (value >> rot) | (value << ((- rot) & 127));
122
0
}
123
#endif
124
125
/*
126
 * Output functions.  These are the core of the PCG generation scheme.
127
 */
128
129
/* XSH RS */
130
131
inline uint8_t pcg_output_xsh_rs_16_8(uint16_t state)
132
0
{
133
0
    return (uint8_t)(((state >> 7u) ^ state) >> ((state >> 14u) + 3u));
134
0
}
135
136
inline uint16_t pcg_output_xsh_rs_32_16(uint32_t state)
137
0
{
138
0
    return (uint16_t)(((state >> 11u) ^ state) >> ((state >> 30u) + 11u));
139
0
}
140
141
inline uint32_t pcg_output_xsh_rs_64_32(uint64_t state)
142
0
{
143
0
144
0
    return (uint32_t)(((state >> 22u) ^ state) >> ((state >> 61u) + 22u));
145
0
}
146
147
#if PCG_HAS_128BIT_OPS
148
inline uint64_t pcg_output_xsh_rs_128_64(pcg128_t state)
149
0
{
150
0
    return (uint64_t)(((state >> 43u) ^ state) >> ((state >> 124u) + 45u));
151
0
}
152
#endif
153
154
/* XSH RR */
155
156
inline uint8_t pcg_output_xsh_rr_16_8(uint16_t state)
157
0
{
158
0
    return pcg_rotr_8(((state >> 5u) ^ state) >> 5u, state >> 13u);
159
0
}
160
161
inline uint16_t pcg_output_xsh_rr_32_16(uint32_t state)
162
0
{
163
0
    return pcg_rotr_16(((state >> 10u) ^ state) >> 12u, state >> 28u);
164
0
}
165
166
inline uint32_t pcg_output_xsh_rr_64_32(uint64_t state)
167
0
{
168
0
    return pcg_rotr_32(((state >> 18u) ^ state) >> 27u, state >> 59u);
169
0
}
170
171
#if PCG_HAS_128BIT_OPS
172
inline uint64_t pcg_output_xsh_rr_128_64(pcg128_t state)
173
0
{
174
0
    return pcg_rotr_64(((state >> 35u) ^ state) >> 58u, state >> 122u);
175
0
}
176
#endif
177
178
/* RXS M XS */
179
180
inline uint8_t pcg_output_rxs_m_xs_8_8(uint8_t state)
181
0
{
182
0
    uint8_t word = ((state >> ((state >> 6u) + 2u)) ^ state) * 217u;
183
0
    return (word >> 6u) ^ word;
184
0
}
185
186
inline uint16_t pcg_output_rxs_m_xs_16_16(uint16_t state)
187
0
{
188
0
    uint16_t word = ((state >> ((state >> 13u) + 3u)) ^ state) * 62169u;
189
0
    return (word >> 11u) ^ word;
190
0
}
191
192
inline uint32_t pcg_output_rxs_m_xs_32_32(uint32_t state)
193
0
{
194
0
    uint32_t word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
195
0
    return (word >> 22u) ^ word;
196
0
}
197
198
inline uint64_t pcg_output_rxs_m_xs_64_64(uint64_t state)
199
0
{
200
0
    uint64_t word = ((state >> ((state >> 59u) + 5u)) ^ state)
201
0
                    * 12605985483714917081ull;
202
0
    return (word >> 43u) ^ word;
203
0
}
204
205
#if PCG_HAS_128BIT_OPS
206
inline pcg128_t pcg_output_rxs_m_xs_128_128(pcg128_t state)
207
0
{
208
0
    pcg128_t word = ((state >> ((state >> 122u) + 6u)) ^ state)
209
0
                       * (PCG_128BIT_CONSTANT(17766728186571221404ULL,
210
0
                                              12605985483714917081ULL));
211
0
    /* 327738287884841127335028083622016905945 */
212
0
    return (word >> 86u) ^ word;
213
0
}
214
#endif
215
216
/* RXS M */
217
218
inline uint8_t pcg_output_rxs_m_16_8(uint16_t state)
219
0
{
220
0
    return (((state >> ((state >> 13u) + 3u)) ^ state) * 62169u) >> 8u;
221
0
}
222
223
inline uint16_t pcg_output_rxs_m_32_16(uint32_t state)
224
0
{
225
0
    return (((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u) >> 16u;
226
0
}
227
228
inline uint32_t pcg_output_rxs_m_64_32(uint64_t state)
229
0
{
230
0
    return (((state >> ((state >> 59u) + 5u)) ^ state)
231
0
               * 12605985483714917081ull) >> 32u;
232
0
}
233
234
#if PCG_HAS_128BIT_OPS
235
inline uint64_t pcg_output_rxs_m_128_64(pcg128_t state)
236
0
{
237
0
    return (((state >> ((state >> 122u) + 6u)) ^ state)
238
0
               * (PCG_128BIT_CONSTANT(17766728186571221404ULL,
239
0
                                      12605985483714917081ULL))) >> 64u;
240
0
    /* 327738287884841127335028083622016905945 */
241
0
}
242
#endif
243
244
/* XSL RR (only defined for >= 64 bits) */
245
246
inline uint32_t pcg_output_xsl_rr_64_32(uint64_t state)
247
0
{
248
0
    return pcg_rotr_32(((uint32_t)(state >> 32u)) ^ (uint32_t)state,
249
0
                       state >> 59u);
250
0
}
251
252
#if PCG_HAS_128BIT_OPS
253
inline uint64_t pcg_output_xsl_rr_128_64(pcg128_t state)
254
0
{
255
0
    return pcg_rotr_64(((uint64_t)(state >> 64u)) ^ (uint64_t)state,
256
0
                       state >> 122u);
257
0
}
258
#endif
259
260
/* XSL RR RR (only defined for >= 64 bits) */
261
262
inline uint64_t pcg_output_xsl_rr_rr_64_64(uint64_t state)
263
0
{
264
0
    uint32_t rot1 = (uint32_t)(state >> 59u);
265
0
    uint32_t high = (uint32_t)(state >> 32u);
266
0
    uint32_t low  = (uint32_t)state;
267
0
    uint32_t xored = high ^ low;
268
0
    uint32_t newlow  = pcg_rotr_32(xored, rot1);
269
0
    uint32_t newhigh = pcg_rotr_32(high, newlow & 31u);
270
0
    return (((uint64_t)newhigh) << 32u) | newlow;
271
0
}
272
273
#if PCG_HAS_128BIT_OPS
274
inline pcg128_t pcg_output_xsl_rr_rr_128_128(pcg128_t state)
275
0
{
276
0
    uint32_t rot1 = (uint32_t)(state >> 122u);
277
0
    uint64_t high = (uint64_t)(state >> 64u);
278
0
    uint64_t low  = (uint64_t)state;
279
0
    uint64_t xored = high ^ low;
280
0
    uint64_t newlow  = pcg_rotr_64(xored, rot1);
281
0
    uint64_t newhigh = pcg_rotr_64(high, newlow & 63u);
282
0
    return (((pcg128_t)newhigh) << 64u) | newlow;
283
0
}
284
#endif
285
286
#define PCG_DEFAULT_MULTIPLIER_8   141U
287
#define PCG_DEFAULT_MULTIPLIER_16  12829U
288
#define PCG_DEFAULT_MULTIPLIER_32  747796405U
289
0
#define PCG_DEFAULT_MULTIPLIER_64  6364136223846793005ULL
290
291
#define PCG_DEFAULT_INCREMENT_8    77U
292
#define PCG_DEFAULT_INCREMENT_16   47989U
293
#define PCG_DEFAULT_INCREMENT_32   2891336453U
294
#define PCG_DEFAULT_INCREMENT_64   1442695040888963407ULL
295
296
#if PCG_HAS_128BIT_OPS
297
#define PCG_DEFAULT_MULTIPLIER_128 \
298
        PCG_128BIT_CONSTANT(2549297995355413924ULL,4865540595714422341ULL)
299
#define PCG_DEFAULT_INCREMENT_128  \
300
        PCG_128BIT_CONSTANT(6364136223846793005ULL,1442695040888963407ULL)
301
#endif
302
303
/*
304
 * Static initialization constants (if you can't call srandom for some
305
 * bizarre reason).
306
 */
307
308
#define PCG_STATE_ONESEQ_8_INITIALIZER      { 0xd7U }
309
#define PCG_STATE_ONESEQ_16_INITIALIZER     { 0x20dfU }
310
#define PCG_STATE_ONESEQ_32_INITIALIZER     { 0x46b56677U }
311
#define PCG_STATE_ONESEQ_64_INITIALIZER     { 0x4d595df4d0f33173ULL }
312
#if PCG_HAS_128BIT_OPS
313
#define PCG_STATE_ONESEQ_128_INITIALIZER                                       \
314
    { PCG_128BIT_CONSTANT(0xb8dc10e158a92392ULL, 0x98046df007ec0a53ULL) }
315
#endif
316
317
#define PCG_STATE_UNIQUE_8_INITIALIZER      PCG_STATE_ONESEQ_8_INITIALIZER
318
#define PCG_STATE_UNIQUE_16_INITIALIZER     PCG_STATE_ONESEQ_16_INITIALIZER
319
#define PCG_STATE_UNIQUE_32_INITIALIZER     PCG_STATE_ONESEQ_32_INITIALIZER
320
#define PCG_STATE_UNIQUE_64_INITIALIZER     PCG_STATE_ONESEQ_64_INITIALIZER
321
#if PCG_HAS_128BIT_OPS
322
#define PCG_STATE_UNIQUE_128_INITIALIZER    PCG_STATE_ONESEQ_128_INITIALIZER
323
#endif
324
325
#define PCG_STATE_MCG_8_INITIALIZER         { 0xe5U }
326
#define PCG_STATE_MCG_16_INITIALIZER        { 0xa5e5U }
327
#define PCG_STATE_MCG_32_INITIALIZER        { 0xd15ea5e5U }
328
#define PCG_STATE_MCG_64_INITIALIZER        { 0xcafef00dd15ea5e5ULL }
329
#if PCG_HAS_128BIT_OPS
330
#define PCG_STATE_MCG_128_INITIALIZER                                          \
331
    { PCG_128BIT_CONSTANT(0x0000000000000000ULL, 0xcafef00dd15ea5e5ULL) }
332
#endif
333
334
#define PCG_STATE_SETSEQ_8_INITIALIZER      { 0x9bU, 0xdbU }
335
#define PCG_STATE_SETSEQ_16_INITIALIZER     { 0xe39bU, 0x5bdbU }
336
#define PCG_STATE_SETSEQ_32_INITIALIZER     { 0xec02d89bU, 0x94b95bdbU }
337
#define PCG_STATE_SETSEQ_64_INITIALIZER                                        \
338
    { 0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL }
339
#if PCG_HAS_128BIT_OPS
340
#define PCG_STATE_SETSEQ_128_INITIALIZER                                       \
341
    { PCG_128BIT_CONSTANT(0x979c9a98d8462005ULL, 0x7d3e9cb6cfe0549bULL),       \
342
      PCG_128BIT_CONSTANT(0x0000000000000001ULL, 0xda3e39cb94b95bdbULL) }
343
#endif
344
345
/* Representations for the oneseq, mcg, and unique variants */
346
347
struct pcg_state_8 {
348
    uint8_t state;
349
};
350
351
struct pcg_state_16 {
352
    uint16_t state;
353
};
354
355
struct pcg_state_32 {
356
    uint32_t state;
357
};
358
359
struct pcg_state_64 {
360
    uint64_t state;
361
};
362
363
#if PCG_HAS_128BIT_OPS
364
struct pcg_state_128 {
365
    pcg128_t state;
366
};
367
#endif
368
369
/* Representations setseq variants */
370
371
struct pcg_state_setseq_8 {
372
    uint8_t state;
373
    uint8_t inc;
374
};
375
376
struct pcg_state_setseq_16 {
377
    uint16_t state;
378
    uint16_t inc;
379
};
380
381
struct pcg_state_setseq_32 {
382
    uint32_t state;
383
    uint32_t inc;
384
};
385
386
struct pcg_state_setseq_64 {
387
    uint64_t state;
388
    uint64_t inc;
389
};
390
391
#if PCG_HAS_128BIT_OPS
392
struct pcg_state_setseq_128 {
393
    pcg128_t state;
394
    pcg128_t inc;
395
};
396
#endif
397
398
/* Multi-step advance functions (jump-ahead, jump-back) */
399
400
extern uint8_t pcg_advance_lcg_8(uint8_t state, uint8_t delta, uint8_t cur_mult,
401
                                 uint8_t cur_plus);
402
extern uint16_t pcg_advance_lcg_16(uint16_t state, uint16_t delta,
403
                                   uint16_t cur_mult, uint16_t cur_plus);
404
extern uint32_t pcg_advance_lcg_32(uint32_t state, uint32_t delta,
405
                                   uint32_t cur_mult, uint32_t cur_plus);
406
extern uint64_t pcg_advance_lcg_64(uint64_t state, uint64_t delta,
407
                                   uint64_t cur_mult, uint64_t cur_plus);
408
409
#if PCG_HAS_128BIT_OPS
410
extern pcg128_t pcg_advance_lcg_128(pcg128_t state, pcg128_t delta,
411
                                    pcg128_t cur_mult, pcg128_t cur_plus);
412
#endif
413
414
/* Functions to advance the underlying LCG, one version for each size and
415
 * each style.  These functions are considered semi-private.  There is rarely
416
 * a good reason to call them directly.
417
 */
418
419
inline void pcg_oneseq_8_step_r(struct pcg_state_8* rng)
420
0
{
421
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8
422
0
                 + PCG_DEFAULT_INCREMENT_8;
423
0
}
424
425
inline void pcg_oneseq_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
426
0
{
427
0
    rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
428
0
                                   PCG_DEFAULT_INCREMENT_8);
429
0
}
430
431
inline void pcg_mcg_8_step_r(struct pcg_state_8* rng)
432
0
{
433
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8;
434
0
}
435
436
inline void pcg_mcg_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
437
0
{
438
0
    rng->state
439
0
        = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8, 0u);
440
0
}
441
442
inline void pcg_unique_8_step_r(struct pcg_state_8* rng)
443
0
{
444
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8
445
0
                 + (uint8_t)(((intptr_t)rng) | 1u);
446
0
}
447
448
inline void pcg_unique_8_advance_r(struct pcg_state_8* rng, uint8_t delta)
449
0
{
450
0
    rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
451
0
                                   (uint8_t)(((intptr_t)rng) | 1u));
452
0
}
453
454
inline void pcg_setseq_8_step_r(struct pcg_state_setseq_8* rng)
455
0
{
456
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_8 + rng->inc;
457
0
}
458
459
inline void pcg_setseq_8_advance_r(struct pcg_state_setseq_8* rng,
460
                                   uint8_t delta)
461
0
{
462
0
    rng->state = pcg_advance_lcg_8(rng->state, delta, PCG_DEFAULT_MULTIPLIER_8,
463
0
                                   rng->inc);
464
0
}
465
466
inline void pcg_oneseq_16_step_r(struct pcg_state_16* rng)
467
0
{
468
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16
469
0
                 + PCG_DEFAULT_INCREMENT_16;
470
0
}
471
472
inline void pcg_oneseq_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
473
0
{
474
0
    rng->state = pcg_advance_lcg_16(
475
0
        rng->state, delta, PCG_DEFAULT_MULTIPLIER_16, PCG_DEFAULT_INCREMENT_16);
476
0
}
477
478
inline void pcg_mcg_16_step_r(struct pcg_state_16* rng)
479
0
{
480
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16;
481
0
}
482
483
inline void pcg_mcg_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
484
0
{
485
0
    rng->state
486
0
        = pcg_advance_lcg_16(rng->state, delta, PCG_DEFAULT_MULTIPLIER_16, 0u);
487
0
}
488
489
inline void pcg_unique_16_step_r(struct pcg_state_16* rng)
490
0
{
491
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16
492
0
                 + (uint16_t)(((intptr_t)rng) | 1u);
493
0
}
494
495
inline void pcg_unique_16_advance_r(struct pcg_state_16* rng, uint16_t delta)
496
0
{
497
0
    rng->state
498
0
        = pcg_advance_lcg_16(rng->state, delta, PCG_DEFAULT_MULTIPLIER_16,
499
0
                             (uint16_t)(((intptr_t)rng) | 1u));
500
0
}
501
502
inline void pcg_setseq_16_step_r(struct pcg_state_setseq_16* rng)
503
0
{
504
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_16 + rng->inc;
505
0
}
506
507
inline void pcg_setseq_16_advance_r(struct pcg_state_setseq_16* rng,
508
                                    uint16_t delta)
509
0
{
510
0
    rng->state = pcg_advance_lcg_16(rng->state, delta,
511
0
                                    PCG_DEFAULT_MULTIPLIER_16, rng->inc);
512
0
}
513
514
inline void pcg_oneseq_32_step_r(struct pcg_state_32* rng)
515
0
{
516
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32
517
0
                 + PCG_DEFAULT_INCREMENT_32;
518
0
}
519
520
inline void pcg_oneseq_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
521
0
{
522
0
    rng->state = pcg_advance_lcg_32(
523
0
        rng->state, delta, PCG_DEFAULT_MULTIPLIER_32, PCG_DEFAULT_INCREMENT_32);
524
0
}
525
526
inline void pcg_mcg_32_step_r(struct pcg_state_32* rng)
527
0
{
528
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32;
529
0
}
530
531
inline void pcg_mcg_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
532
0
{
533
0
    rng->state
534
0
        = pcg_advance_lcg_32(rng->state, delta, PCG_DEFAULT_MULTIPLIER_32, 0u);
535
0
}
536
537
inline void pcg_unique_32_step_r(struct pcg_state_32* rng)
538
0
{
539
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32
540
0
                 + (uint32_t)(((intptr_t)rng) | 1u);
541
0
}
542
543
inline void pcg_unique_32_advance_r(struct pcg_state_32* rng, uint32_t delta)
544
0
{
545
0
    rng->state
546
0
        = pcg_advance_lcg_32(rng->state, delta, PCG_DEFAULT_MULTIPLIER_32,
547
0
                             (uint32_t)(((intptr_t)rng) | 1u));
548
0
}
549
550
inline void pcg_setseq_32_step_r(struct pcg_state_setseq_32* rng)
551
0
{
552
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_32 + rng->inc;
553
0
}
554
555
inline void pcg_setseq_32_advance_r(struct pcg_state_setseq_32* rng,
556
                                    uint32_t delta)
557
0
{
558
0
    rng->state = pcg_advance_lcg_32(rng->state, delta,
559
0
                                    PCG_DEFAULT_MULTIPLIER_32, rng->inc);
560
0
}
561
562
inline void pcg_oneseq_64_step_r(struct pcg_state_64* rng)
563
0
{
564
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64
565
0
                 + PCG_DEFAULT_INCREMENT_64;
566
0
}
567
568
inline void pcg_oneseq_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
569
0
{
570
0
    rng->state = pcg_advance_lcg_64(
571
0
        rng->state, delta, PCG_DEFAULT_MULTIPLIER_64, PCG_DEFAULT_INCREMENT_64);
572
0
}
573
574
inline void pcg_mcg_64_step_r(struct pcg_state_64* rng)
575
0
{
576
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64;
577
0
}
578
579
inline void pcg_mcg_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
580
0
{
581
0
    rng->state
582
0
        = pcg_advance_lcg_64(rng->state, delta, PCG_DEFAULT_MULTIPLIER_64, 0u);
583
0
}
584
585
inline void pcg_unique_64_step_r(struct pcg_state_64* rng)
586
0
{
587
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64
588
0
                 + (uint64_t)(((intptr_t)rng) | 1u);
589
0
}
590
591
inline void pcg_unique_64_advance_r(struct pcg_state_64* rng, uint64_t delta)
592
0
{
593
0
    rng->state
594
0
        = pcg_advance_lcg_64(rng->state, delta, PCG_DEFAULT_MULTIPLIER_64,
595
0
                             (uint64_t)(((intptr_t)rng) | 1u));
596
0
}
597
598
inline void pcg_setseq_64_step_r(struct pcg_state_setseq_64* rng)
599
0
{
600
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64 + rng->inc;
601
0
}
602
603
inline void pcg_setseq_64_advance_r(struct pcg_state_setseq_64* rng,
604
                                    uint64_t delta)
605
0
{
606
0
    rng->state = pcg_advance_lcg_64(rng->state, delta,
607
0
                                    PCG_DEFAULT_MULTIPLIER_64, rng->inc);
608
0
}
609
610
#if PCG_HAS_128BIT_OPS
611
inline void pcg_oneseq_128_step_r(struct pcg_state_128* rng)
612
0
{
613
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128
614
0
                 + PCG_DEFAULT_INCREMENT_128;
615
0
}
616
#endif
617
618
#if PCG_HAS_128BIT_OPS
619
inline void pcg_oneseq_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
620
0
{
621
0
    rng->state
622
0
        = pcg_advance_lcg_128(rng->state, delta, PCG_DEFAULT_MULTIPLIER_128,
623
0
                              PCG_DEFAULT_INCREMENT_128);
624
0
}
625
#endif
626
627
#if PCG_HAS_128BIT_OPS
628
inline void pcg_mcg_128_step_r(struct pcg_state_128* rng)
629
0
{
630
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128;
631
0
}
632
#endif
633
634
#if PCG_HAS_128BIT_OPS
635
inline void pcg_mcg_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
636
0
{
637
0
    rng->state = pcg_advance_lcg_128(rng->state, delta,
638
0
                                     PCG_DEFAULT_MULTIPLIER_128, 0u);
639
0
}
640
#endif
641
642
#if PCG_HAS_128BIT_OPS
643
inline void pcg_unique_128_step_r(struct pcg_state_128* rng)
644
0
{
645
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128
646
0
                 + (pcg128_t)(((intptr_t)rng) | 1u);
647
0
}
648
#endif
649
650
#if PCG_HAS_128BIT_OPS
651
inline void pcg_unique_128_advance_r(struct pcg_state_128* rng, pcg128_t delta)
652
0
{
653
0
    rng->state
654
0
        = pcg_advance_lcg_128(rng->state, delta, PCG_DEFAULT_MULTIPLIER_128,
655
0
                              (pcg128_t)(((intptr_t)rng) | 1u));
656
0
}
657
#endif
658
659
#if PCG_HAS_128BIT_OPS
660
inline void pcg_setseq_128_step_r(struct pcg_state_setseq_128* rng)
661
0
{
662
0
    rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128 + rng->inc;
663
0
}
664
#endif
665
666
#if PCG_HAS_128BIT_OPS
667
inline void pcg_setseq_128_advance_r(struct pcg_state_setseq_128* rng,
668
                                     pcg128_t delta)
669
0
{
670
0
    rng->state = pcg_advance_lcg_128(rng->state, delta,
671
0
                                     PCG_DEFAULT_MULTIPLIER_128, rng->inc);
672
0
}
673
#endif
674
675
/* Functions to seed the RNG state, one version for each size and each
676
 * style.  Unlike the step functions, regular users can and should call
677
 * these functions.
678
 */
679
680
inline void pcg_oneseq_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
681
0
{
682
0
    rng->state = 0U;
683
0
    pcg_oneseq_8_step_r(rng);
684
0
    rng->state += initstate;
685
0
    pcg_oneseq_8_step_r(rng);
686
0
}
687
688
inline void pcg_mcg_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
689
0
{
690
0
    rng->state = initstate | 1u;
691
0
}
692
693
inline void pcg_unique_8_srandom_r(struct pcg_state_8* rng, uint8_t initstate)
694
0
{
695
0
    rng->state = 0U;
696
0
    pcg_unique_8_step_r(rng);
697
0
    rng->state += initstate;
698
0
    pcg_unique_8_step_r(rng);
699
0
}
700
701
inline void pcg_setseq_8_srandom_r(struct pcg_state_setseq_8* rng,
702
                                   uint8_t initstate, uint8_t initseq)
703
0
{
704
0
    rng->state = 0U;
705
0
    rng->inc = (initseq << 1u) | 1u;
706
0
    pcg_setseq_8_step_r(rng);
707
0
    rng->state += initstate;
708
0
    pcg_setseq_8_step_r(rng);
709
0
}
710
711
inline void pcg_oneseq_16_srandom_r(struct pcg_state_16* rng,
712
                                    uint16_t initstate)
713
0
{
714
0
    rng->state = 0U;
715
0
    pcg_oneseq_16_step_r(rng);
716
0
    rng->state += initstate;
717
0
    pcg_oneseq_16_step_r(rng);
718
0
}
719
720
inline void pcg_mcg_16_srandom_r(struct pcg_state_16* rng, uint16_t initstate)
721
0
{
722
0
    rng->state = initstate | 1u;
723
0
}
724
725
inline void pcg_unique_16_srandom_r(struct pcg_state_16* rng,
726
                                    uint16_t initstate)
727
0
{
728
0
    rng->state = 0U;
729
0
    pcg_unique_16_step_r(rng);
730
0
    rng->state += initstate;
731
0
    pcg_unique_16_step_r(rng);
732
0
}
733
734
inline void pcg_setseq_16_srandom_r(struct pcg_state_setseq_16* rng,
735
                                    uint16_t initstate, uint16_t initseq)
736
0
{
737
0
    rng->state = 0U;
738
0
    rng->inc = (initseq << 1u) | 1u;
739
0
    pcg_setseq_16_step_r(rng);
740
0
    rng->state += initstate;
741
0
    pcg_setseq_16_step_r(rng);
742
0
}
743
744
inline void pcg_oneseq_32_srandom_r(struct pcg_state_32* rng,
745
                                    uint32_t initstate)
746
0
{
747
0
    rng->state = 0U;
748
0
    pcg_oneseq_32_step_r(rng);
749
0
    rng->state += initstate;
750
0
    pcg_oneseq_32_step_r(rng);
751
0
}
752
753
inline void pcg_mcg_32_srandom_r(struct pcg_state_32* rng, uint32_t initstate)
754
0
{
755
0
    rng->state = initstate | 1u;
756
0
}
757
758
inline void pcg_unique_32_srandom_r(struct pcg_state_32* rng,
759
                                    uint32_t initstate)
760
0
{
761
0
    rng->state = 0U;
762
0
    pcg_unique_32_step_r(rng);
763
0
    rng->state += initstate;
764
0
    pcg_unique_32_step_r(rng);
765
0
}
766
767
inline void pcg_setseq_32_srandom_r(struct pcg_state_setseq_32* rng,
768
                                    uint32_t initstate, uint32_t initseq)
769
0
{
770
0
    rng->state = 0U;
771
0
    rng->inc = (initseq << 1u) | 1u;
772
0
    pcg_setseq_32_step_r(rng);
773
0
    rng->state += initstate;
774
0
    pcg_setseq_32_step_r(rng);
775
0
}
776
777
inline void pcg_oneseq_64_srandom_r(struct pcg_state_64* rng,
778
                                    uint64_t initstate)
779
0
{
780
0
    rng->state = 0U;
781
0
    pcg_oneseq_64_step_r(rng);
782
0
    rng->state += initstate;
783
0
    pcg_oneseq_64_step_r(rng);
784
0
}
785
786
inline void pcg_mcg_64_srandom_r(struct pcg_state_64* rng, uint64_t initstate)
787
0
{
788
0
    rng->state = initstate | 1u;
789
0
}
790
791
inline void pcg_unique_64_srandom_r(struct pcg_state_64* rng,
792
                                    uint64_t initstate)
793
0
{
794
0
    rng->state = 0U;
795
0
    pcg_unique_64_step_r(rng);
796
0
    rng->state += initstate;
797
0
    pcg_unique_64_step_r(rng);
798
0
}
799
800
inline void pcg_setseq_64_srandom_r(struct pcg_state_setseq_64* rng,
801
                                    uint64_t initstate, uint64_t initseq)
802
0
{
803
0
    rng->state = 0U;
804
0
    rng->inc = (initseq << 1u) | 1u;
805
0
    pcg_setseq_64_step_r(rng);
806
0
    rng->state += initstate;
807
0
    pcg_setseq_64_step_r(rng);
808
0
}
809
810
#if PCG_HAS_128BIT_OPS
811
inline void pcg_oneseq_128_srandom_r(struct pcg_state_128* rng,
812
                                     pcg128_t initstate)
813
0
{
814
0
    rng->state = 0U;
815
0
    pcg_oneseq_128_step_r(rng);
816
0
    rng->state += initstate;
817
0
    pcg_oneseq_128_step_r(rng);
818
0
}
819
#endif
820
821
#if PCG_HAS_128BIT_OPS
822
inline void pcg_mcg_128_srandom_r(struct pcg_state_128* rng, pcg128_t initstate)
823
0
{
824
0
    rng->state = initstate | 1u;
825
0
}
826
#endif
827
828
#if PCG_HAS_128BIT_OPS
829
inline void pcg_unique_128_srandom_r(struct pcg_state_128* rng,
830
                                     pcg128_t initstate)
831
0
{
832
0
    rng->state = 0U;
833
0
    pcg_unique_128_step_r(rng);
834
0
    rng->state += initstate;
835
0
    pcg_unique_128_step_r(rng);
836
0
}
837
#endif
838
839
#if PCG_HAS_128BIT_OPS
840
inline void pcg_setseq_128_srandom_r(struct pcg_state_setseq_128* rng,
841
                                     pcg128_t initstate, pcg128_t initseq)
842
0
{
843
0
    rng->state = 0U;
844
0
    rng->inc = (initseq << 1u) | 1u;
845
0
    pcg_setseq_128_step_r(rng);
846
0
    rng->state += initstate;
847
0
    pcg_setseq_128_step_r(rng);
848
0
}
849
#endif
850
851
/* Now, finally we create each of the individual generators. We provide
852
 * a random_r function that provides a random number of the appropriate
853
 * type (using the full range of the type) and a boundedrand_r version
854
 * that provides
855
 *
856
 * Implementation notes for boundedrand_r:
857
 *
858
 *     To avoid bias, we need to make the range of the RNG a multiple of
859
 *     bound, which we do by dropping output less than a threshold.
860
 *     Let's consider a 32-bit case...  A naive scheme to calculate the
861
 *     threshold would be to do
862
 *
863
 *         uint32_t threshold = 0x100000000ull % bound;
864
 *
865
 *     but 64-bit div/mod is slower than 32-bit div/mod (especially on
866
 *     32-bit platforms).  In essence, we do
867
 *
868
 *         uint32_t threshold = (0x100000000ull-bound) % bound;
869
 *
870
 *     because this version will calculate the same modulus, but the LHS
871
 *     value is less than 2^32.
872
 *
873
 *     (Note that using modulo is only wise for good RNGs, poorer RNGs
874
 *     such as raw LCGs do better using a technique based on division.)
875
 *     Empricical tests show that division is preferable to modulus for
876
 *     reducting the range of an RNG.  It's faster, and sometimes it can
877
 *     even be statistically prefereable.
878
 */
879
880
/* Generation functions for XSH RS */
881
882
inline uint8_t pcg_oneseq_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
883
0
{
884
0
    uint16_t oldstate = rng->state;
885
0
    pcg_oneseq_16_step_r(rng);
886
0
    return pcg_output_xsh_rs_16_8(oldstate);
887
0
}
888
889
inline uint8_t pcg_oneseq_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
890
                                                    uint8_t bound)
891
0
{
892
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
893
0
    for (;;) {
894
0
        uint8_t r = pcg_oneseq_16_xsh_rs_8_random_r(rng);
895
0
        if (r >= threshold)
896
0
            return r % bound;
897
0
    }
898
0
}
899
900
inline uint16_t pcg_oneseq_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
901
0
{
902
0
    uint32_t oldstate = rng->state;
903
0
    pcg_oneseq_32_step_r(rng);
904
0
    return pcg_output_xsh_rs_32_16(oldstate);
905
0
}
906
907
inline uint16_t pcg_oneseq_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
908
                                                      uint16_t bound)
909
0
{
910
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
911
0
    for (;;) {
912
0
        uint16_t r = pcg_oneseq_32_xsh_rs_16_random_r(rng);
913
0
        if (r >= threshold)
914
0
            return r % bound;
915
0
    }
916
0
}
917
918
inline uint32_t pcg_oneseq_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
919
0
{
920
0
    uint64_t oldstate = rng->state;
921
0
    pcg_oneseq_64_step_r(rng);
922
0
    return pcg_output_xsh_rs_64_32(oldstate);
923
0
}
924
925
inline uint32_t pcg_oneseq_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
926
                                                      uint32_t bound)
927
0
{
928
0
    uint32_t threshold = -bound % bound;
929
0
    for (;;) {
930
0
        uint32_t r = pcg_oneseq_64_xsh_rs_32_random_r(rng);
931
0
        if (r >= threshold)
932
0
            return r % bound;
933
0
    }
934
0
}
935
936
#if PCG_HAS_128BIT_OPS
937
inline uint64_t pcg_oneseq_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
938
0
{
939
0
    pcg_oneseq_128_step_r(rng);
940
0
    return pcg_output_xsh_rs_128_64(rng->state);
941
0
}
942
#endif
943
944
#if PCG_HAS_128BIT_OPS
945
inline uint64_t
946
pcg_oneseq_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
947
                                       uint64_t bound)
948
0
{
949
0
    uint64_t threshold = -bound % bound;
950
0
    for (;;) {
951
0
        uint64_t r = pcg_oneseq_128_xsh_rs_64_random_r(rng);
952
0
        if (r >= threshold)
953
0
            return r % bound;
954
0
    }
955
0
}
956
#endif
957
958
inline uint8_t pcg_unique_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
959
0
{
960
0
    uint16_t oldstate = rng->state;
961
0
    pcg_unique_16_step_r(rng);
962
0
    return pcg_output_xsh_rs_16_8(oldstate);
963
0
}
964
965
inline uint8_t pcg_unique_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
966
                                                    uint8_t bound)
967
0
{
968
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
969
0
    for (;;) {
970
0
        uint8_t r = pcg_unique_16_xsh_rs_8_random_r(rng);
971
0
        if (r >= threshold)
972
0
            return r % bound;
973
0
    }
974
0
}
975
976
inline uint16_t pcg_unique_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
977
0
{
978
0
    uint32_t oldstate = rng->state;
979
0
    pcg_unique_32_step_r(rng);
980
0
    return pcg_output_xsh_rs_32_16(oldstate);
981
0
}
982
983
inline uint16_t pcg_unique_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
984
                                                      uint16_t bound)
985
0
{
986
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
987
0
    for (;;) {
988
0
        uint16_t r = pcg_unique_32_xsh_rs_16_random_r(rng);
989
0
        if (r >= threshold)
990
0
            return r % bound;
991
0
    }
992
0
}
993
994
inline uint32_t pcg_unique_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
995
0
{
996
0
    uint64_t oldstate = rng->state;
997
0
    pcg_unique_64_step_r(rng);
998
0
    return pcg_output_xsh_rs_64_32(oldstate);
999
0
}
1000
1001
inline uint32_t pcg_unique_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
1002
                                                      uint32_t bound)
1003
0
{
1004
0
    uint32_t threshold = -bound % bound;
1005
0
    for (;;) {
1006
0
        uint32_t r = pcg_unique_64_xsh_rs_32_random_r(rng);
1007
0
        if (r >= threshold)
1008
0
            return r % bound;
1009
0
    }
1010
0
}
1011
1012
#if PCG_HAS_128BIT_OPS
1013
inline uint64_t pcg_unique_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
1014
0
{
1015
0
    pcg_unique_128_step_r(rng);
1016
0
    return pcg_output_xsh_rs_128_64(rng->state);
1017
0
}
1018
#endif
1019
1020
#if PCG_HAS_128BIT_OPS
1021
inline uint64_t
1022
pcg_unique_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
1023
                                       uint64_t bound)
1024
0
{
1025
0
    uint64_t threshold = -bound % bound;
1026
0
    for (;;) {
1027
0
        uint64_t r = pcg_unique_128_xsh_rs_64_random_r(rng);
1028
0
        if (r >= threshold)
1029
0
            return r % bound;
1030
0
    }
1031
0
}
1032
#endif
1033
1034
inline uint8_t pcg_setseq_16_xsh_rs_8_random_r(struct pcg_state_setseq_16* rng)
1035
0
{
1036
0
    uint16_t oldstate = rng->state;
1037
0
    pcg_setseq_16_step_r(rng);
1038
0
    return pcg_output_xsh_rs_16_8(oldstate);
1039
0
}
1040
1041
inline uint8_t
1042
pcg_setseq_16_xsh_rs_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1043
                                     uint8_t bound)
1044
0
{
1045
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
1046
0
    for (;;) {
1047
0
        uint8_t r = pcg_setseq_16_xsh_rs_8_random_r(rng);
1048
0
        if (r >= threshold)
1049
0
            return r % bound;
1050
0
    }
1051
0
}
1052
1053
inline uint16_t
1054
pcg_setseq_32_xsh_rs_16_random_r(struct pcg_state_setseq_32* rng)
1055
0
{
1056
0
    uint32_t oldstate = rng->state;
1057
0
    pcg_setseq_32_step_r(rng);
1058
0
    return pcg_output_xsh_rs_32_16(oldstate);
1059
0
}
1060
1061
inline uint16_t
1062
pcg_setseq_32_xsh_rs_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1063
                                      uint16_t bound)
1064
0
{
1065
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1066
0
    for (;;) {
1067
0
        uint16_t r = pcg_setseq_32_xsh_rs_16_random_r(rng);
1068
0
        if (r >= threshold)
1069
0
            return r % bound;
1070
0
    }
1071
0
}
1072
1073
inline uint32_t
1074
pcg_setseq_64_xsh_rs_32_random_r(struct pcg_state_setseq_64* rng)
1075
0
{
1076
0
    uint64_t oldstate = rng->state;
1077
0
    pcg_setseq_64_step_r(rng);
1078
0
    return pcg_output_xsh_rs_64_32(oldstate);
1079
0
}
1080
1081
inline uint32_t
1082
pcg_setseq_64_xsh_rs_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1083
                                      uint32_t bound)
1084
0
{
1085
0
    uint32_t threshold = -bound % bound;
1086
0
    for (;;) {
1087
0
        uint32_t r = pcg_setseq_64_xsh_rs_32_random_r(rng);
1088
0
        if (r >= threshold)
1089
0
            return r % bound;
1090
0
    }
1091
0
}
1092
1093
#if PCG_HAS_128BIT_OPS
1094
inline uint64_t
1095
pcg_setseq_128_xsh_rs_64_random_r(struct pcg_state_setseq_128* rng)
1096
0
{
1097
0
    pcg_setseq_128_step_r(rng);
1098
0
    return pcg_output_xsh_rs_128_64(rng->state);
1099
0
}
1100
#endif
1101
1102
#if PCG_HAS_128BIT_OPS
1103
inline uint64_t
1104
pcg_setseq_128_xsh_rs_64_boundedrand_r(struct pcg_state_setseq_128* rng,
1105
                                       uint64_t bound)
1106
0
{
1107
0
    uint64_t threshold = -bound % bound;
1108
0
    for (;;) {
1109
0
        uint64_t r = pcg_setseq_128_xsh_rs_64_random_r(rng);
1110
0
        if (r >= threshold)
1111
0
            return r % bound;
1112
0
    }
1113
0
}
1114
#endif
1115
1116
inline uint8_t pcg_mcg_16_xsh_rs_8_random_r(struct pcg_state_16* rng)
1117
0
{
1118
0
    uint16_t oldstate = rng->state;
1119
0
    pcg_mcg_16_step_r(rng);
1120
0
    return pcg_output_xsh_rs_16_8(oldstate);
1121
0
}
1122
1123
inline uint8_t pcg_mcg_16_xsh_rs_8_boundedrand_r(struct pcg_state_16* rng,
1124
                                                 uint8_t bound)
1125
0
{
1126
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
1127
0
    for (;;) {
1128
0
        uint8_t r = pcg_mcg_16_xsh_rs_8_random_r(rng);
1129
0
        if (r >= threshold)
1130
0
            return r % bound;
1131
0
    }
1132
0
}
1133
1134
inline uint16_t pcg_mcg_32_xsh_rs_16_random_r(struct pcg_state_32* rng)
1135
0
{
1136
0
    uint32_t oldstate = rng->state;
1137
0
    pcg_mcg_32_step_r(rng);
1138
0
    return pcg_output_xsh_rs_32_16(oldstate);
1139
0
}
1140
1141
inline uint16_t pcg_mcg_32_xsh_rs_16_boundedrand_r(struct pcg_state_32* rng,
1142
                                                   uint16_t bound)
1143
0
{
1144
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1145
0
    for (;;) {
1146
0
        uint16_t r = pcg_mcg_32_xsh_rs_16_random_r(rng);
1147
0
        if (r >= threshold)
1148
0
            return r % bound;
1149
0
    }
1150
0
}
1151
1152
inline uint32_t pcg_mcg_64_xsh_rs_32_random_r(struct pcg_state_64* rng)
1153
0
{
1154
0
    uint64_t oldstate = rng->state;
1155
0
    pcg_mcg_64_step_r(rng);
1156
0
    return pcg_output_xsh_rs_64_32(oldstate);
1157
0
}
1158
1159
inline uint32_t pcg_mcg_64_xsh_rs_32_boundedrand_r(struct pcg_state_64* rng,
1160
                                                   uint32_t bound)
1161
0
{
1162
0
    uint32_t threshold = -bound % bound;
1163
0
    for (;;) {
1164
0
        uint32_t r = pcg_mcg_64_xsh_rs_32_random_r(rng);
1165
0
        if (r >= threshold)
1166
0
            return r % bound;
1167
0
    }
1168
0
}
1169
1170
#if PCG_HAS_128BIT_OPS
1171
inline uint64_t pcg_mcg_128_xsh_rs_64_random_r(struct pcg_state_128* rng)
1172
0
{
1173
0
    pcg_mcg_128_step_r(rng);
1174
0
    return pcg_output_xsh_rs_128_64(rng->state);
1175
0
}
1176
#endif
1177
1178
#if PCG_HAS_128BIT_OPS
1179
inline uint64_t pcg_mcg_128_xsh_rs_64_boundedrand_r(struct pcg_state_128* rng,
1180
                                                    uint64_t bound)
1181
0
{
1182
0
    uint64_t threshold = -bound % bound;
1183
0
    for (;;) {
1184
0
        uint64_t r = pcg_mcg_128_xsh_rs_64_random_r(rng);
1185
0
        if (r >= threshold)
1186
0
            return r % bound;
1187
0
    }
1188
0
}
1189
#endif
1190
1191
/* Generation functions for XSH RR */
1192
1193
inline uint8_t pcg_oneseq_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1194
0
{
1195
0
    uint16_t oldstate = rng->state;
1196
0
    pcg_oneseq_16_step_r(rng);
1197
0
    return pcg_output_xsh_rr_16_8(oldstate);
1198
0
}
1199
1200
inline uint8_t pcg_oneseq_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1201
                                                    uint8_t bound)
1202
0
{
1203
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
1204
0
    for (;;) {
1205
0
        uint8_t r = pcg_oneseq_16_xsh_rr_8_random_r(rng);
1206
0
        if (r >= threshold)
1207
0
            return r % bound;
1208
0
    }
1209
0
}
1210
1211
inline uint16_t pcg_oneseq_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1212
0
{
1213
0
    uint32_t oldstate = rng->state;
1214
0
    pcg_oneseq_32_step_r(rng);
1215
0
    return pcg_output_xsh_rr_32_16(oldstate);
1216
0
}
1217
1218
inline uint16_t pcg_oneseq_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1219
                                                      uint16_t bound)
1220
0
{
1221
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1222
0
    for (;;) {
1223
0
        uint16_t r = pcg_oneseq_32_xsh_rr_16_random_r(rng);
1224
0
        if (r >= threshold)
1225
0
            return r % bound;
1226
0
    }
1227
0
}
1228
1229
inline uint32_t pcg_oneseq_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1230
0
{
1231
0
    uint64_t oldstate = rng->state;
1232
0
    pcg_oneseq_64_step_r(rng);
1233
0
    return pcg_output_xsh_rr_64_32(oldstate);
1234
0
}
1235
1236
inline uint32_t pcg_oneseq_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1237
                                                      uint32_t bound)
1238
0
{
1239
0
    uint32_t threshold = -bound % bound;
1240
0
    for (;;) {
1241
0
        uint32_t r = pcg_oneseq_64_xsh_rr_32_random_r(rng);
1242
0
        if (r >= threshold)
1243
0
            return r % bound;
1244
0
    }
1245
0
}
1246
1247
#if PCG_HAS_128BIT_OPS
1248
inline uint64_t pcg_oneseq_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1249
0
{
1250
0
    pcg_oneseq_128_step_r(rng);
1251
0
    return pcg_output_xsh_rr_128_64(rng->state);
1252
0
}
1253
#endif
1254
1255
#if PCG_HAS_128BIT_OPS
1256
inline uint64_t
1257
pcg_oneseq_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1258
                                       uint64_t bound)
1259
0
{
1260
0
    uint64_t threshold = -bound % bound;
1261
0
    for (;;) {
1262
0
        uint64_t r = pcg_oneseq_128_xsh_rr_64_random_r(rng);
1263
0
        if (r >= threshold)
1264
0
            return r % bound;
1265
0
    }
1266
0
}
1267
#endif
1268
1269
inline uint8_t pcg_unique_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1270
0
{
1271
0
    uint16_t oldstate = rng->state;
1272
0
    pcg_unique_16_step_r(rng);
1273
0
    return pcg_output_xsh_rr_16_8(oldstate);
1274
0
}
1275
1276
inline uint8_t pcg_unique_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1277
                                                    uint8_t bound)
1278
0
{
1279
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
1280
0
    for (;;) {
1281
0
        uint8_t r = pcg_unique_16_xsh_rr_8_random_r(rng);
1282
0
        if (r >= threshold)
1283
0
            return r % bound;
1284
0
    }
1285
0
}
1286
1287
inline uint16_t pcg_unique_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1288
0
{
1289
0
    uint32_t oldstate = rng->state;
1290
0
    pcg_unique_32_step_r(rng);
1291
0
    return pcg_output_xsh_rr_32_16(oldstate);
1292
0
}
1293
1294
inline uint16_t pcg_unique_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1295
                                                      uint16_t bound)
1296
0
{
1297
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1298
0
    for (;;) {
1299
0
        uint16_t r = pcg_unique_32_xsh_rr_16_random_r(rng);
1300
0
        if (r >= threshold)
1301
0
            return r % bound;
1302
0
    }
1303
0
}
1304
1305
inline uint32_t pcg_unique_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1306
0
{
1307
0
    uint64_t oldstate = rng->state;
1308
0
    pcg_unique_64_step_r(rng);
1309
0
    return pcg_output_xsh_rr_64_32(oldstate);
1310
0
}
1311
1312
inline uint32_t pcg_unique_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1313
                                                      uint32_t bound)
1314
0
{
1315
0
    uint32_t threshold = -bound % bound;
1316
0
    for (;;) {
1317
0
        uint32_t r = pcg_unique_64_xsh_rr_32_random_r(rng);
1318
0
        if (r >= threshold)
1319
0
            return r % bound;
1320
0
    }
1321
0
}
1322
1323
#if PCG_HAS_128BIT_OPS
1324
inline uint64_t pcg_unique_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1325
0
{
1326
0
    pcg_unique_128_step_r(rng);
1327
0
    return pcg_output_xsh_rr_128_64(rng->state);
1328
0
}
1329
#endif
1330
1331
#if PCG_HAS_128BIT_OPS
1332
inline uint64_t
1333
pcg_unique_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1334
                                       uint64_t bound)
1335
0
{
1336
0
    uint64_t threshold = -bound % bound;
1337
0
    for (;;) {
1338
0
        uint64_t r = pcg_unique_128_xsh_rr_64_random_r(rng);
1339
0
        if (r >= threshold)
1340
0
            return r % bound;
1341
0
    }
1342
0
}
1343
#endif
1344
1345
inline uint8_t pcg_setseq_16_xsh_rr_8_random_r(struct pcg_state_setseq_16* rng)
1346
0
{
1347
0
    uint16_t oldstate = rng->state;
1348
0
    pcg_setseq_16_step_r(rng);
1349
0
    return pcg_output_xsh_rr_16_8(oldstate);
1350
0
}
1351
1352
inline uint8_t
1353
pcg_setseq_16_xsh_rr_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1354
                                     uint8_t bound)
1355
0
{
1356
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
1357
0
    for (;;) {
1358
0
        uint8_t r = pcg_setseq_16_xsh_rr_8_random_r(rng);
1359
0
        if (r >= threshold)
1360
0
            return r % bound;
1361
0
    }
1362
0
}
1363
1364
inline uint16_t
1365
pcg_setseq_32_xsh_rr_16_random_r(struct pcg_state_setseq_32* rng)
1366
0
{
1367
0
    uint32_t oldstate = rng->state;
1368
0
    pcg_setseq_32_step_r(rng);
1369
0
    return pcg_output_xsh_rr_32_16(oldstate);
1370
0
}
1371
1372
inline uint16_t
1373
pcg_setseq_32_xsh_rr_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1374
                                      uint16_t bound)
1375
0
{
1376
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1377
0
    for (;;) {
1378
0
        uint16_t r = pcg_setseq_32_xsh_rr_16_random_r(rng);
1379
0
        if (r >= threshold)
1380
0
            return r % bound;
1381
0
    }
1382
0
}
1383
1384
inline uint32_t
1385
pcg_setseq_64_xsh_rr_32_random_r(struct pcg_state_setseq_64* rng)
1386
0
{
1387
0
    uint64_t oldstate = rng->state;
1388
0
    pcg_setseq_64_step_r(rng);
1389
0
    return pcg_output_xsh_rr_64_32(oldstate);
1390
0
}
1391
1392
inline uint32_t
1393
pcg_setseq_64_xsh_rr_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1394
                                      uint32_t bound)
1395
0
{
1396
0
    uint32_t threshold = -bound % bound;
1397
0
    for (;;) {
1398
0
        uint32_t r = pcg_setseq_64_xsh_rr_32_random_r(rng);
1399
0
        if (r >= threshold)
1400
0
            return r % bound;
1401
0
    }
1402
0
}
1403
1404
#if PCG_HAS_128BIT_OPS
1405
inline uint64_t
1406
pcg_setseq_128_xsh_rr_64_random_r(struct pcg_state_setseq_128* rng)
1407
0
{
1408
0
    pcg_setseq_128_step_r(rng);
1409
0
    return pcg_output_xsh_rr_128_64(rng->state);
1410
0
}
1411
#endif
1412
1413
#if PCG_HAS_128BIT_OPS
1414
inline uint64_t
1415
pcg_setseq_128_xsh_rr_64_boundedrand_r(struct pcg_state_setseq_128* rng,
1416
                                       uint64_t bound)
1417
0
{
1418
0
    uint64_t threshold = -bound % bound;
1419
0
    for (;;) {
1420
0
        uint64_t r = pcg_setseq_128_xsh_rr_64_random_r(rng);
1421
0
        if (r >= threshold)
1422
0
            return r % bound;
1423
0
    }
1424
0
}
1425
#endif
1426
1427
inline uint8_t pcg_mcg_16_xsh_rr_8_random_r(struct pcg_state_16* rng)
1428
0
{
1429
0
    uint16_t oldstate = rng->state;
1430
0
    pcg_mcg_16_step_r(rng);
1431
0
    return pcg_output_xsh_rr_16_8(oldstate);
1432
0
}
1433
1434
inline uint8_t pcg_mcg_16_xsh_rr_8_boundedrand_r(struct pcg_state_16* rng,
1435
                                                 uint8_t bound)
1436
0
{
1437
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
1438
0
    for (;;) {
1439
0
        uint8_t r = pcg_mcg_16_xsh_rr_8_random_r(rng);
1440
0
        if (r >= threshold)
1441
0
            return r % bound;
1442
0
    }
1443
0
}
1444
1445
inline uint16_t pcg_mcg_32_xsh_rr_16_random_r(struct pcg_state_32* rng)
1446
0
{
1447
0
    uint32_t oldstate = rng->state;
1448
0
    pcg_mcg_32_step_r(rng);
1449
0
    return pcg_output_xsh_rr_32_16(oldstate);
1450
0
}
1451
1452
inline uint16_t pcg_mcg_32_xsh_rr_16_boundedrand_r(struct pcg_state_32* rng,
1453
                                                   uint16_t bound)
1454
0
{
1455
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1456
0
    for (;;) {
1457
0
        uint16_t r = pcg_mcg_32_xsh_rr_16_random_r(rng);
1458
0
        if (r >= threshold)
1459
0
            return r % bound;
1460
0
    }
1461
0
}
1462
1463
inline uint32_t pcg_mcg_64_xsh_rr_32_random_r(struct pcg_state_64* rng)
1464
0
{
1465
0
    uint64_t oldstate = rng->state;
1466
0
    pcg_mcg_64_step_r(rng);
1467
0
    return pcg_output_xsh_rr_64_32(oldstate);
1468
0
}
1469
1470
inline uint32_t pcg_mcg_64_xsh_rr_32_boundedrand_r(struct pcg_state_64* rng,
1471
                                                   uint32_t bound)
1472
0
{
1473
0
    uint32_t threshold = -bound % bound;
1474
0
    for (;;) {
1475
0
        uint32_t r = pcg_mcg_64_xsh_rr_32_random_r(rng);
1476
0
        if (r >= threshold)
1477
0
            return r % bound;
1478
0
    }
1479
0
}
1480
1481
#if PCG_HAS_128BIT_OPS
1482
inline uint64_t pcg_mcg_128_xsh_rr_64_random_r(struct pcg_state_128* rng)
1483
0
{
1484
0
    pcg_mcg_128_step_r(rng);
1485
0
    return pcg_output_xsh_rr_128_64(rng->state);
1486
0
}
1487
#endif
1488
1489
#if PCG_HAS_128BIT_OPS
1490
inline uint64_t pcg_mcg_128_xsh_rr_64_boundedrand_r(struct pcg_state_128* rng,
1491
                                                    uint64_t bound)
1492
0
{
1493
0
    uint64_t threshold = -bound % bound;
1494
0
    for (;;) {
1495
0
        uint64_t r = pcg_mcg_128_xsh_rr_64_random_r(rng);
1496
0
        if (r >= threshold)
1497
0
            return r % bound;
1498
0
    }
1499
0
}
1500
#endif
1501
1502
/* Generation functions for RXS M XS (no MCG versions because they
1503
 * don't make sense when you want to use the entire state)
1504
 */
1505
1506
inline uint8_t pcg_oneseq_8_rxs_m_xs_8_random_r(struct pcg_state_8* rng)
1507
0
{
1508
0
    uint8_t oldstate = rng->state;
1509
0
    pcg_oneseq_8_step_r(rng);
1510
0
    return pcg_output_rxs_m_xs_8_8(oldstate);
1511
0
}
1512
1513
inline uint8_t pcg_oneseq_8_rxs_m_xs_8_boundedrand_r(struct pcg_state_8* rng,
1514
                                                     uint8_t bound)
1515
0
{
1516
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
1517
0
    for (;;) {
1518
0
        uint8_t r = pcg_oneseq_8_rxs_m_xs_8_random_r(rng);
1519
0
        if (r >= threshold)
1520
0
            return r % bound;
1521
0
    }
1522
0
}
1523
1524
inline uint16_t pcg_oneseq_16_rxs_m_xs_16_random_r(struct pcg_state_16* rng)
1525
0
{
1526
0
    uint16_t oldstate = rng->state;
1527
0
    pcg_oneseq_16_step_r(rng);
1528
0
    return pcg_output_rxs_m_xs_16_16(oldstate);
1529
0
}
1530
1531
inline uint16_t
1532
pcg_oneseq_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_16* rng,
1533
                                        uint16_t bound)
1534
0
{
1535
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1536
0
    for (;;) {
1537
0
        uint16_t r = pcg_oneseq_16_rxs_m_xs_16_random_r(rng);
1538
0
        if (r >= threshold)
1539
0
            return r % bound;
1540
0
    }
1541
0
}
1542
1543
inline uint32_t pcg_oneseq_32_rxs_m_xs_32_random_r(struct pcg_state_32* rng)
1544
0
{
1545
0
    uint32_t oldstate = rng->state;
1546
0
    pcg_oneseq_32_step_r(rng);
1547
0
    return pcg_output_rxs_m_xs_32_32(oldstate);
1548
0
}
1549
1550
inline uint32_t
1551
pcg_oneseq_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_32* rng,
1552
                                        uint32_t bound)
1553
0
{
1554
0
    uint32_t threshold = -bound % bound;
1555
0
    for (;;) {
1556
0
        uint32_t r = pcg_oneseq_32_rxs_m_xs_32_random_r(rng);
1557
0
        if (r >= threshold)
1558
0
            return r % bound;
1559
0
    }
1560
0
}
1561
1562
inline uint64_t pcg_oneseq_64_rxs_m_xs_64_random_r(struct pcg_state_64* rng)
1563
0
{
1564
0
    uint64_t oldstate = rng->state;
1565
0
    pcg_oneseq_64_step_r(rng);
1566
0
    return pcg_output_rxs_m_xs_64_64(oldstate);
1567
0
}
1568
1569
inline uint64_t
1570
pcg_oneseq_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_64* rng,
1571
                                        uint64_t bound)
1572
0
{
1573
0
    uint64_t threshold = -bound % bound;
1574
0
    for (;;) {
1575
0
        uint64_t r = pcg_oneseq_64_rxs_m_xs_64_random_r(rng);
1576
0
        if (r >= threshold)
1577
0
            return r % bound;
1578
0
    }
1579
0
}
1580
1581
#if PCG_HAS_128BIT_OPS
1582
inline pcg128_t pcg_oneseq_128_rxs_m_xs_128_random_r(struct pcg_state_128* rng)
1583
0
{
1584
0
    pcg_oneseq_128_step_r(rng);
1585
0
    return pcg_output_rxs_m_xs_128_128(rng->state);
1586
0
}
1587
#endif
1588
1589
#if PCG_HAS_128BIT_OPS
1590
inline pcg128_t
1591
pcg_oneseq_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_128* rng,
1592
                                          pcg128_t bound)
1593
0
{
1594
0
    pcg128_t threshold = -bound % bound;
1595
0
    for (;;) {
1596
0
        pcg128_t r = pcg_oneseq_128_rxs_m_xs_128_random_r(rng);
1597
0
        if (r >= threshold)
1598
0
            return r % bound;
1599
0
    }
1600
0
}
1601
#endif
1602
1603
inline uint16_t pcg_unique_16_rxs_m_xs_16_random_r(struct pcg_state_16* rng)
1604
0
{
1605
0
    uint16_t oldstate = rng->state;
1606
0
    pcg_unique_16_step_r(rng);
1607
0
    return pcg_output_rxs_m_xs_16_16(oldstate);
1608
0
}
1609
1610
inline uint16_t
1611
pcg_unique_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_16* rng,
1612
                                        uint16_t bound)
1613
0
{
1614
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1615
0
    for (;;) {
1616
0
        uint16_t r = pcg_unique_16_rxs_m_xs_16_random_r(rng);
1617
0
        if (r >= threshold)
1618
0
            return r % bound;
1619
0
    }
1620
0
}
1621
1622
inline uint32_t pcg_unique_32_rxs_m_xs_32_random_r(struct pcg_state_32* rng)
1623
0
{
1624
0
    uint32_t oldstate = rng->state;
1625
0
    pcg_unique_32_step_r(rng);
1626
0
    return pcg_output_rxs_m_xs_32_32(oldstate);
1627
0
}
1628
1629
inline uint32_t
1630
pcg_unique_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_32* rng,
1631
                                        uint32_t bound)
1632
0
{
1633
0
    uint32_t threshold = -bound % bound;
1634
0
    for (;;) {
1635
0
        uint32_t r = pcg_unique_32_rxs_m_xs_32_random_r(rng);
1636
0
        if (r >= threshold)
1637
0
            return r % bound;
1638
0
    }
1639
0
}
1640
1641
inline uint64_t pcg_unique_64_rxs_m_xs_64_random_r(struct pcg_state_64* rng)
1642
0
{
1643
0
    uint64_t oldstate = rng->state;
1644
0
    pcg_unique_64_step_r(rng);
1645
0
    return pcg_output_rxs_m_xs_64_64(oldstate);
1646
0
}
1647
1648
inline uint64_t
1649
pcg_unique_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_64* rng,
1650
                                        uint64_t bound)
1651
0
{
1652
0
    uint64_t threshold = -bound % bound;
1653
0
    for (;;) {
1654
0
        uint64_t r = pcg_unique_64_rxs_m_xs_64_random_r(rng);
1655
0
        if (r >= threshold)
1656
0
            return r % bound;
1657
0
    }
1658
0
}
1659
1660
#if PCG_HAS_128BIT_OPS
1661
inline pcg128_t pcg_unique_128_rxs_m_xs_128_random_r(struct pcg_state_128* rng)
1662
0
{
1663
0
    pcg_unique_128_step_r(rng);
1664
0
    return pcg_output_rxs_m_xs_128_128(rng->state);
1665
0
}
1666
#endif
1667
1668
#if PCG_HAS_128BIT_OPS
1669
inline pcg128_t
1670
pcg_unique_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_128* rng,
1671
                                          pcg128_t bound)
1672
0
{
1673
0
    pcg128_t threshold = -bound % bound;
1674
0
    for (;;) {
1675
0
        pcg128_t r = pcg_unique_128_rxs_m_xs_128_random_r(rng);
1676
0
        if (r >= threshold)
1677
0
            return r % bound;
1678
0
    }
1679
0
}
1680
#endif
1681
1682
inline uint8_t pcg_setseq_8_rxs_m_xs_8_random_r(struct pcg_state_setseq_8* rng)
1683
0
{
1684
0
    uint8_t oldstate = rng->state;
1685
0
    pcg_setseq_8_step_r(rng);
1686
0
    return pcg_output_rxs_m_xs_8_8(oldstate);
1687
0
}
1688
1689
inline uint8_t
1690
pcg_setseq_8_rxs_m_xs_8_boundedrand_r(struct pcg_state_setseq_8* rng,
1691
                                      uint8_t bound)
1692
0
{
1693
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
1694
0
    for (;;) {
1695
0
        uint8_t r = pcg_setseq_8_rxs_m_xs_8_random_r(rng);
1696
0
        if (r >= threshold)
1697
0
            return r % bound;
1698
0
    }
1699
0
}
1700
1701
inline uint16_t
1702
pcg_setseq_16_rxs_m_xs_16_random_r(struct pcg_state_setseq_16* rng)
1703
0
{
1704
0
    uint16_t oldstate = rng->state;
1705
0
    pcg_setseq_16_step_r(rng);
1706
0
    return pcg_output_rxs_m_xs_16_16(oldstate);
1707
0
}
1708
1709
inline uint16_t
1710
pcg_setseq_16_rxs_m_xs_16_boundedrand_r(struct pcg_state_setseq_16* rng,
1711
                                        uint16_t bound)
1712
0
{
1713
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1714
0
    for (;;) {
1715
0
        uint16_t r = pcg_setseq_16_rxs_m_xs_16_random_r(rng);
1716
0
        if (r >= threshold)
1717
0
            return r % bound;
1718
0
    }
1719
0
}
1720
1721
inline uint32_t
1722
pcg_setseq_32_rxs_m_xs_32_random_r(struct pcg_state_setseq_32* rng)
1723
0
{
1724
0
    uint32_t oldstate = rng->state;
1725
0
    pcg_setseq_32_step_r(rng);
1726
0
    return pcg_output_rxs_m_xs_32_32(oldstate);
1727
0
}
1728
1729
inline uint32_t
1730
pcg_setseq_32_rxs_m_xs_32_boundedrand_r(struct pcg_state_setseq_32* rng,
1731
                                        uint32_t bound)
1732
0
{
1733
0
    uint32_t threshold = -bound % bound;
1734
0
    for (;;) {
1735
0
        uint32_t r = pcg_setseq_32_rxs_m_xs_32_random_r(rng);
1736
0
        if (r >= threshold)
1737
0
            return r % bound;
1738
0
    }
1739
0
}
1740
1741
inline uint64_t
1742
pcg_setseq_64_rxs_m_xs_64_random_r(struct pcg_state_setseq_64* rng)
1743
0
{
1744
0
    uint64_t oldstate = rng->state;
1745
0
    pcg_setseq_64_step_r(rng);
1746
0
    return pcg_output_rxs_m_xs_64_64(oldstate);
1747
0
}
1748
1749
inline uint64_t
1750
pcg_setseq_64_rxs_m_xs_64_boundedrand_r(struct pcg_state_setseq_64* rng,
1751
                                        uint64_t bound)
1752
0
{
1753
0
    uint64_t threshold = -bound % bound;
1754
0
    for (;;) {
1755
0
        uint64_t r = pcg_setseq_64_rxs_m_xs_64_random_r(rng);
1756
0
        if (r >= threshold)
1757
0
            return r % bound;
1758
0
    }
1759
0
}
1760
1761
#if PCG_HAS_128BIT_OPS
1762
inline pcg128_t
1763
pcg_setseq_128_rxs_m_xs_128_random_r(struct pcg_state_setseq_128* rng)
1764
0
{
1765
0
    pcg_setseq_128_step_r(rng);
1766
0
    return pcg_output_rxs_m_xs_128_128(rng->state);
1767
0
}
1768
#endif
1769
1770
#if PCG_HAS_128BIT_OPS
1771
inline pcg128_t
1772
pcg_setseq_128_rxs_m_xs_128_boundedrand_r(struct pcg_state_setseq_128* rng,
1773
                                          pcg128_t bound)
1774
0
{
1775
0
    pcg128_t threshold = -bound % bound;
1776
0
    for (;;) {
1777
0
        pcg128_t r = pcg_setseq_128_rxs_m_xs_128_random_r(rng);
1778
0
        if (r >= threshold)
1779
0
            return r % bound;
1780
0
    }
1781
0
}
1782
#endif
1783
1784
/* Generation functions for RXS M */
1785
1786
inline uint8_t pcg_oneseq_16_rxs_m_8_random_r(struct pcg_state_16* rng)
1787
0
{
1788
0
    uint16_t oldstate = rng->state;
1789
0
    pcg_oneseq_16_step_r(rng);
1790
0
    return pcg_output_rxs_m_16_8(oldstate);
1791
0
}
1792
1793
inline uint8_t pcg_oneseq_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
1794
                                                   uint8_t bound)
1795
0
{
1796
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
1797
0
    for (;;) {
1798
0
        uint8_t r = pcg_oneseq_16_rxs_m_8_random_r(rng);
1799
0
        if (r >= threshold)
1800
0
            return r % bound;
1801
0
    }
1802
0
}
1803
1804
inline uint16_t pcg_oneseq_32_rxs_m_16_random_r(struct pcg_state_32* rng)
1805
0
{
1806
0
    uint32_t oldstate = rng->state;
1807
0
    pcg_oneseq_32_step_r(rng);
1808
0
    return pcg_output_rxs_m_32_16(oldstate);
1809
0
}
1810
1811
inline uint16_t pcg_oneseq_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
1812
                                                     uint16_t bound)
1813
0
{
1814
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1815
0
    for (;;) {
1816
0
        uint16_t r = pcg_oneseq_32_rxs_m_16_random_r(rng);
1817
0
        if (r >= threshold)
1818
0
            return r % bound;
1819
0
    }
1820
0
}
1821
1822
inline uint32_t pcg_oneseq_64_rxs_m_32_random_r(struct pcg_state_64* rng)
1823
0
{
1824
0
    uint64_t oldstate = rng->state;
1825
0
    pcg_oneseq_64_step_r(rng);
1826
0
    return pcg_output_rxs_m_64_32(oldstate);
1827
0
}
1828
1829
inline uint32_t pcg_oneseq_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
1830
                                                     uint32_t bound)
1831
0
{
1832
0
    uint32_t threshold = -bound % bound;
1833
0
    for (;;) {
1834
0
        uint32_t r = pcg_oneseq_64_rxs_m_32_random_r(rng);
1835
0
        if (r >= threshold)
1836
0
            return r % bound;
1837
0
    }
1838
0
}
1839
1840
#if PCG_HAS_128BIT_OPS
1841
inline uint64_t pcg_oneseq_128_rxs_m_64_random_r(struct pcg_state_128* rng)
1842
0
{
1843
0
    pcg_oneseq_128_step_r(rng);
1844
0
    return pcg_output_rxs_m_128_64(rng->state);
1845
0
}
1846
#endif
1847
1848
#if PCG_HAS_128BIT_OPS
1849
inline uint64_t pcg_oneseq_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
1850
                                                      uint64_t bound)
1851
0
{
1852
0
    uint64_t threshold = -bound % bound;
1853
0
    for (;;) {
1854
0
        uint64_t r = pcg_oneseq_128_rxs_m_64_random_r(rng);
1855
0
        if (r >= threshold)
1856
0
            return r % bound;
1857
0
    }
1858
0
}
1859
#endif
1860
1861
inline uint8_t pcg_unique_16_rxs_m_8_random_r(struct pcg_state_16* rng)
1862
0
{
1863
0
    uint16_t oldstate = rng->state;
1864
0
    pcg_unique_16_step_r(rng);
1865
0
    return pcg_output_rxs_m_16_8(oldstate);
1866
0
}
1867
1868
inline uint8_t pcg_unique_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
1869
                                                   uint8_t bound)
1870
0
{
1871
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
1872
0
    for (;;) {
1873
0
        uint8_t r = pcg_unique_16_rxs_m_8_random_r(rng);
1874
0
        if (r >= threshold)
1875
0
            return r % bound;
1876
0
    }
1877
0
}
1878
1879
inline uint16_t pcg_unique_32_rxs_m_16_random_r(struct pcg_state_32* rng)
1880
0
{
1881
0
    uint32_t oldstate = rng->state;
1882
0
    pcg_unique_32_step_r(rng);
1883
0
    return pcg_output_rxs_m_32_16(oldstate);
1884
0
}
1885
1886
inline uint16_t pcg_unique_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
1887
                                                     uint16_t bound)
1888
0
{
1889
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1890
0
    for (;;) {
1891
0
        uint16_t r = pcg_unique_32_rxs_m_16_random_r(rng);
1892
0
        if (r >= threshold)
1893
0
            return r % bound;
1894
0
    }
1895
0
}
1896
1897
inline uint32_t pcg_unique_64_rxs_m_32_random_r(struct pcg_state_64* rng)
1898
0
{
1899
0
    uint64_t oldstate = rng->state;
1900
0
    pcg_unique_64_step_r(rng);
1901
0
    return pcg_output_rxs_m_64_32(oldstate);
1902
0
}
1903
1904
inline uint32_t pcg_unique_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
1905
                                                     uint32_t bound)
1906
0
{
1907
0
    uint32_t threshold = -bound % bound;
1908
0
    for (;;) {
1909
0
        uint32_t r = pcg_unique_64_rxs_m_32_random_r(rng);
1910
0
        if (r >= threshold)
1911
0
            return r % bound;
1912
0
    }
1913
0
}
1914
1915
#if PCG_HAS_128BIT_OPS
1916
inline uint64_t pcg_unique_128_rxs_m_64_random_r(struct pcg_state_128* rng)
1917
0
{
1918
0
    pcg_unique_128_step_r(rng);
1919
0
    return pcg_output_rxs_m_128_64(rng->state);
1920
0
}
1921
#endif
1922
1923
#if PCG_HAS_128BIT_OPS
1924
inline uint64_t pcg_unique_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
1925
                                                      uint64_t bound)
1926
0
{
1927
0
    uint64_t threshold = -bound % bound;
1928
0
    for (;;) {
1929
0
        uint64_t r = pcg_unique_128_rxs_m_64_random_r(rng);
1930
0
        if (r >= threshold)
1931
0
            return r % bound;
1932
0
    }
1933
0
}
1934
#endif
1935
1936
inline uint8_t pcg_setseq_16_rxs_m_8_random_r(struct pcg_state_setseq_16* rng)
1937
0
{
1938
0
    uint16_t oldstate = rng->state;
1939
0
    pcg_setseq_16_step_r(rng);
1940
0
    return pcg_output_rxs_m_16_8(oldstate);
1941
0
}
1942
1943
inline uint8_t
1944
pcg_setseq_16_rxs_m_8_boundedrand_r(struct pcg_state_setseq_16* rng,
1945
                                    uint8_t bound)
1946
0
{
1947
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
1948
0
    for (;;) {
1949
0
        uint8_t r = pcg_setseq_16_rxs_m_8_random_r(rng);
1950
0
        if (r >= threshold)
1951
0
            return r % bound;
1952
0
    }
1953
0
}
1954
1955
inline uint16_t pcg_setseq_32_rxs_m_16_random_r(struct pcg_state_setseq_32* rng)
1956
0
{
1957
0
    uint32_t oldstate = rng->state;
1958
0
    pcg_setseq_32_step_r(rng);
1959
0
    return pcg_output_rxs_m_32_16(oldstate);
1960
0
}
1961
1962
inline uint16_t
1963
pcg_setseq_32_rxs_m_16_boundedrand_r(struct pcg_state_setseq_32* rng,
1964
                                     uint16_t bound)
1965
0
{
1966
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
1967
0
    for (;;) {
1968
0
        uint16_t r = pcg_setseq_32_rxs_m_16_random_r(rng);
1969
0
        if (r >= threshold)
1970
0
            return r % bound;
1971
0
    }
1972
0
}
1973
1974
inline uint32_t pcg_setseq_64_rxs_m_32_random_r(struct pcg_state_setseq_64* rng)
1975
0
{
1976
0
    uint64_t oldstate = rng->state;
1977
0
    pcg_setseq_64_step_r(rng);
1978
0
    return pcg_output_rxs_m_64_32(oldstate);
1979
0
}
1980
1981
inline uint32_t
1982
pcg_setseq_64_rxs_m_32_boundedrand_r(struct pcg_state_setseq_64* rng,
1983
                                     uint32_t bound)
1984
0
{
1985
0
    uint32_t threshold = -bound % bound;
1986
0
    for (;;) {
1987
0
        uint32_t r = pcg_setseq_64_rxs_m_32_random_r(rng);
1988
0
        if (r >= threshold)
1989
0
            return r % bound;
1990
0
    }
1991
0
}
1992
1993
#if PCG_HAS_128BIT_OPS
1994
inline uint64_t
1995
pcg_setseq_128_rxs_m_64_random_r(struct pcg_state_setseq_128* rng)
1996
0
{
1997
0
    pcg_setseq_128_step_r(rng);
1998
0
    return pcg_output_rxs_m_128_64(rng->state);
1999
0
}
2000
#endif
2001
2002
#if PCG_HAS_128BIT_OPS
2003
inline uint64_t
2004
pcg_setseq_128_rxs_m_64_boundedrand_r(struct pcg_state_setseq_128* rng,
2005
                                      uint64_t bound)
2006
0
{
2007
0
    uint64_t threshold = -bound % bound;
2008
0
    for (;;) {
2009
0
        uint64_t r = pcg_setseq_128_rxs_m_64_random_r(rng);
2010
0
        if (r >= threshold)
2011
0
            return r % bound;
2012
0
    }
2013
0
}
2014
#endif
2015
2016
inline uint8_t pcg_mcg_16_rxs_m_8_random_r(struct pcg_state_16* rng)
2017
0
{
2018
0
    uint16_t oldstate = rng->state;
2019
0
    pcg_mcg_16_step_r(rng);
2020
0
    return pcg_output_rxs_m_16_8(oldstate);
2021
0
}
2022
2023
inline uint8_t pcg_mcg_16_rxs_m_8_boundedrand_r(struct pcg_state_16* rng,
2024
                                                uint8_t bound)
2025
0
{
2026
0
    uint8_t threshold = ((uint8_t)(-bound)) % bound;
2027
0
    for (;;) {
2028
0
        uint8_t r = pcg_mcg_16_rxs_m_8_random_r(rng);
2029
0
        if (r >= threshold)
2030
0
            return r % bound;
2031
0
    }
2032
0
}
2033
2034
inline uint16_t pcg_mcg_32_rxs_m_16_random_r(struct pcg_state_32* rng)
2035
0
{
2036
0
    uint32_t oldstate = rng->state;
2037
0
    pcg_mcg_32_step_r(rng);
2038
0
    return pcg_output_rxs_m_32_16(oldstate);
2039
0
}
2040
2041
inline uint16_t pcg_mcg_32_rxs_m_16_boundedrand_r(struct pcg_state_32* rng,
2042
                                                  uint16_t bound)
2043
0
{
2044
0
    uint16_t threshold = ((uint16_t)(-bound)) % bound;
2045
0
    for (;;) {
2046
0
        uint16_t r = pcg_mcg_32_rxs_m_16_random_r(rng);
2047
0
        if (r >= threshold)
2048
0
            return r % bound;
2049
0
    }
2050
0
}
2051
2052
inline uint32_t pcg_mcg_64_rxs_m_32_random_r(struct pcg_state_64* rng)
2053
0
{
2054
0
    uint64_t oldstate = rng->state;
2055
0
    pcg_mcg_64_step_r(rng);
2056
0
    return pcg_output_rxs_m_64_32(oldstate);
2057
0
}
2058
2059
inline uint32_t pcg_mcg_64_rxs_m_32_boundedrand_r(struct pcg_state_64* rng,
2060
                                                  uint32_t bound)
2061
0
{
2062
0
    uint32_t threshold = -bound % bound;
2063
0
    for (;;) {
2064
0
        uint32_t r = pcg_mcg_64_rxs_m_32_random_r(rng);
2065
0
        if (r >= threshold)
2066
0
            return r % bound;
2067
0
    }
2068
0
}
2069
2070
#if PCG_HAS_128BIT_OPS
2071
inline uint64_t pcg_mcg_128_rxs_m_64_random_r(struct pcg_state_128* rng)
2072
0
{
2073
0
    pcg_mcg_128_step_r(rng);
2074
0
    return pcg_output_rxs_m_128_64(rng->state);
2075
0
}
2076
#endif
2077
2078
#if PCG_HAS_128BIT_OPS
2079
inline uint64_t pcg_mcg_128_rxs_m_64_boundedrand_r(struct pcg_state_128* rng,
2080
                                                   uint64_t bound)
2081
0
{
2082
0
    uint64_t threshold = -bound % bound;
2083
0
    for (;;) {
2084
0
        uint64_t r = pcg_mcg_128_rxs_m_64_random_r(rng);
2085
0
        if (r >= threshold)
2086
0
            return r % bound;
2087
0
    }
2088
0
}
2089
#endif
2090
2091
/* Generation functions for XSL RR (only defined for "large" types) */
2092
2093
inline uint32_t pcg_oneseq_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2094
0
{
2095
0
    uint64_t oldstate = rng->state;
2096
0
    pcg_oneseq_64_step_r(rng);
2097
0
    return pcg_output_xsl_rr_64_32(oldstate);
2098
0
}
2099
2100
inline uint32_t pcg_oneseq_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2101
                                                      uint32_t bound)
2102
0
{
2103
0
    uint32_t threshold = -bound % bound;
2104
0
    for (;;) {
2105
0
        uint32_t r = pcg_oneseq_64_xsl_rr_32_random_r(rng);
2106
0
        if (r >= threshold)
2107
0
            return r % bound;
2108
0
    }
2109
0
}
2110
2111
#if PCG_HAS_128BIT_OPS
2112
inline uint64_t pcg_oneseq_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2113
0
{
2114
0
    pcg_oneseq_128_step_r(rng);
2115
0
    return pcg_output_xsl_rr_128_64(rng->state);
2116
0
}
2117
#endif
2118
2119
#if PCG_HAS_128BIT_OPS
2120
inline uint64_t
2121
pcg_oneseq_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2122
                                       uint64_t bound)
2123
0
{
2124
0
    uint64_t threshold = -bound % bound;
2125
0
    for (;;) {
2126
0
        uint64_t r = pcg_oneseq_128_xsl_rr_64_random_r(rng);
2127
0
        if (r >= threshold)
2128
0
            return r % bound;
2129
0
    }
2130
0
}
2131
#endif
2132
2133
inline uint32_t pcg_unique_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2134
0
{
2135
0
    uint64_t oldstate = rng->state;
2136
0
    pcg_unique_64_step_r(rng);
2137
0
    return pcg_output_xsl_rr_64_32(oldstate);
2138
0
}
2139
2140
inline uint32_t pcg_unique_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2141
                                                      uint32_t bound)
2142
0
{
2143
0
    uint32_t threshold = -bound % bound;
2144
0
    for (;;) {
2145
0
        uint32_t r = pcg_unique_64_xsl_rr_32_random_r(rng);
2146
0
        if (r >= threshold)
2147
0
            return r % bound;
2148
0
    }
2149
0
}
2150
2151
#if PCG_HAS_128BIT_OPS
2152
inline uint64_t pcg_unique_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2153
0
{
2154
0
    pcg_unique_128_step_r(rng);
2155
0
    return pcg_output_xsl_rr_128_64(rng->state);
2156
0
}
2157
#endif
2158
2159
#if PCG_HAS_128BIT_OPS
2160
inline uint64_t
2161
pcg_unique_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2162
                                       uint64_t bound)
2163
0
{
2164
0
    uint64_t threshold = -bound % bound;
2165
0
    for (;;) {
2166
0
        uint64_t r = pcg_unique_128_xsl_rr_64_random_r(rng);
2167
0
        if (r >= threshold)
2168
0
            return r % bound;
2169
0
    }
2170
0
}
2171
#endif
2172
2173
inline uint32_t
2174
pcg_setseq_64_xsl_rr_32_random_r(struct pcg_state_setseq_64* rng)
2175
0
{
2176
0
    uint64_t oldstate = rng->state;
2177
0
    pcg_setseq_64_step_r(rng);
2178
0
    return pcg_output_xsl_rr_64_32(oldstate);
2179
0
}
2180
2181
inline uint32_t
2182
pcg_setseq_64_xsl_rr_32_boundedrand_r(struct pcg_state_setseq_64* rng,
2183
                                      uint32_t bound)
2184
0
{
2185
0
    uint32_t threshold = -bound % bound;
2186
0
    for (;;) {
2187
0
        uint32_t r = pcg_setseq_64_xsl_rr_32_random_r(rng);
2188
0
        if (r >= threshold)
2189
0
            return r % bound;
2190
0
    }
2191
0
}
2192
2193
#if PCG_HAS_128BIT_OPS
2194
inline uint64_t
2195
pcg_setseq_128_xsl_rr_64_random_r(struct pcg_state_setseq_128* rng)
2196
0
{
2197
0
    pcg_setseq_128_step_r(rng);
2198
0
    return pcg_output_xsl_rr_128_64(rng->state);
2199
0
}
2200
#endif
2201
2202
#if PCG_HAS_128BIT_OPS
2203
inline uint64_t
2204
pcg_setseq_128_xsl_rr_64_boundedrand_r(struct pcg_state_setseq_128* rng,
2205
                                       uint64_t bound)
2206
0
{
2207
0
    uint64_t threshold = -bound % bound;
2208
0
    for (;;) {
2209
0
        uint64_t r = pcg_setseq_128_xsl_rr_64_random_r(rng);
2210
0
        if (r >= threshold)
2211
0
            return r % bound;
2212
0
    }
2213
0
}
2214
#endif
2215
2216
inline uint32_t pcg_mcg_64_xsl_rr_32_random_r(struct pcg_state_64* rng)
2217
0
{
2218
0
    uint64_t oldstate = rng->state;
2219
0
    pcg_mcg_64_step_r(rng);
2220
0
    return pcg_output_xsl_rr_64_32(oldstate);
2221
0
}
2222
2223
inline uint32_t pcg_mcg_64_xsl_rr_32_boundedrand_r(struct pcg_state_64* rng,
2224
                                                   uint32_t bound)
2225
0
{
2226
0
    uint32_t threshold = -bound % bound;
2227
0
    for (;;) {
2228
0
        uint32_t r = pcg_mcg_64_xsl_rr_32_random_r(rng);
2229
0
        if (r >= threshold)
2230
0
            return r % bound;
2231
0
    }
2232
0
}
2233
2234
#if PCG_HAS_128BIT_OPS
2235
inline uint64_t pcg_mcg_128_xsl_rr_64_random_r(struct pcg_state_128* rng)
2236
0
{
2237
0
    pcg_mcg_128_step_r(rng);
2238
0
    return pcg_output_xsl_rr_128_64(rng->state);
2239
0
}
2240
#endif
2241
2242
#if PCG_HAS_128BIT_OPS
2243
inline uint64_t pcg_mcg_128_xsl_rr_64_boundedrand_r(struct pcg_state_128* rng,
2244
                                                    uint64_t bound)
2245
0
{
2246
0
    uint64_t threshold = -bound % bound;
2247
0
    for (;;) {
2248
0
        uint64_t r = pcg_mcg_128_xsl_rr_64_random_r(rng);
2249
0
        if (r >= threshold)
2250
0
            return r % bound;
2251
0
    }
2252
0
}
2253
#endif
2254
2255
/* Generation functions for XSL RR RR (only defined for "large" types) */
2256
2257
inline uint64_t pcg_oneseq_64_xsl_rr_rr_64_random_r(struct pcg_state_64* rng)
2258
0
{
2259
0
    uint64_t oldstate = rng->state;
2260
0
    pcg_oneseq_64_step_r(rng);
2261
0
    return pcg_output_xsl_rr_rr_64_64(oldstate);
2262
0
}
2263
2264
inline uint64_t
2265
pcg_oneseq_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_64* rng,
2266
                                         uint64_t bound)
2267
0
{
2268
0
    uint64_t threshold = -bound % bound;
2269
0
    for (;;) {
2270
0
        uint64_t r = pcg_oneseq_64_xsl_rr_rr_64_random_r(rng);
2271
0
        if (r >= threshold)
2272
0
            return r % bound;
2273
0
    }
2274
0
}
2275
2276
#if PCG_HAS_128BIT_OPS
2277
inline pcg128_t pcg_oneseq_128_xsl_rr_rr_128_random_r(struct pcg_state_128* rng)
2278
0
{
2279
0
    pcg_oneseq_128_step_r(rng);
2280
0
    return pcg_output_xsl_rr_rr_128_128(rng->state);
2281
0
}
2282
#endif
2283
2284
#if PCG_HAS_128BIT_OPS
2285
inline pcg128_t
2286
pcg_oneseq_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_128* rng,
2287
                                           pcg128_t bound)
2288
0
{
2289
0
    pcg128_t threshold = -bound % bound;
2290
0
    for (;;) {
2291
0
        pcg128_t r = pcg_oneseq_128_xsl_rr_rr_128_random_r(rng);
2292
0
        if (r >= threshold)
2293
0
            return r % bound;
2294
0
    }
2295
0
}
2296
#endif
2297
2298
inline uint64_t pcg_unique_64_xsl_rr_rr_64_random_r(struct pcg_state_64* rng)
2299
0
{
2300
0
    uint64_t oldstate = rng->state;
2301
0
    pcg_unique_64_step_r(rng);
2302
0
    return pcg_output_xsl_rr_rr_64_64(oldstate);
2303
0
}
2304
2305
inline uint64_t
2306
pcg_unique_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_64* rng,
2307
                                         uint64_t bound)
2308
0
{
2309
0
    uint64_t threshold = -bound % bound;
2310
0
    for (;;) {
2311
0
        uint64_t r = pcg_unique_64_xsl_rr_rr_64_random_r(rng);
2312
0
        if (r >= threshold)
2313
0
            return r % bound;
2314
0
    }
2315
0
}
2316
2317
#if PCG_HAS_128BIT_OPS
2318
inline pcg128_t pcg_unique_128_xsl_rr_rr_128_random_r(struct pcg_state_128* rng)
2319
0
{
2320
0
    pcg_unique_128_step_r(rng);
2321
0
    return pcg_output_xsl_rr_rr_128_128(rng->state);
2322
0
}
2323
#endif
2324
2325
#if PCG_HAS_128BIT_OPS
2326
inline pcg128_t
2327
pcg_unique_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_128* rng,
2328
                                           pcg128_t bound)
2329
0
{
2330
0
    pcg128_t threshold = -bound % bound;
2331
0
    for (;;) {
2332
0
        pcg128_t r = pcg_unique_128_xsl_rr_rr_128_random_r(rng);
2333
0
        if (r >= threshold)
2334
0
            return r % bound;
2335
0
    }
2336
0
}
2337
#endif
2338
2339
inline uint64_t
2340
pcg_setseq_64_xsl_rr_rr_64_random_r(struct pcg_state_setseq_64* rng)
2341
0
{
2342
0
    uint64_t oldstate = rng->state;
2343
0
    pcg_setseq_64_step_r(rng);
2344
0
    return pcg_output_xsl_rr_rr_64_64(oldstate);
2345
0
}
2346
2347
inline uint64_t
2348
pcg_setseq_64_xsl_rr_rr_64_boundedrand_r(struct pcg_state_setseq_64* rng,
2349
                                         uint64_t bound)
2350
0
{
2351
0
    uint64_t threshold = -bound % bound;
2352
0
    for (;;) {
2353
0
        uint64_t r = pcg_setseq_64_xsl_rr_rr_64_random_r(rng);
2354
0
        if (r >= threshold)
2355
0
            return r % bound;
2356
0
    }
2357
0
}
2358
2359
#if PCG_HAS_128BIT_OPS
2360
inline pcg128_t
2361
pcg_setseq_128_xsl_rr_rr_128_random_r(struct pcg_state_setseq_128* rng)
2362
0
{
2363
0
    pcg_setseq_128_step_r(rng);
2364
0
    return pcg_output_xsl_rr_rr_128_128(rng->state);
2365
0
}
2366
#endif
2367
2368
#if PCG_HAS_128BIT_OPS
2369
inline pcg128_t
2370
pcg_setseq_128_xsl_rr_rr_128_boundedrand_r(struct pcg_state_setseq_128* rng,
2371
                                           pcg128_t bound)
2372
0
{
2373
0
    pcg128_t threshold = -bound % bound;
2374
0
    for (;;) {
2375
0
        pcg128_t r = pcg_setseq_128_xsl_rr_rr_128_random_r(rng);
2376
0
        if (r >= threshold)
2377
0
            return r % bound;
2378
0
    }
2379
0
}
2380
#endif
2381
2382
/*** Typedefs */
2383
typedef struct pcg_state_setseq_64      pcg32_random_t;
2384
typedef struct pcg_state_64             pcg32s_random_t;
2385
typedef struct pcg_state_64             pcg32u_random_t;
2386
typedef struct pcg_state_64             pcg32f_random_t;
2387
/*** random_r */
2388
0
#define pcg32_random_r                  pcg_setseq_64_xsh_rr_32_random_r
2389
#define pcg32s_random_r                 pcg_oneseq_64_xsh_rr_32_random_r
2390
#define pcg32u_random_r                 pcg_unique_64_xsh_rr_32_random_r
2391
#define pcg32f_random_r                 pcg_mcg_64_xsh_rs_32_random_r
2392
/*** boundedrand_r */
2393
#define pcg32_boundedrand_r             pcg_setseq_64_xsh_rr_32_boundedrand_r
2394
#define pcg32s_boundedrand_r            pcg_oneseq_64_xsh_rr_32_boundedrand_r
2395
#define pcg32u_boundedrand_r            pcg_unique_64_xsh_rr_32_boundedrand_r
2396
#define pcg32f_boundedrand_r            pcg_mcg_64_xsh_rs_32_boundedrand_r
2397
/*** srandom_r */
2398
0
#define pcg32_srandom_r                 pcg_setseq_64_srandom_r
2399
#define pcg32s_srandom_r                pcg_oneseq_64_srandom_r
2400
#define pcg32u_srandom_r                pcg_unique_64_srandom_r
2401
#define pcg32f_srandom_r                pcg_mcg_64_srandom_r
2402
/*** advance_r */
2403
#define pcg32_advance_r                 pcg_setseq_64_advance_r
2404
#define pcg32s_advance_r                pcg_oneseq_64_advance_r
2405
#define pcg32u_advance_r                pcg_unique_64_advance_r
2406
#define pcg32f_advance_r                pcg_mcg_64_advance_r
2407
2408
#if PCG_HAS_128BIT_OPS
2409
/*** Typedefs */
2410
typedef struct pcg_state_setseq_128     pcg64_random_t;
2411
typedef struct pcg_state_128            pcg64s_random_t;
2412
typedef struct pcg_state_128            pcg64u_random_t;
2413
typedef struct pcg_state_128            pcg64f_random_t;
2414
/*** random_r */
2415
#define pcg64_random_r                  pcg_setseq_128_xsl_rr_64_random_r
2416
#define pcg64s_random_r                 pcg_oneseq_128_xsl_rr_64_random_r
2417
#define pcg64u_random_r                 pcg_unique_128_xsl_rr_64_random_r
2418
#define pcg64f_random_r                 pcg_mcg_128_xsl_rr_64_random_r
2419
/*** boundedrand_r */
2420
#define pcg64_boundedrand_r             pcg_setseq_128_xsl_rr_64_boundedrand_r
2421
#define pcg64s_boundedrand_r            pcg_oneseq_128_xsl_rr_64_boundedrand_r
2422
#define pcg64u_boundedrand_r            pcg_unique_128_xsl_rr_64_boundedrand_r
2423
#define pcg64f_boundedrand_r            pcg_mcg_128_xsl_rr_64_boundedrand_r
2424
/*** srandom_r */
2425
#define pcg64_srandom_r                 pcg_setseq_128_srandom_r
2426
#define pcg64s_srandom_r                pcg_oneseq_128_srandom_r
2427
#define pcg64u_srandom_r                pcg_unique_128_srandom_r
2428
#define pcg64f_srandom_r                pcg_mcg_128_srandom_r
2429
/*** advance_r */
2430
#define pcg64_advance_r                 pcg_setseq_128_advance_r
2431
#define pcg64s_advance_r                pcg_oneseq_128_advance_r
2432
#define pcg64u_advance_r                pcg_unique_128_advance_r
2433
#define pcg64f_advance_r                pcg_mcg_128_advance_r
2434
#endif
2435
2436
/*** Typedefs */
2437
typedef struct pcg_state_8              pcg8si_random_t;
2438
typedef struct pcg_state_16             pcg16si_random_t;
2439
typedef struct pcg_state_32             pcg32si_random_t;
2440
typedef struct pcg_state_64             pcg64si_random_t;
2441
/*** random_r */
2442
#define pcg8si_random_r                 pcg_oneseq_8_rxs_m_xs_8_random_r
2443
#define pcg16si_random_r                pcg_oneseq_16_rxs_m_xs_16_random_r
2444
#define pcg32si_random_r                pcg_oneseq_32_rxs_m_xs_32_random_r
2445
#define pcg64si_random_r                pcg_oneseq_64_rxs_m_xs_64_random_r
2446
/*** boundedrand_r */
2447
#define pcg8si_boundedrand_r            pcg_oneseq_8_rxs_m_xs_8_boundedrand_r
2448
#define pcg16si_boundedrand_r           pcg_oneseq_16_rxs_m_xs_16_boundedrand_r
2449
#define pcg32si_boundedrand_r           pcg_oneseq_32_rxs_m_xs_32_boundedrand_r
2450
#define pcg64si_boundedrand_r           pcg_oneseq_64_rxs_m_xs_64_boundedrand_r
2451
/*** srandom_r */
2452
#define pcg8si_srandom_r                pcg_oneseq_8_srandom_r
2453
#define pcg16si_srandom_r               pcg_oneseq_16_srandom_r
2454
#define pcg32si_srandom_r               pcg_oneseq_32_srandom_r
2455
#define pcg64si_srandom_r               pcg_oneseq_64_srandom_r
2456
/*** advance_r */
2457
#define pcg8si_advance_r                pcg_oneseq_8_advance_r
2458
#define pcg16si_advance_r               pcg_oneseq_16_advance_r
2459
#define pcg32si_advance_r               pcg_oneseq_32_advance_r
2460
#define pcg64si_advance_r               pcg_oneseq_64_advance_r
2461
2462
#if PCG_HAS_128BIT_OPS
2463
typedef struct pcg_state_128        pcg128si_random_t;
2464
#define pcg128si_random_r           pcg_oneseq_128_rxs_m_xs_128_random_r
2465
#define pcg128si_boundedrand_r      pcg_oneseq_128_rxs_m_xs_128_boundedrand_r
2466
#define pcg128si_srandom_r          pcg_oneseq_128_srandom_r
2467
#define pcg128si_advance_r          pcg_oneseq_128_advance_r
2468
#endif
2469
2470
/*** Typedefs */
2471
typedef struct pcg_state_setseq_8       pcg8i_random_t;
2472
typedef struct pcg_state_setseq_16      pcg16i_random_t;
2473
typedef struct pcg_state_setseq_32      pcg32i_random_t;
2474
typedef struct pcg_state_setseq_64      pcg64i_random_t;
2475
/*** random_r */
2476
#define pcg8i_random_r                  pcg_setseq_8_rxs_m_xs_8_random_r
2477
#define pcg16i_random_r                 pcg_setseq_16_rxs_m_xs_16_random_r
2478
#define pcg32i_random_r                 pcg_setseq_32_rxs_m_xs_32_random_r
2479
#define pcg64i_random_r                 pcg_setseq_64_rxs_m_xs_64_random_r
2480
/*** boundedrand_r */
2481
#define pcg8i_boundedrand_r             pcg_setseq_8_rxs_m_xs_8_boundedrand_r
2482
#define pcg16i_boundedrand_r            pcg_setseq_16_rxs_m_xs_16_boundedrand_r
2483
#define pcg32i_boundedrand_r            pcg_setseq_32_rxs_m_xs_32_boundedrand_r
2484
#define pcg64i_boundedrand_r            pcg_setseq_64_rxs_m_xs_64_boundedrand_r
2485
/*** srandom_r */
2486
#define pcg8i_srandom_r                 pcg_setseq_8_srandom_r
2487
#define pcg16i_srandom_r                pcg_setseq_16_srandom_r
2488
#define pcg32i_srandom_r                pcg_setseq_32_srandom_r
2489
#define pcg64i_srandom_r                pcg_setseq_64_srandom_r
2490
/*** advance_r */
2491
#define pcg8i_advance_r                 pcg_setseq_8_advance_r
2492
#define pcg16i_advance_r                pcg_setseq_16_advance_r
2493
#define pcg32i_advance_r                pcg_setseq_32_advance_r
2494
#define pcg64i_advance_r                pcg_setseq_64_advance_r
2495
2496
#if PCG_HAS_128BIT_OPS
2497
typedef struct pcg_state_setseq_128   pcg128i_random_t;
2498
#define pcg128i_random_r              pcg_setseq_128_rxs_m_xs_128_random_r
2499
#define pcg128i_boundedrand_r         pcg_setseq_128_rxs_m_xs_128_boundedrand_r
2500
#define pcg128i_srandom_r             pcg_setseq_128_srandom_r
2501
#define pcg128i_advance_r             pcg_setseq_128_advance_r
2502
#endif
2503
2504
extern uint32_t pcg32_random(void);
2505
extern uint32_t pcg32_boundedrand(uint32_t bound);
2506
extern void     pcg32_srandom(uint64_t seed, uint64_t seq);
2507
extern void     pcg32_advance(uint64_t delta);
2508
2509
#if PCG_HAS_128BIT_OPS
2510
extern uint64_t pcg64_random(void);
2511
extern uint64_t pcg64_boundedrand(uint64_t bound);
2512
extern void     pcg64_srandom(pcg128_t seed, pcg128_t seq);
2513
extern void     pcg64_advance(pcg128_t delta);
2514
#endif
2515
2516
/*
2517
 * Static initialization constants (if you can't call srandom for some
2518
 * bizarre reason).
2519
 */
2520
2521
#define PCG32_INITIALIZER       PCG_STATE_SETSEQ_64_INITIALIZER
2522
#define PCG32U_INITIALIZER      PCG_STATE_UNIQUE_64_INITIALIZER
2523
#define PCG32S_INITIALIZER      PCG_STATE_ONESEQ_64_INITIALIZER
2524
#define PCG32F_INITIALIZER      PCG_STATE_MCG_64_INITIALIZER
2525
2526
#if PCG_HAS_128BIT_OPS
2527
#define PCG64_INITIALIZER       PCG_STATE_SETSEQ_128_INITIALIZER
2528
#define PCG64U_INITIALIZER      PCG_STATE_UNIQUE_128_INITIALIZER
2529
#define PCG64S_INITIALIZER      PCG_STATE_ONESEQ_128_INITIALIZER
2530
#define PCG64F_INITIALIZER      PCG_STATE_MCG_128_INITIALIZER
2531
#endif
2532
2533
#define PCG8SI_INITIALIZER      PCG_STATE_ONESEQ_8_INITIALIZER
2534
#define PCG16SI_INITIALIZER     PCG_STATE_ONESEQ_16_INITIALIZER
2535
#define PCG32SI_INITIALIZER     PCG_STATE_ONESEQ_32_INITIALIZER
2536
#define PCG64SI_INITIALIZER     PCG_STATE_ONESEQ_64_INITIALIZER
2537
#if PCG_HAS_128BIT_OPS
2538
#define PCG128SI_INITIALIZER    PCG_STATE_ONESEQ_128_INITIALIZER
2539
#endif
2540
2541
#define PCG8I_INITIALIZER       PCG_STATE_SETSEQ_8_INITIALIZER
2542
#define PCG16I_INITIALIZER      PCG_STATE_SETSEQ_16_INITIALIZER
2543
#define PCG32I_INITIALIZER      PCG_STATE_SETSEQ_32_INITIALIZER
2544
#define PCG64I_INITIALIZER      PCG_STATE_SETSEQ_64_INITIALIZER
2545
#if PCG_HAS_128BIT_OPS
2546
#define PCG128I_INITIALIZER     PCG_STATE_SETSEQ_128_INITIALIZER
2547
#endif
2548
2549
#if __cplusplus
2550
}
2551
#endif
2552
2553
#ifdef _MSC_VER
2554
#pragma warning(pop)
2555
#endif
2556
2557
#endif /* PCG_VARIANTS_H_INCLUDED */