Skip to content

d16 typed buffer loads round the exact UNORM/SNORM quotient once, as RDNA2 does (BUILD-161) - #1759

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
BetterJdue:d16-rounding-probe
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
BetterJdue:d16-rounding-probe

Conversation

@BetterJdue

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: BUILD-161 (claimed in #1548; the tracker is now #1745)

What and why

docs/design.md narrowed the f16 of a typed d16 buffer load from the f32 decode, rounding to nearest even, and said that rounding was inferred, not measured. @shelbykb2 measured it on an RX 6800 in #806: the exact quotient is rounded once, straight to f16. The translator still rounds twice (f32 first, then f16_bits).

This PR adds:

  • tools/d16_format_probe.py, built on tools/hw_oracle. It loads every UNORM8, SNORM8, UNORM10, UNORM16 and SNORM16 value through buffer_load_format_d16_x, tbuffer_load_format_d16_x and buffer_load_format_x, in wave32 and wave64, and counts which candidate rule each result fits. The kernels write their own inputs. Controls: a 16_FLOAT load, the kept high half of d16_x, and the input words read back.
  • The translator fix. Rounding twice is 1 ulp high on exactly four values: UNORM16 0xffaf and 0xffef, SNORM16 0x7ff7 and 0x8009. Over every channel width from 1 to 16 bits, only two f32 quotients land on an f16 halfway point (0x3f7ff000 and 0x3f7fb000). Gen.d16_norm_f16 takes the f16 below for those two, for UNORM/SNORM formats only; a 32_FLOAT 0x3f7ff000 is a real tie and still rounds to even. This costs a few instructions per d16 component and no new function.
  • docs/design.md ("d16 and f16 format narrowing") now says measured for typed buffer loads. Image d16 stays inferred.

Tested

  • On an RX 6700 (gfx1031) with AMD Windows driver 32.0.21045.29003 and the ROCm wheel's clang:

    Format Values d16 once to f16 d16 via f32 d16 via f32, toward zero f32 result
    UNORM8 256 256 256 133 256
    SNORM8 256 256 256 146 256
    UNORM10 1,024 1,024 1,024 514 1,024
    UNORM16 65,536 65,536 65,534 33,796 65,536
    SNORM16 65,536 65,536 65,534 34,820 65,536

    The f32 result column is the correctly rounded f32 quotient. MUBUF matched MTBUF and wave32 matched wave64 everywhere, 16_FLOAT returned its own bits for all 65,536 encodings, and the sweep took 36 s.

  • tests/test_d16_format_probe.py (17 tests, including one on the GPU) passes on that machine.

  • Here: tests/semantic (1455 tests, with 5 new ones in test_buffer_load_format.py that fail without the fix), tests/test_translate.py (469 tests) and tests/test_d16_pack.py pass. spirv-val accepts translated d16 MUBUF/MTBUF loads.

  • Not run: translated shaders on a GPU. A gfx1036 run (@abnalstlll-byte offered one) or the RX 6800 would be a good second check.

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.

AI-assisted change and verification using synthetic inputs.

…RDNA2 does (BUILD-161)

docs/design.md narrowed the f16 of a typed d16 buffer load from the f32 decode with round to nearest even, and
called that rounding inferred, not measured. tools/d16_format_probe.py measures it through tools/hw_oracle: every
UNORM8, SNORM8, UNORM10, UNORM16 and SNORM16 value through buffer_load_format_d16_x, tbuffer_load_format_d16_x
and buffer_load_format_x, in wave32 and wave64, against several candidate rules, with a 16_FLOAT load, the kept
high half of d16_x and the staged inputs as controls.

On an RX 6700 (gfx1031) the hardware rounds the exact quotient once, straight to f16. Rounding to f32 first is
1 ulp high on exactly four values (UNORM16 0xffaf and 0xffef, SNORM16 0x7ff7 and 0x8009); over every width up to
16 bits only two f32 quotients land on an f16 halfway point (0x3f7ff000, 0x3f7fb000). Gen.d16_norm_f16 takes the
f16 below for those two, for normalized formats only. The f32 (non-d16) result was already right.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Pierre-Kookis

Copy link
Copy Markdown
Contributor

Independent hardware run of tools/d16_format_probe.py at 9fa652f7 on a second RDNA2 chip. It supports the PR's rule.

Setup: AMD Radeon Graphics, the Ryzen 7 7800X3D iGPU (gfx1036, RDNA2). Windows 11, AMD Windows driver 32.0.21043.5001 (Vulkan driverInfo 26.3.1), HIP runtime amdhip64_6.dll from the driver. AMD clang/lld 23.1.6 (conda-forge), LEONIDA_HW_ARCH=gfx1036.

Default sweep, all formats, waves 32 and 64; exit 0.

format values rte (buffer_d16 / tbuffer_d16) f32_rte rte_flush buffer_f32 f32_rte
unorm8 256 256 / 256 256 256 256
snorm8 256 256 / 256 256 256 256
unorm10 1024 1024 / 1024 1024 1024 1024
unorm16 65536 65536 / 65536 65534 65533 65536
snorm16 65536 65536 / 65536 65534 65534 65536
  • d16 follows rte, the exact quotient rounded once to f16, on every value. The f32 step misses the two values per 16-bit format the docstring names, and the flushed variants miss the smallest denormal results, so this GPU does not flush them.
  • The f32 (non-d16) load follows f32_rte on every value.
  • Controls: wave32 = wave64, and MUBUF = MTBUF, with 0 differences in every format. float16 d16 returns its own bits, 65536/65536.

(AI-assisted run; synthetic kernels, no game files.)

# Conflicts:
#	docs/design.md
#	tools/rdna2spirv.py
@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 6b5c01da4988f51b550e44fe0fa609fd59c02244 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Independent translated Vulkan check on Linux 7.0.0-43-generic with a GeForce RTX 5090, NVIDIA driver 595.99.02, LLVM 21.1.8, and SPIRV-Tools 2026.1.

All ten result words matched in a synthetic wave32 fixture. The four distinguishing UNORM16 and SNORM16 inputs matched single rounding through both MUBUF and MTBUF, and every load preserved the destination's upper half. A true 32_FLOAT halfway control rounded to even through both paths. Returning the uncorrected f32-to-f16 result from the narrowing helper changed all eight normalized outputs by one f16 ULP and left the two float outputs unchanged.

Both versions passed spirv-val, and all 32 focused buffer-format semantic tests passed. This checks the translated paths against the measured rule.

AI-assisted verification.

@Rossrocha

Copy link
Copy Markdown

Ran the probe at 6b5c01d on a third RDNA2 die, an AMD Radeon RX 6600 (Navi 23, gfx1032): AMD 26.11.2 (Vulkan driver 32.0.21045.29003), HIP runtime amdhip64_6.dll from the driver, conda-forge clang/lld 23.1.6, LEONIDA_HW_ARCH=gfx1032, Windows 11 Pro 10.0.26300. Same result as the RX 6700 and gfx1036: one rounding (rte) on every value.

python -I tools/d16_format_probe.py (all formats, waves 32 and 64), exit 0:

hardware oracle: device=AMD Radeon RX 6600 arch=gfx1032
unorm8   (256):   buffer_d16/tbuffer_d16 rte 256;     buffer_f32 f32_rte 256;     off the rule 0
snorm8   (256):   buffer_d16/tbuffer_d16 rte 256;     buffer_f32 f32_rte 256;     off the rule 0
unorm16  (65536): buffer_d16/tbuffer_d16 rte 65536 (f32_rte 65534, rte_flush 65533); buffer_f32 f32_rte 65536; off the rule 0
snorm16  (65536): buffer_d16/tbuffer_d16 rte 65536 (f32_rte 65534, rte_flush 65534); buffer_f32 f32_rte 65536; off the rule 0
unorm10  (1024):  buffer_d16/tbuffer_d16 rte 1024;    buffer_f32 f32_rte 1024;    off the rule 0
float16  (65536): buffer_d16/tbuffer_d16 own bits 65536/65536

Every format: wave32/64 differ 0, MUBUF/MTBUF d16 differ 0. As on gfx1036, rte_flush misses a few of the smallest 16-bit results (65533 and 65534), so denormals are kept.

python -I tests/test_d16_format_probe.py with the oracle available: 18 tests OK, none skipped (OnHardware.test_unorm8_controls_hold_and_both_wave_sizes_agree included). Without the env vars, 2 skip as designed.

On the earlier run of the merged branch: semantic 1572 OK, run_gpu_tests.py 161/170 with 9 expected skips, native and LEONIDA_W64_EMULATE=1. Note that GitHub now reports a conflict with main, in docs/design.md.

AI-assisted (Claude Code): it set up the oracle toolchain, ran the probe and wrote this comment.

@Spaghetih

Copy link
Copy Markdown
Contributor

Tested 6b5c01da on NVIDIA GeForce RTX 4070 Ti, driver 621.24, Windows 11, MSVC (VS 2022), Vulkan SDK 1.4.363, llvm-mc 21.1.8 in WSL: pass.

No regression against main on the same machine (ctest 97/98 with the same d3d12_draw failure; GPU suite 127/166 passed, 39 skipped, 0 failed).

AI-assisted (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit 18d6f16 into ItsNotRexed:main Nov 22, 2026
10 of 11 checks passed
evilclanker9000 pushed a commit to evilclanker9000/Leonida that referenced this pull request Nov 23, 2026
…RM rounding, two-component routing), all 77 codes

@AwkGrep256 ran tools/buffer_format_fuzz.py on a real RDNA2 (gfx1036, ItsNotRexed#2022): the same totals as the
Radeon 880M, 0 mismatches, and 199,144 elements of the 14 GFX10-only codes, also 0. This follows both.

- 16_FLOAT, 16_16_FLOAT and 16_16_16_16_FLOAT format stores round toward zero, saturate at +-65504 and
  keep a NaN a NaN (Gen.ufloat_encode_bits, now with an optional sign). d16 data rounding (ItsNotRexed#1759) is
  not touched.
- Every UNORM, SNORM and SCALED store rounds the exact product once (Gen.norm_encode_exact): 0.3 *
  65535 stores 19661, not 19660.
- A two-component format stored through a V# takes channel y as its second component when DST_SEL_Y is
  Y and DST_SEL_Z a constant (the runtime's X,Y,0,1: gfx1036 narrow-integer fixtures, gfx1150), and
  channel w otherwise (identity XYZW, @AwkGrep256 and gfx1150); _xy then writes (x, 0). tbuffer keeps
  (x, y). A probe with XYZW alone would have shown (x, w) for every V# and broken the runtime's
  selectors, which the oracle now also covers (XY01 runs for the two-component formats).
- Gen.FORMATS has all 77 codes: 30-35 and 37-42 (10_11_11 and 11_11_10 with UNORM..SINT) join.
- buffer_format_fuzz: the model has one store route per path, select_formats runs all 77 codes by
  default on a GFX10 device, the known-difference count is gone (0 on the 880M).

The three expectedFailure tests pass; the fixtures that baked the old behaviour (cs_fmt_sint32, test_translate
f16 and int32 store tests, test_value_semantics) take the measured words. Sizes: cs_store 206 KB on
main, 125 KB here.

Co-Authored-By: AwkGrep256 <AwkGrep256@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.

6 participants