fix(shader): round scalar f16 arithmetic in directed rounding modes #19505
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/19505"
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 Ab1jithK on 2026-11-13T12:35:18Z.
What
Scalar
v_add_f16,v_sub_f16,v_subrev_f16,v_mul_f16,v_fma_f16,v_fmac_f16,v_fmamk_f16andv_fmaak_f16now follow the f16 round field toward +inf, -inf and zero, declared inFLOAT_MODEor set bys_round_mode. Onmainthey areUnmodelledinRoundingUseOf, so a directedFLOAT_MODEdeclared from the start passedcheckRoundingUse(nothing changed) and they rounded to nearest even without an error, as reported on #981 (for examplev_add_f16 0x3c00, 0x1000toward +inf: hardware0x3c01, recompiler0x3c00).Sources: the RDNA2 ISA §3.5 Table 4 (the f16 round field) and the instruction definitions; the edge behaviour below was measured with
tools/hw-oracleon RDNA2 (gfx1036), since the ISA does not settle tininess, NaN choice or signed zeros under directed rounding. The PS5's gfx1013 is not measured.How it rounds (
Translation/src/FloatInstructions.cpp):FPInterpolateF16, asv_pk_add_f16(#16739) andv_fma_mix*_f16do: add asa * 1 + b, the subtrahend negated in its own position (v_subrev_f16is-src0 + src1), multiply asa * b + 0with the product's sign on a zero result.0x0400 * 0x9000 + 0x0400toward +inf flushes to0x0000, while0x3c01 * 0x03ffrounds to0x0400and is kept. Bit 0 flushes inputs as before.0xfe00for an invalid operation, an invalid product (inf * 0) winning over a NaN src2. On the RTX 3060 a float negate of a NaN gives0x7fff, so the subtrahend's sign is applied to its raw half bits.RoundingUseOf(RoundingMode.cpp) moves the eight instructions fromUnmodelledtoDirected16, so a run-times_round_modebefore them translates like the declared mode instead of throwing. Nearest-even keeps its existing lowering.Tests:
agc_driver_f16_directed_rounding(tests/execution/F16DirectedRounding.cpp): one kernel runs all eight instructions on 1024 lanes (every pair of 32 halves, a varying src2), for each directed mode, every f16 denormal field, IEEE=0/1 and FP16_OVFL=0/1, wave32 and wave64, with and without the Float64 capability (12 CTest cases). Expected values come from an exact reference (__int128product-sum, rounded and flushed as above) that is checked against 19 rows pinned from the gfx1036 measurements. The same reference matches all 997,888 measured hardware rows below.agc_shader_mode_register: theunmodelledcheck now usesv_fract_f16(still unmodelled), and a newdirectedF16Arithmeticcheck requires a run-times_round_modebeforev_add_f16to translate like the declared mode.Overlap: #18487 adds
v_cvt_f32_f64toRoundingUseOfnext to the lines moved here, a small textual conflict for whichever merges second. Not changed here: nearest-even keeps the existing lowering, including its known IEEE=0 signaling-NaN quieting, thev_subrev_f16NaN order and the native f16 path's signaling NaNs (each also onmain); f32 directed rounding (v_add/mul/fma_f32) stays as onmainand is a separate change.Tested
Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 r7 UCRT, Release,
BUILD_TESTING=ON. Headd0f305089c, rebased onmain80b0bdf264(the only conflict was theTranslationContext.hppdeclaration next to #9889'sfloatUnaryValue); the hardware comparison, full-suite run and base-vs-change validation below were made on the previous base0294cba8dc.tools/hw-oracle, HIP runtime vsHW_ORACLE_RUNTIME=vulkanwithagc_oracle_runner, same GPU) on AMD Radeon Graphics1002:164e(gfx1036): the eight instructions over 961 operand pairs (4913 triples forv_fma_f16/v_fmac_f16) in every f16 round and denormal field, at (IEEE, DX10, FP16_OVFL) = (1,0,0), (0,0,0), (1,0,1), (0,1,1): every directed-mode row matches. The remaining differences are all nearest-even and identical onmain.ctest -R "^agc_shader_mode_register$|^agc_driver_f16_directed_rounding"withANYPS5_REQUIRE_VULKAN=1: 14/14 on both GPUs. Negative control: withmain's recompiler sources and this test, 12/12 directed cases fail on both GPUs (e.g.0 - 0toward -inf gives0x0000, expected0x8000).^agc|^shader|recompilerrun (1683 tests) on both GPUs: onlyagc_driver_sample_order_grid_referencefails on the Radeon, as onmain(#9507, fixed by #9503).^agc.*(f16|float16|_rounding)|^agc_shader_mode_register$withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer,ctest --test-dir build -R <pattern> -V, on the base0294cba8dcand on this change, on both GPUs. Every log showsInsert instance layer "VK_LAYER_KHRONOS_validation"for each of the 77 tests that create a Vulkan instance (the other 6 are SPIR-V codegen tests that select no GPU) and nowas not found but was requestedline. Base 69/83 (the 13 new directed-rounding cases andagc_shader_mode_registerfail), this change 83/83; noVUID-message on either side. After the rebase, the same focused pattern (now 100 tests, including #16889'sagc_driver_packed_mul_f16_rounding) passes 100/100 on both GPUs with the layer inserted for the 94 tests that open a device and noVUID-message.tools/check_conventions.py --base origin/main: 0 errors; the one warning is the existing length of the nearest-even f16 denormal entry (1827 characters onmain), where this PR changes one sentence.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)1002:164e), AMD Windows driver 32.0.21042.62, Windows 11; NVIDIA GeForce RTX 3060 (10de:2504), driver 32.0.16.2101, Windows 11🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T02:45:04Z.
Review: DISMISSED
Reviewed — changes requested
Changes are required before merge: fix the
v_fmaak_f16test instruction so its reference uses the actual literal addend.What I ran
Reviewed head
c6cbac5424d1eccf9f64a455c464469113af2db5usingrepo_read.pysearch and targeted reads. No tests were run in this review.Results
Findings
Blocking
Codeindex 15 isv_fmaak_f16, butExpectedmodels operation 6 asa * 0x3c01 + b. The decoder finalizesv_fmaak_f16withsource2as a literal constant and itssource1field empty; the existing assembly fixture also shows the instruction takes a literal addend (v_fmaak_f16 ..., 0x0001). Thus the test compares a result using the encoded literal addend against a reference using varyingb, causing false failures and leaving the intended arithmetic case unvalidated. Encode a genuinely varying-addend operation or make the reference use the actual literal.core/libs/prx/libSceAgcDriver/tests/execution/F16DirectedRounding.cpp:220.Non-blocking
None.
Not verified
No tests or build were run. Reviewed changed files:
ShaderModeRegisters.cpp,agc_driver_f16_directed_rounding_tests.cmake,F16DirectedRounding.cpp,TranslationContext.hpp,FloatInstructions.cpp,RoundingMode.cpp, andshader.md.manifest.jsonlists all seven changed files as included; no changed file was excluded from review. Related decoder, SPIR-V interpolation, modifier, and CMake context were read selectively.To get this to a good state
Correct the
v_fmaak_f16test input/reference mismatch, then rerun the directed-rounding execution tests and shader mode register test.Pull request closed