fix(shader): choose v_fma_mix_f32 NaN results on the bits with f16 sources #19637

Merged
angrysandhill merged 4 commits from upstream-pr/19637 into main 2026-11-21 20:57:03 +00:00
angrysandhill commented 2026-11-13 17:11:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by angrysandhill on 2026-11-13T17:11:42Z.


What

v_fma_mix_f32 with an f16 source (any op_sel_hi bit set) left its NaN results to the driver; shader.md lists this as debt. #7074 (e98cf7f9a0) already gave its invalid operations the default NaN.

On RDNA2 (gfx1031, RX 6750 XT), the hardware applies the same rule as the f32 forms do since #2083:

  • the first NaN source in operand order wins, after its modifiers;
  • it is quieted only with IEEE=1;
  • src2 loses to an invalid product (inf * 0), which gives 0xffc00000.

An f16 NaN source is widened on its bits before its abs and neg: its sign, 0x7f800000, and its mantissa shifted left by 13. 0x7c01 becomes 0x7f802000.

Main differs from the hardware in three ways:

  • f16 sNaN at IEEE=1: the source comes back unquieted. Three f16 sources 0x7c01, 0x3c00, 0x3c00 give 0x7f802000; the hardware gives 0x7fc02000.
  • f32 sNaN in a mixed form: once any source is f16, the instruction leaves the f32 path, so an f32 sNaN isn't quieted either. op_sel_hi:[1,1,0] with src2 0x7f800001 gives 0x7f800001; the hardware gives 0x7fc00001.
  • Negated NaN factors (-a * -b): in the oracle's kernels, which hold that one instruction, the result lost the sign that the negation gives the NaN. -v4, -v5 with f16 0x7c01 in src0 gave 0x7f802000 at IEEE=0; the hardware gives 0xff802000.
    • A single negation kept the sign, so presumably the driver cancels the pair.
    • In the new test's shader, which runs eight forms together, main kept the sign on the AMD Windows driver. tablefrogpillow's RADV runs (Vega, RDNA2 and RDNA3) do fail on it: 0x7f802000, expected 0xff802000.
    • The fix builds these bits with integer operations, so it doesn't depend on what the driver does here.

floatTernary now passes every v_fma_mix_f32 source to nanResultF32. A new mixNanSourceF32, next to readMixF32, gives an f16 source as its widened NaN bits when the f16 lane is a NaN. It reads the lane readF16LaneAsF32 reads, including the high lane with op_sel. The invalid-product test still uses the flushed values the arithmetic uses. f32-only forms build the same IR as before.

With APS5_FAST_F32 set, nanResultF32 returns early, as it does for the f32 forms. Since 6ef285d9e2 (ea27c39e75 before the rebase), the select #7074 added for invalid operations runs only in that mode, where it is still the only thing giving them the default NaN. With exact arithmetic, nanResultF32 gives the same result.

tests/execution/FmaMixF32NanSources.cpp (agc_driver_fma_mix_f32_nan_sources) pins 640 results, all measured on the RX 6750 XT:

  • 16 rows and 8 forms;
  • forms: three f16 sources, -v4, -v5, |v4| with -|v6|, the high lanes through op_sel, and four mixes of f16 and f32 sources with neg and abs;
  • modes: FLOAT_MODE 0xc0 and 0xf0 at IEEE 0 and 1, and 0x00 at IEEE 1;
  • a shader without a known float mode is checked against the 0xc0, IEEE=0 results, as F32ArithmeticNan checks it against its IEEE=0 table;
  • it runs with and without fused exact arithmetic, and skips NaN-to-NaN comparisons under APS5_FAST_F32 as F32ArithmeticNan does.

Tested

Windows 10 22H2, AMD Radeon RX 6750 XT (gfx1031), Adrenalin 26.10.1 (driver 32.0.21045.16010), WinLibs GCC 15.2, Release. Main 6e5972c033, changes ee609fe080 and ea27c39e75; the results below are from those unless noted.

Rebased on 2026-11-14 onto main 0f4538fa02 as 951da1b17a and 6ef285d9e2, 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, so the runs posted on the earlier heads still apply. Rerun at 6ef285d9e2:

  • Negative control: fails on the same IEEE=1 row, 0x7f802000, expected 0x7fc02000.

  • Tests: the 11 below pass 11/11, the 5 under the validation layer with 0 VUID- lines, and fast mode 3/3.

  • Oracle: 0 of the 1,456 rows differ from the hardware.

  • Full ^(agc_|shader|vector_), 4 at a time: 1,782 tests: 1,759 passed, 21 skipped, 1 failed, 1 not run. Every test has the same outcome as on main (1,781 tests); the extra one is the new test, which passes. 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).

  • Hardware oracle, the recompiler (Vulkan runner) against the hardware (HIP), over 1,456 rows:

    • inputs: 14 source triples (signaling and quiet NaNs of both signs with payloads, infinities, zeros, ones, and inf * 0 cases);
    • low f16 lanes in all 7 f16/f32 source mixes, with no modifiers, -v4, -v5, |v4|, |v6| and -|v6|, at IEEE 0 and 1;
    • high f16 lanes (op_sel) in 4 of the mixes, with no modifiers, -v4, -v5 and |v4|, -v5, -|v6|, at IEEE 0 and 1;
    • f16 denormals flushed at IEEE=1, in both lanes.
    • On main, 494 rows differ; with this change, 0 differ.
    • Every hardware result matches the rule above. So do the 640 results in the test, 2,096 in all.
  • Tests: ctest -R '^agc_driver_(fma_mix_f32_nan_sources|f16_fma_invalid_nan|f32_arithmetic_nan|fma_mix_f16_denormal_modes|f32_denormal_flush|fma_rounding|mix_precision|f32_fma_denormal_mode1|f32_fma_denormal_mode2|vop3_mad_clamp|fma_legacy)$' passes 11/11.

  • Fast mode: with APS5_FAST_F32=1, the new test, agc_driver_f16_fma_invalid_nan and agc_driver_f32_arithmetic_nan pass 3/3.

  • Validation layer: the new test, f16_fma_invalid_nan, f32_arithmetic_nan, fma_mix_f16_denormal_modes and mix_precision pass under VK_LAYER_KHRONOS_validation 1.4.367 with ANYPS5_REQUIRE_VULKAN=1, with 0 VUID- lines and no missing-layer warnings.

  • Negative control: with only the new test applied to main, it fails on the first IEEE=1 row: 0x7f802000, expected 0x7fc02000. The modes before it pass on main, as the -a * -b note above explains.

  • Conventions, at 6ef285d9e2: check_conventions.py reports one warning, debt-length on shader.md:167. That entry was already 3,118 characters; this change makes it 3,286.

  • Overlap, rechecked at 6ef285d9e2:

    • Code: pr_overlap.py --branch reports #19710 (f32 directed rounding), which adds its rounding block just above the NaN choice in floatTernary. It is different work. A resolution keeps #19710's block and this change's NaN choice, and drops #19710's own f32Sources && nanResultF32 line, which this change covers.
    • shader.md: .gitattributes merges the debt files with the union driver, which GitHub ignores, so the tool and local merges miss conflicts there. The earlier note here missed these for that reason. A plain three-way merge of shader.md against the 98 open PRs that touch it adds conflicts with four:
      • my #18536 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.
    • Code merge previews are clean with #19563, #19605, #4287 (nanResultF32's selects), #15719, #18527 and my own #18469.
  • Linux and NVIDIA were not tested.

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), Adrenalin 26.10.1 (driver 32.0.21045.16010), Windows 10 22H2
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5299727452 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19637). Originally posted by [angrysandhill](https://github.com/angrysandhill) on 2026-11-13T17:11:42Z. --- ### What `v_fma_mix_f32` with an f16 source (any `op_sel_hi` bit set) left its NaN results to the driver; `shader.md` lists this as debt. #7074 (e98cf7f9a0) already gave its invalid operations the default NaN. On RDNA2 (gfx1031, RX 6750 XT), the hardware applies the same rule as the f32 forms do since #2083: - the first NaN source in operand order wins, after its modifiers; - it is quieted only with IEEE=1; - src2 loses to an invalid product (inf * 0), which gives `0xffc00000`. An f16 NaN source is widened on its bits before its `abs` and `neg`: its sign, `0x7f800000`, and its mantissa shifted left by 13. `0x7c01` becomes `0x7f802000`. Main differs from the hardware in three ways: - **f16 sNaN at IEEE=1:** the source comes back unquieted. Three f16 sources `0x7c01, 0x3c00, 0x3c00` give `0x7f802000`; the hardware gives `0x7fc02000`. - **f32 sNaN in a mixed form:** once any source is f16, the instruction leaves the f32 path, so an f32 sNaN isn't quieted either. `op_sel_hi:[1,1,0]` with src2 `0x7f800001` gives `0x7f800001`; the hardware gives `0x7fc00001`. - **Negated NaN factors (`-a * -b`):** in the oracle's kernels, which hold that one instruction, the result lost the sign that the negation gives the NaN. `-v4, -v5` with f16 `0x7c01` in src0 gave `0x7f802000` at IEEE=0; the hardware gives `0xff802000`. - A single negation kept the sign, so presumably the driver cancels the pair. - In the new test's shader, which runs eight forms together, main kept the sign on the AMD Windows driver. tablefrogpillow's RADV runs (Vega, RDNA2 and RDNA3) do fail on it: `0x7f802000`, expected `0xff802000`. - The fix builds these bits with integer operations, so it doesn't depend on what the driver does here. `floatTernary` now passes every `v_fma_mix_f32` source to `nanResultF32`. A new `mixNanSourceF32`, next to `readMixF32`, gives an f16 source as its widened NaN bits when the f16 lane is a NaN. It reads the lane `readF16LaneAsF32` reads, including the high lane with `op_sel`. The invalid-product test still uses the flushed values the arithmetic uses. f32-only forms build the same IR as before. With `APS5_FAST_F32` set, `nanResultF32` returns early, as it does for the f32 forms. Since `6ef285d9e2` (`ea27c39e75` before the rebase), the select #7074 added for invalid operations runs only in that mode, where it is still the only thing giving them the default NaN. With exact arithmetic, `nanResultF32` gives the same result. `tests/execution/FmaMixF32NanSources.cpp` (`agc_driver_fma_mix_f32_nan_sources`) pins 640 results, all measured on the RX 6750 XT: - 16 rows and 8 forms; - forms: three f16 sources, `-v4, -v5`, `|v4|` with `-|v6|`, the high lanes through `op_sel`, and four mixes of f16 and f32 sources with `neg` and `abs`; - modes: `FLOAT_MODE` 0xc0 and 0xf0 at IEEE 0 and 1, and 0x00 at IEEE 1; - a shader without a known float mode is checked against the 0xc0, IEEE=0 results, as `F32ArithmeticNan` checks it against its IEEE=0 table; - it runs with and without fused exact arithmetic, and skips NaN-to-NaN comparisons under `APS5_FAST_F32` as `F32ArithmeticNan` does. ### Tested Windows 10 22H2, AMD Radeon RX 6750 XT (gfx1031), Adrenalin 26.10.1 (driver 32.0.21045.16010), WinLibs GCC 15.2, Release. Main `6e5972c033`, changes `ee609fe080` and `ea27c39e75`; the results below are from those unless noted. **Rebased on 2026-11-14** onto main `0f4538fa02` as `951da1b17a` and `6ef285d9e2`, 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, so the runs posted on the earlier heads still apply. Rerun at `6ef285d9e2`: - **Negative control:** fails on the same IEEE=1 row, `0x7f802000`, expected `0x7fc02000`. - **Tests:** the 11 below pass 11/11, the 5 under the validation layer with 0 `VUID-` lines, and fast mode 3/3. - **Oracle:** 0 of the 1,456 rows differ from the hardware. - **Full `^(agc_|shader|vector_)`, 4 at a time:** 1,782 tests: 1,759 passed, 21 skipped, 1 failed, 1 not run. Every test has the same outcome as on `main` (1,781 tests); the extra one is the new test, which passes. `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`). - **Hardware oracle**, the recompiler (Vulkan runner) against the hardware (HIP), over 1,456 rows: - inputs: 14 source triples (signaling and quiet NaNs of both signs with payloads, infinities, zeros, ones, and inf * 0 cases); - low f16 lanes in all 7 f16/f32 source mixes, with no modifiers, `-v4, -v5`, `|v4|, |v6|` and `-|v6|`, at IEEE 0 and 1; - high f16 lanes (`op_sel`) in 4 of the mixes, with no modifiers, `-v4, -v5` and `|v4|, -v5, -|v6|`, at IEEE 0 and 1; - f16 denormals flushed at IEEE=1, in both lanes. - On main, 494 rows differ; with this change, 0 differ. - Every hardware result matches the rule above. So do the 640 results in the test, 2,096 in all. - **Tests:** `ctest -R '^agc_driver_(fma_mix_f32_nan_sources|f16_fma_invalid_nan|f32_arithmetic_nan|fma_mix_f16_denormal_modes|f32_denormal_flush|fma_rounding|mix_precision|f32_fma_denormal_mode1|f32_fma_denormal_mode2|vop3_mad_clamp|fma_legacy)$'` passes 11/11. - **Fast mode:** with `APS5_FAST_F32=1`, the new test, `agc_driver_f16_fma_invalid_nan` and `agc_driver_f32_arithmetic_nan` pass 3/3. - **Validation layer:** the new test, `f16_fma_invalid_nan`, `f32_arithmetic_nan`, `fma_mix_f16_denormal_modes` and `mix_precision` pass under `VK_LAYER_KHRONOS_validation` 1.4.367 with `ANYPS5_REQUIRE_VULKAN=1`, with 0 `VUID-` lines and no missing-layer warnings. - **Negative control:** with only the new test applied to main, it fails on the first IEEE=1 row: `0x7f802000`, expected `0x7fc02000`. The modes before it pass on main, as the `-a * -b` note above explains. - **Conventions**, at `6ef285d9e2`: `check_conventions.py` reports one warning, `debt-length` on `shader.md:167`. That entry was already 3,118 characters; this change makes it 3,286. - **Overlap**, rechecked at `6ef285d9e2`: - **Code:** `pr_overlap.py --branch` reports #19710 (f32 directed rounding), which adds its rounding block just above the NaN choice in `floatTernary`. It is different work. A resolution keeps #19710's block and this change's NaN choice, and drops #19710's own `f32Sources && nanResultF32` line, which this change covers. - **shader.md:** `.gitattributes` merges the debt files with the union driver, which GitHub ignores, so the tool and local merges miss conflicts there. The earlier note here missed these for that reason. A plain three-way merge of `shader.md` against the 98 open PRs that touch it adds conflicts with four: - my #18536 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. - **Code merge previews** are clean with #19563, #19605, #4287 (`nanResultF32`'s selects), #15719, #18527 and my own #18469. - Linux and NVIDIA were not tested. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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), Adrenalin 26.10.1 (driver 32.0.21045.16010), Windows 10 22H2 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-21 03:58:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T03:58:27Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the hardware claims in the description are author-reported, and the code path inspected consistently applies source selection, modifiers, and IEEE quieting.

What I ran

Reviewed head a64fe0bfa30d850ba5fecc037edd59f9bc55d7ba. Read the prepared description, commits, manifest, diff, and all five changed files; also read targeted context in OperandAccess.cpp and DispatchInstructions.cpp. No commands or tests were run.

Results

Check Result Note
Diff and changed files passed All five changed files were included and read
Tests not run Hardware and CI results are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

The reported RX 6750 XT, Vulkan validation, fast-mode, and negative-control runs were not independently reproduced. Linux, NVIDIA, and PS5 hardware behavior were not verified. files.json was not available; the file listing reported every changed file as included, and all were read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002684412 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19637#pullrequestreview-6002684412). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T03:58:27Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the hardware claims in the description are author-reported, and the code path inspected consistently applies source selection, modifiers, and IEEE quieting. ## What I ran Reviewed head `a64fe0bfa30d850ba5fecc037edd59f9bc55d7ba`. Read the prepared description, commits, manifest, diff, and all five changed files; also read targeted context in `OperandAccess.cpp` and `DispatchInstructions.cpp`. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and changed files | passed | All five changed files were included and read | | Tests | not run | Hardware and CI results are author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The reported RX 6750 XT, Vulkan validation, fast-mode, and negative-control runs were not independently reproduced. Linux, NVIDIA, and PS5 hardware behavior were not verified. `files.json` was not available; the file listing reported every changed file as included, and all were read. ## To get this to a good state Nothing is required.
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!19637
No description provided.