Shaders: the neg input modifier flips a NaN's sign too #2821

Open
xKineticK wants to merge 1 commit from upstream-pr/2823 into main
xKineticK commented 2026-11-25 21:31:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by xKineticK on 2026-11-25T21:31:52Z.


The neg source modifier was lowered as -0.0 - x. IEEE 754 leaves the sign of a NaN produced by a subtraction to the host, and WARP keeps it, so a negated NaN reached the instruction with its original sign. On the console the modifier flips bit 31 alone, NaN or not (RDNA2 ISA).

The floor conversions make it visible, because they saturate a NaN by its sign:

v_cvt_rpi_i32_f32 v5, -v1, v1 = 0x7fc00000 Result
Model (to_i32_floor of -NaN) 0x80000000
AMD Radeon RX 7900 XTX (RDNA3), driver 32.0.32029.2010 0x80000000
WARP 0x7fffffff

How it was found

#1614's VALU fuzzer, merged onto main (13f91ca), with VICTORY_FUZZ_MODS=1 and 200 inputs per opcode on the RX 7900 XTX against WARP. Only two results were new beyond the 17 known disagreements, and neither appears without modifiers:

  • v_cvt_rpi_i32_f32, neg src0: 20 of 200 inputs differ;
  • v_cvt_flr_i32_f32, neg src0: 9 of 200 inputs differ.

All of the differing inputs are NaNs. This is separate from the Intel UHD finding reported on #1614 (NaN gives 0 there, without modifiers).

Change

  • Lower::fneg negates by XOR-ing the sign bit on the integer bits, which gives the same result on every host.
  • It is used wherever the neg input modifier was a subtraction:
    • f32 sources (fsrc);
    • v_fma_mix* sources;
    • f16 sources read as f32;
    • packed f16 sources.
  • The 16-bit and select paths already flipped the bit and are unchanged.
  • The cube-coordinate negations are internal arithmetic, not the modifier, and are unchanged.

Related, no overlap: #2548 makes v_mov_b32's modifiers bitwise on the integer path.

Test

semantics_tests::source_modifiers_set_the_sign_of_a_nan runs both floor conversions with abs, neg and -|x| on F32_EDGES. It compares against the existing to_i32_floor model on the hardware adapter and on WARP.

  • Without the change, it fails on WARP only: 3 NaN inputs per negated variant. The RX 7900 XTX already matches the model.
  • With the change, it passes on both.

Checks

Windows 11, AMD Radeon RX 7900 XTX (driver 32.0.32029.2010), and WARP:

  • cargo test --workspace --locked: 780 passed, 0 failed.
  • Both CI clippy runs (--all-targets, with and without --all-features, -D warnings): clean.
  • cargo xtask policy: ok.
  • cargo fmt --check reports nothing in the two changed files.
  • #1614's fuzzer with the fix applied, using VICTORY_FUZZ_MODS=1, 400 inputs, on v_cvt_{flr,rpi}_i32_f32 and v_cvt_{i32,u32}_f32: 0 differ.

Not tested: NVIDIA and Intel adapters. I also did not rerun the full 23-minute fuzzer with the fix. Per the floor-conversion test's own note, the title uses neither conversion, so the gain is hosts agreeing, including CI's WARP, rather than a visible change in the game.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:5499520297 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2823). Originally posted by [xKineticK](https://github.com/xKineticK) on 2026-11-25T21:31:52Z. --- The `neg` source modifier was lowered as `-0.0 - x`. IEEE 754 leaves the sign of a NaN produced by a subtraction to the host, and **WARP keeps it**, so a negated NaN reached the instruction with its original sign. On the console the modifier flips bit 31 alone, NaN or not (RDNA2 ISA). The floor conversions make it visible, because they saturate a NaN by its sign: | `v_cvt_rpi_i32_f32 v5, -v1`, v1 = `0x7fc00000` | Result | |---|---| | Model (`to_i32_floor` of `-NaN`) | `0x80000000` | | AMD Radeon RX 7900 XTX (RDNA3), driver 32.0.32029.2010 | `0x80000000` | | WARP | `0x7fffffff` | ## How it was found #1614's VALU fuzzer, merged onto `main` (`13f91ca`), with `VICTORY_FUZZ_MODS=1` and 200 inputs per opcode on the RX 7900 XTX against WARP. Only two results were new beyond the 17 known disagreements, and neither appears without modifiers: - `v_cvt_rpi_i32_f32`, neg src0: 20 of 200 inputs differ; - `v_cvt_flr_i32_f32`, neg src0: 9 of 200 inputs differ. All of the differing inputs are NaNs. This is separate from the Intel UHD finding reported on #1614 (NaN gives 0 there, without modifiers). ## Change - `Lower::fneg` negates by XOR-ing the sign bit on the integer bits, which gives the same result on every host. - It is used wherever the `neg` input modifier was a subtraction: - f32 sources (`fsrc`); - `v_fma_mix*` sources; - f16 sources read as f32; - packed f16 sources. - The 16-bit and select paths already flipped the bit and are unchanged. - The cube-coordinate negations are internal arithmetic, not the modifier, and are unchanged. Related, no overlap: #2548 makes `v_mov_b32`'s modifiers bitwise on the integer path. ## Test `semantics_tests::source_modifiers_set_the_sign_of_a_nan` runs both floor conversions with `abs`, `neg` and `-|x|` on `F32_EDGES`. It compares against the existing `to_i32_floor` model on the hardware adapter and on WARP. - Without the change, it fails on WARP only: 3 NaN inputs per negated variant. The RX 7900 XTX already matches the model. - With the change, it passes on both. ## Checks Windows 11, AMD Radeon RX 7900 XTX (driver 32.0.32029.2010), and WARP: - `cargo test --workspace --locked`: 780 passed, 0 failed. - Both CI clippy runs (`--all-targets`, with and without `--all-features`, `-D warnings`): clean. - `cargo xtask policy`: ok. - `cargo fmt --check` reports nothing in the two changed files. - #1614's fuzzer with the fix applied, using `VICTORY_FUZZ_MODS=1`, 400 inputs, on `v_cvt_{flr,rpi}_i32_f32` and `v_cvt_{i32,u32}_f32`: 0 differ. Not tested: NVIDIA and Intel adapters. I also did not rerun the full 23-minute fuzzer with the fix. Per the floor-conversion test's own note, the title uses neither conversion, so the gain is hosts agreeing, including CI's WARP, rather than a visible change in the game. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
kotekompotek commented 2026-11-25 21:51:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T21:51:37Z.

Review: COMMENTED


Correct: 0.0 - x goes through the host's subtraction semantics which don't guarantee the NaN sign flip the NEG input modifier performs on hardware — bit-level xor of 0x80000000 is the right lowering (matches the ISA's sign-bit flip for any input incl. NaN/-0). All four neg sites converted consistently. LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6065472689 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2823#pullrequestreview-6065472689). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T21:51:37Z. **Review:** COMMENTED --- Correct: 0.0 - x goes through the host's subtraction semantics which don't guarantee the NaN sign flip the NEG input modifier performs on hardware — bit-level xor of 0x80000000 is the right lowering (matches the ISA's sign-bit flip for any input incl. NaN/-0). All four neg sites converted consistently. LGTM.
cloph-dsp commented 2026-11-25 22:04:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-25T21:37:11Z.


Local verification on Windows / rustc 1.100.0 / MSVC, synthetic inputs only, no game data; GPU tests allowed here (hardware + WARP), no clippy/rustup.

  • cargo test --locked -p victory-shader --lib source_modifiers_set_the_sign_of_a_nan --quiet -> 1 passed, 0 failed (273 filtered out), with CARGO_BUILD_JOBS=2. The test covers both the hardware adapter and WARP, abs/neg/-|x| on F32_EDGES and both floor conversions.
  • git diff --check origin/main..HEAD clean; diff is 2 files +43/-8 (emit/lower.rs, emit/semantics_tests.rs).
  • Negative control: source_modifiers_set_the_sign_of_a_nan is absent from origin/main, so the test is new here. Lower::fneg flips the sign bit on integer bits, replacing the subtraction that left a NaN sign host-dependent.
  • No formal review on this PR at time of verification.

AI-assisted (Hy4 flash).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7175319010 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2823#issuecomment-7175319010). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-25T21:37:11Z. --- Local verification on Windows / rustc 1.100.0 / MSVC, synthetic inputs only, no game data; GPU tests allowed here (hardware + WARP), no clippy/rustup. - `cargo test --locked -p victory-shader --lib source_modifiers_set_the_sign_of_a_nan --quiet` -> 1 passed, 0 failed (273 filtered out), with CARGO_BUILD_JOBS=2. The test covers both the hardware adapter and WARP, `abs`/`neg`/`-|x|` on F32_EDGES and both floor conversions. - `git diff --check origin/main..HEAD` clean; diff is 2 files +43/-8 (`emit/lower.rs`, `emit/semantics_tests.rs`). - Negative control: `source_modifiers_set_the_sign_of_a_nan` is absent from origin/main, so the test is new here. `Lower::fneg` flips the sign bit on integer bits, replacing the subtraction that left a NaN sign host-dependent. - No formal review on this PR at time of verification. AI-assisted (Hy4 flash).
fuzeofficlal commented 2026-11-25 22:04:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T21:48:30Z.


Verified 74b15542 on Windows 11 (RTX 5090 + WARP + Windows SDK 10.0.26100.0 dxil.dll):

  • cargo xtask policy: 184 files ok; cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --features host): 0 warnings.
  • RTX 5090 + WARP + dxil.dll: source_modifiers_set_the_sign_of_a_nan 1/1 passed (0 hardware/WARP diff), floor_conversions_match_the_hardware 1/1 passed, emit::lower::tests 27/27 passed.
  • Lower::fneg covers all 4 float input neg modifier sites (fsrc, fma_mix, half_fsrc, packed16) with abs applied before fneg.
  • Clean git merge-tree against origin/main, #1327, and #2081 (1-line adjacent context overlap with #2548 at lower.rs:2075-2085, zero semantic conflict).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7175564781 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2823#issuecomment-7175564781). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T21:48:30Z. --- Verified `74b15542` on Windows 11 (RTX 5090 + WARP + Windows SDK `10.0.26100.0` `dxil.dll`): - `cargo xtask policy`: 184 files ok; `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default & `--features host`): 0 warnings. - RTX 5090 + WARP + `dxil.dll`: `source_modifiers_set_the_sign_of_a_nan` 1/1 passed (0 hardware/WARP diff), `floor_conversions_match_the_hardware` 1/1 passed, `emit::lower::tests` 27/27 passed. - `Lower::fneg` covers all 4 float input `neg` modifier sites (`fsrc`, `fma_mix`, `half_fsrc`, `packed16`) with `abs` applied before `fneg`. - Clean `git merge-tree` against `origin/main`, #1327, and #2081 (1-line adjacent context overlap with #2548 at `lower.rs:2075-2085`, zero semantic conflict).
Dukestar69 commented 2026-11-26 02:28:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T02:28:56Z.

Review: APPROVED


Tested merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, dxil.dll validator active):

  • cargo test -p victory-shader --lib emit::semantics_tests::source_modifiers_set_the_sign_of_a_nan: 1 passed, 0 failed (0 mismatches on both RTX 5090 hardware and WARP, 0 hardware/WARP differences).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6067761090 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2823#pullrequestreview-6067761090). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T02:28:56Z. **Review:** APPROVED --- Tested merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, `dxil.dll` validator active): - `cargo test -p victory-shader --lib emit::semantics_tests::source_modifiers_set_the_sign_of_a_nan`: **1 passed, 0 failed** (0 mismatches on both RTX 5090 hardware and WARP, 0 hardware/WARP differences). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Marvisak commented 2026-11-26 03:05:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T02:22:12Z.


Automated triage for the maintainers.

The neg source modifier was lowered as -0.0 - x, which leaves a NaN's sign to the host. Lower::fneg now XORs bit 31 on the integer bits, as RDNA2's modifier does, at the f32, v_fma_mix, f16-as-f32 and packed sources. The evidence is a fuzzer disagreement on v_cvt_rpi/flr_i32_f32 reproduced on an RX 7900 XTX against WARP, and source_modifiers_set_the_sign_of_a_nan models it. The XOR also avoids any flushing a subtraction could do to a denormal input, which matches a pure sign modifier.

CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7180739596 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2823#issuecomment-7180739596). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T02:22:12Z. --- <!-- victory-triage v1 head=74b155425dea8cdb55eb1766d62ebc87f6eda6d2 verdict=merge-candidate at=2026-11-26T02:22:06Z --> Automated triage for the maintainers. The `neg` source modifier was lowered as `-0.0 - x`, which leaves a NaN's sign to the host. `Lower::fneg` now XORs bit 31 on the integer bits, as RDNA2's modifier does, at the f32, `v_fma_mix`, f16-as-f32 and packed sources. The evidence is a fuzzer disagreement on `v_cvt_rpi/flr_i32_f32` reproduced on an RX 7900 XTX against WARP, and `source_modifiers_set_the_sign_of_a_nan` models it. The XOR also avoids any flushing a subtraction could do to a denormal input, which matches a pure sign modifier. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
andrewpozdnakov7-jpg commented 2026-11-26 05:04:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T04:24:04Z.


I checked whether this fix also covers v_cvt_f32_f16 when the widened result is stored as raw bits. The same synthetic test checks all 2,046 NaN encodings and 14 finite, zero and infinity controls in each of six instruction forms. It uses the project's IEEE-off model and the published RDNA2 widening measurements.

  • main 63bcca43 gives 2,046 wrong results in each of NEG and high-half ABS+NEG. For example, NEG(0x7c01) gives 0x7fc02000, expected 0xff802000. The four other forms pass.
  • Your exact 74b15542 combined with 63bcca43 passes all 12,360 cases per test slot. Your original floor-conversion test passes too. Both slots used app-local D3D12 WARP (SM 6.6) with DXIL validation on this host.
  • With the extra test, semantics 36 passed and 1 ignored, VALU 35 passed, lowerer 33 passed. Strict shader Clippy passed with default and all features, and asset policy passed.

The optional test-only commit df1d930 adds that widening coverage. Its parent is a clean merge of your exact commit and current main. Please keep the Git author if you cherry-pick or rebase it. If you fold the test into a commit, please add Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7182561614 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2823#issuecomment-7182561614). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T04:24:04Z. --- I checked whether this fix also covers `v_cvt_f32_f16` when the widened result is stored as raw bits. The same synthetic test checks all 2,046 NaN encodings and 14 finite, zero and infinity controls in each of six instruction forms. It uses the project's IEEE-off model and the published [RDNA2 widening measurements](https://github.com/boykopovar/AnyPS5/pull/22662). - `main` `63bcca43` gives 2,046 wrong results in each of `NEG` and high-half `ABS+NEG`. For example, `NEG(0x7c01)` gives `0x7fc02000`, expected `0xff802000`. The four other forms pass. - Your exact `74b15542` combined with `63bcca43` passes all 12,360 cases per test slot. Your original floor-conversion test passes too. Both slots used app-local D3D12 WARP (SM 6.6) with DXIL validation on this host. - With the extra test, semantics 36 passed and 1 ignored, VALU 35 passed, lowerer 33 passed. Strict shader Clippy passed with default and all features, and asset policy passed. The optional [test-only commit `df1d930`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/df1d93053f8d2f1750b0e9c9eaead47ce3fa6c12) adds that widening coverage. Its parent is a clean merge of your exact commit and current `main`. Please keep the Git author if you cherry-pick or rebase it. If you fold the test into a commit, please add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`.
fuzeofficlal commented 2026-11-27 01:58:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T01:58:25Z.

Review: APPROVED


Reviewed 74b155425dea8cdb55eb1766d62ebc87f6eda6d2.

LGTM — replacing the remaining -0.0 - x lowering sites (fsrc, half_fsrc, v_fma_mix_*, and packed f16 ops) with Lower::fneg (bitcast to i32, XOR 0x8000_0000, bitcast back) flips bit 31 on NaNs deterministically across adapters while preserving RDNA2's abs-before-neg ordering. All other float neg modifier paths in lower.rs already use bitwise sign XOR.

Verified locally on RTX 5090 + WARP:

  • cargo xtask policy (191 files ok), cargo clippy -p victory-shader --all-targets / --all-features (-D warnings), and zero new rustfmt diffs (lower.rs stays under 512 KiB at 520,622 bytes).
  • emit::semantics_tests:: passed (35 passed, 1 pre-existing ignored, 0 mismatches across hardware and WARP), and source_modifiers_set_the_sign_of_a_nan passes under both VICTORY_WARP=0 and VICTORY_WARP=1.
<!-- greyfinch-sync:Marvisak/VIctory:review:6082506874 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2823#pullrequestreview-6082506874). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-27T01:58:25Z. **Review:** APPROVED --- Reviewed `74b155425dea8cdb55eb1766d62ebc87f6eda6d2`. **LGTM** — replacing the remaining `-0.0 - x` lowering sites (`fsrc`, `half_fsrc`, `v_fma_mix_*`, and packed `f16` ops) with `Lower::fneg` (bitcast to `i32`, XOR `0x8000_0000`, bitcast back) flips bit 31 on NaNs deterministically across adapters while preserving RDNA2's `abs`-before-`neg` ordering. All other float `neg` modifier paths in `lower.rs` already use bitwise sign XOR. Verified locally on RTX 5090 + WARP: - `cargo xtask policy` (`191 files ok`), `cargo clippy -p victory-shader --all-targets` / `--all-features` (`-D warnings`), and zero new `rustfmt` diffs (`lower.rs` stays under 512 KiB at 520,622 bytes). - `emit::semantics_tests::` passed (35 passed, 1 pre-existing ignored, 0 mismatches across hardware and WARP), and `source_modifiers_set_the_sign_of_a_nan` passes under both `VICTORY_WARP=0` and `VICTORY_WARP=1`.
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/2823:upstream-pr/2823'
git switch 'upstream-pr/2823'

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