From 7b978bf1e05727f15fc83ae7d2455c08833cc439 Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Wed, 5 Oct 2022 05:34:52 -0700 Subject: stage2: Rename `Value.compare` to `compareAll`, etc. These functions have a very error-prone API. They are essentially `all(cmp(op, ...))` but that's not reflected in the name. This renames these functions to `compareAllAgainstZero...` etc. for clarity and fixes >20 locations where the predicate was incorrect. In the future, the scalar `compare` should probably be split off from the vector comparison. Rank-polymorphic programming is great, but a proper implementation in Zig would decouple comparison and reduction, which then needs a way to fuse ops at comptime. --- src/value.zig | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src/value.zig') diff --git a/src/value.zig b/src/value.zig index 792ec5068f..6b5ffe02ab 100644 --- a/src/value.zig +++ b/src/value.zig @@ -2005,8 +2005,8 @@ pub const Value = extern union { } /// Asserts the values are comparable. Both operands have type `ty`. - /// Vector results will be reduced with AND. - pub fn compare(lhs: Value, op: std.math.CompareOperator, rhs: Value, ty: Type, mod: *Module) bool { + /// For vectors, returns true if comparison is true for ALL elements. + pub fn compareAll(lhs: Value, op: std.math.CompareOperator, rhs: Value, ty: Type, mod: *Module) bool { if (ty.zigTypeTag() == .Vector) { var i: usize = 0; while (i < ty.vectorLen()) : (i += 1) { @@ -2035,21 +2035,23 @@ pub const Value = extern union { } /// Asserts the value is comparable. - /// Vector results will be reduced with AND. - pub fn compareWithZero(lhs: Value, op: std.math.CompareOperator) bool { - return compareWithZeroAdvanced(lhs, op, null) catch unreachable; + /// For vectors, returns true if comparison is true for ALL elements. + /// + /// Note that `!compareAllWithZero(.eq, ...) != compareAllWithZero(.neq, ...)` + pub fn compareAllWithZero(lhs: Value, op: std.math.CompareOperator) bool { + return compareAllWithZeroAdvanced(lhs, op, null) catch unreachable; } - pub fn compareWithZeroAdvanced( + pub fn compareAllWithZeroAdvanced( lhs: Value, op: std.math.CompareOperator, sema_kit: ?Module.WipAnalysis, ) Module.CompileError!bool { switch (lhs.tag()) { - .repeated => return lhs.castTag(.repeated).?.data.compareWithZeroAdvanced(op, sema_kit), + .repeated => return lhs.castTag(.repeated).?.data.compareAllWithZeroAdvanced(op, sema_kit), .aggregate => { for (lhs.castTag(.aggregate).?.data) |elem_val| { - if (!(try elem_val.compareWithZeroAdvanced(op, sema_kit))) return false; + if (!(try elem_val.compareAllWithZeroAdvanced(op, sema_kit))) return false; } return true; }, @@ -2982,7 +2984,7 @@ pub const Value = extern union { .int_i64, .int_big_positive, .int_big_negative, - => compareWithZero(self, .eq), + => compareAllWithZero(self, .eq), .undef => unreachable, .unreachable_value => unreachable, -- cgit v1.2.3