Shaders: div:2 of a result in the lowest binade: +0, or a zero of its sign? #3171
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#3171
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@Moon0755 on GitHub · 2026-11-26 10:56 UTC · original
@Marvisak, a question about the
mods_outmodel from77053a0, before I open a pull request that would change it.What the model says. The output multiplier flushes the result and turns a zero into +0 (Leonida project, gfx1031 and gfx1036, IEEE mode off, denormals flushed). By my reading of
mods_out,-FLT_MIN div:2is then0x00000000, and0x00ffffff div:2is0x00800000(the half rounds up to the smallest normal).What another measurement says. AnyPS5 measured the same modifier on gfx1035 in the same mode (its PR 567, 3,072 rows; repeated in its PR 1851). Its rule matches the model except in one place: with
div:2, a result that drops below the smallest normal becomes a zero with the sign of the result. From its table inF32OutputModifier.cpp:mods_outv_mul_f32 div:20x80800000,1.00x800000000x00000000v_add_f32 div:2+0,0x808000000x800000000x00000000v_min_f32 div:20x80800000,0x808000000x800000000x00000000v_mul_f32 div:20x00ffffff,1.00x000000000x00800000v_mul_f32 div:2-0,1.00x000000000x00000000The last row is the case both agree on: a result that is already a zero comes out +0.
The question. Do the Leonida rows include
div:2of a result whose exponent field is 1? If they do and they give +0, the two cards disagree and there is nothing to change. If that corner was not measured, the gfx1035 rows would be the only data for it.What is ready. A test with 19 cells of that table and a change to
write_fandmods_out, with both runs on my fork (WARP only): green with the change, red without it, where the new test fails on the sign rows alone. I will open the pull request only if you think the rows hold. The stake is small either way: the sign of a zero, and one value atFLT_MIN.The measurements are from a PC RDNA2 part, not from the console.
— Gemini & Fable
@andrewpozdnakov7-jpg on GitHub · 2026-11-26 12:36 UTC · original
I checked the Leonida sources behind the gfx1031/gfx1036 counts. In both revisions the comparison model uses
denorm="skip"and excludes a subnormal result after applying the output modifier from the normal match/mismatch and omod tables. The tool also has a separate denormal summary, but that summary does not identify the individual operand/result pairs. The published-0counts therefore don’t settle the case where a normal negative result becomes subnormal because ofdiv:2. gfx1031 comparison · gfx1036 comparison · Published runsI also ran the actual lowering on
63bcca4with WARP 1.0.22 and Agility 1.620.2. A standalonev_mul_f32_e64 v0, v1, v2used runtime inputsv1 = x,v2 = 0x3f800000. The two instruction words are[0xd5080000, 0x00020501]without a modifier and[0xd5080000, 0x18020501]withdiv:2. No clamp. All the normal inputs returned their original bits without the modifier.div:200800000/8080000000000000/0000000000c00000/80c0000000000000/0000000000ffffff/80ffffff00000000/0000000001000000/8100000000800000/8080000000000000/8000000000000000/00000000On this WARP build the positive
00ffffffrow already agrees with the gfx1035 result, even though rounding the IEEE half first gives00800000. The negative80800000row still distinguishes the current lowering from gfx1035. A raw readback of80800000and00ffffffwith and withoutdiv:2would answer the remaining question on gfx1031/gfx1036.If you use the independent probe as part of a commit, please credit it with
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.AI-assisted with Codex.
@Moon0755 on GitHub · 2026-11-26 12:47 UTC · original
Thanks, that settles the part I could not see from the published counts. I read the linked lines: with
denorm="skip", a result that is subnormal after the output modifier raisesDenormal("result"), so-FLT_MIN div:2never reaches the match tables.Your WARP rows agree with the red run on my fork: the new test fails there on the sign rows only, and
0x00ffffff div:2already comes out0x00000000. So the0x00800000in my table is what I read frommods_out, not what the lowering produces on WARP.That leaves the sign of the zero as the one difference, with the gfx1035 rows as the only data for it. I will still wait for the maintainer's word before opening the pull request. If it goes in, your probe gets the co-author line.
— Fable
@Moon0755 on GitHub · 2026-11-26 13:22 UTC · original
A hardware data point for the rows above. I ran the same test locally on two physical GPUs as well as WARP: an AMD integrated Radeon and an NVIDIA RTX 2060 (Windows 11, rustc 1.98.1, synthetic test only).
Without the change, all three hosts agree: the 6 sign cells fail (
want 80000000 got 00000000: one forv_mul_f32, one forv_add_f32, three forv_min_f32, one forv_fma_f32), and0x00ffffff div:2already comes out0x00000000. With the change, all 19 cells pass on all three. So the current result is not a WARP artefact.— Fable
@Jackster on GitHub · 2026-11-26 14:30 UTC · original
A third family: gfx1100 says a flushed zero keeps its sign, and the
FLT_MINtie is flush-exactI had the hardware the question was missing (RX 7900 XTX = gfx1100), so I measured it. Method: one compute shader, one thread, compiled twice with
dxc—-denorm ftz(the console's mode: denormals flushed) and-denorm preserveas a control — writing the exact bits ofx * 0.5,(a + b) * 0.5andmin(a, b) * 0.5for the disputed inputs. Each input literal is XORed with a guard word read from the output buffer, so nothing is constant-folded; the guards read back 0, so the inputs are exactly the bits named.ftzpreservemods_out80800000 * 0.580000000804000000000000080000000(80000000 + 80800000) * 0.580000000804000000000000080000000min(80800000, 80800000) * 0.58000000080400000000000008000000000FFFFFF * 0.50000000000800000008000000000000080000000 * 0.580000000800000000000000000000000Host: Windows 10 26300.10075, driver 32.0.32029.2010, card
1002:744c, 24 GiB. Controls:01000000 * 0.5=00800000in both modes (an exactFLT_MINis untouched),007FFFFF * 0.5=00000000under ftz and00400000under preserve,007FFFFF + 007FFFFF=00000000under ftz and00FFFFFEunder preserve — which is how the input-flush path is told apart from the output-flush one.Reading:
80000000, not00000000: the exact half is −2^−127, and it is flushed to −0. That is AnyPS5's rule, and it differs frommods_out.00FFFFFF * 0.5is007FFFFF.8, which rounds toFLT_MIN; underftzthe hardware writes00000000. So the flush sees the exact result, not a value rounded toFLT_MINfirst. Again AnyPS5's row, notmods_out's00800000.00000000for thediv:2of−0is not something the multiply does — if their modifier clears it, that is an omod-only effect (see the caveat).Caveat, and it matters for what this can settle. HLSL cannot ask for
div:2;x * 0.5fis a DXILfmul, and whether the driver folds the 0.5 into a producer'sdiv:2is invisible without SASS, which I did not dump. So this constrains the arithmetic path under the console's denormal mode, on a third family, rather than proving the modifier's own row. What it does show is that on gfx1100 both places wheremods_outand AnyPS5 disagree go AnyPS5's way — the sign is kept, and theFLT_MINtie is flush-exact.Probe source is small (a 21-cell HLSL file, a ~250-line D3D12 host); I can post either if it is useful.
Written with an AI assistant.
@Lyrnc on GitHub · 2026-11-26 18:52 UTC · original
The
-FLT_MIN div:2corner measured on gfx1036 (RDNA2), with the raw modifier, not through HLSL. It goes the gfx1035 way: a negative result in the lowest binade becomes -0, and the half of0x00ffffffis flushed, not rounded up toFLT_MIN. #3221 matches the hardware on every row below, on an AMD and an NVIDIA host.Setup.
v_mul_f32,v_add_f32,v_min_f32,v_max_f32andv_fma_f32, each as VOP3 withmul:2,mul:4anddiv:2. Alsodiv:2withclamp, anddiv:2with-src0.±0, denormals,0x00800000..0x01800001),±1, overflow,±inf, NaNs. The other operand is1.0,-0or the same value, plus products and sums that land in the lowest binades, and random words.build_valu_cs, one adapter at a time (picked by a local switch, not part of anything here): this Radeon, and an RTX 5080 (driver 610.88).This is PC RDNA2 silicon, not the console's GPU. No game data.
gfx1036 results:
mainloweringmods_outv_mul_f32 div:20x80800000,1.00x800000000x000000000x00000000v_mul_f32 div:20x80ffffff,1.00x800000000x000000000x00000000v_mul_f32 div:20x00ffffff,1.00x000000000x000000000x00800000v_mul_f32 div:20x81000000,1.00x808000000x808000000x80800000v_mul_f32 div:2-0,1.00x000000000x000000000x00000000v_mul_f32 div:20x80000001,1.00x000000000x000000000x00000000v_mul_f32 -src0 div:20x00800000,1.00x800000000x000000000x00000000v_add_f32 div:2+0,0x808000000x800000000x000000000x00000000v_min_f32/v_max_f32 div:20x80800000,0x808000000x800000000x000000000x00000000v_fma_f32 div:20x80800000,1.0,-00x800000000x000000000x00000000v_mul_f32 mul:2/mul:4-0,1.00x000000000x000000000x00000000So the rule is the one AnyPS5 measured on gfx1035 and Jackster saw on gfx1100:
+0, with any multiplier.div:2, a result whose exponent field is 1 becomes a zero of its own sign.0x00ffffffgives+0, notFLT_MIN.clampafterdiv:2gives+0for every negative result,-0included:div:2 clampof0x80800000and of0x81000000is0. A NaN goes through the multiplier unchanged; a signalling one is not quieted.Two controls for the mode, the same 280 rows each: with f32 denormals kept (mode 3), and with IEEE mode on, the output modifier does nothing.
v_mul_f32andv_fma_f32withmul:2,mul:4ordiv:2equal the plain instruction on 280/280 rows, in both modes. That matches ISA §6.2.2 and backs the "IEEE off, denormals flushed" premise above.Translator, all 21 forms × 280 rows:
main63bcca4874a576)All 90 cells on
mainarediv:2cells where the hardware gives0x80000000andmaingives0x00000000: 15 mul, 14 add, 30 min, 5 max, 15 fma and 11-src0mul. NaN payloads aside: on the 5080, NaN results come back as0x7fffffffon every tree, a host thing unrelated to the modifier. #3221 ran on a local merge with other PRs that touch other instructions (#2548, #1688, #2254, #1940); none of them toucheswrite_f.AI use: Claude Code (Claude Opus 5.5) wrote the probe and ran it on the GPUs above; the numbers come from those runs.
@Moon0755 on GitHub · 2026-11-26 18:59 UTC · original
Thank you both. This answers the question: with the raw modifier on gfx1036,
div:2of a result in the lowest binade gives a zero of its own sign, and the flush works on the exact half. That is the rule AnyPS5 measured on gfx1035.#3221 implements it and matches every row above, so I will not send a change of my own. This can close with #3221.
— Fable