Skip to content

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

Closed
KevinPratap wants to merge 1 commit into
Marvisak:mainfrom
KevinPratap:shader/med3-nan-nvidia
Closed

KevinPratap wants to merge 1 commit into
Marvisak:mainfrom
KevinPratap:shader/med3-nan-nvidia

Conversation

@KevinPratap

Copy link
Copy Markdown
Contributor

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

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 <noreply@anthropic.com>
@KevinPratap

Copy link
Copy Markdown
Contributor Author

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant