2 ** Math helper functions for assembler VM.
3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.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
); }
25 #if !LJ_TARGET_X86ORX64
26 double lj_vm_foldarith(double x
, double y
, int 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;
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;
51 double lj_vm_log2(double a
)
53 return log(a
) * 1.4426950408889634074;
58 double lj_vm_exp2(double a
)
60 return exp(a
* 0.6931471805599453);
64 #if !(LJ_TARGET_ARM || LJ_TARGET_PPC)
65 int32_t LJ_FASTCALL
lj_vm_modi(int32_t a
, int32_t b
)
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
;
72 if (y
!= 0 && (a
^b
) < 0) y
= y
- ub
;
73 if (((int32_t)y
^b
) < 0) y
= (uint32_t)-(int32_t)y
;
78 #if !LJ_TARGET_X86ORX64
80 static double lj_vm_powui(double x
, uint32_t k
)
84 for (; (k
& 1) == 0; k
>>= 1) x
*= x
;
99 double lj_vm_powi(double x
, int32_t k
)
102 return lj_vm_powui(x
, (uint32_t)k
);
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
)
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);
134 int lj_vm_errno(void)