Skip to content

Translator: one integer RTE narrower for d16 results (f32_to_f16_int folded into f16_bits), and tests for #711 - #806

Merged
ItsNotRexed merged 5 commits into
ItsNotRexed:mainfrom
fearkov:fix/last-packhalf2x16
Nov 22, 2026
Merged

ItsNotRexed merged 5 commits into
ItsNotRexed:mainfrom
fearkov:fix/last-packhalf2x16

Conversation

@fearkov

@fearkov fearkov commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in #374 as: #374 (comment)

What and why

#711 got to the remaining PackHalf2x16 sites first (the c_lz d16 result, d16 buffer loads, f16 buffer
stores) while this was open, so this PR is now what was left. @IAmNotSiob's review pointed out that main had two
integer round-to-nearest-even narrowers, #489's f32_to_f16_int/pack_f16_pair_int and #575's
f16_bits/f16_pack2. Main also had no test for #711's sites.

Commits:

  1. f16_bits without GLSL UMin (OpULessThan + OpSelect, the same bits on the sweep below). Translator: pack d16 image sample/gather results with integer f32->f16 RTE #489 keeps the
    d16 packer free of extended instructions, and tests/test_d16_pack.py checks it; with this, f16_bits fits there.
  2. pack_f16_pair_int narrows through f16_bits, and f32_to_f16_int goes. The two narrowers gave the same bits
    on 30,648 inputs: every f32 exponent with ties, the subnormal and overflow edges, NaN, and random mantissas. That
    was checked by translating v_cvt_f16_f32 shaders once with f16_bits and once with it swapped for
    f32_to_f16_int, run in the semantic interpreter. A control that swaps in round toward zero instead shows 3,941
    differences, so the swap does take effect. The old f16_bits against the new one: identical on all 30,648.
  3. d16 buffer loads pack with pack_f16_pair_int instead of f16_pack2 (rdna2spirv: fix v_fma_mix* f16 literals and replace remaining PackHalf2x16 sites #711 used the VALU packer). It rounds the
    same today, but Translator: MODE f16/f64 rounding from s_setreg FP_ROUND[3:2] (SHADER-35) #753 makes f16_pack2 follow MODE.FP_ROUND. A d16 load is converted by the buffer unit, not
    the VALU, like the d16 image results that already use pack_f16_pair_int.
  4. Tests for rdna2spirv: fix v_fma_mix* f16 literals and replace remaining PackHalf2x16 sites #711's sites: test_buffer_format_f16_narrowing_avoids_packhalf2x16 (a d16 load and a format store,
    spirv-val, no PackHalf2x16) and D16NormalizedRounding in tests/semantic/test_buffer_load_format.py. The
    latter pins the nearest-even words of three normalized bytes, where round toward zero, what PackHalf2x16 is on
    AMD drivers per Translator: pack d16 image sample/gather results with integer f32->f16 RTE #489, gives 1 ulp less.
  5. a "d16 and f16 format narrowing" row in docs/design.md. On AMD drivers about half of all unorm8, unorm16 and
    snorm8 d16 values (123 of 256, 31,742 of 65,536, 110 of 256) came out 1 ulp low before rdna2spirv: fix v_fma_mix* f16 literals and replace remaining PackHalf2x16 sites #711.

Tested

NVIDIA GeForce RTX 2060 SUPER, Linux, driver 620.70.06, llvm-mc 23.1.5, on top of dedd75c.

Check this branch
runtime/test/shaders/build.py 94 translated, 94 pass spirv-val
runtime/test/shaders/check.py 61 of 89 pass, 28 skipped
scripts/run_gpu_tests.py 71/98 passed, 27 skipped
python -m unittest discover -s tests/semantic OK
python -m pytest tests --ignore=tests/semantic 2176 passed, 40 skipped, 1 xfailed

The skips are the wave64 tests (32-wide subgroups here) and, in check.py, the mesh fixtures build.py doesn't build.
The d16 image tests (ps_texture_sample_d16, the gathers, ps_sample_c_lz) pass unchanged.

The runs above are from my own machine.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

@IAmNotSiob IAmNotSiob left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, that's the last PackHalf2x16 sites gone. small thing: main now has two integer RTE narrowers, #551's f32_to_f16_int/pack_f16_pair_int and #575's f16_bits/f16_pack2. might be worth using f16_bits here so there's one path to keep right (or folding one into the other in a follow-up). also needs a rebase, it conflicts with main. tests pass on its own base ^^ AI-assisted review (Claude Code).

An OpULessThan and OpSelect give the same shift (the same bits on 30,648 inputs). With no GLSL.std.450 left, f16_bits can narrow the d16 memory results, which ItsNotRexed#489 keeps free of extended instructions.
…t goes

main had two integer round-to-nearest-even narrowers: ItsNotRexed#489's f32_to_f16_int for the d16 image results and f16_bits for everything else. They gave the same bits on 30,648 inputs, so pack_f16_pair_int now uses f16_bits and there is one path to keep right.
f16_pack2 packs VALU results, and MODE.FP_ROUND will change how it rounds (ItsNotRexed#753). A d16 buffer load is converted outside the VALU, like a d16 image result, so it takes the memory packer.
@fearkov
fearkov force-pushed the fix/last-packhalf2x16 branch from 4e25dee to 238c416 Compare November 22, 2026 03:46
@fearkov fearkov changed the title Translator: no PackHalf2x16 left: the d16 compare result, d16 buffer loads and f16 buffer stores narrow with integer RTE Translator: one integer RTE narrower for d16 results (f32_to_f16_int folded into f16_bits), and tests for #711 Nov 22, 2026
@fearkov

fearkov commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @IAmNotSiob, done, rebased on dedd75c. #711 had already replaced the three sites, so this is now the fold you suggested, plus tests for #711's sites:

@shelbykb2

Copy link
Copy Markdown
Contributor

RDNA2 hardware measurement for the question in this PR's description ("round to nearest even for these conversions is inferred, not measured"): buffer_load_format_d16_* run as raw compute kernels on an AMD Radeon RX 6800 (gfx1030, not the console's gfx1013; the hw-oracle tool from the AnyPS5 project) through V#s built in the kernel. All 256 values of each 8-bit format and all 65,536 of each 16-bit format, destination pre-filled with 0xdead..... wave32 and wave64 agree in every word compared.

Normalized formats loaded as d16 (8_UNORM, 8_SNORM, 16_UNORM, 16_SNORM)

  • Your deciding case: byte 0xbe gives 0x39f6, nearest, not 0x39f5. The other bytes of your test agree too: 0xad → 0x396d, 0xde → 0x3af7, 0xef → 0x3b7f.
  • The rule that fits every value: the exact quotient (v/255, v/65535, v/127, v/32767) rounded once to f16, to nearest. Rounding toward zero misses 123, 110, 31,743 and 30,718 values of the four formats.
  • It does not follow the rounding mode: modes 1, 2 and 3 give the same words as nearest (8-bit formats complete, 16_UNORM on an 8,192-value sample).
  • It is a direct conversion, not narrow(f32 result): 16_UNORM 0xffef gives 0x3bff, while its f32 load is 0x3f7ff000, an exact f16 tie that would narrow to 0x3c00.
  • SNORM −128 and −32768 give −1.0 (0xbc00), the same as −127 and −32767.
  • d16_x writes only bits 15:0: the upper half kept the sentinel in all 131,584 loads. _xy/_xyzw pack x | y << 16 and z | w << 16.

Controls

  • The non-d16 buffer_load_format_x of the same data returns the correctly rounded f32 quotient for every value.
  • 32_FLOAT loaded as d16 rounds toward zero, not to nearest: 0x3f803000 (a tie) gives 0x3c01, 65520 and FLT_MAX give 0x7bff, inf gives 0x7c00; also independent of the rounding mode. So the same number arrives differently by format: 190/255 as 32_FLOAT gives 0x39f5, the 8_UNORM byte 0xbe gives 0x39f6.
  • 16_FLOAT loaded as d16 passes its 16 bits through unchanged, signalling NaNs included.

Against the code (a Python port of the decode on main and in this PR, evaluated on every value; the translator itself was not run, and main and this PR give the same numbers here):

Case Hardware main and this PR
8_UNORM, 8_SNORM d16 nearest match, 256 of 256 each
16_UNORM d16 one rounding differ at 2 of 65,536: 0xffaf (0x3bfd hw, 0x3bfe) and 0xffef (0x3bff hw, 0x3c00)
16_SNORM d16 one rounding differ at 2 of 65,536: 0x7ff7 (0x3bff hw, 0x3c00) and 0x8009 (0xbbff hw, 0xbc00)
32_FLOAT d16 toward zero, overflow 0x7bff differ: nearest even
upper half after d16_x kept differ: written as 0

So the nearest-even assumption in this PR holds for the 8-bit formats exactly and for the 16-bit formats except four values that come from rounding twice (to f32, then to f16).

Not measured. The typed stores (buffer_store_format_d16_*): I stopped the run before them. One extra kernel I added, a 16_16_16_16_UNORM load, hung the driver (22 s, no output), and I did not dispatch anything from this probe afterwards, so there is no hardware data for this PR's store site. Also not measured: 16_16_16_16 formats, other DST_SEL or OOB_SELECT values, idxen, the image d16 paths, gfx1013 itself. The 32_FLOAT NaN payload rule is fitted to 8 values only.

35 completed dispatches, 0.7 to 2.8 s each. AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants