Cleanup math function compilation and fix inconsistencies.

This commit is contained in:
Mike Pall 2020-05-22 04:53:35 +02:00
parent c5b8e9168b
commit 5655be4546
14 changed files with 48 additions and 65 deletions

View File

@ -33,17 +33,17 @@ LJLIB_ASM(math_sqrt) LJLIB_REC(math_unary IRFPM_SQRT)
lj_lib_checknum(L, 1); lj_lib_checknum(L, 1);
return FFH_RETRY; return FFH_RETRY;
} }
LJLIB_ASM_(math_log10) LJLIB_REC(math_unary IRFPM_LOG10) LJLIB_ASM_(math_log10) LJLIB_REC(math_call IRCALL_log10)
LJLIB_ASM_(math_exp) LJLIB_REC(math_unary IRFPM_EXP) LJLIB_ASM_(math_exp) LJLIB_REC(math_call IRCALL_exp)
LJLIB_ASM_(math_sin) LJLIB_REC(math_unary IRFPM_SIN) LJLIB_ASM_(math_sin) LJLIB_REC(math_call IRCALL_sin)
LJLIB_ASM_(math_cos) LJLIB_REC(math_unary IRFPM_COS) LJLIB_ASM_(math_cos) LJLIB_REC(math_call IRCALL_cos)
LJLIB_ASM_(math_tan) LJLIB_REC(math_unary IRFPM_TAN) LJLIB_ASM_(math_tan) LJLIB_REC(math_call IRCALL_tan)
LJLIB_ASM_(math_asin) LJLIB_REC(math_atrig FF_math_asin) LJLIB_ASM_(math_asin) LJLIB_REC(math_call IRCALL_asin)
LJLIB_ASM_(math_acos) LJLIB_REC(math_atrig FF_math_acos) LJLIB_ASM_(math_acos) LJLIB_REC(math_call IRCALL_acos)
LJLIB_ASM_(math_atan) LJLIB_REC(math_atrig FF_math_atan) LJLIB_ASM_(math_atan) LJLIB_REC(math_call IRCALL_atan)
LJLIB_ASM_(math_sinh) LJLIB_REC(math_htrig IRCALL_sinh) LJLIB_ASM_(math_sinh) LJLIB_REC(math_call IRCALL_sinh)
LJLIB_ASM_(math_cosh) LJLIB_REC(math_htrig IRCALL_cosh) LJLIB_ASM_(math_cosh) LJLIB_REC(math_call IRCALL_cosh)
LJLIB_ASM_(math_tanh) LJLIB_REC(math_htrig IRCALL_tanh) LJLIB_ASM_(math_tanh) LJLIB_REC(math_call IRCALL_tanh)
LJLIB_ASM_(math_frexp) LJLIB_ASM_(math_frexp)
LJLIB_ASM_(math_modf) LJLIB_ASM_(math_modf)

View File

@ -1657,14 +1657,13 @@ static void asm_ir(ASMState *as, IRIns *ir)
case IR_NEG: asm_neg(as, ir); break; case IR_NEG: asm_neg(as, ir); break;
#if LJ_SOFTFP32 #if LJ_SOFTFP32
case IR_DIV: case IR_POW: case IR_ABS: case IR_DIV: case IR_POW: case IR_ABS:
case IR_ATAN2: case IR_LDEXP: case IR_FPMATH: case IR_TOBIT: case IR_LDEXP: case IR_FPMATH: case IR_TOBIT:
lua_assert(0); /* Unused for LJ_SOFTFP32. */ lua_assert(0); /* Unused for LJ_SOFTFP32. */
break; break;
#else #else
case IR_DIV: asm_div(as, ir); break; case IR_DIV: asm_div(as, ir); break;
case IR_POW: asm_pow(as, ir); break; case IR_POW: asm_pow(as, ir); break;
case IR_ABS: asm_abs(as, ir); break; case IR_ABS: asm_abs(as, ir); break;
case IR_ATAN2: asm_atan2(as, ir); break;
case IR_LDEXP: asm_ldexp(as, ir); break; case IR_LDEXP: asm_ldexp(as, ir); break;
case IR_FPMATH: asm_fpmath(as, ir); break; case IR_FPMATH: asm_fpmath(as, ir); break;
case IR_TOBIT: asm_tobit(as, ir); break; case IR_TOBIT: asm_tobit(as, ir); break;
@ -2158,11 +2157,6 @@ static void asm_setup_regsp(ASMState *as)
as->modset = RSET_SCRATCH; as->modset = RSET_SCRATCH;
break; break;
#if !LJ_SOFTFP #if !LJ_SOFTFP
case IR_ATAN2:
#if LJ_TARGET_X86
if (as->evenspill < 4) /* Leave room to call atan2(). */
as->evenspill = 4;
#endif
#if !LJ_TARGET_X86ORX64 #if !LJ_TARGET_X86ORX64
case IR_LDEXP: case IR_LDEXP:
#endif #endif

View File

@ -1510,7 +1510,6 @@ static void asm_mul(ASMState *as, IRIns *ir)
#define asm_div(as, ir) asm_fparith(as, ir, ARMI_VDIV_D) #define asm_div(as, ir) asm_fparith(as, ir, ARMI_VDIV_D)
#define asm_pow(as, ir) asm_callid(as, ir, IRCALL_lj_vm_powi) #define asm_pow(as, ir) asm_callid(as, ir, IRCALL_lj_vm_powi)
#define asm_abs(as, ir) asm_fpunary(as, ir, ARMI_VABS_D) #define asm_abs(as, ir) asm_fpunary(as, ir, ARMI_VABS_D)
#define asm_atan2(as, ir) asm_callid(as, ir, IRCALL_atan2)
#define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp) #define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp)
#endif #endif

View File

@ -1455,7 +1455,6 @@ static void asm_pow(ASMState *as, IRIns *ir)
#define asm_mulov(as, ir) asm_mul(as, ir) #define asm_mulov(as, ir) asm_mul(as, ir)
#define asm_abs(as, ir) asm_fpunary(as, ir, A64I_FABS) #define asm_abs(as, ir) asm_fpunary(as, ir, A64I_FABS)
#define asm_atan2(as, ir) asm_callid(as, ir, IRCALL_atan2)
#define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp) #define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp)
static void asm_mod(ASMState *as, IRIns *ir) static void asm_mod(ASMState *as, IRIns *ir)

View File

@ -1838,7 +1838,6 @@ static void asm_abs(ASMState *as, IRIns *ir)
} }
#endif #endif
#define asm_atan2(as, ir) asm_callid(as, ir, IRCALL_atan2)
#define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp) #define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp)
static void asm_arithov(ASMState *as, IRIns *ir) static void asm_arithov(ASMState *as, IRIns *ir)

View File

@ -1387,7 +1387,6 @@ static void asm_neg(ASMState *as, IRIns *ir)
} }
#define asm_abs(as, ir) asm_fpunary(as, ir, PPCI_FABS) #define asm_abs(as, ir) asm_fpunary(as, ir, PPCI_FABS)
#define asm_atan2(as, ir) asm_callid(as, ir, IRCALL_atan2)
#define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp) #define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp)
static void asm_arithov(ASMState *as, IRIns *ir, PPCIns pi) static void asm_arithov(ASMState *as, IRIns *ir, PPCIns pi)

View File

@ -1957,8 +1957,6 @@ static void asm_fpmath(ASMState *as, IRIns *ir)
} }
} }
#define asm_atan2(as, ir) asm_callid(as, ir, IRCALL_atan2)
static void asm_ldexp(ASMState *as, IRIns *ir) static void asm_ldexp(ASMState *as, IRIns *ir)
{ {
int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */ int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */

View File

@ -563,7 +563,7 @@ static void LJ_FASTCALL recff_math_atan2(jit_State *J, RecordFFData *rd)
{ {
TRef tr = lj_ir_tonum(J, J->base[0]); TRef tr = lj_ir_tonum(J, J->base[0]);
TRef tr2 = lj_ir_tonum(J, J->base[1]); TRef tr2 = lj_ir_tonum(J, J->base[1]);
J->base[0] = emitir(IRTN(IR_ATAN2), tr, tr2); J->base[0] = lj_ir_call(J, IRCALL_atan2, tr, tr2);
UNUSED(rd); UNUSED(rd);
} }
@ -580,22 +580,7 @@ static void LJ_FASTCALL recff_math_ldexp(jit_State *J, RecordFFData *rd)
UNUSED(rd); UNUSED(rd);
} }
/* Record math.asin, math.acos, math.atan. */ static void LJ_FASTCALL recff_math_call(jit_State *J, RecordFFData *rd)
static void LJ_FASTCALL recff_math_atrig(jit_State *J, RecordFFData *rd)
{
TRef y = lj_ir_tonum(J, J->base[0]);
TRef x = lj_ir_knum_one(J);
uint32_t ffid = rd->data;
if (ffid != FF_math_atan) {
TRef tmp = emitir(IRTN(IR_MUL), y, y);
tmp = emitir(IRTN(IR_SUB), x, tmp);
tmp = emitir(IRTN(IR_FPMATH), tmp, IRFPM_SQRT);
if (ffid == FF_math_asin) { x = tmp; } else { x = y; y = tmp; }
}
J->base[0] = emitir(IRTN(IR_ATAN2), y, x);
}
static void LJ_FASTCALL recff_math_htrig(jit_State *J, RecordFFData *rd)
{ {
TRef tr = lj_ir_tonum(J, J->base[0]); TRef tr = lj_ir_tonum(J, J->base[0]);
J->base[0] = emitir(IRTN(IR_CALLN), tr, rd->data); J->base[0] = emitir(IRTN(IR_CALLN), tr, rd->data);

View File

@ -75,7 +75,6 @@
_(NEG, N , ref, ref) \ _(NEG, N , ref, ref) \
\ \
_(ABS, N , ref, ref) \ _(ABS, N , ref, ref) \
_(ATAN2, N , ref, ref) \
_(LDEXP, N , ref, ref) \ _(LDEXP, N , ref, ref) \
_(MIN, C , ref, ref) \ _(MIN, C , ref, ref) \
_(MAX, C , ref, ref) \ _(MAX, C , ref, ref) \
@ -178,8 +177,7 @@ LJ_STATIC_ASSERT((int)IR_XLOAD + IRDELTA_L2S == (int)IR_XSTORE);
/* FPMATH sub-functions. ORDER FPM. */ /* FPMATH sub-functions. ORDER FPM. */
#define IRFPMDEF(_) \ #define IRFPMDEF(_) \
_(FLOOR) _(CEIL) _(TRUNC) /* Must be first and in this order. */ \ _(FLOOR) _(CEIL) _(TRUNC) /* Must be first and in this order. */ \
_(SQRT) _(EXP) _(EXP2) _(LOG) _(LOG2) _(LOG10) \ _(SQRT) _(EXP2) _(LOG) _(LOG2) \
_(SIN) _(COS) _(TAN) \
_(OTHER) _(OTHER)
typedef enum { typedef enum {

View File

@ -21,6 +21,7 @@ typedef struct CCallInfo {
#define CCI_OTSHIFT 16 #define CCI_OTSHIFT 16
#define CCI_OPTYPE(ci) ((ci)->flags >> CCI_OTSHIFT) /* Get op/type. */ #define CCI_OPTYPE(ci) ((ci)->flags >> CCI_OTSHIFT) /* Get op/type. */
#define CCI_TYPE(ci) (((ci)->flags>>CCI_OTSHIFT) & IRT_TYPE)
#define CCI_OPSHIFT 24 #define CCI_OPSHIFT 24
#define CCI_OP(ci) ((ci)->flags >> CCI_OPSHIFT) /* Get op. */ #define CCI_OP(ci) ((ci)->flags >> CCI_OPSHIFT) /* Get op. */
@ -172,6 +173,14 @@ typedef struct CCallInfo {
_(ANY, lj_mem_newgco, 2, FS, PGC, CCI_L) \ _(ANY, lj_mem_newgco, 2, FS, PGC, CCI_L) \
_(ANY, lj_math_random_step, 1, FS, NUM, CCI_CASTU64) \ _(ANY, lj_math_random_step, 1, FS, NUM, CCI_CASTU64) \
_(ANY, lj_vm_modi, 2, FN, INT, 0) \ _(ANY, lj_vm_modi, 2, FN, INT, 0) \
_(ANY, log10, 1, N, NUM, XA_FP) \
_(ANY, exp, 1, N, NUM, XA_FP) \
_(ANY, sin, 1, N, NUM, XA_FP) \
_(ANY, cos, 1, N, NUM, XA_FP) \
_(ANY, tan, 1, N, NUM, XA_FP) \
_(ANY, asin, 1, N, NUM, XA_FP) \
_(ANY, acos, 1, N, NUM, XA_FP) \
_(ANY, atan, 1, N, NUM, XA_FP) \
_(ANY, sinh, 1, N, NUM, XA_FP) \ _(ANY, sinh, 1, N, NUM, XA_FP) \
_(ANY, cosh, 1, N, NUM, XA_FP) \ _(ANY, cosh, 1, N, NUM, XA_FP) \
_(ANY, tanh, 1, N, NUM, XA_FP) \ _(ANY, tanh, 1, N, NUM, XA_FP) \
@ -183,14 +192,9 @@ typedef struct CCallInfo {
_(FPMATH, lj_vm_ceil, 1, N, NUM, XA_FP) \ _(FPMATH, lj_vm_ceil, 1, N, NUM, XA_FP) \
_(FPMATH, lj_vm_trunc, 1, N, NUM, XA_FP) \ _(FPMATH, lj_vm_trunc, 1, N, NUM, XA_FP) \
_(FPMATH, sqrt, 1, N, NUM, XA_FP) \ _(FPMATH, sqrt, 1, N, NUM, XA_FP) \
_(ANY, exp, 1, N, NUM, XA_FP) \
_(ANY, lj_vm_exp2, 1, N, NUM, XA_FP) \ _(ANY, lj_vm_exp2, 1, N, NUM, XA_FP) \
_(ANY, log, 1, N, NUM, XA_FP) \ _(ANY, log, 1, N, NUM, XA_FP) \
_(ANY, lj_vm_log2, 1, N, NUM, XA_FP) \ _(ANY, lj_vm_log2, 1, N, NUM, XA_FP) \
_(ANY, log10, 1, N, NUM, XA_FP) \
_(ANY, sin, 1, N, NUM, XA_FP) \
_(ANY, cos, 1, N, NUM, XA_FP) \
_(ANY, tan, 1, N, NUM, XA_FP) \
_(ANY, lj_vm_powi, 2, N, NUM, XA_FP) \ _(ANY, lj_vm_powi, 2, N, NUM, XA_FP) \
_(ANY, pow, 2, N, NUM, XA2_FP) \ _(ANY, pow, 2, N, NUM, XA2_FP) \
_(ANY, atan2, 2, N, NUM, XA2_FP) \ _(ANY, atan2, 2, N, NUM, XA2_FP) \

View File

@ -173,7 +173,6 @@ LJFOLD(ADD KNUM KNUM)
LJFOLD(SUB KNUM KNUM) LJFOLD(SUB KNUM KNUM)
LJFOLD(MUL KNUM KNUM) LJFOLD(MUL KNUM KNUM)
LJFOLD(DIV KNUM KNUM) LJFOLD(DIV KNUM KNUM)
LJFOLD(ATAN2 KNUM KNUM)
LJFOLD(LDEXP KNUM KNUM) LJFOLD(LDEXP KNUM KNUM)
LJFOLD(MIN KNUM KNUM) LJFOLD(MIN KNUM KNUM)
LJFOLD(MAX KNUM KNUM) LJFOLD(MAX KNUM KNUM)
@ -213,6 +212,30 @@ LJFOLDF(kfold_fpmath)
return lj_ir_knum(J, y); return lj_ir_knum(J, y);
} }
LJFOLD(CALLN KNUM any)
LJFOLDF(kfold_fpcall1)
{
const CCallInfo *ci = &lj_ir_callinfo[fins->op2];
if (CCI_TYPE(ci) == IRT_NUM) {
double y = ((double (*)(double))ci->func)(knumleft);
return lj_ir_knum(J, y);
}
return NEXTFOLD;
}
LJFOLD(CALLN CARG IRCALL_atan2)
LJFOLDF(kfold_fpcall2)
{
if (irref_isk(fleft->op1) && irref_isk(fleft->op2)) {
const CCallInfo *ci = &lj_ir_callinfo[fins->op2];
double a = ir_knum(IR(fleft->op1))->n;
double b = ir_knum(IR(fleft->op2))->n;
double y = ((double (*)(double, double))ci->func)(a, b);
return lj_ir_knum(J, y);
}
return NEXTFOLD;
}
LJFOLD(POW KNUM KINT) LJFOLD(POW KNUM KINT)
LJFOLDF(kfold_numpow) LJFOLDF(kfold_numpow)
{ {

View File

@ -426,9 +426,6 @@ static void split_ir(jit_State *J)
} }
hi = split_call_l(J, hisubst, oir, ir, IRCALL_lj_vm_floor + ir->op2); hi = split_call_l(J, hisubst, oir, ir, IRCALL_lj_vm_floor + ir->op2);
break; break;
case IR_ATAN2:
hi = split_call_ll(J, hisubst, oir, ir, IRCALL_atan2);
break;
case IR_LDEXP: case IR_LDEXP:
hi = split_call_li(J, hisubst, oir, ir, IRCALL_ldexp); hi = split_call_li(J, hisubst, oir, ir, IRCALL_ldexp);
break; break;

View File

@ -228,16 +228,10 @@ typedef enum {
/* Note: little-endian byte-order! */ /* Note: little-endian byte-order! */
XI_FLDZ = 0xeed9, XI_FLDZ = 0xeed9,
XI_FLD1 = 0xe8d9, XI_FLD1 = 0xe8d9,
XI_FLDLG2 = 0xecd9,
XI_FLDLN2 = 0xedd9,
XI_FDUP = 0xc0d9, /* Really fld st0. */ XI_FDUP = 0xc0d9, /* Really fld st0. */
XI_FPOP = 0xd8dd, /* Really fstp st0. */ XI_FPOP = 0xd8dd, /* Really fstp st0. */
XI_FPOP1 = 0xd9dd, /* Really fstp st1. */ XI_FPOP1 = 0xd9dd, /* Really fstp st1. */
XI_FRNDINT = 0xfcd9, XI_FRNDINT = 0xfcd9,
XI_FSIN = 0xfed9,
XI_FCOS = 0xffd9,
XI_FPTAN = 0xf2d9,
XI_FPATAN = 0xf3d9,
XI_FSCALE = 0xfdd9, XI_FSCALE = 0xfdd9,
XI_FYL2X = 0xf1d9, XI_FYL2X = 0xf1d9,

View File

@ -48,7 +48,6 @@ double lj_vm_foldarith(double x, double y, int op)
case IR_NEG - IR_ADD: return -x; break; case IR_NEG - IR_ADD: return -x; break;
case IR_ABS - IR_ADD: return fabs(x); break; case IR_ABS - IR_ADD: return fabs(x); break;
#if LJ_HASJIT #if LJ_HASJIT
case IR_ATAN2 - IR_ADD: return atan2(x, y); break;
case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break; case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break;
case IR_MIN - IR_ADD: return x < y ? x : y; break; case IR_MIN - IR_ADD: return x < y ? x : y; break;
case IR_MAX - IR_ADD: return x > y ? x : y; break; case IR_MAX - IR_ADD: return x > y ? x : y; break;
@ -129,14 +128,9 @@ double lj_vm_foldfpm(double x, int fpm)
case IRFPM_CEIL: return lj_vm_ceil(x); case IRFPM_CEIL: return lj_vm_ceil(x);
case IRFPM_TRUNC: return lj_vm_trunc(x); case IRFPM_TRUNC: return lj_vm_trunc(x);
case IRFPM_SQRT: return sqrt(x); case IRFPM_SQRT: return sqrt(x);
case IRFPM_EXP: return exp(x);
case IRFPM_EXP2: return lj_vm_exp2(x); case IRFPM_EXP2: return lj_vm_exp2(x);
case IRFPM_LOG: return log(x); case IRFPM_LOG: return log(x);
case IRFPM_LOG2: return lj_vm_log2(x); case IRFPM_LOG2: return lj_vm_log2(x);
case IRFPM_LOG10: return log10(x);
case IRFPM_SIN: return sin(x);
case IRFPM_COS: return cos(x);
case IRFPM_TAN: return tan(x);
default: lua_assert(0); default: lua_assert(0);
} }
return 0; return 0;