Value: fix comparison of NaN in compareHeteroAdvanaced

Sema: fix equality comparison of signed zeroes and NaN in compareScalar
tests: add test coverage for vector float comparisons
This commit is contained in:
kcbanner 2024-11-06 19:33:52 -05:00
parent edabcf6192
commit 981f84157c
3 changed files with 17 additions and 1 deletions

View File

@ -38137,6 +38137,11 @@ fn compareScalar(
const pt = sema.pt;
const coerced_lhs = try pt.getCoerced(lhs, ty);
const coerced_rhs = try pt.getCoerced(rhs, ty);
// Equality comparisons of signed zero and NaN need to use floating point semantics
if (coerced_lhs.isFloat(pt.zcu) or coerced_rhs.isFloat(pt.zcu))
return Value.compareHeteroSema(coerced_lhs, op, coerced_rhs, pt);
switch (op) {
.eq => return sema.valuesEqual(coerced_lhs, coerced_rhs, ty),
.neq => return !(try sema.valuesEqual(coerced_lhs, coerced_rhs, ty)),

View File

@ -1132,6 +1132,8 @@ pub fn compareHeteroAdvanced(
else => {},
}
}
if (lhs.isNan(zcu) or rhs.isNan(zcu)) return op == .neq;
return (try orderAdvanced(lhs, rhs, strat, zcu, tid)).compare(op);
}

View File

@ -194,7 +194,7 @@ fn testCmp(comptime T: type) !void {
try expect(x <= 2.0);
}
@setEvalBranchQuota(2_000);
@setEvalBranchQuota(4_000);
var edges = [_]T{
-math.inf(T),
-math.floatMax(T),
@ -210,6 +210,7 @@ fn testCmp(comptime T: type) !void {
};
_ = &edges;
for (edges, 0..) |rhs, rhs_i| {
const rhs_v: @Vector(4, T) = @splat(rhs);
for (edges, 0..) |lhs, lhs_i| {
const no_nan = lhs_i != 5 and rhs_i != 5;
const lhs_order = if (lhs_i < 5) lhs_i else lhs_i - 2;
@ -220,6 +221,14 @@ fn testCmp(comptime T: type) !void {
try expect((lhs > rhs) == (no_nan and lhs_order > rhs_order));
try expect((lhs <= rhs) == (no_nan and lhs_order <= rhs_order));
try expect((lhs >= rhs) == (no_nan and lhs_order >= rhs_order));
const lhs_v: @Vector(4, T) = @splat(lhs);
try expect(@reduce(.And, (lhs_v == rhs_v)) == (no_nan and lhs_order == rhs_order));
try expect(@reduce(.And, (lhs_v != rhs_v)) == !(no_nan and lhs_order == rhs_order));
try expect(@reduce(.And, (lhs_v < rhs_v)) == (no_nan and lhs_order < rhs_order));
try expect(@reduce(.And, (lhs_v > rhs_v)) == (no_nan and lhs_order > rhs_order));
try expect(@reduce(.And, (lhs_v <= rhs_v)) == (no_nan and lhs_order <= rhs_order));
try expect(@reduce(.And, (lhs_v >= rhs_v)) == (no_nan and lhs_order >= rhs_order));
}
}
}