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

Merged
AlexWhitehouse merged 2 commits from upstream-pr/16889 into main 2026-11-14 05:14:17 +00:00
AlexWhitehouse commented 2026-11-07 20:36:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexWhitehouse on 2026-11-07T20:36:59Z.


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:

  • RDNA2 ISA §3.5, Table 4 and §12.10 opcode 16 (V_PK_MUL_F16);
  • LLVM llvmorg-19.1.0 VOP3PInstructions.td lines 95–99, where FPDPRounding = 1, so the instruction uses the mode's rounding;
  • the merged packed min/max precedent, #14793.

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 #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):

    • on #16739's head, the 12 directed mul variants fail with the throw above, and the 4 nearest-even ones and all 16 add variants pass;
    • with this PR, all 32 pass (16 mul and #16739's 16 add);
    • no VUID- lines and no missing-layer warning in either run.
  • 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):

    • on #16739's head, the same 12 directed mul variants fail with the throw, and the 4 nearest-even ones pass;
    • with this PR, all 32 pass;
    • no VUID- lines and no missing-layer warning in either run;
    • no compiler warnings in the changed files.
  • After rebasing onto main 527b309dd9 (#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.

    • #16739, which this PR builds on, merged on 2026-11-11, and the branch is rebased onto main after it.
    • #15719 (native packed NaN handling) conflicts in FloatInstructions.cpp, as it already does with #16739. Whoever merges second keeps both conditions in the native-path test (!directedAdd && !directedMul).
    • 21 PRs the tool couldn't compare touch other shader.md clauses or distant code. #14212 only reformats debt entries.

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 (#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

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5205617715 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16889). Originally posted by [AlexWhitehouse](https://github.com/AlexWhitehouse) on 2026-11-07T20:36:59Z. --- ### 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: - RDNA2 ISA §3.5, Table 4 and §12.10 opcode 16 (`V_PK_MUL_F16`); - LLVM llvmorg-19.1.0 [`VOP3PInstructions.td`](https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0/llvm/lib/Target/AMDGPU/VOP3PInstructions.td#L95-L99) lines 95–99, where `FPDPRounding = 1`, so the instruction uses the mode's rounding; - the merged packed min/max precedent, #14793. **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 #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`): - on #16739's head, the 12 directed mul variants fail with the throw above, and the 4 nearest-even ones and all 16 add variants pass; - with this PR, all 32 pass (16 mul and #16739's 16 add); - no `VUID-` lines and no missing-layer warning in either run. - **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): - on #16739's head, the same 12 directed mul variants fail with the throw, and the 4 nearest-even ones pass; - with this PR, all 32 pass; - no `VUID-` lines and no missing-layer warning in either run; - no compiler warnings in the changed files. - **After rebasing onto `main` `527b309dd9` (#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. - #16739, which this PR builds on, merged on 2026-11-11, and the branch is rebased onto `main` after it. - #15719 (native packed NaN handling) conflicts in `FloatInstructions.cpp`, as it already does with #16739. Whoever merges second keeps both conditions in the native-path test (`!directedAdd && !directedMul`). - 21 PRs the tool couldn't compare touch other `shader.md` clauses or distant code. #14212 only reformats debt entries. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none (#16739, which this builds on, merged on 2026-11-11; the branch now holds only this PR's two commits on `main`) - [x] Fixes: none - [x] Unblocks: none - [x] 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 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-07 22:13:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T22:13:02Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5815239114 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16889#pullrequestreview-5815239114). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T22:13:02Z. **Review:** COMMENTED --- 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.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
boykopovar/AnyPS5!16889
No description provided.