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