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

Manually merged
github merged 1 commit from gh-pr/448/head into gh-pr/448/base 2026-11-26 04:04:24 +00:00
Owner

@⁠Dufftwt on GitHub · 2026-11-24 03:31 UTC · original
Dufftwt:fix/min-max-signed-zeros → main · +59 −14 · 2 files · 1 commits · merged 2026-11-24 08:15 UTC

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):

  • This PR: everything passes. victory-shader has 70 passed, the other crates have 0 failed.
  • The new test alone on main: hardware: v_max_f32 [+0, -0] gives 0x80000000 (-0) instead of 0 (+0).
  • I ran with --test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (#181, #244).

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

**@⁠Dufftwt** on GitHub · 2026-11-24 03:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/448) `Dufftwt:fix/min-max-signed-zeros` → `main` · +59 −14 · 2 files · 1 commits · merged 2026-11-24 08:15 UTC ## 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): - **This PR:** everything passes. `victory-shader` has 70 passed, the other crates have 0 failed. - **The new test alone on `main`:** `hardware: v_max_f32 [+0, -0]` gives `0x80000000` (-0) instead of `0` (+0). - I ran with `--test-threads=1`. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (#181, #244). **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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:04:22 +00:00
github manually merged commit bf84134b2b into gh-pr/448/base 2026-11-26 04:04:24 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 03:42 UTC · original

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).

**@⁠DaviTostes** on GitHub · 2026-11-24 03:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/448#issuecomment-7121342923) 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).
Author
Owner

@⁠marquezinii on GitHub · 2026-11-24 05:15 UTC · original

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
**@⁠marquezinii** on GitHub · 2026-11-24 05:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/448#issuecomment-7122782481) 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
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 07:35 UTC · original

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.
**@⁠slint6614-dev** on GitHub · 2026-11-24 07:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/448#issuecomment-7125148999) 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.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!448
No description provided.