1*088332b5SXin Li /*
2*088332b5SXin Li ** $Id: lmathlib.c $
3*088332b5SXin Li ** Standard mathematical library
4*088332b5SXin Li ** See Copyright Notice in lua.h
5*088332b5SXin Li */
6*088332b5SXin Li
7*088332b5SXin Li #define lmathlib_c
8*088332b5SXin Li #define LUA_LIB
9*088332b5SXin Li
10*088332b5SXin Li #include "lprefix.h"
11*088332b5SXin Li
12*088332b5SXin Li
13*088332b5SXin Li #include <float.h>
14*088332b5SXin Li #include <limits.h>
15*088332b5SXin Li #include <math.h>
16*088332b5SXin Li #include <stdlib.h>
17*088332b5SXin Li #include <time.h>
18*088332b5SXin Li
19*088332b5SXin Li #include "lua.h"
20*088332b5SXin Li
21*088332b5SXin Li #include "lauxlib.h"
22*088332b5SXin Li #include "lualib.h"
23*088332b5SXin Li
24*088332b5SXin Li
25*088332b5SXin Li #undef PI
26*088332b5SXin Li #define PI (l_mathop(3.141592653589793238462643383279502884))
27*088332b5SXin Li
28*088332b5SXin Li
math_abs(lua_State * L)29*088332b5SXin Li static int math_abs (lua_State *L) {
30*088332b5SXin Li if (lua_isinteger(L, 1)) {
31*088332b5SXin Li lua_Integer n = lua_tointeger(L, 1);
32*088332b5SXin Li if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
33*088332b5SXin Li lua_pushinteger(L, n);
34*088332b5SXin Li }
35*088332b5SXin Li else
36*088332b5SXin Li lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
37*088332b5SXin Li return 1;
38*088332b5SXin Li }
39*088332b5SXin Li
math_sin(lua_State * L)40*088332b5SXin Li static int math_sin (lua_State *L) {
41*088332b5SXin Li lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
42*088332b5SXin Li return 1;
43*088332b5SXin Li }
44*088332b5SXin Li
math_cos(lua_State * L)45*088332b5SXin Li static int math_cos (lua_State *L) {
46*088332b5SXin Li lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
47*088332b5SXin Li return 1;
48*088332b5SXin Li }
49*088332b5SXin Li
math_tan(lua_State * L)50*088332b5SXin Li static int math_tan (lua_State *L) {
51*088332b5SXin Li lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
52*088332b5SXin Li return 1;
53*088332b5SXin Li }
54*088332b5SXin Li
math_asin(lua_State * L)55*088332b5SXin Li static int math_asin (lua_State *L) {
56*088332b5SXin Li lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
57*088332b5SXin Li return 1;
58*088332b5SXin Li }
59*088332b5SXin Li
math_acos(lua_State * L)60*088332b5SXin Li static int math_acos (lua_State *L) {
61*088332b5SXin Li lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
62*088332b5SXin Li return 1;
63*088332b5SXin Li }
64*088332b5SXin Li
math_atan(lua_State * L)65*088332b5SXin Li static int math_atan (lua_State *L) {
66*088332b5SXin Li lua_Number y = luaL_checknumber(L, 1);
67*088332b5SXin Li lua_Number x = luaL_optnumber(L, 2, 1);
68*088332b5SXin Li lua_pushnumber(L, l_mathop(atan2)(y, x));
69*088332b5SXin Li return 1;
70*088332b5SXin Li }
71*088332b5SXin Li
72*088332b5SXin Li
math_toint(lua_State * L)73*088332b5SXin Li static int math_toint (lua_State *L) {
74*088332b5SXin Li int valid;
75*088332b5SXin Li lua_Integer n = lua_tointegerx(L, 1, &valid);
76*088332b5SXin Li if (valid)
77*088332b5SXin Li lua_pushinteger(L, n);
78*088332b5SXin Li else {
79*088332b5SXin Li luaL_checkany(L, 1);
80*088332b5SXin Li luaL_pushfail(L); /* value is not convertible to integer */
81*088332b5SXin Li }
82*088332b5SXin Li return 1;
83*088332b5SXin Li }
84*088332b5SXin Li
85*088332b5SXin Li
pushnumint(lua_State * L,lua_Number d)86*088332b5SXin Li static void pushnumint (lua_State *L, lua_Number d) {
87*088332b5SXin Li lua_Integer n;
88*088332b5SXin Li if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
89*088332b5SXin Li lua_pushinteger(L, n); /* result is integer */
90*088332b5SXin Li else
91*088332b5SXin Li lua_pushnumber(L, d); /* result is float */
92*088332b5SXin Li }
93*088332b5SXin Li
94*088332b5SXin Li
math_floor(lua_State * L)95*088332b5SXin Li static int math_floor (lua_State *L) {
96*088332b5SXin Li if (lua_isinteger(L, 1))
97*088332b5SXin Li lua_settop(L, 1); /* integer is its own floor */
98*088332b5SXin Li else {
99*088332b5SXin Li lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
100*088332b5SXin Li pushnumint(L, d);
101*088332b5SXin Li }
102*088332b5SXin Li return 1;
103*088332b5SXin Li }
104*088332b5SXin Li
105*088332b5SXin Li
math_ceil(lua_State * L)106*088332b5SXin Li static int math_ceil (lua_State *L) {
107*088332b5SXin Li if (lua_isinteger(L, 1))
108*088332b5SXin Li lua_settop(L, 1); /* integer is its own ceil */
109*088332b5SXin Li else {
110*088332b5SXin Li lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
111*088332b5SXin Li pushnumint(L, d);
112*088332b5SXin Li }
113*088332b5SXin Li return 1;
114*088332b5SXin Li }
115*088332b5SXin Li
116*088332b5SXin Li
math_fmod(lua_State * L)117*088332b5SXin Li static int math_fmod (lua_State *L) {
118*088332b5SXin Li if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
119*088332b5SXin Li lua_Integer d = lua_tointeger(L, 2);
120*088332b5SXin Li if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
121*088332b5SXin Li luaL_argcheck(L, d != 0, 2, "zero");
122*088332b5SXin Li lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
123*088332b5SXin Li }
124*088332b5SXin Li else
125*088332b5SXin Li lua_pushinteger(L, lua_tointeger(L, 1) % d);
126*088332b5SXin Li }
127*088332b5SXin Li else
128*088332b5SXin Li lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
129*088332b5SXin Li luaL_checknumber(L, 2)));
130*088332b5SXin Li return 1;
131*088332b5SXin Li }
132*088332b5SXin Li
133*088332b5SXin Li
134*088332b5SXin Li /*
135*088332b5SXin Li ** next function does not use 'modf', avoiding problems with 'double*'
136*088332b5SXin Li ** (which is not compatible with 'float*') when lua_Number is not
137*088332b5SXin Li ** 'double'.
138*088332b5SXin Li */
math_modf(lua_State * L)139*088332b5SXin Li static int math_modf (lua_State *L) {
140*088332b5SXin Li if (lua_isinteger(L ,1)) {
141*088332b5SXin Li lua_settop(L, 1); /* number is its own integer part */
142*088332b5SXin Li lua_pushnumber(L, 0); /* no fractional part */
143*088332b5SXin Li }
144*088332b5SXin Li else {
145*088332b5SXin Li lua_Number n = luaL_checknumber(L, 1);
146*088332b5SXin Li /* integer part (rounds toward zero) */
147*088332b5SXin Li lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
148*088332b5SXin Li pushnumint(L, ip);
149*088332b5SXin Li /* fractional part (test needed for inf/-inf) */
150*088332b5SXin Li lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
151*088332b5SXin Li }
152*088332b5SXin Li return 2;
153*088332b5SXin Li }
154*088332b5SXin Li
155*088332b5SXin Li
math_sqrt(lua_State * L)156*088332b5SXin Li static int math_sqrt (lua_State *L) {
157*088332b5SXin Li lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
158*088332b5SXin Li return 1;
159*088332b5SXin Li }
160*088332b5SXin Li
161*088332b5SXin Li
math_ult(lua_State * L)162*088332b5SXin Li static int math_ult (lua_State *L) {
163*088332b5SXin Li lua_Integer a = luaL_checkinteger(L, 1);
164*088332b5SXin Li lua_Integer b = luaL_checkinteger(L, 2);
165*088332b5SXin Li lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
166*088332b5SXin Li return 1;
167*088332b5SXin Li }
168*088332b5SXin Li
math_log(lua_State * L)169*088332b5SXin Li static int math_log (lua_State *L) {
170*088332b5SXin Li lua_Number x = luaL_checknumber(L, 1);
171*088332b5SXin Li lua_Number res;
172*088332b5SXin Li if (lua_isnoneornil(L, 2))
173*088332b5SXin Li res = l_mathop(log)(x);
174*088332b5SXin Li else {
175*088332b5SXin Li lua_Number base = luaL_checknumber(L, 2);
176*088332b5SXin Li #if !defined(LUA_USE_C89)
177*088332b5SXin Li if (base == l_mathop(2.0))
178*088332b5SXin Li res = l_mathop(log2)(x); else
179*088332b5SXin Li #endif
180*088332b5SXin Li if (base == l_mathop(10.0))
181*088332b5SXin Li res = l_mathop(log10)(x);
182*088332b5SXin Li else
183*088332b5SXin Li res = l_mathop(log)(x)/l_mathop(log)(base);
184*088332b5SXin Li }
185*088332b5SXin Li lua_pushnumber(L, res);
186*088332b5SXin Li return 1;
187*088332b5SXin Li }
188*088332b5SXin Li
math_exp(lua_State * L)189*088332b5SXin Li static int math_exp (lua_State *L) {
190*088332b5SXin Li lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
191*088332b5SXin Li return 1;
192*088332b5SXin Li }
193*088332b5SXin Li
math_deg(lua_State * L)194*088332b5SXin Li static int math_deg (lua_State *L) {
195*088332b5SXin Li lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
196*088332b5SXin Li return 1;
197*088332b5SXin Li }
198*088332b5SXin Li
math_rad(lua_State * L)199*088332b5SXin Li static int math_rad (lua_State *L) {
200*088332b5SXin Li lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
201*088332b5SXin Li return 1;
202*088332b5SXin Li }
203*088332b5SXin Li
204*088332b5SXin Li
math_min(lua_State * L)205*088332b5SXin Li static int math_min (lua_State *L) {
206*088332b5SXin Li int n = lua_gettop(L); /* number of arguments */
207*088332b5SXin Li int imin = 1; /* index of current minimum value */
208*088332b5SXin Li int i;
209*088332b5SXin Li luaL_argcheck(L, n >= 1, 1, "value expected");
210*088332b5SXin Li for (i = 2; i <= n; i++) {
211*088332b5SXin Li if (lua_compare(L, i, imin, LUA_OPLT))
212*088332b5SXin Li imin = i;
213*088332b5SXin Li }
214*088332b5SXin Li lua_pushvalue(L, imin);
215*088332b5SXin Li return 1;
216*088332b5SXin Li }
217*088332b5SXin Li
218*088332b5SXin Li
math_max(lua_State * L)219*088332b5SXin Li static int math_max (lua_State *L) {
220*088332b5SXin Li int n = lua_gettop(L); /* number of arguments */
221*088332b5SXin Li int imax = 1; /* index of current maximum value */
222*088332b5SXin Li int i;
223*088332b5SXin Li luaL_argcheck(L, n >= 1, 1, "value expected");
224*088332b5SXin Li for (i = 2; i <= n; i++) {
225*088332b5SXin Li if (lua_compare(L, imax, i, LUA_OPLT))
226*088332b5SXin Li imax = i;
227*088332b5SXin Li }
228*088332b5SXin Li lua_pushvalue(L, imax);
229*088332b5SXin Li return 1;
230*088332b5SXin Li }
231*088332b5SXin Li
232*088332b5SXin Li
math_type(lua_State * L)233*088332b5SXin Li static int math_type (lua_State *L) {
234*088332b5SXin Li if (lua_type(L, 1) == LUA_TNUMBER)
235*088332b5SXin Li lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float");
236*088332b5SXin Li else {
237*088332b5SXin Li luaL_checkany(L, 1);
238*088332b5SXin Li luaL_pushfail(L);
239*088332b5SXin Li }
240*088332b5SXin Li return 1;
241*088332b5SXin Li }
242*088332b5SXin Li
243*088332b5SXin Li
244*088332b5SXin Li
245*088332b5SXin Li /*
246*088332b5SXin Li ** {==================================================================
247*088332b5SXin Li ** Pseudo-Random Number Generator based on 'xoshiro256**'.
248*088332b5SXin Li ** ===================================================================
249*088332b5SXin Li */
250*088332b5SXin Li
251*088332b5SXin Li /* number of binary digits in the mantissa of a float */
252*088332b5SXin Li #define FIGS l_floatatt(MANT_DIG)
253*088332b5SXin Li
254*088332b5SXin Li #if FIGS > 64
255*088332b5SXin Li /* there are only 64 random bits; use them all */
256*088332b5SXin Li #undef FIGS
257*088332b5SXin Li #define FIGS 64
258*088332b5SXin Li #endif
259*088332b5SXin Li
260*088332b5SXin Li
261*088332b5SXin Li /*
262*088332b5SXin Li ** LUA_RAND32 forces the use of 32-bit integers in the implementation
263*088332b5SXin Li ** of the PRN generator (mainly for testing).
264*088332b5SXin Li */
265*088332b5SXin Li #if !defined(LUA_RAND32) && !defined(Rand64)
266*088332b5SXin Li
267*088332b5SXin Li /* try to find an integer type with at least 64 bits */
268*088332b5SXin Li
269*088332b5SXin Li #if (ULONG_MAX >> 31 >> 31) >= 3
270*088332b5SXin Li
271*088332b5SXin Li /* 'long' has at least 64 bits */
272*088332b5SXin Li #define Rand64 unsigned long
273*088332b5SXin Li
274*088332b5SXin Li #elif !defined(LUA_USE_C89) && defined(LLONG_MAX)
275*088332b5SXin Li
276*088332b5SXin Li /* there is a 'long long' type (which must have at least 64 bits) */
277*088332b5SXin Li #define Rand64 unsigned long long
278*088332b5SXin Li
279*088332b5SXin Li #elif (LUA_MAXUNSIGNED >> 31 >> 31) >= 3
280*088332b5SXin Li
281*088332b5SXin Li /* 'lua_Integer' has at least 64 bits */
282*088332b5SXin Li #define Rand64 lua_Unsigned
283*088332b5SXin Li
284*088332b5SXin Li #endif
285*088332b5SXin Li
286*088332b5SXin Li #endif
287*088332b5SXin Li
288*088332b5SXin Li
289*088332b5SXin Li #if defined(Rand64) /* { */
290*088332b5SXin Li
291*088332b5SXin Li /*
292*088332b5SXin Li ** Standard implementation, using 64-bit integers.
293*088332b5SXin Li ** If 'Rand64' has more than 64 bits, the extra bits do not interfere
294*088332b5SXin Li ** with the 64 initial bits, except in a right shift. Moreover, the
295*088332b5SXin Li ** final result has to discard the extra bits.
296*088332b5SXin Li */
297*088332b5SXin Li
298*088332b5SXin Li /* avoid using extra bits when needed */
299*088332b5SXin Li #define trim64(x) ((x) & 0xffffffffffffffffu)
300*088332b5SXin Li
301*088332b5SXin Li
302*088332b5SXin Li /* rotate left 'x' by 'n' bits */
rotl(Rand64 x,int n)303*088332b5SXin Li static Rand64 rotl (Rand64 x, int n) {
304*088332b5SXin Li return (x << n) | (trim64(x) >> (64 - n));
305*088332b5SXin Li }
306*088332b5SXin Li
nextrand(Rand64 * state)307*088332b5SXin Li static Rand64 nextrand (Rand64 *state) {
308*088332b5SXin Li Rand64 state0 = state[0];
309*088332b5SXin Li Rand64 state1 = state[1];
310*088332b5SXin Li Rand64 state2 = state[2] ^ state0;
311*088332b5SXin Li Rand64 state3 = state[3] ^ state1;
312*088332b5SXin Li Rand64 res = rotl(state1 * 5, 7) * 9;
313*088332b5SXin Li state[0] = state0 ^ state3;
314*088332b5SXin Li state[1] = state1 ^ state2;
315*088332b5SXin Li state[2] = state2 ^ (state1 << 17);
316*088332b5SXin Li state[3] = rotl(state3, 45);
317*088332b5SXin Li return res;
318*088332b5SXin Li }
319*088332b5SXin Li
320*088332b5SXin Li
321*088332b5SXin Li /* must take care to not shift stuff by more than 63 slots */
322*088332b5SXin Li
323*088332b5SXin Li
324*088332b5SXin Li /*
325*088332b5SXin Li ** Convert bits from a random integer into a float in the
326*088332b5SXin Li ** interval [0,1), getting the higher FIG bits from the
327*088332b5SXin Li ** random unsigned integer and converting that to a float.
328*088332b5SXin Li */
329*088332b5SXin Li
330*088332b5SXin Li /* must throw out the extra (64 - FIGS) bits */
331*088332b5SXin Li #define shift64_FIG (64 - FIGS)
332*088332b5SXin Li
333*088332b5SXin Li /* to scale to [0, 1), multiply by scaleFIG = 2^(-FIGS) */
334*088332b5SXin Li #define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1)))
335*088332b5SXin Li
I2d(Rand64 x)336*088332b5SXin Li static lua_Number I2d (Rand64 x) {
337*088332b5SXin Li return (lua_Number)(trim64(x) >> shift64_FIG) * scaleFIG;
338*088332b5SXin Li }
339*088332b5SXin Li
340*088332b5SXin Li /* convert a 'Rand64' to a 'lua_Unsigned' */
341*088332b5SXin Li #define I2UInt(x) ((lua_Unsigned)trim64(x))
342*088332b5SXin Li
343*088332b5SXin Li /* convert a 'lua_Unsigned' to a 'Rand64' */
344*088332b5SXin Li #define Int2I(x) ((Rand64)(x))
345*088332b5SXin Li
346*088332b5SXin Li
347*088332b5SXin Li #else /* no 'Rand64' }{ */
348*088332b5SXin Li
349*088332b5SXin Li /* get an integer with at least 32 bits */
350*088332b5SXin Li #if LUAI_IS32INT
351*088332b5SXin Li typedef unsigned int lu_int32;
352*088332b5SXin Li #else
353*088332b5SXin Li typedef unsigned long lu_int32;
354*088332b5SXin Li #endif
355*088332b5SXin Li
356*088332b5SXin Li
357*088332b5SXin Li /*
358*088332b5SXin Li ** Use two 32-bit integers to represent a 64-bit quantity.
359*088332b5SXin Li */
360*088332b5SXin Li typedef struct Rand64 {
361*088332b5SXin Li lu_int32 h; /* higher half */
362*088332b5SXin Li lu_int32 l; /* lower half */
363*088332b5SXin Li } Rand64;
364*088332b5SXin Li
365*088332b5SXin Li
366*088332b5SXin Li /*
367*088332b5SXin Li ** If 'lu_int32' has more than 32 bits, the extra bits do not interfere
368*088332b5SXin Li ** with the 32 initial bits, except in a right shift and comparisons.
369*088332b5SXin Li ** Moreover, the final result has to discard the extra bits.
370*088332b5SXin Li */
371*088332b5SXin Li
372*088332b5SXin Li /* avoid using extra bits when needed */
373*088332b5SXin Li #define trim32(x) ((x) & 0xffffffffu)
374*088332b5SXin Li
375*088332b5SXin Li
376*088332b5SXin Li /*
377*088332b5SXin Li ** basic operations on 'Rand64' values
378*088332b5SXin Li */
379*088332b5SXin Li
380*088332b5SXin Li /* build a new Rand64 value */
packI(lu_int32 h,lu_int32 l)381*088332b5SXin Li static Rand64 packI (lu_int32 h, lu_int32 l) {
382*088332b5SXin Li Rand64 result;
383*088332b5SXin Li result.h = h;
384*088332b5SXin Li result.l = l;
385*088332b5SXin Li return result;
386*088332b5SXin Li }
387*088332b5SXin Li
388*088332b5SXin Li /* return i << n */
Ishl(Rand64 i,int n)389*088332b5SXin Li static Rand64 Ishl (Rand64 i, int n) {
390*088332b5SXin Li lua_assert(n > 0 && n < 32);
391*088332b5SXin Li return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n);
392*088332b5SXin Li }
393*088332b5SXin Li
394*088332b5SXin Li /* i1 ^= i2 */
Ixor(Rand64 * i1,Rand64 i2)395*088332b5SXin Li static void Ixor (Rand64 *i1, Rand64 i2) {
396*088332b5SXin Li i1->h ^= i2.h;
397*088332b5SXin Li i1->l ^= i2.l;
398*088332b5SXin Li }
399*088332b5SXin Li
400*088332b5SXin Li /* return i1 + i2 */
Iadd(Rand64 i1,Rand64 i2)401*088332b5SXin Li static Rand64 Iadd (Rand64 i1, Rand64 i2) {
402*088332b5SXin Li Rand64 result = packI(i1.h + i2.h, i1.l + i2.l);
403*088332b5SXin Li if (trim32(result.l) < trim32(i1.l)) /* carry? */
404*088332b5SXin Li result.h++;
405*088332b5SXin Li return result;
406*088332b5SXin Li }
407*088332b5SXin Li
408*088332b5SXin Li /* return i * 5 */
times5(Rand64 i)409*088332b5SXin Li static Rand64 times5 (Rand64 i) {
410*088332b5SXin Li return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */
411*088332b5SXin Li }
412*088332b5SXin Li
413*088332b5SXin Li /* return i * 9 */
times9(Rand64 i)414*088332b5SXin Li static Rand64 times9 (Rand64 i) {
415*088332b5SXin Li return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */
416*088332b5SXin Li }
417*088332b5SXin Li
418*088332b5SXin Li /* return 'i' rotated left 'n' bits */
rotl(Rand64 i,int n)419*088332b5SXin Li static Rand64 rotl (Rand64 i, int n) {
420*088332b5SXin Li lua_assert(n > 0 && n < 32);
421*088332b5SXin Li return packI((i.h << n) | (trim32(i.l) >> (32 - n)),
422*088332b5SXin Li (trim32(i.h) >> (32 - n)) | (i.l << n));
423*088332b5SXin Li }
424*088332b5SXin Li
425*088332b5SXin Li /* for offsets larger than 32, rotate right by 64 - offset */
rotl1(Rand64 i,int n)426*088332b5SXin Li static Rand64 rotl1 (Rand64 i, int n) {
427*088332b5SXin Li lua_assert(n > 32 && n < 64);
428*088332b5SXin Li n = 64 - n;
429*088332b5SXin Li return packI((trim32(i.h) >> n) | (i.l << (32 - n)),
430*088332b5SXin Li (i.h << (32 - n)) | (trim32(i.l) >> n));
431*088332b5SXin Li }
432*088332b5SXin Li
433*088332b5SXin Li /*
434*088332b5SXin Li ** implementation of 'xoshiro256**' algorithm on 'Rand64' values
435*088332b5SXin Li */
nextrand(Rand64 * state)436*088332b5SXin Li static Rand64 nextrand (Rand64 *state) {
437*088332b5SXin Li Rand64 res = times9(rotl(times5(state[1]), 7));
438*088332b5SXin Li Rand64 t = Ishl(state[1], 17);
439*088332b5SXin Li Ixor(&state[2], state[0]);
440*088332b5SXin Li Ixor(&state[3], state[1]);
441*088332b5SXin Li Ixor(&state[1], state[2]);
442*088332b5SXin Li Ixor(&state[0], state[3]);
443*088332b5SXin Li Ixor(&state[2], t);
444*088332b5SXin Li state[3] = rotl1(state[3], 45);
445*088332b5SXin Li return res;
446*088332b5SXin Li }
447*088332b5SXin Li
448*088332b5SXin Li
449*088332b5SXin Li /*
450*088332b5SXin Li ** Converts a 'Rand64' into a float.
451*088332b5SXin Li */
452*088332b5SXin Li
453*088332b5SXin Li /* an unsigned 1 with proper type */
454*088332b5SXin Li #define UONE ((lu_int32)1)
455*088332b5SXin Li
456*088332b5SXin Li
457*088332b5SXin Li #if FIGS <= 32
458*088332b5SXin Li
459*088332b5SXin Li /* 2^(-FIGS) */
460*088332b5SXin Li #define scaleFIG (l_mathop(0.5) / (UONE << (FIGS - 1)))
461*088332b5SXin Li
462*088332b5SXin Li /*
463*088332b5SXin Li ** get up to 32 bits from higher half, shifting right to
464*088332b5SXin Li ** throw out the extra bits.
465*088332b5SXin Li */
I2d(Rand64 x)466*088332b5SXin Li static lua_Number I2d (Rand64 x) {
467*088332b5SXin Li lua_Number h = (lua_Number)(trim32(x.h) >> (32 - FIGS));
468*088332b5SXin Li return h * scaleFIG;
469*088332b5SXin Li }
470*088332b5SXin Li
471*088332b5SXin Li #else /* 32 < FIGS <= 64 */
472*088332b5SXin Li
473*088332b5SXin Li /* must take care to not shift stuff by more than 31 slots */
474*088332b5SXin Li
475*088332b5SXin Li /* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */
476*088332b5SXin Li #define scaleFIG \
477*088332b5SXin Li ((lua_Number)1.0 / (UONE << 30) / 8.0 / (UONE << (FIGS - 33)))
478*088332b5SXin Li
479*088332b5SXin Li /*
480*088332b5SXin Li ** use FIGS - 32 bits from lower half, throwing out the other
481*088332b5SXin Li ** (32 - (FIGS - 32)) = (64 - FIGS) bits
482*088332b5SXin Li */
483*088332b5SXin Li #define shiftLOW (64 - FIGS)
484*088332b5SXin Li
485*088332b5SXin Li /*
486*088332b5SXin Li ** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32)
487*088332b5SXin Li */
488*088332b5SXin Li #define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * 2.0)
489*088332b5SXin Li
490*088332b5SXin Li
I2d(Rand64 x)491*088332b5SXin Li static lua_Number I2d (Rand64 x) {
492*088332b5SXin Li lua_Number h = (lua_Number)trim32(x.h) * shiftHI;
493*088332b5SXin Li lua_Number l = (lua_Number)(trim32(x.l) >> shiftLOW);
494*088332b5SXin Li return (h + l) * scaleFIG;
495*088332b5SXin Li }
496*088332b5SXin Li
497*088332b5SXin Li #endif
498*088332b5SXin Li
499*088332b5SXin Li
500*088332b5SXin Li /* convert a 'Rand64' to a 'lua_Unsigned' */
I2UInt(Rand64 x)501*088332b5SXin Li static lua_Unsigned I2UInt (Rand64 x) {
502*088332b5SXin Li return ((lua_Unsigned)trim32(x.h) << 31 << 1) | (lua_Unsigned)trim32(x.l);
503*088332b5SXin Li }
504*088332b5SXin Li
505*088332b5SXin Li /* convert a 'lua_Unsigned' to a 'Rand64' */
Int2I(lua_Unsigned n)506*088332b5SXin Li static Rand64 Int2I (lua_Unsigned n) {
507*088332b5SXin Li return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n);
508*088332b5SXin Li }
509*088332b5SXin Li
510*088332b5SXin Li #endif /* } */
511*088332b5SXin Li
512*088332b5SXin Li
513*088332b5SXin Li /*
514*088332b5SXin Li ** A state uses four 'Rand64' values.
515*088332b5SXin Li */
516*088332b5SXin Li typedef struct {
517*088332b5SXin Li Rand64 s[4];
518*088332b5SXin Li } RanState;
519*088332b5SXin Li
520*088332b5SXin Li
521*088332b5SXin Li /*
522*088332b5SXin Li ** Project the random integer 'ran' into the interval [0, n].
523*088332b5SXin Li ** Because 'ran' has 2^B possible values, the projection can only be
524*088332b5SXin Li ** uniform when the size of the interval is a power of 2 (exact
525*088332b5SXin Li ** division). Otherwise, to get a uniform projection into [0, n], we
526*088332b5SXin Li ** first compute 'lim', the smallest Mersenne number not smaller than
527*088332b5SXin Li ** 'n'. We then project 'ran' into the interval [0, lim]. If the result
528*088332b5SXin Li ** is inside [0, n], we are done. Otherwise, we try with another 'ran',
529*088332b5SXin Li ** until we have a result inside the interval.
530*088332b5SXin Li */
project(lua_Unsigned ran,lua_Unsigned n,RanState * state)531*088332b5SXin Li static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
532*088332b5SXin Li RanState *state) {
533*088332b5SXin Li if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */
534*088332b5SXin Li return ran & n; /* no bias */
535*088332b5SXin Li else {
536*088332b5SXin Li lua_Unsigned lim = n;
537*088332b5SXin Li /* compute the smallest (2^b - 1) not smaller than 'n' */
538*088332b5SXin Li lim |= (lim >> 1);
539*088332b5SXin Li lim |= (lim >> 2);
540*088332b5SXin Li lim |= (lim >> 4);
541*088332b5SXin Li lim |= (lim >> 8);
542*088332b5SXin Li lim |= (lim >> 16);
543*088332b5SXin Li #if (LUA_MAXUNSIGNED >> 31) >= 3
544*088332b5SXin Li lim |= (lim >> 32); /* integer type has more than 32 bits */
545*088332b5SXin Li #endif
546*088332b5SXin Li lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */
547*088332b5SXin Li && lim >= n /* not smaller than 'n', */
548*088332b5SXin Li && (lim >> 1) < n); /* and it is the smallest one */
549*088332b5SXin Li while ((ran &= lim) > n) /* project 'ran' into [0..lim] */
550*088332b5SXin Li ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */
551*088332b5SXin Li return ran;
552*088332b5SXin Li }
553*088332b5SXin Li }
554*088332b5SXin Li
555*088332b5SXin Li
math_random(lua_State * L)556*088332b5SXin Li static int math_random (lua_State *L) {
557*088332b5SXin Li lua_Integer low, up;
558*088332b5SXin Li lua_Unsigned p;
559*088332b5SXin Li RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
560*088332b5SXin Li Rand64 rv = nextrand(state->s); /* next pseudo-random value */
561*088332b5SXin Li switch (lua_gettop(L)) { /* check number of arguments */
562*088332b5SXin Li case 0: { /* no arguments */
563*088332b5SXin Li lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */
564*088332b5SXin Li return 1;
565*088332b5SXin Li }
566*088332b5SXin Li case 1: { /* only upper limit */
567*088332b5SXin Li low = 1;
568*088332b5SXin Li up = luaL_checkinteger(L, 1);
569*088332b5SXin Li if (up == 0) { /* single 0 as argument? */
570*088332b5SXin Li lua_pushinteger(L, I2UInt(rv)); /* full random integer */
571*088332b5SXin Li return 1;
572*088332b5SXin Li }
573*088332b5SXin Li break;
574*088332b5SXin Li }
575*088332b5SXin Li case 2: { /* lower and upper limits */
576*088332b5SXin Li low = luaL_checkinteger(L, 1);
577*088332b5SXin Li up = luaL_checkinteger(L, 2);
578*088332b5SXin Li break;
579*088332b5SXin Li }
580*088332b5SXin Li default: return luaL_error(L, "wrong number of arguments");
581*088332b5SXin Li }
582*088332b5SXin Li /* random integer in the interval [low, up] */
583*088332b5SXin Li luaL_argcheck(L, low <= up, 1, "interval is empty");
584*088332b5SXin Li /* project random integer into the interval [0, up - low] */
585*088332b5SXin Li p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state);
586*088332b5SXin Li lua_pushinteger(L, p + (lua_Unsigned)low);
587*088332b5SXin Li return 1;
588*088332b5SXin Li }
589*088332b5SXin Li
590*088332b5SXin Li
setseed(lua_State * L,Rand64 * state,lua_Unsigned n1,lua_Unsigned n2)591*088332b5SXin Li static void setseed (lua_State *L, Rand64 *state,
592*088332b5SXin Li lua_Unsigned n1, lua_Unsigned n2) {
593*088332b5SXin Li int i;
594*088332b5SXin Li state[0] = Int2I(n1);
595*088332b5SXin Li state[1] = Int2I(0xff); /* avoid a zero state */
596*088332b5SXin Li state[2] = Int2I(n2);
597*088332b5SXin Li state[3] = Int2I(0);
598*088332b5SXin Li for (i = 0; i < 16; i++)
599*088332b5SXin Li nextrand(state); /* discard initial values to "spread" seed */
600*088332b5SXin Li lua_pushinteger(L, n1);
601*088332b5SXin Li lua_pushinteger(L, n2);
602*088332b5SXin Li }
603*088332b5SXin Li
604*088332b5SXin Li
605*088332b5SXin Li /*
606*088332b5SXin Li ** Set a "random" seed. To get some randomness, use the current time
607*088332b5SXin Li ** and the address of 'L' (in case the machine does address space layout
608*088332b5SXin Li ** randomization).
609*088332b5SXin Li */
randseed(lua_State * L,RanState * state)610*088332b5SXin Li static void randseed (lua_State *L, RanState *state) {
611*088332b5SXin Li lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
612*088332b5SXin Li lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
613*088332b5SXin Li setseed(L, state->s, seed1, seed2);
614*088332b5SXin Li }
615*088332b5SXin Li
616*088332b5SXin Li
math_randomseed(lua_State * L)617*088332b5SXin Li static int math_randomseed (lua_State *L) {
618*088332b5SXin Li RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
619*088332b5SXin Li if (lua_isnone(L, 1)) {
620*088332b5SXin Li randseed(L, state);
621*088332b5SXin Li }
622*088332b5SXin Li else {
623*088332b5SXin Li lua_Integer n1 = luaL_checkinteger(L, 1);
624*088332b5SXin Li lua_Integer n2 = luaL_optinteger(L, 2, 0);
625*088332b5SXin Li setseed(L, state->s, n1, n2);
626*088332b5SXin Li }
627*088332b5SXin Li return 2; /* return seeds */
628*088332b5SXin Li }
629*088332b5SXin Li
630*088332b5SXin Li
631*088332b5SXin Li static const luaL_Reg randfuncs[] = {
632*088332b5SXin Li {"random", math_random},
633*088332b5SXin Li {"randomseed", math_randomseed},
634*088332b5SXin Li {NULL, NULL}
635*088332b5SXin Li };
636*088332b5SXin Li
637*088332b5SXin Li
638*088332b5SXin Li /*
639*088332b5SXin Li ** Register the random functions and initialize their state.
640*088332b5SXin Li */
setrandfunc(lua_State * L)641*088332b5SXin Li static void setrandfunc (lua_State *L) {
642*088332b5SXin Li RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
643*088332b5SXin Li randseed(L, state); /* initialize with a "random" seed */
644*088332b5SXin Li lua_pop(L, 2); /* remove pushed seeds */
645*088332b5SXin Li luaL_setfuncs(L, randfuncs, 1);
646*088332b5SXin Li }
647*088332b5SXin Li
648*088332b5SXin Li /* }================================================================== */
649*088332b5SXin Li
650*088332b5SXin Li
651*088332b5SXin Li /*
652*088332b5SXin Li ** {==================================================================
653*088332b5SXin Li ** Deprecated functions (for compatibility only)
654*088332b5SXin Li ** ===================================================================
655*088332b5SXin Li */
656*088332b5SXin Li #if defined(LUA_COMPAT_MATHLIB)
657*088332b5SXin Li
math_cosh(lua_State * L)658*088332b5SXin Li static int math_cosh (lua_State *L) {
659*088332b5SXin Li lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
660*088332b5SXin Li return 1;
661*088332b5SXin Li }
662*088332b5SXin Li
math_sinh(lua_State * L)663*088332b5SXin Li static int math_sinh (lua_State *L) {
664*088332b5SXin Li lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
665*088332b5SXin Li return 1;
666*088332b5SXin Li }
667*088332b5SXin Li
math_tanh(lua_State * L)668*088332b5SXin Li static int math_tanh (lua_State *L) {
669*088332b5SXin Li lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
670*088332b5SXin Li return 1;
671*088332b5SXin Li }
672*088332b5SXin Li
math_pow(lua_State * L)673*088332b5SXin Li static int math_pow (lua_State *L) {
674*088332b5SXin Li lua_Number x = luaL_checknumber(L, 1);
675*088332b5SXin Li lua_Number y = luaL_checknumber(L, 2);
676*088332b5SXin Li lua_pushnumber(L, l_mathop(pow)(x, y));
677*088332b5SXin Li return 1;
678*088332b5SXin Li }
679*088332b5SXin Li
math_frexp(lua_State * L)680*088332b5SXin Li static int math_frexp (lua_State *L) {
681*088332b5SXin Li int e;
682*088332b5SXin Li lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
683*088332b5SXin Li lua_pushinteger(L, e);
684*088332b5SXin Li return 2;
685*088332b5SXin Li }
686*088332b5SXin Li
math_ldexp(lua_State * L)687*088332b5SXin Li static int math_ldexp (lua_State *L) {
688*088332b5SXin Li lua_Number x = luaL_checknumber(L, 1);
689*088332b5SXin Li int ep = (int)luaL_checkinteger(L, 2);
690*088332b5SXin Li lua_pushnumber(L, l_mathop(ldexp)(x, ep));
691*088332b5SXin Li return 1;
692*088332b5SXin Li }
693*088332b5SXin Li
math_log10(lua_State * L)694*088332b5SXin Li static int math_log10 (lua_State *L) {
695*088332b5SXin Li lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
696*088332b5SXin Li return 1;
697*088332b5SXin Li }
698*088332b5SXin Li
699*088332b5SXin Li #endif
700*088332b5SXin Li /* }================================================================== */
701*088332b5SXin Li
702*088332b5SXin Li
703*088332b5SXin Li
704*088332b5SXin Li static const luaL_Reg mathlib[] = {
705*088332b5SXin Li {"abs", math_abs},
706*088332b5SXin Li {"acos", math_acos},
707*088332b5SXin Li {"asin", math_asin},
708*088332b5SXin Li {"atan", math_atan},
709*088332b5SXin Li {"ceil", math_ceil},
710*088332b5SXin Li {"cos", math_cos},
711*088332b5SXin Li {"deg", math_deg},
712*088332b5SXin Li {"exp", math_exp},
713*088332b5SXin Li {"tointeger", math_toint},
714*088332b5SXin Li {"floor", math_floor},
715*088332b5SXin Li {"fmod", math_fmod},
716*088332b5SXin Li {"ult", math_ult},
717*088332b5SXin Li {"log", math_log},
718*088332b5SXin Li {"max", math_max},
719*088332b5SXin Li {"min", math_min},
720*088332b5SXin Li {"modf", math_modf},
721*088332b5SXin Li {"rad", math_rad},
722*088332b5SXin Li {"sin", math_sin},
723*088332b5SXin Li {"sqrt", math_sqrt},
724*088332b5SXin Li {"tan", math_tan},
725*088332b5SXin Li {"type", math_type},
726*088332b5SXin Li #if defined(LUA_COMPAT_MATHLIB)
727*088332b5SXin Li {"atan2", math_atan},
728*088332b5SXin Li {"cosh", math_cosh},
729*088332b5SXin Li {"sinh", math_sinh},
730*088332b5SXin Li {"tanh", math_tanh},
731*088332b5SXin Li {"pow", math_pow},
732*088332b5SXin Li {"frexp", math_frexp},
733*088332b5SXin Li {"ldexp", math_ldexp},
734*088332b5SXin Li {"log10", math_log10},
735*088332b5SXin Li #endif
736*088332b5SXin Li /* placeholders */
737*088332b5SXin Li {"random", NULL},
738*088332b5SXin Li {"randomseed", NULL},
739*088332b5SXin Li {"pi", NULL},
740*088332b5SXin Li {"huge", NULL},
741*088332b5SXin Li {"maxinteger", NULL},
742*088332b5SXin Li {"mininteger", NULL},
743*088332b5SXin Li {NULL, NULL}
744*088332b5SXin Li };
745*088332b5SXin Li
746*088332b5SXin Li
747*088332b5SXin Li /*
748*088332b5SXin Li ** Open math library
749*088332b5SXin Li */
luaopen_math(lua_State * L)750*088332b5SXin Li LUAMOD_API int luaopen_math (lua_State *L) {
751*088332b5SXin Li luaL_newlib(L, mathlib);
752*088332b5SXin Li lua_pushnumber(L, PI);
753*088332b5SXin Li lua_setfield(L, -2, "pi");
754*088332b5SXin Li lua_pushnumber(L, (lua_Number)HUGE_VAL);
755*088332b5SXin Li lua_setfield(L, -2, "huge");
756*088332b5SXin Li lua_pushinteger(L, LUA_MAXINTEGER);
757*088332b5SXin Li lua_setfield(L, -2, "maxinteger");
758*088332b5SXin Li lua_pushinteger(L, LUA_MININTEGER);
759*088332b5SXin Li lua_setfield(L, -2, "mininteger");
760*088332b5SXin Li setrandfunc(L);
761*088332b5SXin Li return 1;
762*088332b5SXin Li }
763*088332b5SXin Li
764