From 0cc11cca918820e2e54b9b13497e54536fd4a6e5 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 16 Dec 2014 19:43:55 -0800 Subject: * rand.c (rstate): New inline function. (rand32): Use inline function instead of macro. I compared gcc -O2 output on Intel: no difference. --- ChangeLog | 6 ++++++ rand.c | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3a31e6d0..d4ad91cf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2014-12-16 Kaz Kylheku + + * rand.c (rstate): New inline function. + (rand32): Use inline function instead of macro. + I compared gcc -O2 output on Intel: no difference. + 2014-12-13 Kaz Kylheku Factor out some compiling commands into macros. diff --git a/rand.c b/rand.c index 396bb459..9a1e9d82 100644 --- a/rand.c +++ b/rand.c @@ -83,25 +83,28 @@ val random_state_p(val obj) return typeof(obj) == random_state_s ? t : nil; } +INLINE rand32_t *rstate(struct rand_state *r, int offs) +{ + return &r->state[(r->cur + offs) % 16]; +} + static rand32_t rand32(struct rand_state *r) { - #define RSTATE(r,i) ((r)->state[((r)->cur + i) % 16]) - rand32_t s0 = RSTATE(r, 0); - rand32_t s9 = RSTATE(r, 9); - rand32_t s13 = RSTATE(r, 13); - rand32_t s15 = RSTATE(r, 15); + rand32_t s0 = *rstate(r, 0); + rand32_t s9 = *rstate(r, 9); + rand32_t s13 = *rstate(r, 13); + rand32_t s15 = *rstate(r, 15); rand32_t r1 = s0 ^ (s0 << 16) ^ s13 ^ (s13 << 15); rand32_t r2 = s9 ^ (s9 >> 11); - rand32_t ns0 = RSTATE(r, 0) = r1 ^ r2; + rand32_t ns0 = *rstate(r, 0) = r1 ^ r2; rand32_t ns15 = s15 ^ (s15 << 2) ^ r1 ^ (r1 << 18) ^ r2 ^ (r2 << 28) ^ ((ns0 ^ (ns0 << 5)) & 0xda442d24ul); - RSTATE(r, 15) = ns15; + *rstate(r, 15) = ns15; r->cur = (r->cur + 15) % 16; return ns15; - #undef RSTATE } val make_random_state(val seed) -- cgit v1.2.3