fix(shader): apply clamp but not omod to f16 results, as RDNA does #323

Merged
adriadam10 merged 3 commits from upstream-pr/323 into main 2026-10-05 05:06:30 +00:00
adriadam10 commented 2026-10-02 05:51:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-02T05:51:44Z.


What

RDNA f16 VALU results take the clamp bit but, in the default float mode, not omod. The recompiler had it the other way around in several places:

  • writeF16 (every f16 result written through it: VOP3 v_rcp/floor/fract/…_f16, v_cvt_f16_f32, v_cvt_f16_u16/i16, v_fma_f16, SDWA v_add/mul_f16) ignored clamp: v_rcp_f16 0.25 clamp gave 4.0 instead of 1.0.
  • The f16 sqrt/rsq/log path (negative-input check) and the f16 sin/cos path used applyF32ResultModifiers, so they applied omod.
  • v_cvt_pkrtz_f16_f32 applied clamp and omod to its inputs. The hardware ignores both on this opcode.

Measured on an RDNA2 GPU (ROCm HSA, raw kernels; same VALU as gfx1013 for these opcodes):

  • omod is applied only with IEEE mode off and that type's denormals flushed (f32 and f16 alike). Compute shaders run with f16 denormals on, so omod has no effect on f16 results. The f32 path is unchanged.
  • f16 clamp saturates to [0, 1], and NaN and −0 both become +0.

applyF16ResultModifiers does the clamp. writeF16, float16Unary and float16Trig now use it. vCvtPkrtzF16F32 no longer modifies its inputs.

Test agc_driver_float16_result_modifiers: 16 ops × 32 lanes with values captured on the RDNA2 GPU: clamp and omod on VOP1/VOP3/SDWA f16 results, v_fma_f16, v_cvt_f16_f32/u16 and v_cvt_pkrtz, including NaN, ±inf, −0 and SDWA WORD_1 with UNUSED_PRESERVE. f16 inputs are kept normal and f32 inputs away from denormals because those depend on the driver's float controls.

Tested

Linux, full ctest (100) passes.

  • RADV (Radeon 660M) and lavapipe: the test passes.
  • Without the fix: rcp_clamp is 0x4400, expected 0x3c00 (both drivers). Reverting only the pkrtz part: pkrtz_div2 is 0x30cc, expected 0x34cc. Reverting only the sqrt/sin part: sqrt_clamp_mul2 is 0x3c00, expected 0x3800.

Follow-up: in VOP3, the decoder still rejects clamp on the f16 binaries and on mad/min3/max3/med3_f16. That will come in a separate PR after #252, which touches the same checks.

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 in TechnicalDebt
  • 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: #251 (test harness), #317 (f16 round to nearest); review only the last commit
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4713173332 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/323). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-02T05:51:44Z. --- ### What RDNA f16 VALU results take the `clamp` bit but, in the default float mode, not `omod`. The recompiler had it the other way around in several places: - `writeF16` (every f16 result written through it: VOP3 `v_rcp/floor/fract/…_f16`, `v_cvt_f16_f32`, `v_cvt_f16_u16/i16`, `v_fma_f16`, SDWA `v_add/mul_f16`) **ignored clamp**: `v_rcp_f16 0.25 clamp` gave 4.0 instead of 1.0. - The f16 sqrt/rsq/log path (negative-input check) and the f16 sin/cos path used `applyF32ResultModifiers`, so they **applied omod**. - `v_cvt_pkrtz_f16_f32` applied clamp and omod to its inputs. The hardware ignores both on this opcode. Measured on an RDNA2 GPU (ROCm HSA, raw kernels; same VALU as gfx1013 for these opcodes): - omod is applied only with IEEE mode off **and** that type's denormals flushed (f32 and f16 alike). Compute shaders run with f16 denormals on, so omod has no effect on f16 results. The f32 path is unchanged. - f16 clamp saturates to [0, 1], and NaN and −0 both become +0. `applyF16ResultModifiers` does the clamp. `writeF16`, `float16Unary` and `float16Trig` now use it. `vCvtPkrtzF16F32` no longer modifies its inputs. Test `agc_driver_float16_result_modifiers`: 16 ops × 32 lanes with values **captured on the RDNA2 GPU**: clamp and omod on VOP1/VOP3/SDWA f16 results, `v_fma_f16`, `v_cvt_f16_f32`/`u16` and `v_cvt_pkrtz`, including NaN, ±inf, −0 and SDWA `WORD_1` with `UNUSED_PRESERVE`. f16 inputs are kept normal and f32 inputs away from denormals because those depend on the driver's float controls. ### Tested Linux, full ctest (100) passes. - RADV (Radeon 660M) and lavapipe: the test passes. - Without the fix: `rcp_clamp is 0x4400, expected 0x3c00` (both drivers). Reverting only the pkrtz part: `pkrtz_div2 is 0x30cc, expected 0x34cc`. Reverting only the sqrt/sin part: `sqrt_clamp_mul2 is 0x3c00, expected 0x3800`. Follow-up: in VOP3, the decoder still rejects clamp on the f16 binaries and on `mad/min3/max3/med3_f16`. That will come in a separate PR after #252, which touches the same checks. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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: #251 (test harness), #317 (f16 round to nearest); review only the last commit - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 05:40:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:40:45Z.


The fix itself looks right to me. It matches what ACO does: no omod on 16-bit results unless f16 denormals are flushed, and no output modifiers on v_cvt_pkrtz_f16_f32. But agc_driver_float16_result_modifiers fails on my NVIDIA machine (on main with this branch merged in). Lane 4 feeds the quiet NaN 0x7e00 through rcp, sin, fma and the SDWA mul and expects 0x7e00 back. NVIDIA returns its canonical NaN 0x7fffffff for f32 arithmetic on a NaN, so after the f16 conversion the result is 0x7fff. RDNA and lavapipe keep the payload, which is why CI is green. #317's Float16Rounding test accepts any NaN (IsNan16) for this reason. Could this test do the same for the NaN rows, or drop NaN inputs from the arithmetic columns? Then it is ready from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977008251 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/323#issuecomment-5977008251). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:40:45Z. --- The fix itself looks right to me. It matches what ACO does: no omod on 16-bit results unless f16 denormals are flushed, and no output modifiers on `v_cvt_pkrtz_f16_f32`. But `agc_driver_float16_result_modifiers` fails on my NVIDIA machine (on main with this branch merged in). Lane 4 feeds the quiet NaN 0x7e00 through `rcp`, `sin`, `fma` and the SDWA `mul` and expects 0x7e00 back. NVIDIA returns its canonical NaN 0x7fffffff for f32 arithmetic on a NaN, so after the f16 conversion the result is 0x7fff. RDNA and lavapipe keep the payload, which is why CI is green. #317's Float16Rounding test accepts any NaN (`IsNan16`) for this reason. Could this test do the same for the NaN rows, or drop NaN inputs from the arithmetic columns? Then it is ready from my side.
adriadam10 commented 2026-10-04 06:26:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T06:26:53Z.


Thanks for running it on NVIDIA. The test now accepts any NaN in each 16-bit half where a NaN is expected (IsNan16, as in #317's Float16Rounding); all other values are still compared bit for bit. Rebased onto main. It passes on RADV and lavapipe, and with main's translation files it still fails on the first clamp row (rcp_clamp is 0x00004400, expected 0x00003c00).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977304238 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/323#issuecomment-5977304238). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T06:26:53Z. --- Thanks for running it on NVIDIA. The test now accepts any NaN in each 16-bit half where a NaN is expected (`IsNan16`, as in #317's Float16Rounding); all other values are still compared bit for bit. Rebased onto main. It passes on RADV and lavapipe, and with main's translation files it still fails on the first clamp row (`rcp_clamp is 0x00004400, expected 0x00003c00`).
oneandonlydean commented 2026-10-04 21:19:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:19:05Z.


Thanks for the IsNan16 change, that fixes the NaN payload rows on NVIDIA. With those out of the way the test now gets further and stops at "lane 7 sin_mul2 is 0x00000003, expected 0x00000006" on my NVIDIA machine. Input 0x0001 gives sin(2pi * 2^-24), about 6 f16 denormal ulps. That value comes straight from the driver's sine, and NVIDIA's sine has an absolute error bound larger than the value itself (Vulkan only asks for 2^-11 absolute for sin), so 0x0003 is a valid result there and RDNA, RADV and lavapipe just happen to be exact. Could the sin rows use an input whose sine is well above that error (around 0.25 to 0.5, where mul:2 applied or ignored still differs clearly), with the sin columns compared within a tolerance? Then it's ready from my side, and since it changes f16 results that carry clamp, I'll run it through my PPSA21564 check before merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984492785 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/323#issuecomment-5984492785). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T21:19:05Z. --- Thanks for the IsNan16 change, that fixes the NaN payload rows on NVIDIA. With those out of the way the test now gets further and stops at "lane 7 sin_mul2 is 0x00000003, expected 0x00000006" on my NVIDIA machine. Input 0x0001 gives sin(2pi * 2^-24), about 6 f16 denormal ulps. That value comes straight from the driver's sine, and NVIDIA's sine has an absolute error bound larger than the value itself (Vulkan only asks for 2^-11 absolute for sin), so 0x0003 is a valid result there and RDNA, RADV and lavapipe just happen to be exact. Could the sin rows use an input whose sine is well above that error (around 0.25 to 0.5, where mul:2 applied or ignored still differs clearly), with the sin columns compared within a tolerance? Then it's ready from my side, and since it changes f16 results that carry clamp, I'll run it through my PPSA21564 check before merging.
adriadam10 commented 2026-10-04 21:35:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T21:35:24Z.


Thanks, done (new last commit, rebased on current main):

  • The two sine columns (sin_mul2, sin_clamp) are now compared within 2^-10 absolute, inside the Vulkan bound for sin, so 0x0003 for the 0x0001 row passes. Results the code picks on the bits stay exact: a ±0 the cardinal pin chooses must match bit for bit, NaN/inf rows go through SameHalf, and the clamped column must also be non-negative.
  • The 0.25 input (lane 0, sin = 1) keeps mul:2 obvious: with omod applied the test gives 0x4000 where 0x3c00 is expected. Dropping the clamp (lane 1 gives 0xbc00) and dropping the cardinal pin (lavapipe gives 0x8001 for lane 1) also fail.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984633354 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/323#issuecomment-5984633354). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T21:35:24Z. --- Thanks, done (new last commit, rebased on current main): - The two sine columns (`sin_mul2`, `sin_clamp`) are now compared within 2^-10 absolute, inside the Vulkan bound for sin, so 0x0003 for the 0x0001 row passes. Results the code picks on the bits stay exact: a ±0 the cardinal pin chooses must match bit for bit, NaN/inf rows go through `SameHalf`, and the clamped column must also be non-negative. - The 0.25 input (lane 0, sin = 1) keeps mul:2 obvious: with omod applied the test gives 0x4000 where 0x3c00 is expected. Dropping the clamp (lane 1 gives 0xbc00) and dropping the cardinal pin (lavapipe gives 0x8001 for lane 1) also fail.
oneandonlydean commented 2026-10-04 22:37:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:37:49Z.


Thanks, the new comparison looks right to me. NearHalf keeps everything the code picks on the bits exact (an unclamped ±0, NaN and inf through SameHalf), checks the clamped column for a negative sign, and only the two sine columns go through the tolerance. 2^-10 is a fair bound here: the recompiler evaluates v_sin_f16 as an f32 sine of the widened input, so the driver error is within Vulkan's 2^-11 absolute bound for 32-bit sin, and rounding to f16 adds at most half an f16 ulp below 1, 2^-12. Lane 0 (sin = 1) still separates mul:2 applied from ignored by a full 1.0, so the omod check keeps its teeth.

agc_driver_float16_result_modifiers now passes on my NVIDIA machine (on current main, with #537 and the other open shader PRs), where it stopped at lane 7 sin_mul2 before.

Since this changes f16 results that carry clamp, I'll run it through my PPSA21564 check next and merge it after that if it's clean.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985217762 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/323#issuecomment-5985217762). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T22:37:49Z. --- Thanks, the new comparison looks right to me. `NearHalf` keeps everything the code picks on the bits exact (an unclamped ±0, NaN and inf through `SameHalf`), checks the clamped column for a negative sign, and only the two sine columns go through the tolerance. 2^-10 is a fair bound here: the recompiler evaluates `v_sin_f16` as an f32 sine of the widened input, so the driver error is within Vulkan's 2^-11 absolute bound for 32-bit sin, and rounding to f16 adds at most half an f16 ulp below 1, 2^-12. Lane 0 (sin = 1) still separates mul:2 applied from ignored by a full 1.0, so the omod check keeps its teeth. `agc_driver_float16_result_modifiers` now passes on my NVIDIA machine (on current main, with #537 and the other open shader PRs), where it stopped at lane 7 `sin_mul2` before. Since this changes f16 results that carry clamp, I'll run it through my PPSA21564 check next and merge it after that if it's clean.
oneandonlydean commented 2026-10-05 05:03:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T05:03:57Z.


Gated on PPSA21564 together with #537 and #567: no visual or FPS change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5988441530 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/323#issuecomment-5988441530). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T05:03:57Z. --- Gated on PPSA21564 together with #537 and #567: no visual or FPS change.
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!323
No description provided.