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

Open
github-bot wants to merge 1 commit from github-bot/gh-337 into main AGit
Owner

@⁠Caasares on GitHub · 2026-11-24 23:36 UTC · original
Caasares:fix/gpu-rect-fp16-nan-and-ties → main · +104 −4 · 1 files · 1 commits · open

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.

**@⁠Caasares** on GitHub · 2026-11-24 23:36 UTC · [original](https://github.com/fxceless/victory-fork/pull/337) `Caasares:fix/gpu-rect-fp16-nan-and-ties` → `main` · +104 −4 · 1 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:41:06 +00:00
Author
Owner

@⁠kaayzouee on GitHub · 2026-11-25 01:01 UTC · original

Duplicate of #315?

**@⁠kaayzouee** on GitHub · 2026-11-25 01:01 UTC · [original](https://github.com/fxceless/victory-fork/pull/337#issuecomment-7149124209) Duplicate of #315?
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/337/head:github-bot/gh-337'
git switch 'github-bot/gh-337'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-337'
git switch 'github-bot/gh-337'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-337'
git switch 'github-bot/gh-337'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-337'
git switch 'main'
git merge --squash 'github-bot/gh-337'
git switch 'main'
git merge --ff-only 'github-bot/gh-337'
git switch 'main'
git merge 'github-bot/gh-337'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.