Recompiler: D16 reads of UNORM/SNORM data are one ULP low for about half the codes (f32 then truncating f32tof16) #2026

Open
opened 2026-11-26 05:15:30 +00:00 by github-bot · 0 comments
Owner

@⁠upipibi on GitHub · 2026-11-25 01:10 UTC · original

What happens

A D16 read of a UNORM or SNORM channel (image_load/image_sample with D16=1, buffer_load_format_d16_*) packs the f32 value with f32tof16. On D3D12 that rounds toward zero, and the value was already rounded once to f32. RDNA2 instead rounds the exact code / (2^w − 1) once, to nearest even (AnyPS5#22750 on gfx1036 for 8- and 16-bit codes; XCL-BT-020 in #1488 on gfx1031). So about half of all codes come back one unit in the last place low.

Measured on an RTX 3070 (driver 616.56) on main de36462, identical with XCL_DIRECT_DXIL=0 and 1:

  • image_load D16 of an 8_8_8_8_UNORM texture, all 256 codes: 123 differ. For example, code 9 gives 0x2884 where RDNA2 gives 0x2885.
  • buffer_load_format_d16_x of 16_UNORM and 16_SNORM, 128 sampled codes: 57 differ. For example, 16_SNORM 0x5b5b gives 0x39b5, want 0x39b6.

Counting exhaustively in exact arithmetic, today's path is off for 123/256 codes at 8-bit UNORM, 510/1024 at 10-bit, 15358/32768 at 16-bit SNORM and 31740/65536 at 16-bit UNORM. A round-to-nearest of the f32 isn't enough either: the f32 is already rounded, so 0xFFAF (16_UNORM) would give 0x3bfe instead of 0x3bfd.

Plan

code / (2^w − 1) has a repeating binary expansion: the w-bit code, over and over. So the correctly rounded f16 needs only 32-bit integer operations. Fill a word with the repeated code, find the leading one, take 11 bits, and round on the next bit; a tie can't happen because the tail never ends. I checked this against exact rounding for every code at w = 7, 8, 10, 11, 15 and 16, with no mismatches. SNORM uses |v| / (2^(w−1) − 1) and adds the sign.

Two PRs, each covering both paths, with the GPU tests above:

  1. buffer format loads (dxil/lower_mem.rs, recomp/emit.rs FormatLoad): XCL-BT-020's third item;
  2. image reads (dxil/lower_image.rs, the MIMG part of recomp/emit.rs), for UNORM/SNORM texels.

Float formats stay as they are: there, D3D's truncation already matches RDNA2, which rounds 32-bit float texels toward zero (AnyPS5#22750). Integer saturation is #1977.

AI-assisted (Claude Code). Synthetic tests only.

**@⁠upipibi** on GitHub · 2026-11-25 01:10 UTC · [original](https://github.com/xcl-dev/xcl/issues/2026) ### What happens A D16 read of a UNORM or SNORM channel (`image_load`/`image_sample` with `D16=1`, `buffer_load_format_d16_*`) packs the f32 value with `f32tof16`. On D3D12 that rounds toward zero, and the value was already rounded once to f32. RDNA2 instead rounds the exact `code / (2^w − 1)` once, to nearest even (AnyPS5#22750 on gfx1036 for 8- and 16-bit codes; XCL-BT-020 in #1488 on gfx1031). So about half of all codes come back one unit in the last place low. Measured on an RTX 3070 (driver 616.56) on `main` `de36462`, identical with `XCL_DIRECT_DXIL=0` and `1`: - `image_load` D16 of an 8_8_8_8_UNORM texture, all 256 codes: 123 differ. For example, code 9 gives `0x2884` where RDNA2 gives `0x2885`. - `buffer_load_format_d16_x` of 16_UNORM and 16_SNORM, 128 sampled codes: 57 differ. For example, 16_SNORM `0x5b5b` gives `0x39b5`, want `0x39b6`. Counting exhaustively in exact arithmetic, today's path is off for 123/256 codes at 8-bit UNORM, 510/1024 at 10-bit, 15358/32768 at 16-bit SNORM and 31740/65536 at 16-bit UNORM. A round-to-nearest of the f32 isn't enough either: the f32 is already rounded, so `0xFFAF` (16_UNORM) would give `0x3bfe` instead of `0x3bfd`. ### Plan `code / (2^w − 1)` has a repeating binary expansion: the `w`-bit code, over and over. So the correctly rounded f16 needs only 32-bit integer operations. Fill a word with the repeated code, find the leading one, take 11 bits, and round on the next bit; a tie can't happen because the tail never ends. I checked this against exact rounding for every code at `w` = 7, 8, 10, 11, 15 and 16, with no mismatches. SNORM uses `|v| / (2^(w−1) − 1)` and adds the sign. Two PRs, each covering both paths, with the GPU tests above: 1. buffer format loads (`dxil/lower_mem.rs`, `recomp/emit.rs` FormatLoad): XCL-BT-020's third item; 2. image reads (`dxil/lower_image.rs`, the MIMG part of `recomp/emit.rs`), for UNORM/SNORM texels. Float formats stay as they are: there, D3D's truncation already matches RDNA2, which rounds 32-bit float texels toward zero (AnyPS5#22750). Integer saturation is #1977. AI-assisted (Claude Code). Synthetic tests only.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:15:30 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.