Avoid double rounding in shader float-to-integer conversion #2678

Manually merged
github merged 1 commit from github-bot/gh-2678 into main 2026-11-30 14:15:41 +00:00 AGit
Owner

@⁠masteraapjes on GitHub · 2026-11-29 15:55 UTC · original
masteraapjes:codex/shader-rpi-rounding → main · +57 −5 · 2 files · 1 commits · merged 2026-11-30 14:12 UTC

What it changes

v_cvt_rpi_i32_f32 adds 0.5 in f32 and then floors the result. The first
operation rounds before the conversion: 0x3effffff (just below 0.5) becomes
1 instead of 0, and 8388609 becomes 8388610 despite already being integral.

Floor the input first, then increment only if its fractional part is at least
0.5. This gives nearest-integer rounding with ties towards positive infinity,
without an intermediate f32 rounding of x+0.5. The existing conversion and
NaN handling remain in place; the other conversion opcodes are unchanged.

This adapts the rounding-only correction from upipibi's upstream PR
#605
, supported by
Lyrnc's native RDNA 2 measurements.
The separate NaN-sign saturation change discussed there is outside this patch.

How it was tested

  • Windows, Rust 1.98.1, RTX 3090 and D3D12 WARP; synthetic wave32 compute
    shaders, with no game files or vendor SDKs.
  • Before: the new GPU regression fails on hardware for 0x3effffff,
    returning 1 instead of 0.
  • After: cargo test --locked -p victory-shader --lib v_cvt_rpi_i32_f32_rounds_without_an_intermediate_f32_sum -- --nocapture
    passes. Both backends exercise 27 bit-exact cases: half-value neighbours,
    positive/negative ties, large odd integers, signed zero, saturation bounds,
    finite overflows and infinities.
  • An independent CPU model agreed with exact-sum rounding over 500,015
    float bit patterns, including the existing conversion's special cases.
  • Full shader library suite: cargo test --locked -p victory-shader --lib:
    463 passed, 0 failed, 9 ignored.
  • Staged asset policy: both changed files passed. git diff --check passed.
  • Strict Clippy has existing unused-import, immutable-condition and
    too-many-arguments failures elsewhere in this crate. With just those
    existing lint categories allowed on the command line, all-targets,
    all-features Clippy passed. No source or CI lint settings were changed.

In-game rendering has not been measured.

Checked for overlap

  • Searched open PRs for the instruction and changed paths. Related:
    #2018 corrects the opposite uint-to-float conversion, not this opcode.
**@⁠masteraapjes** on GitHub · 2026-11-29 15:55 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2678) `masteraapjes:codex/shader-rpi-rounding` → `main` · +57 −5 · 2 files · 1 commits · merged 2026-11-30 14:12 UTC ## What it changes `v_cvt_rpi_i32_f32` adds 0.5 in f32 and then floors the result. The first operation rounds before the conversion: 0x3effffff (just below 0.5) becomes 1 instead of 0, and 8388609 becomes 8388610 despite already being integral. Floor the input first, then increment only if its fractional part is at least 0.5. This gives nearest-integer rounding with ties towards positive infinity, without an intermediate f32 rounding of x+0.5. The existing conversion and NaN handling remain in place; the other conversion opcodes are unchanged. This adapts the rounding-only correction from [upipibi's upstream PR #605](https://github.com/Marvisak/VIctory/pull/605), supported by [Lyrnc's native RDNA 2 measurements](https://github.com/Marvisak/VIctory/pull/605#issuecomment-7123302580). The separate NaN-sign saturation change discussed there is outside this patch. ## How it was tested - Windows, Rust 1.98.1, RTX 3090 and D3D12 WARP; synthetic wave32 compute shaders, with no game files or vendor SDKs. - Before: the new GPU regression fails on hardware for 0x3effffff, returning 1 instead of 0. - After: `cargo test --locked -p victory-shader --lib v_cvt_rpi_i32_f32_rounds_without_an_intermediate_f32_sum -- --nocapture` passes. Both backends exercise 27 bit-exact cases: half-value neighbours, positive/negative ties, large odd integers, signed zero, saturation bounds, finite overflows and infinities. - An independent CPU model agreed with exact-sum rounding over 500,015 float bit patterns, including the existing conversion's special cases. - Full shader library suite: `cargo test --locked -p victory-shader --lib`: 463 passed, 0 failed, 9 ignored. - Staged asset policy: both changed files passed. `git diff --check` passed. - Strict Clippy has existing unused-import, immutable-condition and too-many-arguments failures elsewhere in this crate. With just those existing lint categories allowed on the command line, all-targets, all-features Clippy passed. No source or CI lint settings were changed. In-game rendering has not been measured. ## Checked for overlap - [x] Searched open PRs for the instruction and changed paths. Related: #2018 corrects the opposite uint-to-float conversion, not this opcode.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 15:58:31 +00:00
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-30 06:11 UTC · original

Checked this against a Radeon gfx1036 (RDNA 2) running the same kernels natively, and ran the translator's output for main and for this PR on an RTX 5080 and on that Radeon through D3D12.

Setup

  • main at 41adb551 with this PR's head (4355f38) merged locally, no conflict. The victory-shader test binary was built from that merged tree (own tree and target directory).
  • Kernel: hand-written GFX10 code, nothing from a title. Wave32, a 64-thread group (two waves); each lane converts its own input from a per-lane table; outputs v10.. stored per lane.
  • Hardware rows: the same machine code dispatched natively on the gfx1036, MODE IEEE on with f32 denormals kept (and once more with IEEE off and denormals flushed, for the lanes that depend on it).
  • Host runs: super::emit (32-lane host), dxil.dll validation, dispatched on the 5080 and on the Radeon (VICTORY_ADAPTER=radeon).

Case cvt_rpi_flr: 64 inputs (ties at ±0.5, ±1.5 .. ±100.5, the neighbours of 0.5, odd integers from 2^23 to 2^24 and their negatives, 2^31 and its neighbours, ±inf, ±qNaN, sNaN, ±denormal, ±max). Outputs: v10 v_cvt_rpi_i32_f32 x, v11 v_cvt_flr_i32_f32 x, v12 v_cvt_i32_f32 x, v13 v_cvt_u32_f32 x, v14 v_cvt_rpi_i32_f32 -x (VOP3 neg), v15 v_cvt_flr_i32_f32 |x| (VOP3 abs).

Results (values that differ from the gfx1036, out of 64 lanes x 6 outputs)

output main, 5080 main, Radeon this PR, 5080 this PR, Radeon
v10 rpi x 11 (lanes 16, 20-23, 26, 27, 55; NaN lanes 40-42) 11 (same) 3 (NaN lanes 40-42) 3 (same)
v11 flr x 4 (NaN lanes 40-42; lane 44) 4 4 (unchanged) 4
v12 i32 x, v13 u32 x exact exact exact exact
v14 rpi -x 11 (lanes 17, 20-23, 26, 27, 55; NaN lanes 40-42) 11 3 (NaN lanes 40-42) 3
v15 flr abs(x) 3 (NaN lanes 40-42) 3 3 (unchanged) 3

The 8 rounding lanes main gets wrong, gfx1036 / main (v10):

lane x gfx1036 main
16 0x3EFFFFFF (0.49999997) 0 1
20 0x4B000001 (8388609) 8388609 8388610
21 0x4B000003 (8388611) 8388611 8388612
22 0xCB000001 (-8388609) -8388609 -8388608
23 0xCB000003 (-8388611) -8388611 -8388610
26 0x4B7FFFFF (16777215) 16777215 16777216
27 0xCB7FFFFF (-16777215) -16777215 -16777214
55 0x4B3FFFFF (12582911) 12582911 12582912

With this PR all eight are exact, and so are the ties (±0.5 .. ±100.5 round up: -0.5 gives 0, -2.5 gives -2), the values next to ±0.5, ±0, the 2^31 boundary (0x4F000000 saturates to 0x7FFFFFFF, 0xCF000000 gives 0x80000000) and ±inf. Both cards give the same values.

Verdict: correct for what it changes. Every rounding lane is exact on both cards in both the plain and the neg form, and nothing else moved (flr, i32, u32 give the same values as main).

What still differs, both cards, main and this PR alike (outside this patch, as you say):

  • NaN: the gfx1036 saturates by sign in rpi and flr, it does not give 0. +qNaN 0x7FC00000 and sNaN 0x7F800001 give 0x7FFFFFFF, -qNaN 0xFFC00000 gives 0x80000000 (lanes 40, 42 / 41; rpi -x the other way round; flr abs(x) 0x7FFFFFFF for all three). v_cvt_i32_f32 and v_cvt_u32_f32 do give 0 for NaN on the hardware, so only the rpi / flr pair differs. The same values with IEEE mode off.
  • Lane 44, flr of -denormal 0x80000001: -1 on the gfx1036 with denormals kept, 0 with them flushed. The translation gives 0 (the host flushes), so this one depends on the console's MODE.

AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.

**@⁠Lyrnc** on GitHub · 2026-11-30 06:11 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2678#issuecomment-7270395701) Checked this against a Radeon gfx1036 (RDNA 2) running the same kernels natively, and ran the translator's output for main and for this PR on an RTX 5080 and on that Radeon through D3D12. **Setup** - `main` at 41adb551 with this PR's head (4355f38) merged locally, no conflict. The victory-shader test binary was built from that merged tree (own tree and target directory). - Kernel: hand-written GFX10 code, nothing from a title. Wave32, a 64-thread group (two waves); each lane converts its own input from a per-lane table; outputs v10.. stored per lane. - Hardware rows: the same machine code dispatched natively on the gfx1036, MODE IEEE on with f32 denormals kept (and once more with IEEE off and denormals flushed, for the lanes that depend on it). - Host runs: `super::emit` (32-lane host), `dxil.dll` validation, dispatched on the 5080 and on the Radeon (`VICTORY_ADAPTER=radeon`). **Case** `cvt_rpi_flr`: 64 inputs (ties at ±0.5, ±1.5 .. ±100.5, the neighbours of 0.5, odd integers from 2^23 to 2^24 and their negatives, 2^31 and its neighbours, ±inf, ±qNaN, sNaN, ±denormal, ±max). Outputs: v10 `v_cvt_rpi_i32_f32 x`, v11 `v_cvt_flr_i32_f32 x`, v12 `v_cvt_i32_f32 x`, v13 `v_cvt_u32_f32 x`, v14 `v_cvt_rpi_i32_f32 -x` (VOP3 neg), v15 `v_cvt_flr_i32_f32 |x|` (VOP3 abs). **Results** (values that differ from the gfx1036, out of 64 lanes x 6 outputs) | output | main, 5080 | main, Radeon | this PR, 5080 | this PR, Radeon | |---|---|---|---|---| | v10 `rpi x` | 11 (lanes 16, 20-23, 26, 27, 55; NaN lanes 40-42) | 11 (same) | 3 (NaN lanes 40-42) | 3 (same) | | v11 `flr x` | 4 (NaN lanes 40-42; lane 44) | 4 | 4 (unchanged) | 4 | | v12 `i32 x`, v13 `u32 x` | exact | exact | exact | exact | | v14 `rpi -x` | 11 (lanes 17, 20-23, 26, 27, 55; NaN lanes 40-42) | 11 | 3 (NaN lanes 40-42) | 3 | | v15 `flr abs(x)` | 3 (NaN lanes 40-42) | 3 | 3 (unchanged) | 3 | The 8 rounding lanes main gets wrong, gfx1036 / main (v10): | lane | x | gfx1036 | main | |---|---|---|---| | 16 | 0x3EFFFFFF (0.49999997) | 0 | 1 | | 20 | 0x4B000001 (8388609) | 8388609 | 8388610 | | 21 | 0x4B000003 (8388611) | 8388611 | 8388612 | | 22 | 0xCB000001 (-8388609) | -8388609 | -8388608 | | 23 | 0xCB000003 (-8388611) | -8388611 | -8388610 | | 26 | 0x4B7FFFFF (16777215) | 16777215 | 16777216 | | 27 | 0xCB7FFFFF (-16777215) | -16777215 | -16777214 | | 55 | 0x4B3FFFFF (12582911) | 12582911 | 12582912 | With this PR all eight are exact, and so are the ties (±0.5 .. ±100.5 round up: -0.5 gives 0, -2.5 gives -2), the values next to ±0.5, ±0, the 2^31 boundary (0x4F000000 saturates to 0x7FFFFFFF, 0xCF000000 gives 0x80000000) and ±inf. Both cards give the same values. **Verdict: correct for what it changes.** Every rounding lane is exact on both cards in both the plain and the `neg` form, and nothing else moved (`flr`, `i32`, `u32` give the same values as main). What still differs, both cards, main and this PR alike (outside this patch, as you say): - NaN: the gfx1036 saturates by sign in `rpi` and `flr`, it does not give 0. +qNaN 0x7FC00000 and sNaN 0x7F800001 give 0x7FFFFFFF, -qNaN 0xFFC00000 gives 0x80000000 (lanes 40, 42 / 41; `rpi -x` the other way round; `flr abs(x)` 0x7FFFFFFF for all three). `v_cvt_i32_f32` and `v_cvt_u32_f32` do give 0 for NaN on the hardware, so only the `rpi` / `flr` pair differs. The same values with IEEE mode off. - Lane 44, `flr` of -denormal 0x80000001: -1 on the gfx1036 with denormals kept, 0 with them flushed. The translation gives 0 (the host flushes), so this one depends on the console's MODE. AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.
github manually merged commit c3a744d833 into main 2026-11-30 14:15:41 +00:00
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!2678
No description provided.