Skip to content

Commit 981f841

Browse files
committed
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
1 parent edabcf6 commit 981f841

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/Sema.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38137,6 +38137,11 @@ fn compareScalar(
3813738137
const pt = sema.pt;
3813838138
const coerced_lhs = try pt.getCoerced(lhs, ty);
3813938139
const coerced_rhs = try pt.getCoerced(rhs, ty);
38140+
38141+
// Equality comparisons of signed zero and NaN need to use floating point semantics
38142+
if (coerced_lhs.isFloat(pt.zcu) or coerced_rhs.isFloat(pt.zcu))
38143+
return Value.compareHeteroSema(coerced_lhs, op, coerced_rhs, pt);
38144+
3814038145
switch (op) {
3814138146
.eq => return sema.valuesEqual(coerced_lhs, coerced_rhs, ty),
3814238147
.neq => return !(try sema.valuesEqual(coerced_lhs, coerced_rhs, ty)),

src/Value.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,8 @@ pub fn compareHeteroAdvanced(
11321132
else => {},
11331133
}
11341134
}
1135+
1136+
if (lhs.isNan(zcu) or rhs.isNan(zcu)) return op == .neq;
11351137
return (try orderAdvanced(lhs, rhs, strat, zcu, tid)).compare(op);
11361138
}
11371139

test/behavior/floatop.zig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn testCmp(comptime T: type) !void {
194194
try expect(x <= 2.0);
195195
}
196196

197-
@setEvalBranchQuota(2_000);
197+
@setEvalBranchQuota(4_000);
198198
var edges = [_]T{
199199
-math.inf(T),
200200
-math.floatMax(T),
@@ -210,6 +210,7 @@ fn testCmp(comptime T: type) !void {
210210
};
211211
_ = &edges;
212212
for (edges, 0..) |rhs, rhs_i| {
213+
const rhs_v: @Vector(4, T) = @splat(rhs);
213214
for (edges, 0..) |lhs, lhs_i| {
214215
const no_nan = lhs_i != 5 and rhs_i != 5;
215216
const lhs_order = if (lhs_i < 5) lhs_i else lhs_i - 2;
@@ -220,6 +221,14 @@ fn testCmp(comptime T: type) !void {
220221
try expect((lhs > rhs) == (no_nan and lhs_order > rhs_order));
221222
try expect((lhs <= rhs) == (no_nan and lhs_order <= rhs_order));
222223
try expect((lhs >= rhs) == (no_nan and lhs_order >= rhs_order));
224+
225+
const lhs_v: @Vector(4, T) = @splat(lhs);
226+
try expect(@reduce(.And, (lhs_v == rhs_v)) == (no_nan and lhs_order == rhs_order));
227+
try expect(@reduce(.And, (lhs_v != rhs_v)) == !(no_nan and lhs_order == rhs_order));
228+
try expect(@reduce(.And, (lhs_v < rhs_v)) == (no_nan and lhs_order < rhs_order));
229+
try expect(@reduce(.And, (lhs_v > rhs_v)) == (no_nan and lhs_order > rhs_order));
230+
try expect(@reduce(.And, (lhs_v <= rhs_v)) == (no_nan and lhs_order <= rhs_order));
231+
try expect(@reduce(.And, (lhs_v >= rhs_v)) == (no_nan and lhs_order >= rhs_order));
223232
}
224233
}
225234
}

0 commit comments

Comments
 (0)