fix(shader): round v_div_fixup overflow and underflow in the MODE round mode #19563
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/19563"
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 Nawid3333 on 2026-11-13T15:00:26Z.
What
v_div_fixup_f16,v_div_fixup_f32andv_div_fixup_f64write the overflow value of the MODE round mode, andv_div_fixup_f32andv_div_fixup_f64also its underflow value, as RDNA2 does, instead of an infinity and a signed zero in every mode.FP16_OVFLstill writes ±0x7bff for the f16 form in every mode. Example:v_div_fixup_f16(0x7c00, 1.0, 1.0)withFLOAT_MODEround16 3 is 0x7bff on hardware;mainwrites 0x7c00.FLOAT_MODEdenormal field of that precision keeps denormal results, and a zero of its sign otherwise. It takes precedence over the overflow value, and the special cases of the denominator and numerator take precedence over both.The overflow test now checks that src0's exponent field is all ones instead of a NaN src0, so an infinite src0 takes the overflow value too. Before, it gave an infinity of the result's sign, which differs from the hardware where the round mode takes that sign toward zero. At round to nearest even the generated IR is the same as before except for that comparison; the
FP16_OVFLclamp ofv_div_fixup_f16moves from a check on the result to the overflow value.RoundingUseOflists the three opcodes as modelling a round mode set at run time (s_round_mode,s_setreg_imm32_b32), so a shader that sets a directed mode before them no longer throwsdoes not model directed rounding(f16, f32) or translatesv_div_fixup_f64at round to nearest even (f64).v_div_fixup_f32in f32 denormal modes 1 and 2 with a directed f32 round mode no longer throws: it flushes its denominator and numerator by bit 0 of the f32 denormal field, as at round to nearest even.docs/dev/debt/shader.mdkeeps that throw forv_div_scale_f32only.Tested
Windows 11 Pro, no title.
Hardware reference: RDNA2 (Ryzen 5 7600X integrated graphics, gfx1036, AMD Windows driver 32.0.21045.16010) through
tools/hw-oracle, wave32, onev_div_fixup_*per lane:v_div_fixup_f16over 2,366 triples (14 quotients x 13 denominators x 13 numerators: ±0, a denormal, 2^-14, 2^-12, ±1, 2^15, ±0x7bff, ±infinity, quiet and signaling NaNs) in all 64 combinations of round16 0-3, f16 denormal mode 0-3,FP16_OVFL0/1 and IEEE 0/1;v_div_fixup_f32andv_div_fixup_f64over 2,197 triples each (13 x 13 x 13 of the same classes plus the f32/f64 limits, 2^±100) and 343 underflow-boundary triples each (numerator and denominator exponents 150/151 and 1075/1076 apart, both signs) in all 32 combinations of the round mode, the denormal mode of that precision and IEEE 0/1.Every one of the 313,984 results equals the rule above.
maindiffers in 12,832 of them (3,400 f16, 4,716 f32, 4,716 f64; for f32 in denormal modes 1 and 2 it throws instead), all with a directed round mode. The PS5's gfx1013 is not measured.agc_driver_div_fixup_directed_rounding(new,DivFixupDirectedRounding.cpp) runs the three instructions on 28 rows (9 f16, 9 f32, 10 f64: infinite and NaN quotients of both result signs, x/0 and inf/y infinities, NaN denominators, 0/0, the f32/f64 underflow threshold and the largest finite values) in 18 float modes (round 0-3 x denormal fields 0x00/0xf0 xFP16_OVFL0/1, and round 1 with f32 denormal modes 1 and 2), then once more afters_round_mode 0xfin a shader declared at round to nearest even. Its expected values are the hardware results of the same instruction words; on hardware thes_round_moderun equals the declared round-toward-zero run.ctest -VwithVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1, on the Radeon Graphics iGPU and on the RX 9070 XT. Neither log reportswas not found but was requested by env var VK_INSTANCE_LAYERS, and none has aVUID-line:agc_driver_div_fixup_directed_rounding, this PRmainv_div_fixup_f16 FLOAT_MODE 0x5 FP16_OVFL 0 lane 1 is 0xfc00, expected 0xfbffroundedLimitswappedv_div_fixup_f16 FLOAT_MODE 0x5 ... lane 0 is 0x7bff, expected 0x7c00keepsDenormalResultsalways falsev_div_fixup_f32 FLOAT_MODE 0xf5 ... lane 13 is 0x0, expected 0x1agc_shader_mode_register,vector_half_double_output_modifiersand every execution test running av_div_fixup_*:div_fixup_f16,div_fixup_f16_denormal_modes,div_fixup_f64_denormal_modes,div_scale_fixup_f32_denormal_modes,division,division_output_modifiers,division_result_modifiers,f32_denormal_flush,f64_division,f64_division_modifiers,float16_modifiers,float16_ternary_clamp,fp16_overflow,nan_quieting), this PRmainThe two failures on the RX 9070 XT are
agc_driver_float16_modifiersandagc_driver_fp16_overflow, both onv_ldexp_f16(... v_ldexp_f16 ... is 0x00000000, expected 0x00008000), the same onmain;v_ldexp_f16takes no path this PR changes.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T04:06:33Z.
Review: COMMENTED
Reviewed — comments
The targeted code inspection found no verified blocking defect, but the review is incomplete and no checks were run.
What I ran
Inspected the prepared diff and targeted source ranges at head
3a7978b16130e66288218dc8c931fb10e83dbd73; no tests or build checks were run. The review bundle listed seven changed files, all included. I read selected ranges from the new execution test,TranslationContext.hpp,Float64Instructions.cpp,FloatInstructions.cpp,RoundingMode.cpp, andVectorInstructions.cpp, plus the changed documentation lines. Other portions of those files were not read.Results
Findings
Blocking
None verified in the inspected ranges.
Non-blocking
Nothing to report from the inspected ranges.
Not verified
The uninspected portions of the seven changed files, test discovery/registration, and runtime behavior were not verified. The hardware and test results described in the PR body are author-reported; I did not reproduce them.
To get this to a good state
Nothing is required. Optional: complete inspection of the remaining changed-file contents and run the reported checks.