FFI: Add unwind definitions for lj_vm_ffi_call.
[luajit-2.0/celess22.git] / src / lib_math.c
blob599f948e985424f8d56d5b81086dbfb346455090
1 /*
2 ** Math library.
3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #include <math.h>
8 #define lib_math_c
9 #define LUA_LIB
11 #include "lua.h"
12 #include "lauxlib.h"
13 #include "lualib.h"
15 #include "lj_obj.h"
16 #include "lj_lib.h"
17 #include "lj_vm.h"
19 /* ------------------------------------------------------------------------ */
21 #define LJLIB_MODULE_math
23 LJLIB_ASM(math_abs) LJLIB_REC(.)
25 lj_lib_checknumber(L, 1);
26 return FFH_RETRY;
28 LJLIB_ASM_(math_floor) LJLIB_REC(math_round IRFPM_FLOOR)
29 LJLIB_ASM_(math_ceil) LJLIB_REC(math_round IRFPM_CEIL)
31 LJLIB_ASM(math_sqrt) LJLIB_REC(math_unary IRFPM_SQRT)
33 lj_lib_checknum(L, 1);
34 return FFH_RETRY;
36 LJLIB_ASM_(math_log) LJLIB_REC(math_unary IRFPM_LOG)
37 LJLIB_ASM_(math_log10) LJLIB_REC(math_unary IRFPM_LOG10)
38 LJLIB_ASM_(math_exp) LJLIB_REC(math_unary IRFPM_EXP)
39 LJLIB_ASM_(math_sin) LJLIB_REC(math_unary IRFPM_SIN)
40 LJLIB_ASM_(math_cos) LJLIB_REC(math_unary IRFPM_COS)
41 LJLIB_ASM_(math_tan) LJLIB_REC(math_unary IRFPM_TAN)
42 LJLIB_ASM_(math_asin) LJLIB_REC(math_atrig FF_math_asin)
43 LJLIB_ASM_(math_acos) LJLIB_REC(math_atrig FF_math_acos)
44 LJLIB_ASM_(math_atan) LJLIB_REC(math_atrig FF_math_atan)
45 LJLIB_ASM_(math_sinh) LJLIB_REC(math_htrig IRCALL_sinh)
46 LJLIB_ASM_(math_cosh) LJLIB_REC(math_htrig IRCALL_cosh)
47 LJLIB_ASM_(math_tanh) LJLIB_REC(math_htrig IRCALL_tanh)
48 LJLIB_ASM_(math_frexp)
49 LJLIB_ASM_(math_modf) LJLIB_REC(.)
51 LJLIB_PUSH(57.29577951308232)
52 LJLIB_ASM_(math_deg) LJLIB_REC(math_degrad)
54 LJLIB_PUSH(0.017453292519943295)
55 LJLIB_ASM_(math_rad) LJLIB_REC(math_degrad)
57 LJLIB_ASM(math_atan2) LJLIB_REC(.)
59 lj_lib_checknum(L, 1);
60 lj_lib_checknum(L, 2);
61 return FFH_RETRY;
63 LJLIB_ASM_(math_pow) LJLIB_REC(.)
64 LJLIB_ASM_(math_fmod)
66 LJLIB_ASM(math_ldexp) LJLIB_REC(.)
68 lj_lib_checknum(L, 1);
69 #if LJ_DUALNUM && !LJ_TARGET_X86ORX64
70 lj_lib_checkint(L, 2);
71 #else
72 lj_lib_checknum(L, 2);
73 #endif
74 return FFH_RETRY;
77 LJLIB_ASM(math_min) LJLIB_REC(math_minmax IR_MIN)
79 int i = 0;
80 do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top);
81 return FFH_RETRY;
83 LJLIB_ASM_(math_max) LJLIB_REC(math_minmax IR_MAX)
85 LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi)
86 LJLIB_PUSH(1e310) LJLIB_SET(huge)
88 /* ------------------------------------------------------------------------ */
90 /* This implements a Tausworthe PRNG with period 2^223. Based on:
91 ** Tables of maximally-equidistributed combined LFSR generators,
92 ** Pierre L'Ecuyer, 1991, table 3, 1st entry.
93 ** Full-period ME-CF generator with L=64, J=4, k=223, N1=49.
96 /* PRNG state. */
97 struct RandomState {
98 uint64_t gen[4]; /* State of the 4 LFSR generators. */
99 int valid; /* State is valid. */
102 /* Union needed for bit-pattern conversion between uint64_t and double. */
103 typedef union { uint64_t u64; double d; } U64double;
105 /* Update generator i and compute a running xor of all states. */
106 #define TW223_GEN(i, k, q, s) \
107 z = rs->gen[i]; \
108 z = (((z<<q)^z) >> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<<s); \
109 r ^= z; rs->gen[i] = z;
111 /* PRNG step function. Returns a double in the range 1.0 <= d < 2.0. */
112 LJ_NOINLINE uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs)
114 uint64_t z, r = 0;
115 TW223_GEN(0, 63, 31, 18)
116 TW223_GEN(1, 58, 19, 28)
117 TW223_GEN(2, 55, 24, 7)
118 TW223_GEN(3, 47, 21, 8)
119 return (r & U64x(000fffff,ffffffff)) | U64x(3ff00000,00000000);
122 /* PRNG initialization function. */
123 static void random_init(RandomState *rs, double d)
125 uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */
126 int i;
127 for (i = 0; i < 4; i++) {
128 U64double u;
129 uint32_t m = 1u << (r&255);
130 r >>= 8;
131 u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354;
132 if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of gen[i] are non-zero. */
133 rs->gen[i] = u.u64;
135 rs->valid = 1;
136 for (i = 0; i < 10; i++)
137 lj_math_random_step(rs);
140 /* PRNG extract function. */
141 LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */
142 LJLIB_CF(math_random) LJLIB_REC(.)
144 int n = (int)(L->top - L->base);
145 RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
146 U64double u;
147 double d;
148 if (LJ_UNLIKELY(!rs->valid)) random_init(rs, 0.0);
149 u.u64 = lj_math_random_step(rs);
150 d = u.d - 1.0;
151 if (n > 0) {
152 #if LJ_DUALNUM
153 int isint = 1;
154 double r1;
155 lj_lib_checknumber(L, 1);
156 if (tvisint(L->base)) {
157 r1 = (lua_Number)intV(L->base);
158 } else {
159 isint = 0;
160 r1 = numV(L->base);
162 #else
163 double r1 = lj_lib_checknum(L, 1);
164 #endif
165 if (n == 1) {
166 d = lj_vm_floor(d*r1) + 1.0; /* d is an int in range [1, r1] */
167 } else {
168 #if LJ_DUALNUM
169 double r2;
170 lj_lib_checknumber(L, 2);
171 if (tvisint(L->base+1)) {
172 r2 = (lua_Number)intV(L->base+1);
173 } else {
174 isint = 0;
175 r2 = numV(L->base+1);
177 #else
178 double r2 = lj_lib_checknum(L, 2);
179 #endif
180 d = lj_vm_floor(d*(r2-r1+1.0)) + r1; /* d is an int in range [r1, r2] */
182 #if LJ_DUALNUM
183 if (isint) {
184 setintV(L->top-1, lj_num2int(d));
185 return 1;
187 #endif
188 } /* else: d is a double in range [0, 1] */
189 setnumV(L->top++, d);
190 return 1;
193 /* PRNG seed function. */
194 LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */
195 LJLIB_CF(math_randomseed)
197 RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
198 random_init(rs, lj_lib_checknum(L, 1));
199 return 0;
202 /* ------------------------------------------------------------------------ */
204 #include "lj_libdef.h"
206 LUALIB_API int luaopen_math(lua_State *L)
208 RandomState *rs;
209 rs = (RandomState *)lua_newuserdata(L, sizeof(RandomState));
210 rs->valid = 0; /* Use lazy initialization to save some time on startup. */
211 LJ_LIB_REG(L, LUA_MATHLIBNAME, math);
212 #if defined(LUA_COMPAT_MOD)
213 lua_getfield(L, -1, "fmod");
214 lua_setfield(L, -2, "mod");
215 #endif
216 return 1;