summaryrefslogtreecommitdiff
path: root/src/lib_math.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib_math.c188
1 files changed, 188 insertions, 0 deletions
diff --git a/src/lib_math.c b/src/lib_math.c
new file mode 100644
index 00000000..ec8b0c2b
--- /dev/null
+++ b/src/lib_math.c
@@ -0,0 +1,188 @@
1/*
2** Math library.
3** Copyright (C) 2005-2009 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#include <math.h>
7
8#define lib_math_c
9#define LUA_LIB
10
11#include "lua.h"
12#include "lauxlib.h"
13#include "lualib.h"
14
15#include "lj_obj.h"
16#include "lj_lib.h"
17
18/* ------------------------------------------------------------------------ */
19
20#define LJLIB_MODULE_math
21
22LJLIB_ASM(math_abs) LJLIB_REC(.)
23{
24 lj_lib_checknum(L, 1);
25 return FFH_RETRY;
26}
27LJLIB_ASM_(math_floor) LJLIB_REC(math_round IRFPM_FLOOR)
28LJLIB_ASM_(math_ceil) LJLIB_REC(math_round IRFPM_CEIL)
29LJLIB_ASM_(math_sqrt) LJLIB_REC(math_unary IRFPM_SQRT)
30LJLIB_ASM_(math_log) LJLIB_REC(math_unary IRFPM_LOG)
31LJLIB_ASM_(math_log10) LJLIB_REC(math_unary IRFPM_LOG10)
32LJLIB_ASM_(math_exp) LJLIB_REC(math_unary IRFPM_EXP)
33LJLIB_ASM_(math_sin) LJLIB_REC(math_unary IRFPM_SIN)
34LJLIB_ASM_(math_cos) LJLIB_REC(math_unary IRFPM_COS)
35LJLIB_ASM_(math_tan) LJLIB_REC(math_unary IRFPM_TAN)
36LJLIB_ASM_(math_asin) LJLIB_REC(math_atrig FF_math_asin)
37LJLIB_ASM_(math_acos) LJLIB_REC(math_atrig FF_math_acos)
38LJLIB_ASM_(math_atan) LJLIB_REC(math_atrig FF_math_atan)
39LJLIB_ASM_(math_sinh)
40LJLIB_ASM_(math_cosh)
41LJLIB_ASM_(math_tanh)
42LJLIB_ASM_(math_frexp)
43LJLIB_ASM_(math_modf) LJLIB_REC(.)
44
45LJLIB_PUSH(57.29577951308232)
46LJLIB_ASM_(math_deg) LJLIB_REC(math_degrad)
47
48LJLIB_PUSH(0.017453292519943295)
49LJLIB_ASM_(math_rad) LJLIB_REC(math_degrad)
50
51LJLIB_ASM(math_atan2) LJLIB_REC(math_binary IR_ATAN2)
52{
53 lj_lib_checknum(L, 1);
54 lj_lib_checknum(L, 2);
55 return FFH_RETRY;
56}
57LJLIB_ASM_(math_ldexp) LJLIB_REC(math_binary IR_LDEXP)
58LJLIB_ASM_(math_pow) LJLIB_REC(.)
59LJLIB_ASM_(math_fmod)
60
61LJLIB_ASM(math_min) LJLIB_REC(math_minmax IR_MIN)
62{
63 int i = 0;
64 do { lj_lib_checknum(L, ++i); } while (L->base+i < L->top);
65 return FFH_RETRY;
66}
67LJLIB_ASM_(math_max) LJLIB_REC(math_minmax IR_MAX)
68
69LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi)
70LJLIB_PUSH(1e310) LJLIB_SET(huge)
71
72#ifdef __MACH__
73LJ_FUNCA double lj_wrapper_sinh(double x) { return sinh(x); }
74LJ_FUNCA double lj_wrapper_cosh(double x) { return cosh(x); }
75LJ_FUNCA double lj_wrapper_tanh(double x) { return tanh(x); }
76#endif
77
78/* ------------------------------------------------------------------------ */
79
80/* This implements a Tausworthe PRNG with period 2^223. Based on:
81** Tables of maximally-equidistributed combined LFSR generators,
82** Pierre L'Ecuyer, 1991, table 3, 1st entry.
83** Full-period ME-CF generator with L=64, J=4, k=223, N1=49.
84*/
85
86/* PRNG state. */
87typedef struct TW223State {
88 uint64_t gen[4]; /* State of the 4 LFSR generators. */
89 int valid; /* State is valid. */
90} TW223State;
91
92/* Union needed for bit-pattern conversion between uint64_t and double. */
93typedef union { uint64_t u64; double d; } U64double;
94
95/* Update generator i and compute a running xor of all states. */
96#define TW223_GEN(i, k, q, s) \
97 z = tw->gen[i]; \
98 z = (((z<<q)^z) >> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<<s); \
99 r ^= z; tw->gen[i] = z;
100
101/* PRNG step function. Returns a double in the range 0.0 <= d < 1.0. */
102static double tw223_step(TW223State *tw)
103{
104 uint64_t z, r = 0;
105 U64double u;
106 TW223_GEN(0, 63, 31, 18)
107 TW223_GEN(1, 58, 19, 28)
108 TW223_GEN(2, 55, 24, 7)
109 TW223_GEN(3, 47, 21, 8)
110 u.u64 = (r & (((uint64_t)1 << 52)-1)) | ((uint64_t)0x3ff << 52);
111#if defined(__GNUC__) && LJ_TARGET_X86 && __pic__
112 /* Compensate for unbelievable GCC pessimization. */
113 {
114 volatile U64double u1;
115 u1.u64 = (uint64_t)0x3f8 << 52;
116 return u.d - u1.d;
117 }
118#else
119 return u.d - 1.0;
120#endif
121}
122
123/* PRNG initialization function. */
124static void tw223_init(TW223State *tw, double d)
125{
126 uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */
127 int i;
128 for (i = 0; i < 4; i++) {
129 U64double u;
130 uint32_t m = 1u << (r&255);
131 r >>= 8;
132 u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354;
133 if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of gen[i] are non-zero. */
134 tw->gen[i] = u.u64;
135 }
136 tw->valid = 1;
137 for (i = 0; i < 10; i++)
138 tw223_step(tw);
139}
140
141/* PRNG extract function. */
142LJLIB_PUSH(top-2) /* Upvalue holds userdata with TW223State. */
143LJLIB_CF(math_random)
144{
145 int n = cast_int(L->top - L->base);
146 TW223State *tw = (TW223State *)(uddata(udataV(lj_lib_upvalue(L, 1))));
147 double d;
148 if (LJ_UNLIKELY(!tw->valid)) tw223_init(tw, 0.0);
149 d = tw223_step(tw);
150 if (n > 0) {
151 double r1 = lj_lib_checknum(L, 1);
152 if (n == 1) {
153 d = floor(d*r1) + 1.0; /* d is an int in range [1, r1] */
154 } else {
155 double r2 = lj_lib_checknum(L, 2);
156 d = floor(d*(r2-r1+1.0)) + r1; /* d is an int in range [r1, r2] */
157 }
158 } /* else: d is a double in range [0, 1] */
159 setnumV(L->top++, d);
160 return 1;
161}
162
163/* PRNG seed function. */
164LJLIB_PUSH(top-2) /* Upvalue holds userdata with TW223State. */
165LJLIB_CF(math_randomseed)
166{
167 TW223State *tw = (TW223State *)(uddata(udataV(lj_lib_upvalue(L, 1))));
168 tw223_init(tw, lj_lib_checknum(L, 1));
169 return 0;
170}
171
172/* ------------------------------------------------------------------------ */
173
174#include "lj_libdef.h"
175
176LUALIB_API int luaopen_math(lua_State *L)
177{
178 TW223State *tw;
179 tw = (TW223State *)lua_newuserdata(L, sizeof(TW223State));
180 tw->valid = 0; /* Use lazy initialization to save some time on startup. */
181 LJ_LIB_REG(L, math);
182#if defined(LUA_COMPAT_MOD)
183 lua_getfield(L, -1, "fmod");
184 lua_setfield(L, -2, "mod");
185#endif
186 return 1;
187}
188