Merge branch 'master' into v2.1
[luajit-2.0.git] / src / lj_vmmath.c
blob63886aa7e36fec3f6253676e4806eb4b8489f029
1 /*
2 ** Math helper functions for assembler VM.
3 ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #define lj_vmmath_c
7 #define LUA_CORE
9 #include <errno.h>
10 #include <math.h>
12 #include "lj_obj.h"
13 #include "lj_ir.h"
14 #include "lj_vm.h"
16 /* -- Helper functions for generated machine code ------------------------- */
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
25 #if !LJ_TARGET_X86ORX64
26 double lj_vm_foldarith(double x, double y, int op)
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;
46 #endif
48 #if LJ_HASJIT
50 #ifdef LUAJIT_NO_LOG2
51 double lj_vm_log2(double a)
53 return log(a) * 1.4426950408889634074;
55 #endif
57 #ifdef LUAJIT_NO_EXP2
58 double lj_vm_exp2(double a)
60 return exp(a * 0.6931471805599453);
62 #endif
64 #if !(LJ_TARGET_ARM || LJ_TARGET_PPC)
65 int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b)
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;
76 #endif
78 #if !LJ_TARGET_X86ORX64
79 /* Unsigned x^k. */
80 static double lj_vm_powui(double x, uint32_t k)
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;
93 y *= x;
95 return y;
98 /* Signed x^k. */
99 double lj_vm_powi(double x, int32_t k)
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);
111 /* Computes fpm(x) for extended math functions. */
112 double lj_vm_foldfpm(double x, int fpm)
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);
129 return 0;
131 #endif
133 #if LJ_HASFFI
134 int lj_vm_errno(void)
136 return errno;
138 #endif
140 #endif