2017-01-11 10:06:31 +08:00
|
|
|
#ifndef JEMALLOC_INTERNAL_PRNG_INLINES_H
|
|
|
|
#define JEMALLOC_INTERNAL_PRNG_INLINES_H
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2016-02-10 08:28:40 +08:00
|
|
|
#ifndef JEMALLOC_ENABLE_INLINE
|
2016-11-08 02:52:44 +08:00
|
|
|
uint32_t prng_state_next_u32(uint32_t state);
|
|
|
|
uint64_t prng_state_next_u64(uint64_t state);
|
|
|
|
size_t prng_state_next_zu(size_t state);
|
|
|
|
|
|
|
|
uint32_t prng_lg_range_u32(uint32_t *state, unsigned lg_range,
|
|
|
|
bool atomic);
|
|
|
|
uint64_t prng_lg_range_u64(uint64_t *state, unsigned lg_range);
|
|
|
|
size_t prng_lg_range_zu(size_t *state, unsigned lg_range, bool atomic);
|
|
|
|
|
|
|
|
uint32_t prng_range_u32(uint32_t *state, uint32_t range, bool atomic);
|
|
|
|
uint64_t prng_range_u64(uint64_t *state, uint64_t range);
|
|
|
|
size_t prng_range_zu(size_t *state, size_t range, bool atomic);
|
2016-02-10 08:28:40 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_PRNG_C_))
|
2016-11-08 02:52:44 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE uint32_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prng_state_next_u32(uint32_t state) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return (state * PRNG_A_32) + PRNG_C_32;
|
2016-11-08 02:52:44 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 08:28:40 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE uint64_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prng_state_next_u64(uint64_t state) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return (state * PRNG_A_64) + PRNG_C_64;
|
2016-11-08 02:52:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prng_state_next_zu(size_t state) {
|
2016-11-08 02:52:44 +08:00
|
|
|
#if LG_SIZEOF_PTR == 2
|
2017-01-20 10:15:45 +08:00
|
|
|
return (state * PRNG_A_32) + PRNG_C_32;
|
2016-11-08 02:52:44 +08:00
|
|
|
#elif LG_SIZEOF_PTR == 3
|
2017-01-20 10:15:45 +08:00
|
|
|
return (state * PRNG_A_64) + PRNG_C_64;
|
2016-11-08 02:52:44 +08:00
|
|
|
#else
|
|
|
|
#error Unsupported pointer size
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE uint32_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prng_lg_range_u32(uint32_t *state, unsigned lg_range, bool atomic) {
|
2016-11-08 02:52:44 +08:00
|
|
|
uint32_t ret, state1;
|
|
|
|
|
|
|
|
assert(lg_range > 0);
|
|
|
|
assert(lg_range <= 32);
|
|
|
|
|
|
|
|
if (atomic) {
|
|
|
|
uint32_t state0;
|
|
|
|
|
|
|
|
do {
|
2016-11-08 03:27:48 +08:00
|
|
|
state0 = atomic_read_u32(state);
|
2016-11-08 02:52:44 +08:00
|
|
|
state1 = prng_state_next_u32(state0);
|
2016-11-08 03:27:48 +08:00
|
|
|
} while (atomic_cas_u32(state, state0, state1));
|
2016-11-08 02:52:44 +08:00
|
|
|
} else {
|
|
|
|
state1 = prng_state_next_u32(*state);
|
|
|
|
*state = state1;
|
|
|
|
}
|
|
|
|
ret = state1 >> (32 - lg_range);
|
2016-05-28 09:57:15 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-05-28 09:57:15 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 02:52:44 +08:00
|
|
|
/* 64-bit atomic operations cannot be supported on all relevant platforms. */
|
2016-05-28 09:57:15 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE uint64_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prng_lg_range_u64(uint64_t *state, unsigned lg_range) {
|
2016-05-28 09:57:15 +08:00
|
|
|
uint64_t ret, state1;
|
2016-02-10 08:28:40 +08:00
|
|
|
|
|
|
|
assert(lg_range > 0);
|
|
|
|
assert(lg_range <= 64);
|
|
|
|
|
2016-11-08 02:52:44 +08:00
|
|
|
state1 = prng_state_next_u64(*state);
|
|
|
|
*state = state1;
|
|
|
|
ret = state1 >> (64 - lg_range);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-11-08 02:52:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prng_lg_range_zu(size_t *state, unsigned lg_range, bool atomic) {
|
2016-11-08 02:52:44 +08:00
|
|
|
size_t ret, state1;
|
|
|
|
|
|
|
|
assert(lg_range > 0);
|
|
|
|
assert(lg_range <= ZU(1) << (3 + LG_SIZEOF_PTR));
|
|
|
|
|
2016-05-28 09:57:15 +08:00
|
|
|
if (atomic) {
|
2016-11-08 02:52:44 +08:00
|
|
|
size_t state0;
|
2016-05-28 09:57:15 +08:00
|
|
|
|
|
|
|
do {
|
2016-11-08 03:27:48 +08:00
|
|
|
state0 = atomic_read_zu(state);
|
2016-11-08 02:52:44 +08:00
|
|
|
state1 = prng_state_next_zu(state0);
|
2016-11-08 03:27:48 +08:00
|
|
|
} while (atomic_cas_zu(state, state0, state1));
|
2016-05-28 09:57:15 +08:00
|
|
|
} else {
|
2016-11-08 02:52:44 +08:00
|
|
|
state1 = prng_state_next_zu(*state);
|
2016-05-28 09:57:15 +08:00
|
|
|
*state = state1;
|
|
|
|
}
|
2016-11-08 02:52:44 +08:00
|
|
|
ret = state1 >> ((ZU(1) << (3 + LG_SIZEOF_PTR)) - lg_range);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-11-08 02:52:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE uint32_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prng_range_u32(uint32_t *state, uint32_t range, bool atomic) {
|
2016-11-08 02:52:44 +08:00
|
|
|
uint32_t ret;
|
|
|
|
unsigned lg_range;
|
|
|
|
|
|
|
|
assert(range > 1);
|
|
|
|
|
|
|
|
/* Compute the ceiling of lg(range). */
|
|
|
|
lg_range = ffs_u32(pow2_ceil_u32(range)) - 1;
|
|
|
|
|
|
|
|
/* Generate a result in [0..range) via repeated trial. */
|
|
|
|
do {
|
|
|
|
ret = prng_lg_range_u32(state, lg_range, atomic);
|
|
|
|
} while (ret >= range);
|
2016-02-10 08:28:40 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-02-10 08:28:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE uint64_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prng_range_u64(uint64_t *state, uint64_t range) {
|
2016-02-10 08:28:40 +08:00
|
|
|
uint64_t ret;
|
|
|
|
unsigned lg_range;
|
|
|
|
|
|
|
|
assert(range > 1);
|
|
|
|
|
|
|
|
/* Compute the ceiling of lg(range). */
|
2016-02-25 02:32:45 +08:00
|
|
|
lg_range = ffs_u64(pow2_ceil_u64(range)) - 1;
|
2016-02-10 08:28:40 +08:00
|
|
|
|
|
|
|
/* Generate a result in [0..range) via repeated trial. */
|
|
|
|
do {
|
2016-11-08 02:52:44 +08:00
|
|
|
ret = prng_lg_range_u64(state, lg_range);
|
|
|
|
} while (ret >= range);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-11-08 02:52:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
prng_range_zu(size_t *state, size_t range, bool atomic) {
|
2016-11-08 02:52:44 +08:00
|
|
|
size_t ret;
|
|
|
|
unsigned lg_range;
|
|
|
|
|
|
|
|
assert(range > 1);
|
|
|
|
|
|
|
|
/* Compute the ceiling of lg(range). */
|
|
|
|
lg_range = ffs_u64(pow2_ceil_u64(range)) - 1;
|
|
|
|
|
|
|
|
/* Generate a result in [0..range) via repeated trial. */
|
|
|
|
do {
|
|
|
|
ret = prng_lg_range_zu(state, lg_range, atomic);
|
2016-02-10 08:28:40 +08:00
|
|
|
} while (ret >= range);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-02-10 08:28:40 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-11 10:06:31 +08:00
|
|
|
#endif /* JEMALLOC_INTERNAL_PRNG_INLINES_H */
|