fix float NaN comparison

This commit is contained in:
dzaima 2023-04-21 16:51:52 +03:00
parent c3347d6815
commit a7ceda6b28

View File

@ -243,9 +243,17 @@ void noop_visit(Value* x);
#endif
#define CMP(W,X) ({ AUTO wt = (W); AUTO xt = (X); (wt>xt?1:0)-(wt<xt?1:0); })
SHOULD_INLINE i32 compareFloat(f64 w, f64 x) {
if (RARE(w!=w || x!=x)) return (w!=w) - (x!=x);
#if __x86_64__
return (w>x) - !(w>=x); // slightly better codegen from being able to reuse the same compare instruction
#else
return (w>x) - (w<x);
#endif
}
NOINLINE i32 compareF(B w, B x);
static i32 compare(B w, B x) { // doesn't consume; -1 if w<x, 1 if w>x, 0 if w≡x; 0==compare(NaN,NaN)
if (isNum(w) & isNum(x)) return CMP(o2fG(w), o2fG(x));
static i32 compare(B w, B x) { // doesn't consume; -1 if w<x, 1 if w>x, 0 if w≡x
if (isNum(w) & isNum(x)) return compareFloat(o2fG(w), o2fG(x));
if (isC32(w) & isC32(x)) return CMP(o2cG(w), o2cG(x));
return compareF(w, x);
}