GPU: f32_to_half turns NaN into infinity and ties normals away from zero (fix on a fork) #843
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#843
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@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).github.com/AndyLatimer/VIctory@63111deb4dBug
In
crates/victory-gpu/src/primitive.rs(f32_to_half):Two IEEE-754 binary32-to-binary16 bugs occur during rectangle-list vertex expansion for
F16elements:raw_exp == 0xffwithman != 0producesexp = 143 >= 31and returnssign | 0x7c00(+inf/-inf) instead of a quiet NaN, even thoughhalf_to_f32right above preserves NaNs.(man + 0x1000) >> 13rounds ties (e.g.1.0 + 2^-11 = 0x3f80_1000) up to odd0x3c01instead of even0x3c00, contradicting IEEE-754 and the subnormal branch directly above it (which explicitly rounds ties to even).Fix
raw_exp == 0xff && man != 0beforeexp >= 31, returningsign | 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).let lsb = (man >> 13) & 1; sign | (half + ((man + 0x0fff + lsb) >> 13)) as u16.primitive::tests::f32_to_half_preserves_nan_and_rounds_normals_to_nearest_evencovering qNaN/sNaN (both signs + payload), infinities, normal ties to even/odd, overflow boundaries, and exhaustive round-trip across all 65,536u16f16 bit patterns.Verification
f32_to_half_preserves_nan_and_rounds_normals_to_nearest_evenfailed on NaN (f32_to_half(0x7fc00000) = 0x7c00, want 0x7e00) and on normal ties to even.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 referenced this issue2026-11-26 04:43:10 +00:00
@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@Louisreed on GitHub · 2026-11-24 13:25 UTC · original
Independently verified
63111de(fix/gpu-f32-to-half-nan-and-rne, baseffe310e, which is stillmain) on Windows 10 22H2 (19045), Rust 1.98.1 MSVC.cargo test -p victory-gpu --locked: 51 passed, 0 failed, 1 ignored.main(test module swapped in,main's code):f32_to_half(0x7fc00000) = 0x7c00, want 0x7e00.-D warnings,rustfmt --edition 2024 --check primitive.rsandcargo xtask policy(154 files ok) are all clean.f32_to_halfwith Rust's nativev as f16(IEEE round-to-nearest-even) for all 2^32f32bit 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 onmain'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) >> 13is correct RNE, and its carry into the exponent still saturates at0x7c00.Not tested:
--features diagnosticsand any title run. Synthetic inputs only, no game data.AI-assisted (Claude). I ran the checks locally.
@nopsled0x on GitHub · 2026-11-24 13:34 UTC · original
I checked
fix/gpu-f32-to-half-nan-and-rne(63111de, on top offfe310e) on Windows 10, debug build:cargo test -p victory-gpu --locked: 51 passed, 0 failed, 1 ignored, includingprimitive::tests::f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even.cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: clean.man + 0x0fff + lsb) cannot overflowu32, and a mantissa carry stops at0x7c00, 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 isKind::F16inprimitive.rs.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.
@Marvisak on GitHub · 2026-11-25 03:55 UTC · original
Automated triage for the maintainers.
Confirmed on current
main:f32_to_halfincrates/victory-gpu/src/primitive.rsstill returnssign | 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