Shaders: min, max and med3 read f32 denormals as signed zeros (#136) #229

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

@⁠bawhen on GitHub · 2026-11-24 22:49 UTC · original
bawhen:shader/min-max-med3-denormals → main · +112 −10 · 2 files · 1 commits · open

Refs #136: the "fp32 denormals and the sign of a flushed zero (v_min/max/med3_f32)" row.

Problem. The console's float mode reads an f32 denormal operand of v_min/max/min3/max3/med3_f32 as a zero of its sign. The translation left that to the host's denormal mode, which varies. And v_med3_f32 didn't order -0 below +0 the way v_min/max_f32 already do here: it used the host's FMin/FMax directly.

Change. A port of the min/max/med3 part of upstream Marvisak/VIctory 77053a0 ("accuracy fixes for discards, f16 rounding, denormals, med3 and signed zeros"). The fork's code here still matched upstream's from before that commit.

  • ftz: an f32 denormal becomes a zero of its sign, decided on the bits.
  • max_min flushes both operands, then calls max_min_flushed, the existing -0-below-+0 ordering.
  • med3 flushes its three operands and is built from max_min_flushed, so its median orders -0 below +0 too. The fork's NaN rule (the minimum of the three) is unchanged.

The f16 forms (v_max/min/med3_f16, v_pk_min/max_f16) go through the same helpers on values widened to f32. ftz never changes those, since the smallest f16 denormal (2^-24) is a normal f32.

The rest of 77053a0 is out of scope: discards, the f16 rounding mode, output modifiers, the FMA probe, and its test file.

Test. min_max_and_med3_read_denormals_as_signed_zeros runs all five instructions over every triple of seven values on the hardware adapter and WARP: ±0, ±denormal, the largest denormal, the smallest normal, and -1. Each result is compared bit for bit with a CPU reference that flushes and orders -0 below +0.

Results on an AMD Radeon RX 7700 (RDNA3, 16 GB, driver 32.0.32029.2010) and WARP, Windows 11:

RX 7700 WARP
main (4e17478) 0 wrong 223 wrong: v_min 35, v_max 35, v_min3 50, v_max3 37, v_med3 66
this PR 0 wrong 0 wrong

Examples on main (WARP):

  • v_min_f32(-0, +denormal) = +0, want -0. WARP flushes the denormal, so the -0/+0 rule never sees two zeros.
  • v_med3_f32(+0, +0, -0) = -0, want +0. No denormal is involved: the median ignored the zero ordering.

This RX 7700 is not affected. It already gives the console's result in all 343 cases for all five instructions. The fix is for hosts that don't flush, plus CI's WARP adapter. Upstream's 77053a0 names an RX 6700 XT (RDNA2) as one that doesn't flush, which suggests RDNA3 and RDNA2 drivers differ here. I haven't tested an RDNA2 card or NVIDIA.

  • cargo test --workspace --locked: 497 passed, 0 failed, 7 ignored (main's 496 plus the new test).
  • cargo clippy --workspace --all-targets [--all-features] --locked -- -D warnings: clean.
  • cargo xtask policy: ok. cargo fmt --check adds no differences in the two changed files beyond what main already has.
  • No game files used. All inputs are synthetic.

AI use: Claude Code (Claude Opus 5.5) found the upstream fix, wrote the port and the test, and ran them on my card; opened at my request.

🤖 Generated with Claude Code

**@⁠bawhen** on GitHub · 2026-11-24 22:49 UTC · [original](https://github.com/fxceless/victory-fork/pull/229) `bawhen:shader/min-max-med3-denormals` → `main` · +112 −10 · 2 files · 1 commits · open Refs #136: the "fp32 denormals and the sign of a flushed zero (`v_min/max/med3_f32`)" row. **Problem.** The console's float mode reads an f32 denormal operand of `v_min/max/min3/max3/med3_f32` as a zero of its sign. The translation left that to the host's denormal mode, which varies. And `v_med3_f32` didn't order -0 below +0 the way `v_min/max_f32` already do here: it used the host's `FMin`/`FMax` directly. **Change.** A port of the min/max/med3 part of upstream Marvisak/VIctory `77053a0` ("accuracy fixes for discards, f16 rounding, denormals, med3 and signed zeros"). The fork's code here still matched upstream's from before that commit. - `ftz`: an f32 denormal becomes a zero of its sign, decided on the bits. - `max_min` flushes both operands, then calls `max_min_flushed`, the existing -0-below-+0 ordering. - `med3` flushes its three operands and is built from `max_min_flushed`, so its median orders -0 below +0 too. The fork's NaN rule (the minimum of the three) is unchanged. The f16 forms (`v_max/min/med3_f16`, `v_pk_min/max_f16`) go through the same helpers on values widened to f32. `ftz` never changes those, since the smallest f16 denormal (2^-24) is a normal f32. The rest of `77053a0` is out of scope: discards, the f16 rounding mode, output modifiers, the FMA probe, and its test file. **Test.** `min_max_and_med3_read_denormals_as_signed_zeros` runs all five instructions over every triple of seven values on the hardware adapter and WARP: ±0, ±denormal, the largest denormal, the smallest normal, and -1. Each result is compared bit for bit with a CPU reference that flushes and orders -0 below +0. Results on an **AMD Radeon RX 7700 (RDNA3, 16 GB, driver 32.0.32029.2010)** and WARP, Windows 11: | | RX 7700 | WARP | |---|---|---| | `main` (`4e17478`) | 0 wrong | 223 wrong: `v_min` 35, `v_max` 35, `v_min3` 50, `v_max3` 37, `v_med3` 66 | | this PR | 0 wrong | 0 wrong | Examples on `main` (WARP): - `v_min_f32(-0, +denormal) = +0`, want -0. WARP flushes the denormal, so the -0/+0 rule never sees two zeros. - `v_med3_f32(+0, +0, -0) = -0`, want +0. No denormal is involved: the median ignored the zero ordering. **This RX 7700 is not affected.** It already gives the console's result in all 343 cases for all five instructions. The fix is for hosts that don't flush, plus CI's WARP adapter. Upstream's `77053a0` names an RX 6700 XT (RDNA2) as one that doesn't flush, which suggests RDNA3 and RDNA2 drivers differ here. I haven't tested an RDNA2 card or NVIDIA. - `cargo test --workspace --locked`: 497 passed, 0 failed, 7 ignored (`main`'s 496 plus the new test). - `cargo clippy --workspace --all-targets [--all-features] --locked -- -D warnings`: clean. - `cargo xtask policy`: ok. `cargo fmt --check` adds no differences in the two changed files beyond what `main` already has. - No game files used. All inputs are synthetic. *AI use: Claude Code (Claude Opus 5.5) found the upstream fix, wrote the port and the test, and ran them on my card; opened at my request.* 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The console's float mode reads f32 denormal operands of
v_min/max/min3/max3/med3_f32 as zeros of their sign. The translation
left that to the host, whose denormal mode varies, and v_med3_f32 took
-0 and +0 in whatever order the host's FMin/FMax gave. Port of the
min/max/med3 part of upstream Marvisak/VIctory 77053a0: `ftz` decides
the flush on the bits, `max_min` flushes and orders -0 below +0, and
`med3` is built from the same ordered minimum and maximum.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:38:54 +00:00
Author
Owner

@⁠YeokiNoewChi on GitHub · 2026-11-25 00:37 UTC · original

RTX 3070 Ti 8 GB (32-lane waves), Windows 11, Series X profile. Tree: this fork with Emiriley's bench7 stack (which merges this PR) plus a few NVIDIA-side changes, VICTORY_SHADER_LOOP_LIMIT=256.

With this PR the main character's hair breaks. In the prologue cell, with a warm cache and in steady state, the hair is drawn as scattered shards with a dark wedge through it, and draws per frame drop from about 5,100 to about 3,400. Reverting only this PR's merge (and nothing else) brings the hair back to normal on the same cache and camera position.

Offline, the hair-strand program is where it changes. I translated the 70 programs whose names contain "hair" with VICTORY_EMIT_DUMP and compared the emitted DXIL with and without this PR: 60 differ. In bvh_skinning_smh:CSGenerateHairVB (#72f552f4553fb5f9), which builds the strands' vertex buffer, the only change is this pattern added five times around min/max operands:

and i32 %x, 2139095040      ; exponent bits
icmp eq i32 %e, 0
and i32 %x, -2147483648     ; sign bit
select i1 %c, i32 %sign, i32 %x
bitcast i32 ... to float

So any operand whose exponent bits are zero is replaced by a signed zero. My guess is that this program runs v_min_f32/v_max_f32 on values that are not normal floats (small integer or packed bit patterns, which look like denormals), and the flush destroys them. That would match the shards.

A possible way forward: limit the flush to operands known to be floats, or put it behind a switch until the console's denormal mode for these instructions is confirmed.

Hashes and counts only, nothing from the game. AI-assisted (Claude Code).

**@⁠YeokiNoewChi** on GitHub · 2026-11-25 00:37 UTC · [original](https://github.com/fxceless/victory-fork/pull/229#issuecomment-7148704114) RTX 3070 Ti 8 GB (32-lane waves), Windows 11, Series X profile. Tree: this fork with Emiriley's `bench7` stack (which merges this PR) plus a few NVIDIA-side changes, `VICTORY_SHADER_LOOP_LIMIT=256`. **With this PR the main character's hair breaks.** In the prologue cell, with a warm cache and in steady state, the hair is drawn as scattered shards with a dark wedge through it, and draws per frame drop from about 5,100 to about 3,400. Reverting only this PR's merge (and nothing else) brings the hair back to normal on the same cache and camera position. **Offline, the hair-strand program is where it changes.** I translated the 70 programs whose names contain "hair" with `VICTORY_EMIT_DUMP` and compared the emitted DXIL with and without this PR: 60 differ. In `bvh_skinning_smh:CSGenerateHairVB` (`#72f552f4553fb5f9`), which builds the strands' vertex buffer, the only change is this pattern added five times around min/max operands: ``` and i32 %x, 2139095040 ; exponent bits icmp eq i32 %e, 0 and i32 %x, -2147483648 ; sign bit select i1 %c, i32 %sign, i32 %x bitcast i32 ... to float ``` So any operand whose exponent bits are zero is replaced by a signed zero. My guess is that this program runs `v_min_f32`/`v_max_f32` on values that are not normal floats (small integer or packed bit patterns, which look like denormals), and the flush destroys them. That would match the shards. A possible way forward: limit the flush to operands known to be floats, or put it behind a switch until the console's denormal mode for these instructions is confirmed. Hashes and counts only, nothing from the game. AI-assisted (Claude Code).
Author
Owner

@⁠HAL9000RELOADED on GitHub · 2026-11-25 19:29 UTC · original

RX 7900 XTX (RDNA3), Windows 10 + #402, main d510ee4: min_max_and_med3_read_denormals_as_signed_zeros passes with this PR. It also passes with the test alone on main, so this card already reads those denormals as the console does. The PR changes nothing on RDNA3 and breaks nothing. Run table: #129 (#129).

**@⁠HAL9000RELOADED** on GitHub · 2026-11-25 19:29 UTC · [original](https://github.com/fxceless/victory-fork/pull/229#issuecomment-7172371547) RX 7900 XTX (RDNA3), Windows 10 + #402, main `d510ee4`: `min_max_and_med3_read_denormals_as_signed_zeros` passes with this PR. It also passes with the test alone on main, so this card already reads those denormals as the console does. The PR changes nothing on RDNA3 and breaks nothing. Run table: #129 (https://git.gtahub.st/github/fxceless--victory-fork/pulls/129).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/229/head:github-bot/gh-229'
git switch 'github-bot/gh-229'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-229'
git switch 'github-bot/gh-229'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-229'
git switch 'github-bot/gh-229'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-229'
git switch 'main'
git merge --squash 'github-bot/gh-229'
git switch 'main'
git merge --ff-only 'github-bot/gh-229'
git switch 'main'
git merge 'github-bot/gh-229'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.