lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I tried to replace LuaJIT 2.0.5 lib_math.c with xoshiro256**, but
did not see any math.random speedup (against Tausworthe)

By swapping s[0] and s[1], and scramble with updated state, it 
matched LuaJIT Tausworthe speed. (Note: it is still xoshiro256**)

LuaJIT, 1 billion math.random(), best of 3

49.2s    Tausworthe: LuaJIT stock PRNG
50.1s.   Xoshiro256**: http://xoshiro.di.unimi.it/xoshiro256starstar.c
48.9s.   Xoshiro256** (modified), lj_math_random_step() patch below

static inline uint64_t rotl(const uint64_t x, int k) {
 return (x << k) | (x >> (64 - k));
}

LJ_NOINLINE uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs)
{
 uint64_t *s = rs->gen;    /* modifed xoshiro256** */
 uint64_t t = s[0] << 17;  /* s[0], s[1] swapped */
 s[2] ^= s[1];
 s[3] ^= s[0];
 s[0] ^= s[2];
 s[1] ^= s[3];
 s[2] ^= t;
 s[3] = rotl(s[3], 45);
 uint64_t r = rotl(s[0] * 5, 7) * 9; /* scramble updated state */
 return (r & U64x(000fffff,ffffffff)) | U64x(3ff00000,00000000);
}