2010-02-11 02:37:56 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_TYPES
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple linear congruential pseudo-random number generator:
|
|
|
|
*
|
2012-03-03 07:59:45 +08:00
|
|
|
* prng(y) = (a*x + c) % m
|
2010-02-11 02:37:56 +08:00
|
|
|
*
|
|
|
|
* where the following constants ensure maximal period:
|
|
|
|
*
|
|
|
|
* a == Odd number (relatively prime to 2^n), and (a-1) is a multiple of 4.
|
|
|
|
* c == Odd number (relatively prime to 2^n).
|
|
|
|
* m == 2^32
|
|
|
|
*
|
|
|
|
* See Knuth's TAOCP 3rd Ed., Vol. 2, pg. 17 for details on these constraints.
|
|
|
|
*
|
|
|
|
* This choice of m has the disadvantage that the quality of the bits is
|
2014-09-05 13:27:26 +08:00
|
|
|
* proportional to bit position. For example, the lowest bit has a cycle of 2,
|
2010-02-11 02:37:56 +08:00
|
|
|
* the next has a cycle of 4, etc. For this reason, we prefer to use the upper
|
|
|
|
* bits.
|
|
|
|
*
|
|
|
|
* Macro parameters:
|
|
|
|
* uint32_t r : Result.
|
|
|
|
* unsigned lg_range : (0..32], number of least significant bits to return.
|
|
|
|
* uint32_t state : Seed value.
|
|
|
|
* const uint32_t a, c : See above discussion.
|
|
|
|
*/
|
2013-12-09 14:28:27 +08:00
|
|
|
#define prng32(r, lg_range, state, a, c) do { \
|
2015-05-05 00:58:36 +08:00
|
|
|
assert((lg_range) > 0); \
|
|
|
|
assert((lg_range) <= 32); \
|
2010-02-11 02:37:56 +08:00
|
|
|
\
|
|
|
|
r = (state * (a)) + (c); \
|
|
|
|
state = r; \
|
2015-05-05 00:58:36 +08:00
|
|
|
r >>= (32 - (lg_range)); \
|
2010-02-11 02:37:56 +08:00
|
|
|
} while (false)
|
|
|
|
|
2012-03-03 07:59:45 +08:00
|
|
|
/* Same as prng32(), but 64 bits of pseudo-randomness, using uint64_t. */
|
2013-12-09 14:28:27 +08:00
|
|
|
#define prng64(r, lg_range, state, a, c) do { \
|
2015-05-05 00:58:36 +08:00
|
|
|
assert((lg_range) > 0); \
|
|
|
|
assert((lg_range) <= 64); \
|
2010-03-02 12:15:26 +08:00
|
|
|
\
|
|
|
|
r = (state * (a)) + (c); \
|
|
|
|
state = r; \
|
2015-05-05 00:58:36 +08:00
|
|
|
r >>= (64 - (lg_range)); \
|
2010-03-02 12:15:26 +08:00
|
|
|
} while (false)
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
#endif /* JEMALLOC_H_TYPES */
|
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_STRUCTS
|
|
|
|
|
|
|
|
#endif /* JEMALLOC_H_STRUCTS */
|
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_EXTERNS
|
|
|
|
|
|
|
|
#endif /* JEMALLOC_H_EXTERNS */
|
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_INLINES
|
|
|
|
|
|
|
|
#endif /* JEMALLOC_H_INLINES */
|
|
|
|
/******************************************************************************/
|