lib_math.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. ** Math library.
  3. ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #include <math.h>
  6. #define lib_math_c
  7. #define LUA_LIB
  8. #include "lua.h"
  9. #include "lauxlib.h"
  10. #include "lualib.h"
  11. #include "lj_obj.h"
  12. #include "lj_lib.h"
  13. #include "lj_vm.h"
  14. /* ------------------------------------------------------------------------ */
  15. #define LJLIB_MODULE_math
  16. LJLIB_ASM(math_abs) LJLIB_REC(.)
  17. {
  18. lj_lib_checknumber(L, 1);
  19. return FFH_RETRY;
  20. }
  21. LJLIB_ASM_(math_floor) LJLIB_REC(math_round IRFPM_FLOOR)
  22. LJLIB_ASM_(math_ceil) LJLIB_REC(math_round IRFPM_CEIL)
  23. LJLIB_ASM(math_sqrt) LJLIB_REC(math_unary IRFPM_SQRT)
  24. {
  25. lj_lib_checknum(L, 1);
  26. return FFH_RETRY;
  27. }
  28. LJLIB_ASM_(math_log10) LJLIB_REC(math_unary IRFPM_LOG10)
  29. LJLIB_ASM_(math_exp) LJLIB_REC(math_unary IRFPM_EXP)
  30. LJLIB_ASM_(math_sin) LJLIB_REC(math_unary IRFPM_SIN)
  31. LJLIB_ASM_(math_cos) LJLIB_REC(math_unary IRFPM_COS)
  32. LJLIB_ASM_(math_tan) LJLIB_REC(math_unary IRFPM_TAN)
  33. LJLIB_ASM_(math_asin) LJLIB_REC(math_atrig FF_math_asin)
  34. LJLIB_ASM_(math_acos) LJLIB_REC(math_atrig FF_math_acos)
  35. LJLIB_ASM_(math_atan) LJLIB_REC(math_atrig FF_math_atan)
  36. LJLIB_ASM_(math_sinh) LJLIB_REC(math_htrig IRCALL_sinh)
  37. LJLIB_ASM_(math_cosh) LJLIB_REC(math_htrig IRCALL_cosh)
  38. LJLIB_ASM_(math_tanh) LJLIB_REC(math_htrig IRCALL_tanh)
  39. LJLIB_ASM_(math_frexp)
  40. LJLIB_ASM_(math_modf) LJLIB_REC(.)
  41. LJLIB_ASM(math_log) LJLIB_REC(math_log)
  42. {
  43. double x = lj_lib_checknum(L, 1);
  44. if (L->base+1 < L->top) {
  45. double y = lj_lib_checknum(L, 2);
  46. #ifdef LUAJIT_NO_LOG2
  47. x = log(x); y = 1.0 / log(y);
  48. #else
  49. x = lj_vm_log2(x); y = 1.0 / lj_vm_log2(y);
  50. #endif
  51. setnumV(L->base-1, x*y); /* Do NOT join the expression to x / y. */
  52. return FFH_RES(1);
  53. }
  54. return FFH_RETRY;
  55. }
  56. LJLIB_PUSH(57.29577951308232)
  57. LJLIB_ASM_(math_deg) LJLIB_REC(math_degrad)
  58. LJLIB_PUSH(0.017453292519943295)
  59. LJLIB_ASM_(math_rad) LJLIB_REC(math_degrad)
  60. LJLIB_ASM(math_atan2) LJLIB_REC(.)
  61. {
  62. lj_lib_checknum(L, 1);
  63. lj_lib_checknum(L, 2);
  64. return FFH_RETRY;
  65. }
  66. LJLIB_ASM_(math_pow) LJLIB_REC(.)
  67. LJLIB_ASM_(math_fmod)
  68. LJLIB_ASM(math_ldexp) LJLIB_REC(.)
  69. {
  70. lj_lib_checknum(L, 1);
  71. #if LJ_DUALNUM && !LJ_TARGET_X86ORX64
  72. lj_lib_checkint(L, 2);
  73. #else
  74. lj_lib_checknum(L, 2);
  75. #endif
  76. return FFH_RETRY;
  77. }
  78. LJLIB_ASM(math_min) LJLIB_REC(math_minmax IR_MIN)
  79. {
  80. int i = 0;
  81. do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top);
  82. return FFH_RETRY;
  83. }
  84. LJLIB_ASM_(math_max) LJLIB_REC(math_minmax IR_MAX)
  85. LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi)
  86. LJLIB_PUSH(1e310) LJLIB_SET(huge)
  87. /* ------------------------------------------------------------------------ */
  88. /* This implements a Tausworthe PRNG with period 2^223. Based on:
  89. ** Tables of maximally-equidistributed combined LFSR generators,
  90. ** Pierre L'Ecuyer, 1991, table 3, 1st entry.
  91. ** Full-period ME-CF generator with L=64, J=4, k=223, N1=49.
  92. */
  93. /* PRNG state. */
  94. struct RandomState {
  95. uint64_t gen[4]; /* State of the 4 LFSR generators. */
  96. int valid; /* State is valid. */
  97. };
  98. /* Union needed for bit-pattern conversion between uint64_t and double. */
  99. typedef union { uint64_t u64; double d; } U64double;
  100. /* Update generator i and compute a running xor of all states. */
  101. #define TW223_GEN(i, k, q, s) \
  102. z = rs->gen[i]; \
  103. z = (((z<<q)^z) >> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<<s); \
  104. r ^= z; rs->gen[i] = z;
  105. /* PRNG step function. Returns a double in the range 1.0 <= d < 2.0. */
  106. LJ_NOINLINE uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs)
  107. {
  108. uint64_t z, r = 0;
  109. TW223_GEN(0, 63, 31, 18)
  110. TW223_GEN(1, 58, 19, 28)
  111. TW223_GEN(2, 55, 24, 7)
  112. TW223_GEN(3, 47, 21, 8)
  113. return (r & U64x(000fffff,ffffffff)) | U64x(3ff00000,00000000);
  114. }
  115. /* PRNG initialization function. */
  116. static void random_init(RandomState *rs, double d)
  117. {
  118. uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */
  119. int i;
  120. for (i = 0; i < 4; i++) {
  121. U64double u;
  122. uint32_t m = 1u << (r&255);
  123. r >>= 8;
  124. u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354;
  125. if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of gen[i] are non-zero. */
  126. rs->gen[i] = u.u64;
  127. }
  128. rs->valid = 1;
  129. for (i = 0; i < 10; i++)
  130. lj_math_random_step(rs);
  131. }
  132. /* PRNG extract function. */
  133. LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */
  134. LJLIB_CF(math_random) LJLIB_REC(.)
  135. {
  136. int n = (int)(L->top - L->base);
  137. RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
  138. U64double u;
  139. double d;
  140. if (LJ_UNLIKELY(!rs->valid)) random_init(rs, 0.0);
  141. u.u64 = lj_math_random_step(rs);
  142. d = u.d - 1.0;
  143. if (n > 0) {
  144. #if LJ_DUALNUM
  145. int isint = 1;
  146. double r1;
  147. lj_lib_checknumber(L, 1);
  148. if (tvisint(L->base)) {
  149. r1 = (lua_Number)intV(L->base);
  150. } else {
  151. isint = 0;
  152. r1 = numV(L->base);
  153. }
  154. #else
  155. double r1 = lj_lib_checknum(L, 1);
  156. #endif
  157. if (n == 1) {
  158. d = lj_vm_floor(d*r1) + 1.0; /* d is an int in range [1, r1] */
  159. } else {
  160. #if LJ_DUALNUM
  161. double r2;
  162. lj_lib_checknumber(L, 2);
  163. if (tvisint(L->base+1)) {
  164. r2 = (lua_Number)intV(L->base+1);
  165. } else {
  166. isint = 0;
  167. r2 = numV(L->base+1);
  168. }
  169. #else
  170. double r2 = lj_lib_checknum(L, 2);
  171. #endif
  172. d = lj_vm_floor(d*(r2-r1+1.0)) + r1; /* d is an int in range [r1, r2] */
  173. }
  174. #if LJ_DUALNUM
  175. if (isint) {
  176. setintV(L->top-1, lj_num2int(d));
  177. return 1;
  178. }
  179. #endif
  180. } /* else: d is a double in range [0, 1] */
  181. setnumV(L->top++, d);
  182. return 1;
  183. }
  184. /* PRNG seed function. */
  185. LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */
  186. LJLIB_CF(math_randomseed)
  187. {
  188. RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
  189. random_init(rs, lj_lib_checknum(L, 1));
  190. return 0;
  191. }
  192. /* ------------------------------------------------------------------------ */
  193. #include "lj_libdef.h"
  194. LUALIB_API int luaopen_math(lua_State *L)
  195. {
  196. RandomState *rs;
  197. rs = (RandomState *)lua_newuserdata(L, sizeof(RandomState));
  198. rs->valid = 0; /* Use lazy initialization to save some time on startup. */
  199. LJ_LIB_REG(L, LUA_MATHLIBNAME, math);
  200. #if defined(LUA_COMPAT_MOD) && !LJ_52
  201. lua_getfield(L, -1, "fmod");
  202. lua_setfield(L, -2, "mod");
  203. #endif
  204. return 1;
  205. }