2 * *****************************************************************************
4 * Parts of this code are adapted from the following:
6 * PCG, A Family of Better Random Number Generators.
8 * You can find the original source code at:
9 * https://github.com/imneme/pcg-c
11 * -----------------------------------------------------------------------------
13 * This code is under the following license:
15 * Copyright (c) 2014-2017 Melissa O'Neill and PCG Project contributors
16 * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this software and associated documentation files (the "Software"), to deal
20 * in the Software without restriction, including without limitation the rights
21 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22 * copies of the Software, and to permit persons to whom the Software is
23 * furnished to do so, subject to the following conditions:
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * *****************************************************************************
38 * Definitions for the RNG.
51 #if BC_ENABLE_EXTRA_MATH
54 #define BC_RAND_USE_FREE (1)
55 #else // BC_ENABLE_LIBRARY
57 #define BC_RAND_USE_FREE (1)
59 #define BC_RAND_USE_FREE (0)
61 #endif // BC_ENABLE_LIBRARY
64 * A function to return a random unsigned long.
65 * @param ptr A void ptr to some data that will help generate the random ulong.
66 * @return The random ulong.
68 typedef ulong (*BcRandUlong)(void* ptr);
72 // If longs are 64 bits, we have the option of 128-bit integers on some
73 // compilers. These two sections test that.
74 #ifdef BC_RAND_BUILTIN
76 #ifndef __SIZEOF_INT128__
77 #undef BC_RAND_BUILTIN
78 #define BC_RAND_BUILTIN (0)
79 #endif // __SIZEOF_INT128__
80 #endif // BC_RAND_BUILTIN
81 #endif // BC_RAND_BUILTIN
83 #ifndef BC_RAND_BUILTIN
84 #ifdef __SIZEOF_INT128__
85 #define BC_RAND_BUILTIN (1)
86 #else // __SIZEOF_INT128__
87 #define BC_RAND_BUILTIN (0)
88 #endif // __SIZEOF_INT128__
89 #endif // BC_RAND_BUILTIN
91 /// The type for random integers.
92 typedef uint64_t BcRand;
94 /// A constant defined by PCG.
95 #define BC_RAND_ROTC (63)
99 /// A typedef for the PCG state.
100 typedef __uint128_t BcRandState;
103 * Multiply two integers, worrying about overflow.
104 * @param a The first integer.
105 * @param b The second integer.
106 * @return The product of the PCG states.
108 #define bc_rand_mul(a, b) (((BcRandState) (a)) * ((BcRandState) (b)))
111 * Add two integers, worrying about overflow.
112 * @param a The first integer.
113 * @param b The second integer.
114 * @return The sum of the PCG states.
116 #define bc_rand_add(a, b) (((BcRandState) (a)) + ((BcRandState) (b)))
119 * Multiply two PCG states.
120 * @param a The first PCG state.
121 * @param b The second PCG state.
122 * @return The product of the PCG states.
124 #define bc_rand_mul2(a, b) (((BcRandState) (a)) * ((BcRandState) (b)))
127 * Add two PCG states.
128 * @param a The first PCG state.
129 * @param b The second PCG state.
130 * @return The sum of the PCG states.
132 #define bc_rand_add2(a, b) (((BcRandState) (a)) + ((BcRandState) (b)))
135 * Figure out if the PRNG has been modified. Since the increment of the PRNG has
136 * to be odd, we use the extra bit to store whether it has been modified or not.
138 * @return True if the PRNG has *not* been modified, false otherwise.
140 #define BC_RAND_NOTMODIFIED(r) (((r)->inc & 1UL) == 0)
143 * Return true if the PRNG has not been seeded yet.
145 * @return True if the PRNG has not been seeded yet, false otherwise.
147 #define BC_RAND_ZERO(r) (!(r)->state)
150 * Returns a constant built from @a h and @a l.
151 * @param h The high 64 bits.
152 * @param l The low 64 bits.
153 * @return The constant built from @a h and @a l.
155 #define BC_RAND_CONSTANT(h, l) ((((BcRandState) (h)) << 64) + (BcRandState) (l))
158 * Truncates a PCG state to the number of bits in a random integer.
159 * @param s The state to truncate.
160 * @return The truncated state.
162 #define BC_RAND_TRUNC(s) ((uint64_t) (s))
165 * Chops a PCG state in half and returns the top bits.
166 * @param s The state to chop.
167 * @return The chopped state's top bits.
169 #define BC_RAND_CHOP(s) ((uint64_t) ((s) >> 64UL))
172 * Rotates a PCG state.
173 * @param s The state to rotate.
174 * @return The rotated state.
176 #define BC_RAND_ROTAMT(s) ((unsigned int) ((s) >> 122UL))
178 #else // BC_RAND_BUILTIN
180 /// A typedef for the PCG state.
181 typedef struct BcRandState
192 * Multiply two integers, worrying about overflow.
193 * @param a The first integer.
194 * @param b The second integer.
195 * @return The product of the PCG states.
197 #define bc_rand_mul(a, b) (bc_rand_multiply((a), (b)))
200 * Add two integers, worrying about overflow.
201 * @param a The first integer.
202 * @param b The second integer.
203 * @return The sum of the PCG states.
205 #define bc_rand_add(a, b) (bc_rand_addition((a), (b)))
208 * Multiply two PCG states.
209 * @param a The first PCG state.
210 * @param b The second PCG state.
211 * @return The product of the PCG states.
213 #define bc_rand_mul2(a, b) (bc_rand_multiply2((a), (b)))
216 * Add two PCG states.
217 * @param a The first PCG state.
218 * @param b The second PCG state.
219 * @return The sum of the PCG states.
221 #define bc_rand_add2(a, b) (bc_rand_addition2((a), (b)))
224 * Figure out if the PRNG has been modified. Since the increment of the PRNG has
225 * to be odd, we use the extra bit to store whether it has been modified or not.
227 * @return True if the PRNG has *not* been modified, false otherwise.
229 #define BC_RAND_NOTMODIFIED(r) (((r)->inc.lo & 1) == 0)
232 * Return true if the PRNG has not been seeded yet.
234 * @return True if the PRNG has not been seeded yet, false otherwise.
236 #define BC_RAND_ZERO(r) (!(r)->state.lo && !(r)->state.hi)
239 * Returns a constant built from @a h and @a l.
240 * @param h The high 64 bits.
241 * @param l The low 64 bits.
242 * @return The constant built from @a h and @a l.
244 #define BC_RAND_CONSTANT(h, l) \
246 .lo = (l), .hi = (h) \
250 * Truncates a PCG state to the number of bits in a random integer.
251 * @param s The state to truncate.
252 * @return The truncated state.
254 #define BC_RAND_TRUNC(s) ((s).lo)
257 * Chops a PCG state in half and returns the top bits.
258 * @param s The state to chop.
259 * @return The chopped state's top bits.
261 #define BC_RAND_CHOP(s) ((s).hi)
264 * Returns the rotate amount for a PCG state.
265 * @param s The state to rotate.
266 * @return The semi-rotated state.
268 #define BC_RAND_ROTAMT(s) ((unsigned int) ((s).hi >> 58UL))
270 /// A 64-bit integer with the bottom 32 bits set.
271 #define BC_RAND_BOTTOM32 (((uint_fast64_t) 0xffffffffULL))
274 * Returns the 32-bit truncated value of @a n.
275 * @param n The integer to truncate.
276 * @return The bottom 32 bits of @a n.
278 #define BC_RAND_TRUNC32(n) ((n) & (BC_RAND_BOTTOM32))
281 * Returns the second 32 bits of @a n.
282 * @param n The integer to truncate.
283 * @return The second 32 bits of @a n.
285 #define BC_RAND_CHOP32(n) ((n) >> 32)
287 #endif // BC_RAND_BUILTIN
289 /// A constant defined by PCG.
290 #define BC_RAND_MULTIPLIER \
291 BC_RAND_CONSTANT(2549297995355413924ULL, 4865540595714422341ULL)
294 * Returns the result of a PCG fold.
295 * @param s The state to fold.
296 * @return The folded state.
298 #define BC_RAND_FOLD(s) ((BcRand) (BC_RAND_CHOP(s) ^ BC_RAND_TRUNC(s)))
300 #else // BC_LONG_BIT >= 64
302 // If we are using 32-bit longs, we need to set these so.
303 #undef BC_RAND_BUILTIN
304 #define BC_RAND_BUILTIN (1)
306 /// The type for random integers.
307 typedef uint32_t BcRand;
309 /// A constant defined by PCG.
310 #define BC_RAND_ROTC (31)
312 /// A typedef for the PCG state.
313 typedef uint_fast64_t BcRandState;
316 * Multiply two integers, worrying about overflow.
317 * @param a The first integer.
318 * @param b The second integer.
319 * @return The product of the PCG states.
321 #define bc_rand_mul(a, b) (((BcRandState) (a)) * ((BcRandState) (b)))
324 * Add two integers, worrying about overflow.
325 * @param a The first integer.
326 * @param b The second integer.
327 * @return The sum of the PCG states.
329 #define bc_rand_add(a, b) (((BcRandState) (a)) + ((BcRandState) (b)))
332 * Multiply two PCG states.
333 * @param a The first PCG state.
334 * @param b The second PCG state.
335 * @return The product of the PCG states.
337 #define bc_rand_mul2(a, b) (((BcRandState) (a)) * ((BcRandState) (b)))
340 * Add two PCG states.
341 * @param a The first PCG state.
342 * @param b The second PCG state.
343 * @return The sum of the PCG states.
345 #define bc_rand_add2(a, b) (((BcRandState) (a)) + ((BcRandState) (b)))
348 * Figure out if the PRNG has been modified. Since the increment of the PRNG has
349 * to be odd, we use the extra bit to store whether it has been modified or not.
351 * @return True if the PRNG has *not* been modified, false otherwise.
353 #define BC_RAND_NOTMODIFIED(r) (((r)->inc & 1UL) == 0)
356 * Return true if the PRNG has not been seeded yet.
358 * @return True if the PRNG has not been seeded yet, false otherwise.
360 #define BC_RAND_ZERO(r) (!(r)->state)
363 * Returns a constant built from a number.
364 * @param n The number.
365 * @return The constant built from @a n.
367 #define BC_RAND_CONSTANT(n) UINT64_C(n)
369 /// A constant defined by PCG.
370 #define BC_RAND_MULTIPLIER BC_RAND_CONSTANT(6364136223846793005)
373 * Truncates a PCG state to the number of bits in a random integer.
374 * @param s The state to truncate.
375 * @return The truncated state.
377 #define BC_RAND_TRUNC(s) ((uint32_t) (s))
380 * Chops a PCG state in half and returns the top bits.
381 * @param s The state to chop.
382 * @return The chopped state's top bits.
384 #define BC_RAND_CHOP(s) ((uint32_t) ((s) >> 32UL))
387 * Returns the rotate amount for a PCG state.
388 * @param s The state to rotate.
389 * @return The semi-rotated state.
391 #define BC_RAND_ROTAMT(s) ((unsigned int) ((s) >> 59UL))
394 * Returns the result of a PCG fold.
395 * @param s The state to fold.
396 * @return The folded state.
398 #define BC_RAND_FOLD(s) ((BcRand) ((((s) >> 18U) ^ (s)) >> 27U))
400 #endif // BC_LONG_BIT >= 64
403 * Rotates @a v by @a r bits.
404 * @param v The value to rotate.
405 * @param r The amount to rotate by.
406 * @return The rotated value.
408 #define BC_RAND_ROT(v, r) \
409 ((BcRand) (((v) >> (r)) | ((v) << ((0 - (r)) & BC_RAND_ROTC))))
411 /// The number of bits in a random integer.
412 #define BC_RAND_BITS (sizeof(BcRand) * CHAR_BIT)
414 /// The number of bits in a PCG state.
415 #define BC_RAND_STATE_BITS (sizeof(BcRandState) * CHAR_BIT)
417 /// The size of a BcNum with the max random integer. This isn't exact; it's
418 /// actually rather crude. But it's always enough.
419 #define BC_RAND_NUM_SIZE (BC_NUM_BIGDIG_LOG10 * 2 + 2)
421 /// The mask for how many bits bc_rand_srand() can set per iteration.
422 #define BC_RAND_SRAND_BITS ((1 << CHAR_BIT) - 1)
424 /// The actual RNG data. These are the actual PRNG's.
425 typedef struct BcRNGData
430 /// The increment and the modified bit.
435 /// The public PRNG. This is just a stack of PRNG's to maintain the globals
439 /// The stack of PRNG's.
445 * Initializes a BcRNG.
446 * @param r The BcRNG to initialize.
449 bc_rand_init(BcRNG* r);
454 * Frees a BcRNG. This is only in debug builds because it would only be freed on
456 * @param r The BcRNG to free.
459 bc_rand_free(BcRNG* r);
461 #endif // BC_RAND_USE_FREE
464 * Returns a random integer from the PRNG.
466 * @return A random integer.
469 bc_rand_int(BcRNG* r);
472 * Returns a random integer from the PRNG bounded by @a bound. Bias is
475 * @param bound The bound for the random integer.
476 * @return A bounded random integer.
479 bc_rand_bounded(BcRNG* r, BcRand bound);
482 * Seed the PRNG with the state in two parts and the increment in two parts.
484 * @param state1 The first part of the state.
485 * @param state2 The second part of the state.
486 * @param inc1 The first part of the increment.
487 * @param inc2 The second part of the increment.
490 bc_rand_seed(BcRNG* r, ulong state1, ulong state2, ulong inc1, ulong inc2);
493 * Pushes a new PRNG onto the PRNG stack.
497 bc_rand_push(BcRNG* r);
500 * Pops one or all but one items off of the PRNG stack.
502 * @param reset True if all but one PRNG should be popped off the stack, false
503 * if only one should be popped.
506 bc_rand_pop(BcRNG* r, bool reset);
509 * Returns, via pointers, the state of the PRNG in pieces.
511 * @param s1 The return value for the first part of the state.
512 * @param s2 The return value for the second part of the state.
513 * @param i1 The return value for the first part of the increment.
514 * @param i2 The return value for the second part of the increment.
517 bc_rand_getRands(BcRNG* r, BcRand* s1, BcRand* s2, BcRand* i1, BcRand* i2);
520 * Seed the PRNG with random data.
521 * @param rng The PRNG.
524 bc_rand_srand(BcRNGData* rng);
526 /// A reference to a constant multiplier.
527 extern const BcRandState bc_rand_multiplier;
529 #endif // BC_ENABLE_EXTRA_MATH