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.c92
1 files changed, 30 insertions, 62 deletions
diff --git a/src/lib_math.c b/src/lib_math.c
index 56fb091b..b677bbcd 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
@@ -33,25 +34,19 @@ LJLIB_ASM(math_sqrt) LJLIB_REC(math_unary IRFPM_SQRT)
33 lj_lib_checknum(L, 1); 34 lj_lib_checknum(L, 1);
34 return FFH_RETRY; 35 return FFH_RETRY;
35} 36}
36LJLIB_ASM_(math_log10) LJLIB_REC(math_unary IRFPM_LOG10) 37LJLIB_ASM_(math_log10) LJLIB_REC(math_call IRCALL_log10)
37LJLIB_ASM_(math_exp) LJLIB_REC(math_unary IRFPM_EXP) 38LJLIB_ASM_(math_exp) LJLIB_REC(math_call IRCALL_exp)
38LJLIB_ASM_(math_sin) LJLIB_REC(math_unary IRFPM_SIN) 39LJLIB_ASM_(math_sin) LJLIB_REC(math_call IRCALL_sin)
39LJLIB_ASM_(math_cos) LJLIB_REC(math_unary IRFPM_COS) 40LJLIB_ASM_(math_cos) LJLIB_REC(math_call IRCALL_cos)
40LJLIB_ASM_(math_tan) LJLIB_REC(math_unary IRFPM_TAN) 41LJLIB_ASM_(math_tan) LJLIB_REC(math_call IRCALL_tan)
41LJLIB_ASM_(math_asin) LJLIB_REC(math_atrig FF_math_asin) 42LJLIB_ASM_(math_asin) LJLIB_REC(math_call IRCALL_asin)
42LJLIB_ASM_(math_acos) LJLIB_REC(math_atrig FF_math_acos) 43LJLIB_ASM_(math_acos) LJLIB_REC(math_call IRCALL_acos)
43LJLIB_ASM_(math_atan) LJLIB_REC(math_atrig FF_math_atan) 44LJLIB_ASM_(math_atan) LJLIB_REC(math_call IRCALL_atan)
44LJLIB_ASM_(math_sinh) LJLIB_REC(math_htrig IRCALL_sinh) 45LJLIB_ASM_(math_sinh) LJLIB_REC(math_call IRCALL_sinh)
45LJLIB_ASM_(math_cosh) LJLIB_REC(math_htrig IRCALL_cosh) 46LJLIB_ASM_(math_cosh) LJLIB_REC(math_call IRCALL_cosh)
46LJLIB_ASM_(math_tanh) LJLIB_REC(math_htrig IRCALL_tanh) 47LJLIB_ASM_(math_tanh) LJLIB_REC(math_call IRCALL_tanh)
47LJLIB_ASM_(math_frexp) 48LJLIB_ASM_(math_frexp)
48LJLIB_ASM_(math_modf) LJLIB_REC(.) 49LJLIB_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 50
56LJLIB_ASM(math_log) LJLIB_REC(math_log) 51LJLIB_ASM(math_log) LJLIB_REC(math_log)
57{ 52{
@@ -63,12 +58,15 @@ LJLIB_ASM(math_log) LJLIB_REC(math_log)
63#else 58#else
64 x = lj_vm_log2(x); y = 1.0 / lj_vm_log2(y); 59 x = lj_vm_log2(x); y = 1.0 / lj_vm_log2(y);
65#endif 60#endif
66 setnumV(L->base-1, x*y); /* Do NOT join the expression to x / y. */ 61 setnumV(L->base-1-LJ_FR2, x*y); /* Do NOT join the expression to x / y. */
67 return FFH_RES(1); 62 return FFH_RES(1);
68 } 63 }
69 return FFH_RETRY; 64 return FFH_RETRY;
70} 65}
71 66
67LJLIB_LUA(math_deg) /* function(x) return x * 57.29577951308232 end */
68LJLIB_LUA(math_rad) /* function(x) return x * 0.017453292519943295 end */
69
72LJLIB_ASM(math_atan2) LJLIB_REC(.) 70LJLIB_ASM(math_atan2) LJLIB_REC(.)
73{ 71{
74 lj_lib_checknum(L, 1); 72 lj_lib_checknum(L, 1);
@@ -108,34 +106,11 @@ LJLIB_PUSH(1e310) LJLIB_SET(huge)
108** 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.
109*/ 107*/
110 108
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. */ 109/* Union needed for bit-pattern conversion between uint64_t and double. */
118typedef union { uint64_t u64; double d; } U64double; 110typedef union { uint64_t u64; double d; } U64double;
119 111
120/* Update generator i and compute a running xor of all states. */ 112/* PRNG seeding function. */
121#define TW223_GEN(i, k, q, s) \ 113static 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{ 114{
140 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. */
141 int i; 116 int i;
@@ -144,24 +119,22 @@ static void random_init(RandomState *rs, double d)
144 uint32_t m = 1u << (r&255); 119 uint32_t m = 1u << (r&255);
145 r >>= 8; 120 r >>= 8;
146 u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354; 121 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. */ 122 if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of u[i] are non-zero. */
148 rs->gen[i] = u.u64; 123 rs->u[i] = u.u64;
149 } 124 }
150 rs->valid = 1;
151 for (i = 0; i < 10; i++) 125 for (i = 0; i < 10; i++)
152 lj_math_random_step(rs); 126 (void)lj_prng_u64(rs);
153} 127}
154 128
155/* PRNG extract function. */ 129/* PRNG extract function. */
156LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */ 130LJLIB_PUSH(top-2) /* Upvalue holds userdata with PRNGState. */
157LJLIB_CF(math_random) LJLIB_REC(.) 131LJLIB_CF(math_random) LJLIB_REC(.)
158{ 132{
159 int n = (int)(L->top - L->base); 133 int n = (int)(L->top - L->base);
160 RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); 134 PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
161 U64double u; 135 U64double u;
162 double d; 136 double d;
163 if (LJ_UNLIKELY(!rs->valid)) random_init(rs, 0.0); 137 u.u64 = lj_prng_u64d(rs);
164 u.u64 = lj_math_random_step(rs);
165 d = u.d - 1.0; 138 d = u.d - 1.0;
166 if (n > 0) { 139 if (n > 0) {
167#if LJ_DUALNUM 140#if LJ_DUALNUM
@@ -206,11 +179,11 @@ LJLIB_CF(math_random) LJLIB_REC(.)
206} 179}
207 180
208/* PRNG seed function. */ 181/* PRNG seed function. */
209LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */ 182LJLIB_PUSH(top-2) /* Upvalue holds userdata with PRNGState. */
210LJLIB_CF(math_randomseed) 183LJLIB_CF(math_randomseed)
211{ 184{
212 RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); 185 PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
213 random_init(rs, lj_lib_checknum(L, 1)); 186 random_seed(rs, lj_lib_checknum(L, 1));
214 return 0; 187 return 0;
215} 188}
216 189
@@ -220,14 +193,9 @@ LJLIB_CF(math_randomseed)
220 193
221LUALIB_API int luaopen_math(lua_State *L) 194LUALIB_API int luaopen_math(lua_State *L)
222{ 195{
223 RandomState *rs; 196 PRNGState *rs = (PRNGState *)lua_newuserdata(L, sizeof(PRNGState));
224 rs = (RandomState *)lua_newuserdata(L, sizeof(RandomState)); 197 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); 198 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; 199 return 1;
232} 200}
233 201