aboutsummaryrefslogtreecommitdiff
path: root/src/lib_math.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib_math.c')
-rw-r--r--src/lib_math.c53
1 files changed, 14 insertions, 39 deletions
diff --git a/src/lib_math.c b/src/lib_math.c
index 4cc2ba6e..95b7d460 100644
--- a/src/lib_math.c
+++ b/src/lib_math.c
@@ -15,6 +15,7 @@
15#include "lj_obj.h" 15#include "lj_obj.h"
16#include "lj_lib.h" 16#include "lj_lib.h"
17#include "lj_vm.h" 17#include "lj_vm.h"
18#include "lj_prng.h"
18 19
19/* ------------------------------------------------------------------------ */ 20/* ------------------------------------------------------------------------ */
20 21
@@ -105,34 +106,11 @@ LJLIB_PUSH(1e310) LJLIB_SET(huge)
105** Full-period ME-CF generator with L=64, J=4, k=223, N1=49. 106** Full-period ME-CF generator with L=64, J=4, k=223, N1=49.
106*/ 107*/
107 108
108/* PRNG state. */
109struct RandomState {
110 uint64_t gen[4]; /* State of the 4 LFSR generators. */
111 int valid; /* State is valid. */
112};
113
114/* Union needed for bit-pattern conversion between uint64_t and double. */ 109/* Union needed for bit-pattern conversion between uint64_t and double. */
115typedef union { uint64_t u64; double d; } U64double; 110typedef union { uint64_t u64; double d; } U64double;
116 111
117/* Update generator i and compute a running xor of all states. */ 112/* PRNG seeding function. */
118#define TW223_GEN(i, k, q, s) \ 113static void random_seed(PRNGState *rs, double d)
119 z = rs->gen[i]; \
120 z = (((z<<q)^z) >> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<<s); \
121 r ^= z; rs->gen[i] = z;
122
123/* PRNG step function. Returns a double in the range 1.0 <= d < 2.0. */
124LJ_NOINLINE uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs)
125{
126 uint64_t z, r = 0;
127 TW223_GEN(0, 63, 31, 18)
128 TW223_GEN(1, 58, 19, 28)
129 TW223_GEN(2, 55, 24, 7)
130 TW223_GEN(3, 47, 21, 8)
131 return (r & U64x(000fffff,ffffffff)) | U64x(3ff00000,00000000);
132}
133
134/* PRNG initialization function. */
135static void random_init(RandomState *rs, double d)
136{ 114{
137 uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */ 115 uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */
138 int i; 116 int i;
@@ -141,24 +119,22 @@ static void random_init(RandomState *rs, double d)
141 uint32_t m = 1u << (r&255); 119 uint32_t m = 1u << (r&255);
142 r >>= 8; 120 r >>= 8;
143 u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354; 121 u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354;
144 if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of gen[i] are non-zero. */ 122 if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of u[i] are non-zero. */
145 rs->gen[i] = u.u64; 123 rs->u[i] = u.u64;
146 } 124 }
147 rs->valid = 1;
148 for (i = 0; i < 10; i++) 125 for (i = 0; i < 10; i++)
149 lj_math_random_step(rs); 126 (void)lj_prng_u64(rs);
150} 127}
151 128
152/* PRNG extract function. */ 129/* PRNG extract function. */
153LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */ 130LJLIB_PUSH(top-2) /* Upvalue holds userdata with PRNGState. */
154LJLIB_CF(math_random) LJLIB_REC(.) 131LJLIB_CF(math_random) LJLIB_REC(.)
155{ 132{
156 int n = (int)(L->top - L->base); 133 int n = (int)(L->top - L->base);
157 RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); 134 PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
158 U64double u; 135 U64double u;
159 double d; 136 double d;
160 if (LJ_UNLIKELY(!rs->valid)) random_init(rs, 0.0); 137 u.u64 = lj_prng_u64d(rs);
161 u.u64 = lj_math_random_step(rs);
162 d = u.d - 1.0; 138 d = u.d - 1.0;
163 if (n > 0) { 139 if (n > 0) {
164#if LJ_DUALNUM 140#if LJ_DUALNUM
@@ -203,11 +179,11 @@ LJLIB_CF(math_random) LJLIB_REC(.)
203} 179}
204 180
205/* PRNG seed function. */ 181/* PRNG seed function. */
206LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */ 182LJLIB_PUSH(top-2) /* Upvalue holds userdata with PRNGState. */
207LJLIB_CF(math_randomseed) 183LJLIB_CF(math_randomseed)
208{ 184{
209 RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); 185 PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
210 random_init(rs, lj_lib_checknum(L, 1)); 186 random_seed(rs, lj_lib_checknum(L, 1));
211 return 0; 187 return 0;
212} 188}
213 189
@@ -217,9 +193,8 @@ LJLIB_CF(math_randomseed)
217 193
218LUALIB_API int luaopen_math(lua_State *L) 194LUALIB_API int luaopen_math(lua_State *L)
219{ 195{
220 RandomState *rs; 196 PRNGState *rs = (PRNGState *)lua_newuserdata(L, sizeof(PRNGState));
221 rs = (RandomState *)lua_newuserdata(L, sizeof(RandomState)); 197 lj_prng_seed_fixed(rs);
222 rs->valid = 0; /* Use lazy initialization to save some time on startup. */
223 LJ_LIB_REG(L, LUA_MATHLIBNAME, math); 198 LJ_LIB_REG(L, LUA_MATHLIBNAME, math);
224 return 1; 199 return 1;
225} 200}