Repository navigation
Shaders: maximums and minimums order -0 below +0, as RDNA2 does - #448
Merged
Merged
Conversation
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.
|
Verified Not covered: the ordered |
Contributor
|
Ran
|
Closed
|
AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and the Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the D3D12 device for
|
Open
suirentakamura
referenced
this pull request
in suirentakamura/victory-suiren
Nov 24, 2026
Shaders: maximums and minimums order -0 below +0, as RDNA2 does
This was referenced Nov 24, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's wrong
RDNA2's float maximum and minimum treat -0 as smaller than +0. Whatever the operand order,
max(-0, +0) = +0andmin(-0, +0) = -0:V_MAX_F32pseudocode handles the two zeros explicitly;fmaximumnum/fminimumnum, which order -0 below +0, directly toV_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)whenxis -0: the hardware gives +0, and a followingv_rsq_f32gives +∞. We can give -0, and then -∞.What the fix does
A new helper,
max_min, used at all those call sites:FMax/FMinas before for every pair of operands except two zeros;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) runsv_max_f32,v_min_f32,v_max3_f32andv_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 --workspaceon GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml(the runner has a hardware D3D12 device as well as WARP):victory-shaderhas 70 passed, the other crates have 0 failed.main:hardware: v_max_f32 [+0, -0]gives0x80000000(-0) instead of0(+0).--test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (Tests: victory-shader tests crash (0xC0000005) when several tests use WARP devices in parallel #181, Shaders: share and serialize WARP host operations across threads #244).Not tested: the f16 and packed forms on a GPU (they go through the same helper, in f32), and a title shader.