fix(shader): choose f64 arithmetic NaN results on the bits #18536

Merged
angrysandhill merged 2 commits from upstream-pr/18536 into main 2026-11-16 06:09:10 +00:00
angrysandhill commented 2026-11-11 20:40:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by angrysandhill on 2026-11-11T20:40:47Z.


What

v_add_f64, v_mul_f64, v_fma_f64 and v_div_fmas_f64 take their NaN result from the SPIR-V backend, which always quiets the first NaN source. On RDNA2 (gfx1031, RX 6750 XT; the PS5's gfx1013 is not measured) two cases differ:

  • IEEE_MODE clear: a signalling NaN source comes back unchanged, and the recompiler quieted it. Shaders without a known float mode run this way too. The f64 unary forms and the f32 arithmetic already follow this rule. #2035 brought it to the unary f64 forms and recorded v_add/mul/fma_f64 as still left to the driver.
  • Both modes: in v_fma_f64 and v_div_fmas_f64 (VCC clear and set), an inf * 0 product gives the default NaN 0xfff8000000000000 even when src2 is a NaN, and the recompiler returned src2. f32 and f16 fma already follow this rule.

The change:

  • Backend (FusedMultiplyAdd): a NaN src2 no longer wins over an invalid product. That makes the backend's choice the full IEEE=1 rule.
  • Translation (nanResultF64, IEEE=0 only): picks the first NaN source after its modifiers, unquieted; src2 loses to an invalid product. float64Operation and vDivFmasF64 use it.
    • Integer f64 path: it sits above the backend, so the integer path (devices without shaderFloat64) gets it too.
    • IEEE=1: it emits nothing.
    • IEEE=0: it adds a second NaN choice after the backend's: a NaN test and two selects per source, plus the inf * 0 test in the fma forms.

Tested

Setup: Windows 10 Home 22H2 (10.0.19045), AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (driver 32.0.21045.16010), WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON. The results below were run on main 65351c2931 and on this branch as it was then, based on it, unless noted. The code was tested as 291c2d7a95; the commit since differs only in a shorter shader.md sentence.

Rebased on 2026-11-14 onto main 0f4538fa02 as 3c1ebd954c, because #16889 edited the shader.md entry above this one. The code diff is unchanged (same patch ID) and the shader.md change is the same line. Rerun at 3c1ebd954c:

  • Negative control: fails with the same message as below, on v_add_f64 of 0x7ff4000000000001 and 0x7ff8000000000003.
  • Tests: both f64 tests pass, and the 29 under the validation layer pass with 0 VUID- lines.
  • Full ^(agc_|shader|vector_), 4 at a time: 1,782 tests: 1,756 passed, 21 skipped, 4 failed, 1 not run. Against the same run on main (1,781 tests), the extra test is the new one, which passes, and three failed here but passed there: agc_driver_linear_readback_label_off, agc_driver_frame_replay_rewind_store and agc_storage_dcc_mip_writeback. Rerun one at a time, those three pass 6 times out of 6 on this branch and on main. agc_driver_sample_order_grid_reference fails on main too. agc_driver_upload_cache_read_only_flush does not build with MinGW on main (it calls setenv).

RDNA2 measurement (hardware oracle, HIP runtime), compared value by value with the recompiler through HW_ORACLE_RUNTIME=vulkan.

  • Rows: 169 operand pairs from 13 values:
    • four signalling NaNs: payload in the high word or only the low word, both signs;
    • two quiet NaNs;
    • ±inf, ±0, 1.0, -1.5 and the smallest denormal.
  • Forms: 24, with abs or neg on each source position in some of them:
    • v_add_f64, including the subtraction form;
    • v_mul_f64;
    • v_fma_f64 with the operands permuted and with the inline constants 0 and 1.0 (inf * 0 with a NaN src2);
    • v_div_fmas_f64 with VCC clear and set.
  • Settings: IEEE 0 and 1, with f64 denormals kept and flushed.
  • Rule check: the rule above predicts all 12,582 NaN results the hardware returned. v_div_fmas_f64 keeps denormal factors, as shader.md already records, so with denormals flushed inf * denormal stays infinite there and is not an invalid product.
main this PR
f64 results that differ from the hardware, of 16,224 4,064 0
of which with IEEE=1 (inf * 0 with a NaN src2) 168 0

Tests:

  • New agc_driver_f64_arithmetic_nan: 32 of the pairs through 8 of the forms. The expected values are RDNA2's.
    • Float modes: five: none, and IEEE 0 and 1 with f64 denormals kept and flushed.
    • Integer f64 path: an add/mul kernel also runs with Float64 removed from the target, in three modes.
    • On main with only this test file added it fails: f64 arithmetic nan: no float mode lane 0 v_add_f64 of 0x7ff4000000000001 and 0x7ff8000000000003 is 0x7ffc000000000001, expected 0x7ff4000000000001.
    • With this change it passes.
  • agc_driver_f64_arithmetic: its run without a float mode (IEEE=0) expected the signalling-NaN rows 7, 13, 29 and 30 quieted in the add, mul and fma columns.
    • RDNA2: returns them unchanged: 20 of its 512 words differ.
    • IEEE=1 run: already matched, 0 of 512.
    • Change: at IEEE=0 the check now clears the quiet bit in those columns. The new expectations match the hardware on all 512 words in both modes.
  • Without the backend half: the new test fails on v_fma_f64 of inf, 0 and 0x7ff4000000000001, which returns 0x7ffc000000000001 where the hardware gives 0xfff8000000000000.
  • Full ^(agc_|shader|vector_), 4 at a time: 1,647 tests: 1,628 passed, 18 skipped, 1 failed.
    • Against main: the same run on main (1,646 tests) gives every test the same outcome. The only extra test is the new one, which passes.
    • The failure: agc_driver_sample_order_grid_reference fails on main too, with the same message (the 2x checkerboard differs from the exact path in 2,427 samples). It is the odd-row failure that #18229 reports on an RX 6700 XT; #9503 addresses it (not run here).
  • Validation layer (1.4.367): the 29 tests matching -R "^agc_driver_(f64|div|nan|vopc_compare_f64|cvt_f32_f64|f32_arithmetic_nan).*$" (28 on main, which lacks the new one), one at a time with ctest -V, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer, ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1.
    • Layer: no log has was not found but was requested by env var VK_INSTANCE_LAYERS. Every test that opens a device logs the layer insertion; agc_driver_f64_denorm_preserve_spirv opens none.
    • Result: no VUID- line on either side.

Debt: shader.md no longer lists the f64 arithmetic as left to the driver, and records the measured rule in one sentence. check_conventions.py warns that this entry is over 1,000 characters. It is already 3,118 on main 0f4538fa02; this adds 200.

Overlap, rechecked at 3c1ebd954c: pr_overlap.py --branch reports no conflict in code. It cannot see conflicts in the debt files: .gitattributes merges them with the union driver, which GitHub ignores. A plain three-way merge of shader.md against the 98 open PRs that touch it adds conflicts with four:

  • my #19637 edits this same entry, in a different sentence; whichever of the two merges second needs a rebase;
  • #15719 and #17733 add an entry after it;
  • #19505 edits the next entry.

None changes this change's text, so a resolution keeps both sides.

Not measured: the PS5's gfx1013, and hosts other than this one.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (32.0.21045.16010), Windows 10 22H2
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5266480710 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18536). Originally posted by [angrysandhill](https://github.com/angrysandhill) on 2026-11-11T20:40:47Z. --- ### What `v_add_f64`, `v_mul_f64`, `v_fma_f64` and `v_div_fmas_f64` take their NaN result from the SPIR-V backend, which always quiets the first NaN source. On RDNA2 (gfx1031, RX 6750 XT; the PS5's gfx1013 is not measured) two cases differ: - **IEEE_MODE clear:** a signalling NaN source comes back unchanged, and the recompiler quieted it. Shaders without a known float mode run this way too. The f64 unary forms and the f32 arithmetic already follow this rule. #2035 brought it to the unary f64 forms and recorded `v_add/mul/fma_f64` as still left to the driver. - **Both modes:** in `v_fma_f64` and `v_div_fmas_f64` (VCC clear and set), an inf * 0 product gives the default NaN `0xfff8000000000000` even when src2 is a NaN, and the recompiler returned src2. f32 and f16 fma already follow this rule. The change: - **Backend** (`FusedMultiplyAdd`): a NaN src2 no longer wins over an invalid product. That makes the backend's choice the full IEEE=1 rule. - **Translation** (`nanResultF64`, IEEE=0 only): picks the first NaN source after its modifiers, unquieted; src2 loses to an invalid product. `float64Operation` and `vDivFmasF64` use it. - **Integer f64 path:** it sits above the backend, so the integer path (devices without shaderFloat64) gets it too. - **IEEE=1:** it emits nothing. - **IEEE=0:** it adds a second NaN choice after the backend's: a NaN test and two selects per source, plus the inf * 0 test in the fma forms. ### Tested **Setup:** Windows 10 Home 22H2 (10.0.19045), AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (driver 32.0.21045.16010), WinLibs GCC 15.2.0 r7 UCRT, Release, `BUILD_TESTING=ON`. The results below were run on `main` `65351c2931` and on this branch as it was then, based on it, unless noted. The code was tested as `291c2d7a95`; the commit since differs only in a shorter shader.md sentence. **Rebased on 2026-11-14** onto main `0f4538fa02` as `3c1ebd954c`, because #16889 edited the shader.md entry above this one. The code diff is unchanged (same patch ID) and the shader.md change is the same line. Rerun at `3c1ebd954c`: - **Negative control:** fails with the same message as below, on `v_add_f64` of `0x7ff4000000000001` and `0x7ff8000000000003`. - **Tests:** both f64 tests pass, and the 29 under the validation layer pass with 0 `VUID-` lines. - **Full `^(agc_|shader|vector_)`, 4 at a time:** 1,782 tests: 1,756 passed, 21 skipped, 4 failed, 1 not run. Against the same run on `main` (1,781 tests), the extra test is the new one, which passes, and three failed here but passed there: `agc_driver_linear_readback_label_off`, `agc_driver_frame_replay_rewind_store` and `agc_storage_dcc_mip_writeback`. Rerun one at a time, those three pass 6 times out of 6 on this branch and on `main`. `agc_driver_sample_order_grid_reference` fails on `main` too. `agc_driver_upload_cache_read_only_flush` does not build with MinGW on `main` (it calls `setenv`). **RDNA2 measurement** ([hardware oracle](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/HW_ORACLE.md), HIP runtime), compared value by value with the recompiler through `HW_ORACLE_RUNTIME=vulkan`. - **Rows:** 169 operand pairs from 13 values: - four signalling NaNs: payload in the high word or only the low word, both signs; - two quiet NaNs; - ±inf, ±0, 1.0, -1.5 and the smallest denormal. - **Forms:** 24, with `abs` or `neg` on each source position in some of them: - `v_add_f64`, including the subtraction form; - `v_mul_f64`; - `v_fma_f64` with the operands permuted and with the inline constants 0 and 1.0 (inf * 0 with a NaN src2); - `v_div_fmas_f64` with VCC clear and set. - **Settings:** IEEE 0 and 1, with f64 denormals kept and flushed. - **Rule check:** the rule above predicts all 12,582 NaN results the hardware returned. `v_div_fmas_f64` keeps denormal factors, as shader.md already records, so with denormals flushed inf * denormal stays infinite there and is not an invalid product. | | `main` | this PR | |---|---|---| | f64 results that differ from the hardware, of 16,224 | 4,064 | 0 | | of which with IEEE=1 (inf * 0 with a NaN src2) | 168 | 0 | **Tests:** - **New `agc_driver_f64_arithmetic_nan`:** 32 of the pairs through 8 of the forms. The expected values are RDNA2's. - **Float modes:** five: none, and IEEE 0 and 1 with f64 denormals kept and flushed. - **Integer f64 path:** an add/mul kernel also runs with Float64 removed from the target, in three modes. - **On `main` with only this test file added** it fails: `f64 arithmetic nan: no float mode lane 0 v_add_f64 of 0x7ff4000000000001 and 0x7ff8000000000003 is 0x7ffc000000000001, expected 0x7ff4000000000001`. - **With this change** it passes. - **`agc_driver_f64_arithmetic`:** its run without a float mode (IEEE=0) expected the signalling-NaN rows 7, 13, 29 and 30 quieted in the add, mul and fma columns. - **RDNA2:** returns them unchanged: 20 of its 512 words differ. - **IEEE=1 run:** already matched, 0 of 512. - **Change:** at IEEE=0 the check now clears the quiet bit in those columns. The new expectations match the hardware on all 512 words in both modes. - **Without the backend half:** the new test fails on `v_fma_f64` of inf, 0 and `0x7ff4000000000001`, which returns `0x7ffc000000000001` where the hardware gives `0xfff8000000000000`. - **Full `^(agc_|shader|vector_)`, 4 at a time:** 1,647 tests: 1,628 passed, 18 skipped, 1 failed. - **Against `main`:** the same run on `main` (1,646 tests) gives every test the same outcome. The only extra test is the new one, which passes. - **The failure:** `agc_driver_sample_order_grid_reference` fails on `main` too, with the same message (the 2x checkerboard differs from the exact path in 2,427 samples). It is the odd-row failure that #18229 reports on an RX 6700 XT; #9503 addresses it (not run here). - **Validation layer** (1.4.367): the 29 tests matching `-R "^agc_driver_(f64|div|nan|vopc_compare_f64|cvt_f32_f64|f32_arithmetic_nan).*$"` (28 on `main`, which lacks the new one), one at a time with `ctest -V`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer`, `ANYPS5_NO_SHADER_CACHE=1` and `ANYPS5_REQUIRE_VULKAN=1`. - **Layer:** no log has `was not found but was requested by env var VK_INSTANCE_LAYERS`. Every test that opens a device logs the layer insertion; `agc_driver_f64_denorm_preserve_spirv` opens none. - **Result:** no `VUID-` line on either side. **Debt:** shader.md no longer lists the f64 arithmetic as left to the driver, and records the measured rule in one sentence. `check_conventions.py` warns that this entry is over 1,000 characters. It is already 3,118 on `main` `0f4538fa02`; this adds 200. **Overlap**, rechecked at `3c1ebd954c`: `pr_overlap.py --branch` reports no conflict in code. It cannot see conflicts in the debt files: `.gitattributes` merges them with the union driver, which GitHub ignores. A plain three-way merge of `shader.md` against the 98 open PRs that touch it adds conflicts with four: - my #19637 edits this same entry, in a different sentence; whichever of the two merges second needs a rebase; - #15719 and #17733 add an entry after it; - #19505 edits the next entry. None changes this change's text, so a resolution keeps both sides. **Not measured:** the PS5's gfx1013, and hosts other than this one. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (32.0.21045.16010), Windows 10 22H2 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!18536
No description provided.