src/lib_math.c - luajit-2.0-src

Data types defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** Math library.
  3. ** Copyright (C) 2005-2015 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-LJ_FR2, x*y);  /* Do NOT join the expression to x / y. */
  52.     return FFH_RES(1);
  53.   }
  54.   return FFH_RETRY;
  55. }

  56. LJLIB_LUA(math_deg) /* function(x) return x * 57.29577951308232 end */
  57. LJLIB_LUA(math_rad) /* function(x) return x * 0.017453292519943295 end */

  58. LJLIB_ASM(math_atan2)                LJLIB_REC(.)
  59. {
  60.   lj_lib_checknum(L, 1);
  61.   lj_lib_checknum(L, 2);
  62.   return FFH_RETRY;
  63. }
  64. LJLIB_ASM_(math_pow)                LJLIB_REC(.)
  65. LJLIB_ASM_(math_fmod)

  66. LJLIB_ASM(math_ldexp)                LJLIB_REC(.)
  67. {
  68.   lj_lib_checknum(L, 1);
  69. #if LJ_DUALNUM && !LJ_TARGET_X86ORX64
  70.   lj_lib_checkint(L, 2);
  71. #else
  72.   lj_lib_checknum(L, 2);
  73. #endif
  74.   return FFH_RETRY;
  75. }

  76. LJLIB_ASM(math_min)                LJLIB_REC(math_minmax IR_MIN)
  77. {
  78.   int i = 0;
  79.   do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top);
  80.   return FFH_RETRY;
  81. }
  82. LJLIB_ASM_(math_max)                LJLIB_REC(math_minmax IR_MAX)

  83. LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi)
  84. LJLIB_PUSH(1e310) LJLIB_SET(huge)

  85. /* ------------------------------------------------------------------------ */

  86. /* This implements a Tausworthe PRNG with period 2^223. Based on:
  87. **   Tables of maximally-equidistributed combined LFSR generators,
  88. **   Pierre L'Ecuyer, 1991, table 3, 1st entry.
  89. ** Full-period ME-CF generator with L=64, J=4, k=223, N1=49.
  90. */

  91. /* PRNG state. */
  92. struct RandomState {
  93.   uint64_t gen[4];        /* State of the 4 LFSR generators. */
  94.   int valid;                /* State is valid. */
  95. };

  96. /* Union needed for bit-pattern conversion between uint64_t and double. */
  97. typedef union { uint64_t u64; double d; } U64double;

  98. /* Update generator i and compute a running xor of all states. */
  99. #define TW223_GEN(i, k, q, s) \
  100.   z = rs->gen[i]; \
  101.   z = (((z<<q)^z) >> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<<s); \
  102.   r ^= z; rs->gen[i] = z;

  103. /* PRNG step function. Returns a double in the range 1.0 <= d < 2.0. */
  104. LJ_NOINLINE uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs)
  105. {
  106.   uint64_t z, r = 0;
  107.   TW223_GEN(0, 63, 31, 18)
  108.   TW223_GEN(1, 58, 19, 28)
  109.   TW223_GEN(2, 55, 247)
  110.   TW223_GEN(3, 47, 218)
  111.   return (r & U64x(000fffff,ffffffff)) | U64x(3ff00000,00000000);
  112. }

  113. /* PRNG initialization function. */
  114. static void random_init(RandomState *rs, double d)
  115. {
  116.   uint32_t r = 0x11090601/* 64-k[i] as four 8 bit constants. */
  117.   int i;
  118.   for (i = 0; i < 4; i++) {
  119.     U64double u;
  120.     uint32_t m = 1u << (r&255);
  121.     r >>= 8;
  122.     u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354;
  123.     if (u.u64 < m) u.u64 += m;  /* Ensure k[i] MSB of gen[i] are non-zero. */
  124.     rs->gen[i] = u.u64;
  125.   }
  126.   rs->valid = 1;
  127.   for (i = 0; i < 10; i++)
  128.     lj_math_random_step(rs);
  129. }

  130. /* PRNG extract function. */
  131. LJLIB_PUSH(top-2/* Upvalue holds userdata with RandomState. */
  132. LJLIB_CF(math_random)                LJLIB_REC(.)
  133. {
  134.   int n = (int)(L->top - L->base);
  135.   RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
  136.   U64double u;
  137.   double d;
  138.   if (LJ_UNLIKELY(!rs->valid)) random_init(rs, 0.0);
  139.   u.u64 = lj_math_random_step(rs);
  140.   d = u.d - 1.0;
  141.   if (n > 0) {
  142. #if LJ_DUALNUM
  143.     int isint = 1;
  144.     double r1;
  145.     lj_lib_checknumber(L, 1);
  146.     if (tvisint(L->base)) {
  147.       r1 = (lua_Number)intV(L->base);
  148.     } else {
  149.       isint = 0;
  150.       r1 = numV(L->base);
  151.     }
  152. #else
  153.     double r1 = lj_lib_checknum(L, 1);
  154. #endif
  155.     if (n == 1) {
  156.       d = lj_vm_floor(d*r1) + 1.0/* d is an int in range [1, r1] */
  157.     } else {
  158. #if LJ_DUALNUM
  159.       double r2;
  160.       lj_lib_checknumber(L, 2);
  161.       if (tvisint(L->base+1)) {
  162.         r2 = (lua_Number)intV(L->base+1);
  163.       } else {
  164.         isint = 0;
  165.         r2 = numV(L->base+1);
  166.       }
  167. #else
  168.       double r2 = lj_lib_checknum(L, 2);
  169. #endif
  170.       d = lj_vm_floor(d*(r2-r1+1.0)) + r1;  /* d is an int in range [r1, r2] */
  171.     }
  172. #if LJ_DUALNUM
  173.     if (isint) {
  174.       setintV(L->top-1, lj_num2int(d));
  175.       return 1;
  176.     }
  177. #endif
  178.   }  /* else: d is a double in range [0, 1] */
  179.   setnumV(L->top++, d);
  180.   return 1;
  181. }

  182. /* PRNG seed function. */
  183. LJLIB_PUSH(top-2/* Upvalue holds userdata with RandomState. */
  184. LJLIB_CF(math_randomseed)
  185. {
  186.   RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
  187.   random_init(rs, lj_lib_checknum(L, 1));
  188.   return 0;
  189. }

  190. /* ------------------------------------------------------------------------ */

  191. #include "lj_libdef.h"

  192. LUALIB_API int luaopen_math(lua_State *L)
  193. {
  194.   RandomState *rs;
  195.   rs = (RandomState *)lua_newuserdata(L, sizeof(RandomState));
  196.   rs->valid = 0/* Use lazy initialization to save some time on startup. */
  197.   LJ_LIB_REG(L, LUA_MATHLIBNAME, math);
  198. #if defined(LUA_COMPAT_MOD) && !LJ_52
  199.   lua_getfield(L, -1, "fmod");
  200.   lua_setfield(L, -2, "mod");
  201. #endif
  202.   return 1;
  203. }