Skip to content

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

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
adriadam10:fix/f16-result-clamp
Oct 5, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
adriadam10:fix/f16-result-clamp

Conversation

@adriadam10

Copy link
Copy Markdown
Collaborator

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

@github-actions github-actions Bot added build shader core/shader agc libSceAgc* ci .github labels Oct 2, 2026
This was referenced Oct 2, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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
adriadam10 force-pushed the fix/f16-result-clamp branch from f3b9360 to 256bc5f Compare October 4, 2026 06:26
@adriadam10

Copy link
Copy Markdown
Collaborator Author

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

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Collaborator Author

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.

@adriadam10
adriadam10 force-pushed the fix/f16-result-clamp branch 2 times, most recently from 01bddbf to cc69286 Compare October 4, 2026 21:42
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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.

adriadam10 and others added 3 commits October 5, 2026 03:38
writeF16 dropped the clamp bit, so VOP3/SDWA f16 results with clamp
(v_rcp_f16, v_fma_f16, v_cvt_f16_f32, v_add_f16_sdwa, ...) were not
saturated. Clamp now limits to [0, 1] with NaN and -0 going to +0.
The f16 sqrt/rsq/log and sin/cos paths applied omod, which the
hardware ignores on f16 results in the default float mode, and
v_cvt_pkrtz_f16_f32 applied clamp and omod to its inputs, which the
hardware ignores as well.

Co-Authored-By: Claude <noreply@anthropic.com>
…yload

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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

@oneandonlydean
oneandonlydean merged commit 9f5fac0 into boykopovar:main Oct 5, 2026
3 checks passed
@adriadam10
adriadam10 deleted the fix/f16-result-clamp branch October 5, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build ci .github shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants