diff options
| author | Cody Tapscott <topolarity@tapscott.me> | 2022-10-05 05:34:52 -0700 |
|---|---|---|
| committer | Cody Tapscott <topolarity@tapscott.me> | 2022-11-10 12:24:02 -0700 |
| commit | 7b978bf1e05727f15fc83ae7d2455c08833cc439 (patch) | |
| tree | 61648de296eb45d762d0bc8440d144a32dea896d /src/value.zig | |
| parent | b1357091ae0876645d75a608bd1e26f21cec7c13 (diff) | |
| download | zig-7b978bf1e05727f15fc83ae7d2455c08833cc439.tar.gz zig-7b978bf1e05727f15fc83ae7d2455c08833cc439.zip | |
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.
Diffstat (limited to 'src/value.zig')
| -rw-r--r-- | src/value.zig | 20 |
1 files changed, 11 insertions, 9 deletions
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, |
