54 #define MATRIX_A 0x9908b0dfU
55 #define UMASK 0x80000000U
56 #define LMASK 0x7fffffffU
57 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
58 #define TWIST(u,v) ((MIXBITS((u),(v)) >> 1) ^ ((v)&1U ? MATRIX_A : 0U))
60 enum {MT_MAX_STATE = N};
69 #define genrand_initialized(mt) ((mt)->next != 0)
70 #define uninit_genrand(mt) ((mt)->next = 0)
72 NO_SANITIZE(
"unsigned-integer-overflow",
static void init_genrand(
struct MT *mt,
unsigned int s));
73 NO_SANITIZE(
"unsigned-integer-overflow",
static void init_by_array(
struct MT *mt,
const uint32_t init_key[],
int key_length));
77 init_genrand(
struct MT *mt,
unsigned int s)
80 mt->state[0] = s & 0xffffffffU;
82 mt->state[j] = (1812433253U * (mt->state[j-1] ^ (mt->state[j-1] >> 30)) + j);
87 mt->state[j] &= 0xffffffff;
90 mt->next = mt->state + N;
98 init_by_array(
struct MT *mt,
const uint32_t init_key[],
int key_length)
101 init_genrand(mt, 19650218U);
103 k = (N>key_length ? N : key_length);
105 mt->state[i] = (mt->state[i] ^ ((mt->state[i-1] ^ (mt->state[i-1] >> 30)) * 1664525U))
107 mt->state[i] &= 0xffffffffU;
109 if (i>=N) { mt->state[0] = mt->state[N-1]; i=1; }
110 if (j>=key_length) j=0;
112 for (k=N-1; k; k--) {
113 mt->state[i] = (mt->state[i] ^ ((mt->state[i-1] ^ (mt->state[i-1] >> 30)) * 1566083941U))
115 mt->state[i] &= 0xffffffffU;
117 if (i>=N) { mt->state[0] = mt->state[N-1]; i=1; }
120 mt->state[0] = 0x80000000U;
124 next_state(
struct MT *mt)
126 uint32_t *p = mt->state;
130 mt->next = mt->state;
132 for (j=N-M+1; --j; p++)
133 *p = p[M] ^ TWIST(p[0], p[1]);
136 *p = p[M-N] ^ TWIST(p[0], p[1]);
138 *p = p[M-N] ^ TWIST(p[0], mt->state[0]);
143 genrand_int32(
struct MT *mt)
148 if (--mt->left <= 0) next_state(mt);
153 y ^= (y << 7) & 0x9d2c5680;
154 y ^= (y << 15) & 0xefc60000;