Skip to content

feat(shader): allow directed rounding modes for packed f16 multiply - #16889

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
AlexWhitehouse:feat/shader-pk-mul-f16-directed-rounding
Nov 14, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
AlexWhitehouse:feat/shader-pk-mul-f16-directed-rounding

Conversation

@AlexWhitehouse

@AlexWhitehouse AlexWhitehouse commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

What

v_pk_mul_f16 threw packed f16 at pc … in FLOAT_MODE … (f16 rounding not to nearest even) is not implemented whenever the shader's FLOAT_MODE selected a directed f16 rounding (toward +∞, −∞ or zero). My #16739 lifted that guard for v_pk_add_f16; this PR does the same for packed multiply, with the same design, and builds on #16739.

Sources:

How it works:

  • Each half's product is computed in f32, which is exact for two finite halves: 11-bit significands, and exponents well inside f32's range, subnormals included.
  • The product is then rounded once to f16 in the mode's direction, through the v_interp_p2_f16 interpolation arithmetic feat(shader): allow directed rounding modes for packed f16 add #16739 uses (FPInterpolateF16 with a factor of 1 and a zero addend of the product's sign).
  • With f16 result denormals flushed, tininess is decided from the exact product rounded to 11 significant bits with an unbounded exponent, as the scalar f16 multiply entry records. A product just below 2^-14 that rounds away from zero to 2^-14 is kept.
  • The native packed path is disabled for directed multiply, as for directed add.
  • Lane selection, neg_lo/neg_hi, clamp, FP16_OVFL and the NaN rules are unchanged.
  • Nearest-even multiply is translated exactly as before.

Debt: shader.md now lists v_pk_mul_f16 with v_pk_add_f16 and v_pk_min/max_f16 as the packed ops that accept directed rounding. It records that its four encodings match RDNA2 (gfx1036) in every mode on the test's rows, and that gfx1013 is not measured, the same way #16739 records packed add. Only those two entries changed.

Tested

  • New test: agc_driver_packed_mul_f16_rounding_*, 16 variants: wave32 and wave64 × rounding 0–3 × native and forced integer paths. Each variant compares 4,096 words against an independent integer significand-and-exponent reference. They cover:

    • every f16 denormal mode; IEEE, DX10_CLAMP and FP16_OVFL;
    • op_sel/op_sel_hi lane selections, negation and clamp;
    • negative products, overflow to ∞ or 65504 by mode, subnormal and underflow boundaries;
    • signed zeros, infinities and NaNs.

    The key case: 0x3c01 × 0x3c01 gives 0x3c02 to nearest even and 0x3c03 toward +∞.

  • Linux x86-64 (Ubuntu 26.04, GCC 15, Release, lavapipe / Mesa 26.0.8, with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation):

  • Windows 11, RTX 3060: (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release, NVIDIA driver 610.88, Vulkan SDK 1.4.363, same validation environment, with the Khronos validation layer confirmed loaded):

  • After rebasing onto main 527b309dd9 (feat(shader): allow directed rounding modes for packed f16 add #16739 merged): on lavapipe (llvmpipe, LLVM 21.1.8) with the same validation environment, all 34 packed add and multiply tests pass with no VUID- lines and no missing-layer warning; with main's FloatInstructions.cpp the 12 directed multiply variants fail with the throw again.

  • RDNA2 hardware reference (run by Nawid3333 on this PR with tools/hw-oracle, gfx1036, AMD Windows driver 32.0.21045.16010, HIP, wave32): the test's four v_pk_mul_f16 encodings in all 128 combinations of f16 round, f16 denormal, IEEE, DX10_CLAMP and FP16_OVFL give 8,192 of 8,192 results equal to the test's expected values.

  • Other results posted on this PR: the 34 tests pass on an NVIDIA Turing laptop GPU and an Intel iGPU (gabichuuuuu) and on an AMD RX 7800 XT (DonRyuzaki). An RDNA 3.5 measurement on gfx1151 with tools/hw-oracle and an independent exact-fraction reference matches in all 73,728 lane results of each of the four forms, in every f16 round, f16 denormal, IEEE and FP16_OVFL mode (Jirka135).

  • Mutation check: forcing nearest-even in the directed multiply fails with actual=bc023c02 expected=bc023c03.

  • python3 tools/check_conventions.py --base origin/main: no errors. The two warnings are about the v_fma_mixlo_f16 and packed-NaN entries, which were already over 1,000 characters on main; this PR only adds v_pk_mul_f16 to one clause in each.

  • python3 tools/pr_overlap.py: no other PR implements directed packed multiply.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none (feat(shader): allow directed rounding modes for packed f16 add #16739, which this builds on, merged on 2026-11-11; the branch now holds only this PR's two commits on main)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3060 (driver 610.88, Windows 11 26300) and lavapipe (Mesa 26.0.8, Ubuntu 26.04), with the Khronos validation layer
  • AI-assisted: yes

🤖 Generated with Claude Code

@github-actions github-actions Bot added build shader core/shader docs agc libSceAgc* labels Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 15719 core/shader/recompiler/Translation/src/FloatInstructions.cpp L64

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5371 docs/dev/debt/shader.md
PR 6235 docs/dev/debt/shader.md
PR 6280 docs/dev/debt/shader.md
PR 6412 docs/dev/debt/shader.md
PR 6488 docs/dev/debt/shader.md
PR 7085 docs/dev/debt/shader.md
PR 7684 docs/dev/debt/shader.md
PR 8004 docs/dev/debt/shader.md
PR 9335 docs/dev/debt/shader.md
PR 9387 docs/dev/debt/shader.md
PR 10652 docs/dev/debt/shader.md
PR 11483 docs/dev/debt/shader.md
PR 12790 docs/dev/debt/shader.md
PR 12944 docs/dev/debt/shader.md
PR 14217 docs/dev/debt/shader.md
PR 14218 docs/dev/debt/shader.md
PR 15548 docs/dev/debt/shader.md
PR 17611 docs/dev/debt/shader.md
PR 17701 docs/dev/debt/shader.md

@gabichuuuuu gabichuuuuu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The Intel and Turing cells. Head 06c15ccb9e (stacked on #16739's two commits) merged onto main 302864d48c (clean). Windows 11 x64, WinLibs GCC 15.2.0 r7, Release, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ctest -R '^agc_driver_packed_(add|mul)_f16_rounding', 3 runs per GPU.

GPU result
NVIDIA laptop GPU (Turing), driver 610 34 of 34 pass (17 mul, 17 add), 3 of 3 runs
Intel iGPU, same machine 34 of 34 pass, 3 of 3 runs

So the new multiply rows, every rounding mode in wave32 and wave64, integer and float, give the expected bits on both drivers, and #16739's add rows still pass with this on top. Not run with VK_LAYER_KHRONOS_validation.

AI-assisted: yes.

@AlexWhitehouse

Copy link
Copy Markdown
Contributor Author

Thanks for the Turing and Intel runs. The 17th test per op is the default no-argument registration every tests/execution source gets (agc_driver_packed_mul_f16_rounding and agc_driver_packed_add_f16_rounding), which repeats the wave32, nearest-even, native-path case, so 34 matches the 16 variants per op in the description.

@DonRyuzaki

Copy link
Copy Markdown
Contributor

Independent Windows/AMD GPU result for exact head 06c15ccb9eade52e95556aeaae90d1c93802fc7e (base dd4dee69710cec8b698bcd7b8949c9e43d25d1d7). I built the packed f16 multiply test and its #16739 add dependency in Release with MinGW GCC 15.2 and two build jobs. With ANYPS5_GPU=AMD, ANYPS5_REQUIRE_VULKAN=1, and ANYPS5_NO_SHADER_CACHE=1, ctest -R '^agc_driver_packed_(mul|add)_f16_rounding' -V --repeat until-fail:2 passed 34/34 cases twice (68 passes). Every verbose run selected the physical AMD Radeon RX 7800 XT (1002:747e, subgroup 64), not a no-GPU skip. This covers Wave32/Wave64, all four rounding modes and integer/float variants for both operations. As @AlexWhitehouse clarified, the 17th registration per operation repeats the default case.

@gabichuuuuu already supplied Turing and Intel Windows results on this head; this adds RDNA3 Windows coverage. I did not run a title, compare against main on this GPU, use Vulkan validation layers or measure performance. The checkout had no tracked source edits; two pre-existing untracked third-party directories remained. Hosted Build is green. AI-assisted: yes (Codex).

@Nawid3333

Copy link
Copy Markdown
Contributor

A check of the semantics on RDNA2 hardware, independent of the recompiler. The four v_pk_mul_f16 instructions of PackedMulF16Rounding.cpp at 06c15ccb9e (plain, op_sel, neg_lo/neg_hi, clamp) ran natively on a Radeon iGPU (gfx1036, AMD Windows driver 32.0.21045.16010) through tools/hw-oracle with the HIP runtime, wave32, with the 16 Rows in v4/v5.

Modes: FLOAT_MODE was set the way the test sets it, through the oracle's flags: --round16 0-3, --denorm16 0-3, --denorm32 3, --ieee 0/1, --dx10-clamp 0/1 and --fp16-overflow 0/1, so 128 modes.

Reference: each result was compared with the test's own expected value. Multiply() and the expected-value block were copied verbatim into a small C++ program, with no port.

Result: 8,192 of 8,192 packed results equal the expected values. That includes:

  • 0x3c01 * 0x3c01 toward +infinity;
  • the subnormal and underflow rows in every denorm mode;
  • overflow to infinity or 65504 per mode;
  • infinity times zero;
  • the NaN rows with IEEE 0 and 1.

So the reference the test pins matches the hardware instruction on gfx1036. gfx1013 itself is not measured.

AI-assisted: yes.

@AlexWhitehouse
AlexWhitehouse force-pushed the feat/shader-pk-mul-f16-directed-rounding branch from 06c15cc to f58d6d9 Compare November 10, 2026 01:47
@AlexWhitehouse

Copy link
Copy Markdown
Contributor Author

Thanks for the gfx1036 run. It is now recorded in docs/dev/debt/shader.md in f58d6d9, the same way #16739 records packed add, with gfx1013 still listed as unmeasured. The branch is also rebased onto #16739's new head f719df5; that base change touches only shader.md, so the code and test are unchanged.

AlexWhitehouse and others added 2 commits November 11, 2026 05:10
Round exact half products once through the interpolation arithmetic, following RDNA2 ISA section 12.10 and LLVM 19.1.0 VOP3PInstructions.td. Cover rounding, tininess, lane selections, modifiers and native fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tiply

The four v_pk_mul_f16 encodings of PackedMulF16Rounding.cpp match the
test's reference on gfx1036 in every f16 round, f16 denormal, IEEE,
DX10_CLAMP and FP16_OVFL mode, as measured with tools/hw-oracle on the
pull request. gfx1013 remains unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AlexWhitehouse
AlexWhitehouse force-pushed the feat/shader-pk-mul-f16-directed-rounding branch from 236b0fb to b5cf88e Compare November 11, 2026 05:22
@AlexWhitehouse

Copy link
Copy Markdown
Contributor Author

Rebased onto main now that #16739 has merged, so the branch is only this PR's two commits on 527b309 and Depends on is none. The 34 packed add and multiply rounding tests pass on lavapipe with the Khronos validation layer and no VUID errors, and the 12 directed multiply variants still fail with main's FloatInstructions.cpp.

@Jirka135

Copy link
Copy Markdown
Contributor

An RDNA 3.5 data point for the semantics this PR implements for v_pk_mul_f16 with a directed f16 rounding mode, measured with tools/hw-oracle (HIP runtime). It's independent of the recompiler. The comments have a gfx1036 run; I did not find a gfx11 one. The PS5's gfx1013 is not measured.

Setup:

  • AMD Radeon 8060S (Ryzen AI MAX+ 395, gfx1151), Windows 11 Pro 10.0.26300, driver 32.0.22032.6002
  • oracle from tools/hw-oracle at f57c971, wave32
  • all settings explicit, in every combination: round16 0 to 3 (nearest even, +infinity, −infinity, zero) × denorm16 0 to 3 × IEEE 0 and 1 × FP16_OVFL 0 and 1, which is 64 modes per form. denorm32 3, round32 0 and DX10_CLAMP 1 throughout.

Rows: 576 per mode and form, two results each.

  • 480 rows whose lanes mix edge halves (±0, the smallest denormal, 0x03ff, the smallest normal and its neighbour, ±1, 1 + 1 ulp, 1 − 1 ulp, ±65504, ±infinity, a quiet NaN, signalling NaNs, 0.5, 2, 100, about 1/3) with random halves and halves within a few ulps of 2^-14, 2^-13, 2^-11, 2^-9, 2^-7 and 2^-5
  • 96 rows built so the exact product lies within two ulps of 2^-14: a in 0x2000 to 0x3bfe, b the f16 nearest to 2^-14 / a, ±2 ulps, in both lanes

Forms:

  • v_pk_mul_f16 v10, v4, v5
  • op_sel:[1,1] op_sel_hi:[0,0] (the two results swap their sources)
  • neg_lo:[1,0] neg_hi:[0,1]
  • clamp

Reference. Written separately in Python with exact fractions, not copied from the PR's test:

  • each result is the exact product of the (flushed, if the denorm field says so) inputs, rounded once to f16 in the mode's direction
  • with f16 output flushing (denorm16 0 and 1), the product is rounded with an unbounded exponent and then flushed to a zero of its sign if it is below 2^-14, as the description says
  • infinity times 0 and any NaN input give a NaN, compared as a NaN class (not by payload)
  • overflow gives infinity or ±65504 by the mode and sign, and ±65504 whenever FP16_OVFL is 1
  • clamp: NaN to 0, results ≤ 0 to +0, results ≥ 1 to 1.0

Results:

form lane results equal to the reference modes with a mismatch
plain 73,728 of 73,728 0 of 64
op_sel swap 73,728 of 73,728 0 of 64
neg_lo / neg_hi 73,728 of 73,728 0 of 64
clamp 73,728 of 73,728 0 of 64

The rows reach the interesting cases. In one mode (round16 1, denorm16 0, IEEE 0) a re-run counted, of 1,034 lanes:

  • 256 lanes with a non-zero product between 0 and 2^-14 flushed to a signed zero
  • 20 lanes whose exact product is below 2^-14 but rounds up to 0x0400, which is kept
  • 9 lanes that overflow to +infinity
  • 118 NaN lanes

So gfx1151 agrees with the semantics in the description in every mode and form measured: a single rounding per half in the mode's direction, tininess decided after rounding with an unbounded exponent when f16 results are flushed, 0x3c01 * 0x3c01 rounding as the mode says, and op_sel, neg_lo/neg_hi and clamp working as for the other packed ops.

Not covered:

  • NaN payloads: only the NaN class is compared
  • clamp with DX10_CLAMP 0
  • the PS5's gfx1013 is not measured

Not run: the recompiler side (no Windows build of the runner here), and the PR's tests.

AI-assisted: yes (measurement scripted with Claude Code; rows and results checked by me).

@adriadam10 adriadam10 self-assigned this Nov 11, 2026
@adriadam10
adriadam10 merged commit 6f6d066 into boykopovar:main Nov 14, 2026
18 of 19 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the directed-multiply lowering in packedFloat16 (exact f32 product, rounded once through FPInterpolateF16, tiny-product threshold per direction, native path off) against the test's integer reference, the debt entries, the unrelated stub removal main made nearby (still merges cleanly), and the RDNA2 (gfx1036, @Nawid3333) and RDNA 3.5 (gfx1151, @Jirka135) oracle runs plus the NVIDIA, Intel and AMD runs in the thread; merged.

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

Labels

agc libSceAgc* build docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants