GPU: f32_to_half keeps NaN and rounds normals to nearest even (#843) #1732

Open
github-bot wants to merge 2 commits from github-bot/gh-1732 into main AGit
Owner

@⁠moe-miau on GitHub · 2026-11-24 21:30 UTC · original
moe-miau:fix/gpu-f32-to-half-nan-rne → main · +83 −4 · 1 files · 2 commits · open

Fixes #843.

This is AndyLatimer's fix from the issue (branch fix/gpu-f32-to-half-nan-and-rne, 63111de), cherry-picked unchanged onto main with the original author kept on the commit. It was posted as a fork branch while PRs were limited to collaborators. The only PR that carried it (#1069) was closed without being merged, so nothing open fixes #843 now.

f32_to_half (crates/victory-gpu/src/primitive.rs, used for F16 attributes in the rectangle-list expansion) had two IEEE-754 errors:

  • NaN became infinity. An f32 NaN has exponent field 0xff, so it fell into exp >= 31 and returned ±0x7c00. It now returns a quiet half NaN that keeps the sign and the upper payload bits (0x7e00 | man >> 13), so a signalling NaN with a low payload stays a NaN.
  • Normals rounded ties away from zero. (man + 0x1000) >> 13 rounded 1.0 + 2^-11 up to odd 0x3c01. It now rounds to nearest even (man + 0x0fff + lsb), matching the subnormal branch just above it. The carry still stops at 0x7c00 (65520 rounds to infinity).

The added test covers quiet and signalling NaNs of both signs, infinities, ties on both sides, the overflow boundary, and a round trip of every half bit pattern.

Checked on Windows 11 x64, Rust 1.98.1 MSVC, main ffe310e + this commit:

  • cargo clippy --workspace --all-targets --locked -- -D warnings and the --all-features variant: clean
  • cargo test --workspace --locked: pass (victory-gpu: 51 passed, including the new test)
  • cargo xtask policy: ok

Independent checks are posted on #843: the new test fails on main, and an exhaustive comparison over all 2^32 f32 inputs against Rust's as f16 found 0 mismatches on non-NaN inputs.

AI-assisted (Claude Code): I reviewed the diff and reran the checks. The fix itself is AndyLatimer's (commit author Manuel Pérez).

🤖 Generated with Claude Code

**@⁠moe-miau** on GitHub · 2026-11-24 21:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1732) `moe-miau:fix/gpu-f32-to-half-nan-rne` → `main` · +83 −4 · 1 files · 2 commits · open Fixes #843. This is AndyLatimer's fix from the issue (branch `fix/gpu-f32-to-half-nan-and-rne`, `63111de`), cherry-picked unchanged onto `main` with the original author kept on the commit. It was posted as a fork branch while PRs were limited to collaborators. The only PR that carried it (#1069) was closed without being merged, so nothing open fixes #843 now. `f32_to_half` (`crates/victory-gpu/src/primitive.rs`, used for `F16` attributes in the rectangle-list expansion) had two IEEE-754 errors: - **NaN became infinity.** An f32 NaN has exponent field `0xff`, so it fell into `exp >= 31` and returned `±0x7c00`. It now returns a quiet half NaN that keeps the sign and the upper payload bits (`0x7e00 | man >> 13`), so a signalling NaN with a low payload stays a NaN. - **Normals rounded ties away from zero.** `(man + 0x1000) >> 13` rounded `1.0 + 2^-11` up to odd `0x3c01`. It now rounds to nearest even (`man + 0x0fff + lsb`), matching the subnormal branch just above it. The carry still stops at `0x7c00` (65520 rounds to infinity). The added test covers quiet and signalling NaNs of both signs, infinities, ties on both sides, the overflow boundary, and a round trip of every half bit pattern. Checked on Windows 11 x64, Rust 1.98.1 MSVC, `main` `ffe310e` + this commit: - `cargo clippy --workspace --all-targets --locked -- -D warnings` and the `--all-features` variant: clean - `cargo test --workspace --locked`: pass (`victory-gpu`: 51 passed, including the new test) - `cargo xtask policy`: ok Independent checks are posted on #843: the new test fails on `main`, and an exhaustive comparison over all 2^32 f32 inputs against Rust's `as f16` found 0 mismatches on non-NaN inputs. AI-assisted (Claude Code): I reviewed the diff and reran the checks. The fix itself is AndyLatimer's (commit author Manuel Pérez). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
(cherry picked from commit 63111deb4d007dd0830864f4e22cd5264bd01275)
Carry the rectangle regression from Marvisak/VIctory#2264. Test both a NaN input and an infinity subtraction in the rectangle fallback while retaining the converter fix from Marvisak/VIctory#1732.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:43:11 +00:00
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 22:52 UTC · original

NVIDIA run: RTX 3070 8 GB, driver 616.56, Windows 11, debug, at the PR head 5c781cf. cargo test -p victory-gpu -- f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even: passes.

AI-assisted (Claude Code). Synthetic tests only.

**@⁠upipibi** on GitHub · 2026-11-24 22:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7146751574) NVIDIA run: RTX 3070 8 GB, driver 616.56, Windows 11, debug, at the PR head `5c781cf`. `cargo test -p victory-gpu -- f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even`: passes. AI-assisted (Claude Code). Synthetic tests only.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:35 UTC · original · edited 2026-11-26 00:26 UTC

Automated triage for the maintainers.

Merge candidate. The f32_to_half fix in crates/victory-gpu/src/primitive.rs is unchanged from the head judged before: NaN becomes a quiet half NaN that keeps its sign, and normals round to nearest even with (man + 0x0fff + lsb) >> 13. The new commit be58082 only adds a test. reconstructed_fp16_corners_preserve_nan_and_infinity runs the F16 rectangle expansion (expand_rects) and checks that a NaN input stays NaN, that infinities are kept, and that infinity minus infinity in the fourth-corner arithmetic gives NaN. It keeps its original author.

On your reply: noted, and it merges cleanly with current main.

CI: passing (check).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7149721153) · edited 2026-11-26 00:26 UTC <!-- victory-triage v1 head=be58082eb266b77408721cfaa76ca47ad310ca6f verdict=merge-candidate at=2026-11-26T00:26:00Z --> Automated triage for the maintainers. **Merge candidate.** The `f32_to_half` fix in `crates/victory-gpu/src/primitive.rs` is unchanged from the head judged before: NaN becomes a quiet half NaN that keeps its sign, and normals round to nearest even with `(man + 0x0fff + lsb) >> 13`. The new commit `be58082` only adds a test. `reconstructed_fp16_corners_preserve_nan_and_infinity` runs the F16 rectangle expansion (`expand_rects`) and checks that a NaN input stays NaN, that infinities are kept, and that infinity minus infinity in the fourth-corner arithmetic gives NaN. It keeps its original author. On your reply: noted, and it merges cleanly with current `main`. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠moe-miau on GitHub · 2026-11-25 02:38 UTC · original

Rebased onto main 940d32f (head 1b97ccd). The only conflict was primitive.rs's tests: main added new tests at the end of the module and so does this PR; both are kept, and the f32_to_half change applied unchanged.

Checked on Windows 11, RTX 4070 SUPER, Rust 1.98.1 MSVC: both clippy configurations are clean, cargo xtask policy passes (182 files), and f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even passes. cargo test --workspace has 5 failures, the same 5 that fail on unmodified 940d32f on this NVIDIA card (dynamic_slots_are_held_until_released and lists_share_a_table_and_copies_keep_their_views from the 1,000,000-descriptor heap cap, plus gathers_at_a_level_match_the_hardware and the two stride_tests hardware checks); none involves this change.

AI-assisted (Claude Code).

**@⁠moe-miau** on GitHub · 2026-11-25 02:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7150986148) Rebased onto `main` `940d32f` (head `1b97ccd`). The only conflict was `primitive.rs`'s tests: `main` added new tests at the end of the module and so does this PR; both are kept, and the `f32_to_half` change applied unchanged. Checked on Windows 11, RTX 4070 SUPER, Rust 1.98.1 MSVC: both clippy configurations are clean, `cargo xtask policy` passes (182 files), and `f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even` passes. `cargo test --workspace` has 5 failures, the same 5 that fail on unmodified `940d32f` on this NVIDIA card (`dynamic_slots_are_held_until_released` and `lists_share_a_table_and_copies_keep_their_views` from the 1,000,000-descriptor heap cap, plus `gathers_at_a_level_match_the_hardware` and the two `stride_tests` hardware checks); none involves this change. AI-assisted (Claude Code).
Author
Owner

@⁠StExpeditus on GitHub · 2026-11-25 08:58 UTC · original

Test report: RTX 5060 Ti (Blackwell), Windows 11 Pro 26200, driver 621.24. Branch at 1b97ccd. cargo test --workspace --locked --no-fail-fast -> 697 passed, 5 failed, 10 ignored. The failures are the same 5 pre-existing NVIDIA 1M-view-heap-cap failures (also on main; see my reports on #2030 and #1940). Nothing introduced by this PR fails on this machine.

**@⁠StExpeditus** on GitHub · 2026-11-25 08:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7157701099) **Test report: RTX 5060 Ti (Blackwell), Windows 11 Pro 26200, driver 621.24.** Branch at `1b97ccd`. `cargo test --workspace --locked --no-fail-fast` -> 697 passed, 5 failed, 10 ignored. The failures are the same 5 pre-existing NVIDIA 1M-view-heap-cap failures (also on main; see my reports on #2030 and #1940). Nothing introduced by this PR fails on this machine.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-25 09:54 UTC · original

I reran the FP16 rectangle fallback from #2264 on 1b97ccd. The same synthetic Windows/WARP point draw read [+inf, +inf, -inf, -inf] on 940d32f and [NaN, +inf, -inf, NaN] on this head. This covers both an input NaN and a NaN generated by the fourth-corner arithmetic.

The test-only 541917b sits directly on 1b97ccd. It verifies the generated FP16 vertex through expand_rects alongside the converter tests already here. All 10 primitive tests, strict GPU Clippy with diagnostics and the staged asset policy pass.

If the extra regression is useful, it can be cherry-picked as-is. If it is folded into another commit, please retain Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-25 09:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7158955135) I reran the FP16 rectangle fallback from #2264 on `1b97ccd`. The same synthetic Windows/WARP point draw read `[+inf, +inf, -inf, -inf]` on `940d32f` and `[NaN, +inf, -inf, NaN]` on this head. This covers both an input NaN and a NaN generated by the fourth-corner arithmetic. The test-only [`541917b`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/541917b0cc82357aecb6db2df2f78cbed04ed070) sits directly on `1b97ccd`. It verifies the generated FP16 vertex through `expand_rects` alongside the converter tests already here. All 10 primitive tests, strict GPU Clippy with diagnostics and the staged asset policy pass. If the extra regression is useful, it can be cherry-picked as-is. If it is folded into another commit, please retain `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`.
Author
Owner

@⁠moe-miau on GitHub · 2026-11-25 22:53 UTC · original

@⁠andrewpozdnakov7-jpg Thanks: cherry-picked as-is (541917b → be58082, your authorship kept). cargo test -p victory-gpu --lib primitive: 10 passed.

**@⁠moe-miau** on GitHub · 2026-11-25 22:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7176905622) @⁠andrewpozdnakov7-jpg Thanks: cherry-picked as-is (`541917b` → `be58082`, your authorship kept). `cargo test -p victory-gpu --lib primitive`: 10 passed.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 00:44 UTC · original

Code review and RTX 5090 + WARP verification at head be58082eb266 merged cleanly onto main (63bcca43c6a0):

  • IEEE 754 binary32 -> binary16 audit (crates/victory-gpu/src/primitive.rs:158-192):
    • NaN: raw_exp == 0xff && man != 0 returns sign | 0x7e00 | (man >> 13) as u16, preserving sign + top 10 payload bits and forcing the quiet bit (0x0200) so low-payload sNaNs (e.g. 0x7f80_0001) quiet properly instead of collapsing to ±Inf.
    • Overflow / ±Inf: exp >= 31 returns sign | 0x7c00; max finite boundary 0x477f_efff -> 0x7bff, tie-to-even 65520.0 (0x477f_f000) rounds to 0x7c00.
    • Subnormals / underflow: exp < -10 (< 2^-25) returns sign (±0); -10 <= exp <= 0 rounds to nearest-even with carry naturally promoting max subnormal to min normal 0x0400.
    • Normals: lsb = (man >> 13) & 1; sign | (half + ((man + 0x0fff + lsb) >> 13)) as u16 handles RNE and mantissa-to-exponent carry cleanly. Confirmed primitive.rs:128 (expand_rects) is the sole call site.
  • Verified (Windows 11, NVIDIA RTX 5090 + WARP, VICTORY_D3D12_DEBUG=1, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy (default + --features diagnostics): 0 warnings
    • primitive:: draw::tests: 31/31 passed on both RTX 5090 and WARP (including exhaustive 65,536 u16 round-trip check).

LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 00:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7179073795) Code review and RTX 5090 + WARP verification at head `be58082eb266` merged cleanly onto `main` (`63bcca43c6a0`): - **IEEE 754 binary32 -> binary16 audit (`crates/victory-gpu/src/primitive.rs:158-192`)**: - **NaN**: `raw_exp == 0xff && man != 0` returns `sign | 0x7e00 | (man >> 13) as u16`, preserving sign + top 10 payload bits and forcing the quiet bit (`0x0200`) so low-payload sNaNs (e.g. `0x7f80_0001`) quiet properly instead of collapsing to `±Inf`. - **Overflow / `±Inf`**: `exp >= 31` returns `sign | 0x7c00`; max finite boundary `0x477f_efff -> 0x7bff`, tie-to-even `65520.0` (`0x477f_f000`) rounds to `0x7c00`. - **Subnormals / underflow**: `exp < -10` (`< 2^-25`) returns `sign` (`±0`); `-10 <= exp <= 0` rounds to nearest-even with carry naturally promoting max subnormal to min normal `0x0400`. - **Normals**: `lsb = (man >> 13) & 1; sign | (half + ((man + 0x0fff + lsb) >> 13)) as u16` handles RNE and mantissa-to-exponent carry cleanly. Confirmed `primitive.rs:128` (`expand_rects`) is the sole call site. - **Verified (Windows 11, NVIDIA RTX 5090 + WARP, `VICTORY_D3D12_DEBUG=1`, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy` (default + `--features diagnostics`): 0 warnings - `primitive:: draw::tests`: **31/31 passed** on both RTX 5090 and WARP (including exhaustive 65,536 `u16` round-trip check). LGTM.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1732/head:github-bot/gh-1732'
git switch 'github-bot/gh-1732'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1732'
git switch 'github-bot/gh-1732'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1732'
git switch 'github-bot/gh-1732'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1732'
git switch 'main'
git merge --squash 'github-bot/gh-1732'
git switch 'main'
git merge --ff-only 'github-bot/gh-1732'
git switch 'main'
git merge 'github-bot/gh-1732'
git push origin 'main'
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!1732
No description provided.