Should -frounding-math prevent folding of 0.f + (-0.f)?

The code in the following Compiler Explorer link prints -0.0 on -O0, but 0.0 on -O1, despite passing -frounding-math (which “forces the compiler to honor the dynamically-set rounding mode”): Compiler Explorer
Because the dynamically-set rounding mode is FE_DOWNWARD, the correct result is -0.0. It sounds to me like a miscompilation, but I’m not sure if it’s worth fixing.

1 Like

The constrained FP intrinsics still have experimental in the name for a reason. I’d still expect a number of bugs here

I think it can happen any time the result differs based on rounding mode, yet there is no information loss in the arithmetic calculation itself.

AFAIK the only case this can happen is with +0 vs -0 results based on rounding.

Zero is the only number representable by two distinct floating point values, so the current check (result of folding would lose information) works in all other cases.

To fix it for folds that result in exactly zero, we’d have to allow the folding in this compilation mode only if the result is always +0 or always -0, regardless of rounding mode.

Yes, it’s a miscompilation. I don’t think there’s any question of “worth it” here – if you ask for -frounding-math (or one of the other modes that implies it), this ought to work.