From b222647a8ead16b462f544e43ed2c4df43c3101c Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Thu, 1 Jun 2023 19:22:29 -0700 Subject: [PATCH] jit: compile FLOOR and CEILING --- src/zc/jit.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/zc/jit.cpp b/src/zc/jit.cpp index 67228611a2..25889099ce 100644 --- a/src/zc/jit.cpp +++ b/src/zc/jit.cpp @@ -4,7 +4,7 @@ // * Preprocess the ZASM byte code into function sections, and only compile one function at a time when deemed "hot". // * Multiple PUSHR (for example: Maths in playground.qst) commands could be combined to only modify the stack index pointer // just once. Could be problematic for cases where an overflow might happen (detect this?). Same for POP. -// * Compile: LSHIFTR RSHIFTR FLOOR CEILING +// * Compile: LSHIFTR RSHIFTR #include "zc/jit.h" #include "zc/ffscript.h" @@ -399,8 +399,10 @@ static bool command_is_compiled(int command) case ANDV: case CASTBOOLF: case CASTBOOLI: + case CEILING: case DIVR: case DIVV: + case FLOOR: case LOADD: case LOADI: case MAXR: @@ -1157,6 +1159,33 @@ static JittedFunction compile_script(script_data *script) cc.cmp(val2, val); } break; + // https://gcc.godbolt.org/z/r9zq67bK1 + case FLOOR: + { + x86::Gp val = get_z_register(cc, vStackIndex, arg1); + x86::Xmm y = cc.newXmm(); + x86::Mem mem = cc.newQWordConst(ConstPoolScope::kGlobal, 4547007122018943789); + cc.cvtsi2sd(y, val); + cc.mulsd(y, mem); + cc.roundsd(y, y, 9); + cc.cvttsd2si(val, y); + cc.imul(val, val, 10000); + set_z_register(cc, vStackIndex, arg1, val); + } + break; + case CEILING: + { + x86::Gp val = get_z_register(cc, vStackIndex, arg1); + x86::Xmm y = cc.newXmm(); + x86::Mem mem = cc.newQWordConst(ConstPoolScope::kGlobal, 4547007122018943789); + cc.cvtsi2sd(y, val); + cc.mulsd(y, mem); + cc.roundsd(y, y, 10); + cc.cvttsd2si(val, y); + cc.imul(val, val, 10000); + set_z_register(cc, vStackIndex, arg1, val); + } + break; default: { error(script, fmt::format("unhandled command: {}", command));