Skip to content

fix(shader): give f16 fma invalid operations the default NaN - #7074

Merged
adriadam10 merged 4 commits into
boykopovar:mainfrom
CrimsonMac:fix/f16-fma-default-nan
Oct 19, 2026
Merged

adriadam10 merged 4 commits into
boykopovar:mainfrom
CrimsonMac:fix/f16-fma-default-nan

Conversation

@CrimsonMac

@CrimsonMac CrimsonMac commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

What

On NVIDIA, an invalid operation in v_fma_f16 and its family, or in v_fma_mix_f32 with f16 sources, returns NVIDIA's own NaN (0x7fff / 0x7fffffff), not the default NaN. AMD's driver returns the default NaN (0xfe00 / 0xffc00000), and so does lavapipe on x86. agc_driver_f16_fma_denormal_modes (#7034) and agc_driver_fma_mix_f16_denormal_modes (#7040) originally failed on NVIDIA because of it:

AGC graphics: v_fma_f16 without a known float mode of 0x7c00, 0x0000, 0x0000 is 0x7fff, expected 0xfe00
AGC graphics: v_fma_mix_f32 of three f16 sources f32 denormals flushed, f16 denormal mode 0, IEEE mode off of 0x00007c00, 0x00000001, 0x3f800000 is 0x7fffffff, expected 0xffc00000

#7093 has since made both tests accept any NaN, so this PR adds its own test, agc_driver_f16_fma_invalid_nan, which requires the exact bits.

v_fma_f16, v_fmac_f16, v_fmamk_f16, v_fmaak_f16 and v_fma_mix_f32 with f16 sources left all of their NaN bits to the driver (docs/dev/debt/shader.md, "Signaling NaN inputs"). The f32-source forms already pick their NaN on the bits through nanResultF32, where an invalid product gives 0xffc00000. The f16-source forms can't reuse it, because NVIDIA's f16 to f32 conversion loses the payload of an f16 NaN source. An invalid operation has no payload to keep, so this PR handles only that case:

  • A NaN from sources that are not NaN (inf * 0, or inf - inf in the add) becomes the default NaN.
    • v_fma_f16 family: 0xfe00, selected on the f16 bits after the conversion and clamp. A DX10 clamp still turns it into 0.
    • v_fma_mix_f32 with an f16 source: 0xffc00000.
  • An inf * 0 product with a NaN src2 also gives the default NaN, as in the f32 forms: 0xfe00 in the f16 fma forms and 0xffc00000 in v_fma_mix_f32. @thuanquang measured this natively on gfx1031 through HIP in all four modes (comment). The RDNA 2 iGPU's Vulkan driver instead returns src2 quieted in the f16 fma forms, so the test requires the default NaN there too.
  • Any other NaN source is still left to the driver.

invalidOperationF32 holds the condition. writeF16 takes it as an optional argument, and only the f16 FMA family passes it.

Follow-up, not in this PR: scalar f16 add, sub and mul have the same gap for inf - inf and inf * 0. Their tests accept any NaN, and their hardware bits are not measured here.

Tested

Windows 11 (26100), Ryzen 5 7600, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, this branch at 3b9c285ea (based on 673f7406c; the earlier runs below were on main 47a266cf9 merged with be204648d).

agc_driver_f16_fma_invalid_nan covers v_fma_f16, v_fmac_f16 and both v_fma_mix_f32 forms. It runs 10 rows each, in 5 float modes (no known mode, denormals flushed or kept, IEEE off and on):

NVIDIA RTX 4070 SUPER (32.0.16.1742) AMD Radeon iGPU (RDNA 2, 0x164e, 32.0.21042.62)
new test, core/shader/recompiler reverted to main fails on every row (0x7fff, 0x7fffffff) fails on the first inf * 0 + NaN src2 row (0x7e00)
new test with 5bb403d84's src2 carve-out fails (0x7fff) fails (0x7e00)
new test and the existing f16 fma, fma_mix and *_nan tests, this PR, --repeat until-fail:5, ANYPS5_NO_SHADER_CACHE=1 5/5 5/5
ctest -R '^agc_' -j8 (645 tests), 3b9c285ea all pass except one agc_driver_multisample_fill_host_only failure under -j8 (passes 10/10 alone) fails only #6666 and #5804
full ctest -j 8 (1399 tests), be204648d fails only guest_sce_net (host DNS timeout, also on main) and dynamic_segment/windows_icon (antivirus in TEMP; both pass with TEMP on an excluded folder) the same three, plus agc_driver_lds_mixed_width_atomics (#6666, fixed by #6976) and agc_driver_layered_volume_vertex_no_gpl (#5804), both failing on main

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: NVIDIA GeForce RTX 4070 SUPER, driver 32.0.16.1742, Windows 11; AMD Radeon(TM) Graphics (RDNA 2 iGPU, 0x164e), driver 32.0.21042.62, Windows 11
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4072 core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp
core/shader/recompiler/Translation/src/FloatInstructions.cpp
PR 5021 core/shader/recompiler/Translation/src/FloatInstructions.cpp
docs/dev/debt/shader.md
PR 5371 docs/dev/debt/shader.md
PR 5426 docs/dev/debt/shader.md
PR 5500 docs/dev/debt/shader.md
PR 5941 docs/dev/debt/shader.md
PR 6009 docs/dev/debt/shader.md
PR 6235 docs/dev/debt/shader.md
PR 6280 docs/dev/debt/shader.md
PR 6604 docs/dev/debt/shader.md
PR 6977 docs/dev/debt/shader.md
PR 7085 docs/dev/debt/shader.md
PR 7223 docs/dev/debt/shader.md
PR 7332 core/shader/recompiler/Translation/src/FloatInstructions.cpp
docs/dev/debt/shader.md
PR 7392 docs/dev/debt/shader.md
PR 7395 docs/dev/debt/shader.md
PR 7484 docs/dev/debt/shader.md

@github-actions github-actions Bot added shader core/shader docs labels Oct 17, 2026
@marco-scheffler

Copy link
Copy Markdown

Linux NVIDIA data point: Ubuntu 24.04.4, RTX 4000 SFF Ada Generation, driver 595.99.02, GCC 13.3.0, Release. This PR at b8190ee12 merged onto main 47a266cf9, which already has #7093's NaN-tolerant checks. To see the bits this PR produces, I also ran both tests with their strict versions from before #7093 (32f28a462^1). ANYPS5_NO_SHADER_CACHE=1, 5 runs each.

Build strict tests, RTX strict tests, lavapipe main's tests, RTX
main 0/5: 0x7fff, expected 0xfe00; 0x7fffffff, expected 0xffc00000 5/5 5/5
this PR 5/5 5/5 5/5

With this PR ctest -R '^agc_' passes 662/662 on the RTX.

@CrimsonMac

Copy link
Copy Markdown
Contributor Author

Thanks for the RTX 4000 Ada / Linux run, and for checking against the strict tests from before #7093. Since #7093 made those checks accept any NaN, nothing in the suite would catch a regression here, so I've added agc_driver_f16_fma_invalid_nan (be204648d). It requires 0xfe00 / 0xffc00000 for invalid operations in v_fma_f16, v_fmac_f16 and both v_fma_mix_f32 forms, in 5 float modes. With core/shader/recompiler reverted to main it fails on every row on the RTX 4070 SUPER.

Writing it turned up one case the first commit got wrong. Without this PR, AMD's driver on the RDNA 2 iGPU returns src2 for inf * 0 plus a NaN src2 in the f16 fma forms (0x7e00, and 0x7d00 quieted to 0x7f00), while v_fma_mix_f32 gives 0xffc00000 for the same sources. The first commit forced 0xfe00 there. 5bb403d84 now leaves that case to the driver in the f16 fma forms and keeps it in v_fma_mix_f32; the debt entry records it. The body has the updated runs.

@github-actions github-actions Bot added the agc libSceAgc* label Oct 17, 2026
@marco-scheffler

Copy link
Copy Markdown

Re-ran be204648d on main cb1d6e5bd (RTX 4000 SFF Ada, 595.99.02, Linux; ANYPS5_NO_SHADER_CACHE=1): agc_driver_f16_fma_invalid_nan and both denormal-mode tests pass 5/5 on the RTX and on lavapipe, and ctest -R '^agc_' passes 709/709 on the RTX. With core/shader/recompiler taken from main, the new test fails 3/3 on both: v_fma_f16 … is 0x7fff, expected 0xfe00 on the RTX and v_fma_mix_f32 of three f16 sources with denormals kept, IEEE mode off of 0x7e007c00, 0x00008000, 0x80000000 is 0x7fc00000, expected 0xffc00000 on lavapipe, so CI's lavapipe job would catch a regression too.

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apple GPU data point for agc_driver_f16_fma_invalid_nan: Apple's driver gives a third NaN encoding here, so the test is red on Apple without the fix, not only on NVIDIA.

Machine: Apple M2 Max, macOS 27.2, x86-64 build under Rosetta. Head be20464. Method: macos-port @ b7eb6b6 + ImportWatched + PR diff, not main. One context conflict in FloatInstructions.cpp: the port predates the f16 input/tiny-result flush around fmaF16RoundedToOdd, so I kept the port's line and added this PR's invalidOperationF32(...) argument to its writeF16 call. The other four files applied cleanly.

Tree MoltenVK 1.4.2 (SDK) MoltenVK main + SPIRV-Cross #2704
new test only, recompiler unchanged Failed: v_fma_f16 without a known float mode of 0x00007c00, 0x00000000, 0x00000000 is 0x00007e00, expected 0x0000fe00 Failed, same message
full PR Passed Passed

So for inf * 0 in v_fma_f16, Apple returns the positive quiet NaN 0x7e00, while NVIDIA returns 0x7fff and AMD returns 0xfe00. The PR selects 0xfe00 on the bits, so the result no longer depends on the vendor. The test stops at the first row that fails, so this is the only raw Apple value I have.

Also with the PR: agc_driver_f16_min_max_nan passes on both drivers. agc_driver_float16_fma_rounding fails on the SDK driver and passes with #2704, the same as the port baseline (signed-zero issue, not this PR).

Full ^agc suite with the PR (SDK driver, -j1 --timeout 30, flip/display/lds_atomics64 excluded): 13 failures out of 434, the same 13 as the port baseline. No new failures.

GPUs tested: Apple M2 Max (MoltenVK 1.4.2 and MoltenVK main + SPIRV-Cross #2704)

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@thuanquang

Copy link
Copy Markdown
Contributor

Native hardware data for the case the debt line leaves open ("the native v_fma_f16 is not measured"). RX 6700 XT (gfx1031, RDNA 2), the instructions run directly through HIP with tools/hw-oracle/template.s (assembled by the AMD Windows driver's comgr, driver 32.0.21045.5002), in all four modes: IEEE 0 and 1, f16/f32 denormals kept and flushed.

  • The PR's own rows and encodings (the FmaCode and MixCode words of F16FmaInvalidNan.cpp with its 8 FmaRows and 8 MixRows): every result is what the test expects, 0x0000fe00 from v_fma_f16 and v_fmac_f16, 0xffc00000 from both v_fma_mix_f32 forms, in all four modes.

  • inf * 0 with a NaN src2, the case 5bb403d leaves to the driver: the hardware gives the default NaN here too, not src2. Products inf*0, 0*inf, -inf*-0, inf*-0, -inf*0 and -0*-inf, with src2 0x7e00, 0x7d00, 0xfe00, 0xfd00, 0x7c01, 0x7fff, 0x7e55 and 0x7d55:

    form rows result, every mode
    v_fma_f16, v_fmac_f16 48 each 0x0000fe00
    v_fmamk_f16 (K 0x0000 and 0x7c00) 52 0x0000fe00
    v_fmaak_f16 (K 0x7e00, 0x7d00, 0xfe00) 89 each 0x0000fe00
    v_fma_mix_f32, three f16 sources 48 0xffc00000
  • inf * 0 with a non-NaN src2 and inf - inf (inf*1 + -inf and similar) give 0x0000fe00 in every f16 form, and 0xffc00000 in v_fma_mix_f32.

  • NaN sources without an invalid product, including a NaN factor times zero: the first NaN in operand order, quieted only with IEEE=1, in 384 of 384 rows (v_fma_f16, v_fmac_f16 and v_fma_mix_f32, 32 rows each in each of the four modes).

So on this GPU the f16 fma forms follow the f32 rule in the debt entry: an invalid product gives the default NaN even when src2 is a NaN. The 0x7e00/0x7f00 from the RDNA 2 iGPU came through the Vulkan driver, so it reflects how that driver compiles f16 fma, not what v_fma_f16 does. By this data, b8190ee's form (the default NaN for an inf * 0 product whatever src2 is) matches the hardware. A row like inf * 0 + 0x7e00 in FmaRows would cover the case.

This is desktop RDNA 2, not the PS5's gfx1013. I have not run the PR's test through the Vulkan driver on this GPU.

@CrimsonMac

Copy link
Copy Markdown
Contributor Author

Thanks, that settles it. Since the iGPU's 0x7e00/0x7f00 comes from the Vulkan driver's compilation and not from v_fma_f16 itself, 5bb403d84 was wrong. 3b9c285ea goes back to b8190ee12's form: in every f16 fma form, an inf * 0 product gives 0xfe00 whatever src2 is. The debt entry now cites your gfx1031 measurement and notes the iGPU driver's Vulkan result separately.

FmaRows gets inf * 0 + 0x7e00 and -0 * -inf + 0x7d00, and MixRows gets -inf * -0 + 0x7e00 and inf * 0 + 0x7fff. With 5bb403d84's line put back, the test now fails on both of my GPUs: v_fma_f16 … of 0x00007c00, 0x00000000, 0x00007e00 is 0x00007fff on the RTX 4070 SUPER and … is 0x00007e00 on the RDNA 2 iGPU, both expecting 0x0000fe00. With the commit, the new test and the f16 fma, fma_mix, float16_fma and f16_min_max tests pass 5/5 on both. ctest -R '^agc_' (645 tests) passes on the RTX, apart from one -j8 failure of agc_driver_multisample_fill_host_only that passes 10/10 on its own. On the iGPU it fails only #5804 and #6666.

@thuanquang

Copy link
Copy Markdown
Contributor

The updated test on the AMD Windows driver whose 0x7e00 started this: RX 6700 XT (gfx1031), driver 32.0.21045.5002, Windows 11 26200, WinLibs GCC 15.2.0 r7, Release. main 7f596df48 with this PR's head 3b9c285ea merged (clean merge). Each run is a fresh renamed copy of the test executable, with ANYPS5_NO_SHADER_CACHE=1.

  • agc_driver_f16_fma_invalid_nan passes 3 of 3.
  • With FloatInstructions.cpp set back to 5bb403d's form (the test unchanged), it fails 3 of 3 on the new row: v_fma_f16 without a known float mode of 0x00007c00, 0x00000000, 0x00007e00 is 0x00007e00, expected 0x0000fe00. So on this driver the new rows catch the case, and the forced default NaN is what makes them pass.

v_fma_f16, v_fmac_f16, v_fmamk_f16, v_fmaak_f16 and v_fma_mix_f32 with
f16 sources left the NaN of an invalid operation to the driver, so
inf * 0 gave 0x7fff and 0x7fffffff on NVIDIA where the hardware gives
the default NaN 0xfe00 and 0xffc00000. An inf * 0 product, or a NaN
from sources that are not NaN, now gives the default NaN; NaN sources
are still left to the driver.
@adriadam10
adriadam10 force-pushed the fix/f16-fma-default-nan branch from 3b9c285 to bd03daa Compare October 19, 2026 13:04
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@adriadam10
adriadam10 merged commit 2232629 into boykopovar:main Oct 19, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the diff against the description, the new agc_driver_f16_fma_invalid_nan (requires exact bits 0xfe00 / 0xffc00000; fails without the change per the NVIDIA, Linux and AMD runs in the thread) and the debt entry (cites the gfx1031 measurement). Rebased onto current main (only docs/dev/debt/shader.md conflicted, next to the v_fma_mixlo_f16 line; diff otherwise unchanged); merged.

angrysandhill added a commit to angrysandhill/AnyPS5 that referenced this pull request Nov 13, 2026
…2 mode

With exact f32 arithmetic, nanResultF32 already gives an invalid
operation of v_fma_mix_f32 with f16 sources the default NaN, so the
select from boykopovar#7074 before it was redundant there. It now runs only with
APS5_FAST_F32 set, where nanResultF32 returns early and that select is
still the only thing giving inf * 0 the default NaN. In that mode the
widened mix NaN sources are no longer built either, since nothing reads
them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
angrysandhill added a commit to angrysandhill/AnyPS5 that referenced this pull request Nov 14, 2026
…2 mode

With exact f32 arithmetic, nanResultF32 already gives an invalid
operation of v_fma_mix_f32 with f16 sources the default NaN, so the
select from boykopovar#7074 before it was redundant there. It now runs only with
APS5_FAST_F32 set, where nanResultF32 returns early and that select is
still the only thing giving inf * 0 the default NaN. In that mode the
widened mix NaN sources are no longer built either, since nothing reads
them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants