fix: implement v_rndne_f32 in integer HLSL to avoid NVIDIA Round_ne f… #260

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

@⁠pandolfipedro on GitHub · 2026-11-25 22:16 UTC · original
pandolfipedro:fix/hlsl-rndne-f32-nvidia → main · +47 −1 · 3 files · 1 commits · merged 2026-11-26 00:15 UTC

…olding bug

Fixes #227. On NVIDIA driver 621.xx (measured on RTX 4060 Laptop 621.14 and reported on RTX 5070 Ti 621.40), DXIL Round_ne(2.5) after a barrier is constant-folded to 3.0 (round half away from zero) instead of 2.0 (ties to even), and NaN payloads are replaced with 0x7FFFFFFF (while WARP leaves signalling NaNs unquieted). Computing RndneF32 in integer bit operations matches the reference interpreter bit for bit on NVIDIA, Intel UHD, and WARP.

Change

Linked issue: Fixes # / Refs #

Verification

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

Oracle and test data

Checklist

  • The PR covers a single reviewable behaviour.
  • 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 (E_NOTIMPL), never a fake success.
  • README: the same change in all 9 languages (test_readme_sync.py checks it).
  • No game, console or SDK file, no key, no dump, no download link.
  • Findings drawn from a title: limited to what rule 2 of AGENTS.md allows (names, slots, argument shapes, flag values, HRESULTs, IIDs, counts), with how they were obtained; no address or RVA.
  • Code taken from another project: compatible licence and credit in THIRD_PARTY.md.
**@⁠pandolfipedro** on GitHub · 2026-11-25 22:16 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/260) `pandolfipedro:fix/hlsl-rndne-f32-nvidia` → `main` · +47 −1 · 3 files · 1 commits · merged 2026-11-26 00:15 UTC …olding bug Fixes #227. On NVIDIA driver 621.xx (measured on RTX 4060 Laptop 621.14 and reported on RTX 5070 Ti 621.40), DXIL Round_ne(2.5) after a barrier is constant-folded to 3.0 (round half away from zero) instead of 2.0 (ties to even), and NaN payloads are replaced with 0x7FFFFFFF (while WARP leaves signalling NaNs unquieted). Computing RndneF32 in integer bit operations matches the reference interpreter bit for bit on NVIDIA, Intel UHD, and WARP. ## Change <!-- Please write in English. The observable behaviour that changes, and why. One reviewable thing per PR. --> Linked issue: Fixes # / Refs # ## Verification <!-- For each check: the exact command and its result (copy the last line). GPU: card or WARP, and the first difference if there is one. If you could not run a check, say so and name what was missing. --> ``` python -m pytest → … passed python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! ``` ## Oracle and test data <!-- Decoder: compared with the LLVM tables (`tools/rdna2_mnemonics.py … --verifier`). HLSL translation: compared with the reference interpreter, byte for byte. New test data: where it comes from (built in the tests, LLVM…) and its licence. Failure case: which test fails without this change? --> ## Checklist - [ ] The PR covers a single reviewable behaviour. - [ ] 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 (`E_NOTIMPL`), never a fake success. - [ ] README: the same change in all 9 languages (`test_readme_sync.py` checks it). - [ ] No game, console or SDK file, no key, no dump, no download link. - [ ] Findings drawn from a title: limited to what rule 2 of `AGENTS.md` allows (names, slots, argument shapes, flag values, HRESULTs, IIDs, counts), with how they were obtained; no address or RVA. - [ ] Code taken from another project: compatible licence and credit in `THIRD_PARTY.md`. <!-- If this change was made with AI assistance, say so here. -->
Fixes #227. On NVIDIA driver 621.xx (measured on RTX 4060 Laptop 621.14 and reported on RTX 5070 Ti 621.40), DXIL Round_ne(2.5) after a barrier is constant-folded to 3.0 (round half away from zero) instead of 2.0 (ties to even), and NaN payloads are replaced with 0x7FFFFFFF (while WARP leaves signalling NaNs unquieted). Computing RndneF32 in integer bit operations matches the reference interpreter bit for bit on NVIDIA, Intel UHD, and WARP.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:40:48 +00:00
github manually merged commit 4d459a42e6 into gh-pr/260/base 2026-11-26 05:40:48 +00:00
Author
Owner

@⁠Cai-piroska on GitHub · 2026-11-25 22:38 UTC · original

AI use: tests run, regression located and variant written with Claude Code (Claude Opus 5.5) on my machine.

This fixes the RTX 3080 Ti, but it breaks test_extended_alu_matches_interpreter_on_gpu on an RX 9070 XT (RDNA 4). This is the same AMD driver problem as the first version of #160: the early returns in RndneF32 make the driver miscompile the neighbouring v_bfrev_b32. A single-return form of the same integer algorithm passes on all three adapters.

Setup: Windows 11 26300, dxc 1.9.2602.17 (Windows SDK 10.0.26100), C++ runtime built from main 9a06443 with VS 2022 Build Tools. Trees: this head 4d459a4 (base affaf15), and main 9a06443 as the control. Command: python -m pytest tests/test_rdna2*.py, 2 runs per adapter. The totals differ because main also has tests/test_rdna2_exec_ds_addtid.py from #248, which this branch's base does not.

Adapter main 9a06443 this PR 4d459a4 this PR, single return (below)
RX 9070 XT (RDNA 4), driver 32.0.32029.2010 997 passed 990 passed, 1 failed 991 passed
RTX 3080 Ti (Ampere), driver 32.0.16.2114 996 passed, 1 failed 991 passed 991 passed
WARP 10.0.26100.9818 997 passed 991 passed 991 passed
  • The failure on main on the RTX 3080 Ti is the v_rndne_f32 tie from #227, which this PR fixes.
  • With this PR, the RX 9070 XT fails test_extended_alu_matches_interpreter_on_gpu in both runs. v_rndne_f32 itself is right there. The wrong word is the v_bfrev_b32 result (v13, stored in slot 9): on all 32 lanes the GPU returns 0x60c04080 and the interpreter 0x20c04080, with bit 30 set. Your new test_v_rndne_f32_ties_to_even_and_specials_on_gpu passes there.
  • #160 hit the same thing on this card: its first version also had early returns. 855e527 fixed it by computing the result and choosing it at the end, and added test_rndne_helper_has_a_single_return. #160 was closed for a conflict, so neither the fix nor that test is on main.

The variant I ran keeps your algorithm and only replaces the early returns with selects:

uint RndneF32(uint a) {
  uint sign_bit = a & 0x80000000u, mag = a & 0x7FFFFFFFu;
  uint shift = (mag >= 0x3F800000u && mag < 0x4B000000u) ? 150u - (mag >> 23) : 1u;
  uint mask = (1u << shift) - 1u, half_bit = 1u << (shift - 1u);
  uint rem = mag & mask, rounded = mag & ~mask;
  bool up = rem > half_bit || (rem == half_bit && (rounded & (1u << shift)) != 0u);
  uint mid = sign_bit | (rounded + (up ? (1u << shift) : 0u));
  uint big = mag > 0x7F800000u ? (a | 0x400000u) : a;
  uint small = mag < 0x3F800000u ? (mag <= 0x3F000000u ? sign_bit : (sign_bit | 0x3F800000u)) : mid;
  return mag >= 0x4B000000u ? big : small;
}

shift is held at 1 outside the range where it is used, so the shifts stay defined. With it, both v_rndne_f32 tests and the v_bfrev_b32 word match on all three adapters. Bringing back #160's test_rndne_helper_has_a_single_return would keep this from coming back.

**@⁠Cai-piroska** on GitHub · 2026-11-25 22:38 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/260#issuecomment-7176609742) *AI use: tests run, regression located and variant written with Claude Code (Claude Opus 5.5) on my machine.* This fixes the RTX 3080 Ti, but it breaks `test_extended_alu_matches_interpreter_on_gpu` on an RX 9070 XT (RDNA 4). This is the same AMD driver problem as the first version of #160: the early `return`s in `RndneF32` make the driver miscompile the neighbouring `v_bfrev_b32`. A single-return form of the same integer algorithm passes on all three adapters. **Setup:** Windows 11 26300, dxc 1.9.2602.17 (Windows SDK 10.0.26100), C++ runtime built from `main` `9a06443` with VS 2022 Build Tools. Trees: this head `4d459a4` (base `affaf15`), and `main` `9a06443` as the control. Command: `python -m pytest tests/test_rdna2*.py`, 2 runs per adapter. The totals differ because `main` also has `tests/test_rdna2_exec_ds_addtid.py` from #248, which this branch's base does not. | Adapter | `main` `9a06443` | this PR `4d459a4` | this PR, single `return` (below) | |---|---|---|---| | RX 9070 XT (RDNA 4), driver 32.0.32029.2010 | 997 passed | **990 passed, 1 failed** | 991 passed | | RTX 3080 Ti (Ampere), driver 32.0.16.2114 | 996 passed, 1 failed | 991 passed | 991 passed | | WARP 10.0.26100.9818 | 997 passed | 991 passed | 991 passed | - The failure on `main` on the RTX 3080 Ti is the `v_rndne_f32` tie from #227, which this PR fixes. - With this PR, the RX 9070 XT fails `test_extended_alu_matches_interpreter_on_gpu` in both runs. `v_rndne_f32` itself is right there. The wrong word is the `v_bfrev_b32` result (`v13`, stored in slot 9): on all 32 lanes the GPU returns `0x60c04080` and the interpreter `0x20c04080`, with bit 30 set. Your new `test_v_rndne_f32_ties_to_even_and_specials_on_gpu` passes there. - #160 hit the same thing on this card: its first version also had early `return`s. 855e527 fixed it by computing the result and choosing it at the end, and added `test_rndne_helper_has_a_single_return`. #160 was closed for a conflict, so neither the fix nor that test is on `main`. The variant I ran keeps your algorithm and only replaces the early `return`s with selects: ```hlsl uint RndneF32(uint a) { uint sign_bit = a & 0x80000000u, mag = a & 0x7FFFFFFFu; uint shift = (mag >= 0x3F800000u && mag < 0x4B000000u) ? 150u - (mag >> 23) : 1u; uint mask = (1u << shift) - 1u, half_bit = 1u << (shift - 1u); uint rem = mag & mask, rounded = mag & ~mask; bool up = rem > half_bit || (rem == half_bit && (rounded & (1u << shift)) != 0u); uint mid = sign_bit | (rounded + (up ? (1u << shift) : 0u)); uint big = mag > 0x7F800000u ? (a | 0x400000u) : a; uint small = mag < 0x3F800000u ? (mag <= 0x3F000000u ? sign_bit : (sign_bit | 0x3F800000u)) : mid; return mag >= 0x4B000000u ? big : small; } ``` `shift` is held at 1 outside the range where it is used, so the shifts stay defined. With it, both `v_rndne_f32` tests and the `v_bfrev_b32` word match on all three adapters. Bringing back #160's `test_rndne_helper_has_a_single_return` would keep this from coming back.
Author
Owner

@⁠KainanP on GitHub · 2026-11-25 23:42 UTC · original

Setup: Windows 11 26200, dxc 1.8.2502.11 (Windows SDK 10.0.26100.0), Python 3.12.10, C++ runtime built from main 3e258c8. Adapters: RTX 4060 Laptop (driver 32.0.16.1047, i.e. 610.47; default adapter), Intel UHD 8086:a78b (31.0.101.5445, ANYXBOXX_GPU=Intel, 16 lanes, no FP64), WARP 10.0.26100.9818 (ANYXBOXX_GPU=warp). Trees: main 3e258c8; this PR merged onto it (clean merge); the same plus the single-return RndneF32 from Cai-piroska's comment, pasted verbatim.

main this PR single-return variant
RTX 4060, python -m pytest 1404 passed, 17 skipped 1405 passed, 17 skipped 1405 passed, 17 skipped
WARP, tests/test_rdna2*.py 996 passed, 2 skipped 997 passed, 2 skipped 997 passed, 2 skipped
Intel UHD, tests/test_rdna2*.py 994 passed, 4 skipped 995 passed, 4 skipped 995 passed, 4 skipped

tests/test_rdna2_alu_parity.py (test_extended_alu_matches_interpreter_on_gpu and your new test) was run a second time on each adapter and tree, with the same results.

  • #227 on the RTX 4060: test_extended_alu_matches_interpreter_on_gpu passes on main with this driver, so that test alone does not show the bug here. Your new test does. I ran its body on main without the uint RndneF32( text check: v_rndne_f32 of the 2.5, -2.5 and 0.5 literals after the ballot gives 3.0, -3.0 and 1.0 on lanes 10-31 (22 of 32), and all five NaN inputs come back as 0x7FFFFFFF. On main, Intel UHD shows no difference, and WARP returns only the two signalling NaNs unquieted (0x7F800001 where the interpreter gives 0x7FC00001). With this PR or the variant there are no differences on any of the three adapters.
  • On main the new test fails on all three adapters, but on WARP and Intel only because of the uint RndneF32( text check.
  • The variant gives the same results as this PR on all three adapters, so taking it costs nothing here. I can't test the RX 9070 XT miscompile. Adopting the variant and bringing back test_rndne_helper_has_a_single_return seems the safe choice.
  • Semantics: the integer helper quiets a NaN (a | 0x400000), as the interpreter does in bits_float, and gives a signed zero for subnormals and |x| <= 0.5, like _round_even. No refusal changes.

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this.

**@⁠KainanP** on GitHub · 2026-11-25 23:42 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/260#issuecomment-7177941934) **Setup:** Windows 11 26200, dxc 1.8.2502.11 (Windows SDK 10.0.26100.0), Python 3.12.10, C++ runtime built from `main` `3e258c8`. Adapters: RTX 4060 Laptop (driver 32.0.16.1047, i.e. 610.47; default adapter), Intel UHD 8086:a78b (31.0.101.5445, `ANYXBOXX_GPU=Intel`, 16 lanes, no FP64), WARP 10.0.26100.9818 (`ANYXBOXX_GPU=warp`). Trees: `main` `3e258c8`; this PR merged onto it (clean merge); the same plus the single-return `RndneF32` from Cai-piroska's comment, pasted verbatim. | | `main` | this PR | single-return variant | |---|---|---|---| | RTX 4060, `python -m pytest` | 1404 passed, 17 skipped | 1405 passed, 17 skipped | 1405 passed, 17 skipped | | WARP, `tests/test_rdna2*.py` | 996 passed, 2 skipped | 997 passed, 2 skipped | 997 passed, 2 skipped | | Intel UHD, `tests/test_rdna2*.py` | 994 passed, 4 skipped | 995 passed, 4 skipped | 995 passed, 4 skipped | `tests/test_rdna2_alu_parity.py` (`test_extended_alu_matches_interpreter_on_gpu` and your new test) was run a second time on each adapter and tree, with the same results. - **#227 on the RTX 4060:** `test_extended_alu_matches_interpreter_on_gpu` passes on `main` with this driver, so that test alone does not show the bug here. Your new test does. I ran its body on `main` without the `uint RndneF32(` text check: `v_rndne_f32` of the 2.5, -2.5 and 0.5 literals after the ballot gives 3.0, -3.0 and 1.0 on lanes 10-31 (22 of 32), and all five NaN inputs come back as `0x7FFFFFFF`. On `main`, Intel UHD shows no difference, and WARP returns only the two signalling NaNs unquieted (`0x7F800001` where the interpreter gives `0x7FC00001`). With this PR or the variant there are no differences on any of the three adapters. - On `main` the new test fails on all three adapters, but on WARP and Intel only because of the `uint RndneF32(` text check. - The variant gives the same results as this PR on all three adapters, so taking it costs nothing here. I can't test the RX 9070 XT miscompile. Adopting the variant and bringing back `test_rndne_helper_has_a_single_return` seems the safe choice. - Semantics: the integer helper quiets a NaN (`a | 0x400000`), as the interpreter does in `bits_float`, and gives a signed zero for subnormals and `|x| <= 0.5`, like `_round_even`. No refusal changes. _AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this._
This discussion has been locked. Commenting is limited to contributors.
No description provided.