feat: half-precision compares (v_cmp[x]_*_f16, v_cmp[x]_class_f16) in the HLSL recompiler #250

Manually merged
github merged 1 commit from gh-pr/250/head into gh-pr/250/base 2026-11-26 05:40:28 +00:00
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 21:27 UTC · original
lcg007xd-c:feat/hlsl-f16-compares → main · +218 −15 · 5 files · 1 commits · merged 2026-11-26 00:15 UTC

Change

The HLSL recompiler now translates the 68 f16 compare encodings that #244 added to the interpreter: v_cmp[x]_{f,lt,…,tru}_f16 and v_cmp[x]_class_f16, in VOPC and VOP3, for wave32 and wave64. Until now it refused them.

The code is a new module, emu/rdna2_hlsl_cmpf16.py (about 50 lines), with one branch each in the VOPC and VOP3 paths of rdna2_hlsl.py and rdna2_hlsl_wave64.py:

  • Sources: read as for the other f16 instructions (rdna2_hlsl_f16._operand): a register's low half, an inline float as binary16 (0x3C00), a literal's low half. VOP3 abs/neg act on bit 15 (rdna2_hlsl_f16._modified).
  • Predicates: both halves are widened with the existing F16ToF32, which is exact (an f16 denormal is a normal f32), and the existing f32 conditions are reused (compare_condition), so NaN is tested apart as in #134.
  • Class: v_cmp_class_f16 reads the f16 bits through a new ClassF16 helper, because widening would make a signalling NaN quiet and a denormal normal.
  • Refused like the interpreter: op_sel, clamp, omod, and abs/neg on the class mask. The f16 compares are routed before the SDWA path, so their SDWA/DPP forms are refused by name too.

Linked issue: Refs #115 (f16).

Verification

Linux (Arch, Python 3.14.10), no C++ runtime, no GPU:

python -m pytest                → 1091 passed, 323 skipped
python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed!

GPU parity: Windows CI only. CI run 45704586464 (WARP): tests/test_rdna2_hlsl_cmpf16.py 11 passed, the 4 GPU tests included; whole suite 1411 passed, 3 skipped. Not run on an AMD or NVIDIA card.

Locally: I have no Windows, dxc or WARP; the Windows CI job runs them. On the CPU I checked that:

  • every GPU program translates (wave32 and wave64);
  • with the GPU runner stubbed to return the interpreter's memory, each test builds and its own asserts hold;
  • the generated text uses F16ToF32, IsNanF32, ClassF16 and Ballot64 where expected.

Oracle and test data

  • New tests/test_rdna2_hlsl_cmpf16.py:

    • CPU tests: the widening and the class helper in the text; inline 1.0 as 0x3C00; a literal's low half; abs/neg on bit 15; refusals of SDWA, op_sel, clamp and abs/neg on the mask; wave64 using Ballot64.
    • GPU parity, byte for byte against the interpreter:
      • every predicate, as v_cmp and v_cmpx, in VOPC and VOP3, under a partial EXEC (68 masks, in two programs);
      • inline constants, a literal and modifiers;
      • wave64 with a partial 64-bit EXEC.

    The inputs are the hand-labelled pairs of #244 (±0, quiet and signalling NaN, ± denormals, ±inf, neighbours), one value per class against single class bits, and pseudo-random words, all with junk high halves.

  • The interpreter tests of #244 that asserted the HLSL refusal are removed, since the recompiler now translates these instructions.

Made with AI assistance (Claude Code).

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:27 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/250) `lcg007xd-c:feat/hlsl-f16-compares` → `main` · +218 −15 · 5 files · 1 commits · merged 2026-11-26 00:15 UTC ## Change The HLSL recompiler now translates the 68 f16 compare encodings that #244 added to the interpreter: `v_cmp[x]_{f,lt,…,tru}_f16` and `v_cmp[x]_class_f16`, in VOPC and VOP3, for wave32 and wave64. Until now it refused them. The code is a new module, `emu/rdna2_hlsl_cmpf16.py` (about 50 lines), with one branch each in the VOPC and VOP3 paths of `rdna2_hlsl.py` and `rdna2_hlsl_wave64.py`: - **Sources**: read as for the other f16 instructions (`rdna2_hlsl_f16._operand`): a register's low half, an inline float as binary16 (`0x3C00`), a literal's low half. VOP3 `abs`/`neg` act on bit 15 (`rdna2_hlsl_f16._modified`). - **Predicates**: both halves are widened with the existing `F16ToF32`, which is exact (an f16 denormal is a normal f32), and the existing f32 conditions are reused (`compare_condition`), so NaN is tested apart as in #134. - **Class**: `v_cmp_class_f16` reads the f16 bits through a new `ClassF16` helper, because widening would make a signalling NaN quiet and a denormal normal. - **Refused like the interpreter**: `op_sel`, `clamp`, `omod`, and `abs`/`neg` on the class mask. The f16 compares are routed before the SDWA path, so their SDWA/DPP forms are refused by name too. Linked issue: Refs #115 (f16). ## Verification Linux (Arch, Python 3.14.10), no C++ runtime, no GPU: ``` python -m pytest → 1091 passed, 323 skipped python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! ``` **GPU parity: Windows CI only.** CI run 45704586464 (WARP): `tests/test_rdna2_hlsl_cmpf16.py` 11 passed, the 4 GPU tests included; whole suite 1411 passed, 3 skipped. Not run on an AMD or NVIDIA card. Locally: I have no Windows, `dxc` or WARP; the Windows CI job runs them. On the CPU I checked that: - every GPU program translates (wave32 and wave64); - with the GPU runner stubbed to return the interpreter's memory, each test builds and its own asserts hold; - the generated text uses `F16ToF32`, `IsNanF32`, `ClassF16` and `Ballot64` where expected. ## Oracle and test data - New `tests/test_rdna2_hlsl_cmpf16.py`: - CPU tests: the widening and the class helper in the text; inline 1.0 as `0x3C00`; a literal's low half; abs/neg on bit 15; refusals of SDWA, op_sel, clamp and abs/neg on the mask; wave64 using `Ballot64`. - GPU parity, byte for byte against the interpreter: - every predicate, as `v_cmp` and `v_cmpx`, in VOPC and VOP3, under a partial EXEC (68 masks, in two programs); - inline constants, a literal and modifiers; - wave64 with a partial 64-bit EXEC. The inputs are the hand-labelled pairs of #244 (±0, quiet and signalling NaN, ± denormals, ±inf, neighbours), one value per class against single class bits, and pseudo-random words, all with junk high halves. - The interpreter tests of #244 that asserted the HLSL refusal are removed, since the recompiler now translates these instructions. Made with AI assistance (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The 68 f16 compare encodings run in the interpreter (#244); the recompiler
refused them. New emu/rdna2_hlsl_cmpf16.py, used by the wave32 and wave64
VOPC and VOP3 paths:
- sources as the other f16 instructions (rdna2_hlsl_f16._operand): low
  half of a register, inline float as binary16, literal's low half; VOP3
  abs/neg on bit 15;
- the 16 predicates widen both halves with F16ToF32 (exact: an f16
  denormal is a normal f32) and reuse the f32 conditions, NaN tested
  apart; v_cmp_class_f16 reads the f16 bits (new ClassF16 helper), since
  widening would make a signalling NaN quiet and a denormal normal;
- refused like the interpreter: op_sel, clamp, omod, abs/neg on the class
  mask, SDWA/DPP (f16 compares are routed before the SDWA path).
GPU parity tests compare every mask with the interpreter in wave32 (all
68 encodings, partial EXEC), constants/modifiers, and wave64.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:40:26 +00:00
github manually merged commit b244a2431e into gh-pr/250/base 2026-11-26 05:40:28 +00:00
Author
Owner

@⁠LooperSalty on GitHub · 2026-11-25 21:56 UTC · original

Thanks — this one is approved, but it now conflicts with main after #242, #244 and #248 were merged (mostly the README RDNA 2 line, plus shared rdna2 files for some). Could you merge or rebase onto main, recount the README line with rdna2_exec.coverage() on the merged tree (all 9 languages), and push? I will merge it as soon as CI is green.

**@⁠LooperSalty** on GitHub · 2026-11-25 21:56 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/250#issuecomment-7175742178) Thanks — this one is approved, but it now conflicts with main after #242, #244 and #248 were merged (mostly the README RDNA 2 line, plus shared rdna2 files for some). Could you merge or rebase onto main, recount the README line with `rdna2_exec.coverage()` on the merged tree (all 9 languages), and push? I will merge it as soon as CI is green.
Author
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 22:01 UTC · original

Rebased onto main (9a06443); the only conflict was the rdna2_hlsl_wave64.py import line next to #248's LDS_ADD_TID. No README change in this one.

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:01 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/250#issuecomment-7175834692) Rebased onto `main` (`9a06443`); the only conflict was the `rdna2_hlsl_wave64.py` import line next to #248's `LDS_ADD_TID`. No README change in this one.
This discussion has been locked. Commenting is limited to contributors.
No description provided.