lj_vmmath.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. ** Math helper functions for assembler VM.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #define lj_vmmath_c
  6. #define LUA_CORE
  7. #include <errno.h>
  8. #include <math.h>
  9. #include "lj_obj.h"
  10. #include "lj_ir.h"
  11. #include "lj_vm.h"
  12. /* -- Wrapper functions --------------------------------------------------- */
  13. #if LJ_TARGET_X86 && __ELF__ && __PIC__
  14. /* Wrapper functions to deal with the ELF/x86 PIC disaster. */
  15. LJ_FUNCA double lj_wrap_log(double x) { return log(x); }
  16. LJ_FUNCA double lj_wrap_log10(double x) { return log10(x); }
  17. LJ_FUNCA double lj_wrap_exp(double x) { return exp(x); }
  18. LJ_FUNCA double lj_wrap_sin(double x) { return sin(x); }
  19. LJ_FUNCA double lj_wrap_cos(double x) { return cos(x); }
  20. LJ_FUNCA double lj_wrap_tan(double x) { return tan(x); }
  21. LJ_FUNCA double lj_wrap_asin(double x) { return asin(x); }
  22. LJ_FUNCA double lj_wrap_acos(double x) { return acos(x); }
  23. LJ_FUNCA double lj_wrap_atan(double x) { return atan(x); }
  24. LJ_FUNCA double lj_wrap_sinh(double x) { return sinh(x); }
  25. LJ_FUNCA double lj_wrap_cosh(double x) { return cosh(x); }
  26. LJ_FUNCA double lj_wrap_tanh(double x) { return tanh(x); }
  27. LJ_FUNCA double lj_wrap_atan2(double x, double y) { return atan2(x, y); }
  28. LJ_FUNCA double lj_wrap_pow(double x, double y) { return pow(x, y); }
  29. LJ_FUNCA double lj_wrap_fmod(double x, double y) { return fmod(x, y); }
  30. #endif
  31. /* -- Helper functions ---------------------------------------------------- */
  32. /* Required to prevent the C compiler from applying FMA optimizations.
  33. **
  34. ** Yes, there's -ffp-contract and the FP_CONTRACT pragma ... in theory.
  35. ** But the current state of C compilers is a mess in this regard.
  36. ** Also, this function is not performance sensitive at all.
  37. */
  38. LJ_NOINLINE static double lj_vm_floormul(double x, double y)
  39. {
  40. return lj_vm_floor(x / y) * y;
  41. }
  42. double lj_vm_foldarith(double x, double y, int op)
  43. {
  44. switch (op) {
  45. case IR_ADD - IR_ADD: return x+y; break;
  46. case IR_SUB - IR_ADD: return x-y; break;
  47. case IR_MUL - IR_ADD: return x*y; break;
  48. case IR_DIV - IR_ADD: return x/y; break;
  49. case IR_MOD - IR_ADD: return x-lj_vm_floormul(x, y); break;
  50. case IR_POW - IR_ADD: return pow(x, y); break;
  51. case IR_NEG - IR_ADD: return -x; break;
  52. case IR_ABS - IR_ADD: return fabs(x); break;
  53. #if LJ_HASJIT
  54. case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break;
  55. case IR_MIN - IR_ADD: return x < y ? x : y; break;
  56. case IR_MAX - IR_ADD: return x > y ? x : y; break;
  57. #endif
  58. default: return x;
  59. }
  60. }
  61. /* -- Helper functions for generated machine code ------------------------- */
  62. #if (LJ_HASJIT && !(LJ_TARGET_ARM || LJ_TARGET_ARM64 || LJ_TARGET_PPC)) || LJ_TARGET_MIPS
  63. int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b)
  64. {
  65. uint32_t y, ua, ub;
  66. /* This must be checked before using this function. */
  67. lj_assertX(b != 0, "modulo with zero divisor");
  68. ua = a < 0 ? ~(uint32_t)a+1u : (uint32_t)a;
  69. ub = b < 0 ? ~(uint32_t)b+1u : (uint32_t)b;
  70. y = ua % ub;
  71. if (y != 0 && (a^b) < 0) y = y - ub;
  72. if (((int32_t)y^b) < 0) y = ~y+1u;
  73. return (int32_t)y;
  74. }
  75. #endif
  76. #if LJ_HASJIT
  77. #ifdef LUAJIT_NO_LOG2
  78. double lj_vm_log2(double a)
  79. {
  80. return log(a) * 1.4426950408889634074;
  81. }
  82. #endif
  83. /* Computes fpm(x) for extended math functions. */
  84. double lj_vm_foldfpm(double x, int fpm)
  85. {
  86. switch (fpm) {
  87. case IRFPM_FLOOR: return lj_vm_floor(x);
  88. case IRFPM_CEIL: return lj_vm_ceil(x);
  89. case IRFPM_TRUNC: return lj_vm_trunc(x);
  90. case IRFPM_SQRT: return sqrt(x);
  91. case IRFPM_LOG: return log(x);
  92. case IRFPM_LOG2: return lj_vm_log2(x);
  93. default: lj_assertX(0, "bad fpm %d", fpm);
  94. }
  95. return 0;
  96. }
  97. #if LJ_HASFFI
  98. int lj_vm_errno(void)
  99. {
  100. return errno;
  101. }
  102. #endif
  103. #endif