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

Merged
Dufftwt merged 1 commit from upstream-pr/448 into main 2026-11-24 08:15:49 +00:00
Dufftwt commented 2026-11-24 03:31:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T03:31:08Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573980539 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/448). Originally posted by [Dufftwt](https://github.com/Dufftwt) on 2026-11-24T03:31:08Z. --- ## 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.
DaviTostes commented 2026-11-24 06:55:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T03:42:20Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121342923 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/448#issuecomment-7121342923). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T03:42:20Z. --- 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 commented 2026-11-24 06:55:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by marquezinii on 2026-11-24T05:15:52Z.


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
<!-- greyfinch-sync:Marvisak/VIctory:comment:7122782481 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/448#issuecomment-7122782481). Originally posted by [marquezinii](https://github.com/marquezinii) on 2026-11-24T05:15:52Z. --- 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 commented 2026-11-24 08:02:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by slint6614-dev on 2026-11-24T07:35:05Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7125148999 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/448#issuecomment-7125148999). Originally posted by [slint6614-dev](https://github.com/slint6614-dev) on 2026-11-24T07:35:05Z. --- 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.
upstream-sync closed this pull request 2026-11-24 09:05:32 +00:00
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!448
No description provided.