shader: v_div_fmas_f32 flushes denormal inputs, misses the result flush in denormal mode 1 and rounds a scaled denormal result twice #10922
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 TechLevelZero on 2026-10-25T10:19:14Z.
What happens
On RDNA2
v_div_fmas_f32follows one rule in every f32 denormal mode:src0 * src1 + src2once, after scaling by 2^64 (VCC set, src2 exponent 128 or more) or 2^-64 (VCC set otherwise).FLOAT_MODEf32 denormal field is clear (modes 0 and 1).The translation (
TranslationContext::vDivFmasF32, with noVDivFmasF32entry inf32DenormalFlushFor) differs in three ways:DenormFlushToZero 32), the driver also flushes the inputs, and a flushed result can lose its signExamples (hardware, translation):
0x807fffff, 0xff7fffff, 0x3f0000000x408fffff0x3f000000(src0 flushed)0xff800000, 0x00000001, 0x1e8b9c7b0xff8000000xffc00000(inf * flushed 0)0x0becd7b0, 0x2535a659, 0x800000010x800000000x000000000x0becd7b0, 0x2535a659, 0x800000010x800000000x800000010x32d351d2, 0x1f800000, 0x9f8000000x800000000x802000000xbec6b7ec, 0x20000000, 0x008000000x8018d6fd0x8018d6feLLVM wraps
v_div_scale/v_div_fmasins_denorm_modeto keep f32 denormals when a function flushes them, so the one-ulp error in modes 2 and 3 is the case compiled divisions would hit.Title, test or build step
Found by differential testing of generated shaders against the hardware (
tools/hw-oracle), then measured with a directed sweep.AnyPS5 commit
Measured with a build of
d01070d6c3.vDivFmasF32and its denormal handling are unchanged onmainda6c581a02.System
tools/hw-oracle.Steps to reproduce
Run
v_div_fmas_f32 v10, v4, v5, v6, withvcc_loset to 0 or -1, throughtools/hw-oracleand through the recompiler. Use 728 operand triples: 512 random (with denormals, small exponents, zeros, infinities and the largest finite value) and 216 combinations of denormals, powers of two and ones. Run each f32 denormal mode, 0 to 3.Log
One rule reproduces every finite hardware result in all four modes with VCC clear and set: 5,536 of 5,536 rows. The rule is the exact
src0 * src1 + src2, times 2^±64 when VCC is set, rounded once to f32 with denormals kept, then flushed to a signed zero in modes 0 and 1. On the hardware, modes 0 and 1 give identical results, and so do modes 2 and 3.Fix
A fix is on the fork branch
fix/div-fmas-f32-single-rounding(04d5c84bf3, one commit onmainda6c581a02). On devices with the Float64 capability,v_div_fmas_f32is computed in f64:f32DenormalFlushFornow flushes only the result in modes 0 and 1.Devices without Float64 keep the previous lowering, and a debt entry in
docs/dev/debt/shader.mdrecords what that misses.agc_driver_divisiongains a check with 26 vectors from the sweep above, in each f32 denormal mode. It runs where Float64 is present.The branch was run against
mainwithANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, on the 1,401 teststools/select_tests.pyselects:VUID-errorsI'll open it as a pull request once my first one is merged (open-PR limit).
AI-assisted: yes (Claude Code)