1 /*
2 ** Math library.
3 ** Copyright (C) 2005-2021 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 #include "lj_vm.h"
18 #include "lj_prng.h"
19 
20 /* ------------------------------------------------------------------------ */
21 
22 #define LJLIB_MODULE_math
23 
24 LJLIB_ASM(math_abs)		LJLIB_REC(.)
25 {
26   lj_lib_checknumber(L, 1);
27   return FFH_RETRY;
28 }
LJLIB_REC(math_round IRFPM_FLOOR)29 LJLIB_ASM_(math_floor)		LJLIB_REC(math_round IRFPM_FLOOR)
30 LJLIB_ASM_(math_ceil)		LJLIB_REC(math_round IRFPM_CEIL)
31 
32 LJLIB_ASM(math_sqrt)		LJLIB_REC(math_unary IRFPM_SQRT)
33 {
34   lj_lib_checknum(L, 1);
35   return FFH_RETRY;
36 }
LJLIB_REC(math_call IRCALL_log10)37 LJLIB_ASM_(math_log10)		LJLIB_REC(math_call IRCALL_log10)
38 LJLIB_ASM_(math_exp)		LJLIB_REC(math_call IRCALL_exp)
39 LJLIB_ASM_(math_sin)		LJLIB_REC(math_call IRCALL_sin)
40 LJLIB_ASM_(math_cos)		LJLIB_REC(math_call IRCALL_cos)
41 LJLIB_ASM_(math_tan)		LJLIB_REC(math_call IRCALL_tan)
42 LJLIB_ASM_(math_asin)		LJLIB_REC(math_call IRCALL_asin)
43 LJLIB_ASM_(math_acos)		LJLIB_REC(math_call IRCALL_acos)
44 LJLIB_ASM_(math_atan)		LJLIB_REC(math_call IRCALL_atan)
45 LJLIB_ASM_(math_sinh)		LJLIB_REC(math_call IRCALL_sinh)
46 LJLIB_ASM_(math_cosh)		LJLIB_REC(math_call IRCALL_cosh)
47 LJLIB_ASM_(math_tanh)		LJLIB_REC(math_call IRCALL_tanh)
48 LJLIB_ASM_(math_frexp)
49 LJLIB_ASM_(math_modf)
50 
51 LJLIB_ASM(math_log)		LJLIB_REC(math_log)
52 {
53   double x = lj_lib_checknum(L, 1);
54   if (L->base+1 < L->top) {
55     double y = lj_lib_checknum(L, 2);
56 #ifdef LUAJIT_NO_LOG2
57     x = log(x); y = 1.0 / log(y);
58 #else
59     x = lj_vm_log2(x); y = 1.0 / lj_vm_log2(y);
60 #endif
61     setnumV(L->base-1-LJ_FR2, x*y);  /* Do NOT join the expression to x / y. */
62     return FFH_RES(1);
63   }
64   return FFH_RETRY;
65 }
66 
67 LJLIB_LUA(math_deg) /* function(x) return x * 57.29577951308232 end */
LJLIB_LUA(math_rad)68 LJLIB_LUA(math_rad) /* function(x) return x * 0.017453292519943295 end */
69 
70 LJLIB_ASM(math_atan2)		LJLIB_REC(.)
71 {
72   lj_lib_checknum(L, 1);
73   lj_lib_checknum(L, 2);
74   return FFH_RETRY;
75 }
76 LJLIB_ASM_(math_pow)		LJLIB_REC(.)
LJLIB_ASM_(math_fmod)77 LJLIB_ASM_(math_fmod)
78 
79 LJLIB_ASM(math_ldexp)		LJLIB_REC(.)
80 {
81   lj_lib_checknum(L, 1);
82 #if LJ_DUALNUM && !LJ_TARGET_X86ORX64
83   lj_lib_checkint(L, 2);
84 #else
85   lj_lib_checknum(L, 2);
86 #endif
87   return FFH_RETRY;
88 }
89 
LJLIB_REC(math_minmax IR_MIN)90 LJLIB_ASM(math_min)		LJLIB_REC(math_minmax IR_MIN)
91 {
92   int i = 0;
93   do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top);
94   return FFH_RETRY;
95 }
96 LJLIB_ASM_(math_max)		LJLIB_REC(math_minmax IR_MAX)
97 
98 LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi)
99 LJLIB_PUSH(1e310) LJLIB_SET(huge)
100 
101 /* ------------------------------------------------------------------------ */
102 
103 /* This implements a Tausworthe PRNG with period 2^223. Based on:
104 **   Tables of maximally-equidistributed combined LFSR generators,
105 **   Pierre L'Ecuyer, 1991, table 3, 1st entry.
106 ** Full-period ME-CF generator with L=64, J=4, k=223, N1=49.
107 */
108 
109 /* Union needed for bit-pattern conversion between uint64_t and double. */
110 typedef union { uint64_t u64; double d; } U64double;
111 
112 /* PRNG seeding function. */
random_seed(PRNGState * rs,double d)113 static void random_seed(PRNGState *rs, double d)
114 {
115   uint32_t r = 0x11090601;  /* 64-k[i] as four 8 bit constants. */
116   int i;
117   for (i = 0; i < 4; i++) {
118     U64double u;
119     uint32_t m = 1u << (r&255);
120     r >>= 8;
121     u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354;
122     if (u.u64 < m) u.u64 += m;  /* Ensure k[i] MSB of u[i] are non-zero. */
123     rs->u[i] = u.u64;
124   }
125   for (i = 0; i < 10; i++)
126     (void)lj_prng_u64(rs);
127 }
128 
129 /* PRNG extract function. */
130 LJLIB_PUSH(top-2)  /* Upvalue holds userdata with PRNGState. */
LJLIB_CF(math_random)131 LJLIB_CF(math_random)		LJLIB_REC(.)
132 {
133   int n = (int)(L->top - L->base);
134   PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
135   U64double u;
136   double d;
137   u.u64 = lj_prng_u64d(rs);
138   d = u.d - 1.0;
139   if (n > 0) {
140 #if LJ_DUALNUM
141     int isint = 1;
142     double r1;
143     lj_lib_checknumber(L, 1);
144     if (tvisint(L->base)) {
145       r1 = (lua_Number)intV(L->base);
146     } else {
147       isint = 0;
148       r1 = numV(L->base);
149     }
150 #else
151     double r1 = lj_lib_checknum(L, 1);
152 #endif
153     if (n == 1) {
154       d = lj_vm_floor(d*r1) + 1.0;  /* d is an int in range [1, r1] */
155     } else {
156 #if LJ_DUALNUM
157       double r2;
158       lj_lib_checknumber(L, 2);
159       if (tvisint(L->base+1)) {
160 	r2 = (lua_Number)intV(L->base+1);
161       } else {
162 	isint = 0;
163 	r2 = numV(L->base+1);
164       }
165 #else
166       double r2 = lj_lib_checknum(L, 2);
167 #endif
168       d = lj_vm_floor(d*(r2-r1+1.0)) + r1;  /* d is an int in range [r1, r2] */
169     }
170 #if LJ_DUALNUM
171     if (isint) {
172       setintV(L->top-1, lj_num2int(d));
173       return 1;
174     }
175 #endif
176   }  /* else: d is a double in range [0, 1] */
177   setnumV(L->top++, d);
178   return 1;
179 }
180 
181 /* PRNG seed function. */
182 LJLIB_PUSH(top-2)  /* Upvalue holds userdata with PRNGState. */
LJLIB_CF(math_randomseed)183 LJLIB_CF(math_randomseed)
184 {
185   PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
186   random_seed(rs, lj_lib_checknum(L, 1));
187   return 0;
188 }
189 
190 /* ------------------------------------------------------------------------ */
191 
192 #include "lj_libdef.h"
193 
luaopen_math(lua_State * L)194 LUALIB_API int luaopen_math(lua_State *L)
195 {
196   PRNGState *rs = (PRNGState *)lua_newuserdata(L, sizeof(PRNGState));
197   lj_prng_seed_fixed(rs);
198   LJ_LIB_REG(L, LUA_MATHLIBNAME, math);
199   return 1;
200 }
201 
202