The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
test_rng.h File Reference

Pseudo random number generator for tests. More...

#include <core/intrinsics.h>
#include <memory.h>
#include <stdint.h>
+ Include dependency graph for test_rng.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define LCG_MULTIPLIER   ((((__uint128_t)0x0fc94e3bf4e9ab32ULL) << 64) + 0x866458cd56f5e605ULL)
 The multiplier of this linear congruential PRNG generator.
 
#define lcg_init(rng_state, initseq)   (rng_state) = ((initseq) << 1u) | 1u
 Initializes the random number generator. More...
 
#define lcg_random_u(rng_state)
 Computes a pseudo random 64 bit number. More...
 
#define lcg_random(rng_state)
 Computes a pseudo random number in the [0, 1] range. More...
 

Typedefs

typedef __uint128_t test_rng_state
 The type of this pseudo random number generator state.
 

Detailed Description

Pseudo random number generator for tests.

An acceptable quality pseudo random number generator to be used in tests

Definition in file test_rng.h.

Macro Definition Documentation

◆ lcg_init

#define lcg_init (   rng_state,
  initseq 
)    (rng_state) = ((initseq) << 1u) | 1u

Initializes the random number generator.

Parameters
rng_statea test_rng_state object which will be initialized
initseqthe seed to use to initialize rng_state

Definition at line 30 of file test_rng.h.

◆ lcg_random

#define lcg_random (   rng_state)
Value:
__extension__({ \
uint64_t __u_val = lcg_random_u(rng_state); \
double __ret = 0.0; \
if (__builtin_expect(!!__u_val, 1)) { \
unsigned __lzs = intrinsics_clz(__u_val) + 1; \
__u_val <<= __lzs; \
__u_val >>= 12; \
\
uint64_t __exp = 1023 - __lzs; \
__u_val |= __exp << 52; \
\
memcpy(&__ret, &__u_val, sizeof(double)); \
} \
__ret; \
})

Computes a pseudo random number in the [0, 1] range.

Parameters
rng_statea test_rng_state object
Returns
a uniformly distributed pseudo random double value in [0, 1]

Definition at line 49 of file test_rng.h.

◆ lcg_random_u

#define lcg_random_u (   rng_state)
Value:
__extension__({ \
(rng_state) *= LCG_MULTIPLIER; \
uint64_t __rng_val = (uint64_t)((rng_state) >> 64); \
__rng_val; \
})

Computes a pseudo random 64 bit number.

Parameters
rng_statea test_rng_state object
Returns
a uniformly distributed 64 bit pseudo random number

Definition at line 37 of file test_rng.h.

intrinsics_clz
#define intrinsics_clz(x)
Counts the leading zeros in a base 2 number.
Definition: intrinsics.h:47
lcg_random_u
#define lcg_random_u(rng_state)
Computes a pseudo random 64 bit number.
Definition: test_rng.h:37
LCG_MULTIPLIER
#define LCG_MULTIPLIER
The multiplier of this linear congruential PRNG generator.
Definition: test_rng.h:22