perf: v_rndne_f32 rounds with the host's float addition, sine and cosine too (2.85x / 1.97x on a 4060 Laptop) #585

Open
KainanP wants to merge 5 commits from upstream-pr/585 into main
KainanP commented 2026-11-28 16:36:43 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-28T16:36:43Z.


Stacked on #581 (which is stacked on #570): it uses #581's per-instruction bench (skinning_bench::op_loop_bench). Only the last commit is new; merge #570 and #581 first.

What

v_rndne_f32 was rounded in integer bit arithmetic (32 instructions), so as not to use DXIL Round_ne. The NVIDIA driver folds Round_ne wrongly for half-way literals after a barrier, and it replaces NaN payloads.

Below 2^23 the magnitude is now rounded by the host's own float addition. |x| + 2^23 lies in [2^23, 2^24), where every float is a whole number, so the sum is |x| rounded to nearest even. Taking 2^23 off again is exact, and the sign is or-ed back, so -0.4 gives -0. |x| >= 2^23, infinities and NaNs keep the old rule: unchanged, and a NaN is quieted with its payload kept. A denormal gives a zero of its sign, whether the host flushes it or not.

  • v_rndne_f32: 32 instructions become 12.
  • v_sin_f32 / v_cos_f32: their turn reduction is x - rndne(x), so they go from 42 to 22.

TRANSLATOR_VERSION 22.

Is the driver bug still there? Yes, on 610.47

I tested plain Round_ne (opcode 26) on our RTX 4060 Laptop, driver 610.47, with a literal after s_barrier:

Literal NVIDIA 610.47 Intel UHD WARP
2.5 3.0 2.0 2.0
-2.5 -3.0 -2.0 -2.0
0.5 1.0 0 0
-0.5 -1.0 -0 -0

The same values read at run time round correctly everywhere (65,536 half-way values). NaN payloads come out as 0x7FFFFFFF on NVIDIA. So Round_ne stays out. The addition form gives the right bits for the literal cases on all three adapters; the existing test v_rndne_f32_ties_to_even_and_specials covers it.

Bench (RTX 4060 Laptop, 8 dependent chains x 256 rounds, 8192 x 32 threads; median of 3 alternating rounds)

Instruction #581 (before) This PR HLSL Gain
v_rndne_f32 3.348 ms 1.174 ms 0.609 ms (round) 2.85x
v_sin_f32 3.60 ms 1.83 ms 0.670 ms (sin(x*2pi), not exact) 1.97x

Command: cargo test -p victory-shader --release --lib skinning_bench::op_loop_bench -- --ignored --nocapture. The before and after binaries ran alternately under one GPU lock.

Tests (fail before / exact)

  • rndne_tests::rndne_rounds_with_the_hosts_addition (shape): v_rndne_f32 must be ≤ 16 instructions and v_sin_f32 ≤ 26. It fails on #581's head ("v_rndne_f32 writes 32 instructions").

  • rndne_tests::rndne_matches_rdna2_on_every_lane: 283,648 lanes checked against RDNA 2's rule:

    • every exponent of both signs with edge mantissas;
    • every n + 0.5 below 2^16, and half-way values up to 2^23;
    • NaNs, denormals and 2^17 random patterns.

    It ran on the RTX 4060 Laptop, on Intel UHD (VICTORY_D3D12_ADAPTER=Intel) and on WARP. Through the new bulk harness it also compares against the reference interpreter (crates/anyxboxx-rdna2-ref, via ANYXBOXX_RDNA2_REF or target/) on 281,678 of them. The interpreter keeps a signalling NaN unquieted; the translation has always quieted it, as IEEE roundToIntegral does, so those 1,970 lanes are left out of the interpreter comparison only. Worth settling on hardware (PortBridge).

  • rndne_tests::sin_and_cos_reduce_whole_turns_exactly: v_sin_f32 and v_cos_f32 of each angle must give the same bits as of its fraction of a turn, computed exactly on the CPU. This proves the reduction loses nothing on all three adapters. The sine itself is the host's, so no interpreter has its bits.

  • New exact_tests.rs: a bulk harness. One instruction runs on thousands of lanes, each with its own 4 dwords. The harness also runs the same instruction through the reference interpreter, 128 blocks of 32 lanes per run.

The full victory-shader suite (top of the stack, with #586 and #587) passes on the RTX 4060 Laptop and WARP (380 passed). On Intel UHD 370 pass; the same 10 wave64/FP64/BVH tests also fail on #581's head there (the adapter refuses those pipelines). pytest: 1454 passed, 529 skipped.

AI use: written with Claude Code (Claude Opus 5.5): lowering, tests, measurements on my machine. The driver check and the benches ran on my RTX 4060 Laptop and Intel UHD.

🤖 Generated with Claude Code

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5542346777 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/585). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-28T16:36:43Z. --- **Stacked on #581** (which is stacked on #570): it uses #581's per-instruction bench (`skinning_bench::op_loop_bench`). Only the last commit is new; merge #570 and #581 first. ## What `v_rndne_f32` was rounded in integer bit arithmetic (32 instructions), so as not to use DXIL `Round_ne`. The NVIDIA driver folds `Round_ne` wrongly for half-way literals after a barrier, and it replaces NaN payloads. Below 2^23 the magnitude is now rounded by the host's own float addition. `|x| + 2^23` lies in [2^23, 2^24), where every float is a whole number, so the sum is `|x|` rounded to nearest even. Taking 2^23 off again is exact, and the sign is or-ed back, so -0.4 gives -0. `|x| >= 2^23`, infinities and NaNs keep the old rule: unchanged, and a NaN is quieted with its payload kept. A denormal gives a zero of its sign, whether the host flushes it or not. - `v_rndne_f32`: 32 instructions become 12. - `v_sin_f32` / `v_cos_f32`: their turn reduction is `x - rndne(x)`, so they go from 42 to 22. `TRANSLATOR_VERSION` 22. ## Is the driver bug still there? Yes, on 610.47 I tested plain `Round_ne` (opcode 26) on our RTX 4060 Laptop, driver 610.47, with a literal after `s_barrier`: | Literal | NVIDIA 610.47 | Intel UHD | WARP | |---|---|---|---| | 2.5 | **3.0** | 2.0 | 2.0 | | -2.5 | **-3.0** | -2.0 | -2.0 | | 0.5 | **1.0** | 0 | 0 | | -0.5 | **-1.0** | -0 | -0 | The same values read at run time round correctly everywhere (65,536 half-way values). NaN payloads come out as `0x7FFFFFFF` on NVIDIA. So `Round_ne` stays out. The addition form gives the right bits for the literal cases on all three adapters; the existing test `v_rndne_f32_ties_to_even_and_specials` covers it. ## Bench (RTX 4060 Laptop, 8 dependent chains x 256 rounds, 8192 x 32 threads; median of 3 alternating rounds) | Instruction | #581 (before) | This PR | HLSL | Gain | |---|---|---|---|---| | `v_rndne_f32` | 3.348 ms | 1.174 ms | 0.609 ms (`round`) | **2.85x** | | `v_sin_f32` | 3.60 ms | 1.83 ms | 0.670 ms (`sin(x*2pi)`, not exact) | **1.97x** | Command: `cargo test -p victory-shader --release --lib skinning_bench::op_loop_bench -- --ignored --nocapture`. The before and after binaries ran alternately under one GPU lock. ## Tests (fail before / exact) - `rndne_tests::rndne_rounds_with_the_hosts_addition` (shape): `v_rndne_f32` must be ≤ 16 instructions and `v_sin_f32` ≤ 26. **It fails on #581's head** ("v_rndne_f32 writes 32 instructions"). - `rndne_tests::rndne_matches_rdna2_on_every_lane`: 283,648 lanes checked against RDNA 2's rule: - every exponent of both signs with edge mantissas; - every n + 0.5 below 2^16, and half-way values up to 2^23; - NaNs, denormals and 2^17 random patterns. It ran on the RTX 4060 Laptop, on Intel UHD (`VICTORY_D3D12_ADAPTER=Intel`) and on WARP. Through the new bulk harness it also compares against the reference interpreter (`crates/anyxboxx-rdna2-ref`, via `ANYXBOXX_RDNA2_REF` or `target/`) on 281,678 of them. The interpreter keeps a signalling NaN unquieted; the translation has always quieted it, as IEEE `roundToIntegral` does, so those 1,970 lanes are left out of the interpreter comparison only. Worth settling on hardware (PortBridge). - `rndne_tests::sin_and_cos_reduce_whole_turns_exactly`: `v_sin_f32` and `v_cos_f32` of each angle must give the same bits as of its fraction of a turn, computed exactly on the CPU. This proves the reduction loses nothing on all three adapters. The sine itself is the host's, so no interpreter has its bits. - New `exact_tests.rs`: a bulk harness. One instruction runs on thousands of lanes, each with its own 4 dwords. The harness also runs the same instruction through the reference interpreter, 128 blocks of 32 lanes per run. The full `victory-shader` suite (top of the stack, with #586 and #587) passes on the RTX 4060 Laptop and WARP (380 passed). On Intel UHD 370 pass; the same 10 wave64/FP64/BVH tests also fail on #581's head there (the adapter refuses those pipelines). pytest: 1454 passed, 529 skipped. _AI use: written with Claude Code (Claude Opus 5.5): lowering, tests, measurements on my machine. The driver check and the benches ran on my RTX 4060 Laptop and Intel UHD._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/585:upstream-pr/585'
git switch 'upstream-pr/585'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/585'
git switch 'upstream-pr/585'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/585'
git switch 'upstream-pr/585'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/585'
git switch 'main'
git merge --squash 'upstream-pr/585'
git switch 'main'
git merge --ff-only 'upstream-pr/585'
git switch 'main'
git merge 'upstream-pr/585'
git push origin 'main'
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!585
No description provided.