Repository navigation
Description
Problem
The LLVM backend lowers floating-point != with FloatPredicate::ONE in all three numeric paths: float/float, int/float, and float/int. ONE requires ordered operands, so a quiet NaN makes the comparison false.
Equality already uses ordered equality. As a result, n == n and n != n are both false for NaN, while !(n == n) is true. The != operator does not behave as the negation of equality for these operands.
Code references:
- llvm/src/expression/rvalue/binary.rs — Operator::NotEqual in the three floating-point paths
- std/math/float.wave — nan_f32 and nan_f64 test inputs
- LLVM reference — ordered and unordered fcmp predicates
Source-derived regression
Save as nan_inequality.wave:
import("std::math::float")::{nan_f32, nan_f64};
fun main() -> i32 {
var n64: f64 = nan_f64();
var n32: f32 = nan_f32();
var equal: bool = n64 == n64;
var unequal: bool = n64 != n64;
var negated_equal: bool = !(n64 == n64);
var unequal32: bool = n32 != n32;
if (equal) {
return 1;
}
if (!unequal) {
return 2;
}
if (!negated_equal) {
return 3;
}
if (!unequal32) {
return 4;
}
return 0;
}
Expected: exit 0, with equality false and both inequality forms true. The current ONE selection predicts a false result for each NaN inequality. This Wave program has not yet been compiled or executed for this report.
A separately authored LLVM predicate test produced OEQ=false, ONE=false, and UNE=true for quiet NaN at both float widths and at O0/O2. That tests the selected LLVM operations, not the complete Wave pipeline.
Expected behavior and scope
Use unordered-or-not-equal semantics for floating-point !=, such as FloatPredicate::UNE, consistently across the three numeric paths. Keep equality and ordered relational comparisons unchanged.
This does not change integer/pointer comparisons, introduce approximate equality, or redefine mixed numeric promotion. Tests must use ordinary floating-point settings, not options that explicitly permit assuming NaNs do not occur.
Completion criteria
-
wavec check nan_inequality.waveaccepts the source, and the compiled program exits 0 at unoptimized and optimized settings. - Actual Wave-generated IR uses unordered inequality for float/float, int/float, and float/int
!=paths. - Cover quiet NaN on the left, right, and both sides for
f32andf64. - Use typed integer variables in mixed tests so literal contextualization cannot bypass the mixed lowering branches.
- Preserve results for equal/unequal finite values, equal/opposite infinities, and positive/negative zero.
- Verify
a != bagrees with!(a == b)for the supported comparison cases, without changing<,>,<=, or>=predicates. - Add focused compiler IR tests and runnable Wave cases; no new runtime helper or external math library is required.
Difficulty: easy. Related #555 concerns the standard library's approximate-comparison implementation, not the compiler's != operator.
Activity
Fix numeric lowering and standard-library boundary handling (wavefnd#707