Skip to content

Conversation

@andrewrk
Copy link
Member

Rather than lowering float negation as 0.0 - x, this commit makes Zig lower it with a negation AIR instruction. It also adds compiler-rt functions for f128 and f80 negation.

This revealed a problem with the way generic functions work for floats:

Zig guarantees the memory layout of f16, f32, f64, f80, and f128 which
means for generic function purposes, values of these types need to be
compared on the basis of their bits in memory. This means nan-packing
can be used with generic functions, for example.

For comptime_float, the sign is observable, whether it is nan is
observable, but not any more kinds of bit patterns are observable.

This fixes the std.fmt tests that check printing "-nan".

closes #11853

andrewrk added 2 commits June 30, 2022 00:02
Rather than lowering float negation as `0.0 - x`.

 * Add AIR instruction for float negation.
 * Add compiler-rt functions for f128, f80 negation

closes #11853
Zig guarantees the memory layout of f16, f32, f64, f80, and f128 which
means for generic function purposes, values of these types need to be
compared on the basis of their bits in memory. This means nan-packing
can be used with generic functions, for example.

For comptime_float, the sign is observable, whether it is nan is
observable, but not any more kinds of bit patterns are observable.

This fixes the std.fmt tests that check printing "-nan".
@Vexu
Copy link
Member

Vexu commented Jun 30, 2022

It also adds compiler-rt functions for f128 and f80 negation.

Stage1 (and LLVM) just directly generates an XOR with an appropriate constant, I think stage2 should too.

Rather than a compiler-rt call in the case that LLVM does not support
lowering the fneg instruction.
@andrewrk andrewrk merged commit 77a3344 into master Jun 30, 2022
@andrewrk andrewrk deleted the runtime-float-negation branch June 30, 2022 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stage2 can't negate a NaN at runtime

2 participants