fix(shader): return +0 when v_div_fmas_f32 cancels with |src2| >= 2^64 #1963

Merged
Deftera186 merged 1 commit from upstream-pr/1963 into main 2026-10-10 06:11:37 +00:00
Deftera186 commented 2026-10-09 03:04:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Deftera186 on 2026-10-09T03:04:20Z.


What

v_div_fmas_f32 with VCC set and a src2 exponent field of 128 or more (|src2| >= 2, scale 2^64) recomputed a plain fma result with exponent field 0 as fma(smaller multiplicand * 2^64, other multiplicand, src2 * 2^64). For |src2| >= 2^64, src2 * 2^64 rounds to infinity, so an fma that cancels exactly returned -inf or +inf. The hardware returns +0, for example (1.0, FLT_MAX, -FLT_MAX) gave 0xff800000 and (1.0, -2^100, 2^100) gave 0x7f800000.

Only exact cancellations reach that recomputation. With |src2| >= 2, a nonzero exact sum S below 2^-126 needs |src0 * src1| > 1; such a product is a multiple of 2^-47 (two 24-bit significands, exponents >= -149) and src2 is a multiple of 2^-22, so a nonzero S is at least 2^-47 and the fma result is normal. The result is therefore the plain fma times 2^64, which is +0 for a cancellation and the single rounding of 2^64 * S otherwise. The recomputation is now done only for the 2^-64 scale, when the plain fma overflows; scaleLhs only differed from lhsLarger in the removed case and is replaced by it. VCC=0, the 2^-64 path, v_div_scale_f32 and v_div_fixup_f32 are unchanged, and so is the unmodelled f32 denormal flush of the v_div_fmas_f32 result (TechnicalDebt "f32 denormals").

Source: hardware measurement (below). The 2^64 / 2^-64 scaling rule is the one measured for 11bc2799; the ISA pseudocode does not match the hardware there.

Tested

Gentoo Linux, AMD Radeon RX 7700 XT (gfx1101), RADV (Mesa 26.2.3).

  • agc_driver_division with 7 rows added to HelperVectors. Before the fix: AGC graphics: division helpers: vector 256 (0x3f800000, 0x7f7fffff, 0xff7fffff) vcc 1: v_div_fmas_f32 is 0xff800000, expected 0x00000000. After: passed.
  • Full suite: 100% tests passed, 0 tests failed out of 494.
  • Not run on lavapipe; the added rows check v_div_fmas_f32 only when the device fuses fma, as the existing rows do.

Hardware: gfx1101 through tools/hw-oracle, with the instruction sequence of HelperCode (v_div_scale_f32, v_cmp_ne_u32 vcc_lo, 0, v7, v_div_fmas_f32, v_div_fixup_f32), in all 16 combinations of IEEE 0/1, f32 denormals 0/3, f16/f64 denormals 0/3 and DX10_CLAMP 0/1 (round mode 0). Every value below is the same in all 16 modes. The 256 existing HelperVectors rows (from 11bc2799, measured on RDNA2 according to its message) match gfx1101 bit for bit in the test's mode. Not re-measured on RDNA2.

src0, src1, src2 VCC hw before after
0x3f800000, 0x7f7fffff, 0xff7fffff (1.0, FLT_MAX, -FLT_MAX) 1 0x00000000 0xff800000 0x00000000
0x3f800000, 0xf1800000, 0x71800000 (1.0, -2^100, 2^100) 1 0x00000000 0x7f800000 0x00000000
0x7f7fffff, 0xbf800000, 0x7f7fffff (FLT_MAX, -1.0, FLT_MAX) 1 0x00000000 0x7f800000 0x00000000
0x3f800000, 0xdf800000, 0x5f800000 (1.0, -2^64, 2^64) 1 0x00000000 0x7f800000 0x00000000
0x5f400000, 0xdf200000, 0x7ef00000 (1.5 * 2^63, -1.25 * 2^63, 1.875 * 2^126) 1 0x00000000 0x7f800000 0x00000000
0xe40a117d, 0xbf800000, 0xe40a117d (existing row, VCC set) 1 0x00000000 0xff800000 0x00000000
0x3f800000, 0xdf7fffff, 0x5f7fffff (1.0, -(2^64 - 2^40), 2^64 - 2^40) 1 0x00000000 0x00000000 0x00000000

With VCC 0 every row gives 0x00000000 on hardware, before and after.

Sweep: 11502 operand triples, each with VCC 0 and 1 (23004 rows), on gfx1101 in all 16 modes and through the recompiler before and after: the 256 table rows with both VCC values, 8126 exact cancellations (8120 with 2 <= |src2| <= FLT_MAX; every sign combination), 5736 nonzero near-cancellations with |src2| >= 2, ±0 operands, overflowing products with |src2| < 2, and 8000 random normal rows.

  • The change alters 1820 results: exactly the VCC=1 cancellations with |src2| >= 2^64. Before, they were 0x7f800000 (902) or 0xff800000 (918); hardware and the fix give 0x00000000 in all 16 modes. No other row changes.
  • Every exact cancellation is 0x00000000 on hardware in all 16 modes; the 28 rows giving 0x80000000 have a -0 product and src2 = -0 and match before and after.
  • Rows without denormals (22915), differing from hardware: IEEE=1 modes 1820 before, 0 after; IEEE=0 modes 1840 before, 20 after. The 20 have a signaling NaN input, which the hardware returns unquieted at IEEE=0 while the recompiler quiets it; unchanged here.
  • 89 rows whose hardware result is denormal with f32 denormals kept are not claimed: they are identical before and after and return the flushed ±0 (#981).

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:4795423626 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1963). Originally posted by [Deftera186](https://github.com/Deftera186) on 2026-10-09T03:04:20Z. --- ### What `v_div_fmas_f32` with VCC set and a src2 exponent field of 128 or more (|src2| >= 2, scale 2^64) recomputed a plain fma result with exponent field 0 as `fma(smaller multiplicand * 2^64, other multiplicand, src2 * 2^64)`. For |src2| >= 2^64, `src2 * 2^64` rounds to infinity, so an fma that cancels exactly returned -inf or +inf. The hardware returns +0, for example `(1.0, FLT_MAX, -FLT_MAX)` gave `0xff800000` and `(1.0, -2^100, 2^100)` gave `0x7f800000`. Only exact cancellations reach that recomputation. With |src2| >= 2, a nonzero exact sum S below 2^-126 needs |src0 * src1| > 1; such a product is a multiple of 2^-47 (two 24-bit significands, exponents >= -149) and src2 is a multiple of 2^-22, so a nonzero S is at least 2^-47 and the fma result is normal. The result is therefore the plain fma times 2^64, which is +0 for a cancellation and the single rounding of 2^64 * S otherwise. The recomputation is now done only for the 2^-64 scale, when the plain fma overflows; `scaleLhs` only differed from `lhsLarger` in the removed case and is replaced by it. VCC=0, the 2^-64 path, `v_div_scale_f32` and `v_div_fixup_f32` are unchanged, and so is the unmodelled f32 denormal flush of the `v_div_fmas_f32` result (TechnicalDebt "f32 denormals"). Source: hardware measurement (below). The 2^64 / 2^-64 scaling rule is the one measured for 11bc2799; the ISA pseudocode does not match the hardware there. ### Tested Gentoo Linux, AMD Radeon RX 7700 XT (gfx1101), RADV (Mesa 26.2.3). - `agc_driver_division` with 7 rows added to `HelperVectors`. Before the fix: `AGC graphics: division helpers: vector 256 (0x3f800000, 0x7f7fffff, 0xff7fffff) vcc 1: v_div_fmas_f32 is 0xff800000, expected 0x00000000`. After: passed. - Full suite: `100% tests passed, 0 tests failed out of 494`. - Not run on lavapipe; the added rows check `v_div_fmas_f32` only when the device fuses fma, as the existing rows do. Hardware: gfx1101 through `tools/hw-oracle`, with the instruction sequence of `HelperCode` (`v_div_scale_f32`, `v_cmp_ne_u32 vcc_lo, 0, v7`, `v_div_fmas_f32`, `v_div_fixup_f32`), in all 16 combinations of IEEE 0/1, f32 denormals 0/3, f16/f64 denormals 0/3 and DX10_CLAMP 0/1 (round mode 0). Every value below is the same in all 16 modes. The 256 existing `HelperVectors` rows (from 11bc2799, measured on RDNA2 according to its message) match gfx1101 bit for bit in the test's mode. Not re-measured on RDNA2. | src0, src1, src2 | VCC | hw | before | after | |---|---|---|---|---| | `0x3f800000, 0x7f7fffff, 0xff7fffff` (1.0, FLT_MAX, -FLT_MAX) | 1 | `0x00000000` | `0xff800000` | `0x00000000` | | `0x3f800000, 0xf1800000, 0x71800000` (1.0, -2^100, 2^100) | 1 | `0x00000000` | `0x7f800000` | `0x00000000` | | `0x7f7fffff, 0xbf800000, 0x7f7fffff` (FLT_MAX, -1.0, FLT_MAX) | 1 | `0x00000000` | `0x7f800000` | `0x00000000` | | `0x3f800000, 0xdf800000, 0x5f800000` (1.0, -2^64, 2^64) | 1 | `0x00000000` | `0x7f800000` | `0x00000000` | | `0x5f400000, 0xdf200000, 0x7ef00000` (1.5 * 2^63, -1.25 * 2^63, 1.875 * 2^126) | 1 | `0x00000000` | `0x7f800000` | `0x00000000` | | `0xe40a117d, 0xbf800000, 0xe40a117d` (existing row, VCC set) | 1 | `0x00000000` | `0xff800000` | `0x00000000` | | `0x3f800000, 0xdf7fffff, 0x5f7fffff` (1.0, -(2^64 - 2^40), 2^64 - 2^40) | 1 | `0x00000000` | `0x00000000` | `0x00000000` | With VCC 0 every row gives `0x00000000` on hardware, before and after. Sweep: 11502 operand triples, each with VCC 0 and 1 (23004 rows), on gfx1101 in all 16 modes and through the recompiler before and after: the 256 table rows with both VCC values, 8126 exact cancellations (8120 with 2 <= |src2| <= FLT_MAX; every sign combination), 5736 nonzero near-cancellations with |src2| >= 2, ±0 operands, overflowing products with |src2| < 2, and 8000 random normal rows. - The change alters 1820 results: exactly the VCC=1 cancellations with |src2| >= 2^64. Before, they were `0x7f800000` (902) or `0xff800000` (918); hardware and the fix give `0x00000000` in all 16 modes. No other row changes. - Every exact cancellation is `0x00000000` on hardware in all 16 modes; the 28 rows giving `0x80000000` have a -0 product and src2 = -0 and match before and after. - Rows without denormals (22915), differing from hardware: IEEE=1 modes 1820 before, 0 after; IEEE=0 modes 1840 before, 20 after. The 20 have a signaling NaN input, which the hardware returns unquieted at IEEE=0 while the recompiler quiets it; unchanged here. - 89 rows whose hardware result is denormal with f32 denormals kept are not claimed: they are identical before and after and return the flushed ±0 (#981). ### 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 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
MarcZX10 commented 2026-10-09 03:11:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T03:11:06Z.


The hardware measurements and local full-suite results are documented in detail, but this head currently has no upstream check runs (gh pr checks reports none). CONTRIBUTING says a fork PR with an action_required workflow needs maintainer approval before CI has actually run. Could you check whether the Build/Conventions workflows are waiting for approval and request a rerun if so?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6073469391 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1963#issuecomment-6073469391). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T03:11:06Z. --- The hardware measurements and local full-suite results are documented in detail, but this head currently has no upstream check runs (`gh pr checks` reports none). CONTRIBUTING says a fork PR with an `action_required` workflow needs maintainer approval before CI has actually run. Could you check whether the Build/Conventions workflows are waiting for approval and request a rerun if so?
MarcZX10 commented 2026-10-09 03:14:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T03:14:26Z.


I checked the Actions API for this exact head: Build, Conventions and Progress report are marked action_required, so upstream CI has not run. Per CONTRIBUTING, a maintainer needs to approve those workflow runs; please request that approval.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6073509876 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1963#issuecomment-6073509876). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T03:14:26Z. --- I checked the Actions API for this exact head: Build, Conventions and Progress report are marked `action_required`, so upstream CI has not run. Per CONTRIBUTING, a maintainer needs to approve those workflow runs; please request that approval.
snoozedDev commented 2026-10-09 03:51:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by snoozedDev on 2026-10-09T03:51:10Z.


Re-measured on RDNA2: Ryzen 7 7700X iGPU (Raphael, gfx1036, not the PS5's gfx1013), tools/hw-oracle on Linux (hsa-rocr 7.2.4, LLVM 22.1.8), head ccef16b4. HelperCode words 10-18 unchanged, one HelperVectors row per lane, wave32, in the same 16 modes as above (IEEE 0/1 × f32 denormals 0/3 × f16/f64 denormals 0/3 × DX10_CLAMP 0/1, round modes 0, FP16_OVFL 0).

  • All 263 rows, all four columns, match the table bit for bit in the 8 IEEE=1 modes, including the test's mode. In the 8 IEEE=0 modes only the 10 existing sNaN rows differ, by quieting (0x7f800001 vs 0x7fc00001), as on gfx1101. The 7 new rows match in all 16 modes.
  • 750 random exact cancellations with 2^64 <= |src2| < FLT_MAX, each with VCC 1 and 0: v_div_fmas_f32 is 0x00000000 in every row and mode.
  • Hosts: agc_driver_division passes on an RTX 3090 (NVIDIA 610.57.04) and on the RADV iGPU (Mesa 26.2.2); with main's FloatInstructions.cpp both fail at vector 256 ... v_div_fmas_f32 is 0xff800000, expected 0x00000000.

Bodies, rows and per-mode results: https://gist.github.com/snoozedDev/c81c82507b46f9de87c5ecbdff54cde6 (pr1963-*). Generated with Claude Code (Claude Fable 5.1), driven and checked by me; the numbers are the hardware's output.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6073896745 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1963#issuecomment-6073896745). Originally posted by [snoozedDev](https://github.com/snoozedDev) on 2026-10-09T03:51:10Z. --- Re-measured on RDNA2: Ryzen 7 7700X iGPU (Raphael, **gfx1036**, not the PS5's gfx1013), `tools/hw-oracle` on Linux (hsa-rocr 7.2.4, LLVM 22.1.8), head ccef16b4. `HelperCode` words 10-18 unchanged, one `HelperVectors` row per lane, wave32, in the same 16 modes as above (IEEE 0/1 × f32 denormals 0/3 × f16/f64 denormals 0/3 × DX10_CLAMP 0/1, round modes 0, FP16_OVFL 0). - All 263 rows, all four columns, match the table bit for bit in the 8 IEEE=1 modes, including the test's mode. In the 8 IEEE=0 modes only the 10 existing sNaN rows differ, by quieting (`0x7f800001` vs `0x7fc00001`), as on gfx1101. The 7 new rows match in all 16 modes. - 750 random exact cancellations with 2^64 <= |src2| < FLT_MAX, each with VCC 1 and 0: `v_div_fmas_f32` is `0x00000000` in every row and mode. - Hosts: `agc_driver_division` passes on an RTX 3090 (NVIDIA 610.57.04) and on the RADV iGPU (Mesa 26.2.2); with `main`'s `FloatInstructions.cpp` both fail at `vector 256 ... v_div_fmas_f32 is 0xff800000, expected 0x00000000`. Bodies, rows and per-mode results: https://gist.github.com/snoozedDev/c81c82507b46f9de87c5ecbdff54cde6 (`pr1963-*`). Generated with Claude Code (Claude Fable 5.1), driven and checked by me; the numbers are the hardware's output.
johnmaia commented 2026-10-09 08:59:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by johnmaia on 2026-10-09T08:59:51Z.


NVIDIA and Intel results on Windows, to go with the RADV runs.

Windows 11, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, full ctest with ANYPS5_REQUIRE_VULKAN=1 on both GPUs of one machine: RTX 4080 (driver 617.42) and Intel UHD Graphics 770 (Raptor Lake-S, driver 101.6129, Vulkan 1.3.295), selected with ANYPS5_GPU. Base: main 6cc0503d plus #2093, since main alone does not build its tests right now (spirv/unified1/spirv.hpp not found).

RTX 4080 UHD 770
base 494/494 489/494
base + this PR 494/494 489/494

agc_driver_division passes on both GPUs. The five UHD 770 failures are the same tests with and without this PR (vop3_mad_i64, #1246, plus four filed as #2114), so nothing new fails.

Since the new v_div_fmas_f32 rows are only checked when HostFusesFma is true, I added a temporary print to confirm: it returns true on both the RTX 4080 and the UHD 770, so the new rows were checked on both.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6077780777 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1963#issuecomment-6077780777). Originally posted by [johnmaia](https://github.com/johnmaia) on 2026-10-09T08:59:51Z. --- NVIDIA and Intel results on Windows, to go with the RADV runs. Windows 11, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, full `ctest` with `ANYPS5_REQUIRE_VULKAN=1` on both GPUs of one machine: RTX 4080 (driver 617.42) and Intel UHD Graphics 770 (Raptor Lake-S, driver 101.6129, Vulkan 1.3.295), selected with `ANYPS5_GPU`. Base: `main` `6cc0503d` plus #2093, since `main` alone does not build its tests right now (`spirv/unified1/spirv.hpp` not found). | | RTX 4080 | UHD 770 | |---|---|---| | base | 494/494 | 489/494 | | base + this PR | 494/494 | 489/494 | `agc_driver_division` passes on both GPUs. The five UHD 770 failures are the same tests with and without this PR (`vop3_mad_i64`, #1246, plus four filed as #2114), so nothing new fails. Since the new `v_div_fmas_f32` rows are only checked when `HostFusesFma` is true, I added a temporary print to confirm: it returns true on both the RTX 4080 and the UHD 770, so the new rows were checked on both.
popcoder565 commented 2026-10-09 09:26:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T09:26:08Z.


NVIDIA data point. Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), GCC 15.2.0 (WinLibs r7), Release. Head ccef16b4 merged onto main d9484c71, together with #1979 and #1966.

agc_driver_division passes. The device fuses fma (the test doesn't print its "splits fma" message), so the new v_div_fmas_f32 rows are checked on NVIDIA too.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078179772 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1963#issuecomment-6078179772). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T09:26:08Z. --- NVIDIA data point. Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), GCC 15.2.0 (WinLibs r7), Release. Head `ccef16b4` merged onto `main` d9484c71, together with #1979 and #1966. `agc_driver_division` passes. The device fuses fma (the test doesn't print its "splits fma" message), so the new `v_div_fmas_f32` rows are checked on NVIDIA too. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
adriadam10 commented 2026-10-10 06:06:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:06:14Z.


Checked on the HW oracle (gfx1035, IEEE 0/1 × f32 denormals flush/keep, 15660 rows incl. 3000 exact cancellations with |src2| >= 2): every result this PR changes now matches hardware and nothing else moves; the |a*b| > 1 ⇒ multiple of 2^-47 argument holds. agc_driver_division fails without the fix and passes with it on RADV and CI's lavapipe. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094489555 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1963#issuecomment-6094489555). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:06:14Z. --- Checked on the HW oracle (gfx1035, IEEE 0/1 × f32 denormals flush/keep, 15660 rows incl. 3000 exact cancellations with |src2| >= 2): every result this PR changes now matches hardware and nothing else moves; the |a*b| > 1 ⇒ multiple of 2^-47 argument holds. `agc_driver_division` fails without the fix and passes with it on RADV and CI's lavapipe. Merging.
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!1963
No description provided.