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