feat: v_floor/ceil/trunc/rndne_f16 in the HLSL recompiler #251

Manually merged
github merged 2 commits from gh-pr/251/head into gh-pr/251/base 2026-11-26 05:40:33 +00:00
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 21:33 UTC · original
lcg007xd-c:feat/hlsl-f16-rounding → main · +162 −18 · 12 files · 2 commits · merged 2026-11-26 00:15 UTC

Draft, stacked on #245. The first commit is #245 (f16 rounding in the interpreter); only the second commit, feat: v_floor/ceil/trunc/rndne_f16 in the HLSL recompiler, is new here. I will rebase onto main once #245 merges, and mark it ready then.

Change

The HLSL recompiler now translates v_floor_f16, v_ceil_f16, v_trunc_f16 and v_rndne_f16, in VOP1 and VOP3, which #245 adds to the interpreter. They are four new entries in rdna2_hlsl_f16.TEMPLATES:

F32ToF16(asuint(floor(asfloat(F16ToF32({a})))))      // ceil and trunc alike
F16Rndne({a})                                         // v_rndne_f16: integer only
  • F16ToF32 widens the half exactly. floor, ceil and trunc use the same HLSL functions as the f32 forms already checked on GPU (v_floor/ceil/trunc_f32 in rdna2_hlsl_ops).

  • v_rndne_f16 does not use round() (DXIL Round_ne). #260 measured the NVIDIA 621.xx driver folding Round_ne(2.5) to 3.0 (#227), so a new F16Rndne helper rounds to nearest even on the binary16 bits:

    • values of 1024 and above, ±inf and NaN (as 0x7E00) are returned unchanged;
    • |x| < 1 is handled apart (±0, or ±1 above one half);
    • otherwise the fraction bits are truncated, and the value rounds up on more than half, or on exactly half when the truncated value is odd; the carry flows into the exponent.

    A Python model of the helper matches the interpreter's v_rndne_f16 on all 65,536 inputs.

  • F32ToF16 is exact for an integer value, keeps the sign of zero, and gives 0x7E00 for a NaN, matching the interpreter's rule.

  • The module's VOP1/VOP3 handling applies unchanged: the low or high half (op_sel), abs/neg on bit 15, and clamp through F16Clamp.

Linked issue: Refs #115 (f16), #227.

Verification

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

python -m pytest                → 1103 passed, 326 skipped

Ruff: left to the CI lint job for this push.

GPU parity: Windows CI only (WARP), not run on an AMD or NVIDIA card. The first version (with round()) passed the GPU test on WARP, but WARP does not show the NVIDIA folding. Locally the GPU test's program translates and, with the GPU runner stubbed to return the interpreter's memory, builds and passes its own checks.

Oracle and test data

In tests/test_rdna2_f16_rounding.py (from #245), the HLSL refusal tests are replaced by:

  • a text test per op, also checking coverage_names(); for v_rndne_f16, that the shader body has no round(;
  • test_rounding_matches_the_interpreter_on_gpu: each op in VOP1 on three sources, VOP3 with op_sel high half in and out, neg, and abs + clamp, plus a literal (0xDEAD4100, 2.5), compared byte for byte with the interpreter. It reuses the inputs of tests/test_rdna2_hlsl_f16.py (ROWS: ties, denormals, ±0, ±inf, NaNs, random halves).

Made with AI assistance (Claude Code).

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:33 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/251) `lcg007xd-c:feat/hlsl-f16-rounding` → `main` · +162 −18 · 12 files · 2 commits · merged 2026-11-26 00:15 UTC **Draft, stacked on #245.** The first commit is #245 (f16 rounding in the interpreter); only the second commit, `feat: v_floor/ceil/trunc/rndne_f16 in the HLSL recompiler`, is new here. I will rebase onto `main` once #245 merges, and mark it ready then. ## Change The HLSL recompiler now translates `v_floor_f16`, `v_ceil_f16`, `v_trunc_f16` and `v_rndne_f16`, in VOP1 and VOP3, which #245 adds to the interpreter. They are four new entries in `rdna2_hlsl_f16.TEMPLATES`: ``` F32ToF16(asuint(floor(asfloat(F16ToF32({a}))))) // ceil and trunc alike F16Rndne({a}) // v_rndne_f16: integer only ``` - `F16ToF32` widens the half exactly. floor, ceil and trunc use the same HLSL functions as the f32 forms already checked on GPU (`v_floor/ceil/trunc_f32` in `rdna2_hlsl_ops`). - `v_rndne_f16` does not use `round()` (DXIL `Round_ne`). #260 measured the NVIDIA 621.xx driver folding `Round_ne(2.5)` to 3.0 (#227), so a new `F16Rndne` helper rounds to nearest even on the binary16 bits: - values of 1024 and above, ±inf and NaN (as `0x7E00`) are returned unchanged; - |x| < 1 is handled apart (±0, or ±1 above one half); - otherwise the fraction bits are truncated, and the value rounds up on more than half, or on exactly half when the truncated value is odd; the carry flows into the exponent. A Python model of the helper matches the interpreter's `v_rndne_f16` on all 65,536 inputs. - `F32ToF16` is exact for an integer value, keeps the sign of zero, and gives `0x7E00` for a NaN, matching the interpreter's rule. - The module's VOP1/VOP3 handling applies unchanged: the low or high half (`op_sel`), `abs`/`neg` on bit 15, and `clamp` through `F16Clamp`. Linked issue: Refs #115 (f16), #227. ## Verification Linux (Arch, Python 3.14), no C++ runtime, no GPU: ``` python -m pytest → 1103 passed, 326 skipped ``` Ruff: left to the CI lint job for this push. GPU parity: Windows CI only (WARP), not run on an AMD or NVIDIA card. The first version (with `round()`) passed the GPU test on WARP, but WARP does not show the NVIDIA folding. Locally the GPU test's program translates and, with the GPU runner stubbed to return the interpreter's memory, builds and passes its own checks. ## Oracle and test data In `tests/test_rdna2_f16_rounding.py` (from #245), the HLSL refusal tests are replaced by: - a text test per op, also checking `coverage_names()`; for `v_rndne_f16`, that the shader body has no `round(`; - `test_rounding_matches_the_interpreter_on_gpu`: each op in VOP1 on three sources, VOP3 with `op_sel` high half in and out, `neg`, and `abs` + `clamp`, plus a literal (`0xDEAD4100`, 2.5), compared byte for byte with the interpreter. It reuses the inputs of `tests/test_rdna2_hlsl_f16.py` (`ROWS`: ties, denormals, ±0, ±inf, NaNs, random halves). Made with AI assistance (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The four f16 rounding ops (VOP1 91-94 and their VOP3 forms, 8 encodings)
raised Untranslated. They join rdna2_exec_f16.OPERATIONS: the integral
value of S0 (exact in binary16), sign kept as the f32 forms do (IEEE 754
roundToIntegral: trunc(-0.5) = -0), infinities unchanged, NaN -> 0x7E00;
v_rndne rounds ties to even. The existing f16 rules apply: low half
written and high half kept, op_sel, abs/neg on bit 15, clamp to [0, 1].
ISA: AMD RDNA 2, section 12.8 (p. 148). HLSL not changed: it still
refuses them (tested). README: 1,208 -> 1,216 of 1,490 (82%).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The four f16 rounding ops run in the interpreter (#245); the recompiler
refused them. They join rdna2_hlsl_f16.TEMPLATES: the f16 half widened
with F16ToF32 (exact), the same HLSL functions as the f32 forms
(floor, ceil, trunc, round), then F32ToF16 (exact for an integer value,
sign of zero kept, NaN -> 0x7E00). The f16 VOP1/VOP3 rules of the module
apply (low/high half, op_sel, abs/neg on bit 15, clamp). A GPU test
compares VOP1, VOP3 op_sel/neg/abs/clamp and a literal with the
interpreter, byte for byte.

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:32 +00:00
github manually merged commit 2cedd7b940 into gh-pr/251/base 2026-11-26 05:40:33 +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/251#issuecomment-7175743392) 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 the updated #245 (itself on main 9a06443). No README change in this one; it stays a draft until #245 merges.

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:01 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/251#issuecomment-7175835379) Rebased onto the updated #245 (itself on `main` `9a06443`). No README change in this one; it stays a draft until #245 merges.
Author
Owner

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

Switched v_rndne_f16 to an integer-only helper (F16Rndne) so it does not hit the NVIDIA Round_ne(2.5) folding from #227/#260; checked against the interpreter on all 65,536 f16 inputs.

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:38 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/251#issuecomment-7176605342) Switched `v_rndne_f16` to an integer-only helper (`F16Rndne`) so it does not hit the NVIDIA `Round_ne(2.5)` folding from #227/#260; checked against the interpreter on all 65,536 f16 inputs.
This discussion has been locked. Commenting is limited to contributors.
No description provided.