Repository navigation
feat(shader): choose f32 arithmetic NaN results on the bits - #2083
Merged
Merged
Conversation
Co-Authored-By: Claude <noreply@anthropic.com>
This was referenced Oct 9, 2026
9 tasks done
This was referenced Oct 9, 2026
adriadam10
pushed a commit
that referenced
this pull request
Oct 10, 2026
#2083 chooses the NaN result of the f32 fma family on the bits: the first NaN source, quieted in IEEE mode, with the addend skipped when the product is infinity times zero, and 0xffc00000 otherwise. The selection and the infinity-times-zero test ran for every fma, but a NaN source always gives a NaN result, so for any other result they return it unchanged. FPNanResultFma32 carries the result and the three sources, with the IEEE mode as a flag. The SPIR-V emitter makes the selection, including the infinity-times-zero test, in a branch taken only for a NaN result, and the SRT evaluator computes it exactly. Add, sub and mul keep the inline selects: with two sources a branch costs more than it saves.
This was referenced Oct 10, 2026
12 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Part of #981. The NaN result of f32 add, sub, mul and fma was whatever NaN the driver produced. Vulkan doesn't specify its payload or sign, and NVIDIA returns a canonical NaN. The NaN is now chosen on the bits, following the hardware and
IEEE_MODE(#1816, #1867).Measured on RDNA2 (gfx1035) with the HW oracle, over 1000 operand triples (quiet and signaling NaNs of both signs and payloads, ±inf, ±0, ±1) at IEEE=0 and IEEE=1. The ops were
v_add/sub/subrev/mul/fma/fmac/fmamk/fmaak/mul_legacy/fma_legacy_f32,v_fma_mix_f32,v_add_f32 -v4, |v5|andv_mul_f32 mul:2. The rule below matches every cell (0 mismatches):v_sub_f32andv_subrev_f32add the negation of the subtrahend, so a NaN in that position comes out with its sign flipped (src0 + -src1,-src0 + src1).0xffc00000.The sign flip is applied as an integer XOR.
OpFNegateisn't relied on for a NaN's payload.The f16 arithmetic,
v_fma_mix_f32with f16 sources,v_cubema,v_mac_legacy_f32and the separately rounded mad/mac forms (gfx1035 doesn't have them) still leave the NaN to the driver. TechnicalDebt says so.Tested
Linux (CachyOS, GCC), RADV (gfx1035) and CI's lavapipe (Mesa 25.2.8).
agc_driver_f32_arithmetic_nan: 16 instructions (including modifiers, omod and clamp) × 32 rows captured on hardware at IEEE=0 and IEEE=1. 624 of the 1024 cells are NaN, and every cell is compared bit for bit, because the translation now chooses the NaN bits. The non-NaN rows use exact values (±0, ±1, ±inf), so the result doesn't depend on whether the driver fusesFma.agc_driver_f32_denormal_flush,agc_driver_nan_quieting,agc_driver_f32_output_modifier,agc_driver_divisionandvector_fused_multiply_addpass unchanged. Full Release ctest: 494/494.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)