Recompiler: f32 to UNORM / SNORM conversions round twice instead of once #659

Open
opened 2026-11-26 04:28:40 +00:00 by github-bot · 6 comments
Owner

@⁠acaracargo on GitHub · 2026-11-24 02:59 UTC · original

The f32-to-UNORM/SNORM conversions round the product to f32 before applying round(). When the exact product is just below or above a halfway value and the multiplication rounds it to exactly halfway, the final result can be off by one.

For example, v_cvt_pknorm_u16_f32(2.2888533e-5) returns 2 instead of 1.

A CPU search found 32,640 affected f32 inputs for UNORM16 and 16,128 for SNORM16 in [0, 1). #80 addressed the equivalent problem for f16 sources.

The same issue also occurs when packing typed buffer stores into UNORM/SNORM formats, including RGBA8.

A proposed fix uses integer arithmetic to multiply the significand by the scale and round only once, preserving the existing clamps and NaN handling.

Test results

  • NVIDIA GeForce RTX 4060, driver 610.88, wave32.
  • The additional buffer-store test fails on 54 cases without the fix: 23 UNORM16, 13 SNORM16, 8 UNORM8 and 10 SNORM8.
  • With the fix: 686 passed, 0 failed, 41 ignored.
  • Existing format-store tests still pass.

The proposed fix and regression tests are available here:
https://github.com/acaracargo/xcl/tree/fork/pknorm-f32-exact

Latest tested commit: 0bca2d7.

No retail game regression run is available on this machine, so I'm reporting this as an issue rather than opening a runtime PR. Console rounding behavior has not been independently verified.

AI use

Claude Code (Claude Opus 5.5) identified the issue, implemented the proposed fix and ran the tests. ChatGPT assisted with drafting this report.

**@⁠acaracargo** on GitHub · 2026-11-24 02:59 UTC · [original](https://github.com/xcl-dev/xcl/issues/659) The f32-to-UNORM/SNORM conversions round the product to f32 before applying `round()`. When the exact product is just below or above a halfway value and the multiplication rounds it to exactly halfway, the final result can be off by one. For example, `v_cvt_pknorm_u16_f32(2.2888533e-5)` returns 2 instead of 1. A CPU search found 32,640 affected f32 inputs for UNORM16 and 16,128 for SNORM16 in [0, 1). #80 addressed the equivalent problem for f16 sources. The same issue also occurs when packing typed buffer stores into UNORM/SNORM formats, including RGBA8. A proposed fix uses integer arithmetic to multiply the significand by the scale and round only once, preserving the existing clamps and NaN handling. **Test results** - NVIDIA GeForce RTX 4060, driver 610.88, wave32. - The additional buffer-store test fails on 54 cases without the fix: 23 UNORM16, 13 SNORM16, 8 UNORM8 and 10 SNORM8. - With the fix: 686 passed, 0 failed, 41 ignored. - Existing format-store tests still pass. The proposed fix and regression tests are available here: https://github.com/acaracargo/xcl/tree/fork/pknorm-f32-exact Latest tested commit: `0bca2d7`. No retail game regression run is available on this machine, so I'm reporting this as an issue rather than opening a runtime PR. Console rounding behavior has not been independently verified. **AI use** Claude Code (Claude Opus 5.5) identified the issue, implemented the proposed fix and ran the tests. ChatGPT assisted with drafting this report.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:28:40 +00:00
Author
Owner

@⁠requiemsnkrp on GitHub · 2026-11-24 03:05 UTC · original

@⁠acaracargo same offer as on #658: I have the retail title. If you open the PR from your branch, I'll make the two scripts/smoke.ps1 runs (your head commit and the main it is based on) and post the xcl-smoke blocks there for you to paste into Regression run. pr_check.py reads them from the description.

Written with Claude Code (Claude Opus 5.5).

**@⁠requiemsnkrp** on GitHub · 2026-11-24 03:05 UTC · [original](https://github.com/xcl-dev/xcl/issues/659#issuecomment-7120764910) @⁠acaracargo same offer as on #658: I have the retail title. If you open the PR from your branch, I'll make the two `scripts/smoke.ps1` runs (your head commit and the `main` it is based on) and post the `xcl-smoke` blocks there for you to paste into **Regression run**. `pr_check.py` reads them from the description. _Written with Claude Code (Claude Opus 5.5)._
Author
Owner

@⁠acaracargo on GitHub · 2026-11-24 03:09 UTC · original

Thanks, that would be really helpful! I'll open the PR after checking the current branch and let you know when it's ready for the smoke runs.
@⁠requiemsnkrp

**@⁠acaracargo** on GitHub · 2026-11-24 03:09 UTC · [original](https://github.com/xcl-dev/xcl/issues/659#issuecomment-7120832170) Thanks, that would be really helpful! I'll open the PR after checking the current branch and let you know when it's ready for the smoke runs. @⁠requiemsnkrp
Author
Owner

@⁠NAbobo101 on GitHub · 2026-11-24 03:21 UTC · original

Independent check of the proposed fix on NVIDIA Blackwell with DXC 1.8: acaracargo/xcl fork/pknorm-f32-exact at 0bca2d7bbb7715c3cad9d65d0301421452239930, tested as is (merges cleanly onto main dd2ae9a). For the PR's own Regression run, the blocks requiemsnkrp offered are the ones to paste; these are only data points.

Tested

  • NVIDIA GeForce RTX 5060 8 GB (Blackwell, 32-lane waves only), driver 621.14 (32.0.16.2114); AMD Ryzen 7 5700G, 32 GB; Windows 11 Pro 26300; Rust 1.100.0 x86_64-pc-windows-msvc; DXC 1.8.2502.11 (Windows SDK 10.0.26100); tests in debug, runs in release.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 0bca2d7: 686 passed, 0 failed, 41 ignored; SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • gpu_pknorm_f32_rounds_once and gpu_buffer_store_norm_formats_round_once pass. With emit.rs put back to 9ce2070 (tests kept) both fail: the buffer-store test on 54 cases (23 UNORM16, 13 SNORM16, 8 UNORM8, 10 SNORM8 – the same counts as on the RTX 4060 with DXC 1.9), the pknorm test on 15 v_cvt_pknorm_u16_f32 and 26 v_cvt_pknorm_i16_f32 results, e.g. v_cvt_pknorm_u16_f32(2.2888533e-5) = 0x00020002, want 0x00010001.
  • Retail title, scripts/smoke.ps1, 60 s, two runs each: main dd2ae9a 1489 / 1519 frames, peak commit 14432 / 14416 MB; 0bca2d7 1447 / 1467 frames (97 %), 14580 / 14575 MB; no crash and no device removal in any run.

Not tested

  • AMD GPUs and native wave64.
  • Whether the title reaches v_cvt_pknorm_*_f32 or norm-format typed buffer stores in the first 60 s: the runs show no regression, not an improvement.

AI use

  • Claude Code (Claude Opus 5.5) ran the builds, tests and smoke runs and drafted this comment; I checked the results.
**@⁠NAbobo101** on GitHub · 2026-11-24 03:21 UTC · [original](https://github.com/xcl-dev/xcl/issues/659#issuecomment-7121017310) Independent check of the proposed fix on NVIDIA Blackwell with DXC 1.8: `acaracargo/xcl` `fork/pknorm-f32-exact` at `0bca2d7bbb7715c3cad9d65d0301421452239930`, tested as is (merges cleanly onto `main` `dd2ae9a`). For the PR's own **Regression run**, the blocks requiemsnkrp offered are the ones to paste; these are only data points. **Tested** - NVIDIA GeForce RTX 5060 8 GB (Blackwell, 32-lane waves only), driver 621.14 (32.0.16.2114); AMD Ryzen 7 5700G, 32 GB; Windows 11 Pro 26300; Rust 1.100.0 `x86_64-pc-windows-msvc`; DXC 1.8.2502.11 (Windows SDK 10.0.26100); tests in debug, runs in release. - `cargo test --workspace --no-fail-fast -- --test-threads=4` on `0bca2d7`: 686 passed, 0 failed, 41 ignored; SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - `gpu_pknorm_f32_rounds_once` and `gpu_buffer_store_norm_formats_round_once` pass. With `emit.rs` put back to `9ce2070` (tests kept) both fail: the buffer-store test on 54 cases (23 UNORM16, 13 SNORM16, 8 UNORM8, 10 SNORM8 – the same counts as on the RTX 4060 with DXC 1.9), the pknorm test on 15 `v_cvt_pknorm_u16_f32` and 26 `v_cvt_pknorm_i16_f32` results, e.g. `v_cvt_pknorm_u16_f32(2.2888533e-5) = 0x00020002, want 0x00010001`. - Retail title, `scripts/smoke.ps1`, 60 s, two runs each: `main` `dd2ae9a` 1489 / 1519 frames, peak commit 14432 / 14416 MB; `0bca2d7` 1447 / 1467 frames (97 %), 14580 / 14575 MB; no crash and no device removal in any run. **Not tested** - AMD GPUs and native wave64. - Whether the title reaches `v_cvt_pknorm_*_f32` or norm-format typed buffer stores in the first 60 s: the runs show no regression, not an improvement. **AI use** - Claude Code (Claude Opus 5.5) ran the builds, tests and smoke runs and drafted this comment; I checked the results.
Author
Owner

@⁠acaracargo on GitHub · 2026-11-24 03:37 UTC · original

Thanks for the independent verification! Great to see the same 54 buffer-store failures reproduced on Blackwell with DXC 1.8, and that both regression tests pass with the fix. The retail smoke results are also very helpful.

**@⁠acaracargo** on GitHub · 2026-11-24 03:37 UTC · [original](https://github.com/xcl-dev/xcl/issues/659#issuecomment-7121277223) Thanks for the independent verification! Great to see the same 54 buffer-store failures reproduced on Blackwell with DXC 1.8, and that both regression tests pass with the fix. The retail smoke results are also very helpful.
Author
Owner

@⁠luckyteagirl on GitHub · 2026-11-24 05:59 UTC · original

Native RDNA2 packed-normal rounding check

I ran v_cvt_pknorm_u16_f32 and v_cvt_pknorm_i16_f32 directly on a Radeon RX 6800M (gfx1031), Windows 11 build 26200, display driver 30.0.13002.13003, and AMD OpenCL 3302.6 (PAL,LC). The kernel supplied the same f32 input to both source operands and checked both destination halves. Four work-items per case agreed.

I compared against CPU models that multiply the exact value of the input f32 by 65535 or 32767, then round to nearest even. The two-step model first rounds the product to f32. Hardware matched single rounding for all six distinct UNORM16 and seven distinct SNORM16 inputs where the models differed in this probe.

Conversion f32 input bits Hardware per half Single rounding Two-step rounding
UNORM16, issue example 0x37C000C0 0x0001 0x0001 0x0002
UNORM16 0x3B008081 0x0081 0x0081 0x0080
SNORM16 0x38400180 0x0001 0x0001 0x0002
SNORM16 0xBF00FE02 0xBF81 0xBF81 0xBF82

At exactly 0.5, both instructions gave 0x8000 (UNORM16) or 0x4000 (SNORM16), consistent with ties to even.

Not tested

Typed buffer stores, the proposed XCL patch, other GPUs, and console hardware.

AI use

OpenAI Codex (GPT-6 family, exact model variant unavailable) wrote and ran the probe and drafted this report.

**@⁠luckyteagirl** on GitHub · 2026-11-24 05:59 UTC · [original](https://github.com/xcl-dev/xcl/issues/659#issuecomment-7123467514) **Native RDNA2 packed-normal rounding check** I ran `v_cvt_pknorm_u16_f32` and `v_cvt_pknorm_i16_f32` directly on a Radeon RX 6800M (`gfx1031`), Windows 11 build 26200, display driver `30.0.13002.13003`, and AMD OpenCL `3302.6 (PAL,LC)`. The kernel supplied the same f32 input to both source operands and checked both destination halves. Four work-items per case agreed. I compared against CPU models that multiply the exact value of the input f32 by 65535 or 32767, then round to nearest even. The two-step model first rounds the product to f32. Hardware matched single rounding for all six distinct UNORM16 and seven distinct SNORM16 inputs where the models differed in this probe. | Conversion | f32 input bits | Hardware per half | Single rounding | Two-step rounding | | --- | --- | --- | --- | --- | | UNORM16, issue example | `0x37C000C0` | `0x0001` | `0x0001` | `0x0002` | | UNORM16 | `0x3B008081` | `0x0081` | `0x0081` | `0x0080` | | SNORM16 | `0x38400180` | `0x0001` | `0x0001` | `0x0002` | | SNORM16 | `0xBF00FE02` | `0xBF81` | `0xBF81` | `0xBF82` | At exactly `0.5`, both instructions gave `0x8000` (UNORM16) or `0x4000` (SNORM16), consistent with ties to even. **Not tested** Typed buffer stores, the proposed XCL patch, other GPUs, and console hardware. **AI use** OpenAI Codex (GPT-6 family, exact model variant unavailable) wrote and ran the probe and drafted this report.
Author
Owner

@⁠Kadthicu on GitHub · 2026-11-24 08:34 UTC · original

Could we add -f32::from_bits(0x3840_0180) to gpu_buffer_store_norm_formats_round_once? It covers a negative SNORM16 double-rounding case. The exact product rounds to -1 (0xffff), while rounding the product to f32 first gives -1.5, which rounds to -2 (0xfffe). Checked the diff and arithmetic with Codex (GPT-6). No GPU run.

**@⁠Kadthicu** on GitHub · 2026-11-24 08:34 UTC · [original](https://github.com/xcl-dev/xcl/issues/659#issuecomment-7126293322) Could we add `-f32::from_bits(0x3840_0180)` to `gpu_buffer_store_norm_formats_round_once`? It covers a negative SNORM16 double-rounding case. The exact product rounds to `-1` (`0xffff`), while rounding the product to f32 first gives `-1.5`, which rounds to `-2` (`0xfffe`). Checked the diff and arithmetic with Codex (GPT-6). No GPU run.
This discussion has been locked. Commenting is limited to contributors.
No description provided.