Skip to content

Commit 44dba31

Browse files
Sheppard, KevinSheppard, Kevin
authored andcommitted
REF: Split random-kit core rng into two functions to impreve performance
Splitting the function into the small commonly used part and the rarely used larger part (typically 1 in 624 calls) improves inline performance
1 parent 37d6d47 commit 44dba31

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

randomstate/src/random-kit/random-kit.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,22 @@ extern void init_by_array(rk_state *state, unsigned long init_key[], int key_len
8484
mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
8585
}
8686

87+
void rk_gen(rk_state *state)
88+
{
89+
uint32_t y;
90+
int i;
8791

92+
for (i = 0; i < N - M; i++) {
93+
y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK);
94+
state->key[i] = state->key[i+M] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
95+
}
96+
for (; i < N - 1; i++) {
97+
y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK);
98+
state->key[i] = state->key[i+(M-N)] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
99+
}
100+
y = (state->key[N - 1] & UPPER_MASK) | (state->key[0] & LOWER_MASK);
101+
state->key[N - 1] = state->key[M - 1] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
102+
103+
state->pos = 0;
104+
}
88105

randomstate/src/random-kit/random-kit.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,16 @@ rk_state;
2727

2828
extern void rk_seed(rk_state *state, uint32_t seed);
2929

30+
extern void rk_gen(rk_state *state);
31+
3032
/* Slightly optimized reference implementation of the Mersenne Twister */
3133
inline uint32_t rk_random(rk_state *state)
3234
{
3335
uint32_t y;
3436

3537
if (state->pos == RK_STATE_LEN) {
36-
int i;
37-
38-
for (i = 0; i < N - M; i++) {
39-
y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK);
40-
state->key[i] = state->key[i+M] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
41-
}
42-
for (; i < N - 1; i++) {
43-
y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK);
44-
state->key[i] = state->key[i+(M-N)] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
45-
}
46-
y = (state->key[N - 1] & UPPER_MASK) | (state->key[0] & LOWER_MASK);
47-
state->key[N - 1] = state->key[M - 1] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
48-
49-
state->pos = 0;
38+
// Move to function to help inlining
39+
rk_gen(state);
5040
}
5141
y = state->key[state->pos++];
5242

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,5 @@ def write_config(file_name, config):
161161
# Clean up generated files
162162
for config in configs:
163163
os.unlink(join(mod_dir, config['file_name'] + '.pyx'))
164-
# os.unlink(join(mod_dir, config['file_name'] + '-config.pxi'))
165-
# os.unlink(join(mod_dir, config['file_name'] + '.c'))
164+
os.unlink(join(mod_dir, config['file_name'] + '-config.pxi'))
165+
os.unlink(join(mod_dir, config['file_name'] + '.c'))

0 commit comments

Comments
 (0)