Shader tests: v_med3 orders -0 below +0 (f32 and f16) #1259

Manually merged
github merged 5 commits from gh-pr/1259/head into gh-pr/1259/base 2026-11-26 04:25:25 +00:00
Owner

@⁠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 UTC

What

A test only: med3_orders_negative_zero_below_positive_zero pins the sign of zero from v_med3_f32 and v_med3_f16 over nine cases each.

This branch first also changed lower::med3. main has since fixed that a different way (77053a0c builds the chain from max_min_flushed), so the lower.rs change is dropped and lower.rs is identical to main.

Why

The ISA defines v_med3 over v_min/v_max, which order -0 below +0. main also has semantics_tests::med3_orders_signed_zeros for 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's lower.rs, RX 9070 XT, Mesa RADV, vkd3d-proton under Wine (Linux):

  • victory-shader lib tests filtered on med3: 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

  • WARP (not available under Wine), NVIDIA, native Windows.

AI-assisted.

**@⁠pablogonz12** on GitHub · 2026-11-24 16:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/1259) `pablogonz12:up/med3` → `main` · +53 −0 · 1 files · 5 commits · merged 2026-11-25 21:50 UTC ## What A test only: `med3_orders_negative_zero_below_positive_zero` pins the sign of zero from `v_med3_f32` and `v_med3_f16` over nine cases each. This branch first also changed `lower::med3`. `main` has since fixed that a different way (77053a0c builds the chain from `max_min_flushed`), so the `lower.rs` change is dropped and `lower.rs` is identical to `main`. ## Why The ISA defines `v_med3` over `v_min`/`v_max`, which order -0 below +0. `main` also has `semantics_tests::med3_orders_signed_zeros` for 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`'s `lower.rs`, RX 9070 XT, Mesa RADV, vkd3d-proton under Wine (Linux): - `victory-shader` lib tests filtered on `med3`: 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 - WARP (not available under Wine), NVIDIA, native Windows. AI-assisted.
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 ivall/xcl#1348 (DavidM1911).

Tests: hardware execution tests in valu_tests for both operand orders and
the halves.

Written with an AI assistant.
main's max_min_flushed chain (77053a0c) already orders -0 below +0, so the
majority-sign select this branch added gives the same result.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:25:24 +00:00
github manually merged commit 76558298e6 into gh-pr/1259/base 2026-11-26 04:25:25 +00:00
Author
Owner

@⁠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_f32 is v_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)), mask 0x8000_0000, select when OEQ(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 OEQ is false for NaN, so a NaN median never takes the signed-zero; with any NaN input the final select still returns least = 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 med3 runs the new med3_orders_negative_zero_below_positive_zero on the GPU (f32 opcode 0x157 and f16 0x357, 7 cases each) plus both pre-existing v_med3_*_with_nan tests: 3 passed / 0 failed. Full cargo test -p victory-shader --lib: 121 passed / 0 failed / 1 ignored. v_med3_f16 routes through the same med3 (lower.rs:4573), so one fix covers both widths — and the test pins both.

4. Cost is reasonable and the comment's max_min note is accurate. The fix adds ~9 VALU ops on the med3 path (4 logic + 2 const + mask + cmp + select); reusing max_min would cost four min/max chains as the comment says. v_max3_f32/v_min3_f32 also lower through med3 (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 warnings clean; cargo build --release -p victory builds; rustfmt --edition 2024 --check on both changed files is fully clean (the PR also reformats three pre-existing fmt-unclean spots in lower.rs's test module — a small bonus, no behavior change).

Nothing found that would block this. One observation, not a defect: DXIL's FMin/FMax giving "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.

**@⁠adamdotpy** on GitHub · 2026-11-24 17:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/1259#issuecomment-7138865778) 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_f32` is `v_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))`, mask `0x8000_0000`, select when `OEQ(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 `OEQ` is false for NaN, so a NaN median never takes the signed-zero; with any NaN input the final select still returns `least = 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 med3` runs the new `med3_orders_negative_zero_below_positive_zero` on the GPU (f32 opcode `0x157` and f16 `0x357`, 7 cases each) plus both pre-existing `v_med3_*_with_nan` tests: 3 passed / 0 failed. Full `cargo test -p victory-shader --lib`: 121 passed / 0 failed / 1 ignored. `v_med3_f16` routes through the same `med3` (`lower.rs:4573`), so one fix covers both widths — and the test pins both. **4. Cost is reasonable and the comment's `max_min` note is accurate.** The fix adds ~9 VALU ops on the med3 path (4 logic + 2 const + mask + cmp + select); reusing `max_min` would cost four min/max chains as the comment says. `v_max3_f32`/`v_min3_f32` also lower through `med3` (`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 warnings` clean; `cargo build --release -p victory` builds; `rustfmt --edition 2024 --check` on both changed files is fully clean (the PR also reformats three pre-existing fmt-unclean spots in `lower.rs`'s test module — a small bonus, no behavior change). Nothing found that would block this. One observation, not a defect: DXIL's `FMin`/`FMax` giving "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.
Author
Owner

@⁠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. Ran cargo test -p victory-shader med3 -- --test-threads=1 twice:

  • main 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.
  • this PR's 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.

**@⁠wwwwwwww3333333** on GitHub · 2026-11-24 18:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/1259#issuecomment-7140900098) 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. Ran `cargo test -p victory-shader med3 -- --test-threads=1` twice: - **main `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. - **this PR's `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.
Author
Owner

@⁠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_zero in crates/victory-shader/src/emit/valu_tests.rs, with no change to lower.rs.

It pins main's Lower::med3 (built on max_min_flushed) on signed zeros for both v_med3_f32 (VOP3 0x157) and v_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. main already has semantics_tests::med3_orders_signed_zeros for f32. The new part is the f16 form, and the WARP report in the thread shows this is a case where hosts differ.

CI check passes at 7655829. Nit: a blank line is missing between the new test and the next doc comment.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/1259#issuecomment-7149442746) · edited 2026-11-25 12:23 UTC <!-- victory-triage v1 head=76558298e63af2bd8493e43875d9606d2296c49e verdict=merge-candidate at=2026-11-25T12:42:00Z --> Automated triage for the maintainers. **Merge candidate.** Thanks for making the change. The PR is now test-only: `med3_orders_negative_zero_below_positive_zero` in `crates/victory-shader/src/emit/valu_tests.rs`, with no change to `lower.rs`. It pins `main`'s `Lower::med3` (built on `max_min_flushed`) on signed zeros for both `v_med3_f32` (VOP3 `0x157`) and `v_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. `main` already has `semantics_tests::med3_orders_signed_zeros` for f32. The new part is the f16 form, and the WARP report in the thread shows this is a case where hosts differ. CI `check` passes at `7655829`. Nit: a blank line is missing between the new test and the next doc comment. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠pablogonz12 on GitHub · 2026-11-25 11:26 UTC · original

Done as suggested: the lower.rs change is dropped (it is now identical to main), only the test remains, and the title and description say so. Checked here with main's lowering on an RX 9070 XT (RADV, vkd3d-proton, Wine): the 5 med3 tests pass, this one included. WARP is not available here. main also has semantics_tests::med3_orders_signed_zeros for f32; what this one adds is the f16 form.

AI-assisted.

**@⁠pablogonz12** on GitHub · 2026-11-25 11:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1259#issuecomment-7161054168) Done as suggested: the `lower.rs` change is dropped (it is now identical to `main`), only the test remains, and the title and description say so. Checked here with `main`'s lowering on an RX 9070 XT (RADV, vkd3d-proton, Wine): the 5 `med3` tests pass, this one included. WARP is not available here. `main` also has `semantics_tests::med3_orders_signed_zeros` for f32; what this one adds is the f16 form. AI-assisted.
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!1259
No description provided.