fix(shader): convert f16 bits to f32 without UnpackHalf2x16 #1463

Merged
drk1rd merged 1 commit from upstream-pr/1463 into main 2026-10-11 05:27:10 +00:00
drk1rd commented 2026-10-08 08:06:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by drk1rd on 2026-10-08T08:06:52Z.


What

On the AMD Windows driver, UnpackHalf2x16 of a byte-selected half converts the unmasked source register. agc_driver_sdwa_float_selectors and agc_driver_sdwa_ldexp_f16 fail on an AMD RDNA2 iGPU (both pass on NVIDIA, and CI has no AMD device):

sdwa ldexp f16: lane 2 BYTE_0/DWORD -> DWORD is 0x0000fff0, expected 0x00000000

Probing the ldexp shader's SPIR-V on both GPUs (lane 2, input 0xfff0fff0):

value AMD NVIDIA
BitFieldUExtract(x, 0, 8) & 0xffff 0x000000f0 0x000000f0
UnpackHalf2x16(that).x 0xfffe0000 (f16 0xfff0) 0x37700000

In all 11 failing lanes the result is the unselected low half quieted. Replacing the extract with shift+mask, dropping the redundant & 0xffff, or feeding the half through the high word does not change the AMD result.

EmitF16BitsToF32 now decodes sign, exponent and mantissa with integer operations and scales subnormals by 2^-24 (exact in f32). Checked bit-exact against IEEE f16→f32 for all 65536 halves, including ±0, subnormals, infinities and NaN payloads.

Tested

Windows 11 (10.0.26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, -DBUILD_TESTING=ON, full ctest with ANYPS5_GPU set to each device:

  • AMD Radeon(TM) Graphics (Ryzen 7 7700X iGPU, device 0x164e, driver 32.0.21030.2001): 420/420 (418/420 before)
  • NVIDIA GeForce RTX 3090 (driver 32.0.15.9186): 420/420

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4783806242 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1463). Originally posted by [drk1rd](https://github.com/drk1rd) on 2026-10-08T08:06:52Z. --- ### What On the AMD Windows driver, `UnpackHalf2x16` of a byte-selected half converts the unmasked source register. `agc_driver_sdwa_float_selectors` and `agc_driver_sdwa_ldexp_f16` fail on an AMD RDNA2 iGPU (both pass on NVIDIA, and CI has no AMD device): sdwa ldexp f16: lane 2 BYTE_0/DWORD -> DWORD is 0x0000fff0, expected 0x00000000 Probing the ldexp shader's SPIR-V on both GPUs (lane 2, input 0xfff0fff0): | value | AMD | NVIDIA | |---|---|---| | `BitFieldUExtract(x, 0, 8) & 0xffff` | 0x000000f0 | 0x000000f0 | | `UnpackHalf2x16(that).x` | 0xfffe0000 (f16 0xfff0) | 0x37700000 | In all 11 failing lanes the result is the unselected low half quieted. Replacing the extract with shift+mask, dropping the redundant `& 0xffff`, or feeding the half through the high word does not change the AMD result. `EmitF16BitsToF32` now decodes sign, exponent and mantissa with integer operations and scales subnormals by 2^-24 (exact in f32). Checked bit-exact against IEEE f16→f32 for all 65536 halves, including ±0, subnormals, infinities and NaN payloads. ### Tested Windows 11 (10.0.26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `-DBUILD_TESTING=ON`, full `ctest` with `ANYPS5_GPU` set to each device: - AMD Radeon(TM) Graphics (Ryzen 7 7700X iGPU, device 0x164e, driver 32.0.21030.2001): 420/420 (418/420 before) - NVIDIA GeForce RTX 3090 (driver 32.0.15.9186): 420/420 ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
CouchRiv commented 2026-10-09 06:04:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-09T06:04:14Z.


NaN rows for v_cvt_f32_f16 on RDNA2 (gfx1036, Radeon 610M) with tools/hw-oracle, DX10_CLAMP=1, f16 denormals kept, round to nearest, from a sweep over 68 f16 inputs:

f16 input IEEE=0, f32 denormals flushed IEEE=1, f32 denormals kept
0x7c01 0x7f802000 0x7fc02000
0x7e55 0x7fcaa000 0x7fcaa000

The hardware keeps the sign and the payload (mantissa << 13), and sets the quiet bit 0x400000 only with IEEE=1. Reading the diff, the new decode gives 0x7f800000 | mantissa << 13 for an f16 NaN, so it matches the hardware at IEEE=0, the mode titles run with. On main the NVIDIA driver returns 0x7fffffff for every NaN here, and RADV doesn't quiet with IEEE=1. I haven't run this PR's code; the rows are hardware only.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6075314714 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1463#issuecomment-6075314714). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-09T06:04:14Z. --- NaN rows for `v_cvt_f32_f16` on RDNA2 (gfx1036, Radeon 610M) with `tools/hw-oracle`, DX10_CLAMP=1, f16 denormals kept, round to nearest, from a sweep over 68 f16 inputs: | f16 input | IEEE=0, f32 denormals flushed | IEEE=1, f32 denormals kept | |---|---|---| | `0x7c01` | `0x7f802000` | `0x7fc02000` | | `0x7e55` | `0x7fcaa000` | `0x7fcaa000` | The hardware keeps the sign and the payload (`mantissa << 13`), and sets the quiet bit `0x400000` only with IEEE=1. Reading the diff, the new decode gives `0x7f800000 | mantissa << 13` for an f16 NaN, so it matches the hardware at IEEE=0, the mode titles run with. On `main` the NVIDIA driver returns `0x7fffffff` for every NaN here, and RADV doesn't quiet with IEEE=1. I haven't run this PR's code; the rows are hardware only.
drk1rd commented 2026-10-09 06:08:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by drk1rd on 2026-10-09T06:08:32Z.


Thanks for the hardware rows. That matches what the decode does now: sign and mantissa << 13 kept, no quiet bit, as at IEEE=0. Setting the quiet bit for IEEE=1 shaders needs the per-shader float mode, which the recompiler doesn't track yet (#981), so I've left that out of this PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6075377955 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1463#issuecomment-6075377955). Originally posted by [drk1rd](https://github.com/drk1rd) on 2026-10-09T06:08:32Z. --- Thanks for the hardware rows. That matches what the decode does now: sign and `mantissa << 13` kept, no quiet bit, as at IEEE=0. Setting the quiet bit for IEEE=1 shaders needs the per-shader float mode, which the recompiler doesn't track yet (#981), so I've left that out of this PR.
MarcZX10 commented 2026-10-09 14:45:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T14:45:14Z.

Review: APPROVED


Reviewed the complete diff and the full Linux and Windows Build checks. The integer sign/exponent/mantissa path avoids the AMD UnpackHalf2x16 behavior while preserving normal, subnormal, zero, infinity, and NaN payload representations; the reported device runs cover the affected tests and the full suite. AI-assisted review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5471597442 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1463#pullrequestreview-5471597442). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T14:45:14Z. **Review:** APPROVED --- Reviewed the complete diff and the full Linux and Windows Build checks. The integer sign/exponent/mantissa path avoids the AMD `UnpackHalf2x16` behavior while preserving normal, subnormal, zero, infinity, and NaN payload representations; the reported device runs cover the affected tests and the full suite. AI-assisted review.
fma-rc commented 2026-10-10 12:28:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-10T12:28:48Z.


Full-range check on RDNA2 (gfx1036, Radeon 610M) with tools/hw-oracle: v_cvt_f32_f16 for all 65,536 halves, wave32 and wave64, IEEE=0, DX10_CLAMP=1, f16 denormals kept, f32 denormals flushed, round to nearest.

I then ran the same 65,536 halves through the recompiler (low half and shifted high half, guest wave64 and wave32, 262,144 results per device) and compared with the hardware rows, on Linux with this PR merged onto main 197ffac9:

NVIDIA RTX 5080 (615.78.08) RADV 26.1.4 on the 610M
with this PR 0 differ 0 differ
without 8,184 differ, all NaN inputs (e.g. half 0xffff gives 0x7fffffff, hardware 0xffffe000) 0 differ

So the new decode equals the hardware for every half at IEEE=0. The float16, sdwa_float_selectors, sdwa_ldexp_f16 and half_expansion tests pass on both devices with the PR. On RADV they also pass without it, so the unmasked-source behaviour did not reproduce with the Linux driver on the same iGPU model.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097497829 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1463#issuecomment-6097497829). Originally posted by [fma-rc](https://github.com/fma-rc) on 2026-10-10T12:28:48Z. --- Full-range check on RDNA2 (gfx1036, Radeon 610M) with `tools/hw-oracle`: `v_cvt_f32_f16` for all 65,536 halves, wave32 and wave64, IEEE=0, DX10_CLAMP=1, f16 denormals kept, f32 denormals flushed, round to nearest. I then ran the same 65,536 halves through the recompiler (low half and shifted high half, guest wave64 and wave32, 262,144 results per device) and compared with the hardware rows, on Linux with this PR merged onto `main` 197ffac9: | | NVIDIA RTX 5080 (615.78.08) | RADV 26.1.4 on the 610M | |---|---|---| | with this PR | 0 differ | 0 differ | | without | 8,184 differ, all NaN inputs (e.g. half `0xffff` gives `0x7fffffff`, hardware `0xffffe000`) | 0 differ | So the new decode equals the hardware for every half at IEEE=0. The float16, `sdwa_float_selectors`, `sdwa_ldexp_f16` and `half_expansion` tests pass on both devices with the PR. On RADV they also pass without it, so the unmasked-source behaviour did not reproduce with the Linux driver on the same iGPU model.
drk1rd commented 2026-10-10 18:02:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by drk1rd on 2026-10-10T18:02:12Z.


Thanks for the full sweep. The unmasked-source result only reproduced with AMD's Windows driver (32.0.21030.2001) here, so matching RADV fits.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6100550100 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1463#issuecomment-6100550100). Originally posted by [drk1rd](https://github.com/drk1rd) on 2026-10-10T18:02:12Z. --- Thanks for the full sweep. The unmasked-source result only reproduced with AMD's Windows driver (32.0.21030.2001) here, so matching RADV fits.
poonasor commented 2026-10-11 00:24:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by poonasor on 2026-10-11T00:24:46Z.


Data point from a discrete RDNA 2 card on a newer driver: this fixes both tests on an AMD Radeon RX 6700 XT (Navi 22, 0x73df), Adrenalin 25.10.45.05 (driver 32.0.21045.5002), Windows 11 Pro 23H2, WinLibs GCC 15.2.0 r7, Release.

Cherry-picked 58f596ee onto main 379d8b4e + #3173:

Test Before With this PR
agc_driver_sdwa_float_selectors lane 2 add_f16 BYTE_1/WORD_1 -> BYTE_0 is 0x000000ba, expected 0x00000000 passes 3/3
agc_driver_sdwa_ldexp_f16 lane 2 BYTE_0/DWORD -> DWORD is 0x0000fff0, expected 0x00000000 passes 3/3

Full ctest: 726 of 732 passed (724 before), 5 skipped (video_out), 1 failed (agc_driver_pixel_interlock, #2009, unrelated). No new failures. These are the two wrong results reported in #2944.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103729804 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1463#issuecomment-6103729804). Originally posted by [poonasor](https://github.com/poonasor) on 2026-10-11T00:24:46Z. --- Data point from a discrete RDNA 2 card on a newer driver: this fixes both tests on an AMD Radeon RX 6700 XT (Navi 22, 0x73df), Adrenalin 25.10.45.05 (driver 32.0.21045.5002), Windows 11 Pro 23H2, WinLibs GCC 15.2.0 r7, Release. Cherry-picked `58f596ee` onto `main` `379d8b4e` + #3173: | Test | Before | With this PR | |---|---|---| | `agc_driver_sdwa_float_selectors` | `lane 2 add_f16 BYTE_1/WORD_1 -> BYTE_0 is 0x000000ba, expected 0x00000000` | passes 3/3 | | `agc_driver_sdwa_ldexp_f16` | `lane 2 BYTE_0/DWORD -> DWORD is 0x0000fff0, expected 0x00000000` | passes 3/3 | Full `ctest`: 726 of 732 passed (724 before), 5 skipped (`video_out`), 1 failed (`agc_driver_pixel_interlock`, #2009, unrelated). No new failures. These are the two wrong results reported in #2944. AI-assisted (Claude Code).
Silverash-i8 commented 2026-10-11 02:10:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-11T02:10:55Z.


Another data point for this one, on Windows: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto main d31b57a4, RTX 5060 Laptop and the Arrow Lake iGPU (ANYPS5_GPU=Intel), with #1505's test added.

  • agc_driver_half_expansion (#1505) passes on both GPUs with this PR; on the RTX 5060 it fails without it.
  • On the Arrow Lake iGPU, agc_driver_vopc_compare fails on plain main and passes with this PR, the Intel case from #2822. The other f16 tests (f16_min_max_nan, div_fixup_f16, ldexp_f16_inline_constants, sdwa_cvt_f16_u16, sdwa_ldexp_f16, vopc_compare_f64, vopc_compare_table) pass on both GPUs with it.

AI-assisted (Claude Code); I ran this on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104497593 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1463#issuecomment-6104497593). Originally posted by [Silverash-i8](https://github.com/Silverash-i8) on 2026-10-11T02:10:55Z. --- Another data point for this one, on Windows: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto `main` d31b57a4, RTX 5060 Laptop and the Arrow Lake iGPU (`ANYPS5_GPU=Intel`), with #1505's test added. - `agc_driver_half_expansion` (#1505) passes on both GPUs with this PR; on the RTX 5060 it fails without it. - On the Arrow Lake iGPU, `agc_driver_vopc_compare` fails on plain `main` and passes with this PR, the Intel case from #2822. The other f16 tests (`f16_min_max_nan`, `div_fixup_f16`, `ldexp_f16_inline_constants`, `sdwa_cvt_f16_u16`, `sdwa_ldexp_f16`, `vopc_compare_f64`, `vopc_compare_table`) pass on both GPUs with it. AI-assisted (Claude Code); I ran this on my machine.
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!1463
No description provided.