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

Open
moe-miau wants to merge 2 commits from upstream-pr/1732 into main
moe-miau commented 2026-11-24 21:30:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by moe-miau on 2026-11-24T21:30:57Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:5480671921 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1732). Originally posted by [moe-miau](https://github.com/moe-miau) on 2026-11-24T21:30:57Z. --- 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)
catminou4 commented 2026-11-24 21:36:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T21:36:46Z.

Review: COMMENTED


Static review only: this crate is Windows-only (D3D12/windows_sys/Win32) — nothing compiles or runs on macOS; source review of the diff.

f32_to_half fixes: NaN preserved as quiet f16 NaN with sign and upper payload bits (man >> 13, bit 9 set so a signalling NaN with payload below bit 13 never becomes infinity), overflow → infinity, and round-to-nearest-even implemented manually via lsb/sticky trick. The unit test covers quiet/signalling NaNs both signs plus normals. This is host-side bit manipulation — pure logic, but victory-gpu doesn't compile on macOS; the test runs on Windows only.

<!-- greyfinch-sync:Marvisak/VIctory:review:6047808612 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1732#pullrequestreview-6047808612). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T21:36:46Z. **Review:** COMMENTED --- Static review only: this crate is Windows-only (D3D12/windows_sys/Win32) — nothing compiles or runs on macOS; source review of the diff. f32_to_half fixes: NaN preserved as quiet f16 NaN with sign and upper payload bits (man >> 13, bit 9 set so a signalling NaN with payload below bit 13 never becomes infinity), overflow → infinity, and round-to-nearest-even implemented manually via lsb/sticky trick. The unit test covers quiet/signalling NaNs both signs plus normals. This is host-side bit manipulation — pure logic, but victory-gpu doesn't compile on macOS; the test runs on Windows only.
upipibi commented 2026-11-24 23:06:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-24T22:52:50Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7146751574 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7146751574). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-24T22:52:50Z. --- 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.
Marvisak commented 2026-11-25 02:06:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:35:20Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149721153 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7149721153). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:35:20Z. --- <!-- 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)_
moe-miau commented 2026-11-25 03:03:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by moe-miau on 2026-11-25T02:38:26Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150986148 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7150986148). Originally posted by [moe-miau](https://github.com/moe-miau) on 2026-11-25T02:38:26Z. --- 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).
StExpeditus commented 2026-11-25 05:06:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by StExpeditus on 2026-11-25T04:23:35Z.


Test report: RTX 5060 Ti (Blackwell), Windows 11 Pro 26200, driver 621.24. Branch at 1b97ccd. cargo test --workspace --locked --no-fail-fast -> all green: 0 passed, 0 ignored, 0 failed.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152800928 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7152800928). Originally posted by [StExpeditus](https://github.com/StExpeditus) on 2026-11-25T04:23:35Z. --- **Test report: RTX 5060 Ti (Blackwell), Windows 11 Pro 26200, driver 621.24.** Branch at `1b97ccd`. `cargo test --workspace --locked --no-fail-fast` -> **all green**: 0 passed, 0 ignored, 0 failed.
StExpeditus commented 2026-11-25 09:06:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by StExpeditus on 2026-11-25T08:58:06Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7157701099 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7157701099). Originally posted by [StExpeditus](https://github.com/StExpeditus) on 2026-11-25T08:58:06Z. --- **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.
andrewpozdnakov7-jpg commented 2026-11-25 10:07:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-25T09:54:12Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158955135 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7158955135). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-25T09:54:12Z. --- 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]>`.
moe-miau commented 2026-11-25 23:06:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by moe-miau on 2026-11-25T22:53:27Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176905622 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7176905622). Originally posted by [moe-miau](https://github.com/moe-miau) on 2026-11-25T22:53:27Z. --- @andrewpozdnakov7-jpg Thanks: cherry-picked as-is (`541917b` → `be58082`, your authorship kept). `cargo test -p victory-gpu --lib primitive`: 10 passed.
fuzeofficlal commented 2026-11-26 01:04:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T00:44:54Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179073795 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7179073795). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T00:44:54Z. --- 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.
cloph-dsp commented 2026-11-27 05:50:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-27T05:50:42Z.


Independent CPU-only verification of PR #1732 (f32_to_half: preserve NaN payloads and round normals to nearest-even).

  • Windows, rustc/cargo 1.100.0/MSVC, worktree-local CARGO_TARGET_DIR (D:\Victory-Contrib\target-1732-watch), CARGO_BUILD_JOBS=2.
  • Head be58082eb266b77408721cfaa76ca47ad310ca6f, merge base 940d32f.
  • Command: cargo test --locked -p victory-gpu --lib -- primitive::tests::f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even --exact --test-threads=1 --quiet -> 1 passed; 0 failed; 146 filtered.
  • Negative control: git grep -F for the new test name on 940d32f returned no hits (test absent on base).
  • git diff --check 940d32f HEAD exit 0 (clean).
  • Only the named synthetic test was replayed; full crate suite and render/shader paths were not exercised. This PR already carries other verification reports; this is an additional Windows/MSVC data point.
  • Only pre-existing fetch_update deprecation warnings.
  • AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only conversion test). No game data, assets, shaders, DRM or decrypt used.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7210994782 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1732#issuecomment-7210994782). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-27T05:50:42Z. --- Independent CPU-only verification of PR #1732 (`f32_to_half: preserve NaN payloads and round normals to nearest-even`). - Windows, rustc/cargo 1.100.0/MSVC, worktree-local `CARGO_TARGET_DIR` (`D:\Victory-Contrib\target-1732-watch`), `CARGO_BUILD_JOBS=2`. - Head `be58082eb266b77408721cfaa76ca47ad310ca6f`, merge base `940d32f`. - Command: `cargo test --locked -p victory-gpu --lib -- primitive::tests::f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even --exact --test-threads=1 --quiet` -> `1 passed; 0 failed; 146 filtered`. - Negative control: `git grep -F` for the new test name on `940d32f` returned no hits (test absent on base). - `git diff --check 940d32f HEAD` exit 0 (clean). - Only the named synthetic test was replayed; full crate suite and render/shader paths were not exercised. This PR already carries other verification reports; this is an additional Windows/MSVC data point. - Only pre-existing `fetch_update` deprecation warnings. - AI-assisted (Hy4 flash). GPU/driver: N/A (CPU-only conversion test). No game data, assets, shaders, DRM or decrypt used.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1732:upstream-pr/1732'
git switch 'upstream-pr/1732'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1732'
git switch 'upstream-pr/1732'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1732'
git switch 'upstream-pr/1732'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1732'
git switch 'main'
git merge --squash 'upstream-pr/1732'
git switch 'main'
git merge --ff-only 'upstream-pr/1732'
git switch 'main'
git merge 'upstream-pr/1732'
git push origin 'main'
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!1731
No description provided.