Skip to content

Commit

Permalink
Replace addition with bitwise OR in Q_rand().
Browse files Browse the repository at this point in the history
  • Loading branch information
skullernet authored and Paril committed Dec 10, 2021
1 parent 05ee85f commit 53d8324
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/shared/shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ void Q_srand(uint32_t seed)
mt_index = N;
mt_state[0] = seed;
for (int i = 1; i < N; i++)
mt_state[i] = seed = 1812433253U * (seed ^ seed >> 30) + i;
mt_state[i] = seed = 1812433253 * (seed ^ seed >> 30) + i;
}

/*
Expand All @@ -923,12 +923,12 @@ uint32_t Q_rand(void)
if (mt_index >= N) {
mt_index = 0;

#define STEP(j, k) do { \
x = mt_state[i] & (1U << 31); \
x += mt_state[j] & ((1U << 31) - 1); \
y = x >> 1; \
y ^= 0x9908B0DF & -(x & 1); \
mt_state[i] = mt_state[k] ^ y; \
#define STEP(j, k) do { \
x = mt_state[i] & 0x80000000; \
x |= mt_state[j] & 0x7FFFFFFF; \
y = x >> 1; \
y ^= 0x9908B0DF & -(x & 1); \
mt_state[i] = mt_state[k] ^ y; \
} while (0)

for (i = 0; i < N - M; i++)
Expand Down Expand Up @@ -961,7 +961,7 @@ uint32_t Q_rand_uniform(uint32_t n)
if (n < 2)
return 0;

m = -n % n; // 2^32 mod n
m = -n % n; // m = 2^32 mod n
do {
r = Q_rand();
} while (r < m);
Expand Down

0 comments on commit 53d8324

Please sign in to comment.