Skip to content

Commit b3a27e7

Browse files
authored
Fix issues of fnn.cmp (#1204)
- use native functions to do f.eq and f.ne - only use ZF=0 and CF=0 to do f.lt and f.gt - only use CF=0 to do f.le and f.ge could use comiss and setCC to replace comiss and jmpCC be able to pass f32_cmp and f64_cmp ``` cmp_eq: xor eax, eax ucomisd xmm0, xmm1 mov edx, 0 setnp al cmovne eax, edx ret cmp_ne: xor eax, eax ucomisd xmm0, xmm1 mov edx, 1 setp al cmovne eax, edx ret ```
1 parent 66cd90d commit b3a27e7

File tree

2 files changed

+116
-40
lines changed

2 files changed

+116
-40
lines changed

core/iwasm/fast-jit/cg/x86-64/jit_codegen_x86_64.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ cmp_r_and_jmp_label(JitCompContext *cc, x86::Assembler &a,
363363
case GTS:
364364
{
365365
if (fp_cmp) {
366-
a.jnbe(imm);
366+
a.ja(imm);
367367
}
368368
else {
369369
a.jg(imm);
@@ -373,7 +373,7 @@ cmp_r_and_jmp_label(JitCompContext *cc, x86::Assembler &a,
373373
case LES:
374374
{
375375
if (fp_cmp) {
376-
a.jbe(imm);
376+
a.jnb(imm);
377377
}
378378
else {
379379
a.jng(imm);
@@ -394,7 +394,7 @@ cmp_r_and_jmp_label(JitCompContext *cc, x86::Assembler &a,
394394
case LTS:
395395
{
396396
if (fp_cmp) {
397-
a.jb(imm);
397+
a.ja(imm);
398398
}
399399
else {
400400
a.jl(imm);
@@ -5071,7 +5071,7 @@ cmp_r_and_jmp_relative(JitCompContext *cc, x86::Assembler &a, int32 reg_no,
50715071
case GTS:
50725072
{
50735073
if (fp_cmp) {
5074-
a.jnbe(target);
5074+
a.ja(target);
50755075
}
50765076
else {
50775077
a.jg(target);
@@ -5081,7 +5081,7 @@ cmp_r_and_jmp_relative(JitCompContext *cc, x86::Assembler &a, int32 reg_no,
50815081
case LES:
50825082
{
50835083
if (fp_cmp) {
5084-
a.jbe(target);
5084+
a.jnb(target);
50855085
}
50865086
else {
50875087
a.jng(target);
@@ -5094,15 +5094,14 @@ cmp_r_and_jmp_relative(JitCompContext *cc, x86::Assembler &a, int32 reg_no,
50945094
a.jnb(target);
50955095
}
50965096
else {
5097-
50985097
a.jnl(target);
50995098
}
51005099
break;
51015100
}
51025101
case LTS:
51035102
{
51045103
if (fp_cmp) {
5105-
a.jb(target);
5104+
a.ja(target);
51065105
}
51075106
else {
51085107
a.jl(target);

core/iwasm/fast-jit/fe/jit_emit_compare.c

Lines changed: 110 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "jit_emit_compare.h"
77
#include "../jit_frontend.h"
8+
#include "../jit_codegen.h"
89

910
static bool
1011
jit_compile_op_compare_integer(JitCompContext *cc, IntCond cond, bool is64Bit)
@@ -116,53 +117,129 @@ jit_compile_op_i64_compare(JitCompContext *cc, IntCond cond)
116117
return jit_compile_op_compare_integer(cc, cond, true);
117118
}
118119

120+
static int32
121+
float_cmp_eq(float f1, float f2)
122+
{
123+
if (isnan(f1) || isnan(f2))
124+
return 0;
125+
126+
return f1 == f2;
127+
}
128+
129+
static int32
130+
float_cmp_ne(float f1, float f2)
131+
{
132+
if (isnan(f1) || isnan(f2))
133+
return 1;
134+
135+
return f1 != f2;
136+
}
137+
138+
static int32
139+
double_cmp_eq(double d1, double d2)
140+
{
141+
if (isnan(d1) || isnan(d2))
142+
return 0;
143+
144+
return d1 == d2;
145+
}
146+
147+
static int32
148+
double_cmp_ne(double d1, double d2)
149+
{
150+
if (isnan(d1) || isnan(d2))
151+
return 1;
152+
153+
return d1 != d2;
154+
}
155+
119156
static bool
120157
jit_compile_op_compare_float_point(JitCompContext *cc, FloatCond cond,
121158
JitReg lhs, JitReg rhs)
122159
{
123160
JitReg res, const_zero, const_one;
124161

125-
GEN_INSN(CMP, cc->cmp_reg, lhs, rhs);
162+
if (cond == FLOAT_EQ) {
163+
JitInsn *insn = NULL;
164+
JitRegKind kind = jit_reg_kind(lhs);
126165

127-
res = jit_cc_new_reg_I32(cc);
128-
const_zero = NEW_CONST(I32, 0);
129-
const_one = NEW_CONST(I32, 1);
130-
switch (cond) {
131-
case FLOAT_EQ:
132-
{
133-
GEN_INSN(SELECTEQ, res, cc->cmp_reg, const_one, const_zero);
134-
break;
135-
}
136-
case FLOAT_NE:
137-
{
138-
GEN_INSN(SELECTNE, res, cc->cmp_reg, const_one, const_zero);
139-
break;
166+
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
167+
res = jit_codegen_get_hreg_by_name("eax");
168+
#else
169+
res = jit_cc_new_reg_I32(cc);
170+
#endif
171+
172+
if (kind == JIT_REG_KIND_F32) {
173+
insn = GEN_INSN(CALLNATIVE, res,
174+
NEW_CONST(PTR, (uintptr_t)float_cmp_eq), 2);
140175
}
141-
case FLOAT_LT:
142-
{
143-
GEN_INSN(SELECTLTS, res, cc->cmp_reg, const_one, const_zero);
144-
break;
176+
else {
177+
insn = GEN_INSN(CALLNATIVE, res,
178+
NEW_CONST(PTR, (uintptr_t)double_cmp_eq), 2);
145179
}
146-
case FLOAT_GT:
147-
{
148-
GEN_INSN(SELECTGTS, res, cc->cmp_reg, const_one, const_zero);
149-
break;
180+
if (!insn) {
181+
goto fail;
150182
}
151-
case FLOAT_LE:
152-
{
153-
GEN_INSN(SELECTLES, res, cc->cmp_reg, const_one, const_zero);
154-
break;
183+
*(jit_insn_opndv(insn, 2)) = lhs;
184+
*(jit_insn_opndv(insn, 3)) = rhs;
185+
}
186+
else if (cond == FLOAT_NE) {
187+
JitInsn *insn = NULL;
188+
JitRegKind kind = jit_reg_kind(lhs);
189+
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
190+
res = jit_codegen_get_hreg_by_name("eax");
191+
#else
192+
res = jit_cc_new_reg_I32(cc);
193+
#endif
194+
if (kind == JIT_REG_KIND_F32) {
195+
insn = GEN_INSN(CALLNATIVE, res,
196+
NEW_CONST(PTR, (uintptr_t)float_cmp_ne), 2);
155197
}
156-
case FLOAT_GE:
157-
{
158-
GEN_INSN(SELECTGES, res, cc->cmp_reg, const_one, const_zero);
159-
break;
198+
else {
199+
insn = GEN_INSN(CALLNATIVE, res,
200+
NEW_CONST(PTR, (uintptr_t)double_cmp_ne), 2);
160201
}
161-
default:
162-
{
163-
bh_assert(!"unknown FloatCond");
202+
if (!insn) {
164203
goto fail;
165204
}
205+
*(jit_insn_opndv(insn, 2)) = lhs;
206+
*(jit_insn_opndv(insn, 3)) = rhs;
207+
}
208+
else {
209+
res = jit_cc_new_reg_I32(cc);
210+
const_zero = NEW_CONST(I32, 0);
211+
const_one = NEW_CONST(I32, 1);
212+
switch (cond) {
213+
case FLOAT_LT:
214+
{
215+
GEN_INSN(CMP, cc->cmp_reg, rhs, lhs);
216+
GEN_INSN(SELECTLTS, res, cc->cmp_reg, const_one, const_zero);
217+
break;
218+
}
219+
case FLOAT_GT:
220+
{
221+
GEN_INSN(CMP, cc->cmp_reg, lhs, rhs);
222+
GEN_INSN(SELECTGTS, res, cc->cmp_reg, const_one, const_zero);
223+
break;
224+
}
225+
case FLOAT_LE:
226+
{
227+
GEN_INSN(CMP, cc->cmp_reg, rhs, lhs);
228+
GEN_INSN(SELECTLES, res, cc->cmp_reg, const_one, const_zero);
229+
break;
230+
}
231+
case FLOAT_GE:
232+
{
233+
GEN_INSN(CMP, cc->cmp_reg, lhs, rhs);
234+
GEN_INSN(SELECTGES, res, cc->cmp_reg, const_one, const_zero);
235+
break;
236+
}
237+
default:
238+
{
239+
bh_assert(!"unknown FloatCond");
240+
goto fail;
241+
}
242+
}
166243
}
167244
PUSH_I32(res);
168245

0 commit comments

Comments
 (0)