GPU: a half-float rounding carry raises the exponent instead of halving the value #260
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!260
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/260/head"
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?
@Dufftwt on GitHub · 2026-11-24 01:53 UTC · original
Dufftwt:fix/f32-to-half-rounding-carry→main· +22 −1 · 1 files · 1 commits · merged 2026-11-24 07:59 UTCWhat's wrong
Rectangle lists (
primitive.rs) compute each rectangle's fourth corner on the CPU asv0 + v2 − v1. For half-float elements (R16_FLOAT,R16G16_FLOAT,R16G16B16A16_FLOAT), the result goes back throughf32_to_half, which rounds to nearest by adding half an ulp to the mantissa:When that rounding carries out of the 10-bit mantissa, the carry lands on bit 10, the exponent's lowest bit, and gets or-ed into the exponent. If the exponent is odd, that bit is already set and the carry is lost. The value then drops to the power of two below instead of rounding up to the one above:
f32_to_halftoday2 − 2⁻¹²(1.99976)0x4000= 2.00x3C00= 1.00.5 − 2⁻¹³(0.49988)0x3800= 0.50x3400= 0.25This happens in every range with an odd exponent ([0.25, 0.5), [1, 2), [4, 8), …), for values within half an ulp of the next power of two. In a rectangle, that corner's attribute (a texture coordinate, a color) comes out half or double its value. For example, corners at 1.0, 2⁻¹², 1.0 give a fourth corner of 1.0 instead of 2.0.
What the fix does
The carry is added into the exponent instead of or-ed:
The rounding stays round-to-nearest, as the code intended. The largest result is still
0x7C00: a value that rounds past the largest half becomes infinity, as before. Nothing else in the conversion changes.How it behaves after the fix
Half-float attributes of a rectangle's fourth corner round to the nearest half, including just below a power of two. I compared the old and new functions over the whole half range in steps of 256 f32 ulps. Only the values where the carry was lost change (240 of the sampled values), and the new function is monotonic everywhere.
Tests
The new test
halves_round_up_into_the_next_power_of_twochecks:f32_to_half(2 − 2⁻¹²) == 0x4000;f32_to_half(0.5 − 2⁻¹³) == 0x3800;f32_to_half(65520.0) == 0x7C00, which still becomes infinity past the largest half;expand_rects, anR16_FLOATelement at 1.0, 2⁻¹², 1.0 gets 2.0 (0x4000) at the fourth corner.I ran
cargo test --workspaceon GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml:victory-gpuhas 44 passed (1 ignored), the other crates have 0 failed.main: it fails at the first check:left: 15360(0x3C00),right: 16384(0x4000).--test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (STATUS_ACCESS_VIOLATION), on unmodifiedmainas well.Not tested: a title's rectangle list with half-float attributes.
Related: #196 changes the same file's RGBA8 fast path. It doesn't touch
f32_to_half, and the two merge cleanly.@jeremygold02 on GitHub · 2026-11-24 05:59 UTC · original
Tested current PR head
63df36315956f1fef1dd7068a336ce5177b0baa3.cargo test --locked -p victory-gpu --lib primitive -- --test-threads=1: 3 passed, 42 filtered out. The newhalves_round_up_into_the_next_power_of_twotest checks mantissa carry at values just below 2.0 and 0.5, overflow at 65520 to half infinity, and an R16_FLOAT rectangle whose computed fourth corner rounds to 2.0. The same target passed the existing rectangle expansion and stride-overrun preservation cases (rounding regression).The checks use synthetic float and vertex buffers; they do not run a title or validate rendered output.