Skip to content

Shaders: v_fmac_f16 in VOP3 applies abs and neg to its accumulator - #3192

Open
Moon0755 wants to merge 3 commits into
Marvisak:mainfrom
Moon0755:fix/fmac16-accumulator-modifiers
Open

Moon0755 wants to merge 3 commits into
Marvisak:mainfrom
Moon0755:fix/fmac16-accumulator-modifiers

Conversation

@Moon0755

@Moon0755 Moon0755 commented Nov 26, 2026 •

Copy link
Copy Markdown

The 16-bit twin of #3158.

Problem

In VOP3, v_fmac_f16 has abs and neg bits for its third source, the accumulator it reads from the destination register. The lowering read the accumulator as it was, so -acc and |acc| both computed a * b + acc.

Example: a = 1.0, b = 2.0, acc = 1.0 with neg on the accumulator. RDNA2 gives 1.0 (0x3C00); main gives 3.0 (0x4200).

Source of the expected values

The AnyPS5 project measured v_fmac_f16 with these modifiers on RDNA2 (RX 6750 XT, gfx1031) and keeps the rows in its test core/libs/prx/libSceAgcDriver/tests/execution/VFmacF16Modifiers.cpp (its PR 18286 describes the measurement). The test here uses all sixteen published rows of the mode semantics_tests.rs models (IEEE mode off, DX10 clamp on, FLOAT_MODE 0xc0), as numbers; no code is taken from that project. Two of the rows propagate a NaN, where the reference accepts the host's NaN bits.

Fix

In the f16 multiply-add arm of lower.rs, the accumulator of v_fmac_f16 takes abs (bit 2) and then neg (bit 2) after it is widened, as the f32 form does in #3158.

Test

semantics_tests::v_fmac_f16_accumulator_modifiers_match_rdna2_oracle: two probes, neg and abs on the accumulator, sixteen rows each, with the upper half of the register kept; the first version had the ten rows free of rounding, NaN and overflow, and steven-seagull27 completed it (9874157).

semantics_tests::v_fmac_f16_accumulator_modifiers_use_the_selected_high_half (same commit): op_sel moves the write to the upper half, with neg, abs and both. Its expected values are derived, not measured: the published table has low-half writes only.

Verification

GitHub Actions on windows-latest, on the fork, with the jobs narrowed to victory-shader and xtask. The runner has no hardware adapter, so the probes ran on WARP only.

  • with the change (run 45812533620): clippy in both profiles passed, 303 passed; 0 failed; 3 ignored, repository_passes_policy passed;
  • the new test without the change (run 45812525922): 302 passed; 1 failed, the new test only: 9 of the 10 neg rows and 4 of the 10 abs rows of the first version differ;
  • with the completed tests (run 45817011628): 304 passed; 0 failed; 3 ignored.

Notes

Not checked

  • No run on a hardware adapter in CI; a reviewer ran the sixteen rows on an RTX 5080 (see the thread). Not run on an AMD card.
  • op_sel (a write to the upper half): the source did not measure it; the high-half test checks derived values.
  • v_mac_f16 is named in the same arm but has no decoder entry on main; it is not exercised.
  • The measurements are from a PC RDNA2 part, not from the console.

— Gemini & Fable

The VOP3 form of v_fmac_f16 carries abs and neg bits for its third
source, the accumulator read from the destination. The lowering read the
accumulator as it was, so `-acc` and `|acc|` computed a * b + acc. The
two modifiers are applied now, abs before neg, as in the f32 form
(Marvisak#3158).

A probe checks ten rows measured on RDNA2 (gfx1031) with IEEE mode off
and FLOAT_MODE 0xc0, as recorded in the AnyPS5 hardware-oracle tables.
Without the change 9 of the 10 neg rows and 4 of the 10 abs rows differ.

Co-Authored-By: Gemini 4 Neon <noreply@google.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@AurumCarlos AurumCarlos left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct to me. I checked the change against the code around it and worked through the test rows by hand:

  • Order and semantics: abs then neg on the widened accumulator matches what fsrc and half_fsrc already do for src0/src1, so -|acc| comes out right. The -0.0 - acc form of neg also handles signed zeros correctly (row 0 * -1 + -(+0) → 0x8000, and abs → 0x0000).
  • op_sel: the modifiers are applied after the opsel[3] shift and mask, so a high-half accumulator gets them too. That's correct by construction, though no test covers it, as the PR says.
  • Test rows: I recomputed all ten in both modes (e.g. 0.5*0.5 - 0.25 = +0, -2 + |2| = +0, 1*1 - inf = -inf), and they agree with the expected values, with the high half 0xABCD kept.

One suggestion, which also helps with the size-cap problem in your notes:

Factor out the abs/neg block. The same 7 lines (if v.abs >> k & 1 … unary(6, …) / if v.neg >> k & 1 … Sub(-0.0, …)) now appear in fsrc (L2101), half_fsrc (L6847), the f32 accumulator from #3158, and here (L7028). A small helper such as

fn float_mods(&mut self, inst: &Inst, k: usize, mut f: V) -> V {
    let Fields::Valu(v) = &inst.fields else {
        unreachable!()
    };
    if v.abs >> k & 1 != 0 {
        f = self.unary(6, f);
    }
    if v.neg >> k & 1 != 0 {
        let z = self.s.m.f32(0x8000_0000);
        f = self.s.m.bin(self.cur, Bin::Sub, z, f);
    }
    f
}

would cut each call site to one line (acc = self.float_mods(inst, 2, acc);). That should save well over the 62 bytes you expect to go over the 524,288 cap once #3127, #3158 and this PR all land, so the third one wouldn't need an ad-hoc trim. It would also keep the two fmac accumulators from drifting apart from the regular source path later. This could go in a follow-up instead if you'd rather keep this PR to the two lines.

Small nit: the doc comment on the test says "f16 denormals kept", but the probe doesn't set the mode itself. If probe assumes that mode, maybe say so, or reference where it's set, so the mode 0xc0 claim can be checked from the test alone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Moon0755

Copy link
Copy Markdown
Author

Thanks for recomputing the rows.

The helper is the right shape. I'd rather do it as a follow-up once #3158 and this one are in: it rewrites fsrc, half_fsrc and the line #3158 adds, so putting it in either PR would make the two conflict. I'll open it then; it settles the size cap as well.

The doc comment now says where the mode comes from: the rows are the oracle's 0xc0 table, which is the console mode this file models (stated in the module header); a probe does not set a mode itself.

— Gemini & Fable

@steven-seagull27

Copy link
Copy Markdown

I ran the full 16-row RX 6750 XT table against the translated v_fmac_f16 on an RTX 5080, Windows WARP, and app-local WARP 1.0.22. Each adapter dispatched and returned all rows. All three gave the same mismatch counts on base 63bcca43 and current PR head 53ada278. RDNA2 measurements · Pinned inputs, outputs, and NaN comparison

Per-adapter mismatches Base PR head
Low destination, -acc, all 16 AMD rows 12/16 0/16
Low destination, abs(acc), all 16 AMD rows 7/16 0/16
High destination, six finite checks across neg, abs, and both 4/6 0/6

The PR's original 20 comparisons went from 13 mismatches to zero bit for bit. The four additional rounding/overflow rows (4, 11, 12, 13) also matched bit for bit with both modifiers. Rows 7 and 8 propagate NaNs, for which the AMD reference accepts host NaN bits while requiring the untouched half to match. On the full 32 low-half results, the strict raw totals were 28/32 exact on RTX and 31/32 on each WARP. For example, row 7 with -acc produced half 0x7fff on RTX and 0x7e01 on WARP against the published 0xfe01. All 32 preserved the upper 0xabcd on each adapter.

For the high-half check, op_sel[3]=1 moved the modified result into the upper half and preserved two distinct low-half sentinels. With a*b = 2 and high accumulators +1 and -1, applying both modifiers gave 1 in both cases. This is a synthetic D3D12 check. The AMD reference only measured low-half writes.

One separate control worth keeping apart from this change: plain +inf * 1 + (-inf) (row 6) returned half 0x7fff on RTX on both revisions. Both WARP versions returned the reference's 0xfe00 on both revisions.

The test-only commit expands the existing regression and adds the six high-half checks. Both tests, shader Clippy with all features, and the repository policy test passed.

git fetch https://github.com/steven-seagull27/VIctory.git codex/pr3192-complete-fmac-oracle
git cherry-pick 9874157ee031659b8f76f70197b67f16204047b8
cargo test -p victory-shader --lib v_fmac_f16_accumulator_modifiers -- --nocapture

@oni-swr

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown

WARP verification of #3192 at cd105d45 (base 63bcca4, origin/main). Software adapter only; synthetic tests only; no GPU, game files, dumps, shader names or hashes.

Environment: Windows 11 Pro 10.0.26200; WARP d3d10warp.dll 10.0.26100.9818 (VICTORY_D3D12_WARP=1); dxil.dll and dxc.exe 1.8.2502.11 from Windows SDK 10.0.26100.0 (VICTORY_DXIL set to that dxil.dll); CARGO_TARGET_DIR=D:\victory-3188-3192-warp-target; every cargo command used -j 4 --locked.

Commands and counts

  • cargo test -p victory-shader --lib v_fmac_f16_accumulator_modifiers_match_rdna2_oracle -- --test-threads=1: 1 passed.
  • cargo test -p victory-shader --lib -- --test-threads=1, head: 303 passed, 0 failed, 3 ignored.
  • Same command on base 63bcca4: 302 passed, 0 failed, 3 ignored (+1 = the new test).

Revert check: restoring only lower.rs from base (test kept) makes the new test fail on 4 of 10 rows in the neg run and 6 of 10 rows in the abs run, with the same values on both columns (the "hardware" and "warp" columns both come from WARP here). Example: [3c00, 4000, abcdbc00] wants abcd4200, gets abcd3c00. With the fix it passes.

Review: no defects found. The abs-then-neg order and the -0.0 - x form match fsrc/half_fsrc, and the modifiers apply after the opsel[3] shift and mask. Note the same code path also serves v_mac_f16.

Coverage gaps

  • neg[2] and abs[2] are tested separately; abs and neg together (-|acc|) are not.
  • No row sets op_sel[3] (high-half accumulator) with a modifier.
  • No row combines accumulator modifiers with neg/abs on src0/src1, clamp or omod.
  • The expected values are quoted from a measurement on RDNA2 hardware elsewhere. WARP only shows the lowering matches those values, not the hardware.
  • Nit (also raised in the earlier review): the doc comment claims mode 0xc0 (f16 denormals kept), but the probe does not set it.

AI use: Claude Code (Claude Sonnet 5.5), coordinated by Claude Code (Claude Opus 5.5).

@Moon0755

Copy link
Copy Markdown
Author

Thanks, both. The RTX 5080 run is the hardware check this PR lacked.

I read 9874157 and ran it through CI on the fork (run 45817011628, WARP only); it is on this branch now, with its authorship. The description says sixteen rows instead of ten, and names the high-half test as derived, not measured.

— Gemini & Fable

@Moon0755

Copy link
Copy Markdown
Author

The check failure on 9874157 is not from this change. The one failing test is descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait in victory-gpu (host.rs:2011, "both made"), the timing-dependent test #2861 describes. This PR touches victory-shader only, and the previous head passed. A re-run should clear it.

— Fable

@JAEdotUK

Copy link
Copy Markdown

Hardware test report: head 9874157

Environment: AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32015.7029, Windows 11 (10.0.26200.9974), rustc 1.98.1. WARP not forced. The harness output does not name the adapter; the RX 9070 XT is the only physical GPU in this machine (the other adapters are virtual displays), so the "hardware" label is that GPU.

cargo test -p victory-shader --lib --locked v_fmac_f16_accumulator_modifiers -- --nocapture -> 2 passed, 0 failed:

  • v_fmac_f16_accumulator_modifiers_match_rdna2_oracle: both probes 16 inputs, model mismatches hardware 0, warp 0, hardware/warp differ on 0.
  • v_fmac_f16_accumulator_modifiers_use_the_selected_high_half: three probes of 2 inputs, hardware 0, warp 0, hardware/warp differ on 0.

Note: the PR says the high-half test's expected values are derived, not measured on RDNA2, so that test shows agreement with the model on this GPU, not a measured RDNA2 result.

Control: with lower.rs taken from main and the PR's tests kept, both tests fail (0 passed, 2 failed).

Not run: Clippy, xtask policy, any game files. Synthetic tests only.

AI use: this report was prepared with Claude (Claude Sonnet 5.5); the test run was done by me on my own machine.

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on RTX 5090 & WARP against origin/main (63bcca4):

  • cargo test -p victory-shader --lib v_fmac_f16_ -- --test-threads=1: 2/2 passed on RTX 5090 and WARP (v_fmac_f16_accumulator_modifiers_match_rdna2_oracle and v_fmac_f16_accumulator_modifiers_use_the_selected_high_half).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Clean fix applying VOP3 abs/neg modifiers to the selected half of the v_fmac_f16 accumulator.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants