fix(shader): preserve signed zero when f32 div:2 underflows #3221

Open
github-bot wants to merge 1 commit from github-bot/gh-3221 into main AGit
Owner

@⁠brainbudt on GitHub · 2026-11-26 13:34 UTC · original
brainbudt:fix/shader-div2-signed-zero → main · +55 −13 · 2 files · 1 commits · open

Problem

With IEEE mode off and f32 denormals flushed, gfx1035 returns -0 when div:2 halves a normal result in the lowest negative binade. VIctory returned +0 on WARP for both 0x80800000 and 0x80ffffff. The CPU model also rounded the half of 0x00ffffff up to FLT_MIN, while gfx1035 and WARP both returned +0. Hardware measurements · Independent WARP probe

Change

Classify the unscaled f32 result before applying the output multiplier. An existing zero or denormal becomes +0. For div:2, a result with exponent field 1 becomes a zero of its original sign. The next binade halves normally, and clamp still applies afterward.

Update the CPU model and add a bit-exact regression covering eight boundary inputs across multiply, add, and minimum.

Scope

Fixes #3171 in the f32 shader lowering and its semantic tests. The signed-zero rule comes from measurements on a PC gfx1035; console behavior remains unverified.

Verification

  • Windows WARP reproduced the failure on the base revision for both negative boundary inputs across all three instructions. With the patch, all 24 comparisons passed. Five existing Windows tests also passed, covering output multipliers, arithmetic, min/max and clamp, transcendental operations, and DXIL validation with dxil.dll 1.9.2609.5.
  • On macOS ARM64 and Linux x86-64, victory-shader passed native library Clippy with -D warnings, all three resource-tracking tests, and Windows MSVC cargo check --tests and cargo clippy --tests -- -D warnings.
  • Exhaustive CPU model check passed all 50,331,648 bit patterns in exponent fields 0 through 2 on both hosts.

Reviewed with mistral-loop: APPROVE (1 round, mistral-large-4-0 + deepseek-v4-1-flash-1017).
Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).

**@⁠brainbudt** on GitHub · 2026-11-26 13:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/3221) `brainbudt:fix/shader-div2-signed-zero` → `main` · +55 −13 · 2 files · 1 commits · open ## Problem With IEEE mode off and f32 denormals flushed, gfx1035 returns `-0` when `div:2` halves a normal result in the lowest negative binade. VIctory returned `+0` on WARP for both `0x80800000` and `0x80ffffff`. The CPU model also rounded the half of `0x00ffffff` up to `FLT_MIN`, while gfx1035 and WARP both returned `+0`. [Hardware measurements](https://github.com/boykopovar/AnyPS5/pull/567) · [Independent WARP probe](https://git.gtahub.st/github/Marvisak--VIctory/issues/3171) ## Change Classify the unscaled f32 result before applying the output multiplier. An existing zero or denormal becomes `+0`. For `div:2`, a result with exponent field 1 becomes a zero of its original sign. The next binade halves normally, and clamp still applies afterward. Update the CPU model and add a bit-exact regression covering eight boundary inputs across multiply, add, and minimum. ## Scope Fixes #3171 in the f32 shader lowering and its semantic tests. The signed-zero rule comes from measurements on a PC gfx1035; console behavior remains unverified. ## Verification - Windows WARP reproduced the failure on the base revision for both negative boundary inputs across all three instructions. With the patch, all 24 comparisons passed. Five existing Windows tests also passed, covering output multipliers, arithmetic, min/max and clamp, transcendental operations, and DXIL validation with `dxil.dll` 1.9.2609.5. - On macOS ARM64 and Linux x86-64, `victory-shader` passed native library Clippy with `-D warnings`, all three resource-tracking tests, and Windows MSVC `cargo check --tests` and `cargo clippy --tests -- -D warnings`. - Exhaustive CPU model check passed all 50,331,648 bit patterns in exponent fields 0 through 2 on both hosts. --- Reviewed with mistral-loop: APPROVE (1 round, `mistral-large-4-0` + `deepseek-v4-1-flash-1017`). Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:35:58 +00:00
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-26 13:45 UTC · original · reviewed

The change itself looks right to me. I checked the lowering against the model:

  • Lowering: the class is now taken from the unscaled result. A zero or denormal becomes +0. With div:2, exponent field 1 becomes a zero of its sign. Anything else is f * factor, which can't land in the denormal range: mul:2/mul:4 only grow, and div:2 of exponent field ≥ 2 stays normal. So the result no longer depends on how the host flushes, and the two selects can't overlap.
  • Model: mods_out with d(r) == 0 → 0, else out64(x * k), gives exactly the same rule. out64 keeps the sign below FLT_MIN, and 0x00ffffff * 0.5 is just under FLT_MIN, so it comes out +0. Your eight rows agree with the gfx1035 table and with the WARP probe in #3171.

Two points that aren't about the code:

1. Coordination with #3171. In #1633, andrewpozdnakov7-jpg's claim says "Moon0755 owns the proposed correction". In #3171, Moon0755 has the same change ready (19 cells, with runs that go red then green on the fork, and since then the AMD iGPU and RTX 2060 data) and wrote that they were waiting for the maintainer before opening it. This PR doesn't mention that. It would be good to link it and sort out with Moon0755 which PR goes forward, so the maintainer isn't reviewing two versions of the same fix. Their table also covers v_fma_f32 and the 0x00c00000/0x80c00000 rows, which could be folded into your test if this one is kept. (The co-author line for andrewpozdnakov7-jpg's probe is there 👍.)

2. Provenance in the comment. The old comment in write_f credited the "zero or denormal → +0" rule to RDNA2 measurements (Leonida, gfx1031/gfx1036). The new one attributes the whole rule to gfx1035. Keeping both sources would be more accurate: Leonida for the +0 part, AnyPS5 #567 on gfx1035 for the signed zero after div:2. mods_out's doc comment already does this.

**@⁠AurumCarlos** on GitHub · 2026-11-26 13:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/3221#pullrequestreview-6075195924) · **reviewed** The change itself looks right to me. I checked the lowering against the model: - **Lowering**: the class is now taken from the *unscaled* result. A zero or denormal becomes `+0`. With `div:2`, exponent field 1 becomes a zero of its sign. Anything else is `f * factor`, which can't land in the denormal range: `mul:2`/`mul:4` only grow, and `div:2` of exponent field ≥ 2 stays normal. So the result no longer depends on how the host flushes, and the two selects can't overlap. - **Model**: `mods_out` with `d(r) == 0 → 0`, else `out64(x * k)`, gives exactly the same rule. `out64` keeps the sign below `FLT_MIN`, and `0x00ffffff * 0.5` is just under `FLT_MIN`, so it comes out `+0`. Your eight rows agree with the gfx1035 table and with the WARP probe in #3171. Two points that aren't about the code: **1. Coordination with #3171.** In #1633, andrewpozdnakov7-jpg's claim says "Moon0755 owns the proposed correction". In #3171, Moon0755 has the same change ready (19 cells, with runs that go red then green on the fork, and since then the AMD iGPU and RTX 2060 data) and wrote that they were waiting for the maintainer before opening it. This PR doesn't mention that. It would be good to link it and sort out with Moon0755 which PR goes forward, so the maintainer isn't reviewing two versions of the same fix. Their table also covers `v_fma_f32` and the `0x00c00000`/`0x80c00000` rows, which could be folded into your test if this one is kept. (The co-author line for andrewpozdnakov7-jpg's probe is there 👍.) **2. Provenance in the comment.** The old comment in `write_f` credited the "zero or denormal → +0" rule to RDNA2 measurements (Leonida, gfx1031/gfx1036). The new one attributes the whole rule to gfx1035. Keeping both sources would be more accurate: Leonida for the `+0` part, AnyPS5 #567 on gfx1035 for the signed zero after `div:2`. `mods_out`'s doc comment already does this.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 13:49 UTC · original

WARP verification of head 874a5765de38ff07aee614f07220964875a978d3 (rebased on origin/main 63bcca4; no earlier verification on this PR)

Environment: Windows 11, no physical GPU used. VICTORY_WARP=1 and VICTORY_D3D12_WARP=1 set in every process; a local probe (not committed) printed and asserted the device adapter: Microsoft Basic Render Driver, DXGI flags 0x2 (software). dxil.dll from the newest installed Windows SDK (10.0.26100.0).

Commands (-j 4, --locked, --test-threads=1):

  • cargo test -p victory-shader --lib emit::semantics_tests::div2_of_the_lowest_binade_keeps_the_sign: 1 passed
  • cargo test -p victory-shader --lib emit::semantics_tests: 35 passed, 0 failed, 1 ignored
  • cargo test -p victory-shader --lib (whole crate): 299 passed, 4 failed, 3 ignored. The 4 failures (synthetic_tests::{a_mask_joined_with_data_at_a_uniform_loop_is_its_number, gathers_at_a_level_match_the_hardware, permutes_mask_high_address_bits_and_divergent_branches, permutes_read_the_lane_named_and_zero_from_lanes_left_out}) fail identically on plain main (298 passed, 4 failed), so they are WARP limits, not this PR.

Control (main's lower.rs + this PR's semantics_tests.rs): emit::semantics_tests 32 passed, 3 failed: the new div2_of_the_lowest_binade_keeps_the_sign, plus arithmetic_matches_the_hardware and min_max_and_clamp_match_the_hardware (their updated model expects the new behaviour). So the test catches the missing fix.

Diff review: no correctness problem found. Lowering is limited to omod 1..3; the div:2 special case only applies to the lowest normal binade (exp == 0x00800000), and denormal/zero inputs still go to +0 as before. One caveat: the expected values come from the cited gfx1035 measurement; WARP agrees with the model here but I could not check it against that hardware.

AI-assisted: verification by Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-26 13:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/3221#issuecomment-7193889118) **WARP verification of head `874a5765de38ff07aee614f07220964875a978d3`** (rebased on origin/main `63bcca4`; no earlier verification on this PR) Environment: Windows 11, no physical GPU used. `VICTORY_WARP=1` and `VICTORY_D3D12_WARP=1` set in every process; a local probe (not committed) printed and asserted the device adapter: `Microsoft Basic Render Driver`, DXGI flags `0x2` (software). `dxil.dll` from the newest installed Windows SDK (10.0.26100.0). Commands (`-j 4`, `--locked`, `--test-threads=1`): - `cargo test -p victory-shader --lib emit::semantics_tests::div2_of_the_lowest_binade_keeps_the_sign`: 1 passed - `cargo test -p victory-shader --lib emit::semantics_tests`: 35 passed, 0 failed, 1 ignored - `cargo test -p victory-shader --lib` (whole crate): 299 passed, 4 failed, 3 ignored. The 4 failures (`synthetic_tests::{a_mask_joined_with_data_at_a_uniform_loop_is_its_number, gathers_at_a_level_match_the_hardware, permutes_mask_high_address_bits_and_divergent_branches, permutes_read_the_lane_named_and_zero_from_lanes_left_out}`) fail identically on plain main (298 passed, 4 failed), so they are WARP limits, not this PR. Control (main's `lower.rs` + this PR's `semantics_tests.rs`): `emit::semantics_tests` 32 passed, 3 failed: the new `div2_of_the_lowest_binade_keeps_the_sign`, plus `arithmetic_matches_the_hardware` and `min_max_and_clamp_match_the_hardware` (their updated model expects the new behaviour). So the test catches the missing fix. Diff review: no correctness problem found. Lowering is limited to `omod` 1..3; the `div:2` special case only applies to the lowest normal binade (`exp == 0x00800000`), and denormal/zero inputs still go to +0 as before. One caveat: the expected values come from the cited gfx1035 measurement; WARP agrees with the model here but I could not check it against that hardware. AI-assisted: verification by Claude Code (Claude Sonnet 5.5).
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 14:10 UTC · original · approved these changes

Verified on RTX 5090 & WARP against origin/main (63bcca4):

  • cargo test -p victory-shader --lib div2_of_the_lowest_binade_keeps_the_sign -- --test-threads=1: passed on RTX 5090 and WARP.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Clean fix preserving signed zero when f32 div:2 underflows.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 14:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/3221#pullrequestreview-6075557506) · **approved these changes** Verified on RTX 5090 & WARP against `origin/main` (`63bcca4`): - `cargo test -p victory-shader --lib div2_of_the_lowest_binade_keeps_the_sign -- --test-threads=1`: passed on RTX 5090 and WARP. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Clean fix preserving signed zero when f32 `div:2` underflows. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠Jackster on GitHub · 2026-11-26 16:06 UTC · original · edited 2026-11-26 16:08 UTC

AMD check on a Radeon RX 7900 XTX (gfx1100, 32.0.32029.2010), victory-shader built with --features host so the hardware backend runs:

  • With only the PR's test and model applied to 63bcca4, 2 of 8 boundary inputs fail on the hardware backend, hardware and WARP agreeing: 80800000 and 80ffffff give 00000000 where 80000000 is expected.
  • With the PR, 8/8 pass and hardware/warp differ on 0.

The gfx1035 signed-zero rule holds on RDNA3 here. Synthetic only. AI-assisted.

**@⁠Jackster** on GitHub · 2026-11-26 16:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/3221#issuecomment-7197192307) · edited 2026-11-26 16:08 UTC AMD check on a Radeon RX 7900 XTX (gfx1100, 32.0.32029.2010), `victory-shader` built with `--features host` so the hardware backend runs: - With only the PR's test and model applied to 63bcca4, 2 of 8 boundary inputs fail on the hardware backend, hardware and WARP agreeing: `80800000` and `80ffffff` give `00000000` where `80000000` is expected. - With the PR, 8/8 pass and hardware/warp differ on 0. The gfx1035 signed-zero rule holds on RDNA3 here. Synthetic only. AI-assisted.
Author
Owner

@⁠Moon0755 on GitHub · 2026-11-26 16:43 UTC · original

Hardware report for head 874a576, plus a wider table.

I opened #3171 and had a fix for the same rows waiting on the maintainer's answer. This PR reaches the same lowering with less code, so I am not sending mine; I ran yours instead.

Setup. Windows 11, rustc 1.98.1, cargo test --locked -p victory-shader --lib --features host -- div2 output_multipliers, one adapter at a time: an AMD integrated Radeon, an NVIDIA RTX 2060, and WARP (VICTORY_WARP=1). The log names the adapter each run used.

Result, the same on all three adapters:

  • div2_of_the_lowest_binade_keeps_the_sign: ok (8 inputs on three instructions, 0 mismatches).
  • output_multipliers_give_positive_zero: ok.
  • An extra test of 19 cells copied from the AnyPS5 table F32OutputModifier.cpp (d3f2611384, gfx1035, IEEE off, f32 denormals flushed): ok. It covers what the PR's test does not: mul:2 (2 cells), v_fma_f32 with div:2 (2), div:2 together with clamp (3), and two-operand v_add_f32 and v_min_f32 rows.

For the base: that 19-cell test fails on 63bcca4 in 6 cells, each one the sign of a zero, identically on the three adapters (posted in #3171).

If useful: the 19-cell test is a test-only commit, a7b74c9 on Moon0755/VIctory, branch gemini/ci-a8-omod-div2-negative (81 lines in semantics_tests.rs). Its expected values are literals, so it does not depend on mods_out, and it merges onto this head without a conflict. Take it or leave it as you prefer.

Not checked: an RDNA2 card (the Radeon here is an older integrated one); the console; the full suite on this head (only the three tests above); whether each of your eight rows is a measured cell of the table. I compared the rule, not every row.

Synthetic tests only; no game files.

— Gemini & Fable

**@⁠Moon0755** on GitHub · 2026-11-26 16:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/3221#issuecomment-7198005128) Hardware report for head `874a576`, plus a wider table. I opened #3171 and had a fix for the same rows waiting on the maintainer's answer. This PR reaches the same lowering with less code, so I am not sending mine; I ran yours instead. **Setup.** Windows 11, rustc 1.98.1, `cargo test --locked -p victory-shader --lib --features host -- div2 output_multipliers`, one adapter at a time: an AMD integrated Radeon, an NVIDIA RTX 2060, and WARP (`VICTORY_WARP=1`). The log names the adapter each run used. **Result, the same on all three adapters:** - `div2_of_the_lowest_binade_keeps_the_sign`: ok (8 inputs on three instructions, 0 mismatches). - `output_multipliers_give_positive_zero`: ok. - An extra test of 19 cells copied from the AnyPS5 table `F32OutputModifier.cpp` (`d3f2611384`, gfx1035, IEEE off, f32 denormals flushed): ok. It covers what the PR's test does not: `mul:2` (2 cells), `v_fma_f32` with `div:2` (2), `div:2` together with `clamp` (3), and two-operand `v_add_f32` and `v_min_f32` rows. For the base: that 19-cell test fails on `63bcca4` in 6 cells, each one the sign of a zero, identically on the three adapters (posted in #3171). **If useful:** the 19-cell test is a test-only commit, `a7b74c9` on `Moon0755/VIctory`, branch `gemini/ci-a8-omod-div2-negative` (81 lines in `semantics_tests.rs`). Its expected values are literals, so it does not depend on `mods_out`, and it merges onto this head without a conflict. Take it or leave it as you prefer. **Not checked:** an RDNA2 card (the Radeon here is an older integrated one); the console; the full suite on this head (only the three tests above); whether each of your eight rows is a measured cell of the table. I compared the rule, not every row. Synthetic tests only; no game files. — Gemini & Fable
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3221/head:github-bot/gh-3221'
git switch 'github-bot/gh-3221'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3221'
git switch 'github-bot/gh-3221'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3221'
git switch 'github-bot/gh-3221'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3221'
git switch 'main'
git merge --squash 'github-bot/gh-3221'
git switch 'main'
git merge --ff-only 'github-bot/gh-3221'
git switch 'main'
git merge 'github-bot/gh-3221'
git push origin 'main'
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!3221
No description provided.