3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
20 /* ------------------------------------------------------------------------ */
22 #define LJLIB_MODULE_math
24 LJLIB_ASM(math_abs
) LJLIB_REC(.)
26 lj_lib_checknumber(L
, 1);
29 LJLIB_ASM_(math_floor
) LJLIB_REC(math_round IRFPM_FLOOR
)
30 LJLIB_ASM_(math_ceil
) LJLIB_REC(math_round IRFPM_CEIL
)
32 LJLIB_ASM(math_sqrt
) LJLIB_REC(math_unary IRFPM_SQRT
)
34 lj_lib_checknum(L
, 1);
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
)
51 LJLIB_ASM(math_log
) LJLIB_REC(math_log
)
53 double x
= lj_lib_checknum(L
, 1);
54 if (L
->base
+1 < L
->top
) {
55 double y
= lj_lib_checknum(L
, 2);
57 x
= log(x
); y
= 1.0 / log(y
);
59 x
= lj_vm_log2(x
); y
= 1.0 / lj_vm_log2(y
);
61 setnumV(L
->base
-1-LJ_FR2
, x
*y
); /* Do NOT join the expression to x / y. */
67 LJLIB_LUA(math_deg
) /* function(x) return x * 57.29577951308232 end */
68 LJLIB_LUA(math_rad
) /* function(x) return x * 0.017453292519943295 end */
70 LJLIB_ASM(math_atan2
) LJLIB_REC(.)
72 lj_lib_checknum(L
, 1);
73 lj_lib_checknum(L
, 2);
76 LJLIB_ASM_(math_pow
) LJLIB_REC(.)
79 LJLIB_ASM(math_ldexp
) LJLIB_REC(.)
81 lj_lib_checknum(L
, 1);
82 #if LJ_DUALNUM && !LJ_TARGET_X86ORX64
83 lj_lib_checkint(L
, 2);
85 lj_lib_checknum(L
, 2);
90 LJLIB_ASM(math_min
) LJLIB_REC(math_minmax IR_MIN
)
93 do { lj_lib_checknumber(L
, ++i
); } while (L
->base
+i
< L
->top
);
96 LJLIB_ASM_(math_max
) LJLIB_REC(math_minmax IR_MAX
)
98 LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi
)
99 LJLIB_PUSH(1e310
) LJLIB_SET(huge
)
101 /* ------------------------------------------------------------------------ */
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.
109 /* Union needed for bit-pattern conversion between uint64_t and double. */
110 typedef union { uint64_t u64
; double d
; } U64double
;
112 /* PRNG seeding function. */
113 static void random_seed(PRNGState
*rs
, double d
)
115 uint32_t r
= 0x11090601; /* 64-k[i] as four 8 bit constants. */
117 for (i
= 0; i
< 4; i
++) {
119 uint32_t m
= 1u << (r
&255);
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. */
125 for (i
= 0; i
< 10; i
++)
126 (void)lj_prng_u64(rs
);
129 /* PRNG extract function. */
130 LJLIB_PUSH(top
-2) /* Upvalue holds userdata with PRNGState. */
131 LJLIB_CF(math_random
) LJLIB_REC(.)
133 int n
= (int)(L
->top
- L
->base
);
134 PRNGState
*rs
= (PRNGState
*)(uddata(udataV(lj_lib_upvalue(L
, 1))));
137 u
.u64
= lj_prng_u64d(rs
);
143 lj_lib_checknumber(L
, 1);
144 if (tvisint(L
->base
)) {
145 r1
= (lua_Number
)intV(L
->base
);
151 double r1
= lj_lib_checknum(L
, 1);
154 d
= lj_vm_floor(d
*r1
) + 1.0; /* d is an int in range [1, r1] */
158 lj_lib_checknumber(L
, 2);
159 if (tvisint(L
->base
+1)) {
160 r2
= (lua_Number
)intV(L
->base
+1);
163 r2
= numV(L
->base
+1);
166 double r2
= lj_lib_checknum(L
, 2);
168 d
= lj_vm_floor(d
*(r2
-r1
+1.0)) + r1
; /* d is an int in range [r1, r2] */
172 setintV(L
->top
-1, lj_num2int(d
));
176 } /* else: d is a double in range [0, 1] */
177 setnumV(L
->top
++, d
);
181 /* PRNG seed function. */
182 LJLIB_PUSH(top
-2) /* Upvalue holds userdata with PRNGState. */
183 LJLIB_CF(math_randomseed
)
185 PRNGState
*rs
= (PRNGState
*)(uddata(udataV(lj_lib_upvalue(L
, 1))));
186 random_seed(rs
, lj_lib_checknum(L
, 1));
190 /* ------------------------------------------------------------------------ */
192 #include "lj_libdef.h"
194 LUALIB_API
int luaopen_math(lua_State
*L
)
196 PRNGState
*rs
= (PRNGState
*)lua_newuserdata(L
, sizeof(PRNGState
));
197 lj_prng_seed_fixed(rs
);
198 LJ_LIB_REG(L
, LUA_MATHLIBNAME
, math
);