Skip to content

Shaders: maximums and minimums order -0 below +0, as RDNA2 does - #448

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
Dufftwt:fix/min-max-signed-zeros
Nov 24, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
Dufftwt:fix/min-max-signed-zeros

Conversation

@Dufftwt

@Dufftwt Dufftwt commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

What's wrong

RDNA2's float maximum and minimum treat -0 as smaller than +0. Whatever the operand order, max(-0, +0) = +0 and min(-0, +0) = -0:

  • the ISA's V_MAX_F32 pseudocode handles the two zeros explicitly;
  • LLVM selects fmaximumnum/fminimumnum, which order -0 below +0, directly to V_MAX_F32/V_MIN_F32.

The lowering maps these instructions to DXIL's FMax / FMin, and DXIL defines those as a compare and select (DXIL.rst: ">= is used instead of > so that if min(x,y) = x then max(x,y) = y"). For two zeros, then, which one comes out depends on the operand order and on the host, not on the sign. For example, FMax(-0, +0) gives -0.

Affected instructions:

  • v_max_f32, v_min_f32;
  • v_max3_f32, v_min3_f32;
  • v_max_f16, v_min_f16, v_max3_f16, v_min3_f16;
  • v_pk_max_f16, v_pk_min_f16.

A wrong zero sign matters where it reaches something sign-sensitive. A common shader pattern is max(x, 0.0) when x is -0: the hardware gives +0, and a following v_rsq_f32 gives +∞. We can give -0, and then -∞.

What the fix does

A new helper, max_min, used at all those call sites:

  • returns FMax / FMin as before for every pair of operands except two zeros;
  • for two zeros (both magnitudes 0), resolves the result by the sign bits: the maximum is -0 only if both are -0 (a & b), and the minimum is +0 only if both are +0 (a | b).

NaN handling and every non-zero result are unchanged. v_med3_*, which builds on FMax/FMin internally, is left as it was.

How it behaves after the fix

Maximums and minimums of a -0 and a +0 give +0 and -0 respectively in either operand order, as on the console. Everything else is unchanged.

Tests

The new test max_and_min_order_negative_zero_below_positive_zero (valu_tests.rs, run on the host GPU and WARP) runs v_max_f32, v_min_f32, v_max3_f32 and v_min3_f32. Each case has a -0 and a +0 among the first two operands, in both orders. The expected result is +0 for the maximums and -0 for the minimums.

I ran cargo test --workspace on GitHub's windows-latest with Rust 1.98.1 from rust-toolchain.toml (the runner has a hardware D3D12 device as well as WARP):

Not tested: the f16 and packed forms on a GPU (they go through the same helper, in f32), and a title shader.

v_max_f32, v_min_f32, their three-operand and f16 forms and the packed
v_pk_max_f16/v_pk_min_f16 were lowered to DXIL's FMax and FMin. DXIL
specifies those as a compare and select, so with a -0 and a +0 either
zero can come out, depending on the operand order and the host; RDNA2
orders -0 below +0, so max(-0, +0) is +0 and min(-0, +0) is -0 in
either order. A -0 that should have been +0 then turns, say, a
following reciprocal square root's +inf into -inf.

A new helper keeps FMax/FMin for everything but two zeros, which it
resolves by their sign bits.
@DaviTostes

Copy link
Copy Markdown

Verified bf84134 on Arch Linux x86_64 (rustc 1.98.1, cargo test -p victory-shader): 56 passed, 0 failed, 0 ignored.

Not covered: the ordered -0 results on a real GPU (no SM 6.6 device here) and title shaders (no game data).

@marquezinii

Copy link
Copy Markdown
Contributor

Ran bf84134 merged onto main d2a4bae (clean) on NVIDIA. valu_tests runs each case on the hardware device and on WARP.

  • NVIDIA GeForce RTX 5070 12 GB (32-lane waves), driver 621.24; Windows 11 Pro 26300; Rust 1.98.1 (pinned) x86_64-pc-windows-msvc, debug
  • max_and_min_order_negative_zero_below_positive_zero: ok on both, so the RTX 5070 gives -0 for min(+0, -0) and +0 for max with this lowering
  • cargo test -p victory-shader --lib -- --test-threads=1: 70 passed, 0 failed
  • cargo test --workspace -- --test-threads=1: 265 passed, 0 failed, 4 ignored

@slint6614-dev

Copy link
Copy Markdown

AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and the Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the D3D12 device for cargo test -p victory-shader --release (local adapter-selection patch, not part of this PR).

  • Whole victory-shader suite: 70 passed, 0 failed on both GPUs.
  • Negative control: with lower.rs reverted to main, max_and_min_order_negative_zero_below_positive_zero fails on both AMD GPUs (left: 0x80000000, right: 0): the host max/min returns -0 where RDNA2 gives +0, and this PR fixes it on AMD hardware.

@Marvisak
Marvisak merged commit dd54de2 into Marvisak:main Nov 24, 2026
@slint6614-dev slint6614-dev mentioned this pull request Nov 24, 2026
suirentakamura referenced this pull request in suirentakamura/victory-suiren Nov 24, 2026
Shaders: maximums and minimums order -0 below +0, as RDNA2 does
itsonlysimbo pushed a commit to itsonlysimbo/VIctory that referenced this pull request Nov 24, 2026
Shaders: maximums and minimums order -0 below +0, as RDNA2 does
github-actions Bot pushed a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
The ISA defines v_med3_f32/v_med3_f16 over v_min_f32/v_max_f32, which order
-0 below +0 (Marvisak#448 settled those), so of three zeros the median is the sign
two of them share; DXIL's plain FMin/FMax chain gives either zero. The zero
the chain leaves now takes that majority's sign (11 instructions). A NaN
still gives the minimum of the three.

Noticed while porting the idea of xcl-dev/xcl#1348 (DavidM1911).

Tests: hardware execution tests in valu_tests for both operand orders and
the halves.

Written with an AI assistant.
Marvisak pushed a commit that referenced this pull request Nov 25, 2026
* Shaders: v_med3 orders -0 below +0, as RDNA2 does

The ISA defines v_med3_f32/v_med3_f16 over v_min_f32/v_max_f32, which order
-0 below +0 (#448 settled those), so of three zeros the median is the sign
two of them share; DXIL's plain FMin/FMax chain gives either zero. The zero
the chain leaves now takes that majority's sign (11 instructions). A NaN
still gives the minimum of the three.

Noticed while porting the idea of xcl-dev/xcl#1348 (DavidM1911).

Tests: hardware execution tests in valu_tests for both operand orders and
the halves.

Written with an AI assistant.

* Shaders: keep main's med3 lowering, leave only the signed-zero test

main's max_min_flushed chain (77053a0) already orders -0 below +0, so the
majority-sign select this branch added gives the same result.

---------

Co-authored-by: pgm1207 <104554896+pgm1207@users.noreply.github.com>
Co-authored-by: pablogonz12 <pablogonz12@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants