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

Open
opened 2026-11-24 02:59:42 +00:00 by acaracargo · 7 comments
acaracargo commented 2026-11-24 02:59:42 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by acaracargo on 2026-11-24T02:59:42Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6573618457 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/659). Originally posted by [acaracargo](https://github.com/acaracargo) on 2026-11-24T02:59:42Z. --- 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.
requiemsnkrp commented 2026-11-24 07:05:45 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by requiemsnkrp on 2026-11-24T03:05:40Z.


@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).

<!-- greyfinch-sync:ivall/xcl:comment:7120764910 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/659#issuecomment-7120764910). Originally posted by [requiemsnkrp](https://github.com/requiemsnkrp) on 2026-11-24T03:05:40Z. --- @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)._
acaracargo commented 2026-11-24 07:05:46 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by acaracargo on 2026-11-24T03:09:56Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7120832170 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/659#issuecomment-7120832170). Originally posted by [acaracargo](https://github.com/acaracargo) on 2026-11-24T03:09:56Z. --- 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
NAbobo101 commented 2026-11-24 07:05:50 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by NAbobo101 on 2026-11-24T03:21:48Z.


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.
<!-- greyfinch-sync:ivall/xcl:comment:7121017310 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/659#issuecomment-7121017310). Originally posted by [NAbobo101](https://github.com/NAbobo101) on 2026-11-24T03:21:48Z. --- 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.
acaracargo commented 2026-11-24 07:05:56 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by acaracargo on 2026-11-24T03:37:57Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7121277223 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/659#issuecomment-7121277223). Originally posted by [acaracargo](https://github.com/acaracargo) on 2026-11-24T03:37:57Z. --- 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.
luckyteagirl commented 2026-11-24 07:06:42 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-24T05:59:34Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7123467514 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/659#issuecomment-7123467514). Originally posted by [luckyteagirl](https://github.com/luckyteagirl) on 2026-11-24T05:59:34Z. --- **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.
Kadthicu commented 2026-11-24 09:07:07 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by Kadthicu on 2026-11-24T08:34:09Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7126293322 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/659#issuecomment-7126293322). Originally posted by [Kadthicu](https://github.com/Kadthicu) on 2026-11-24T08:34:09Z. --- 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.
andrewDoing commented 2026-11-26 20:47:54 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by andrewDoing on 2026-11-26T20:47:54Z.


Tested the requested negative halfway-adjacent case on RTX 5070 / driver 616.92, Ryzen 7 5700X3D, Windows build 26300.10075, Rust 1.100.0 MSVC debug, SDK DXC/DXIL 1.8.2502.11.

Used the proposed branch at 0bca2d7bbb7715c3cad9d65d0301421452239930, adding only xs.push(-f32::from_bits(0x3840_0180)); to gpu_buffer_store_norm_formats_round_once. With default translation and separate writable caches, ran cargo test --locked -p xcl --lib gpu_buffer_store_norm_formats_round_once -- --test-threads=1 --nocapture.

  • Candidate: 1 passed, 0 failed, 0 ignored, no reported skips.
  • Restoring only emit.rs from parent f4c03b5d666d6ad78f755dbba20762d8cb14c024 with the identical extended fixture: test failed with 55 mismatches. The added input (0xb8400180) returned 0xfffe instead of 0xffff; the other 54 mismatches were previously reported.

The exact product is approximately -1.4999999986030161, so rounding to nearest gives -1. Not tested: the full suite, other GPUs, or a retail title.

AI use: OpenAI Codex with GPT-6.2 Astra ran the tests and reported results; Codex coordinated and prepared this summary.

<!-- greyfinch-sync:ivall/xcl:comment:7203083843 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/659#issuecomment-7203083843). Originally posted by [andrewDoing](https://github.com/andrewDoing) on 2026-11-26T20:47:54Z. --- Tested the requested negative halfway-adjacent case on RTX 5070 / driver 616.92, Ryzen 7 5700X3D, Windows build 26300.10075, Rust 1.100.0 MSVC debug, SDK DXC/DXIL 1.8.2502.11. Used [the proposed branch](https://github.com/acaracargo/xcl/tree/fork/pknorm-f32-exact) at `0bca2d7bbb7715c3cad9d65d0301421452239930`, adding only `xs.push(-f32::from_bits(0x3840_0180));` to `gpu_buffer_store_norm_formats_round_once`. With default translation and separate writable caches, ran `cargo test --locked -p xcl --lib gpu_buffer_store_norm_formats_round_once -- --test-threads=1 --nocapture`. - Candidate: 1 passed, 0 failed, 0 ignored, no reported skips. - Restoring only `emit.rs` from parent `f4c03b5d666d6ad78f755dbba20762d8cb14c024` with the identical extended fixture: test failed with 55 mismatches. The added input (`0xb8400180`) returned `0xfffe` instead of `0xffff`; the other 54 mismatches were previously reported. The exact product is approximately -1.4999999986030161, so rounding to nearest gives -1. Not tested: the full suite, other GPUs, or a retail title. AI use: OpenAI Codex with GPT-6.2 Astra ran the tests and reported results; Codex coordinated and prepared this summary.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl#654
No description provided.