Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lmathlib.c File Reference
#include "lprefix.h"
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

Go to the source code of this file.

Data Structures

struct  Rand64
 
struct  RanState
 

Macros

#define lmathlib_c
 
#define LUA_LIB
 
#define PI   (l_mathop(3.141592653589793238462643383279502884))
 
#define FIGS   l_floatatt(MANT_DIG)
 
#define trim32(x)   ((x) & 0xffffffffu)
 
#define UONE   ((lu_int32)1)
 
#define scaleFIG   (l_mathop(0.5) / (UONE << (FIGS - 1)))
 

Typedefs

typedef unsigned long lu_int32
 
typedef struct Rand64 Rand64
 

Functions

static int math_abs (lua_State *L)
 
static int math_sin (lua_State *L)
 
static int math_cos (lua_State *L)
 
static int math_tan (lua_State *L)
 
static int math_asin (lua_State *L)
 
static int math_acos (lua_State *L)
 
static int math_atan (lua_State *L)
 
static int math_toint (lua_State *L)
 
static void pushnumint (lua_State *L, lua_Number d)
 
static int math_floor (lua_State *L)
 
static int math_ceil (lua_State *L)
 
static int math_fmod (lua_State *L)
 
static int math_modf (lua_State *L)
 
static int math_sqrt (lua_State *L)
 
static int math_ult (lua_State *L)
 
static int math_log (lua_State *L)
 
static int math_exp (lua_State *L)
 
static int math_deg (lua_State *L)
 
static int math_rad (lua_State *L)
 
static int math_min (lua_State *L)
 
static int math_max (lua_State *L)
 
static int math_type (lua_State *L)
 
static Rand64 packI (lu_int32 h, lu_int32 l)
 
static Rand64 Ishl (Rand64 i, int n)
 
static void Ixor (Rand64 *i1, Rand64 i2)
 
static Rand64 Iadd (Rand64 i1, Rand64 i2)
 
static Rand64 times5 (Rand64 i)
 
static Rand64 times9 (Rand64 i)
 
static Rand64 rotl (Rand64 i, int n)
 
static Rand64 rotl1 (Rand64 i, int n)
 
static Rand64 nextrand (Rand64 *state)
 
static lua_Number I2d (Rand64 x)
 
static lua_Unsigned I2UInt (Rand64 x)
 
static Rand64 Int2I (lua_Unsigned n)
 
static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, RanState *state)
 
static int math_random (lua_State *L)
 
static void setseed (lua_State *L, Rand64 *state, lua_Unsigned n1, lua_Unsigned n2)
 
static void randseed (lua_State *L, RanState *state)
 
static int math_randomseed (lua_State *L)
 
static void setrandfunc (lua_State *L)
 
LUAMOD_API int luaopen_math (lua_State *L)
 

Variables

static const luaL_Reg randfuncs []
 
static const luaL_Reg mathlib []
 

Macro Definition Documentation

◆ FIGS

#define FIGS   l_floatatt(MANT_DIG)

Definition at line 253 of file lua-5.4.3/src/lmathlib.c.

Referenced by I2d().

◆ lmathlib_c

#define lmathlib_c

Definition at line 7 of file lua-5.4.3/src/lmathlib.c.

◆ LUA_LIB

#define LUA_LIB

Definition at line 8 of file lua-5.4.3/src/lmathlib.c.

◆ PI

#define PI   (l_mathop(3.141592653589793238462643383279502884))

Definition at line 26 of file lua-5.4.3/src/lmathlib.c.

Referenced by luaopen_math(), math_deg(), and math_rad().

◆ scaleFIG

#define scaleFIG   (l_mathop(0.5) / (UONE << (FIGS - 1)))

Definition at line 461 of file lua-5.4.3/src/lmathlib.c.

Referenced by I2d().

◆ trim32

#define trim32 ( x)    ((x) & 0xffffffffu)

Definition at line 374 of file lua-5.4.3/src/lmathlib.c.

Referenced by I2d(), I2UInt(), Iadd(), Ishl(), rotl(), and rotl1().

◆ UONE

#define UONE   ((lu_int32)1)

Definition at line 455 of file lua-5.4.3/src/lmathlib.c.

Typedef Documentation

◆ lu_int32

typedef unsigned long lu_int32

Definition at line 354 of file lua-5.4.3/src/lmathlib.c.

◆ Rand64

typedef struct Rand64 Rand64

Function Documentation

◆ I2d()

static lua_Number I2d ( Rand64 x)
static

Definition at line 467 of file lua-5.4.3/src/lmathlib.c.

467 {
468 lua_Number h = (lua_Number)(trim32(x.h) >> (32 - FIGS));
469 return h * scaleFIG;
470}
LUA_NUMBER lua_Number
#define FIGS
#define trim32(x)
#define scaleFIG

References FIGS, Rand64::h, scaleFIG, and trim32.

Referenced by math_random().

◆ I2UInt()

static lua_Unsigned I2UInt ( Rand64 x)
static

Definition at line 502 of file lua-5.4.3/src/lmathlib.c.

502 {
503 return ((lua_Unsigned)trim32(x.h) << 31 << 1) | (lua_Unsigned)trim32(x.l);
504}
LUA_UNSIGNED lua_Unsigned

References Rand64::h, Rand64::l, and trim32.

Referenced by math_random(), and project().

◆ Iadd()

static Rand64 Iadd ( Rand64 i1,
Rand64 i2 )
static

Definition at line 402 of file lua-5.4.3/src/lmathlib.c.

402 {
403 Rand64 result = packI(i1.h + i2.h, i1.l + i2.l);
404 if (trim32(result.l) < trim32(i1.l)) /* carry? */
405 result.h++;
406 return result;
407}
static Rand64 packI(lu_int32 h, lu_int32 l)

References Rand64::h, Rand64::l, packI(), and trim32.

Referenced by times5(), and times9().

◆ Int2I()

static Rand64 Int2I ( lua_Unsigned n)
static

Definition at line 507 of file lua-5.4.3/src/lmathlib.c.

507 {
508 return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n);
509}
LUAI_UINT32 lu_int32

References packI().

Referenced by setseed().

◆ Ishl()

static Rand64 Ishl ( Rand64 i,
int n )
static

Definition at line 390 of file lua-5.4.3/src/lmathlib.c.

390 {
391 lua_assert(n > 0 && n < 32);
392 return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n);
393}
#define lua_assert(c)

References Rand64::h, Rand64::l, lua_assert, packI(), and trim32.

Referenced by nextrand(), times5(), and times9().

◆ Ixor()

static void Ixor ( Rand64 * i1,
Rand64 i2 )
static

Definition at line 396 of file lua-5.4.3/src/lmathlib.c.

396 {
397 i1->h ^= i2.h;
398 i1->l ^= i2.l;
399}

References Rand64::h, and Rand64::l.

Referenced by nextrand().

◆ luaopen_math()

LUAMOD_API int luaopen_math ( lua_State * L)

Definition at line 751 of file lua-5.4.3/src/lmathlib.c.

751 {
753 lua_pushnumber(L, PI);
754 lua_setfield(L, -2, "pi");
755 lua_pushnumber(L, (lua_Number)HUGE_VAL);
756 lua_setfield(L, -2, "huge");
757 lua_pushinteger(L, LUA_MAXINTEGER);
758 lua_setfield(L, -2, "maxinteger");
759 lua_pushinteger(L, LUA_MININTEGER);
760 lua_setfield(L, -2, "mininteger");
761 setrandfunc(L);
762 return 1;
763}
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
#define luaL_newlib(L, l)
#define PI
static const luaL_Reg mathlib[]
static void setrandfunc(lua_State *L)

References lua_pushinteger(), lua_pushnumber(), lua_setfield(), luaL_newlib, mathlib, PI, and setrandfunc().

◆ math_abs()

static int math_abs ( lua_State * L)
static

Definition at line 29 of file lua-5.4.3/src/lmathlib.c.

29 {
30 if (lua_isinteger(L, 1)) {
31 lua_Integer n = lua_tointeger(L, 1);
32 if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
33 lua_pushinteger(L, n);
34 }
35 else
37 return 1;
38}
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
LUA_INTEGER lua_Integer
#define lua_tointeger(L, i)
#define l_mathop(x)
LUA_API int lua_isinteger(lua_State *L, int idx)

References l_mathop, lua_isinteger(), lua_pushinteger(), lua_pushnumber(), lua_tointeger, and luaL_checknumber().

◆ math_acos()

static int math_acos ( lua_State * L)
static

Definition at line 60 of file lua-5.4.3/src/lmathlib.c.

60 {
62 return 1;
63}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_asin()

static int math_asin ( lua_State * L)
static

Definition at line 55 of file lua-5.4.3/src/lmathlib.c.

55 {
57 return 1;
58}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_atan()

static int math_atan ( lua_State * L)
static

Definition at line 65 of file lua-5.4.3/src/lmathlib.c.

65 {
67 lua_Number x = luaL_optnumber(L, 2, 1);
68 lua_pushnumber(L, l_mathop(atan2)(y, x));
69 return 1;
70}
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int narg, lua_Number def)

References l_mathop, lua_pushnumber(), luaL_checknumber(), and luaL_optnumber().

◆ math_ceil()

static int math_ceil ( lua_State * L)
static

Definition at line 106 of file lua-5.4.3/src/lmathlib.c.

106 {
107 if (lua_isinteger(L, 1))
108 lua_settop(L, 1); /* integer is its own ceil */
109 else {
110 lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
111 pushnumint(L, d);
112 }
113 return 1;
114}
LUA_API void lua_settop(lua_State *L, int idx)
static void pushnumint(lua_State *L, lua_Number d)

References l_mathop, lua_isinteger(), lua_settop(), luaL_checknumber(), and pushnumint().

◆ math_cos()

static int math_cos ( lua_State * L)
static

Definition at line 45 of file lua-5.4.3/src/lmathlib.c.

45 {
47 return 1;
48}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_deg()

static int math_deg ( lua_State * L)
static

Definition at line 195 of file lua-5.4.3/src/lmathlib.c.

195 {
196 lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
197 return 1;
198}

References l_mathop, lua_pushnumber(), luaL_checknumber(), and PI.

◆ math_exp()

static int math_exp ( lua_State * L)
static

Definition at line 190 of file lua-5.4.3/src/lmathlib.c.

190 {
192 return 1;
193}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_floor()

static int math_floor ( lua_State * L)
static

Definition at line 95 of file lua-5.4.3/src/lmathlib.c.

95 {
96 if (lua_isinteger(L, 1))
97 lua_settop(L, 1); /* integer is its own floor */
98 else {
99 lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
100 pushnumint(L, d);
101 }
102 return 1;
103}

References l_mathop, lua_isinteger(), lua_settop(), luaL_checknumber(), and pushnumint().

◆ math_fmod()

static int math_fmod ( lua_State * L)
static

Definition at line 117 of file lua-5.4.3/src/lmathlib.c.

117 {
118 if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
119 lua_Integer d = lua_tointeger(L, 2);
120 if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
121 luaL_argcheck(L, d != 0, 2, "zero");
122 lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
123 }
124 else
125 lua_pushinteger(L, lua_tointeger(L, 1) % d);
126 }
127 else
129 luaL_checknumber(L, 2)));
130 return 1;
131}
#define luaL_argcheck(L, cond, numarg, extramsg)

References l_mathop, lua_isinteger(), lua_pushinteger(), lua_pushnumber(), lua_tointeger, luaL_argcheck, and luaL_checknumber().

◆ math_log()

static int math_log ( lua_State * L)
static

Definition at line 169 of file lua-5.4.3/src/lmathlib.c.

169 {
171 lua_Number res;
172 if (lua_isnoneornil(L, 2))
173 res = l_mathop(log)(x);
174 else {
175 lua_Number base = luaL_checknumber(L, 2);
176#if !defined(LUA_USE_C89)
177 if (base == l_mathop(2.0))
178 res = l_mathop(log2)(x);
179 else
180#endif
181 if (base == l_mathop(10.0))
182 res = l_mathop(log10)(x);
183 else
184 res = l_mathop(log)(x)/l_mathop(log)(base);
185 }
186 lua_pushnumber(L, res);
187 return 1;
188}
#define lua_isnoneornil(L, n)

References l_mathop, lua_isnoneornil, lua_pushnumber(), and luaL_checknumber().

◆ math_max()

static int math_max ( lua_State * L)
static

Definition at line 220 of file lua-5.4.3/src/lmathlib.c.

220 {
221 int n = lua_gettop(L); /* number of arguments */
222 int imax = 1; /* index of current maximum value */
223 int i;
224 luaL_argcheck(L, n >= 1, 1, "value expected");
225 for (i = 2; i <= n; i++) {
226 if (lua_compare(L, imax, i, LUA_OPLT))
227 imax = i;
228 }
229 lua_pushvalue(L, imax);
230 return 1;
231}
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API int lua_gettop(lua_State *L)
LUA_API int lua_compare(lua_State *L, int index1, int index2, int op)
#define LUA_OPLT

References lua_compare(), lua_gettop(), LUA_OPLT, lua_pushvalue(), and luaL_argcheck.

◆ math_min()

static int math_min ( lua_State * L)
static

Definition at line 206 of file lua-5.4.3/src/lmathlib.c.

206 {
207 int n = lua_gettop(L); /* number of arguments */
208 int imin = 1; /* index of current minimum value */
209 int i;
210 luaL_argcheck(L, n >= 1, 1, "value expected");
211 for (i = 2; i <= n; i++) {
212 if (lua_compare(L, i, imin, LUA_OPLT))
213 imin = i;
214 }
215 lua_pushvalue(L, imin);
216 return 1;
217}

References lua_compare(), lua_gettop(), LUA_OPLT, lua_pushvalue(), and luaL_argcheck.

◆ math_modf()

static int math_modf ( lua_State * L)
static

Definition at line 139 of file lua-5.4.3/src/lmathlib.c.

139 {
140 if (lua_isinteger(L ,1)) {
141 lua_settop(L, 1); /* number is its own integer part */
142 lua_pushnumber(L, 0); /* no fractional part */
143 }
144 else {
146 /* integer part (rounds toward zero) */
147 lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
148 pushnumint(L, ip);
149 /* fractional part (test needed for inf/-inf) */
150 lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
151 }
152 return 2;
153}

References l_mathop, lua_isinteger(), lua_pushnumber(), lua_settop(), luaL_checknumber(), and pushnumint().

◆ math_rad()

static int math_rad ( lua_State * L)
static

Definition at line 200 of file lua-5.4.3/src/lmathlib.c.

200 {
201 lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
202 return 1;
203}

References l_mathop, lua_pushnumber(), luaL_checknumber(), and PI.

◆ math_random()

static int math_random ( lua_State * L)
static

Definition at line 557 of file lua-5.4.3/src/lmathlib.c.

557 {
558 lua_Integer low, up;
559 lua_Unsigned p;
561 Rand64 rv = nextrand(state->s); /* next pseudo-random value */
562 switch (lua_gettop(L)) { /* check number of arguments */
563 case 0: { /* no arguments */
564 lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */
565 return 1;
566 }
567 case 1: { /* only upper limit */
568 low = 1;
569 up = luaL_checkinteger(L, 1);
570 if (up == 0) { /* single 0 as argument? */
571 lua_pushinteger(L, I2UInt(rv)); /* full random integer */
572 return 1;
573 }
574 break;
575 }
576 case 2: { /* lower and upper limits */
577 low = luaL_checkinteger(L, 1);
578 up = luaL_checkinteger(L, 2);
579 break;
580 }
581 default: return luaL_error(L, "wrong number of arguments");
582 }
583 /* random integer in the interval [low, up] */
584 luaL_argcheck(L, low <= up, 1, "interval is empty");
585 /* project random integer into the interval [0, up - low] */
586 p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state);
587 lua_pushinteger(L, p + (lua_Unsigned)low);
588 return 1;
589}
LUA_API void * lua_touserdata(lua_State *L, int idx)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)
#define lua_upvalueindex(i)
static lua_Unsigned I2UInt(Rand64 x)
static lua_Unsigned project(lua_Unsigned ran, lua_Unsigned n, RanState *state)
static lua_Number I2d(Rand64 x)
static Rand64 nextrand(Rand64 *state)

References I2d(), I2UInt(), lua_gettop(), lua_pushinteger(), lua_pushnumber(), lua_touserdata(), lua_upvalueindex, luaL_argcheck, luaL_checkinteger(), luaL_error(), nextrand(), project(), and RanState::s.

◆ math_randomseed()

static int math_randomseed ( lua_State * L)
static

Definition at line 618 of file lua-5.4.3/src/lmathlib.c.

618 {
620 if (lua_isnone(L, 1)) {
621 randseed(L, state);
622 }
623 else {
625 lua_Integer n2 = luaL_optinteger(L, 2, 0);
626 setseed(L, state->s, n1, n2);
627 }
628 return 2; /* return seeds */
629}
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg, lua_Integer def)
#define lua_isnone(L, n)
static void setseed(lua_State *L, Rand64 *state, lua_Unsigned n1, lua_Unsigned n2)
static void randseed(lua_State *L, RanState *state)

References lua_isnone, lua_touserdata(), lua_upvalueindex, luaL_checkinteger(), luaL_optinteger(), randseed(), RanState::s, and setseed().

◆ math_sin()

static int math_sin ( lua_State * L)
static

Definition at line 40 of file lua-5.4.3/src/lmathlib.c.

40 {
42 return 1;
43}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_sqrt()

static int math_sqrt ( lua_State * L)
static

Definition at line 156 of file lua-5.4.3/src/lmathlib.c.

156 {
158 return 1;
159}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_tan()

static int math_tan ( lua_State * L)
static

Definition at line 50 of file lua-5.4.3/src/lmathlib.c.

50 {
52 return 1;
53}

References l_mathop, lua_pushnumber(), and luaL_checknumber().

◆ math_toint()

static int math_toint ( lua_State * L)
static

Definition at line 73 of file lua-5.4.3/src/lmathlib.c.

73 {
74 int valid;
75 lua_Integer n = lua_tointegerx(L, 1, &valid);
76 if (l_likely(valid))
77 lua_pushinteger(L, n);
78 else {
79 luaL_checkany(L, 1);
80 luaL_pushfail(L); /* value is not convertible to integer */
81 }
82 return 1;
83}
LUALIB_API void luaL_checkany(lua_State *L, int narg)
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *isnum)
#define luaL_pushfail(L)

References lua_pushinteger(), lua_tointegerx(), luaL_checkany(), and luaL_pushfail.

◆ math_type()

static int math_type ( lua_State * L)
static

Definition at line 234 of file lua-5.4.3/src/lmathlib.c.

234 {
235 if (lua_type(L, 1) == LUA_TNUMBER)
236 lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float");
237 else {
238 luaL_checkany(L, 1);
239 luaL_pushfail(L);
240 }
241 return 1;
242}
LUA_API void lua_pushstring(lua_State *L, const char *s)
LUA_API int lua_type(lua_State *L, int idx)
#define LUA_TNUMBER

References lua_isinteger(), lua_pushstring(), LUA_TNUMBER, lua_type(), luaL_checkany(), and luaL_pushfail.

◆ math_ult()

static int math_ult ( lua_State * L)
static

Definition at line 162 of file lua-5.4.3/src/lmathlib.c.

162 {
166 return 1;
167}
LUA_API void lua_pushboolean(lua_State *L, int b)

References lua_pushboolean(), and luaL_checkinteger().

◆ nextrand()

static Rand64 nextrand ( Rand64 * state)
static

Definition at line 437 of file lua-5.4.3/src/lmathlib.c.

437 {
438 Rand64 res = times9(rotl(times5(state[1]), 7));
439 Rand64 t = Ishl(state[1], 17);
440 Ixor(&state[2], state[0]);
441 Ixor(&state[3], state[1]);
442 Ixor(&state[1], state[2]);
443 Ixor(&state[0], state[3]);
444 Ixor(&state[2], t);
445 state[3] = rotl1(state[3], 45);
446 return res;
447}
static Rand64 rotl1(Rand64 i, int n)
static Rand64 times5(Rand64 i)
static Rand64 Ishl(Rand64 i, int n)
static Rand64 times9(Rand64 i)
static void Ixor(Rand64 *i1, Rand64 i2)
static Rand64 rotl(Rand64 i, int n)

References Ishl(), Ixor(), rotl(), rotl1(), times5(), and times9().

Referenced by math_random(), project(), and setseed().

◆ packI()

static Rand64 packI ( lu_int32 h,
lu_int32 l )
static

Definition at line 382 of file lua-5.4.3/src/lmathlib.c.

382 {
383 Rand64 result;
384 result.h = h;
385 result.l = l;
386 return result;
387}

References Rand64::h, and Rand64::l.

Referenced by Iadd(), Int2I(), Ishl(), rotl(), and rotl1().

◆ project()

static lua_Unsigned project ( lua_Unsigned ran,
lua_Unsigned n,
RanState * state )
static

Definition at line 532 of file lua-5.4.3/src/lmathlib.c.

533 {
534 if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */
535 return ran & n; /* no bias */
536 else {
537 lua_Unsigned lim = n;
538 /* compute the smallest (2^b - 1) not smaller than 'n' */
539 lim |= (lim >> 1);
540 lim |= (lim >> 2);
541 lim |= (lim >> 4);
542 lim |= (lim >> 8);
543 lim |= (lim >> 16);
544#if (LUA_MAXUNSIGNED >> 31) >= 3
545 lim |= (lim >> 32); /* integer type has more than 32 bits */
546#endif
547 lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */
548 && lim >= n /* not smaller than 'n', */
549 && (lim >> 1) < n); /* and it is the smallest one */
550 while ((ran &= lim) > n) /* project 'ran' into [0..lim] */
551 ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */
552 return ran;
553 }
554}

References I2UInt(), lua_assert, nextrand(), and RanState::s.

Referenced by math_random().

◆ pushnumint()

static void pushnumint ( lua_State * L,
lua_Number d )
static

Definition at line 86 of file lua-5.4.3/src/lmathlib.c.

86 {
88 if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
89 lua_pushinteger(L, n); /* result is integer */
90 else
91 lua_pushnumber(L, d); /* result is float */
92}
#define lua_numbertointeger(n, p)

References lua_numbertointeger, lua_pushinteger(), and lua_pushnumber().

Referenced by math_ceil(), math_floor(), and math_modf().

◆ randseed()

static void randseed ( lua_State * L,
RanState * state )
static

Definition at line 611 of file lua-5.4.3/src/lmathlib.c.

611 {
612 lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
613 lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
614 setseed(L, state->s, seed1, seed2);
615}
#define NULL
Definition gmacros.h:924

References NULL, RanState::s, and setseed().

Referenced by math_randomseed(), and setrandfunc().

◆ rotl()

static Rand64 rotl ( Rand64 i,
int n )
static

Definition at line 420 of file lua-5.4.3/src/lmathlib.c.

420 {
421 lua_assert(n > 0 && n < 32);
422 return packI((i.h << n) | (trim32(i.l) >> (32 - n)),
423 (trim32(i.h) >> (32 - n)) | (i.l << n));
424}

References Rand64::h, Rand64::l, lua_assert, packI(), and trim32.

Referenced by nextrand().

◆ rotl1()

static Rand64 rotl1 ( Rand64 i,
int n )
static

Definition at line 427 of file lua-5.4.3/src/lmathlib.c.

427 {
428 lua_assert(n > 32 && n < 64);
429 n = 64 - n;
430 return packI((trim32(i.h) >> n) | (i.l << (32 - n)),
431 (i.h << (32 - n)) | (trim32(i.l) >> n));
432}

References Rand64::h, Rand64::l, lua_assert, packI(), and trim32.

Referenced by nextrand().

◆ setrandfunc()

static void setrandfunc ( lua_State * L)
static

Definition at line 642 of file lua-5.4.3/src/lmathlib.c.

642 {
643 RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
644 randseed(L, state); /* initialize with a "random" seed */
645 lua_pop(L, 2); /* remove pushed seeds */
647}
#define lua_pop(L, n)
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
LUA_API void * lua_newuserdatauv(lua_State *L, size_t size, int nuvalue)
static const luaL_Reg randfuncs[]

References lua_newuserdatauv(), lua_pop, luaL_setfuncs(), randfuncs, and randseed().

Referenced by luaopen_math().

◆ setseed()

static void setseed ( lua_State * L,
Rand64 * state,
lua_Unsigned n1,
lua_Unsigned n2 )
static

Definition at line 592 of file lua-5.4.3/src/lmathlib.c.

593 {
594 int i;
595 state[0] = Int2I(n1);
596 state[1] = Int2I(0xff); /* avoid a zero state */
597 state[2] = Int2I(n2);
598 state[3] = Int2I(0);
599 for (i = 0; i < 16; i++)
600 nextrand(state); /* discard initial values to "spread" seed */
601 lua_pushinteger(L, n1);
602 lua_pushinteger(L, n2);
603}
static Rand64 Int2I(lua_Unsigned n)

References Int2I(), lua_pushinteger(), and nextrand().

Referenced by math_randomseed(), and randseed().

◆ times5()

static Rand64 times5 ( Rand64 i)
static

Definition at line 410 of file lua-5.4.3/src/lmathlib.c.

410 {
411 return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */
412}
static Rand64 Iadd(Rand64 i1, Rand64 i2)

References Iadd(), and Ishl().

Referenced by nextrand().

◆ times9()

static Rand64 times9 ( Rand64 i)
static

Definition at line 415 of file lua-5.4.3/src/lmathlib.c.

415 {
416 return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */
417}

References Iadd(), and Ishl().

Referenced by nextrand().

Variable Documentation

◆ mathlib

const luaL_Reg mathlib[]
static

Definition at line 705 of file lua-5.4.3/src/lmathlib.c.

705 {
706 {"abs", math_abs},
707 {"acos", math_acos},
708 {"asin", math_asin},
709 {"atan", math_atan},
710 {"ceil", math_ceil},
711 {"cos", math_cos},
712 {"deg", math_deg},
713 {"exp", math_exp},
714 {"tointeger", math_toint},
715 {"floor", math_floor},
716 {"fmod", math_fmod},
717 {"ult", math_ult},
718 {"log", math_log},
719 {"max", math_max},
720 {"min", math_min},
721 {"modf", math_modf},
722 {"rad", math_rad},
723 {"sin", math_sin},
724 {"sqrt", math_sqrt},
725 {"tan", math_tan},
726 {"type", math_type},
727#if defined(LUA_COMPAT_MATHLIB)
728 {"atan2", math_atan},
729 {"cosh", math_cosh},
730 {"sinh", math_sinh},
731 {"tanh", math_tanh},
732 {"pow", math_pow},
733 {"frexp", math_frexp},
734 {"ldexp", math_ldexp},
735 {"log10", math_log10},
736#endif
737 /* placeholders */
738 {"random", NULL},
739 {"randomseed", NULL},
740 {"pi", NULL},
741 {"huge", NULL},
742 {"maxinteger", NULL},
743 {"mininteger", NULL},
744 {NULL, NULL}
745};
static int math_frexp(lua_State *L)
static int math_pow(lua_State *L)
static int math_tanh(lua_State *L)
static int math_log10(lua_State *L)
static int math_sinh(lua_State *L)
static int math_cosh(lua_State *L)
static int math_ldexp(lua_State *L)
static int math_max(lua_State *L)
static int math_tan(lua_State *L)
static int math_fmod(lua_State *L)
static int math_ult(lua_State *L)
static int math_rad(lua_State *L)
static int math_cos(lua_State *L)
static int math_atan(lua_State *L)
static int math_toint(lua_State *L)
static int math_exp(lua_State *L)
static int math_log(lua_State *L)
static int math_sqrt(lua_State *L)
static int math_abs(lua_State *L)
static int math_acos(lua_State *L)
static int math_floor(lua_State *L)
static int math_asin(lua_State *L)
static int math_min(lua_State *L)
static int math_deg(lua_State *L)
static int math_sin(lua_State *L)
static int math_modf(lua_State *L)
static int math_ceil(lua_State *L)
static int math_type(lua_State *L)

Referenced by luaopen_math().

◆ randfuncs

const luaL_Reg randfuncs[]
static
Initial value:
= {
{"random", math_random},
{"randomseed", math_randomseed},
}
static int math_randomseed(lua_State *L)
static int math_random(lua_State *L)

Definition at line 632 of file lua-5.4.3/src/lmathlib.c.

632 {
633 {"random", math_random},
634 {"randomseed", math_randomseed},
635 {NULL, NULL}
636};

Referenced by setrandfunc().