perf(shader): pick f16 min, max and med3 results on the bits #3590

Merged
karanvora2599 merged 1 commit from upstream-pr/3590 into main 2026-10-11 21:39:47 +00:00
karanvora2599 commented 2026-10-11 13:54:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-11T13:54:29Z.


What

v_min_f16, v_max_f16, v_min3_f16, v_max3_f16, v_med3_f16, v_pk_min_f16 and v_pk_max_f16 now pick their result on the f16 bits instead of computing it in f32. The NaN, IEEE_MODE, f16 input denormal flush (#2876) and clamp rules are unchanged.

Background. The result of these instructions is always one of the sources, or, for two zeros, their sign bits combined.

  • Until now each source was converted to f32 and the f32 min, max or med3 ran with its own NaN and signed-zero rules.
  • The result was then converted back to f16, and the f16 NaN rules were applied on the bits.
  • The f32 detour costs two conversions per source pair, the f32 rules, and a conversion back. It doesn't depend on any device feature, so this change applies to every GPU, NVIDIA Pascal through Ada included.

How. minMaxF16Bits turns each f16 into an integer ordering key: sign-extend the 16 bits, then flip the magnitude of negative values. It compares the keys as signed integers, selects the source bits, and keeps the existing rule for two zeros (AND of the bits for max, OR for min). The scalar pair, min3/max3 and med3 paths and the packed lanes all use it. The NaN rules that follow are untouched.

I first compared the f32 conversions instead of integer keys, which works on NVIDIA and lavapipe. On an Intel UHD it fails: once these instructions no longer convert to f16, the module declares no f16 DenormPreserve, and Intel's UnpackHalf2x16 then turns f16 denormals into zero. v_min_f16 0x81ff, 0x01ff gave 0x01ff. The integer keys don't depend on that conversion.

Before / after on an RTX 4060 Laptop (Ada, driver 591.86). SASS instructions per instruction come from APS5_PIPELINE_STATS binary size over a chain of 16 instructions, FLOAT_MODE 0xc0:

Instruction SASS before SASS after
v_min_f16 35.0 12.5
v_max_f16 35.0 13.5
v_min3_f16 53.5 22.5
v_max3_f16 53.5 24.5
v_med3_f16 90.5 46.0
v_pk_min_f16 77.5 26.5
v_pk_max_f16 77.5 28.5

All other opcodes in the sweep compile to the same size.

Why results don't change.

  • The ordering key is strictly monotonic in the f16 value, except that -0 orders below +0. Both paths handle two zeros with the same AND/OR rule before comparing, so the comparison picks the same source as the f32 ordered comparison of the exact conversions.
  • A NaN source has always been replaced by the NaN rules that run afterwards, so its key is never used. v_med3_f16 takes the min3 path when any source is NaN, as before.
  • In min3/max3 the inner result's bits feed the outer comparison. The old path compared the inner f32 result instead, but where the inner bits aren't NaN the two values are equal, and where they are NaN the outer NaN rules decide.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, Release, main at 1c75f4d2. RTX 4060 Laptop (driver 591.86), Intel UHD Graphics (Raptor Lake, driver 101.6790) and lavapipe from Mesa 25.2.8 (the version CI installs; Windows build, GALLIUM_OVERRIDE_CPU_CAPS=avx).

  • New agc_driver_float16_min_max_bits:
    • It checks the seven instructions against a CPU model of the rules: VOP2 and VOP3 forms with abs, neg, op_sel and clamp, and packed op_sel, op_sel_hi, neg_lo, neg_hi and clamp.
    • Inputs are 32 batches of random halves weighted toward zeros, denormals, infinities, quiet and signaling NaNs and values near 1, plus rows with equal and negated pairs.
    • It runs in seven float modes: no FLOAT_MODE, 0xc0 with each DX10_CLAMP × IEEE_MODE combination, 0x30 and 0xb0 (f16 input denormals flushed; scalar only, since packed f16 throws there).
    • Current main matched the model on all three devices (155,648 results each) before the change, and this branch does too.
    • Swapping the two-zeros rule (OR for max, AND for min) makes it fail: v_max3_f16 ... is 0x00008000, expected 0x00000000.
  • The f16 and packed tests (24) pass on all three devices; the Intel UHD skips the three that need Float64 or interpolation features, lavapipe agc_driver_interpolation_f16.
  • Shader tests (agc_driver_*, agc_shader_*, agc_spirv_*, vector_*, agc_masked_select, agc_validate_program, agc_denormal*, agc_float16_*; 320 tests):
    • RTX 4060: everything passes except the device-lost agc_driver_flat_loads*, agc_driver_global_loads*, agc_driver_flat_apertures_2, agc_driver_flat_apertures_4 (then 0xc0000409) and agc_driver_pixel_interlock, which fail the same way on main.
    • lavapipe 25.2.8: everything passes or is skipped for missing features, except agc_driver_image_plane_address_on_line_implicit_lod, which fails the same way with main's recompiler, and an intermittent segfault in agc_driver_depth_fast_clear_submit, which crashed in 7 of 12 runs with main's recompiler.

Limits.

  • I haven't run it on AMD hardware.
  • v_med3_f16 still computes both the min3 used for NaN sources and the median, and selects between them.
  • This doesn't depend on #3531 (native packed f16 add/mul), but both move laneBits in packedFloat16 (lines 34-43 on main), so whichever lands second needs a small rebase; I'll do it.

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: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4819563975 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3590). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-11T13:54:29Z. --- ### What `v_min_f16`, `v_max_f16`, `v_min3_f16`, `v_max3_f16`, `v_med3_f16`, `v_pk_min_f16` and `v_pk_max_f16` now pick their result on the f16 bits instead of computing it in f32. The NaN, `IEEE_MODE`, f16 input denormal flush (#2876) and clamp rules are unchanged. **Background.** The result of these instructions is always one of the sources, or, for two zeros, their sign bits combined. - Until now each source was converted to f32 and the f32 min, max or med3 ran with its own NaN and signed-zero rules. - The result was then converted back to f16, and the f16 NaN rules were applied on the bits. - The f32 detour costs two conversions per source pair, the f32 rules, and a conversion back. It doesn't depend on any device feature, so this change applies to every GPU, NVIDIA Pascal through Ada included. **How.** `minMaxF16Bits` turns each f16 into an integer ordering key: sign-extend the 16 bits, then flip the magnitude of negative values. It compares the keys as signed integers, selects the source bits, and keeps the existing rule for two zeros (AND of the bits for max, OR for min). The scalar pair, min3/max3 and med3 paths and the packed lanes all use it. The NaN rules that follow are untouched. I first compared the f32 conversions instead of integer keys, which works on NVIDIA and lavapipe. On an Intel UHD it fails: once these instructions no longer convert to f16, the module declares no f16 `DenormPreserve`, and Intel's `UnpackHalf2x16` then turns f16 denormals into zero. `v_min_f16 0x81ff, 0x01ff` gave 0x01ff. The integer keys don't depend on that conversion. **Before / after** on an RTX 4060 Laptop (Ada, driver 591.86). SASS instructions per instruction come from `APS5_PIPELINE_STATS` binary size over a chain of 16 instructions, `FLOAT_MODE` 0xc0: | Instruction | SASS before | SASS after | |---|---|---| | `v_min_f16` | 35.0 | 12.5 | | `v_max_f16` | 35.0 | 13.5 | | `v_min3_f16` | 53.5 | 22.5 | | `v_max3_f16` | 53.5 | 24.5 | | `v_med3_f16` | 90.5 | 46.0 | | `v_pk_min_f16` | 77.5 | 26.5 | | `v_pk_max_f16` | 77.5 | 28.5 | All other opcodes in the sweep compile to the same size. **Why results don't change.** - The ordering key is strictly monotonic in the f16 value, except that -0 orders below +0. Both paths handle two zeros with the same AND/OR rule before comparing, so the comparison picks the same source as the f32 ordered comparison of the exact conversions. - A NaN source has always been replaced by the NaN rules that run afterwards, so its key is never used. `v_med3_f16` takes the min3 path when any source is NaN, as before. - In min3/max3 the inner result's bits feed the outer comparison. The old path compared the inner f32 result instead, but where the inner bits aren't NaN the two values are equal, and where they are NaN the outer NaN rules decide. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, Release, `main` at 1c75f4d2. RTX 4060 Laptop (driver 591.86), Intel UHD Graphics (Raptor Lake, driver 101.6790) and lavapipe from Mesa 25.2.8 (the version CI installs; Windows build, `GALLIUM_OVERRIDE_CPU_CAPS=avx`). - New `agc_driver_float16_min_max_bits`: - It checks the seven instructions against a CPU model of the rules: VOP2 and VOP3 forms with abs, neg, `op_sel` and clamp, and packed `op_sel`, `op_sel_hi`, `neg_lo`, `neg_hi` and clamp. - Inputs are 32 batches of random halves weighted toward zeros, denormals, infinities, quiet and signaling NaNs and values near 1, plus rows with equal and negated pairs. - It runs in seven float modes: no `FLOAT_MODE`, 0xc0 with each `DX10_CLAMP` × `IEEE_MODE` combination, 0x30 and 0xb0 (f16 input denormals flushed; scalar only, since packed f16 throws there). - Current `main` matched the model on all three devices (155,648 results each) before the change, and this branch does too. - Swapping the two-zeros rule (OR for max, AND for min) makes it fail: `v_max3_f16 ... is 0x00008000, expected 0x00000000`. - The f16 and packed tests (24) pass on all three devices; the Intel UHD skips the three that need `Float64` or interpolation features, lavapipe `agc_driver_interpolation_f16`. - Shader tests (`agc_driver_*`, `agc_shader_*`, `agc_spirv_*`, `vector_*`, `agc_masked_select`, `agc_validate_program`, `agc_denormal*`, `agc_float16_*`; 320 tests): - RTX 4060: everything passes except the device-lost `agc_driver_flat_loads*`, `agc_driver_global_loads*`, `agc_driver_flat_apertures_2`, `agc_driver_flat_apertures_4` (then `0xc0000409`) and `agc_driver_pixel_interlock`, which fail the same way on `main`. - lavapipe 25.2.8: everything passes or is skipped for missing features, except `agc_driver_image_plane_address_on_line_implicit_lod`, which fails the same way with `main`'s recompiler, and an intermittent segfault in `agc_driver_depth_fast_clear_submit`, which crashed in 7 of 12 runs with `main`'s recompiler. **Limits.** - I haven't run it on AMD hardware. - `v_med3_f16` still computes both the min3 used for NaN sources and the median, and selects between them. - This doesn't depend on #3531 (native packed f16 add/mul), but both move `laneBits` in `packedFloat16` (lines 34-43 on `main`), so whichever lands second needs a small rebase; I'll do it. ### 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: none - [x] AI-assisted: yes
adriadam10 commented 2026-10-11 21:39:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:39:43Z.


Checked on RADV and CI's lavapipe: the new agc_driver_float16_min_max_bits passes (155,648 results) along with the existing f16/min-max tests, and the HW oracle (gfx1035, IEEE=0, f16 denormals kept) matches the selection and two-zero rules of v_min/max/min3/max3/med3_f16 on 1024 rows without NaN. Integer keys are monotonic as described, and the result is unchanged from main. Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114075775 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3590#issuecomment-6114075775). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:39:43Z. --- Checked on RADV and CI's lavapipe: the new `agc_driver_float16_min_max_bits` passes (155,648 results) along with the existing f16/min-max tests, and the HW oracle (gfx1035, IEEE=0, f16 denormals kept) matches the selection and two-zero rules of `v_min/max/min3/max3/med3_f16` on 1024 rows without NaN. Integer keys are monotonic as described, and the result is unchanged from `main`. Merging once CI is green.
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!3590
No description provided.