nir: Make some notes about fsign versus NaN

Graphics / Mesa 3D Graphics Library / Mesa - Ian Romanick [] - 5 January 2021 02:07 UTC

This commit only documents the current behavior, even if that behavior is not the behavior preferred by the relevant specs.

In SPIR-V, there are two flavors of the sign instruction, and each lives in an extended instruction set. The GLSL.std.450 FSign instruction is defined as:

Result is 1.0 if x > 0, 0.0 if x = 0, or -1.0 if x < 0.

This also matches the GLSL 4.60 definition.

However, the OpenCL.ExtendedInstructionSet.100 sign instruction is defined as:

Returns 1.0 if x > 0, -0.0 if x = -0.0, +0.0 if x = +0.0, or -1.0 if x < 0. Returns 0.0 if x is a NaN.

There are two differences. Each treats -0.0 differently, and each also treats NaN differently. Specifically, GLSL.std.450 FSign does not define any specific behavior for NaN.

There has been some discussion in Khronos about the NaN behavior of GLSL.std.450 FSign. As part of that discussion, I did some research into how we treat NaN for nir_op_fsign, and this commit just captures some of those notes.

v2: Document the expected behavior of nir_op_fsign more thoroughly. Suggested by Rhys. Note that the current implementation of constant folding does not produce the expected result for NaN. Suggested by Caio.

363efc28234 nir: Make some notes about fsign versus NaN
src/compiler/nir/ | 15 +++++++++++++++
src/compiler/nir/ | 8 ++++++++
2 files changed, 23 insertions(+)


  • Share