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.c96
1 files changed, 34 insertions, 62 deletions
diff --git a/src/lib_math.c b/src/lib_math.c
index c18fae42..2f598355 100644
--- a/src/lib_math.c
+++ b/src/lib_math.c
@@ -13,8 +13,10 @@
13#include "lualib.h" 13#include "lualib.h"
14 14
15#include "lj_obj.h" 15#include "lj_obj.h"
16#include "lj_err.h"
16#include "lj_lib.h" 17#include "lj_lib.h"
17#include "lj_vm.h" 18#include "lj_vm.h"
19#include "lj_prng.h"
18 20
19/* ------------------------------------------------------------------------ */ 21/* ------------------------------------------------------------------------ */
20 22
@@ -33,25 +35,19 @@ LJLIB_ASM(math_sqrt) LJLIB_REC(math_unary IRFPM_SQRT)
33 lj_lib_checknum(L, 1); 35 lj_lib_checknum(L, 1);
34 return FFH_RETRY; 36 return FFH_RETRY;
35} 37}
36LJLIB_ASM_(math_log10) LJLIB_REC(math_unary IRFPM_LOG10) 38LJLIB_ASM_(math_log10) LJLIB_REC(math_call IRCALL_log10)
37LJLIB_ASM_(math_exp) LJLIB_REC(math_unary IRFPM_EXP) 39LJLIB_ASM_(math_exp) LJLIB_REC(math_call IRCALL_exp)
38LJLIB_ASM_(math_sin) LJLIB_REC(math_unary IRFPM_SIN) 40LJLIB_ASM_(math_sin) LJLIB_REC(math_call IRCALL_sin)
39LJLIB_ASM_(math_cos) LJLIB_REC(math_unary IRFPM_COS) 41LJLIB_ASM_(math_cos) LJLIB_REC(math_call IRCALL_cos)
40LJLIB_ASM_(math_tan) LJLIB_REC(math_unary IRFPM_TAN) 42LJLIB_ASM_(math_tan) LJLIB_REC(math_call IRCALL_tan)
41LJLIB_ASM_(math_asin) LJLIB_REC(math_atrig FF_math_asin) 43LJLIB_ASM_(math_asin) LJLIB_REC(math_call IRCALL_asin)
42LJLIB_ASM_(math_acos) LJLIB_REC(math_atrig FF_math_acos) 44LJLIB_ASM_(math_acos) LJLIB_REC(math_call IRCALL_acos)
43LJLIB_ASM_(math_atan) LJLIB_REC(math_atrig FF_math_atan) 45LJLIB_ASM_(math_atan) LJLIB_REC(math_call IRCALL_atan)
44LJLIB_ASM_(math_sinh) LJLIB_REC(math_htrig IRCALL_sinh) 46LJLIB_ASM_(math_sinh) LJLIB_REC(math_call IRCALL_sinh)
45LJLIB_ASM_(math_cosh) LJLIB_REC(math_htrig IRCALL_cosh) 47LJLIB_ASM_(math_cosh) LJLIB_REC(math_call IRCALL_cosh)
46LJLIB_ASM_(math_tanh) LJLIB_REC(math_htrig IRCALL_tanh) 48LJLIB_ASM_(math_tanh) LJLIB_REC(math_call IRCALL_tanh)
47LJLIB_ASM_(math_frexp) 49LJLIB_ASM_(math_frexp)
48LJLIB_ASM_(math_modf) LJLIB_REC(.) 50LJLIB_ASM_(math_modf)
49
50LJLIB_PUSH(57.29577951308232)
51LJLIB_ASM_(math_deg) LJLIB_REC(math_degrad)
52
53LJLIB_PUSH(0.017453292519943295)
54LJLIB_ASM_(math_rad) LJLIB_REC(math_degrad)
55 51
56LJLIB_ASM(math_log) LJLIB_REC(math_log) 52LJLIB_ASM(math_log) LJLIB_REC(math_log)
57{ 53{
@@ -63,12 +59,15 @@ LJLIB_ASM(math_log) LJLIB_REC(math_log)
63#else 59#else
64 x = lj_vm_log2(x); y = 1.0 / lj_vm_log2(y); 60 x = lj_vm_log2(x); y = 1.0 / lj_vm_log2(y);
65#endif 61#endif
66 setnumV(L->base-1, x*y); /* Do NOT join the expression to x / y. */ 62 setnumV(L->base-1-LJ_FR2, x*y); /* Do NOT join the expression to x / y. */
67 return FFH_RES(1); 63 return FFH_RES(1);
68 } 64 }
69 return FFH_RETRY; 65 return FFH_RETRY;
70} 66}
71 67
68LJLIB_LUA(math_deg) /* function(x) return x * 57.29577951308232 end */
69LJLIB_LUA(math_rad) /* function(x) return x * 0.017453292519943295 end */
70
72LJLIB_ASM(math_atan2) LJLIB_REC(.) 71LJLIB_ASM(math_atan2) LJLIB_REC(.)
73{ 72{
74 lj_lib_checknum(L, 1); 73 lj_lib_checknum(L, 1);
@@ -108,34 +107,11 @@ LJLIB_PUSH(1e310) LJLIB_SET(huge)
108** Full-period ME-CF generator with L=64, J=4, k=223, N1=49. 107** Full-period ME-CF generator with L=64, J=4, k=223, N1=49.
109*/ 108*/
110 109
111/* PRNG state. */
112struct RandomState {
113 uint64_t gen[4]; /* State of the 4 LFSR generators. */
114 int valid; /* State is valid. */
115};
116
117/* Union needed for bit-pattern conversion between uint64_t and double. */ 110/* Union needed for bit-pattern conversion between uint64_t and double. */
118typedef union { uint64_t u64; double d; } U64double; 111typedef union { uint64_t u64; double d; } U64double;
119 112
120/* Update generator i and compute a running xor of all states. */ 113/* PRNG seeding function. */
121#define TW223_GEN(i, k, q, s) \ 114static void random_seed(PRNGState *rs, double d)
122 z = rs->gen[i]; \
123 z = (((z<<q)^z) >> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<<s); \
124 r ^= z; rs->gen[i] = z;
125
126/* PRNG step function. Returns a double in the range 1.0 <= d < 2.0. */
127LJ_NOINLINE uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs)
128{
129 uint64_t z, r = 0;
130 TW223_GEN(0, 63, 31, 18)
131 TW223_GEN(1, 58, 19, 28)
132 TW223_GEN(2, 55, 24, 7)
133 TW223_GEN(3, 47, 21, 8)
134 return (r & U64x(000fffff,ffffffff)) | U64x(3ff00000,00000000);
135}
136
137/* PRNG initialization function. */
138static void random_init(RandomState *rs, double d)
139{ 115{
140 uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */ 116 uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */
141 int i; 117 int i;
@@ -144,24 +120,22 @@ static void random_init(RandomState *rs, double d)
144 uint32_t m = 1u << (r&255); 120 uint32_t m = 1u << (r&255);
145 r >>= 8; 121 r >>= 8;
146 u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354; 122 u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354;
147 if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of gen[i] are non-zero. */ 123 if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of u[i] are non-zero. */
148 rs->gen[i] = u.u64; 124 rs->u[i] = u.u64;
149 } 125 }
150 rs->valid = 1;
151 for (i = 0; i < 10; i++) 126 for (i = 0; i < 10; i++)
152 lj_math_random_step(rs); 127 (void)lj_prng_u64(rs);
153} 128}
154 129
155/* PRNG extract function. */ 130/* PRNG extract function. */
156LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */ 131LJLIB_PUSH(top-2) /* Upvalue holds userdata with PRNGState. */
157LJLIB_CF(math_random) LJLIB_REC(.) 132LJLIB_CF(math_random) LJLIB_REC(.)
158{ 133{
159 int n = (int)(L->top - L->base); 134 int n = (int)(L->top - L->base);
160 RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); 135 PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
161 U64double u; 136 U64double u;
162 double d; 137 double d;
163 if (LJ_UNLIKELY(!rs->valid)) random_init(rs, 0.0); 138 u.u64 = lj_prng_u64d(rs);
164 u.u64 = lj_math_random_step(rs);
165 d = u.d - 1.0; 139 d = u.d - 1.0;
166 if (n > 0) { 140 if (n > 0) {
167#if LJ_DUALNUM 141#if LJ_DUALNUM
@@ -206,11 +180,14 @@ LJLIB_CF(math_random) LJLIB_REC(.)
206} 180}
207 181
208/* PRNG seed function. */ 182/* PRNG seed function. */
209LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */ 183LJLIB_PUSH(top-2) /* Upvalue holds userdata with PRNGState. */
210LJLIB_CF(math_randomseed) 184LJLIB_CF(math_randomseed)
211{ 185{
212 RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); 186 PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
213 random_init(rs, lj_lib_checknum(L, 1)); 187 if (L->base != L->top)
188 random_seed(rs, lj_lib_checknum(L, 1));
189 else if (!lj_prng_seed_secure(rs))
190 lj_err_caller(L, LJ_ERR_PRNGSD);
214 return 0; 191 return 0;
215} 192}
216 193
@@ -220,14 +197,9 @@ LJLIB_CF(math_randomseed)
220 197
221LUALIB_API int luaopen_math(lua_State *L) 198LUALIB_API int luaopen_math(lua_State *L)
222{ 199{
223 RandomState *rs; 200 PRNGState *rs = (PRNGState *)lua_newuserdata(L, sizeof(PRNGState));
224 rs = (RandomState *)lua_newuserdata(L, sizeof(RandomState)); 201 lj_prng_seed_fixed(rs);
225 rs->valid = 0; /* Use lazy initialization to save some time on startup. */
226 LJ_LIB_REG(L, LUA_MATHLIBNAME, math); 202 LJ_LIB_REG(L, LUA_MATHLIBNAME, math);
227#if defined(LUA_COMPAT_MOD) && !LJ_52
228 lua_getfield(L, -1, "fmod");
229 lua_setfield(L, -2, "mod");
230#endif
231 return 1; 203 return 1;
232} 204}
233 205