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

Closed
github-bot wants to merge 1 commit from gh-pr/1220/head into gh-pr/1220/base
Owner

@⁠BastienRodz on GitHub · 2026-11-24 16:30 UTC · original
BastienRodz:split/346-half-ties-even → main · +15 −3 · 1 files · 1 commits · closed without merging

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

**@⁠BastienRodz** on GitHub · 2026-11-24 16:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1220) `BastienRodz:split/346-half-ties-even` → `main` · +15 −3 · 1 files · 1 commits · closed without merging ## 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://git.gtahub.st/github/Marvisak--VIctory/pulls/346), 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)
f32_to_half adds 0x1000 before dropping the low 13 mantissa bits, so a
value exactly halfway between two halves always rounds up: 1 + 2^-11
packs as 0x3c01 instead of 0x3c00. A rectangle's fourth corner computed
on such a value gets a half one ulp off. fuzeofficlal measured it in
native draws of the direct path on RTX 5090 and WARP (10 of 16 reference
pixels before, 16 of 16 after) and wrote the fix: the retained bit
decides halfway cases.

Co-Authored-By: fuzeofficlal <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:24:07 +00:00
github-bot closed this pull request 2026-11-26 04:24:07 +00:00
Author
Owner

@⁠Aniseto on GitHub · 2026-11-24 16:37 UTC · original

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.

**@⁠Aniseto** on GitHub · 2026-11-24 16:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/1220#issuecomment-7137944708) 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.
Author
Owner

@⁠shortselow-byte on GitHub · 2026-11-24 16:54 UTC · original

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.
**@⁠shortselow-byte** on GitHub · 2026-11-24 16:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/1220#issuecomment-7138395733) 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.
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 17:59 UTC · original

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

**@⁠Zipettte** on GitHub · 2026-11-24 17:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/1220#issuecomment-7140007125) 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)._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:35 UTC · original

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.

**@⁠Marvisak** on GitHub · 2026-11-25 00:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1220#issuecomment-7148663567) 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

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/Marvisak--VIctory!1220
No description provided.