GPU: f32_to_half turns NaN into infinity and ties normals away from zero (fix on a fork) #843

Open
opened 2026-11-26 04:14:03 +00:00 by github-bot · 4 comments
Owner

@⁠AndyLatimer on GitHub · 2026-11-24 12:07 UTC · original

Pull requests are currently restricted to collaborators (#717), so I am sharing this bug report and verified fix from a fork branch off main (ffe310e).

Bug

In crates/victory-gpu/src/primitive.rs (f32_to_half):

let exp = ((bits >> 23) & 0xff) as i32 - 127 + 15;
let man = bits & 0x7f_ffff;
if exp >= 31 {
    return sign | 0x7c00; // too large: infinity
}
...
let half = (exp as u32) << 10;
sign | (half + ((man + 0x1000) >> 13)) as u16

Two IEEE-754 binary32-to-binary16 bugs occur during rectangle-list vertex expansion for F16 elements:

  1. NaN becomes ±Infinity: raw_exp == 0xff with man != 0 produces exp = 143 >= 31 and returns sign | 0x7c00 (+inf / -inf) instead of a quiet NaN, even though half_to_f32 right above preserves NaNs.
  2. Normal rounding ties away from zero instead of to even: (man + 0x1000) >> 13 rounds ties (e.g. 1.0 + 2^-11 = 0x3f80_1000) up to odd 0x3c01 instead of even 0x3c00, contradicting IEEE-754 and the subnormal branch directly above it (which explicitly rounds ties to even).

Fix

  • Handle raw_exp == 0xff && man != 0 before exp >= 31, returning sign | 0x7e00 | (man >> 13) as u16 (quiet NaN preserving sign and upper payload bits, and keeping sNaNs with payload below bit 13 as NaN rather than infinity).
  • Use IEEE-754 round-to-nearest-even for normals: let lsb = (man >> 13) & 1; sign | (half + ((man + 0x0fff + lsb) >> 13)) as u16.
  • Added synthetic unit test primitive::tests::f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even covering qNaN/sNaN (both signs + payload), infinities, normal ties to even/odd, overflow boundaries, and exhaustive round-trip across all 65,536 u16 f16 bit patterns.

Verification

  • Before fix: f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even failed on NaN (f32_to_half(0x7fc00000) = 0x7c00, want 0x7e00) and on normal ties to even.
  • After fix (Windows 11):
    • cargo test -p victory-gpu: 51 passed, 0 failed, 1 ignored.
    • cargo test -p victory-gpu --features diagnostics: 54 passed, 0 failed, 1 ignored.
    • cargo clippy -p victory-gpu --all-targets -- -D warnings (default and --features diagnostics): clean.
    • cargo check -p victory --features diagnostics: clean.
    • rustfmt --edition 2024 --check crates/victory-gpu/src/primitive.rs: clean.
    • cargo xtask policy: 154 files ok.

Disclosure

Synthetic tests only; no game data or console SDK assets were used. Assisted by Google Antigravity.

**@⁠AndyLatimer** on GitHub · 2026-11-24 12:07 UTC · [original](https://github.com/Marvisak/VIctory/issues/843) Pull requests are currently restricted to collaborators (#717), so I am sharing this bug report and verified fix from a fork branch off `main` (`ffe310e`). - **Branch:** https://github.com/AndyLatimer/VIctory/tree/fix/gpu-f32-to-half-nan-and-rne - **Commit:** https://github.com/AndyLatimer/VIctory/commit/63111deb4d007dd0830864f4e22cd5264bd01275 ### Bug In `crates/victory-gpu/src/primitive.rs` (`f32_to_half`): ```rust let exp = ((bits >> 23) & 0xff) as i32 - 127 + 15; let man = bits & 0x7f_ffff; if exp >= 31 { return sign | 0x7c00; // too large: infinity } ... let half = (exp as u32) << 10; sign | (half + ((man + 0x1000) >> 13)) as u16 ``` Two IEEE-754 binary32-to-binary16 bugs occur during rectangle-list vertex expansion for `F16` elements: 1. **NaN becomes ±Infinity:** `raw_exp == 0xff` with `man != 0` produces `exp = 143 >= 31` and returns `sign | 0x7c00` (`+inf` / `-inf`) instead of a quiet NaN, even though `half_to_f32` right above preserves NaNs. 2. **Normal rounding ties away from zero instead of to even:** `(man + 0x1000) >> 13` rounds ties (e.g. `1.0 + 2^-11 = 0x3f80_1000`) up to odd `0x3c01` instead of even `0x3c00`, contradicting IEEE-754 and the subnormal branch directly above it (which explicitly rounds ties to even). ### Fix - Handle `raw_exp == 0xff && man != 0` before `exp >= 31`, returning `sign | 0x7e00 | (man >> 13) as u16` (quiet NaN preserving sign and upper payload bits, and keeping sNaNs with payload below bit 13 as NaN rather than infinity). - Use IEEE-754 round-to-nearest-even for normals: `let lsb = (man >> 13) & 1; sign | (half + ((man + 0x0fff + lsb) >> 13)) as u16`. - Added synthetic unit test `primitive::tests::f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even` covering qNaN/sNaN (both signs + payload), infinities, normal ties to even/odd, overflow boundaries, and exhaustive round-trip across all 65,536 `u16` f16 bit patterns. ### Verification - **Before fix:** `f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even` failed on NaN (`f32_to_half(0x7fc00000) = 0x7c00, want 0x7e00`) and on normal ties to even. - **After fix (Windows 11):** - `cargo test -p victory-gpu`: 51 passed, 0 failed, 1 ignored. - `cargo test -p victory-gpu --features diagnostics`: 54 passed, 0 failed, 1 ignored. - `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default and `--features diagnostics`): clean. - `cargo check -p victory --features diagnostics`: clean. - `rustfmt --edition 2024 --check crates/victory-gpu/src/primitive.rs`: clean. - `cargo xtask policy`: 154 files ok. ### Disclosure Synthetic tests only; no game data or console SDK assets were used. Assisted by Google Antigravity.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:03 +00:00
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 12:52 UTC · original

The indirect rectangle expander in #346 has a separate HLSL conversion. Melissa checked it on current head 8ef6bfe: midpoint rounding and subnormals are already handled there, but the generated fourth corner still converted a NaN half attribute to infinity. We carried your CPU fix onto that branch and submitted the matching HLSL change with an RTX 4090 readback regression. Both paths now return a quiet NaN and all other output bytes agree. Follow-up PR and test details

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 12:52 UTC · [original](https://github.com/Marvisak/VIctory/issues/843#issuecomment-7132030460) The indirect rectangle expander in #346 has a separate HLSL conversion. Melissa checked it on current head 8ef6bfe: midpoint rounding and subnormals are already handled there, but the generated fourth corner still converted a NaN half attribute to infinity. We carried your CPU fix onto that branch and submitted the matching HLSL change with an RTX 4090 readback regression. Both paths now return a quiet NaN and all other output bytes agree. [Follow-up PR and test details](https://github.com/BastienRodz/VIctory/pull/1)
Author
Owner

@⁠Louisreed on GitHub · 2026-11-24 13:25 UTC · original

Independently verified 63111de (fix/gpu-f32-to-half-nan-and-rne, base ffe310e, which is still main) on Windows 10 22H2 (19045), Rust 1.98.1 MSVC.

  • cargo test -p victory-gpu --locked: 51 passed, 0 failed, 1 ignored.
  • The new test fails on unfixed main (test module swapped in, main's code): f32_to_half(0x7fc00000) = 0x7c00, want 0x7e00.
  • Clippy -D warnings, rustfmt --edition 2024 --check primitive.rs and cargo xtask policy (154 files ok) are all clean.
  • Exhaustive check: I compared the patched f32_to_half with Rust's native v as f16 (IEEE round-to-nearest-even) for all 2^32 f32 bit patterns. There were 0 mismatches on non-NaN inputs, and every NaN maps to a same-sign f16 NaN. As a control, the same harness on main's version reports 30,720 rounding mismatches and 16,777,214 NaN failures, so it does detect the original bugs.

The diff matches the description: the NaN branch runs before the overflow check and always sets bit 9, so a NaN can't collapse to infinity. (man + 0x0fff + lsb) >> 13 is correct RNE, and its carry into the exponent still saturates at 0x7c00.

Not tested: --features diagnostics and any title run. Synthetic inputs only, no game data.

AI-assisted (Claude). I ran the checks locally.

**@⁠Louisreed** on GitHub · 2026-11-24 13:25 UTC · [original](https://github.com/Marvisak/VIctory/issues/843#issuecomment-7132818581) Independently verified `63111de` (`fix/gpu-f32-to-half-nan-and-rne`, base `ffe310e`, which is still `main`) on Windows 10 22H2 (19045), Rust 1.98.1 MSVC. - `cargo test -p victory-gpu --locked`: 51 passed, 0 failed, 1 ignored. - The new test fails on unfixed `main` (test module swapped in, `main`'s code): `f32_to_half(0x7fc00000) = 0x7c00, want 0x7e00`. - Clippy `-D warnings`, `rustfmt --edition 2024 --check primitive.rs` and `cargo xtask policy` (154 files ok) are all clean. - **Exhaustive check:** I compared the patched `f32_to_half` with Rust's native `v as f16` (IEEE round-to-nearest-even) for all 2^32 `f32` bit patterns. There were 0 mismatches on non-NaN inputs, and every NaN maps to a same-sign f16 NaN. As a control, the same harness on `main`'s version reports 30,720 rounding mismatches and 16,777,214 NaN failures, so it does detect the original bugs. The diff matches the description: the NaN branch runs before the overflow check and always sets bit 9, so a NaN can't collapse to infinity. `(man + 0x0fff + lsb) >> 13` is correct RNE, and its carry into the exponent still saturates at `0x7c00`. Not tested: `--features diagnostics` and any title run. Synthetic inputs only, no game data. _AI-assisted (Claude). I ran the checks locally._
Author
Owner

@⁠nopsled0x on GitHub · 2026-11-24 13:34 UTC · original

I checked fix/gpu-f32-to-half-nan-and-rne (63111de, on top of ffe310e) on Windows 10, debug build:

  • cargo test -p victory-gpu --locked: 51 passed, 0 failed, 1 ignored, including primitive::tests::f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even.
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: clean.
  • From reading the diff (not tested beyond the unit test above): the NaN check runs before the overflow check and keeps the sign and the upper payload bits, so a signaling NaN with a low payload becomes a quiet NaN and not infinity. The tie-to-even rounding (man + 0x0fff + lsb) cannot overflow u32, and a mantissa carry stops at 0x7c00, so 65520 rounds to infinity as expected. The subnormal branch from #418 is untouched, and the exponent carry from #260 still works the same way: only the rounding bias changed. The only production caller I found is Kind::F16 in primitive.rs.
  • The round-trip part of the test compares the class and sign of NaNs, not their bits. That fits, since a signaling f16 NaN comes back quiet.

Not verified: --features diagnostics, cargo test --workspace, xtask policy, rustfmt, workspace-wide clippy, the separate HLSL conversion mentioned in the comment above (#346), or in-game behavior.

AI-assisted verification (Claude); tests were run locally by me.

**@⁠nopsled0x** on GitHub · 2026-11-24 13:34 UTC · [original](https://github.com/Marvisak/VIctory/issues/843#issuecomment-7133017243) I checked `fix/gpu-f32-to-half-nan-and-rne` (63111de, on top of ffe310e) on Windows 10, debug build: - `cargo test -p victory-gpu --locked`: 51 passed, 0 failed, 1 ignored, including `primitive::tests::f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even`. - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings`: clean. - From reading the diff (not tested beyond the unit test above): the NaN check runs before the overflow check and keeps the sign and the upper payload bits, so a signaling NaN with a low payload becomes a quiet NaN and not infinity. The tie-to-even rounding (`man + 0x0fff + lsb`) cannot overflow `u32`, and a mantissa carry stops at `0x7c00`, so 65520 rounds to infinity as expected. The subnormal branch from #418 is untouched, and the exponent carry from #260 still works the same way: only the rounding bias changed. The only production caller I found is `Kind::F16` in `primitive.rs`. - The round-trip part of the test compares the class and sign of NaNs, not their bits. That fits, since a signaling f16 NaN comes back quiet. Not verified: `--features diagnostics`, `cargo test --workspace`, `xtask policy`, `rustfmt`, workspace-wide clippy, the separate HLSL conversion mentioned in the comment above (#346), or in-game behavior. _AI-assisted verification (Claude); tests were run locally by me._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:55 UTC · original

Automated triage for the maintainers.

Confirmed on current main: f32_to_half in crates/victory-gpu/src/primitive.rs still returns sign | 0x7c00 (infinity) for any input whose exponent field is all ones, NaN included. Normals are still rounded with (man + 0x1000) >> 13, which rounds ties away from zero, while the subnormal branch rounds ties to even.

The open PR #1732 carries this fix and references this issue, so review continues there. The HLSL conversion in the indirect rectangle expander mentioned in the comments is a separate code path from this function.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:55 UTC · [original](https://github.com/Marvisak/VIctory/issues/843#issuecomment-7152362256) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-24T13:34:08Z at=2026-11-25T03:55:13Z --> Automated triage for the maintainers. Confirmed on current `main`: `f32_to_half` in `crates/victory-gpu/src/primitive.rs` still returns `sign | 0x7c00` (infinity) for any input whose exponent field is all ones, NaN included. Normals are still rounded with `(man + 0x1000) >> 13`, which rounds ties away from zero, while the subnormal branch rounds ties to even. The open PR #1732 carries this fix and references this issue, so review continues there. The HLSL conversion in the indirect rectangle expander mentioned in the comments is a separate code path from this function. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
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#843
No description provided.