root/lj_vmmath.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. lj_vm_sinh
  2. lj_vm_cosh
  3. lj_vm_tanh
  4. lj_vm_foldarith
  5. lj_vm_log2
  6. lj_vm_exp2
  7. lj_vm_modi
  8. lj_vm_powui
  9. lj_vm_powi
  10. lj_vm_foldfpm
  11. lj_vm_errno

   1 /*
   2 ** Math helper functions for assembler VM.
   3 ** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
   4 */
   5 
   6 #define lj_vmmath_c
   7 #define LUA_CORE
   8 
   9 #include <errno.h>
  10 #include <math.h>
  11 
  12 #include "lj_obj.h"
  13 #include "lj_ir.h"
  14 #include "lj_vm.h"
  15 
  16 /* -- Helper functions for generated machine code ------------------------- */
  17 
  18 #if LJ_TARGET_X86ORX64
  19 /* Wrapper functions to avoid linker issues on OSX. */
  20 LJ_FUNCA double lj_vm_sinh(double x) { return sinh(x); }
  21 LJ_FUNCA double lj_vm_cosh(double x) { return cosh(x); }
  22 LJ_FUNCA double lj_vm_tanh(double x) { return tanh(x); }
  23 #endif
  24 
  25 #if !LJ_TARGET_X86ORX64
  26 double lj_vm_foldarith(double x, double y, int op)
  27 {
  28   switch (op) {
  29   case IR_ADD - IR_ADD: return x+y; break;
  30   case IR_SUB - IR_ADD: return x-y; break;
  31   case IR_MUL - IR_ADD: return x*y; break;
  32   case IR_DIV - IR_ADD: return x/y; break;
  33   case IR_MOD - IR_ADD: return x-lj_vm_floor(x/y)*y; break;
  34   case IR_POW - IR_ADD: return pow(x, y); break;
  35   case IR_NEG - IR_ADD: return -x; break;
  36   case IR_ABS - IR_ADD: return fabs(x); break;
  37 #if LJ_HASJIT
  38   case IR_ATAN2 - IR_ADD: return atan2(x, y); break;
  39   case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break;
  40   case IR_MIN - IR_ADD: return x > y ? y : x; break;
  41   case IR_MAX - IR_ADD: return x < y ? y : x; break;
  42 #endif
  43   default: return x;
  44   }
  45 }
  46 #endif
  47 
  48 #if LJ_HASJIT
  49 
  50 #ifdef LUAJIT_NO_LOG2
  51 double lj_vm_log2(double a)
  52 {
  53   return log(a) * 1.4426950408889634074;
  54 }
  55 #endif
  56 
  57 #ifdef LUAJIT_NO_EXP2
  58 double lj_vm_exp2(double a)
  59 {
  60   return exp(a * 0.6931471805599453);
  61 }
  62 #endif
  63 
  64 #if !(LJ_TARGET_ARM || LJ_TARGET_PPC)
  65 int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b)
  66 {
  67   uint32_t y, ua, ub;
  68   lua_assert(b != 0);  /* This must be checked before using this function. */
  69   ua = a < 0 ? (uint32_t)-a : (uint32_t)a;
  70   ub = b < 0 ? (uint32_t)-b : (uint32_t)b;
  71   y = ua % ub;
  72   if (y != 0 && (a^b) < 0) y = y - ub;
  73   if (((int32_t)y^b) < 0) y = (uint32_t)-(int32_t)y;
  74   return (int32_t)y;
  75 }
  76 #endif
  77 
  78 #if !LJ_TARGET_X86ORX64
  79 /* Unsigned x^k. */
  80 static double lj_vm_powui(double x, uint32_t k)
  81 {
  82   double y;
  83   lua_assert(k != 0);
  84   for (; (k & 1) == 0; k >>= 1) x *= x;
  85   y = x;
  86   if ((k >>= 1) != 0) {
  87     for (;;) {
  88       x *= x;
  89       if (k == 1) break;
  90       if (k & 1) y *= x;
  91       k >>= 1;
  92     }
  93     y *= x;
  94   }
  95   return y;
  96 }
  97 
  98 /* Signed x^k. */
  99 double lj_vm_powi(double x, int32_t k)
 100 {
 101   if (k > 1)
 102     return lj_vm_powui(x, (uint32_t)k);
 103   else if (k == 1)
 104     return x;
 105   else if (k == 0)
 106     return 1.0;
 107   else
 108     return 1.0 / lj_vm_powui(x, (uint32_t)-k);
 109 }
 110 
 111 /* Computes fpm(x) for extended math functions. */
 112 double lj_vm_foldfpm(double x, int fpm)
 113 {
 114   switch (fpm) {
 115   case IRFPM_FLOOR: return lj_vm_floor(x);
 116   case IRFPM_CEIL: return lj_vm_ceil(x);
 117   case IRFPM_TRUNC: return lj_vm_trunc(x);
 118   case IRFPM_SQRT: return sqrt(x);
 119   case IRFPM_EXP: return exp(x);
 120   case IRFPM_EXP2: return lj_vm_exp2(x);
 121   case IRFPM_LOG: return log(x);
 122   case IRFPM_LOG2: return lj_vm_log2(x);
 123   case IRFPM_LOG10: return log10(x);
 124   case IRFPM_SIN: return sin(x);
 125   case IRFPM_COS: return cos(x);
 126   case IRFPM_TAN: return tan(x);
 127   default: lua_assert(0);
 128   }
 129   return 0;
 130 }
 131 #endif
 132 
 133 #if LJ_HASFFI
 134 int lj_vm_errno(void)
 135 {
 136   return errno;
 137 }
 138 #endif
 139 
 140 #endif

/* [<][>][^][v][top][bottom][index][help] */