fix(shader): round scalar f16 arithmetic in directed rounding modes #19505

Closed
Ab1jithK wants to merge 2 commits from upstream-pr/19505 into main
Ab1jithK commented 2026-11-13 12:35:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ab1jithK on 2026-11-13T12:35:18Z.


What

Scalar v_add_f16, v_sub_f16, v_subrev_f16, v_mul_f16, v_fma_f16, v_fmac_f16, v_fmamk_f16 and v_fmaak_f16 now follow the f16 round field toward +inf, -inf and zero, declared in FLOAT_MODE or set by s_round_mode. On main they are Unmodelled in RoundingUseOf, so a directed FLOAT_MODE declared from the start passed checkRoundingUse (nothing changed) and they rounded to nearest even without an error, as reported on #981 (for example v_add_f16 0x3c00, 0x1000 toward +inf: hardware 0x3c01, recompiler 0x3c00).

Sources: the RDNA2 ISA §3.5 Table 4 (the f16 round field) and the instruction definitions; the edge behaviour below was measured with tools/hw-oracle on RDNA2 (gfx1036), since the ISA does not settle tininess, NaN choice or signed zeros under directed rounding. The PS5's gfx1013 is not measured.

How it rounds (Translation/src/FloatInstructions.cpp):

  • The exact product-sum is rounded once in the selected mode through FPInterpolateF16, as v_pk_add_f16 (#16739) and v_fma_mix*_f16 do: add as a * 1 + b, the subtrahend negated in its own position (v_subrev_f16 is -src0 + src1), multiply as a * b + 0 with the product's sign on a zero result.
  • With bit 1 of the f16 denormal field clear, the result is flushed to its signed zero when the exact value rounded to 11 significant bits with an unbounded exponent is below 2^-14 (tininess after rounding). It is computed on the same operation scaled by 2^10: 0x0400 * 0x9000 + 0x0400 toward +inf flushes to 0x0000, while 0x3c01 * 0x03ff rounds to 0x0400 and is kept. Bit 0 flushes inputs as before.
  • NaN results are chosen on the half bits, never carried in a float: the first NaN source in operand order after its modifiers (the subtrahend negated), quieted only with IEEE=1, or the default NaN 0xfe00 for an invalid operation, an invalid product (inf * 0) winning over a NaN src2. On the RTX 3060 a float negate of a NaN gives 0x7fff, so the subtrahend's sign is applied to its raw half bits.
  • RoundingUseOf (RoundingMode.cpp) moves the eight instructions from Unmodelled to Directed16, so a run-time s_round_mode before them translates like the declared mode instead of throwing. Nearest-even keeps its existing lowering.

Tests:

  • New agc_driver_f16_directed_rounding (tests/execution/F16DirectedRounding.cpp): one kernel runs all eight instructions on 1024 lanes (every pair of 32 halves, a varying src2), for each directed mode, every f16 denormal field, IEEE=0/1 and FP16_OVFL=0/1, wave32 and wave64, with and without the Float64 capability (12 CTest cases). Expected values come from an exact reference (__int128 product-sum, rounded and flushed as above) that is checked against 19 rows pinned from the gfx1036 measurements. The same reference matches all 997,888 measured hardware rows below.
  • agc_shader_mode_register: the unmodelled check now uses v_fract_f16 (still unmodelled), and a new directedF16Arithmetic check requires a run-time s_round_mode before v_add_f16 to translate like the declared mode.

Overlap: #18487 adds v_cvt_f32_f64 to RoundingUseOf next to the lines moved here, a small textual conflict for whichever merges second. Not changed here: nearest-even keeps the existing lowering, including its known IEEE=0 signaling-NaN quieting, the v_subrev_f16 NaN order and the native f16 path's signaling NaNs (each also on main); f32 directed rounding (v_add/mul/fma_f32) stays as on main and is a separate change.

Tested

Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON. Head d0f305089c, rebased on main 80b0bdf264 (the only conflict was the TranslationContext.hpp declaration next to #9889's floatUnaryValue); the hardware comparison, full-suite run and base-vs-change validation below were made on the previous base 0294cba8dc.

  • Hardware comparison (tools/hw-oracle, HIP runtime vs HW_ORACLE_RUNTIME=vulkan with agc_oracle_runner, same GPU) on AMD Radeon Graphics 1002:164e (gfx1036): the eight instructions over 961 operand pairs (4913 triples for v_fma_f16/v_fmac_f16) in every f16 round and denormal field, at (IEEE, DX10, FP16_OVFL) = (1,0,0), (0,0,0), (1,0,1), (0,1,1): every directed-mode row matches. The remaining differences are all nearest-even and identical on main.
  • ctest -R "^agc_shader_mode_register$|^agc_driver_f16_directed_rounding" with ANYPS5_REQUIRE_VULKAN=1: 14/14 on both GPUs. Negative control: with main's recompiler sources and this test, 12/12 directed cases fail on both GPUs (e.g. 0 - 0 toward -inf gives 0x0000, expected 0x8000).
  • Full ^agc|^shader|recompiler run (1683 tests) on both GPUs: only agc_driver_sample_order_grid_reference fails on the Radeon, as on main (#9507, fixed by #9503).
  • Validation layer (Vulkan SDK 1.4.363.0, at least header version 335): the 83 focused tests matching ^agc.*(f16|float16|_rounding)|^agc_shader_mode_register$ with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, ctest --test-dir build -R <pattern> -V, on the base 0294cba8dc and on this change, on both GPUs. Every log shows Insert instance layer "VK_LAYER_KHRONOS_validation" for each of the 77 tests that create a Vulkan instance (the other 6 are SPIR-V codegen tests that select no GPU) and no was not found but was requested line. Base 69/83 (the 13 new directed-rounding cases and agc_shader_mode_register fail), this change 83/83; no VUID- message on either side. After the rebase, the same focused pattern (now 100 tests, including #16889's agc_driver_packed_mul_f16_rounding) passes 100/100 on both GPUs with the layer inserted for the 94 tests that open a device and no VUID- message.
  • tools/check_conventions.py --base origin/main: 0 errors; the one warning is the existing length of the nearest-even f16 denormal entry (1827 characters on main), where this PR changes one sentence.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon Graphics (Ryzen 7 7700X integrated, RDNA2 gfx1036, 1002:164e), AMD Windows driver 32.0.21042.62, Windows 11; NVIDIA GeForce RTX 3060 (10de:2504), driver 32.0.16.2101, Windows 11
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5295578211 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19505). Originally posted by [Ab1jithK](https://github.com/Ab1jithK) on 2026-11-13T12:35:18Z. --- ### What Scalar `v_add_f16`, `v_sub_f16`, `v_subrev_f16`, `v_mul_f16`, `v_fma_f16`, `v_fmac_f16`, `v_fmamk_f16` and `v_fmaak_f16` now follow the f16 round field toward +inf, -inf and zero, declared in `FLOAT_MODE` or set by `s_round_mode`. On `main` they are `Unmodelled` in `RoundingUseOf`, so a directed `FLOAT_MODE` declared from the start passed `checkRoundingUse` (nothing *changed*) and they rounded to nearest even without an error, as reported on #981 (for example `v_add_f16 0x3c00, 0x1000` toward +inf: hardware `0x3c01`, recompiler `0x3c00`). **Sources:** the [RDNA2 ISA](https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf) §3.5 Table 4 (the f16 round field) and the instruction definitions; the edge behaviour below was measured with `tools/hw-oracle` on RDNA2 (gfx1036), since the ISA does not settle tininess, NaN choice or signed zeros under directed rounding. The PS5's gfx1013 is not measured. **How it rounds** (`Translation/src/FloatInstructions.cpp`): - The exact product-sum is rounded once in the selected mode through `FPInterpolateF16`, as `v_pk_add_f16` (#16739) and `v_fma_mix*_f16` do: add as `a * 1 + b`, the subtrahend negated in its own position (`v_subrev_f16` is `-src0 + src1`), multiply as `a * b + 0` with the product's sign on a zero result. - With bit 1 of the f16 denormal field clear, the result is flushed to its signed zero when the exact value rounded to 11 significant bits with an unbounded exponent is below 2^-14 (tininess after rounding). It is computed on the same operation scaled by 2^10: `0x0400 * 0x9000 + 0x0400` toward +inf flushes to `0x0000`, while `0x3c01 * 0x03ff` rounds to `0x0400` and is kept. Bit 0 flushes inputs as before. - NaN results are chosen on the half bits, never carried in a float: the first NaN source in operand order after its modifiers (the subtrahend negated), quieted only with IEEE=1, or the default NaN `0xfe00` for an invalid operation, an invalid product (`inf * 0`) winning over a NaN src2. On the RTX 3060 a float negate of a NaN gives `0x7fff`, so the subtrahend's sign is applied to its raw half bits. - `RoundingUseOf` (`RoundingMode.cpp`) moves the eight instructions from `Unmodelled` to `Directed16`, so a run-time `s_round_mode` before them translates like the declared mode instead of throwing. Nearest-even keeps its existing lowering. **Tests:** - New `agc_driver_f16_directed_rounding` (`tests/execution/F16DirectedRounding.cpp`): one kernel runs all eight instructions on 1024 lanes (every pair of 32 halves, a varying src2), for each directed mode, every f16 denormal field, IEEE=0/1 and FP16_OVFL=0/1, wave32 and wave64, with and without the Float64 capability (12 CTest cases). Expected values come from an exact reference (`__int128` product-sum, rounded and flushed as above) that is checked against 19 rows pinned from the gfx1036 measurements. The same reference matches all 997,888 measured hardware rows below. - `agc_shader_mode_register`: the `unmodelled` check now uses `v_fract_f16` (still unmodelled), and a new `directedF16Arithmetic` check requires a run-time `s_round_mode` before `v_add_f16` to translate like the declared mode. Overlap: #18487 adds `v_cvt_f32_f64` to `RoundingUseOf` next to the lines moved here, a small textual conflict for whichever merges second. Not changed here: nearest-even keeps the existing lowering, including its known IEEE=0 signaling-NaN quieting, the `v_subrev_f16` NaN order and the native f16 path's signaling NaNs (each also on `main`); f32 directed rounding (`v_add/mul/fma_f32`) stays as on `main` and is a separate change. ### Tested Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 r7 UCRT, Release, `BUILD_TESTING=ON`. Head `d0f305089c`, rebased on `main` `80b0bdf264` (the only conflict was the `TranslationContext.hpp` declaration next to #9889's `floatUnaryValue`); the hardware comparison, full-suite run and base-vs-change validation below were made on the previous base `0294cba8dc`. - Hardware comparison (`tools/hw-oracle`, HIP runtime vs `HW_ORACLE_RUNTIME=vulkan` with `agc_oracle_runner`, same GPU) on AMD Radeon Graphics `1002:164e` (gfx1036): the eight instructions over 961 operand pairs (4913 triples for `v_fma_f16`/`v_fmac_f16`) in every f16 round and denormal field, at (IEEE, DX10, FP16_OVFL) = (1,0,0), (0,0,0), (1,0,1), (0,1,1): every directed-mode row matches. The remaining differences are all nearest-even and identical on `main`. - `ctest -R "^agc_shader_mode_register$|^agc_driver_f16_directed_rounding"` with `ANYPS5_REQUIRE_VULKAN=1`: 14/14 on both GPUs. Negative control: with `main`'s recompiler sources and this test, 12/12 directed cases fail on both GPUs (e.g. `0 - 0` toward -inf gives `0x0000`, expected `0x8000`). - Full `^agc|^shader|recompiler` run (1683 tests) on both GPUs: only `agc_driver_sample_order_grid_reference` fails on the Radeon, as on `main` (#9507, fixed by #9503). - Validation layer (Vulkan SDK 1.4.363.0, at least header version 335): the 83 focused tests matching `^agc.*(f16|float16|_rounding)|^agc_shader_mode_register$` with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer`, `ctest --test-dir build -R <pattern> -V`, on the base `0294cba8dc` and on this change, on both GPUs. Every log shows `Insert instance layer "VK_LAYER_KHRONOS_validation"` for each of the 77 tests that create a Vulkan instance (the other 6 are SPIR-V codegen tests that select no GPU) and no `was not found but was requested` line. Base 69/83 (the 13 new directed-rounding cases and `agc_shader_mode_register` fail), this change 83/83; no `VUID-` message on either side. After the rebase, the same focused pattern (now 100 tests, including #16889's `agc_driver_packed_mul_f16_rounding`) passes 100/100 on both GPUs with the layer inserted for the 94 tests that open a device and no `VUID-` message. - `tools/check_conventions.py --base origin/main`: 0 errors; the one warning is the existing length of the nearest-even f16 denormal entry (1827 characters on `main`), where this PR changes one sentence. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon Graphics (Ryzen 7 7700X integrated, RDNA2 gfx1036, `1002:164e`), AMD Windows driver 32.0.21042.62, Windows 11; NVIDIA GeForce RTX 3060 (`10de:2504`), driver 32.0.16.2101, Windows 11 - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-21 02:45:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T02:45:04Z.

Review: DISMISSED


Reviewed — changes requested

Changes are required before merge: fix the v_fmaak_f16 test instruction so its reference uses the actual literal addend.

What I ran

Reviewed head c6cbac5424d1eccf9f64a455c464469113af2db5 using repo_read.py search and targeted reads. No tests were run in this review.

Results

Check Result Note
Source review failed Test oracle disagrees with instruction operands
Tests not run No local test execution

Findings

Blocking

  • The new execution test's opcode at Code index 15 is v_fmaak_f16, but Expected models operation 6 as a * 0x3c01 + b. The decoder finalizes v_fmaak_f16 with source2 as a literal constant and its source1 field empty; the existing assembly fixture also shows the instruction takes a literal addend (v_fmaak_f16 ..., 0x0001). Thus the test compares a result using the encoded literal addend against a reference using varying b, causing false failures and leaving the intended arithmetic case unvalidated. Encode a genuinely varying-addend operation or make the reference use the actual literal. core/libs/prx/libSceAgcDriver/tests/execution/F16DirectedRounding.cpp:220.

Non-blocking

None.

Not verified

No tests or build were run. Reviewed changed files: ShaderModeRegisters.cpp, agc_driver_f16_directed_rounding_tests.cmake, F16DirectedRounding.cpp, TranslationContext.hpp, FloatInstructions.cpp, RoundingMode.cpp, and shader.md. manifest.json lists all seven changed files as included; no changed file was excluded from review. Related decoder, SPIR-V interpolation, modifier, and CMake context were read selectively.

To get this to a good state

Correct the v_fmaak_f16 test input/reference mismatch, then rerun the directed-rounding execution tests and shader mode register test.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002349752 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19505#pullrequestreview-6002349752). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T02:45:04Z. **Review:** DISMISSED --- # Reviewed — changes requested Changes are required before merge: fix the `v_fmaak_f16` test instruction so its reference uses the actual literal addend. ## What I ran Reviewed head `c6cbac5424d1eccf9f64a455c464469113af2db5` using `repo_read.py` search and targeted reads. No tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Source review | failed | Test oracle disagrees with instruction operands | | Tests | not run | No local test execution | ## Findings ### Blocking - The new execution test's opcode at `Code` index 15 is `v_fmaak_f16`, but `Expected` models operation 6 as `a * 0x3c01 + b`. The decoder finalizes `v_fmaak_f16` with `source2` as a literal constant and its `source1` field empty; the existing assembly fixture also shows the instruction takes a literal addend (`v_fmaak_f16 ..., 0x0001`). Thus the test compares a result using the encoded literal addend against a reference using varying `b`, causing false failures and leaving the intended arithmetic case unvalidated. Encode a genuinely varying-addend operation or make the reference use the actual literal. `core/libs/prx/libSceAgcDriver/tests/execution/F16DirectedRounding.cpp:220`. ### Non-blocking None. ## Not verified No tests or build were run. Reviewed changed files: `ShaderModeRegisters.cpp`, `agc_driver_f16_directed_rounding_tests.cmake`, `F16DirectedRounding.cpp`, `TranslationContext.hpp`, `FloatInstructions.cpp`, `RoundingMode.cpp`, and `shader.md`. `manifest.json` lists all seven changed files as included; no changed file was excluded from review. Related decoder, SPIR-V interpolation, modifier, and CMake context were read selectively. ## To get this to a good state Correct the `v_fmaak_f16` test input/reference mismatch, then rerun the directed-rounding execution tests and shader mode register test.

Pull request closed

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!19505
No description provided.