fix(shader): convert f16 bits to f32 without UnpackHalf2x16 #1463
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1463"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by drk1rd on 2026-10-08T08:06:52Z.
What
On the AMD Windows driver,
UnpackHalf2x16of a byte-selected half converts the unmasked source register.agc_driver_sdwa_float_selectorsandagc_driver_sdwa_ldexp_f16fail on an AMD RDNA2 iGPU (both pass on NVIDIA, and CI has no AMD device):Probing the ldexp shader's SPIR-V on both GPUs (lane 2, input 0xfff0fff0):
BitFieldUExtract(x, 0, 8) & 0xffffUnpackHalf2x16(that).xIn 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.EmitF16BitsToF32now 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, fullctestwithANYPS5_GPUset to each device: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)Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-09T06:04:14Z.
NaN rows for
v_cvt_f32_f16on RDNA2 (gfx1036, Radeon 610M) withtools/hw-oracle, DX10_CLAMP=1, f16 denormals kept, round to nearest, from a sweep over 68 f16 inputs:0x7c010x7f8020000x7fc020000x7e550x7fcaa0000x7fcaa000The hardware keeps the sign and the payload (
mantissa << 13), and sets the quiet bit0x400000only with IEEE=1. Reading the diff, the new decode gives0x7f800000 | mantissa << 13for an f16 NaN, so it matches the hardware at IEEE=0, the mode titles run with. Onmainthe NVIDIA driver returns0x7ffffffffor every NaN here, and RADV doesn't quiet with IEEE=1. I haven't run this PR's code; the rows are hardware only.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 << 13kept, 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.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
UnpackHalf2x16behavior 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.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_f16for 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
main197ffac9:0xffffgives0x7fffffff, hardware0xffffe000)So the new decode equals the hardware for every half at IEEE=0. The float16,
sdwa_float_selectors,sdwa_ldexp_f16andhalf_expansiontests 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.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.
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
58f596eeontomain379d8b4e+ #3173:agc_driver_sdwa_float_selectorslane 2 add_f16 BYTE_1/WORD_1 -> BYTE_0 is 0x000000ba, expected 0x00000000agc_driver_sdwa_ldexp_f16lane 2 BYTE_0/DWORD -> DWORD is 0x0000fff0, expected 0x00000000Full
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).
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
maind31b57a4, 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.agc_driver_vopc_comparefails on plainmainand 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.