shader: round f16 ALU results to nearest even on every host #1598
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1598
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1598/head"
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?
@Nawid3333 on GitHub · 2026-10-17 13:35 UTC · original
Nawid3333:shader/f16-round-nearest-even→main· +137 −11 · 4 files · 2 commits · closed without mergingKyty computes f16 ALU results in f32 and converted them to f16 with
PackHalf2x16(EmitConvertF16F32, and thePackHalf2x16IR op behind packed f16 math and the f16 unary/trig ops). On RDNA2,V_CVT_F16_F32and f16 arithmetic round to nearest even. AMD drivers compilePackHalf2x16tov_cvt_pkrtz_f16_f32/v_cvt_pk_rtz_f16_f32, which round toward zero (see #1575), so with AMD's own driver every inexact f16 result was truncated. On the Ryzen 7600X iGPU (RDNA2, AMD's non-LLPC compiler) even a plainOpFConvertto f16 compiles tov_cvt_pkrtz_f16_f32unless the module setsRoundingModeRTE 16.Change:
EmitF32ToF16RneBitsconverts with integer operations, like yourEmitF32BitsToF16RtzBits(9f88043e, renamed in59d8256b): round to nearest even, also into the f16 denormals; finite values from 65520 up become infinity; NaN keeps the top mantissa bits and gets the quiet bit.EmitConvertF16F32uses it, andTranslator::PackHalf2x16packs both halves throughConvertF16F32, so thePackHalf2x16IR op has no producers left and is removed. D16 format loads and formatted stores are not changed here: they are format conversions in the hardware, which round toward zero and keep NaN payloads (measured on both GPUs). The stores are #1588; the D16 loads can follow the same way.Tests
CvtF16F32RoundsToNearestEven: 24 inputs (ties whose lower half has an even and an odd last bit, values just above a tie, 65504 / 65520 / 2^16 / f32 max, the denormal range (the smallest denormals give +0 and -0), NaN payloads) loaded from memory and converted with V_CVT_F16_F32; the expected values are whatv_cvt_f16_f32gives on both GPUs.F16AddRoundsToNearestEven: V_ADD_F16 and V_PK_ADD_F16 sums that fall halfway between two halves, and one that rounds to infinity; the expected values are whatv_add_f16/v_pk_add_f16give on both GPUs.PackHalf2x16in the SPIR-V and pass on both GPUs, also with the harness forced to 32-wide subgroups (a local switch), with no validation-layer messages. With the tie-breaking bit left out of the rounding (a local mutant), both fail in every configuration.VectorFractF16CapturedAndEdges,VectorFractF16Modifiers,VectorCosF16CapturedSdwaAndEdgesandVectorSinF16SdwaAndEdgesrequiredPackHalf2x16in the SPIR-V; that requirement is removed, the rest of each test is unchanged and their results pass.VectorFractF16CapturedAndEdges: sincee864cb20, V_FRACT clamps its result below one and the test expects0x3bfffor the tiny negative inputs, the hardware result of my #1209 measurement; it passes on main and with this change.EmitF32ToF16RneBitsagainst the GPUs' ownv_cvt_f16_f32for all 2^32 f32 inputs: 0 differences on the RX 9070 XT (RDNA4) and the Ryzen 7600X iGPU (RDNA2), NaNs included.packHalf2x16differs from it on 2,231,337,984 inputs on both GPUs.6aa3846f: the other 723 compute-suite entries in both subgroup modes and the 30 graphics cases keep their status. After merging main (c67352d5), on the RX 9070 XT compared with main: every other compute-suite entry keeps its status, natively and with 32-wide subgroups, and both new tests pass on the iGPU too.shader_cfg_tests(not run by CI) passes.9ebe1892(the merge with main).If you'd like this done differently, tell me and I'll change it.
Written with Claude Code (AI): it wrote the change and ran the tests and measurements on my PC (drivers 26.8.1 and 26.10.1, Windows 11).
🤖 Generated with Claude Code
@coderabbitai[bot] on GitHub · 2026-10-17 13:39 UTC · original · edited 2026-10-28 19:39 UTC
No actionable comments were generated in the recent review.
5f4bf4ae-5682-489a-9cab-c050351c1ecaReviewing files that changed from the base of the PR and between
da867ea141and9ebe189248.src/graphics/shader/recompiler/backend/spirv/spirvEmitterAlu.cppsrc/graphics/shader/recompiler/frontend/translate/Translate.cppsrc/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.inctests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Walkthrough
The SPIR-V backend now converts F32 bit patterns to F16 with integer round-to-nearest-even logic, including subnormal handling, overflow, sign preservation, and NaN encoding. The translator converts each packed F32 input to F16 and combines the resulting bits. The
PackHalf2x16IR opcode is removed. Compute tests add conversion and F16 addition cases, and update SPIR-V expectations.Suggested reviewers:
nmzikPriority:
Normal
Change: Bug fix
Merge Risk:
Minimal · up to
9ebe1No identified issue needs resolution before merge; proceed with normal checks.
Pre-merge checks:
5
Full details: Title check
Explanation
The title names the main change: f16 ALU results are rounded to nearest even on every host. The changeset adds an integer-based F32-to-F16 round-to-nearest-even converter, uses it in EmitConvertF16F32 and Translator::PackHalf2x16, and removes the PackHalf2x16 IR opcode. The title is specific and matches the objectives. It does not mention the removed opcode or the D16 exclusion, which is acceptable.
Full details: Description check
Explanation
The description covers the change in detail. It explains the AMD truncation problem caused by PackHalf2x16, the new EmitF32ToF16RneBits converter, the removal of the PackHalf2x16 IR op, the excluded D16 paths, the added tests, and the verification measurements. It is directly related to the changeset.
Full details: Docstring Coverage
Explanation
No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 2 files. (1 skipped: 1 too large.)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@NeySlim on GitHub · 2026-10-17 23:09 UTC · original
RADV data point: Radeon 8060S (RDNA 3.5), Mesa 26.2.4, against
763d3be3. The two new tests fail on main here the same way (with thePackHalf2x16check removed):CvtF16F32RoundsToNearestEvengets0x3c03for0x3c04,0x7bfffor infinity and truncated denormals, and ACO emitss_cvt_pk_rtz_f16_f32forPackHalf2x16. With the PR both pass,VectorFractF16CapturedAndEdgesgoes from fail to pass, and the rest of the suite is unchanged.Pull request closed