feat: d16 typed buffer loads and stores (buffer/tbuffer_*_format_d16_*) in the RDNA 2 interpreter and HLSL — 1,078/1,490 #223

Closed
github-bot wants to merge 1 commit from gh-pr/223/head into gh-pr/223/base
Owner

@⁠KainanP on GitHub · 2026-11-25 17:37 UTC · original
KainanP:feat/buffer-format-d16 → main · +684 −59 · 17 files · 1 commits · closed without merging

Change

The 18 d16 typed buffer encodings that main still refused now run in the reference interpreter and translate to HLSL: buffer_load_format_d16_{x,xy,xyz,xyzw,hi_x}, buffer_store_format_d16_{x,xy,xyz,xyzw,hi_x}, tbuffer_load_format_d16_{x,xy,xyz,xyzw}, tbuffer_store_format_d16_{x,xy,xyz,xyzw} (the full list of _format_d16 names in MNEMONICS where rdna2_exec.executable was false). The PR builds on the 32-bit typed formats of #150 and reuses its format tables, DST_SEL handling, bounds and measured conversions.

Interpreter coverage: 1,060 → 1,078 of 1,490 encodings (rdna2_exec.coverage()). The README still said 1,046, so it now says 70% → 72% in all 9 languages, and the test count goes to 1200+ (1,250 collected).

Rules and sources

AMD, "RDNA 2" Instruction Set Architecture, November 2020 (archived copy from docs/research/rdna2-sources.md, SHA-256 22aec03f…4795, printed page numbers):

Rule Source
RDNA 2 is packed d16: component i goes in VGPR VDATA + i/2, low half first (xyzw uses 2 VGPRs) §8.1.2 p. 56 ("BUFFER_LOAD_FORMAT_D16_XYZW writes two VGPRs"), §8.1.4 p. 59 ("a pair of data are stored in each 32-bit VGPR (LSBs first, then MSBs)"); LLVM 20.1.8: FeatureUnpackedD16VMem is set only for GFX8 (AMDGPU.td), and the packed TBUFFER_LOAD_FORMAT_D16_XY uses VGPR_32 (BUFInstructions.td)
_d16_x writes bits 15-0, _d16_hi_x bits 31-16, and the other half is kept §12.14 p. 215 / p. 211 pseudo-code D0[15:0] = MEM[ADDR] / D0[31:16] = MEM[ADDR]; §8.1.6 p. 64; AMD RDNA 2 XML: "store the result into the low/high 16 bits of a 32-bit vector register"
A d16 load converts the 32-bit result of the matching non-d16 load to 16 bits §8.1.4 p. 59: "data returned from the texture unit is converted to 16 bits … Control over int vs. float is controlled by FORMAT"; §8.1.3 p. 58: integer formats come back as integers of the storage type
Stores: the texture unit converts each 16-bit half to the memory format §8.1.4 p. 59; XML: "Convert 16 bits of data from the low/high 16 bits of a 32-bit vector input register"

Applied conversions:

  • Loads. UINT/SINT formats of 16 bits or less give the low 16 bits of the zero- or sign-extended field. Every other format (UNORM, SNORM, USCALED, SSCALED, FLOAT) goes float32 → float16. A 16-bit FLOAT field comes back unchanged, signalling-NaN payload included.
  • Stores. Each half is widened to the value the non-d16 store takes: UINT is zero-extended, SINT sign-extended, float16 → float32 is exact for 32-bit FLOAT formats (NaN payload shifted left by 13, as measured in #150), and a 16-bit FLOAT field is copied. The value then goes through #150's measured saturation (UINT to 2^n−1, SINT to the field's range).
  • DST_SEL = 1 gives 1.0 (0x3C00) for float, normalized and scaled formats and the integer 1 otherwise: #150's measured value, narrowed like any other component.

Assumptions (not measured, stated in emu/rdna2_buffer_d16.py)

  1. float32 → float16 narrowing uses IEEE round to nearest even, overflow to infinity, and float16 subnormals are kept. This extends #150's measured round-to-nearest; nothing documents the narrowing. It is a double rounding, so it differs by one ulp from rounding the exact quotient once for exactly three fields: 16_UNORM 65455 (0x3BFE instead of 0x3BFD), 16_UNORM 65519 (0x3C00 instead of 0x3BFF) and 16_SNORM 32759 (0x3C00 instead of 0x3BFF). I checked every quotient c / (2^n − 1) for n = 1 to 16 exhaustively: only 4 differ, and the fourth (n = 14) matches no format. These three are pinned in the tests and are the cheapest hardware measurement that would settle the order.
  2. A float32 NaN keeps its sign and the top 10 payload bits, with no quiet bit added: the inverse of the widening measured in #150.
  3. The high half of the second VGPR of _d16_xyz is kept, like the high half of _d16_x. The ISA only gives pseudo-code for _x and _hi_x.

Refused by name (no fake success)

  • d16 loads of 32-bit integer formats (32_UINT, 32_32_SINT, …): whether 32 → 16 bits truncates or clamps is not documented.
  • A float32 NaN whose top 10 payload bits are zero: under assumption 2 it would turn into infinity.
  • d16 stores to UNORM/SNORM/SCALED formats. They stay refused, as in #150.

The HLSL follows the module's existing convention: what the interpreter refuses at run time (MUBUF format read from the V#, NaN) reads zeros there.

HLSL

BufHalfFromFloat uses integer operations only, with no double and no f32tof16 (Intel UHD has no FP64, and f32tof16 rounding is driver-defined). BufNarrow and BufWiden sit on top of #150's BufDecode and BufEncode. BufField now copies a 16-bit FLOAT field, a path #150 could not reach. Only the halves a load fills are written.

Verification

python -m pytest (RTX, default)       → 1233 passed, 17 skipped (NVIDIA RTX)
ANYXBOXX_GPU=warp python -m pytest    → 1233 passed, 17 skipped
ANYXBOXX_GPU=Intel python -m pytest   → 1231 passed, 19 skipped (Intel UHD, no FP64; all 8 new GPU tests run)
python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed!

Runtime built from this branch with VS 2022 Build Tools CMake (ANYXBOXX_RUNTIME=<worktree>/runtime/build/bin).

Oracle and test data

  • tests/test_rdna2_exec_format_d16.py (54 tests, hand-computed values):
    • float32 → float16 ties, overflow (65520 → inf), subnormals (2^-25 → 0, 1.5×2^-24 → 2^-23), NaN payloads, −0.0 and float32 subnormals;
    • as an independent oracle, Python's binary16 rounding (struct format e) over 20,000 random float32 values and the subnormal range, plus every normalized field of widths 2, 8, 10, 11 and 16;
    • the three double-rounding cases, scaled (2049 → 2048, 65535 → inf), integer extension (8_SINT 0x80 → 0xFF80), 16-bit FLOAT pass-through;
    • packing of x/xy/xyz/xyzw/hi_x with the untouched halves kept, partial EXEC, DST_SEL 0/1 and out-of-range reads, MTBUF format and the INVALID V#;
    • stores: float16 copied or widened, integer saturation from 16-bit halves, z read from bits 15-0 of the second VGPR;
    • every refusal by name, and the coverage of all 32 typed-buffer names.
  • tests/test_rdna2_hlsl_format_d16.py (8 tests): interpreter vs GPU, byte for byte. Float special values and random bits through 32/32x4/16x4 FLOAT; UNORM/SNORM/SCALED/INT 8, 10 and 16-bit formats with the midpoint fields injected; DST_SEL and bounds; MTBUF with an INVALID V#; partial EXEC on loads (v8-v11 refilled before each load so kept halves are compared); stores with partial words (stride 1 and 2); wave64.
  • Failure case: on main the new interpreter tests fail (52 of 54; the module does not exist), and test_every_typed_buffer_encoding_counts_in_coverage fails because 18 names are not executable. I also mutated the HLSL: dropping ties-to-even fails 2 GPU tests, and zeroing the kept half fails 7.
  • Test data is built in the tests; no external data.
  • Removed: the three _d16 cases from #150's refusal test and its "14 executable" coverage assertion, both superseded.

Checklist

  • The PR covers a single reviewable behaviour.
  • A test fails without this change and passes with it.
  • The commands and results of each check are given above.
  • An unimplemented function returns an error (E_NOTIMPL), never a fake success.
  • README: the same change in all 9 languages (test_readme_sync.py checks it).
  • No game, console or SDK file, no key, no dump, no download link.
  • Findings drawn from a title: none.
  • Code taken from another project: none.

AI assistance: written with Claude Code (Claude Opus 5.5), reviewed and run on our hardware (RTX, WARP, Intel UHD).

🤖 Generated with Claude Code

**@⁠KainanP** on GitHub · 2026-11-25 17:37 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/223) `KainanP:feat/buffer-format-d16` → `main` · +684 −59 · 17 files · 1 commits · closed without merging ## Change The 18 d16 typed buffer encodings that main still refused now run in the reference interpreter and translate to HLSL: `buffer_load_format_d16_{x,xy,xyz,xyzw,hi_x}`, `buffer_store_format_d16_{x,xy,xyz,xyzw,hi_x}`, `tbuffer_load_format_d16_{x,xy,xyz,xyzw}`, `tbuffer_store_format_d16_{x,xy,xyz,xyzw}` (the full list of `_format_d16` names in `MNEMONICS` where `rdna2_exec.executable` was false). The PR builds on the 32-bit typed formats of #150 and reuses its format tables, DST_SEL handling, bounds and measured conversions. Interpreter coverage: **1,060 → 1,078 of 1,490 encodings** (`rdna2_exec.coverage()`). The README still said 1,046, so it now says 70% → 72% in all 9 languages, and the test count goes to 1200+ (1,250 collected). ### Rules and sources AMD, "RDNA 2" Instruction Set Architecture, November 2020 (archived copy from `docs/research/rdna2-sources.md`, SHA-256 `22aec03f…4795`, printed page numbers): | Rule | Source | | --- | --- | | RDNA 2 is packed d16: component i goes in VGPR `VDATA + i/2`, low half first (xyzw uses 2 VGPRs) | §8.1.2 p. 56 ("BUFFER_LOAD_FORMAT_D16_XYZW writes two VGPRs"), §8.1.4 p. 59 ("a pair of data are stored in each 32-bit VGPR (LSBs first, then MSBs)"); LLVM 20.1.8: `FeatureUnpackedD16VMem` is set only for GFX8 (`AMDGPU.td`), and the packed `TBUFFER_LOAD_FORMAT_D16_XY` uses `VGPR_32` (`BUFInstructions.td`) | | `_d16_x` writes bits 15-0, `_d16_hi_x` bits 31-16, and the other half is kept | §12.14 p. 215 / p. 211 pseudo-code `D0[15:0] = MEM[ADDR]` / `D0[31:16] = MEM[ADDR]`; §8.1.6 p. 64; AMD RDNA 2 XML: "store the result into the low/high 16 bits of a 32-bit vector register" | | A d16 load converts the 32-bit result of the matching non-d16 load to 16 bits | §8.1.4 p. 59: "data returned from the texture unit is converted to 16 bits … Control over int vs. float is controlled by FORMAT"; §8.1.3 p. 58: integer formats come back as integers of the storage type | | Stores: the texture unit converts each 16-bit half to the memory format | §8.1.4 p. 59; XML: "Convert 16 bits of data from the low/high 16 bits of a 32-bit vector input register" | Applied conversions: - **Loads.** UINT/SINT formats of 16 bits or less give the low 16 bits of the zero- or sign-extended field. Every other format (UNORM, SNORM, USCALED, SSCALED, FLOAT) goes float32 → float16. A 16-bit FLOAT field comes back unchanged, signalling-NaN payload included. - **Stores.** Each half is widened to the value the non-d16 store takes: UINT is zero-extended, SINT sign-extended, float16 → float32 is exact for 32-bit FLOAT formats (NaN payload shifted left by 13, as measured in #150), and a 16-bit FLOAT field is copied. The value then goes through #150's measured saturation (UINT to 2^n−1, SINT to the field's range). - **DST_SEL = 1** gives 1.0 (`0x3C00`) for float, normalized and scaled formats and the integer 1 otherwise: #150's measured value, narrowed like any other component. ### Assumptions (not measured, stated in `emu/rdna2_buffer_d16.py`) 1. **float32 → float16 narrowing** uses IEEE round to nearest even, overflow to infinity, and float16 subnormals are kept. This extends #150's measured round-to-nearest; nothing documents the narrowing. It is a double rounding, so it differs by one ulp from rounding the exact quotient once for exactly three fields: 16_UNORM 65455 (`0x3BFE` instead of `0x3BFD`), 16_UNORM 65519 (`0x3C00` instead of `0x3BFF`) and 16_SNORM 32759 (`0x3C00` instead of `0x3BFF`). I checked every quotient c / (2^n − 1) for n = 1 to 16 exhaustively: only 4 differ, and the fourth (n = 14) matches no format. These three are pinned in the tests and are the cheapest hardware measurement that would settle the order. 2. **A float32 NaN** keeps its sign and the top 10 payload bits, with no quiet bit added: the inverse of the widening measured in #150. 3. **The high half of the second VGPR of `_d16_xyz`** is kept, like the high half of `_d16_x`. The ISA only gives pseudo-code for `_x` and `_hi_x`. ### Refused by name (no fake success) - d16 **loads** of 32-bit integer formats (`32_UINT`, `32_32_SINT`, …): whether 32 → 16 bits truncates or clamps is not documented. - A float32 NaN whose top 10 payload bits are zero: under assumption 2 it would turn into infinity. - d16 **stores** to UNORM/SNORM/SCALED formats. They stay refused, as in #150. The HLSL follows the module's existing convention: what the interpreter refuses at run time (MUBUF format read from the V#, NaN) reads zeros there. ### HLSL `BufHalfFromFloat` uses integer operations only, with no `double` and no `f32tof16` (Intel UHD has no FP64, and `f32tof16` rounding is driver-defined). `BufNarrow` and `BufWiden` sit on top of #150's `BufDecode` and `BufEncode`. `BufField` now copies a 16-bit FLOAT field, a path #150 could not reach. Only the halves a load fills are written. ## Verification ``` python -m pytest (RTX, default) → 1233 passed, 17 skipped (NVIDIA RTX) ANYXBOXX_GPU=warp python -m pytest → 1233 passed, 17 skipped ANYXBOXX_GPU=Intel python -m pytest → 1231 passed, 19 skipped (Intel UHD, no FP64; all 8 new GPU tests run) python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! ``` Runtime built from this branch with VS 2022 Build Tools CMake (`ANYXBOXX_RUNTIME=<worktree>/runtime/build/bin`). ## Oracle and test data - `tests/test_rdna2_exec_format_d16.py` (54 tests, hand-computed values): - float32 → float16 ties, overflow (65520 → inf), subnormals (2^-25 → 0, 1.5×2^-24 → 2^-23), NaN payloads, −0.0 and float32 subnormals; - as an independent oracle, Python's binary16 rounding (`struct` format `e`) over 20,000 random float32 values and the subnormal range, plus every normalized field of widths 2, 8, 10, 11 and 16; - the three double-rounding cases, scaled (2049 → 2048, 65535 → inf), integer extension (8_SINT 0x80 → 0xFF80), 16-bit FLOAT pass-through; - packing of x/xy/xyz/xyzw/hi_x with the untouched halves kept, partial EXEC, DST_SEL 0/1 and out-of-range reads, MTBUF format and the INVALID V#; - stores: float16 copied or widened, integer saturation from 16-bit halves, z read from bits 15-0 of the second VGPR; - every refusal by name, and the coverage of all 32 typed-buffer names. - `tests/test_rdna2_hlsl_format_d16.py` (8 tests): interpreter vs GPU, byte for byte. Float special values and random bits through 32/32x4/16x4 FLOAT; UNORM/SNORM/SCALED/INT 8, 10 and 16-bit formats with the midpoint fields injected; DST_SEL and bounds; MTBUF with an INVALID V#; partial EXEC on loads (v8-v11 refilled before each load so kept halves are compared); stores with partial words (stride 1 and 2); wave64. - Failure case: on main the new interpreter tests fail (52 of 54; the module does not exist), and `test_every_typed_buffer_encoding_counts_in_coverage` fails because 18 names are not executable. I also mutated the HLSL: dropping ties-to-even fails 2 GPU tests, and zeroing the kept half fails 7. - Test data is built in the tests; no external data. - Removed: the three `_d16` cases from #150's refusal test and its "14 executable" coverage assertion, both superseded. ## Checklist - [x] The PR covers a single reviewable behaviour. - [x] A test fails without this change and passes with it. - [x] The commands and results of each check are given above. - [x] An unimplemented function returns an error (`E_NOTIMPL`), never a fake success. - [x] README: the same change in all 9 languages (`test_readme_sync.py` checks it). - [x] No game, console or SDK file, no key, no dump, no download link. - [x] Findings drawn from a title: none. - [x] Code taken from another project: none. AI assistance: written with Claude Code (Claude Opus 5.5), reviewed and run on our hardware (RTX, WARP, Intel UHD). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The 18 encodings buffer_load/store_format_d16_{x,xy,xyz,xyzw,hi_x} and
tbuffer_load/store_format_d16_{x,xy,xyz,xyzw} now run in the reference
interpreter and translate to HLSL, on top of the 32-bit typed formats of #150.

- Packing (RDNA 2 is packed d16): component i goes to VGPR VDATA + i/2, low
  half first; _d16_x and the third component of _d16_xyz write bits 15-0,
  _d16_hi_x bits 31-16, and the other half is kept. ISA section 8.1.2 (p. 56),
  8.1.4 (p. 59), 8.1.6 (p. 64), 12.14 (p. 211, 215: "D0[15:0] = MEM[ADDR]",
  "D0[31:16] = MEM[ADDR]"); AMD RDNA 2 XML ("low/high 16 bits of a 32-bit
  vector register"); LLVM 20.1.8 FeatureUnpackedD16VMem only on GFX8.
- Loads narrow the 32-bit result of the non-d16 load (section 8.1.4: "data
  returned from the texture unit is converted to 16 bits"): integer formats of
  16 bits or less keep the low 16 bits; float, normalized and scaled values go
  float32 -> float16, round to nearest even, overflow to infinity, subnormals
  kept (assumed). 32-bit integer formats are refused by name (truncation or
  clamping is not documented), as is a float32 NaN whose top 10 payload bits
  are zero.
- Stores widen each half to the value the non-d16 store takes (UINT
  zero-extended, SINT sign-extended, float16 -> float32 exact, 16-bit FLOAT
  copied), then reuse #150's measured saturation. UNORM/SNORM/SCALED stores
  stay refused.
- HLSL uses integer operations only for the float16 conversion (no double:
  Intel UHD has no FP64).

Interpreter coverage 1,060 -> 1,078 of 1,490 encodings (README 70% -> 72%,
it still showed 1,046); test count 1200+. New tests in
tests/test_rdna2_exec_format_d16.py (hand-computed values, Python's binary16
rounding as an independent oracle) and tests/test_rdna2_hlsl_format_d16.py
(byte-for-byte GPU parity in wave32 and wave64).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:38:26 +00:00
github-bot closed this pull request 2026-11-26 05:38:26 +00:00
Author
Owner

@⁠LooperSalty on GitHub · 2026-11-25 18:39 UTC · original

This PR now conflicts with main after the other merges (RDNA 2 interpreter/HLSL files). Please rebase on main and reopen. Thanks!

**@⁠LooperSalty** on GitHub · 2026-11-25 18:39 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/223#issuecomment-7171204650) This PR now conflicts with main after the other merges (RDNA 2 interpreter/HLSL files). Please rebase on main and reopen. Thanks!
Author
Owner

@⁠KainanP on GitHub · 2026-11-25 18:59 UTC · original

Already measured on RDNA 2 hardware, no request needed: every UNORM16/SNORM16 value on gfx1031 gives the exact quotient rounded once to f16, not the f32 result narrowed, so assumption 1 is wrong for the pinned cases (16_UNORM 0xFFAF → 0x3BFD, 0xFFEF → 0x3BFF, 16_SNORM 0x7FF7 → 0x3BFF) and there is a fourth one, 16_SNORM 0x8009 → 0xBBFF; f16 denormals kept, buffer and tbuffer alike: github.com/ubiquitarianism/leonida@9f9a7c5f30/docs/shader_validation.md (L1612-L1665)

AI use: Claude Code (Claude Opus 5.5).

**@⁠KainanP** on GitHub · 2026-11-25 18:59 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/223#issuecomment-7171680165) Already measured on RDNA 2 hardware, no request needed: every UNORM16/SNORM16 value on gfx1031 gives the exact quotient rounded once to f16, not the f32 result narrowed, so assumption 1 is wrong for the pinned cases (16_UNORM 0xFFAF → 0x3BFD, 0xFFEF → 0x3BFF, 16_SNORM 0x7FF7 → 0x3BFF) and there is a fourth one, 16_SNORM 0x8009 → 0xBBFF; f16 denormals kept, buffer and tbuffer alike: https://github.com/ubiquitarianism/leonida/blob/9f9a7c5f30f8559c52675780cf84004571b8d6e5/docs/shader_validation.md#L1612-L1665 _AI use: Claude Code (Claude Opus 5.5)._
Author
Owner

@⁠KainanP on GitHub · 2026-11-25 19:40 UTC · original

Superseded by #230: rebased onto main cf84f8d as asked, with UNORM/SNORM d16 loads now rounding the exact quotient once, as measured on gfx1031 (0xFFAF, 0xFFEF, 0x7FF7, 0x8009); full suite 1314/1314/1312 passed on RTX/WARP/Intel.

AI use: Claude Code (Claude Opus 5.5).

**@⁠KainanP** on GitHub · 2026-11-25 19:40 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/223#issuecomment-7172608602) Superseded by #230: rebased onto main cf84f8d as asked, with UNORM/SNORM d16 loads now rounding the exact quotient once, as measured on gfx1031 (0xFFAF, 0xFFEF, 0x7FF7, 0x8009); full suite 1314/1314/1312 passed on RTX/WARP/Intel. _AI use: Claude Code (Claude Opus 5.5)._

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.