| ... | ... | @@ -1437,6 +1437,19 @@ pub const CompareOperator = enum { |
| 1437 | 1437 | gt, |
| 1438 | 1438 | /// Not equal (`!=`) |
| 1439 | 1439 | neq, |
| 1440 | |
| 1441 | /// Reverse the direction of the comparison. |
| 1442 | /// Use when swapping the left and right hand operands. |
| 1443 | pub fn reverse(op: CompareOperator) CompareOperator { |
| 1444 | return switch (op) { |
| 1445 | .lt => .gt, |
| 1446 | .lte => .gte, |
| 1447 | .gt => .lt, |
| 1448 | .gte => .lte, |
| 1449 | .eq => .eq, |
| 1450 | .neq => .neq, |
| 1451 | }; |
| 1452 | } |
| 1440 | 1453 | }; |
| 1441 | 1454 | |
| 1442 | 1455 | /// This function does the same thing as comparison operators, however the |
| ... | ... | @@ -1496,6 +1509,15 @@ test "order.compare" { |
| 1496 | 1509 | try testing.expect(order(1, 0).compare(.neq)); |
| 1497 | 1510 | } |
| 1498 | 1511 | |
| 1512 | test "compare.reverse" { |
| 1513 | inline for (@typeInfo(CompareOperator).Enum.fields) |op_field| { |
| 1514 | const op = @intToEnum(CompareOperator, op_field.value); |
| 1515 | try testing.expect(compare(2, op, 3) == compare(3, op.reverse(), 2)); |
| 1516 | try testing.expect(compare(3, op, 3) == compare(3, op.reverse(), 3)); |
| 1517 | try testing.expect(compare(4, op, 3) == compare(3, op.reverse(), 4)); |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1499 | 1521 | /// Returns a mask of all ones if value is true, |
| 1500 | 1522 | /// and a mask of all zeroes if value is false. |
| 1501 | 1523 | /// Compiles to one instruction for register sized integers. |