Shader tests: v_med3 orders -0 below +0 (f32 and f16) #1259
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!1259
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1259/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?
@pablogonz12 on GitHub · 2026-11-24 16:52 UTC · original
pablogonz12:up/med3→main· +53 −0 · 1 files · 5 commits · merged 2026-11-25 21:50 UTCWhat
A test only:
med3_orders_negative_zero_below_positive_zeropins the sign of zero fromv_med3_f32andv_med3_f16over nine cases each.This branch first also changed
lower::med3.mainhas since fixed that a different way (77053a0cbuilds the chain frommax_min_flushed), so thelower.rschange is dropped andlower.rsis identical tomain.Why
The ISA defines
v_med3overv_min/v_max, which order -0 below +0.mainalso hassemantics_tests::med3_orders_signed_zerosfor f32; what this test adds is the f16 form. (On the old FMin/FMax chain, a tester's run saw the f32 form fail on WARP: see the RX 6750 XT comment.) If the maintainers see it as redundant with the semantics test, closing is fine.How tested
At head, with
main'slower.rs, RX 9070 XT, Mesa RADV, vkd3d-proton under Wine (Linux):victory-shaderlib tests filtered onmed3: 5 passed, 0 failed (this test,med3_orders_signed_zeros, the two NaN tests and the integer-form test).cargo xwin clippy --workspace --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.Not tested
AI-assisted.
@adamdotpy on GitHub · 2026-11-24 17:13 UTC · original
Reviewed at head
5be1fe4(worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked:1. The claim holds, and the fix is exactly right — verified exhaustively, not just by the new test.
v_med3_f32isv_max_f32(v_min_f32(a,b), v_min_f32(v_max_f32(a,b), c)), and this codebase already models RDNA min/max as ordering -0 below +0 (max_and_min_order_negative_zero_below_positive_zero), so the median of three zeros is the majority sign. The emitted sequence (4-logic-op majority(a&b)|(c&(a|b)), mask0x8000_0000, select whenOEQ(median, +0)) computes precisely that. I modeled the emitted ops against the ISA semantics over all 2,744 triples of {±0, ±1, ±inf, ±subnormal, smallest subnormals, ±NaN, ±3.5} plus 200,000 random bit patterns (25-50% zeros): 0 mismatches, including the 596 zero-result cases. The key invariant — whenever the chain's median is a zero, the majority sign of the three inputs equals that zero's sign — holds for every case: all-zero triples, two zeros plus a positive/negative third, and one zero straddled by opposite signs.2. The NaN path is untouched. The zero-select sits before the existing NaN select, and
OEQis false for NaN, so a NaN median never takes the signed-zero; with any NaN input the final select still returnsleast = min(lo, c)as before. The model confirms this including negative NaNs (0xffc00000), whose sign bit could otherwise leak into the majority.3. Hardware evidence on this machine.
cargo test -p victory-shader --lib med3runs the newmed3_orders_negative_zero_below_positive_zeroon the GPU (f32 opcode0x157and f160x357, 7 cases each) plus both pre-existingv_med3_*_with_nantests: 3 passed / 0 failed. Fullcargo test -p victory-shader --lib: 121 passed / 0 failed / 1 ignored.v_med3_f16routes through the samemed3(lower.rs:4573), so one fix covers both widths — and the test pins both.4. Cost is reasonable and the comment's
max_minnote is accurate. The fix adds ~9 VALU ops on the med3 path (4 logic + 2 const + mask + cmp + select); reusingmax_minwould cost four min/max chains as the comment says.v_max3_f32/v_min3_f32also lower throughmed3(lower.rs:3995), so their zero-ordering gets the same majority treatment, consistent with the existing pair-ordering test.5. Checks:
cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warningsclean;cargo build --release -p victorybuilds;rustfmt --edition 2024 --checkon both changed files is fully clean (the PR also reformats three pre-existing fmt-unclean spots inlower.rs's test module — a small bonus, no behavior change).Nothing found that would block this. One observation, not a defect: DXIL's
FMin/FMaxgiving "either zero" is host-dependent, so the majority-sign select makes the emitted code deterministic regardless of which host compiled it — worth keeping exactly as is.@wwwwwwww3333333 on GitHub · 2026-11-24 18:37 UTC · original
Tested on AMD RDNA2 hardware — source-level analysis plus unit tests; not runtime-tested with game files.
Setup: Windows 11, RX 6750 XT (RDNA2), Rust 1.98.1 MSVC, base
ffe310e. Rancargo test -p victory-shader med3 -- --test-threads=1twice:lower.rs+ this PR's new test: on the RX 6750 XT all nine f32 cases already pass, so the AMD driver's FMin/FMax happen to order -0 below +0 here. WARP then fails:v_med3_f32(0x80000000, 0xbf800000, 0x00000000) = 0x00000000, want 0x80000000, i.e. med3(-0, -1, +0) gives +0. The panic stopped the run before the f16 half on main.lower.rs+ test: 3 passed (the new test, f32 and f16, plus both existing NaN tests) on both the RX 6750 XT and WARP.So the new test does catch the bug on main (on WARP), and the fix keeps RDNA2 hardware results unchanged while making the output the same on every host.
@Marvisak on GitHub · 2026-11-25 01:20 UTC · original · edited 2026-11-25 12:23 UTC
Automated triage for the maintainers.
Merge candidate. Thanks for making the change. The PR is now test-only:
med3_orders_negative_zero_below_positive_zeroincrates/victory-shader/src/emit/valu_tests.rs, with no change tolower.rs.It pins
main'sLower::med3(built onmax_min_flushed) on signed zeros for bothv_med3_f32(VOP30x157) andv_med3_f16(0x357). The expected values follow from the ISA's min/max chain with -0 ordered below +0, so the test is not just copying the lowering.mainalready hassemantics_tests::med3_orders_signed_zerosfor f32. The new part is the f16 form, and the WARP report in the thread shows this is a case where hosts differ.CI
checkpasses at7655829. Nit: a blank line is missing between the new test and the next doc comment.Generated by Claude Code
@pablogonz12 on GitHub · 2026-11-25 11:26 UTC · original
Done as suggested: the
lower.rschange is dropped (it is now identical tomain), only the test remains, and the title and description say so. Checked here withmain's lowering on an RX 9070 XT (RADV, vkd3d-proton, Wine): the 5med3tests pass, this one included. WARP is not available here.mainalso hassemantics_tests::med3_orders_signed_zerosfor f32; what this one adds is the f16 form.AI-assisted.