Shaders: invalid f32 sqrt, rsq and log inputs return negative quiet NaN #3612

Open
Moon0755 wants to merge 1 commit from upstream-pr/3612 into main
Moon0755 commented 2026-11-27 21:09:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Moon0755 on 2026-11-27T21:09:56Z.


On RDNA2 (gfx1030, measured by the Leonida project), v_sqrt_f32, v_rsq_f32, and v_log_f32 return the negative quiet NaN 0xffc00000 for any invalid negative input, while -0.0 remains valid (-0.0 for v_sqrt_f32, and -inf for v_rsq_f32 and v_log_f32). Without a guard, D3D12 hosts return 0x7fffffff on NVIDIA across all three instructions and 0xffffffff on WARP for v_log_f32.

Changes

  • crates/victory-shader/src/emit/lower.rs: in the v_rsq_f32 / v_sqrt_f32 / v_log_f32 lowering arm (matches!(op, 23..=25)), select 0xFFC0_0000 when pred::OLT(a, 0.0) holds, leaving -0.0, positive inputs, and NaNs untouched (lower.rs size: 521,221 B < 524,288 B).
  • crates/victory-shader/src/emit/valu_tests.rs: add transcendental_invalid_results_are_negative_qnan covering negative inputs (-1.0, -4.0, -1000.0, -inf) and valid -0.0 control cases for v_sqrt_f32, v_rsq_f32, and v_log_f32.

Verification

  • Local 3-host D3D12 verification (AMD Radeon(TM) Graphics, NVIDIA GeForce RTX 2060 with Max-Q Design, WARP):
    • Before this fix on main (63bcca4): NVIDIA returned 0x7fffffff for all three operations (v_sqrt_f32, v_rsq_f32, v_log_f32), and WARP returned 0xffffffff for v_log_f32.
    • After this fix (aa63053): cargo test --locked -p victory-shader --lib --features host -- transcendental_invalid_results_are_negative_qnan --nocapture passed on AMD Radeon(TM) Graphics, NVIDIA GeForce RTX 2060 with Max-Q Design, and WARP (1 passed; 0 failed).
  • cargo xtask policy: 191 files ok.
  • cargo clippy: both cargo clippy --locked -p victory-shader --lib --tests -- -D warnings and --all-features -- -D warnings passed cleanly.
  • Fork CI positive (gemini/ci-transcendental-invalid-qnan): https://github.com/Moon0755/VIctory/actions/runs/46067684463 (SUCCESS; victory-shader 303 passed / 0 failed / 3 ignored, victory-gpu 231 passed / 0 failed, xtask 6/6, both Clippy checks clean).
  • Fork CI negative (gemini/ci-transcendental-invalid-qnan-negative, test only on 63bcca4): https://github.com/Moon0755/VIctory/actions/runs/46067684675 (FAILURE; victory-shader 302 passed / 1 failed exclusively on emit::valu_tests::transcendental_invalid_results_are_negative_qnan: warp: v_log_f32(0xbf800000) = 0xffffffff, want 0xffc00000).

— Devin & Neon

<!-- greyfinch-sync:Marvisak/VIctory:issue:5533137709 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3612). Originally posted by [Moon0755](https://github.com/Moon0755) on 2026-11-27T21:09:56Z. --- On RDNA2 (`gfx1030`, measured by the Leonida project), `v_sqrt_f32`, `v_rsq_f32`, and `v_log_f32` return the negative quiet NaN `0xffc00000` for any invalid negative input, while `-0.0` remains valid (`-0.0` for `v_sqrt_f32`, and `-inf` for `v_rsq_f32` and `v_log_f32`). Without a guard, D3D12 hosts return `0x7fffffff` on NVIDIA across all three instructions and `0xffffffff` on WARP for `v_log_f32`. ### Changes - **`crates/victory-shader/src/emit/lower.rs`**: in the `v_rsq_f32` / `v_sqrt_f32` / `v_log_f32` lowering arm (`matches!(op, 23..=25)`), select `0xFFC0_0000` when `pred::OLT(a, 0.0)` holds, leaving `-0.0`, positive inputs, and NaNs untouched (`lower.rs` size: `521,221 B` < `524,288 B`). - **`crates/victory-shader/src/emit/valu_tests.rs`**: add `transcendental_invalid_results_are_negative_qnan` covering negative inputs (`-1.0`, `-4.0`, `-1000.0`, `-inf`) and valid `-0.0` control cases for `v_sqrt_f32`, `v_rsq_f32`, and `v_log_f32`. ### Verification - **Local 3-host D3D12 verification** (`AMD Radeon(TM) Graphics`, `NVIDIA GeForce RTX 2060 with Max-Q Design`, `WARP`): - Before this fix on `main` (`63bcca4`): NVIDIA returned `0x7fffffff` for all three operations (`v_sqrt_f32`, `v_rsq_f32`, `v_log_f32`), and WARP returned `0xffffffff` for `v_log_f32`. - After this fix (`aa63053`): `cargo test --locked -p victory-shader --lib --features host -- transcendental_invalid_results_are_negative_qnan --nocapture` passed on **AMD Radeon(TM) Graphics**, **NVIDIA GeForce RTX 2060 with Max-Q Design**, and **WARP** (1 passed; 0 failed). - **`cargo xtask policy`**: `191 files ok`. - **`cargo clippy`**: both `cargo clippy --locked -p victory-shader --lib --tests -- -D warnings` and `--all-features -- -D warnings` passed cleanly. - **Fork CI positive (`gemini/ci-transcendental-invalid-qnan`)**: https://github.com/Moon0755/VIctory/actions/runs/46067684463 (`SUCCESS`; `victory-shader` 303 passed / 0 failed / 3 ignored, `victory-gpu` 231 passed / 0 failed, `xtask` 6/6, both Clippy checks clean). - **Fork CI negative (`gemini/ci-transcendental-invalid-qnan-negative`, test only on `63bcca4`)**: https://github.com/Moon0755/VIctory/actions/runs/46067684675 (`FAILURE`; `victory-shader` 302 passed / 1 failed exclusively on `emit::valu_tests::transcendental_invalid_results_are_negative_qnan`: `warp: v_log_f32(0xbf800000) = 0xffffffff, want 0xffc00000`). — Devin & Neon
oni-swr commented 2026-11-27 22:03:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-27T22:03:46Z.


Independent verification of aa6305357c3b90c6d64309c6c94bab3895df0547 against main at 63bcca43c6a00a758510398fa43096ce7e4f7df0. Clean merge. Native Windows x64, D3D12 WARP only, with both WARP switches set and SDK dxil.dll. Tests (passed / failed / ignored, unit and integration combined) were 301 / 4 / 4 on base and 302 / 4 / 4 on head. Both runs had the same four WARP failures (three wave-32 pipeline checks and one texture gather check). The backported regression returned 0xffffffff for a negative logarithm input on base and passed on head. Signed-zero cases passed, and 30 sampled outputs for inputs the guard leaves unchanged matched bit for bit. Clippy passed with all targets, all features, and -D warnings. Review found no blocking defects. Asset policy passed, and lower.rs was 521,221 bytes on head and after merging with main, 3,067 bytes below the 524,288-byte limit.

AI-assisted (Codex, gpt-6.2-sol).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7228993924 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3612#issuecomment-7228993924). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-27T22:03:46Z. --- Independent verification of `aa6305357c3b90c6d64309c6c94bab3895df0547` against `main` at `63bcca43c6a00a758510398fa43096ce7e4f7df0`. Clean merge. Native Windows x64, D3D12 WARP only, with both WARP switches set and SDK `dxil.dll`. Tests (passed / failed / ignored, unit and integration combined) were **301 / 4 / 4** on base and **302 / 4 / 4** on head. Both runs had the same four WARP failures (three wave-32 pipeline checks and one texture gather check). The backported regression returned `0xffffffff` for a negative logarithm input on base and passed on head. Signed-zero cases passed, and 30 sampled outputs for inputs the guard leaves unchanged matched bit for bit. Clippy passed with all targets, all features, and `-D warnings`. Review found no blocking defects. Asset policy passed, and `lower.rs` was **521,221 bytes** on head and after merging with `main`, **3,067 bytes** below the 524,288-byte limit. AI-assisted (Codex, gpt-6.2-sol).
ColorMeBoom commented 2026-11-27 23:48:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ColorMeBoom on 2026-11-27T23:48:52Z.


Hardware run on AMD, since the report so far is WARP only: RX 5700 XT (Navi 10, RDNA1, driver 32.0.21045.29003, Windows 11 26200), head aa63053 on 63bcca4. The test fixture checks the hardware adapter and WARP in turn, and neither was skipped.

  • transcendental_invalid_results_are_negative_qnan passes on both.
  • Full victory-shader --lib --features host: 303 passed, 0 failed, 3 ignored.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7230428246 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3612#issuecomment-7230428246). Originally posted by [ColorMeBoom](https://github.com/ColorMeBoom) on 2026-11-27T23:48:52Z. --- Hardware run on AMD, since the report so far is WARP only: RX 5700 XT (Navi 10, RDNA1, driver 32.0.21045.29003, Windows 11 26200), head `aa63053` on `63bcca4`. The test fixture checks the hardware adapter and WARP in turn, and neither was skipped. - `transcendental_invalid_results_are_negative_qnan` passes on both. - Full `victory-shader --lib --features host`: 303 passed, 0 failed, 3 ignored. AI use: Claude Code (Claude Opus 5.5).
kilolimaVI commented 2026-11-28 16:00:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kilolimaVI on 2026-11-28T16:00:05Z.


AMD Radeon 780M (RDNA3) + WARP, Windows 11, head aa63053.

  • Before (main 63bcca4 + this PR's test): the hardware adapter already returns the console's results for every case (0xffc00000 for negative v_sqrt_f32, v_rsq_f32 and v_log_f32 inputs, and the -0.0 cases). The test fails only on WARP: v_log_f32(0xbf800000) = 0xffffffff.
  • After: passes on both, 5 of 5 runs.
  • Full suite (victory-shader + victory-gpu --features diagnostics): 552 passed.

So RDNA3 matches the RDNA2 measurement natively; the change matters for WARP and (per the description) NVIDIA.

AI-assisted (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7241896884 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3612#issuecomment-7241896884). Originally posted by [kilolimaVI](https://github.com/kilolimaVI) on 2026-11-28T16:00:05Z. --- AMD Radeon 780M (RDNA3) + WARP, Windows 11, head `aa63053`. - **Before** (main `63bcca4` + this PR's test): the hardware adapter already returns the console's results for every case (`0xffc00000` for negative `v_sqrt_f32`, `v_rsq_f32` and `v_log_f32` inputs, and the -0.0 cases). The test fails only on WARP: `v_log_f32(0xbf800000)` = `0xffffffff`. - **After:** passes on both, 5 of 5 runs. - Full suite (`victory-shader` + `victory-gpu --features diagnostics`): 552 passed. So RDNA3 matches the RDNA2 measurement natively; the change matters for WARP and (per the description) NVIDIA. AI-assisted (Claude).
upipibi commented 2026-11-28 17:37:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-28T17:37:17Z.


NVIDIA row for this PR: it passes on Ampere, and its test fails on this card without the fix. On main, the RTX 3070 returns 0x7fffffff for every negative input of all three instructions, as the description says of the RTX 2060.

RTX 3070 (desktop), driver 616.56, Windows 11 Pro 10.0.26100, debug. Head aa63053 on main 63bcca4 (one commit, clean).

  • cargo test --locked -p victory-shader --lib -- transcendental_invalid_results_are_negative_qnan --nocapture: 1 passed, 0 failed, 0 ignored, in 3 of 3 runs. In each run the test checks this card and then WARP. Three more runs with VICTORY_WARP=1, where every device is WARP, also passed.

  • Negative control: main's emit/lower.rs, with the test kept in valu_tests.rs. git diff --stat HEAD: 1 file, 12 deletions. git diff --stat 63bcca4: only valu_tests.rs (46 insertions). Result: 0 passed, 1 failed, in 3 of 3 runs on hardware and 3 of 3 with VICTORY_WARP=1:

    • hardware runs: hardware: v_sqrt_f32(0xbf800000) = 0x7fffffff, want 0xffc00000 (left: 2147483647, right: 4290772992). That is the first case checked, so the assertion stops there.
    • VICTORY_WARP=1 runs: hardware: v_log_f32(0xbf800000) = 0xffffffff, want 0xffc00000 (left: 4294967295). The label is hardware, but that run is WARP too. WARP passes the v_sqrt_f32 and v_rsq_f32 cases before that. This is the description's fork CI control value.
  • The test stops at its first wrong case, so I ran it 3 more times with the PR's code and 3 times with main's lower.rs, with its assert_eq! replaced by an eprintln! of every case. Then I restored the file. All three runs of each set printed the same values:

    case 3070, main WARP, main both, PR
    v_sqrt_f32(-1.0), (-4.0) 0x7fffffff 0xffc00000 0xffc00000
    v_sqrt_f32(-0.0) 0x80000000 0x80000000 0x80000000
    v_rsq_f32(-1.0) 0x7fffffff 0xffc00000 0xffc00000
    v_rsq_f32(-0.0) 0xff800000 0xff800000 0xff800000
    v_log_f32(-1.0), (-1000.0), (-inf) 0x7fffffff 0xffffffff 0xffc00000
    v_log_f32(-0.0) 0xff800000 0xff800000 0xff800000

    So the 3070 gives 0x7fffffff for all 6 invalid inputs, and WARP's only wrong results are the 3 v_log_f32 ones. The -0.0 cases match RDNA2 on both with and without the PR. That is what the description reports for the RTX 2060 and WARP. The 780M passes every case on main, per kilolimaVI.

  • cargo test --locked -p victory-shader --lib -- --skip heap_direct_texture_reads_mark_their_heap_index:

    • this PR: 299 passed, 3 failed, 3 ignored;
    • main 63bcca4: 298 passed, 3 failed, 3 ignored (earlier run on this card).

    The PR adds one test. The 3 failures are the same on both: stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros and synthetic_tests::gathers_at_a_level_match_the_hardware. They come from the structured-view raw read on NVIDIA (#2178, #2323, issue #1835). The skipped test crashes the binary on this card (open issue #2571).

  • The PR does not touch victory-gpu, so I did not run its lib suite.

NVIDIA-specific: Ampere, like Turing in the description, returns the positive NaN 0x7fffffff for an invalid sqrt, rsqrt or log2, where RDNA2 returns 0xffc00000. So on this card the guard changes the result of all three instructions. On WARP it changes only v_log_f32.

Not tested:

  • inputs the test leaves out: negative denormals, NaN inputs (the guard's OLT leaves them alone, but I did not check what this card returns for them), other negative values;
  • the other transcendentals (v_exp_f32, v_rcp_f32, the f16 and f64 forms);
  • the merges with #3597, #3602, #3587 or #2997 (lower.rs size);
  • clippy, a title run.

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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7243157215 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3612#issuecomment-7243157215). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-28T17:37:17Z. --- NVIDIA row for this PR: it passes on Ampere, and its test fails on this card without the fix. On main, the RTX 3070 returns `0x7fffffff` for every negative input of all three instructions, as the description says of the RTX 2060. RTX 3070 (desktop), driver 616.56, Windows 11 Pro 10.0.26100, debug. Head `aa63053` on main `63bcca4` (one commit, clean). - `cargo test --locked -p victory-shader --lib -- transcendental_invalid_results_are_negative_qnan --nocapture`: 1 passed, 0 failed, 0 ignored, in 3 of 3 runs. In each run the test checks this card and then WARP. Three more runs with `VICTORY_WARP=1`, where every device is WARP, also passed. - Negative control: main's `emit/lower.rs`, with the test kept in `valu_tests.rs`. `git diff --stat HEAD`: 1 file, 12 deletions. `git diff --stat 63bcca4`: only `valu_tests.rs` (46 insertions). Result: 0 passed, 1 failed, in 3 of 3 runs on hardware and 3 of 3 with `VICTORY_WARP=1`: - hardware runs: `hardware: v_sqrt_f32(0xbf800000) = 0x7fffffff, want 0xffc00000` (`left: 2147483647`, `right: 4290772992`). That is the first case checked, so the assertion stops there. - `VICTORY_WARP=1` runs: `hardware: v_log_f32(0xbf800000) = 0xffffffff, want 0xffc00000` (`left: 4294967295`). The label is `hardware`, but that run is WARP too. WARP passes the `v_sqrt_f32` and `v_rsq_f32` cases before that. This is the description's fork CI control value. - The test stops at its first wrong case, so I ran it 3 more times with the PR's code and 3 times with main's `lower.rs`, with its `assert_eq!` replaced by an `eprintln!` of every case. Then I restored the file. All three runs of each set printed the same values: | case | 3070, main | WARP, main | both, PR | |---|---|---|---| | `v_sqrt_f32(-1.0)`, `(-4.0)` | `0x7fffffff` | `0xffc00000` | `0xffc00000` | | `v_sqrt_f32(-0.0)` | `0x80000000` | `0x80000000` | `0x80000000` | | `v_rsq_f32(-1.0)` | `0x7fffffff` | `0xffc00000` | `0xffc00000` | | `v_rsq_f32(-0.0)` | `0xff800000` | `0xff800000` | `0xff800000` | | `v_log_f32(-1.0)`, `(-1000.0)`, `(-inf)` | `0x7fffffff` | `0xffffffff` | `0xffc00000` | | `v_log_f32(-0.0)` | `0xff800000` | `0xff800000` | `0xff800000` | So the 3070 gives `0x7fffffff` for all 6 invalid inputs, and WARP's only wrong results are the 3 `v_log_f32` ones. The `-0.0` cases match RDNA2 on both with and without the PR. That is what the description reports for the RTX 2060 and WARP. The 780M passes every case on main, per kilolimaVI. - `cargo test --locked -p victory-shader --lib -- --skip heap_direct_texture_reads_mark_their_heap_index`: - this PR: 299 passed, 3 failed, 3 ignored; - main `63bcca4`: 298 passed, 3 failed, 3 ignored (earlier run on this card). The PR adds one test. The 3 failures are the same on both: `stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride`, `stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros` and `synthetic_tests::gathers_at_a_level_match_the_hardware`. They come from the structured-view raw read on NVIDIA (#2178, #2323, issue #1835). The skipped test crashes the binary on this card (open issue #2571). - The PR does not touch `victory-gpu`, so I did not run its lib suite. NVIDIA-specific: Ampere, like Turing in the description, returns the positive NaN `0x7fffffff` for an invalid `sqrt`, `rsqrt` or `log2`, where RDNA2 returns `0xffc00000`. So on this card the guard changes the result of all three instructions. On WARP it changes only `v_log_f32`. Not tested: - inputs the test leaves out: negative denormals, NaN inputs (the guard's `OLT` leaves them alone, but I did not check what this card returns for them), other negative values; - the other transcendentals (`v_exp_f32`, `v_rcp_f32`, the f16 and f64 forms); - the merges with #3597, #3602, #3587 or #2997 (`lower.rs` size); - clippy, a title run. AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report.
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/3612:upstream-pr/3612'
git switch 'upstream-pr/3612'

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