fix(shader): detect f32 tininess after rounding when flushing products #2839
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2839"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-10T16:24:49Z.
What
With f32 denormals flushed (FLOAT_MODE 0xc0, the usual one),
v_mul_f32 v0, 0x3f7ffffe, v1withv1 = 0x00800001gives0x00000000. RDNA2 gives0x00800000.The exact product is 2^-126 * (1 - 2^-46). It is below 2^-126, but rounded to 24 bits it is exactly 2^-126. The tiny-product check from #1850 tests the exact product (tininess before rounding), so it flushes it. The hardware tests the product rounded to 24 bits with an unbounded exponent, in the shader's f32 round mode (tininess after rounding).
flushTinyProductnow flushes only when the rounded product is below 2^-126. A product that rounds up to 2^-126 gives 2^-126 with the product's sign. This needs one more 32-bit multiply (the low half of the significand product) and is settled for the FLOAT_MODE round mode (bits 1:0) at translation time. Everything else stays the same: callers, the nonzero-addend rule, #2020's constant-factor shortcuts (a power-of-two factor gives an exact product, so it skips the rounding check as well as the multiply-high), and the products themselves, which are still rounded to nearest even.v_mad_legacy_f32andv_mac_legacy_f32keep the exact-product check: on the console they round the product separately, and that encoding is not measured at this boundary (RDNA2 runs a fused form of it).Measured on RDNA2 (gfx1036, Radeon 610M) with
tools/hw-oracle, wave32, DX10_CLAMP=1, f32 denormals flushed. To nearest it was measured at IEEE=0 and IEEE=1 (same bits), and the other round modes at IEEE=0. Products are 2^-126 * (1 - D/2^47). At 24 bits, D < 2^22 rounds up to 2^-126 to nearest and D = 2^22 is a tie. Rounding away from zero (+inf for a positive product, -inf for a negative one) rounds up any D < 2^23:main, nearest0x00800001 * 0x3f7ffffe0x008000000x008000000x000000000x000000000x000000000x80800001 * 0x3f7ffffe0x808000000x800000000x808000000x800000000x800000000x008003e6 * 0x3f7ff8340x008000000x008000000x000000000x000000000x000000000x008005a8 * 0x3f7ff4b00x008000000x008000000x000000000x000000000x000000000x00f80000 * 0x3f0421080x008000000x008000000x000000000x000000000x000000000x008005a9 * 0x3f7ff4ae0x000000000x008000000x000000000x000000000x000000000x00800f95 * 0x3f7fe0d90x000000000x008000000x000000000x000000000x000000000x00800001 * 0x3f7ffffd0x000000000x000000000x000000000x000000000x000000000x00ffffff * 0x3f0000000x000000000x000000000x000000000x000000000x00000000v_mul_f32,v_mul_legacy_f32,v_fma_f32,v_fmac_f32,v_fmamk_f32,v_fmaak_f32andv_fma_mix_f32with all-f32 sources with a ±0 addend give the same bits when they evaluate the same tiny product.v_mullit_f32, which uses the same check since #2046, does too: on gfx1036 it gives the same bits asv_mul_f32on all 16 rows measured. The 6646451 row rules out "round to a denormal, then flush": with denormals kept, it gives0x00800000.Mixed f16/f32
v_fma_mix_f32uses the same after-rounding boundary. Fresh measurements on the same gfx1036 GPU cover f16 in either multiplicand slot, low and high half selection, both product signs, and f32 or f16 zero addends. With f320x00801002and f160x3bff, nearest gives0x00800000at IEEE=0 and IEEE=1;maingives zero. The adjacent f32 values0x00801001and0x00801003give zero and0x00800001. At IEEE=0, the tiny boundary also follows all four f32 round modes. Input conversion and source denormal handling are unchanged.The f32-source hardware table has 32 rows times 8 instructions; 220 of the 256 cells are tiny products. To nearest,
maindiffers from the hardware on 120 of them; in +inf and -inf on 74. With this change all 220 tiny-product cells match in every round mode. Toward zero,main's tiny-product cells already matched. Normal products and signed-zero cases outside this path still round to nearest in the other modes, as before (8 to 11 cells per directed mode, unchanged by this PR).The TechnicalDebt line on f32 denormals recorded this gap (
db491f0c); it now states the after-rounding rule instead, and that legacy mad/mac still use the exact product.Tested
CachyOS Linux, GCC, NVIDIA RTX 4070 Ti (615.78.08), Radeon 610M on RADV and lavapipe (Mesa 26.2.4). Tested on base
db343a934plus this change. After the rebase on57f96fd2b, the 22 shader and f32 denormal flush tests pass again on NVIDIA and RADV, and the flush test on lavapipe.agc_driver_f32_denormal_flushgets a second kernel with the instructions above on 32 rows. Each is checked against hardware in no float mode, 0xc0 and 0x00.v_mul_f32with and without a negated source is also checked with round modes +inf, -inf and zero. The original f32-source regression failed without the fix (lane 0 no float mode v_mul_f32 v10, v4, v5 is 0x00000000, expected 0x00800000). The current test also checks mixed sources and passes on both GPUs; its new negative-control failure is described below.v_mad_legacy_f32andv_mac_legacy_f32on the 32 rows, in all six modes. They give the same bits as onmain; with the after-rounding rule applied to them they would not (lane 0 no float mode v_mad_legacy_f32 v18, v4, v5, v6 is 0x00800000, expected 0x00000000).tools/check_conventions.py --base origin/mainandgit diff --checkpass.Checklist
mainNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-10T17:08:43Z.
Independent check on a Radeon 610M, at rows the PR's tests do not have: the exact nearest-even tie, the directed-mode threshold, and the directed modes on the product forms other than
v_mul_f32. The PR gives the hardware bits on all of them.Setup: PR head
d59efa47merged ontomain57f96fd2. Arch Linux, GCC 16.2.1, Release. Hardware rows fromtools/hw-oracleon a Radeon 610M (gfx1036, RDNA2), DX10_CLAMP=1, f32 denormals flushed, f16 denormals kept; each round mode at IEEE=0 and IEEE=1, wave32 and wave64, same bits in all four. Recompiler runs on an RTX 5080 (615.78.08) and on the Radeon 610M (RADV, Mesa 26.1.4), FLOAT_MODE0xc0 | round.Hardware. Each row was run as
v_mul_f32,v_mul_legacy_f32,v_mullit_f32(third operand 1.0),v_fma_f32with a+0addend andv_fma_f32with a-0addend. The five forms gave the same bits in every cell, so the table has one value per cell. P is the 48-bit significand product.3f042108 * 00f8000000800000008000000000000000000000bf042108 * 00f80000808000008000000080800000800000003f04796c * 00f75a8700000000008000000000000000000000bf04796c * 00f75a87800000008000000080800000800000003f7fffff * 0080000000000000000000000000000000000000bf7fffff * 00800000800000008000000080000000800000003f001802 * 00ffd00400000000008000000000000000000000bf001802 * 00ffd004800000008000000080800000800000003f000001 * 00fffffe00800000008000000000000000000000bf000001 * 00fffffe808000008000000080800000800000003effffff * 00ffffff00000000000000000000000000000000beffffff * 00ffffff80000000800000008000000080000000So the tie goes to 2^-126 to nearest, and a product that is exactly 2^-126 * (1 - 2^-24) stays flushed in every mode.
Recompiler against those bits (temporary test: 12 rows, 5 forms, 4 round modes, 240 results):
main57f96fd2main+ this PROn
mainevery difference is a0x00000000/0x80000000where the hardware gives 2^-126 with the product's sign.The PR's own tests (
ctest -R 'denormal|float|f32|mul|mad|fma', 30 tests) pass on both devices.Not measured:
v_mad_legacy_f32/v_mac_legacy_f32,v_fmac_f32,v_fmamk_f32,v_fmaak_f32andv_fma_mix_f32at these rows; products at or above 2^-126 in the directed modes.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T22:16:09Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cppL162-168docs/dev/TechnicalDebt.mdL405Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T22:57:18Z.
Checked on Linux (merged onto current
main): build + ctest green for the AgcDriver/Agc scope,agc_driver_f32_denormal_flushfails without theOperandAccess.cppchange and passes with it, also on CI's lavapipe. HW oracle on RDNA2 (gfx1035, IEEE=0, f32 denormals flushed, all four round modes): 706 rows around the 2^-126 boundary (ties, D = 2^22 / 2^23, several exponent splits, both signs, ±0 addends,v_fma_mix_f32with f16 in either slot) × 9 forms (v_mul,v_mul_legacy,v_mullit,v_fma,v_fmac,v_fma_mixf32 and mixed, negated source), 25416 cells: the PR's rule matches the hardware on every one. Also confirmed independently by @fma-rc on gfx1036, NVIDIA and RADV. Merging.