Shaders: v_med3 tests NaN from the bits, which NVIDIA's compiler keeps (TEST-MED3-NAN) #1689

Closed
github-bot wants to merge 1 commit from gh-pr/1689/head into gh-pr/1689/base
Owner

@⁠KevinPratap on GitHub · 2026-11-24 20:46 UTC · original
KevinPratap:shader/med3-nan-nvidia → main · +18 −4 · 1 files · 1 commits · closed without merging

Claimed in #1633 as TEST-MED3-NAN.

Problem

v_med3_f32_matches_rdna2_with_nan fails on main on NVIDIA (RTX 4050 Laptop):

hardware: v_med3_f32(1.0, 2.0, NaN) = 2 (0x40000000), want 1

When an operand is NaN, the hardware returns the minimum of the three. The lowering picks that case with float self-compares (fcmp uno). NVIDIA's shader compiler folds those away, so the median comes back instead. A comment on fract in the same file describes the same kind of folding on NVIDIA.

Fix

NaN is tested from the bits instead: (bits & 0x7FFFFFFF) > 0x7F800000, an integer compare that the driver keeps. med3 serves both v_med3_f32 and v_med3_f16, and the f16 operands are widened to f32 before it runs, so the one test covers both.

Verified

  • On an RTX 4050 Laptop (Windows 11):
    • v_med3_f32_matches_rdna2_with_nan and v_med3_f16_matches_rdna2_with_nan pass on the GPU and on WARP. Before the change, the f32 one failed.
    • The whole cargo test -p victory-shader passes: 120 passed, 0 failed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings is clean.
  • cargo fmt --check reports no changes in the edited lines. The remaining diffs in lower.rs are already on main.

Only crates/victory-shader/src/emit/lower.rs changes.

AI-assisted.

🤖 Generated with Claude Code

**@⁠KevinPratap** on GitHub · 2026-11-24 20:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/1689) `KevinPratap:shader/med3-nan-nvidia` → `main` · +18 −4 · 1 files · 1 commits · closed without merging Claimed in #1633 as `TEST-MED3-NAN`. ## Problem `v_med3_f32_matches_rdna2_with_nan` fails on `main` on NVIDIA (RTX 4050 Laptop): ``` hardware: v_med3_f32(1.0, 2.0, NaN) = 2 (0x40000000), want 1 ``` When an operand is NaN, the hardware returns the minimum of the three. The lowering picks that case with float self-compares (`fcmp uno`). NVIDIA's shader compiler folds those away, so the median comes back instead. A comment on `fract` in the same file describes the same kind of folding on NVIDIA. ## Fix NaN is tested from the bits instead: `(bits & 0x7FFFFFFF) > 0x7F800000`, an integer compare that the driver keeps. `med3` serves both `v_med3_f32` and `v_med3_f16`, and the f16 operands are widened to f32 before it runs, so the one test covers both. ## Verified - On an RTX 4050 Laptop (Windows 11): - `v_med3_f32_matches_rdna2_with_nan` and `v_med3_f16_matches_rdna2_with_nan` pass on the GPU and on WARP. Before the change, the f32 one failed. - The whole `cargo test -p victory-shader` passes: 120 passed, 0 failed. - `cargo clippy -p victory-shader --all-targets -- -D warnings` is clean. - `cargo fmt --check` reports no changes in the edited lines. The remaining diffs in `lower.rs` are already on `main`. Only `crates/victory-shader/src/emit/lower.rs` changes. AI-assisted. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
With a NaN operand the hardware gives the minimum of the three. The lowering chose that with float self-compares (fcmp uno); on an RTX 4050 Laptop the driver folded them away and gave the median: v_med3_f32(1.0, 2.0, NaN) = 2 where RDNA2 gives 1, failing v_med3_f32_matches_rdna2_with_nan on main. NaN is now tested as an integer compare of the magnitude bits (> 0x7F800000), for both the f32 and f16 forms (the f16 one is widened to f32 first).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:41:39 +00:00
github-bot closed this pull request 2026-11-26 04:41:39 +00:00
Author
Owner

@⁠KevinPratap on GitHub · 2026-11-24 21:18 UTC · original

Closing as a duplicate of #1483, which already tests NaN from the bits in med3, and in the v_cmp lowering too. It passes the same tests on an RTX 4050 Laptop (results on #1483).

**@⁠KevinPratap** on GitHub · 2026-11-24 21:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/1689#issuecomment-7144753163) Closing as a duplicate of #1483, which already tests NaN from the bits in `med3`, and in the `v_cmp` lowering too. It passes the same tests on an RTX 4050 Laptop (results on #1483).

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1689
No description provided.