fix(shader): preserve signed zero when f32 div:2 underflows #3219
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3221"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by brainbudt on 2026-11-26T13:34:36Z.
Problem
With IEEE mode off and f32 denormals flushed, gfx1035 returns
-0whendiv:2halves a normal result in the lowest negative binade. VIctory returned+0on WARP for both0x80800000and0x80ffffff. The CPU model also rounded the half of0x00ffffffup toFLT_MIN, while gfx1035 and WARP both returned+0. Hardware measurements · Independent WARP probeChange
Classify the unscaled f32 result before applying the output multiplier. An existing zero or denormal becomes
+0(Leonida RDNA2 measurements). Fordiv:2, a result with exponent field 1 becomes a zero of its original sign (AnyPS5 #567, gfx1035). The next binade halves normally, and clamp still applies afterward.Update the CPU model and add bit-exact regressions covering:
mul:2,v_fma_f32withdiv:2,div:2+clamp, and two-operandv_add_f32/v_min_f32).Scope
Fixes #3171 in the f32 shader lowering and its semantic tests (coordinated with @Moon0755, who deferred their branch in favor of this PR). The signed-zero rule comes from measurements on a PC gfx1035; console behavior remains unverified.
Verification
dxil.dll1.9.2609.5.victory-shaderpassed native library Clippy with-D warnings,cargo test -p xtask --locked(includingrepository_passes_policy), and Windows MSVCcargo check --testsandcargo clippy --tests -- -D warnings.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).
Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T13:45:38Z.
Review: COMMENTED
The change itself looks right to me. I checked the lowering against the model:
+0. Withdiv:2, exponent field 1 becomes a zero of its sign. Anything else isf * factor, which can't land in the denormal range:mul:2/mul:4only grow, anddiv:2of exponent field ≥ 2 stays normal. So the result no longer depends on how the host flushes, and the two selects can't overlap.mods_outwithd(r) == 0 → 0, elseout64(x * k), gives exactly the same rule.out64keeps the sign belowFLT_MIN, and0x00ffffff * 0.5is just underFLT_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_f32and the0x00c00000/0x80c00000rows, 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_fcredited 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+0part, AnyPS5 #567 on gfx1035 for the signed zero afterdiv:2.mods_out's doc comment already does this.Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T13:49:33Z.
WARP verification of head
874a5765de38ff07aee614f07220964875a978d3(rebased on origin/main63bcca4; no earlier verification on this PR)Environment: Windows 11, no physical GPU used.
VICTORY_WARP=1andVICTORY_D3D12_WARP=1set in every process; a local probe (not committed) printed and asserted the device adapter:Microsoft Basic Render Driver, DXGI flags0x2(software).dxil.dllfrom 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 passedcargo test -p victory-shader --lib emit::semantics_tests: 35 passed, 0 failed, 1 ignoredcargo 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'ssemantics_tests.rs):emit::semantics_tests32 passed, 3 failed: the newdiv2_of_the_lowest_binade_keeps_the_sign, plusarithmetic_matches_the_hardwareandmin_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
omod1..3; thediv:2special 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).
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T14:10:39Z.
Review: APPROVED
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:2underflows.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T16:06:33Z.
AMD check on a Radeon RX 7900 XTX (gfx1100, 32.0.32029.2010),
victory-shaderbuilt with--features hostso the hardware backend runs:63bcca4, 2 of 8 boundary inputs fail on the hardware backend, hardware and WARP agreeing:80800000and80ffffffgive00000000where80000000is expected.The gfx1035 signed-zero rule holds on RDNA3 here. Synthetic only. AI-assisted.
Imported from Marvisak/VIctory. Originally posted by Moon0755 on 2026-11-26T16:43:02Z.
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.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_f32withdiv:2(2),div:2together withclamp(3), and two-operandv_add_f32andv_min_f32rows.For the base: that 19-cell test fails on
63bcca4in 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,
a7b74c9onMoon0755/VIctory, branchgemini/ci-a8-omod-div2-negative(81 lines insemantics_tests.rs). Its expected values are literals, so it does not depend onmods_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
Imported from Marvisak/VIctory. Originally posted by brainbudt on 2026-11-27T11:13:55Z.
Updated in
b6931edto address @AurumCarlos's review and fold in @Moon0755's test:crates/victory-shader/src/emit/lower.rs): Restored the Leonida RDNA2 (gfx1031/gfx1036) attribution for the zero/denormal →+0rule alongside AnyPS5 #567 (gfx1035) fordiv:2signed-zero underflow.crates/victory-shader/src/emit/semantics_tests.rs): Folded in @Moon0755'sf32_omod_div2_underflow_matches_rdna2_oracletest (froma7b74c9, withCo-authored-byattribution) coveringmul:2,v_fma_f32withdiv:2,div:2 + clamp, and two-operandv_add_f32/v_min_f32.Verified locally on macOS ARM64:
cargo clippy -p victory-shader --lib -- -D warningsandcargo test -p xtask --locked(6/6, includingrepository_passes_policy) pass; Windows CI will run the full test suite.Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T23:10:10Z.
There is now a physical RDNA1 check of this PR together with #1554. On the RX 5700 XT, ColorMeBoom ran the combined branch
1a96aa6, which includes this PR's exactb6931ed. The seven legacy FMA inputs matched all 14 output words fordiv:2anddiv:2 clampon both the hardware adapter and WARP. The full host shader suite passed 307 tests with three ignored. RX 5700 XT report in #1554 · Combined branchThis adds RDNA1 coverage for the combined legacy path alongside the earlier results for the standalone
div:2fix. Thanks @ColorMeBoom for running it. AI-assisted with Codex.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.