GPU: a half exactly halfway between two rounds to the even one, not up #1220

Closed
BastienRodz wants to merge 1 commit from upstream-pr/1220 into main
BastienRodz commented 2026-11-24 16:30:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-24T16:30:14Z.


What's wrong

f32_to_half (crates/victory-gpu/src/primitive.rs), which packs the fourth corner of a rectangle-list draw into a half-float vertex attribute, rounds by adding 0x1000 before dropping the low 13 mantissa bits:

sign | (half + ((man + 0x1000) >> 13)) as u16

So a value exactly halfway between two halves always rounds up. For example, 1 + 2^-11 packs as 0x3c01 where round-to-nearest-even (IEEE 754, and the GPU's own conversion) gives 0x3c00. The corner computed on such a value lands one ulp off the other three.

@fuzeofficlal measured it in native draws on an RTX 5090 and on WARP: 10 of 16 reference pixels matched before, 16 of 16 after. They wrote the fix (#346 comment, their c256b97).

What the fix does

The retained bit decides halfway cases: (man + 0x0fff + retained) >> 13, where retained is bit 13 of the mantissa. A carry out of the mantissa still goes into the exponent, as before. The subnormal branch above already rounded halfway values to even and is unchanged.

Tests

primitive::tests::half_midpoints_round_to_even:

  • 1 + 2^-11 gives 0x3c00 (even);
  • 1 + 3·2^-11 gives 0x3c02 (even);
  • a value just past a midpoint still rounds up, to 0x3c01.

On main, the first assertion fails with left: 15361 (0x3c01), right: 15360 (0x3c00).

On GitHub's windows-latest with the upstream CI's commands (clippy -D warnings with and without --all-features, then cargo test --workspace --locked), everything passes; victory-gpu 51 passed, 0 failed: https://github.com/BastienRodz/VIctory/actions/runs/45437173565

This is the CPU-path half of the rounding fix carried in #346, split out so it can land on its own. #346 keeps the GPU expander's copy of the same rounding. @SkillIssueXO checked the pair on an RTX 5070 Ti there: with only the old rounding put back, the byte-for-byte CPU/GPU test fails.

No game data used. AI-assisted: fix by @fuzeofficlal, split out and verified with Claude Code.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6586549383 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1220). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-24T16:30:14Z. --- ## What's wrong `f32_to_half` (`crates/victory-gpu/src/primitive.rs`), which packs the fourth corner of a rectangle-list draw into a half-float vertex attribute, rounds by adding `0x1000` before dropping the low 13 mantissa bits: ```rust sign | (half + ((man + 0x1000) >> 13)) as u16 ``` So a value exactly halfway between two halves always rounds up. For example, `1 + 2^-11` packs as `0x3c01` where round-to-nearest-even (IEEE 754, and the GPU's own conversion) gives `0x3c00`. The corner computed on such a value lands one ulp off the other three. @fuzeofficlal measured it in native draws on an RTX 5090 and on WARP: 10 of 16 reference pixels matched before, 16 of 16 after. They wrote the fix ([#346 comment](https://github.com/Marvisak/VIctory/pull/346#issuecomment-7122043116), their `c256b97`). ## What the fix does The retained bit decides halfway cases: `(man + 0x0fff + retained) >> 13`, where `retained` is bit 13 of the mantissa. A carry out of the mantissa still goes into the exponent, as before. The subnormal branch above already rounded halfway values to even and is unchanged. ## Tests `primitive::tests::half_midpoints_round_to_even`: - `1 + 2^-11` gives `0x3c00` (even); - `1 + 3·2^-11` gives `0x3c02` (even); - a value just past a midpoint still rounds up, to `0x3c01`. On `main`, the first assertion fails with `left: 15361` (`0x3c01`), `right: 15360` (`0x3c00`). On GitHub's `windows-latest` with the upstream CI's commands (clippy `-D warnings` with and without `--all-features`, then `cargo test --workspace --locked`), everything passes; `victory-gpu` 51 passed, 0 failed: https://github.com/BastienRodz/VIctory/actions/runs/45437173565 ## Related This is the CPU-path half of the rounding fix carried in #346, split out so it can land on its own. #346 keeps the GPU expander's copy of the same rounding. @SkillIssueXO checked the pair on an RTX 5070 Ti there: with only the old rounding put back, the byte-for-byte CPU/GPU test fails. No game data used. AI-assisted: fix by @fuzeofficlal, split out and verified with Claude Code. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Aniseto commented 2026-11-24 17:04:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Aniseto on 2026-11-24T16:37:02Z.


Ran the new rounding test at 71bd075:

Environment: Windows 11 Pro build 26200 x64, Intel Core i5-12400F, 64 GB RAM, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only) driver 32.0.16.2124, Rust 1.98.1 (pinned by rust-toolchain.toml), MSVC 14.44.35207, DXC 1.9.2609.5, debug build.

cargo test -p victory-gpu half_midpoints
1 passed; 0 failed; 0 ignored      (exit 0)

cargo test -p victory-gpu primitive
5 passed; 0 failed; 0 ignored      (exit 0)

primitive::tests::half_midpoints_round_to_even passes and the rest of the module is unaffected. It is a unit-level test, so the adapter is not the variable; the datapoint is the pinned 1.98.1 toolchain. No test report on this PR yet.

Not tested: a title whose rasterizer actually hits the midpoint case (no game data here), RDNA2/RDNA4 and Intel GPUs.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash 1017 - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; no game data was used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137944708 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1220#issuecomment-7137944708). Originally posted by [Aniseto](https://github.com/Aniseto) on 2026-11-24T16:37:02Z. --- Ran the new rounding test at `71bd075`: Environment: Windows 11 Pro build 26200 x64, Intel Core i5-12400F, 64 GB RAM, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only) driver 32.0.16.2124, Rust 1.98.1 (pinned by `rust-toolchain.toml`), MSVC 14.44.35207, DXC 1.9.2609.5, debug build. ``` cargo test -p victory-gpu half_midpoints 1 passed; 0 failed; 0 ignored (exit 0) cargo test -p victory-gpu primitive 5 passed; 0 failed; 0 ignored (exit 0) ``` `primitive::tests::half_midpoints_round_to_even` passes and the rest of the module is unaffected. It is a unit-level test, so the adapter is not the variable; the datapoint is the pinned 1.98.1 toolchain. No test report on this PR yet. Not tested: a title whose rasterizer actually hits the midpoint case (no game data here), RDNA2/RDNA4 and Intel GPUs. AI-assisted (OpenCode), model: **DeepSeek V4.1 Flash 1017** - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; no game data was used.
shortselow-byte commented 2026-11-24 17:04:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by shortselow-byte on 2026-11-24T16:54:38Z.


I checked f32_to_half at head b4e4647 (same result at the earlier 71bd075) on every f32 bit pattern (2^32 values) against an independent reference (exact f64 arithmetic with round_ties_even):

  • 0 mismatches on the 4,278,190,082 non-NaN values: rounding, subnormals and overflow to infinity are all exact now.
  • The 16,777,214 NaN inputs still come out as infinity (e.g. 0x7f800001 gives 0x7c00). That is the other half of #843, out of this PR's scope; #1069 (closed) fixed both and passed the same check with 0 mismatches overall.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7138395733 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1220#issuecomment-7138395733). Originally posted by [shortselow-byte](https://github.com/shortselow-byte) on 2026-11-24T16:54:38Z. --- I checked `f32_to_half` at head `b4e4647` (same result at the earlier `71bd075`) on **every f32 bit pattern** (2^32 values) against an independent reference (exact `f64` arithmetic with `round_ties_even`): - **0 mismatches on the 4,278,190,082 non-NaN values**: rounding, subnormals and overflow to infinity are all exact now. - The 16,777,214 NaN inputs still come out as infinity (e.g. `0x7f800001` gives `0x7c00`). That is the other half of #843, out of this PR's scope; #1069 (closed) fixed both and passed the same check with 0 mismatches overall.
mercuryVM commented 2026-11-24 18:04:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T17:54:30Z.

Review: APPROVED


Tested b4e46473 on Windows 11, RTX 5070, MSVC:

  • cargo test -p victory-gpu on the head: the primitive tests pass, including the new midpoint test.
  • In one full run, frame::tests::waits_end_on_ticks_and_count_frames failed. That test is timing-based and this PR doesn't touch it, and it passed 3 out of 3 when rerun alone.
  • Merged into our fork: workspace tests, shader tests and clippy are all clean.

The rounding is right for normal halves. With r the retained bit, +0x0fff + r carries out of the low 13 bits exactly when they are above 0x1000, or equal to it with r = 1. That is round-half-to-even, and a carry into the exponent still saturates at 0x7c00 as before.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6044555627 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1220#pullrequestreview-6044555627). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T17:54:30Z. **Review:** APPROVED --- Tested b4e46473 on Windows 11, RTX 5070, MSVC: - `cargo test -p victory-gpu` on the head: the primitive tests pass, including the new midpoint test. - In one full run, `frame::tests::waits_end_on_ticks_and_count_frames` failed. That test is timing-based and this PR doesn't touch it, and it passed 3 out of 3 when rerun alone. - Merged into our fork: workspace tests, shader tests and clippy are all clean. The rounding is right for normal halves. With `r` the retained bit, `+0x0fff + r` carries out of the low 13 bits exactly when they are above `0x1000`, or equal to it with `r = 1`. That is round-half-to-even, and a carry into the exponent still saturates at `0x7c00` as before. AI-assisted (Claude Code).
Zipettte commented 2026-11-24 18:06:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T17:59:51Z.


Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at b4e4647: cargo test -p victory-gpu passes, 51 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140007125 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1220#issuecomment-7140007125). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T17:59:51Z. --- Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at b4e4647: `cargo test -p victory-gpu` passes, 51 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
upstream-sync closed this pull request 2026-11-25 01:05:57 +00:00
Marvisak commented 2026-11-25 01:08:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:35:03Z.


Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #1732. The same round-half-to-even formula as #1732, which also fixes NaN and has the fuller test (every half round-trips).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148663567 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1220#issuecomment-7148663567). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:35:03Z. --- Automated triage for the maintainers. Closing because another open pull request covers the same change. See #1732. The same round-half-to-even formula as #1732, which also fixes NaN and has the fuller test (every half round-trips). If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

Sign in to join this conversation.
No reviewers
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
Marvisak/VIctory!1220
No description provided.