perf: v_med3_f32 clamp between constants as max then min on every host (3-6x on a 4060 Laptop) #581

Open
KainanP wants to merge 4 commits from upstream-pr/581 into main
KainanP commented 2026-11-28 12:54:15 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-28T12:54:15Z.


Stacked on #570. Merge #570 first; this PR is then its last commit.

What

v_med3_f32 of a value between two constants (clamp(x, lo, hi), the console compiler's clamp idiom, v_med3_f32 v, v, 0, 1.0 for a saturate) now lowers on every host as the hardware's maximum then minimum, min(max(x, lo), hi). Until now that short form was taken only on AMD hosts (rdna::MED3); NVIDIA, Intel and WARP got the general median: 4 FMin/FMax, 3 NaN tests and a select.

The maximum and minimum it uses (max_min, also every v_max_f32, v_min_f32, v_max3_f32, v_min3_f32) get two constant forms. Both give exactly what the general form gives:

  • If one operand is a constant other than a zero (a NaN included), two zeros can never meet, so the result is the host's FMax/FMin alone.
  • A maximum with +0, or a minimum with -0, is the host's operation plus one bitwise and/or that gives a zero result that zero's sign. The result can never have the other sign.

So the median between constants keeps RDNA 2's rules: a NaN gives the lower constant, and -0 orders below +0. The general form did not keep the second rule on WARP: v_med3_f32(+0, -0, 1.0) gave -0 there, where RDNA 2 gives +0. The new test caught it.

Bench (RTX 4060 Laptop, synthetic programs only)

op_loop_bench runs 8 dependent chains of one console instruction (each followed by an add) for 256 rounds, in 8192 groups of 32 threads. op_loop_ideal_bench runs the same chains written in HLSL (dxc -O3). Before and after were built as two test executables and run interleaved, 3 rounds each. The table gives the median GPU time per dispatch:

Chain step Before After HLSL
v_med3_f32 x, 0, 1.0 + add 1.10-1.56 ms 0.377 ms 0.162 ms (saturate)
v_med3_f32 x, -1.0, 1.0 + add 1.20-2.18 ms 0.377 ms 0.309 ms (clamp)
v_max_f32 x, 0 + add 0.492 ms 0.377 ms 0.178 ms
v_max_f32 x, 0.5 + add 0.228 ms 0.227 ms 0.178 ms
v_min_f32 x, 1.0 + add 0.228 ms 0.228 ms 0.178 ms
v_med3_f32 x, a, b (registers) + add 1.3-2.3 ms unchanged 0.756 ms

That is 3-6× on a clamp between constants and 1.3× on a maximum with 0. The nonzero-constant max/min form gains nothing measurable on NVIDIA, because the driver already folds that fix-up, but it writes 48 fewer instructions per chain. The "before" spread is the 4060 Laptop's clock, which runs at two levels between rounds. "After" was stable.

Whether the title's slow skinning shaders use these is unknown. #570's census now counts c_minmax and c_minmax_k per shader hash, so a tester's census line will tell.

The same file holds the benches behind that decision:

  • skinning_bench: a skinning-shaped program (4 influences, current and previous bone matrices, 24 multiply-adds per influence, loop and unrolled). It runs as fast translated as in HLSL (0.210 vs 0.212 ms, ratio 0.99-1.00) and is checked against a CPU reference.
  • op_census: the module instructions each console instruction costs.

Tests

  • max_min_and_med3_with_constants_write_no_select: a max or min with 0.5, +0 or -2, a min with -0, and a median between 0 and 1.0 or -1.0 and 1.0 write no select. It fails before (op 0x110 [257, 240, 0] writes 1 selects) and passes after.
  • max_min_and_med3_with_a_constant_match_rdna2: 20 forms (max, min, max3, min3 and med3 with the constant in every operand place, including -0 through an SGPR) × 16 inputs (both zeros, both infinities, NaNs of both signs, the constants and their neighbours), compared bit for bit with RDNA 2's rules on the hardware adapter and WARP. It fails before on WARP (v_med3_f32 [128, 257, 242] of 0x80000000 = 0x80000000, want 0x00000000) and passes after. It also passes on the RTX 4060 Laptop and the Intel UHD (VICTORY_D3D12_ADAPTER=Intel).
  • The existing min/max/med3 tests (v_med3_f32_matches_rdna2_with_nan, max_and_min_order_negative_zero_below_positive_zero, the f16 and integer forms) pass on RTX, WARP and Intel UHD.
  • cargo test -p victory-shader --lib --release on the RTX 4060 Laptop: 371 passed, 0 failed, 24 ignored. Full pytest (WARP): 1454 passed, 529 skipped. cargo xtask policy: 381 files ok.

The translator output changes, so TRANSLATOR_VERSION goes to 21 (17-20 are taken by #456, #535, #489 and #571; I will bump it again if one of them lands first).

AI use: written, benched and tested by Claude Code (Claude Opus 5.5) for KainanP, on synthetic programs only (no game shader was used).

🤖 Generated with Claude Code

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5540321613 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/581). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-28T12:54:15Z. --- **Stacked on #570.** Merge #570 first; this PR is then its last commit. ## What `v_med3_f32` of a value between two constants (`clamp(x, lo, hi)`, the console compiler's clamp idiom, `v_med3_f32 v, v, 0, 1.0` for a saturate) now lowers on every host as the hardware's maximum then minimum, `min(max(x, lo), hi)`. Until now that short form was taken only on AMD hosts (`rdna::MED3`); NVIDIA, Intel and WARP got the general median: 4 FMin/FMax, 3 NaN tests and a select. The maximum and minimum it uses (`max_min`, also every `v_max_f32`, `v_min_f32`, `v_max3_f32`, `v_min3_f32`) get two constant forms. Both give exactly what the general form gives: - If one operand is a constant other than a zero (a NaN included), two zeros can never meet, so the result is the host's FMax/FMin alone. - A maximum with +0, or a minimum with -0, is the host's operation plus one bitwise `and`/`or` that gives a zero result that zero's sign. The result can never have the other sign. So the median between constants keeps RDNA 2's rules: a NaN gives the lower constant, and -0 orders below +0. The general form did not keep the second rule on WARP: `v_med3_f32(+0, -0, 1.0)` gave -0 there, where RDNA 2 gives +0. The new test caught it. ## Bench (RTX 4060 Laptop, synthetic programs only) `op_loop_bench` runs 8 dependent chains of one console instruction (each followed by an add) for 256 rounds, in 8192 groups of 32 threads. `op_loop_ideal_bench` runs the same chains written in HLSL (`dxc -O3`). Before and after were built as two test executables and run interleaved, 3 rounds each. The table gives the median GPU time per dispatch: | Chain step | Before | After | HLSL | |---|---|---|---| | `v_med3_f32 x, 0, 1.0` + add | 1.10-1.56 ms | 0.377 ms | 0.162 ms (`saturate`) | | `v_med3_f32 x, -1.0, 1.0` + add | 1.20-2.18 ms | 0.377 ms | 0.309 ms (`clamp`) | | `v_max_f32 x, 0` + add | 0.492 ms | 0.377 ms | 0.178 ms | | `v_max_f32 x, 0.5` + add | 0.228 ms | 0.227 ms | 0.178 ms | | `v_min_f32 x, 1.0` + add | 0.228 ms | 0.228 ms | 0.178 ms | | `v_med3_f32 x, a, b` (registers) + add | 1.3-2.3 ms | unchanged | 0.756 ms | That is 3-6× on a clamp between constants and 1.3× on a maximum with 0. The nonzero-constant max/min form gains nothing measurable on NVIDIA, because the driver already folds that fix-up, but it writes 48 fewer instructions per chain. The "before" spread is the 4060 Laptop's clock, which runs at two levels between rounds. "After" was stable. Whether the title's slow skinning shaders use these is unknown. #570's census now counts `c_minmax` and `c_minmax_k` per shader hash, so a tester's census line will tell. The same file holds the benches behind that decision: - `skinning_bench`: a skinning-shaped program (4 influences, current and previous bone matrices, 24 multiply-adds per influence, loop and unrolled). It runs **as fast translated as in HLSL** (0.210 vs 0.212 ms, ratio 0.99-1.00) and is checked against a CPU reference. - `op_census`: the module instructions each console instruction costs. ## Tests - `max_min_and_med3_with_constants_write_no_select`: a max or min with 0.5, +0 or -2, a min with -0, and a median between 0 and 1.0 or -1.0 and 1.0 write no select. It **fails before** (`op 0x110 [257, 240, 0] writes 1 selects`) and passes after. - `max_min_and_med3_with_a_constant_match_rdna2`: 20 forms (max, min, max3, min3 and med3 with the constant in every operand place, including -0 through an SGPR) × 16 inputs (both zeros, both infinities, NaNs of both signs, the constants and their neighbours), compared bit for bit with RDNA 2's rules on the hardware adapter and WARP. It **fails before on WARP** (`v_med3_f32 [128, 257, 242] of 0x80000000 = 0x80000000, want 0x00000000`) and passes after. It also passes on the RTX 4060 Laptop and the Intel UHD (`VICTORY_D3D12_ADAPTER=Intel`). - The existing min/max/med3 tests (`v_med3_f32_matches_rdna2_with_nan`, `max_and_min_order_negative_zero_below_positive_zero`, the f16 and integer forms) pass on RTX, WARP and Intel UHD. - `cargo test -p victory-shader --lib --release` on the RTX 4060 Laptop: 371 passed, 0 failed, 24 ignored. Full pytest (WARP): 1454 passed, 529 skipped. `cargo xtask policy`: 381 files ok. The translator output changes, so `TRANSLATOR_VERSION` goes to 21 (17-20 are taken by #456, #535, #489 and #571; I will bump it again if one of them lands first). _AI use: written, benched and tested by Claude Code (Claude Opus 5.5) for KainanP, on synthetic programs only (no game shader was used)._ 🤖 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/581:upstream-pr/581'
git switch 'upstream-pr/581'

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