Shaders: maximums and minimums order -0 below +0, as RDNA2 does #448
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!448
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/448/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCWhat'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 (#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 on GitHub · 2026-11-24 03:42 UTC · original
Verified
bf84134on Arch Linux x86_64 (rustc 1.98.1,cargo test -p victory-shader): 56 passed, 0 failed, 0 ignored.Not covered: the ordered
-0results on a real GPU (no SM 6.6 device here) and title shaders (no game data).@marquezinii on GitHub · 2026-11-24 05:15 UTC · original
Ran
bf84134merged ontomaind2a4bae(clean) on NVIDIA.valu_testsruns each case on the hardware device and on WARP.x86_64-pc-windows-msvc, debugmax_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 loweringcargo test -p victory-shader --lib -- --test-threads=1: 70 passed, 0 failedcargo test --workspace -- --test-threads=1: 265 passed, 0 failed, 4 ignored@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).victory-shadersuite: 70 passed, 0 failed on both GPUs.lower.rsreverted to main,max_and_min_order_negative_zero_below_positive_zerofails 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.