Repository navigation
Description
Problem
The floating-point branch of unary negation builds +0.0 - value with build_float_sub. The result is named fneg, but the emitted operation is subtraction, not LLVM's unary fneg instruction.
Subtracting positive zero from positive zero produces positive zero under the ordinary default rounding behavior. Negating a variable containing positive zero should instead produce negative zero. Numeric equality does not reveal the difference, but the public bit-conversion helpers do.
Code references:
- llvm/src/expression/rvalue/unary.rs — Operator::Neg for FloatValue
- std/math/float.wave — float_to_bits_f32 and float_to_bits_f64
- LLVM reference — fneg instruction
Source-derived regression
Save as negate_zero.wave:
import("std::math::float")::{float_to_bits_f32, float_to_bits_f64};
fun main() -> i32 {
var zero64: f64 = 0.0;
var zero32: f32 = 0.0;
var negative64: f64 = -zero64;
var negative32: f32 = -zero32;
var bits64: u64 = float_to_bits_f64(negative64);
var bits32: u32 = float_to_bits_f32(negative32);
if (bits64 != (0x8000000000000000 as u64)) {
return 1;
}
if (bits32 != (0x80000000 as u32)) {
return 2;
}
return 0;
}
Expected: exit 0. Negating a variable, rather than relying only on the spelling of a negative literal, exercises the unary-expression lowerer.
The current subtraction path predicts all-zero bits for both results. Separately authored LLVM reductions confirmed the following at O0/O2:
| Operation on positive zero | f32 bits | f64 bits |
|---|---|---|
fsub +0.0, value |
0x00000000 |
0x0000000000000000 |
fneg value |
0x80000000 |
0x8000000000000000 |
Those reductions were hand-written, not emitted by Wave. The supplied Wave input still needs an actual compiler/IR/runtime regression.
Expected behavior and scope
Lower floating-point unary minus with the corresponding LLVM negation operation, preserving the operand's float width. fneg directly reverses the sign bit without requiring an arithmetic subtraction.
Keep integer negation and binary subtraction unchanged. This is not a request to implement dynamic rounding modes, change standard-library rounding functions, or add a runtime helper. Validate the contract without fast-math options that explicitly permit discarding signed zero.
Completion criteria
-
wavec check negate_zero.waveaccepts the source, and the compiled program exits 0 at unoptimized and optimized settings. - Actual unoptimized Wave-generated IR for a nonconstant float parameter contains genuine floating-point negation rather than subtraction from positive zero.
- At both float widths, negating positive zero yields negative zero and negating negative zero yields positive zero, checked by exact bit patterns.
- Cover grouped operands, double negation, variables, and function-returned operands in addition to literals.
- Preserve ordinary finite and infinite values under sign reversal; include a quiet-NaN bit-pattern case without depending on decimal formatting.
- Integer unary minus and floating-point binary subtraction retain their existing behavior.
- Add focused compiler IR tests and runnable Wave cases using the supported LLVM toolchain; numeric approximate equality alone is insufficient for the zero cases.
Difficulty: easy. Related #554 concerns signed-zero behavior inside the standard-library round functions. This issue owns the compiler's unary-minus lowering.
Activity
Fix numeric lowering and standard-library boundary handling (wavefnd#707