fix(shader): give f16 and f64 output modifiers +0 for a zero or denormal result #18313

Merged
angrysandhill merged 4 commits from upstream-pr/18313 into main 2026-11-21 09:21:52 +00:00
angrysandhill commented 2026-11-11 06:49:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by angrysandhill on 2026-11-11T06:49:14Z.


What

When an output modifier (mul:2, mul:4, div:2) applies, which is with IEEE mode off and output denormals of the result's precision flushed, the hardware gives +0 for a zero or denormal result. The f32 path already does this. The f16 and f64 paths kept the result's sign instead, so for example -0 + -0 with mul:2 gave -0.

The shader debt list called this unmeasured: "flushing a denormal to a signed zero (the f32 path gives +0) … remain unmeasured".

The change:

  • scaleF16Result, scaleF16Bits and scaleF64Result: a zero or denormal result now becomes +0 in all three, as in scaleF32Result.
  • div:2: where it takes a normal result below the smallest normal, the result still keeps its sign, as measured. Only that rule's zero-or-denormal branch changes.
  • vector_half_double_output_modifiers: it asserted the old sign-keeping select. Its three checks now require the +0 select.
  • Debt note: updated with the measurement.

Tested

Setup: Windows 10 Home 22H2 (10.0.19045), AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (driver 32.0.21045.16010), WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON. Tested on main bb520eba29. The branch is rebased onto 774111474c, whose only new commit touches libSceNKWebKit.

Hardware (hardware oracle, HIP runtime).

  • Instructions: v_add_f16_e64, v_add_f32_e64 and v_add_f64, each with mul:2, mul:4 and div:2. v_add_f16 goes through scaleF16Result.
  • The f16 bit path: scaleF16Bits is reached by v_max_f16, v_min_f16 and v_ldexp_f16, so v_max_f16_e64, v_min_f16_e64 and v_ldexp_f16_e64 were measured too. They use the same f16 operands (exponent 0 for v_ldexp_f16) and the same modes, with each modifier and without one.
  • Rows: 20 per width. They produce ±0 (+0 + -0, -0 + -0, 1 + -1), ±the smallest denormal, ±the smallest normal, values whose div:2 underflows, ±near-max values that overflow, ±infinity, NaNs and ±1.
  • Modes: IEEE 0 with the FLOAT_MODE denormal field at 0 and at 1, where the modifier applies. As controls, field 3 and IEEE 1, where the modifier is ignored.

Results, against the recompiler on main with HW_ORACLE_RUNTIME=vulkan:

rows differing from the hardware, each of mul:2, mul:4, div:2
f32, modifier applies 0 of 20
f16, modifier applies 2 of 20: -0 + -0 and -denormal + -0, hardware +0, recompiler -0
f64, modifier applies 2 of 20: the same two rows
f16 bit path, modifier applies v_max_f16 8, v_min_f16 3, v_ldexp_f16 2 of 20; each is hardware +0, recompiler -0
modifier ignored (every instruction above), or absent (the bit path) 0 of 20

With this change no row differs in any of these combinations. So on the hardware a zero or denormal result becomes +0 at all three widths and on the f16 bit path. A normal result that div:2 takes below the smallest normal keeps its sign; the recompiler already matched that at all three widths.

Tests:

  • New agc_driver_f16_f64_output_modifier_zero: the 20 rows each for v_add_f16 and v_add_f64, run with each modifier in FLOAT_MODE 0x00, 0x50 and 0xf0 (IEEE off). The expected values are the hardware's, except in the two f16 NaN rows (0x7e01 + 0 and 0xfe01 + 0), which need only a NaN in the low half since d0a24a8f8c: scalar v_add_f16 returns the NaN of the device's own add, and NVIDIA's is 0x7fff.
    • On main it fails: v_add_f16 mul:2 FLOAT_MODE 0x00000000: lane 1 word 0 is 0xabcd8000, expected 0xabcd0000 (lane 1 is -0 + -0).
    • With this change it passes, also at d0a24a8f8c under the validation layer (no VUID- line).
    • NaN rows at d0a24a8f8c: if scaleF16Result scales a NaN instead of passing it through, the test fails on them: lane 15 word 0 is 0xabcd7c00, expected a NaN in the low half.
  • vector_half_double_output_modifiers: with the updated checks it fails on main ("v_add_f16 mul:2 did not scale a zero or denormal result to +0") and passes with this change.
  • Full ^(agc_|shader|vector_), 4 at a time: 1,612 tests: 1,594 passed, 17 skipped, 1 failed.
    • Against main: the same run on main (1,611 tests) gives every test the same outcome. The only extra test is the new one, which passes.
    • The failure: agc_driver_sample_order_grid_reference fails on main too, with the same message ("the exact path did not render the checkerboard bit for bit like the reference"). It is the odd-row failure that #18229 reports on an RX 6700 XT (0 of 2048 odd-row samples at the guest's locations), which #9503 fixes.
  • Validation layer: VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, one test at a time, on 54 tests: -R "^(agc_driver_f16_f64_output_modifier_zero|vector_half_double_output_modifiers|agc_driver_.*(modifier|omod|float16|f64|fp16|fma|division).*|shader_.*modifier.*)$" -E fmask.
    • Result: no VUID- on this change or on main.
    • Coverage: the layer loaded in each of the 51 tests that open a Vulkan device, the new test included. The other three only check generated SPIR-V or IR.
    • Skipped: agc_driver_float16_unused_nan_payloads skips on both sides, because this device has no native f16 conversions with RTE rounding.

Not measured: the PS5's gfx1013, and the ordering against the FP16_OVFL clamp, which the debt note still lists.

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 RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (32.0.21045.16010), Windows 10 22H2
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5255130260 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18313). Originally posted by [angrysandhill](https://github.com/angrysandhill) on 2026-11-11T06:49:14Z. --- ### What When an output modifier (`mul:2`, `mul:4`, `div:2`) applies, which is with IEEE mode off and output denormals of the result's precision flushed, the hardware gives **+0** for a zero or denormal result. The f32 path already does this. The f16 and f64 paths kept the result's sign instead, so for example `-0 + -0` with `mul:2` gave `-0`. The shader debt list called this unmeasured: "flushing a denormal to a signed zero (the f32 path gives +0) … remain unmeasured". The change: - **`scaleF16Result`, `scaleF16Bits` and `scaleF64Result`:** a zero or denormal result now becomes `+0` in all three, as in `scaleF32Result`. - **`div:2`:** where it takes a normal result below the smallest normal, the result still keeps its sign, as measured. Only that rule's zero-or-denormal branch changes. - **`vector_half_double_output_modifiers`:** it asserted the old sign-keeping select. Its three checks now require the `+0` select. - **Debt note:** updated with the measurement. ### Tested **Setup:** Windows 10 Home 22H2 (10.0.19045), AMD Radeon RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (driver 32.0.21045.16010), WinLibs GCC 15.2.0 r7 UCRT, Release, `BUILD_TESTING=ON`. Tested on `main` `bb520eba29`. The branch is rebased onto `774111474c`, whose only new commit touches `libSceNKWebKit`. **Hardware** ([hardware oracle](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/HW_ORACLE.md), HIP runtime). - **Instructions:** `v_add_f16_e64`, `v_add_f32_e64` and `v_add_f64`, each with `mul:2`, `mul:4` and `div:2`. `v_add_f16` goes through `scaleF16Result`. - **The f16 bit path:** `scaleF16Bits` is reached by `v_max_f16`, `v_min_f16` and `v_ldexp_f16`, so `v_max_f16_e64`, `v_min_f16_e64` and `v_ldexp_f16_e64` were measured too. They use the same f16 operands (exponent 0 for `v_ldexp_f16`) and the same modes, with each modifier and without one. - **Rows:** 20 per width. They produce ±0 (`+0 + -0`, `-0 + -0`, `1 + -1`), ±the smallest denormal, ±the smallest normal, values whose `div:2` underflows, ±near-max values that overflow, ±infinity, NaNs and ±1. - **Modes:** IEEE 0 with the FLOAT_MODE denormal field at 0 and at 1, where the modifier applies. As controls, field 3 and IEEE 1, where the modifier is ignored. Results, against the recompiler on `main` with `HW_ORACLE_RUNTIME=vulkan`: | | rows differing from the hardware, each of `mul:2`, `mul:4`, `div:2` | |---|---| | f32, modifier applies | 0 of 20 | | f16, modifier applies | 2 of 20: `-0 + -0` and `-denormal + -0`, hardware `+0`, recompiler `-0` | | f64, modifier applies | 2 of 20: the same two rows | | f16 bit path, modifier applies | `v_max_f16` 8, `v_min_f16` 3, `v_ldexp_f16` 2 of 20; each is hardware `+0`, recompiler `-0` | | modifier ignored (every instruction above), or absent (the bit path) | 0 of 20 | With this change no row differs in any of these combinations. So on the hardware a zero or denormal result becomes `+0` at all three widths and on the f16 bit path. A normal result that `div:2` takes below the smallest normal keeps its sign; the recompiler already matched that at all three widths. **Tests:** - **New `agc_driver_f16_f64_output_modifier_zero`:** the 20 rows each for `v_add_f16` and `v_add_f64`, run with each modifier in FLOAT_MODE `0x00`, `0x50` and `0xf0` (IEEE off). The expected values are the hardware's, except in the two f16 NaN rows (`0x7e01 + 0` and `0xfe01 + 0`), which need only a NaN in the low half since `d0a24a8f8c`: scalar `v_add_f16` returns the NaN of the device's own add, and NVIDIA's is `0x7fff`. - On `main` it fails: `v_add_f16 mul:2 FLOAT_MODE 0x00000000: lane 1 word 0 is 0xabcd8000, expected 0xabcd0000` (lane 1 is `-0 + -0`). - With this change it passes, also at `d0a24a8f8c` under the validation layer (no `VUID-` line). - **NaN rows at `d0a24a8f8c`:** if `scaleF16Result` scales a NaN instead of passing it through, the test fails on them: `lane 15 word 0 is 0xabcd7c00, expected a NaN in the low half`. - **`vector_half_double_output_modifiers`:** with the updated checks it fails on `main` ("v_add_f16 mul:2 did not scale a zero or denormal result to +0") and passes with this change. - **Full `^(agc_|shader|vector_)`, 4 at a time:** 1,612 tests: 1,594 passed, 17 skipped, 1 failed. - **Against `main`:** the same run on `main` (1,611 tests) gives every test the same outcome. The only extra test is the new one, which passes. - **The failure:** `agc_driver_sample_order_grid_reference` fails on `main` too, with the same message ("the exact path did not render the checkerboard bit for bit like the reference"). It is the odd-row failure that #18229 reports on an RX 6700 XT (0 of 2048 odd-row samples at the guest's locations), which #9503 fixes. - **Validation layer:** `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, one test at a time, on 54 tests: `-R "^(agc_driver_f16_f64_output_modifier_zero|vector_half_double_output_modifiers|agc_driver_.*(modifier|omod|float16|f64|fp16|fma|division).*|shader_.*modifier.*)$" -E fmask`. - **Result:** no `VUID-` on this change or on `main`. - **Coverage:** the layer loaded in each of the 51 tests that open a Vulkan device, the new test included. The other three only check generated SPIR-V or IR. - **Skipped:** `agc_driver_float16_unused_nan_payloads` skips on both sides, because this device has no native f16 conversions with RTE rounding. **Not measured:** the PS5's gfx1013, and the ordering against the `FP16_OVFL` clamp, which the debt note still lists. ### Checklist - [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 RX 6750 XT (gfx1031), AMD Software Adrenalin 26.10.1 (32.0.21045.16010), Windows 10 22H2 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-21 04:43:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T04:43:50Z.

Review: COMMENTED


Reviewed — no blocking findings

The changed scaling paths and their focused regression checks are consistent with the stated +0 behavior; I found no verified blocking defect.

What I ran

Reviewed head 6311ad351d30c25a10a8d954d9fa34a16ba10037 against base main. No commands or tests were run; repo_read.py was used to inspect the prepared review material and repository files at the pinned head.

Results

Check Result Note
Change review passed Scaling and regression assertions inspected
Tests not run Reported runs were supplied by the author

Findings

Blocking

None.

Non-blocking

None.

Not verified

The new execution test was inspected only at its setup, checks, and dispatch loop; the rest of its full source and the other changed test/documentation files were not read. Runtime behavior, build integration, and the author-reported hardware and test results were not independently verified. The change is narrowly limited in the inspected translation functions, but this review is incomplete across the changed files, so approval is not justified.

Files read: review/description.md, review/commits.md, review/manifest.json, review/diff.patch, core/shader/recompiler/Translation/src/Float64Instructions.cpp, core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp, and selected ranges of core/libs/tests/VectorHalfDoubleOutputModifiers.cpp and core/libs/prx/libSceAgcDriver/tests/execution/F16F64OutputModifierZero.cpp. The manifest lists four changed files; all are marked included, and none are listed as excluded.

To get this to a good state

Nothing is required. Optional: complete review of the new execution test and remaining changed files, and run the relevant checks on this head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002896019 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18313#pullrequestreview-6002896019). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T04:43:50Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The changed scaling paths and their focused regression checks are consistent with the stated +0 behavior; I found no verified blocking defect. ## What I ran Reviewed head `6311ad351d30c25a10a8d954d9fa34a16ba10037` against base `main`. No commands or tests were run; `repo_read.py` was used to inspect the prepared review material and repository files at the pinned head. ## Results | Check | Result | Note | |---|---|---| | Change review | passed | Scaling and regression assertions inspected | | Tests | not run | Reported runs were supplied by the author | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The new execution test was inspected only at its setup, checks, and dispatch loop; the rest of its full source and the other changed test/documentation files were not read. Runtime behavior, build integration, and the author-reported hardware and test results were not independently verified. The change is narrowly limited in the inspected translation functions, but this review is incomplete across the changed files, so approval is not justified. Files read: `review/description.md`, `review/commits.md`, `review/manifest.json`, `review/diff.patch`, `core/shader/recompiler/Translation/src/Float64Instructions.cpp`, `core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp`, and selected ranges of `core/libs/tests/VectorHalfDoubleOutputModifiers.cpp` and `core/libs/prx/libSceAgcDriver/tests/execution/F16F64OutputModifierZero.cpp`. The manifest lists four changed files; all are marked included, and none are listed as excluded. ## To get this to a good state Nothing is required. Optional: complete review of the new execution test and remaining changed files, and run the relevant checks on this head.
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!18313
No description provided.