GPU: f32_to_half keeps NaN and rounds ties to even (fixes #843) #1069

Closed
github-bot wants to merge 1 commit from gh-pr/1069/head into gh-pr/1069/base
Owner

@⁠acleofe on GitHub · 2026-11-24 15:27 UTC · original
fix/gpu-f32-to-half-nan-even → main · +28 −3 · 1 files · 1 commits · closed without merging

What

f32_to_half (used for F16 elements when rectangle lists are expanded) converts NaN to a quiet half NaN, keeping the payload's top bits, and rounds normal values to nearest with ties to even: (man + 0xfff + ((man >> 13) & 1)) >> 13 in place of (man + 0x1000) >> 13.

Why

  • NaN (exponent field 255) took the exp >= 31 branch and came out as infinity.
  • + 0x1000 rounds a value exactly halfway between two halves away from zero; IEEE 754 and the GPU round to the even one. The subnormal branch already rounded to even.

Test

  • halves_keep_nan_and_round_ties_to_even: NaN, -NaN and the smallest NaN payload stay NaN; infinities stay; 1 + 2^-11 gives 0x3c00, 1 + 3·2^-11 gives 0x3c02, just past halfway rounds up. Fails before this change.
  • The existing carry, overflow and subnormal tests pass unchanged.
  • cargo clippy --workspace --all-targets [--all-features] --locked -- -D warnings and cargo test --workspace --locked pass.

🤖 Generated with Claude Code

**@⁠acleofe** on GitHub · 2026-11-24 15:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/1069) `fix/gpu-f32-to-half-nan-even` → `main` · +28 −3 · 1 files · 1 commits · closed without merging ## What `f32_to_half` (used for F16 elements when rectangle lists are expanded) converts NaN to a quiet half NaN, keeping the payload's top bits, and rounds normal values to nearest with ties to even: `(man + 0xfff + ((man >> 13) & 1)) >> 13` in place of `(man + 0x1000) >> 13`. ## Why - NaN (exponent field 255) took the `exp >= 31` branch and came out as infinity. - `+ 0x1000` rounds a value exactly halfway between two halves away from zero; IEEE 754 and the GPU round to the even one. The subnormal branch already rounded to even. ## Test - `halves_keep_nan_and_round_ties_to_even`: NaN, -NaN and the smallest NaN payload stay NaN; infinities stay; `1 + 2^-11` gives `0x3c00`, `1 + 3·2^-11` gives `0x3c02`, just past halfway rounds up. Fails before this change. - The existing carry, overflow and subnormal tests pass unchanged. - `cargo clippy --workspace --all-targets [--all-features] --locked -- -D warnings` and `cargo test --workspace --locked` pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
NaN took the overflow branch and became infinity, and normal values
rounded ties away from zero (+0x1000) where IEEE 754 and the GPU round
to even.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:18:52 +00:00
github-bot closed this pull request 2026-11-26 04:18:52 +00:00
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 16:30 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 5f026f3: cargo test -p victory-gpu passes, 51 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 16:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1069#issuecomment-7137761081) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 5f026f3: `cargo test -p victory-gpu` passes, 51 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._

Pull request closed

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!1069
No description provided.