feat: half precision (f16) in the HLSL recompiler (25 encodings) #219

Manually merged
github merged 5 commits from gh-pr/219/head into gh-pr/219/base 2026-11-26 05:38:08 +00:00
Owner

@⁠IxMxAMAR on GitHub · 2026-11-25 16:22 UTC · original
IxMxAMAR:feat/rdna2-f16-hlsl → main · +308 −7 · 6 files · 5 commits · merged 2026-11-25 18:37 UTC

Change

Replaces #165, which was closed during the 25 November triage. It is stacked on #218 (the f16 interpreter, which replaces #163): its first 5 commits are that PR's, and this one starts at "the HLSL recompiler translates f16 VOP1 and VOP2". I will rebase it on main as soon as #218 is merged.

The RDNA 2 → HLSL recompiler translates the 25 f16 encodings the interpreter runs with #218, in compute, vertex and pixel shaders:

  • v_add, v_sub, v_subrev, v_mul, v_fmac, v_fmamk, v_fmaak, v_max, v_min and v_ldexp_f16, in VOP2 and VOP3;
  • v_fma_f16, in VOP3;
  • v_cvt_f16_f32 and v_cvt_f32_f16, in VOP1 and VOP3.

They live in a new module, emu/rdna2_hlsl_f16.py, wired into rdna2_hlsl.py (before the SDWA/DPP path, as in the interpreter), rdna2_hlsl_graphics.py and, since the rebase, rdna2_hlsl_wave64.py.

There are no 16-bit types (shader model 6.0 is unchanged, with neither half nor f16tof32): an f16 value stays half of a VGPR.

  • Each operation goes through f32 in a precise function and comes back to f16 through an integer routine: round to nearest even, denormals, infinities, NaN 0x7E00.
  • The sum, difference and product of two f16 values stay exact through this double step (24 ≥ 2 × 11 + 2).
  • The FMA computes the exact error of the sum (TwoSum) and rounds to odd before the f16 rounding.
  • Min, max and clamp are done in integer.

The forms the interpreter refuses are refused here too, with the same message; the tests reuse the REFUSED_VOP3 list of tests/test_rdna2_f16.py.

New since the rebase: the wave64 path that main gained (emu/rdna2_hlsl_wave64.py) translates f16 through rdna2_hlsl.statements, but its prelude lacked the F16* functions. A wave64 program with f16 called F16Add or F16Fma without defining them. One commit adds the functions to that prelude, and the f16 code now takes the caller's active-lane condition (LANE_ON in wave64), like the rest of main. test_wave64_translation_carries_the_f16_helpers fails without that commit.

Linked issue: Refs #115

AMD driver: x + 0 folded despite precise

On the RX 9070 XT (RDNA 4), v_add_f16 v10, 0, v1 with v1 = -0 gave -0 instead of +0. The DXIL from dxc does contain fadd float %x, 0.0, with no fast-math flag: the AMD driver folds it into x. Intel and WARP give +0. The sign of an exactly zero result (sum or FMA) is therefore set in integer from the operands; without this, the GPU test fails on the Radeon.

Verification

python -m pytest → 954 passed, 289 skipped
python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed!
$env:ANYXBOXX_GPU="Radeon"; python -m pytest tests/test_rdna2_hlsl_f16.py tests/test_rdna2_f16.py → 51 passed (AMD Radeon RX 9070 XT, on the previous base `f5cdd8e`)
$env:ANYXBOXX_GPU="Intel";  python -m pytest tests/test_rdna2_hlsl_f16.py tests/test_rdna2_f16.py → 51 passed (Intel(R) Graphics)
$env:ANYXBOXX_GPU="warp";   python -m pytest tests/test_rdna2_hlsl_f16.py tests/test_rdna2_f16.py → 51 passed (Microsoft Basic Render Driver)
  • The skips are the tests that need the C++ runtime, the Rust crate or the TomBox DLLs, none of which were built in this checkout.
  • For the GPU runs, anyxboxx_gpu_run.exe came from an older build of runtime/ (set through ANYXBOXX_RUNTIME). With it, the whole suite gives 14 failures, all in runtime tests that need the newer runtime (HDR luminance, console identity, the --adaptateur fields); none is in the f16 files.
  • The Radeon run was on the previous base: it was busy with a game run after the last rebase. The Intel and WARP runs are on ab55a2a.
  • On the Intel iGPU, main's new test_multi_lane_pixel_shader_lane_dependent_matches_interpreter[diagonal_split-verts1] (#68) fails now and then, on main alone too.
  • 1,243 tests, so the README stays at 1200+ from #218.

On NVIDIA, for the earlier revisions:

  • akimfomin, RTX 4070 Ti SUPER, at b73d77c: tests/test_rdna2_hlsl_f16.py 19 passed, same bytes as the interpreter (#112).
  • minecraftafk072, at f6ac137: 19 passed on an RTX 4070 Ti SUPER, an RDNA 2 iGPU and WARP.
  • @⁠GravynChips, RTX 4070 SUPER, at b3d0b59: 31/31 and 19/19, plus 65,536 FMA cases checked on the GPU against exact integer arithmetic (comment on #165).

Oracle and test data

The reference interpreter (#218): each GPU test runs the same program in the interpreter and on the GPU, then compares the whole memory byte for byte.

  • Each of the wave's 32 lanes carries one case: ties, overflows, denormals, ±0, infinities, NaN, extreme ldexp exponents, then random values (fixed seed).
  • The high halves of the sources are marked per lane, for op_sel.
  • Without this change, each test raises Untranslatable; the refusal tests also require the reason in the message.

Checklist

  • The PR covers a single reviewable behaviour (f16 in the HLSL recompiler).
  • A test fails without this change and passes with it.
  • The commands and results of each check are given above.
  • An unimplemented function returns an error, never a fake success (unsourced forms are refused with Untranslatable).
  • README: no change beyond #218's (test_readme_sync.py checks it).
  • No game, console or SDK file, no key, no dump, no download link.
  • Findings drawn from a title: none.
  • Code taken from another project: none.

Made with AI assistance.

**@⁠IxMxAMAR** on GitHub · 2026-11-25 16:22 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/219) `IxMxAMAR:feat/rdna2-f16-hlsl` → `main` · +308 −7 · 6 files · 5 commits · merged 2026-11-25 18:37 UTC ## Change Replaces #165, which was closed during the 25 November triage. It is stacked on #218 (the f16 interpreter, which replaces #163): its first 5 commits are that PR's, and this one starts at "the HLSL recompiler translates f16 VOP1 and VOP2". I will rebase it on `main` as soon as #218 is merged. The RDNA 2 → HLSL recompiler translates the 25 f16 encodings the interpreter runs with #218, in compute, vertex and pixel shaders: - `v_add`, `v_sub`, `v_subrev`, `v_mul`, `v_fmac`, `v_fmamk`, `v_fmaak`, `v_max`, `v_min` and `v_ldexp_f16`, in VOP2 and VOP3; - `v_fma_f16`, in VOP3; - `v_cvt_f16_f32` and `v_cvt_f32_f16`, in VOP1 and VOP3. They live in a new module, `emu/rdna2_hlsl_f16.py`, wired into `rdna2_hlsl.py` (before the SDWA/DPP path, as in the interpreter), `rdna2_hlsl_graphics.py` and, since the rebase, `rdna2_hlsl_wave64.py`. There are no 16-bit types (shader model 6.0 is unchanged, with neither `half` nor `f16tof32`): an f16 value stays half of a VGPR. - Each operation goes through f32 in a `precise` function and comes back to f16 through an integer routine: round to nearest even, denormals, infinities, NaN 0x7E00. - The sum, difference and product of two f16 values stay exact through this double step (24 ≥ 2 × 11 + 2). - The FMA computes the exact error of the sum (TwoSum) and rounds to odd before the f16 rounding. - Min, max and clamp are done in integer. The forms the interpreter refuses are refused here too, with the same message; the tests reuse the `REFUSED_VOP3` list of `tests/test_rdna2_f16.py`. New since the rebase: the wave64 path that `main` gained (`emu/rdna2_hlsl_wave64.py`) translates f16 through `rdna2_hlsl.statements`, but its prelude lacked the `F16*` functions. A wave64 program with f16 called `F16Add` or `F16Fma` without defining them. One commit adds the functions to that prelude, and the f16 code now takes the caller's active-lane condition (`LANE_ON` in wave64), like the rest of `main`. `test_wave64_translation_carries_the_f16_helpers` fails without that commit. Linked issue: Refs #115 ## AMD driver: `x + 0` folded despite `precise` On the RX 9070 XT (RDNA 4), `v_add_f16 v10, 0, v1` with v1 = -0 gave -0 instead of +0. The DXIL from dxc does contain `fadd float %x, 0.0`, with no fast-math flag: the AMD driver folds it into x. Intel and WARP give +0. The sign of an exactly zero result (sum or FMA) is therefore set in integer from the operands; without this, the GPU test fails on the Radeon. ## Verification ``` python -m pytest → 954 passed, 289 skipped python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! $env:ANYXBOXX_GPU="Radeon"; python -m pytest tests/test_rdna2_hlsl_f16.py tests/test_rdna2_f16.py → 51 passed (AMD Radeon RX 9070 XT, on the previous base `f5cdd8e`) $env:ANYXBOXX_GPU="Intel"; python -m pytest tests/test_rdna2_hlsl_f16.py tests/test_rdna2_f16.py → 51 passed (Intel(R) Graphics) $env:ANYXBOXX_GPU="warp"; python -m pytest tests/test_rdna2_hlsl_f16.py tests/test_rdna2_f16.py → 51 passed (Microsoft Basic Render Driver) ``` - The skips are the tests that need the C++ runtime, the Rust crate or the TomBox DLLs, none of which were built in this checkout. - For the GPU runs, `anyxboxx_gpu_run.exe` came from an older build of `runtime/` (set through `ANYXBOXX_RUNTIME`). With it, the whole suite gives 14 failures, all in runtime tests that need the newer runtime (HDR luminance, console identity, the `--adaptateur` fields); none is in the f16 files. - The Radeon run was on the previous base: it was busy with a game run after the last rebase. The Intel and WARP runs are on `ab55a2a`. - On the Intel iGPU, `main`'s new `test_multi_lane_pixel_shader_lane_dependent_matches_interpreter[diagonal_split-verts1]` (#68) fails now and then, on `main` alone too. - 1,243 tests, so the README stays at `1200+` from #218. On NVIDIA, for the earlier revisions: - akimfomin, RTX 4070 Ti SUPER, at `b73d77c`: `tests/test_rdna2_hlsl_f16.py` 19 passed, same bytes as the interpreter (#112). - minecraftafk072, at `f6ac137`: 19 passed on an RTX 4070 Ti SUPER, an RDNA 2 iGPU and WARP. - @⁠GravynChips, RTX 4070 SUPER, at `b3d0b59`: 31/31 and 19/19, plus 65,536 FMA cases checked on the GPU against exact integer arithmetic (comment on #165). ## Oracle and test data The reference interpreter (#218): each GPU test runs the same program in the interpreter and on the GPU, then compares the whole memory byte for byte. - Each of the wave's 32 lanes carries one case: ties, overflows, denormals, ±0, infinities, NaN, extreme ldexp exponents, then random values (fixed seed). - The high halves of the sources are marked per lane, for `op_sel`. - Without this change, each test raises `Untranslatable`; the refusal tests also require the reason in the message. ## Checklist - [x] The PR covers a single reviewable behaviour (f16 in the HLSL recompiler). - [x] A test fails without this change and passes with it. - [x] The commands and results of each check are given above. - [x] An unimplemented function returns an error, never a fake success (unsourced forms are refused with `Untranslatable`). - [x] README: no change beyond #218's (`test_readme_sync.py` checks it). - [x] No game, console or SDK file, no key, no dump, no download link. - [x] Findings drawn from a title: none. - [x] Code taken from another project: none. Made with AI assistance.
The wave64 path (emu/rdna2_hlsl_wave64.py, which reached main after this work was written) translates
f16 through rdna2_hlsl.statements, but its prelude lacked the F16* functions: the HLSL of a wave64
program with f16 called F16Add or F16Fma without defining them. The f16 code also takes the caller's
active-lane condition (LANE_ON in wave64), like the rest of main.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:38:06 +00:00
github manually merged commit 670f91a15a into gh-pr/219/base 2026-11-26 05:38:08 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.