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

Manually merged
github merged 1 commit from gh-pr/260/head into gh-pr/260/base 2026-11-26 03:57:02 +00:00
Owner

@⁠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 UTC

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.

**@⁠Dufftwt** on GitHub · 2026-11-24 01:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/260) `Dufftwt:fix/f32-to-half-rounding-carry` → `main` · +22 −1 · 1 files · 1 commits · merged 2026-11-24 07:59 UTC ## 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.
f32_to_half rounds to nearest by adding half an ulp to the mantissa,
then or-ed the shifted mantissa into the exponent field. When the
rounding carried out of the mantissa, that carry is the exponent's low
bit: or-ed into an odd exponent it was lost, so values just below a
power of two came out at the power below (2 - 2^-12 gave 1.0, not 2.0).
Rectangle lists use it for the fourth corner of half-float elements.

The carry is now added into the exponent.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:57:00 +00:00
github manually merged commit 63df363159 into gh-pr/260/base 2026-11-26 03:57:02 +00:00
Author
Owner

@⁠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 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.

**@⁠jeremygold02** on GitHub · 2026-11-24 05:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/260#issuecomment-7123467418) 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.
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!260
No description provided.