Add required PHIs for implicit conversions (via XREF fwd).
[luajit-2.0.git] / src / lj_vmmath.c
blob1416b600fd67501ca15d55b71e83f1c333e08a1a
1 /*
2 ** Math helper functions for assembler VM.
3 ** Copyright (C) 2005-2012 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 #if LJ_HASJIT || LJ_TARGET_MIPS
14 #include "lj_ir.h"
15 #endif
16 #include "lj_vm.h"
18 /* -- Helper functions for generated machine code ------------------------- */
20 #if LJ_TARGET_X86ORX64
21 /* Wrapper functions to avoid linker issues on OSX. */
22 LJ_FUNCA double lj_vm_sinh(double x) { return sinh(x); }
23 LJ_FUNCA double lj_vm_cosh(double x) { return cosh(x); }
24 LJ_FUNCA double lj_vm_tanh(double x) { return tanh(x); }
25 #endif
27 #if LJ_TARGET_MIPS
28 double lj_vm_foldarith(double x, double y, int op)
30 switch (op) {
31 case IR_ADD - IR_ADD: return x+y; break;
32 case IR_SUB - IR_ADD: return x-y; break;
33 case IR_MUL - IR_ADD: return x*y; break;
34 case IR_DIV - IR_ADD: return x/y; break;
35 case IR_MOD - IR_ADD: return x-lj_vm_floor(x/y)*y; break;
36 case IR_POW - IR_ADD: return pow(x, y); break;
37 case IR_NEG - IR_ADD: return -x; break;
38 case IR_ABS - IR_ADD: return fabs(x); break;
39 #if LJ_HASJIT
40 case IR_ATAN2 - IR_ADD: return atan2(x, y); break;
41 case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break;
42 case IR_MIN - IR_ADD: return x > y ? y : x; break;
43 case IR_MAX - IR_ADD: return x < y ? y : x; break;
44 #endif
45 default: return x;
48 #endif
50 #if LJ_HASJIT
52 #ifdef LUAJIT_NO_LOG2
53 double lj_vm_log2(double a)
55 return log(a) * 1.4426950408889634074;
57 #endif
59 #ifdef LUAJIT_NO_EXP2
60 double lj_vm_exp2(double a)
62 return exp(a * 0.6931471805599453);
64 #endif
66 #if !(LJ_TARGET_ARM || LJ_TARGET_PPC)
67 int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b)
69 uint32_t y, ua, ub;
70 lua_assert(b != 0); /* This must be checked before using this function. */
71 ua = a < 0 ? (uint32_t)-a : (uint32_t)a;
72 ub = b < 0 ? (uint32_t)-b : (uint32_t)b;
73 y = ua % ub;
74 if (y != 0 && (a^b) < 0) y = y - ub;
75 if (((int32_t)y^b) < 0) y = (uint32_t)-(int32_t)y;
76 return (int32_t)y;
78 #endif
80 #if !LJ_TARGET_X86ORX64
81 /* Unsigned x^k. */
82 static double lj_vm_powui(double x, uint32_t k)
84 double y;
85 lua_assert(k != 0);
86 for (; (k & 1) == 0; k >>= 1) x *= x;
87 y = x;
88 if ((k >>= 1) != 0) {
89 for (;;) {
90 x *= x;
91 if (k == 1) break;
92 if (k & 1) y *= x;
93 k >>= 1;
95 y *= x;
97 return y;
100 /* Signed x^k. */
101 double lj_vm_powi(double x, int32_t k)
103 if (k > 1)
104 return lj_vm_powui(x, (uint32_t)k);
105 else if (k == 1)
106 return x;
107 else if (k == 0)
108 return 1.0;
109 else
110 return 1.0 / lj_vm_powui(x, (uint32_t)-k);
113 /* Computes fpm(x) for extended math functions. */
114 double lj_vm_foldfpm(double x, int fpm)
116 switch (fpm) {
117 case IRFPM_FLOOR: return lj_vm_floor(x);
118 case IRFPM_CEIL: return lj_vm_ceil(x);
119 case IRFPM_TRUNC: return lj_vm_trunc(x);
120 case IRFPM_SQRT: return sqrt(x);
121 case IRFPM_EXP: return exp(x);
122 case IRFPM_EXP2: return lj_vm_exp2(x);
123 case IRFPM_LOG: return log(x);
124 case IRFPM_LOG2: return lj_vm_log2(x);
125 case IRFPM_LOG10: return log10(x);
126 case IRFPM_SIN: return sin(x);
127 case IRFPM_COS: return cos(x);
128 case IRFPM_TAN: return tan(x);
129 default: lua_assert(0);
131 return 0;
133 #endif
135 #if LJ_HASFFI
136 int lj_vm_errno(void)
138 return errno;
140 #endif
142 #endif