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

Closed
acleofe wants to merge 1 commit from upstream-pr/1069 into main
acleofe commented 2026-11-24 15:27:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by acleofe on 2026-11-24T15:27:53Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6585309939 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1069). Originally posted by [acleofe](https://github.com/acleofe) on 2026-11-24T15:27:53Z. --- ## 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)
upstream-sync closed this pull request 2026-11-24 17:03:10 +00:00
Zipettte commented 2026-11-24 17:04:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T16:30:01Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137761081 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1069#issuecomment-7137761081). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T16:30:01Z. --- 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

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