summaryrefslogtreecommitdiff
path: root/src/lib_math.c
diff options
context:
space:
mode:
authorMike Pall <mike>2009-12-08 20:35:29 +0100
committerMike Pall <mike>2009-12-08 20:35:29 +0100
commit3f1f9e11f4f699ae94182d4cba158092f434a7f6 (patch)
tree88fbb674a21a1d554d4b1ee9d4ef2c5fed6a1d88 /src/lib_math.c
parent5287b9326479ea2b7dddd6f642673e58e5a7f354 (diff)
downloadluajit-3f1f9e11f4f699ae94182d4cba158092f434a7f6.tar.gz
luajit-3f1f9e11f4f699ae94182d4cba158092f434a7f6.tar.bz2
luajit-3f1f9e11f4f699ae94182d4cba158092f434a7f6.zip
Fast forward to sync public repo.
Compile math.sinh(), math.cosh(), math.tanh() and math.random(). Compile various io.*() functions. Drive the GC forward on string allocations in the parser. Improve KNUM fuse vs. load heuristics. Add abstract C call handling to IR.
Diffstat (limited to 'src/lib_math.c')
-rw-r--r--src/lib_math.c52
1 files changed, 26 insertions, 26 deletions
diff --git a/src/lib_math.c b/src/lib_math.c
index adc77c9d..f3803e8f 100644
--- a/src/lib_math.c
+++ b/src/lib_math.c
@@ -36,9 +36,9 @@ LJLIB_ASM_(math_tan) LJLIB_REC(math_unary IRFPM_TAN)
36LJLIB_ASM_(math_asin) LJLIB_REC(math_atrig FF_math_asin) 36LJLIB_ASM_(math_asin) LJLIB_REC(math_atrig FF_math_asin)
37LJLIB_ASM_(math_acos) LJLIB_REC(math_atrig FF_math_acos) 37LJLIB_ASM_(math_acos) LJLIB_REC(math_atrig FF_math_acos)
38LJLIB_ASM_(math_atan) LJLIB_REC(math_atrig FF_math_atan) 38LJLIB_ASM_(math_atan) LJLIB_REC(math_atrig FF_math_atan)
39LJLIB_ASM_(math_sinh) 39LJLIB_ASM_(math_sinh) LJLIB_REC(math_htrig IRCALL_sinh)
40LJLIB_ASM_(math_cosh) 40LJLIB_ASM_(math_cosh) LJLIB_REC(math_htrig IRCALL_cosh)
41LJLIB_ASM_(math_tanh) 41LJLIB_ASM_(math_tanh) LJLIB_REC(math_htrig IRCALL_tanh)
42LJLIB_ASM_(math_frexp) 42LJLIB_ASM_(math_frexp)
43LJLIB_ASM_(math_modf) LJLIB_REC(.) 43LJLIB_ASM_(math_modf) LJLIB_REC(.)
44 44
@@ -82,35 +82,33 @@ LJ_FUNCA double lj_wrapper_tanh(double x) { return tanh(x); }
82*/ 82*/
83 83
84/* PRNG state. */ 84/* PRNG state. */
85typedef struct TW223State { 85struct RandomState {
86 uint64_t gen[4]; /* State of the 4 LFSR generators. */ 86 uint64_t gen[4]; /* State of the 4 LFSR generators. */
87 int valid; /* State is valid. */ 87 int valid; /* State is valid. */
88} TW223State; 88};
89 89
90/* Union needed for bit-pattern conversion between uint64_t and double. */ 90/* Union needed for bit-pattern conversion between uint64_t and double. */
91typedef union { uint64_t u64; double d; } U64double; 91typedef union { uint64_t u64; double d; } U64double;
92 92
93/* Update generator i and compute a running xor of all states. */ 93/* Update generator i and compute a running xor of all states. */
94#define TW223_GEN(i, k, q, s) \ 94#define TW223_GEN(i, k, q, s) \
95 z = tw->gen[i]; \ 95 z = rs->gen[i]; \
96 z = (((z<<q)^z) >> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<<s); \ 96 z = (((z<<q)^z) >> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<<s); \
97 r ^= z; tw->gen[i] = z; 97 r ^= z; rs->gen[i] = z;
98 98
99/* PRNG step function. Returns a double in the range 1.0 <= d < 2.0. */ 99/* PRNG step function. Returns a double in the range 1.0 <= d < 2.0. */
100static LJ_NOINLINE double tw223_step(TW223State *tw) 100LJ_NOINLINE uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs)
101{ 101{
102 uint64_t z, r = 0; 102 uint64_t z, r = 0;
103 U64double u;
104 TW223_GEN(0, 63, 31, 18) 103 TW223_GEN(0, 63, 31, 18)
105 TW223_GEN(1, 58, 19, 28) 104 TW223_GEN(1, 58, 19, 28)
106 TW223_GEN(2, 55, 24, 7) 105 TW223_GEN(2, 55, 24, 7)
107 TW223_GEN(3, 47, 21, 8) 106 TW223_GEN(3, 47, 21, 8)
108 u.u64 = (r & (((uint64_t)1 << 52)-1)) | ((uint64_t)0x3ff << 52); 107 return (r & U64x(000fffff,ffffffff)) | U64x(3ff00000,00000000);
109 return u.d;
110} 108}
111 109
112/* PRNG initialization function. */ 110/* PRNG initialization function. */
113static void tw223_init(TW223State *tw, double d) 111static void random_init(RandomState *rs, double d)
114{ 112{
115 uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */ 113 uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */
116 int i; 114 int i;
@@ -120,22 +118,24 @@ static void tw223_init(TW223State *tw, double d)
120 r >>= 8; 118 r >>= 8;
121 u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354; 119 u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354;
122 if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of gen[i] are non-zero. */ 120 if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of gen[i] are non-zero. */
123 tw->gen[i] = u.u64; 121 rs->gen[i] = u.u64;
124 } 122 }
125 tw->valid = 1; 123 rs->valid = 1;
126 for (i = 0; i < 10; i++) 124 for (i = 0; i < 10; i++)
127 tw223_step(tw); 125 lj_math_random_step(rs);
128} 126}
129 127
130/* PRNG extract function. */ 128/* PRNG extract function. */
131LJLIB_PUSH(top-2) /* Upvalue holds userdata with TW223State. */ 129LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */
132LJLIB_CF(math_random) 130LJLIB_CF(math_random) LJLIB_REC(.)
133{ 131{
134 int n = cast_int(L->top - L->base); 132 int n = cast_int(L->top - L->base);
135 TW223State *tw = (TW223State *)(uddata(udataV(lj_lib_upvalue(L, 1)))); 133 RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
134 U64double u;
136 double d; 135 double d;
137 if (LJ_UNLIKELY(!tw->valid)) tw223_init(tw, 0.0); 136 if (LJ_UNLIKELY(!rs->valid)) random_init(rs, 0.0);
138 d = tw223_step(tw) - 1.0; 137 u.u64 = lj_math_random_step(rs);
138 d = u.d - 1.0;
139 if (n > 0) { 139 if (n > 0) {
140 double r1 = lj_lib_checknum(L, 1); 140 double r1 = lj_lib_checknum(L, 1);
141 if (n == 1) { 141 if (n == 1) {
@@ -150,11 +150,11 @@ LJLIB_CF(math_random)
150} 150}
151 151
152/* PRNG seed function. */ 152/* PRNG seed function. */
153LJLIB_PUSH(top-2) /* Upvalue holds userdata with TW223State. */ 153LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */
154LJLIB_CF(math_randomseed) 154LJLIB_CF(math_randomseed)
155{ 155{
156 TW223State *tw = (TW223State *)(uddata(udataV(lj_lib_upvalue(L, 1)))); 156 RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
157 tw223_init(tw, lj_lib_checknum(L, 1)); 157 random_init(rs, lj_lib_checknum(L, 1));
158 return 0; 158 return 0;
159} 159}
160 160
@@ -164,9 +164,9 @@ LJLIB_CF(math_randomseed)
164 164
165LUALIB_API int luaopen_math(lua_State *L) 165LUALIB_API int luaopen_math(lua_State *L)
166{ 166{
167 TW223State *tw; 167 RandomState *rs;
168 tw = (TW223State *)lua_newuserdata(L, sizeof(TW223State)); 168 rs = (RandomState *)lua_newuserdata(L, sizeof(RandomState));
169 tw->valid = 0; /* Use lazy initialization to save some time on startup. */ 169 rs->valid = 0; /* Use lazy initialization to save some time on startup. */
170 LJ_LIB_REG(L, math); 170 LJ_LIB_REG(L, math);
171#if defined(LUA_COMPAT_MOD) 171#if defined(LUA_COMPAT_MOD)
172 lua_getfield(L, -1, "fmod"); 172 lua_getfield(L, -1, "fmod");