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

Merged
lcg007xd-c merged 2 commits from upstream-pr/251 into main 2026-11-26 00:15:06 +00:00
lcg007xd-c commented 2026-11-25 21:33:09 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by lcg007xd-c on 2026-11-25T21:33:09Z.


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

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:6614185372 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/251). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T21:33:09Z. --- **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)
LooperSalty commented 2026-11-25 21:56:55 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by LooperSalty on 2026-11-25T21:56:55Z.


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.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7175743392 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/251#issuecomment-7175743392). Originally posted by [LooperSalty](https://github.com/LooperSalty) on 2026-11-25T21:56:55Z. --- 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.
lcg007xd-c commented 2026-11-25 22:01:20 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by lcg007xd-c on 2026-11-25T22:01:20Z.


Rebased onto the updated #245 (itself on main 9a06443). No README change in this one; it stays a draft until #245 merges.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7175835379 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/251#issuecomment-7175835379). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T22:01:20Z. --- Rebased onto the updated #245 (itself on `main` `9a06443`). No README change in this one; it stays a draft until #245 merges.
lcg007xd-c commented 2026-11-25 22:38:07 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by lcg007xd-c on 2026-11-25T22:38:07Z.


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.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7176605342 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/251#issuecomment-7176605342). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T22:38:07Z. --- 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.
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
LooperSalty/AnyXboxX!251
No description provided.