3 ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h
21 /* ------------------------------------------------------------------------ */
23 #define LJLIB_MODULE_math
25 LJLIB_ASM(math_abs
) LJLIB_REC(.)
27 lj_lib_checknumber(L
, 1);
30 LJLIB_ASM_(math_floor
) LJLIB_REC(math_round IRFPM_FLOOR
)
31 LJLIB_ASM_(math_ceil
) LJLIB_REC(math_round IRFPM_CEIL
)
33 LJLIB_ASM(math_sqrt
) LJLIB_REC(math_unary IRFPM_SQRT
)
35 lj_lib_checknum(L
, 1);
38 LJLIB_ASM_(math_log10
) LJLIB_REC(math_call IRCALL_log10
)
39 LJLIB_ASM_(math_exp
) LJLIB_REC(math_call IRCALL_exp
)
40 LJLIB_ASM_(math_sin
) LJLIB_REC(math_call IRCALL_sin
)
41 LJLIB_ASM_(math_cos
) LJLIB_REC(math_call IRCALL_cos
)
42 LJLIB_ASM_(math_tan
) LJLIB_REC(math_call IRCALL_tan
)
43 LJLIB_ASM_(math_asin
) LJLIB_REC(math_call IRCALL_asin
)
44 LJLIB_ASM_(math_acos
) LJLIB_REC(math_call IRCALL_acos
)
45 LJLIB_ASM_(math_atan
) LJLIB_REC(math_call IRCALL_atan
)
46 LJLIB_ASM_(math_sinh
) LJLIB_REC(math_call IRCALL_sinh
)
47 LJLIB_ASM_(math_cosh
) LJLIB_REC(math_call IRCALL_cosh
)
48 LJLIB_ASM_(math_tanh
) LJLIB_REC(math_call IRCALL_tanh
)
49 LJLIB_ASM_(math_frexp
)
52 LJLIB_ASM(math_log
) LJLIB_REC(math_log
)
54 double x
= lj_lib_checknum(L
, 1);
55 if (L
->base
+1 < L
->top
) {
56 double y
= lj_lib_checknum(L
, 2);
58 x
= log(x
); y
= 1.0 / log(y
);
60 x
= lj_vm_log2(x
); y
= 1.0 / lj_vm_log2(y
);
62 setnumV(L
->base
-1-LJ_FR2
, x
*y
); /* Do NOT join the expression to x / y. */
68 LJLIB_LUA(math_deg
) /* function(x) return x * 57.29577951308232 end */
69 LJLIB_LUA(math_rad
) /* function(x) return x * 0.017453292519943295 end */
71 LJLIB_ASM(math_atan2
) LJLIB_REC(.)
73 lj_lib_checknum(L
, 1);
74 lj_lib_checknum(L
, 2);
77 LJLIB_ASM_(math_pow
) LJLIB_REC(.)
80 LJLIB_ASM(math_ldexp
) LJLIB_REC(.)
82 lj_lib_checknum(L
, 1);
83 #if LJ_DUALNUM && !LJ_TARGET_X86ORX64
84 lj_lib_checkint(L
, 2);
86 lj_lib_checknum(L
, 2);
91 LJLIB_ASM(math_min
) LJLIB_REC(math_minmax IR_MIN
)
94 do { lj_lib_checknumber(L
, ++i
); } while (L
->base
+i
< L
->top
);
97 LJLIB_ASM_(math_max
) LJLIB_REC(math_minmax IR_MAX
)
99 LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi
)
100 LJLIB_PUSH(1e310
) LJLIB_SET(huge
)
102 /* ------------------------------------------------------------------------ */
104 /* This implements a Tausworthe PRNG with period 2^223. Based on:
105 ** Tables of maximally-equidistributed combined LFSR generators,
106 ** Pierre L'Ecuyer, 1991, table 3, 1st entry.
107 ** Full-period ME-CF generator with L=64, J=4, k=223, N1=49.
110 /* Union needed for bit-pattern conversion between uint64_t and double. */
111 typedef union { uint64_t u64
; double d
; } U64double
;
113 /* PRNG seeding function. */
114 static void random_seed(PRNGState
*rs
, double d
)
116 uint32_t r
= 0x11090601; /* 64-k[i] as four 8 bit constants. */
118 for (i
= 0; i
< 4; i
++) {
120 uint32_t m
= 1u << (r
&255);
122 u
.d
= d
= d
* 3.14159265358979323846 + 2.7182818284590452354;
123 if (u
.u64
< m
) u
.u64
+= m
; /* Ensure k[i] MSB of u[i] are non-zero. */
126 for (i
= 0; i
< 10; i
++)
127 (void)lj_prng_u64(rs
);
130 /* PRNG extract function. */
131 LJLIB_PUSH(top
-2) /* Upvalue holds userdata with PRNGState. */
132 LJLIB_CF(math_random
) LJLIB_REC(.)
134 int n
= (int)(L
->top
- L
->base
);
135 PRNGState
*rs
= (PRNGState
*)(uddata(udataV(lj_lib_upvalue(L
, 1))));
138 u
.u64
= lj_prng_u64d(rs
);
144 lj_lib_checknumber(L
, 1);
145 if (tvisint(L
->base
)) {
146 r1
= (lua_Number
)intV(L
->base
);
152 double r1
= lj_lib_checknum(L
, 1);
155 d
= lj_vm_floor(d
*r1
) + 1.0; /* d is an int in range [1, r1] */
159 lj_lib_checknumber(L
, 2);
160 if (tvisint(L
->base
+1)) {
161 r2
= (lua_Number
)intV(L
->base
+1);
164 r2
= numV(L
->base
+1);
167 double r2
= lj_lib_checknum(L
, 2);
169 d
= lj_vm_floor(d
*(r2
-r1
+1.0)) + r1
; /* d is an int in range [r1, r2] */
173 setintV(L
->top
-1, lj_num2int(d
));
177 } /* else: d is a double in range [0, 1] */
178 setnumV(L
->top
++, d
);
182 /* PRNG seed function. */
183 LJLIB_PUSH(top
-2) /* Upvalue holds userdata with PRNGState. */
184 LJLIB_CF(math_randomseed
)
186 PRNGState
*rs
= (PRNGState
*)(uddata(udataV(lj_lib_upvalue(L
, 1))));
187 if (L
->base
!= L
->top
)
188 random_seed(rs
, lj_lib_checknum(L
, 1));
189 else if (!lj_prng_seed_secure(rs
))
190 lj_err_caller(L
, LJ_ERR_PRNGSD
);
194 /* ------------------------------------------------------------------------ */
196 #include "lj_libdef.h"
198 LUALIB_API
int luaopen_math(lua_State
*L
)
200 PRNGState
*rs
= (PRNGState
*)lua_newuserdata(L
, sizeof(PRNGState
));
201 lj_prng_seed_fixed(rs
);
202 LJ_LIB_REG(L
, LUA_MATHLIBNAME
, math
);