GPU: a half-float rounding carry raises the exponent instead of halving the value #260
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/260"
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 Dufftwt on 2026-11-24T01:53:19Z.
What'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.Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T05:59:33Z.
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.
Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T07:52:33Z.
Review: APPROVED
Tested
63df363on Windows 11, RTX 5070, MSVC:cargo test --workspace --no-fail-fastpasses (265 passed, 0 failed).(exp << 10) | ((man + 0x1000) >> 13)loses the carry when it lands on an already-set exponent bit 0, so2 − 2⁻¹²became 1.0 instead of 2.0. Adding instead of OR-ing propagates the carry. The largest carry, at exponent 30 with an all-ones mantissa, reaches exactly0x7c00(infinity), which is the right overflow, and the test covers it with 65520.exp <= 0subnormal branch. Both together merge cleanly.No issues found.
AI-assisted (Claude Code).