fix(shader): preserve signed zero when f32 div:2 underflows #3221
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3221
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@brainbudt on GitHub · 2026-11-26 13:34 UTC · original
brainbudt:fix/shader-div2-signed-zero→main· +55 −13 · 2 files · 1 commits · openProblem
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. Fordiv: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
dxil.dll1.9.2609.5.victory-shaderpassed native library Clippy with-D warnings, all three resource-tracking tests, 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).
@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:
+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.@oni-swr on GitHub · 2026-11-26 13:49 UTC · original
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).
@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:2underflows.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@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-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.
@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.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
div:2of a result in the lowest binade: +0, or a zero of its sign? #3171div:2of a result in the lowest binade: +0, or a zero of its sign? #3171View 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.