gpu(primitive): preserve NaN and use ties-to-even in FP16 rect expansion #337

Open
Caasares wants to merge 1 commit from upstream-pr/337 into main
Caasares commented 2026-11-24 23:36:21 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Caasares on 2026-11-24T23:36:21Z.


Fixes #245

Summary

Fixes two IEEE-754 FP16 conversion bugs in the GPU rectangle list CPU fallback (f32_to_half in crates/victory-gpu/src/primitive.rs):

  • Root cause:
    1. exp >= 31 caught raw_exp == 0xff inputs indiscriminately, turning NaN values into ±inf (sign | 0x7c00).
    2. Normal values used (man + 0x1000) >> 13, which always rounds exact halfway ties up instead of rounding ties to even (unlike the subnormal branch).
  • Fix:
    1. Check raw_exp == 0xff before normal/overflow handling: when man != 0, return a sign-preserving quiet NaN with a guaranteed nonzero mantissa (sign | 0x7e00 | ((man >> 13) & 0x03ff)); when man == 0, return sign | 0x7c00.
    2. For normal values, resolve ties using the retained mantissa LSB parity ((man >> 13) & 1) ((man + 0x0fff + ((man >> 13) & 1)) >> 13), preserving carry-out into the exponent and overflow to sign | 0x7c00.
  • Tests:
    • Added R16_FLOAT expand_rects regression tests for both #245 cases (normal even tie 1.0, 0.0, 2^-11 -> 0x3c00 and quiet NaN 0x7e00 / 0xfe00 preservation) and verified they failed before the fix and pass after.
    • Added direct f32_to_half edge-case tests covering odd tie rounding up (1.0 + 2^-10 + 2^-11 -> 0x3c02), non-ties, ±0.0, ±inf, finite overflow to ±inf, quiet/low-payload NaNs, and subnormal round-to-even ties.

Verification

  • rustfmt --edition 2024 --check crates/victory-gpu/src/primitive.rs — passed
  • cargo clippy --workspace --all-targets --locked -- -D warnings — passed (0 warnings)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings — passed (0 warnings)
  • cargo test --workspace --locked — 499 passed; 0 failed; 7 ignored across the workspace (victory-gpu: 113 passed; 0 failed; 1 ignored; xtask including repository_passes_policy: 4 passed; 0 failed)

Note: AI-assisted fix; contains no game files, keys, or assets.

<!-- greyfinch-sync:fxceless/victory-fork:issue:6593620002 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/337). Originally posted by [Caasares](https://github.com/Caasares) on 2026-11-24T23:36:21Z. --- Fixes #245 ## Summary Fixes two IEEE-754 FP16 conversion bugs in the GPU rectangle list CPU fallback (`f32_to_half` in `crates/victory-gpu/src/primitive.rs`): - **Root cause:** 1. `exp >= 31` caught `raw_exp == 0xff` inputs indiscriminately, turning `NaN` values into `±inf` (`sign | 0x7c00`). 2. Normal values used `(man + 0x1000) >> 13`, which always rounds exact halfway ties up instead of rounding ties to even (unlike the subnormal branch). - **Fix:** 1. Check `raw_exp == 0xff` before normal/overflow handling: when `man != 0`, return a sign-preserving quiet `NaN` with a guaranteed nonzero mantissa (`sign | 0x7e00 | ((man >> 13) & 0x03ff)`); when `man == 0`, return `sign | 0x7c00`. 2. For normal values, resolve ties using the retained mantissa LSB parity `((man >> 13) & 1)` (`(man + 0x0fff + ((man >> 13) & 1)) >> 13`), preserving carry-out into the exponent and overflow to `sign | 0x7c00`. - **Tests:** - Added `R16_FLOAT` `expand_rects` regression tests for both `#245` cases (normal even tie `1.0, 0.0, 2^-11 -> 0x3c00` and quiet `NaN` `0x7e00` / `0xfe00` preservation) and verified they failed before the fix and pass after. - Added direct `f32_to_half` edge-case tests covering odd tie rounding up (`1.0 + 2^-10 + 2^-11 -> 0x3c02`), non-ties, `±0.0`, `±inf`, finite overflow to `±inf`, quiet/low-payload `NaN`s, and subnormal round-to-even ties. ## Verification - `rustfmt --edition 2024 --check crates/victory-gpu/src/primitive.rs` — passed - `cargo clippy --workspace --all-targets --locked -- -D warnings` — passed (0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` — passed (0 warnings) - `cargo test --workspace --locked` — `499 passed; 0 failed; 7 ignored` across the workspace (`victory-gpu`: `113 passed; 0 failed; 1 ignored`; `xtask` including `repository_passes_policy`: `4 passed; 0 failed`) Note: AI-assisted fix; contains no game files, keys, or assets.
kaayzouee commented 2026-11-25 01:12:06 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by kaayzouee on 2026-11-25T01:01:17Z.


Duplicate of #315?

<!-- greyfinch-sync:fxceless/victory-fork:comment:7149124209 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/337#issuecomment-7149124209). Originally posted by [kaayzouee](https://github.com/kaayzouee) on 2026-11-25T01:01:17Z. --- Duplicate of #315?
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/337:upstream-pr/337'
git switch 'upstream-pr/337'

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/337'
git switch 'upstream-pr/337'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/337'
git switch 'upstream-pr/337'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/337'
git switch 'main'
git merge --squash 'upstream-pr/337'
git switch 'main'
git merge --ff-only 'upstream-pr/337'
git switch 'main'
git merge 'upstream-pr/337'
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
fxceless/victory-fork!337
No description provided.