Skip to content

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

Open
Caasares wants to merge 1 commit into
fxceless:mainfrom
Caasares:fix/gpu-rect-fp16-nan-and-ties
Open

Caasares wants to merge 1 commit into
fxceless:mainfrom
Caasares:fix/gpu-rect-fp16-nan-and-ties

Conversation

@Caasares

Copy link
Copy Markdown

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.

@kaayzouee

Copy link
Copy Markdown

Duplicate of #315?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GPU rectangle CPU fallback: FP16 conversion turns NaN into infinity and rounds ties up

2 participants