GPU: a half exactly halfway between two rounds to the even one, not up #1220
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1220"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 adding0x1000before dropping the low 13 mantissa bits:So a value exactly halfway between two halves always rounds up. For example,
1 + 2^-11packs as0x3c01where round-to-nearest-even (IEEE 754, and the GPU's own conversion) gives0x3c00. 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, whereretainedis 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^-11gives0x3c00(even);1 + 3·2^-11gives0x3c02(even);0x3c01.On
main, the first assertion fails withleft: 15361(0x3c01),right: 15360(0x3c00).On GitHub's
windows-latestwith the upstream CI's commands (clippy-D warningswith and without--all-features, thencargo test --workspace --locked), everything passes;victory-gpu51 passed, 0 failed: https://github.com/BastienRodz/VIctory/actions/runs/45437173565Related
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
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.primitive::tests::half_midpoints_round_to_evenpasses 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.
Imported from Marvisak/VIctory. Originally posted by shortselow-byte on 2026-11-24T16:54:38Z.
I checked
f32_to_halfat headb4e4647(same result at the earlier71bd075) on every f32 bit pattern (2^32 values) against an independent reference (exactf64arithmetic withround_ties_even):0x7f800001gives0x7c00). 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.Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T17:54:30Z.
Review: APPROVED
Tested
b4e46473on Windows 11, RTX 5070, MSVC:cargo test -p victory-gpuon the head: the primitive tests pass, including the new midpoint test.frame::tests::waits_end_on_ticks_and_count_framesfailed. That test is timing-based and this PR doesn't touch it, and it passed 3 out of 3 when rerun alone.The rounding is right for normal halves. With
rthe retained bit,+0x0fff + rcarries out of the low 13 bits exactly when they are above0x1000, or equal to it withr = 1. That is round-half-to-even, and a carry into the exponent still saturates at0x7c00as before.AI-assisted (Claude Code).
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-gpupasses, 51 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).Tested with AI assistance (Claude).
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.
Pull request closed