GPU: a half-float rounding carry raises the exponent instead of halving the value #260

Merged
Dufftwt merged 1 commit from upstream-pr/260 into main 2026-11-24 07:59:04 +00:00
Dufftwt commented 2026-11-24 01:53:19 +00:00 (Migrated from github.com)

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 as v0 + v2 − v1. For half-float elements (R16_FLOAT, R16G16_FLOAT, R16G16B16A16_FLOAT), the result goes back through f32_to_half, which rounds to nearest by adding half an ulp to the mantissa:

sign | (exp as u16) << 10 | ((man + 0x1000) >> 13) as u16

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:

value half rounded to nearest f32_to_half today
2 − 2⁻¹² (1.99976) 0x4000 = 2.0 0x3C00 = 1.0
0.5 − 2⁻¹³ (0.49988) 0x3800 = 0.5 0x3400 = 0.25

This 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:

let half = (exp as u32) << 10;
sign | (half + ((man + 0x1000) >> 13)) as u16

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_two checks:

  • 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;
  • through expand_rects, an R16_FLOAT element at 1.0, 2⁻¹², 1.0 gets 2.0 (0x4000) at the fourth corner.

I ran cargo test --workspace on GitHub's windows-latest with Rust 1.98.1 from rust-toolchain.toml:

  • This PR: everything passes. victory-gpu has 44 passed (1 ignored), the other crates have 0 failed.
  • The new test alone on main: it fails at the first check: left: 15360 (0x3C00), right: 16384 (0x4000).
  • I ran with --test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (STATUS_ACCESS_VIOLATION), on unmodified main as 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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572806560 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/260). Originally posted by [Dufftwt](https://github.com/Dufftwt) on 2026-11-24T01:53:19Z. --- ## What's wrong Rectangle lists (`primitive.rs`) compute each rectangle's fourth corner on the CPU as `v0 + v2 − v1`. For half-float elements (`R16_FLOAT`, `R16G16_FLOAT`, `R16G16B16A16_FLOAT`), the result goes back through `f32_to_half`, which rounds to nearest by adding half an ulp to the mantissa: ```rust sign | (exp as u16) << 10 | ((man + 0x1000) >> 13) as u16 ``` 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: | value | half rounded to nearest | `f32_to_half` today | |---|---|---| | `2 − 2⁻¹²` (1.99976) | `0x4000` = 2.0 | `0x3C00` = **1.0** | | `0.5 − 2⁻¹³` (0.49988) | `0x3800` = 0.5 | `0x3400` = **0.25** | This 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: ```rust let half = (exp as u32) << 10; sign | (half + ((man + 0x1000) >> 13)) as u16 ``` 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_two` checks: - `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; - through `expand_rects`, an `R16_FLOAT` element at 1.0, 2⁻¹², 1.0 gets 2.0 (`0x4000`) at the fourth corner. I ran `cargo test --workspace` on GitHub's `windows-latest` with Rust 1.98.1 from `rust-toolchain.toml`: - **This PR:** everything passes. `victory-gpu` has 44 passed (1 ignored), the other crates have 0 failed. - **The new test alone on `main`:** it fails at the first check: `left: 15360` (`0x3C00`), `right: 16384` (`0x4000`). - I ran with `--test-threads=1`. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (`STATUS_ACCESS_VIOLATION`), on unmodified `main` as 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 commented 2026-11-24 06:56:12 +00:00 (Migrated from github.com)

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 new halves_round_up_into_the_next_power_of_two test 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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123467418 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/260#issuecomment-7123467418). Originally posted by [jeremygold02](https://github.com/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 new `halves_round_up_into_the_next_power_of_two` test 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](https://github.com/Marvisak/VIctory/blob/63df36315956f1fef1dd7068a336ce5177b0baa3/crates/victory-gpu/src/primitive.rs#L171-L187)). The checks use synthetic float and vertex buffers; they do not run a title or validate rendered output.
mercuryVM commented 2026-11-24 07:52:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T07:52:33Z.

Review: APPROVED


Tested 63df363 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (265 passed, 0 failed).

  • The bug is real. (exp << 10) | ((man + 0x1000) >> 13) loses the carry when it lands on an already-set exponent bit 0, so 2 − 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 exactly 0x7c00 (infinity), which is the right overflow, and the test covers it with 65520.
  • No conflict with #418, which changes only the exp <= 0 subnormal branch. Both together merge cleanly.

No issues found.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6035511406 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/260#pullrequestreview-6035511406). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T07:52:33Z. **Review:** APPROVED --- Tested 63df363 on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (265 passed, 0 failed). - The bug is real. `(exp << 10) | ((man + 0x1000) >> 13)` loses the carry when it lands on an already-set exponent bit 0, so `2 − 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 exactly `0x7c00` (infinity), which is the right overflow, and the test covers it with 65520. - No conflict with #418, which changes only the `exp <= 0` subnormal branch. Both together merge cleanly. No issues found. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-24 08:02:00 +00:00
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!260
No description provided.