Skip to content

spirv_asm: truncate excess hex-float mantissa bits toward zero, as spirv-as does - #7994

Open
pmaho wants to merge 1 commit into
ItsNotRexed:mainfrom
pmaho:fix/spirv-asm-hexfloat-truncate
Open

pmaho wants to merge 1 commit into
ItsNotRexed:mainfrom
pmaho:fix/spirv-asm-hexfloat-truncate

Conversation

@pmaho

@pmaho pmaho commented Nov 23, 2026

Copy link
Copy Markdown

Fixes #4330.

What

tools/spirv_asm.py encoded hex-float literals with float.fromhex + struct.pack, which round to nearest. spirv-as keeps only the target mantissa bits and drops the rest (truncates toward zero). The two assemblers wrote different constants:

literal before spirv-as
f16 0x1.8p-24 0x0002 0x0001
f16 0x1.ffffp-24 0x0002 0x0001
f32 0x1.0000018p+0 0x3F800001 0x3F800000

Change

The hex-float fallback now shifts the significand directly (integer arithmetic), so excess bits are dropped the same way spirv-as drops them. Inf/NaN handling is unchanged.

Verification

  • Compared against spirv-as (v2026.1, --target-env vulkan1.2) on 600 generated literals (300 f16, 300 f32, covering normals, subnormals and truncation): 0 mismatches (checked by reading the constant words from the spirv-as binary).
  • New regression test test_hex_float_excess_mantissa_truncates_toward_zero_like_spirv_as: fails on the previous code (4 of 5 subtests), passes now.
  • python -m unittest discover -s tests -p 'test_spirv_asm*.py' -t tests: OK.
  • scripts/check_no_game_data.py: OK. No game files attached.
  • Linux host only; no GPU involved.

AI disclosure

AI-assisted (Claude). The author reviewed the change and reran the comparison against spirv-as before opening this PR.

…irv-as does

The fallback encoded hex-float literals with float.fromhex + struct.pack,
which round to nearest. spirv-as keeps only the target mantissa bits and
drops the rest, so the two assemblers wrote different constants (e.g. f16
0x1.8p-24: ours 0x0002, spirv-as 0x0001).

Encode the significand directly with an integer shift, matching spirv-as.
Verified bit-for-bit against spirv-as on 600 generated f16/f32 literals
(normals, subnormals, truncation cases): 0 mismatches.

Adds a regression test with values measured from spirv-as v2026.1 (fails
on the previous code, passes now). Refs ItsNotRexed#4330

@0xKaji 0xKaji left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the diff: handles excess mantissa truncation toward zero as spirv-as does, with measured cases. Good.

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested 498d6e6 on Ubuntu 24.04.4 (kernel 7.0.0-31-generic), Python 3.12.3, CPU only, no game files: pass.

  • python3 -I tests/test_spirv_asm.py: 24 tests run, 23 pass, 1 skipped.
  • Negative control: base 86a7807d (merge-base) tool with the PR test file: 24 run, 19 pass, 4 fail, 1 skipped — the four failures are all subtests of the new test_hex_float_excess_mantissa_truncates_toward_zero_like_spirv_as (0x1.0000018p+0, 0x1.8p-24, 0x1.ffffp-24, 0x1.29a8p-22); no other test failed. The fifth case (0x1.3e7ap-24) already matches on the base.
  • Code read: the hex-float branch now derives the mantissa by bit truncation (>> (top - mant_bits) for normal values, << for short significands) instead of float.fromhex + struct.pack, and returns the infinity encoding when the biased exponent reaches exp_max; the subnormal path shifts sig directly. Decimal literals still go through float().
  • compileall / check_no_game_data / diff --check: pass.

Synthetic inputs only; no game data.

AI-assisted.

@skorex

skorex commented Nov 23, 2026

Copy link
Copy Markdown

Duplicate of #4349

#4349 makes the same change and was opened earlier. Topic: spirv_asm: truncate excess hex-float mantissa bits toward zero, like spirv-as.

Suggest closing this PR in favour of #4349, and porting anything this one adds that #4349 lacks there.

@Batushn

Batushn commented Nov 23, 2026

Copy link
Copy Markdown

Tested 498d6e6e on Windows 11, Python 3.12.10 (CPU only, no game data): pass.

  • python -I tests/test_spirv_asm.py -v: 23 passed, 1 skipped (24 run)
  • Reviewed _parse_float_bits diff: normal truncation (frac >> (top - mant_bits)), overflow (e_biased >= exp_max -> signed Inf), and subnormal/underflow shift (s = exp + (mant_bits + bias - 1)) all look clean and consistent.

AI-assisted.

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested 498d6e6 on Windows 11 (26200.9974), Python 3.12.0: pass.

  • python -I tests/test_spirv_asm.py: 24/24 OK. python -I -m unittest discover -s tests -p 'test_spirv_asm*.py' -t tests: 34/34 OK.
  • Negative control (merge-base 86a7807dd, old tools/spirv_asm.py, the head's test file): 24 tests, 4 failures — exactly the four truncating subtests the PR describes; 0x1.3e7ap-24 already lands on the same word at the old rounding code.
  • Extra boundary probes beyond the five cases, all matching the expected IEEE words: f32 0x1p-149 -> 0x1, 0x1.8p-149 -> 0x1, 0x1.fffffep-127 -> 0x7FFFFF, 0x1p-126 -> 0x800000; f16 0x1p-14 -> 0x400, 0x1.ffffp-15 -> 0x3FF, 0x1.ffcp+15 -> 0x7BFF, 0x1p+16 -> 0x7C00; f64 0x1.00000000000008p+0 -> 0x3FF0000000000000; and -0x1.8p-24 -> 0x8001. Note 0x1.0000000000001p+0 is exactly 1+2^-52 and correctly encodes as 0x3FF0000000000001, not a truncation case.
  • Code read: sign is kept separate, the exponent comes from sig.bit_length(), excess fraction bits are dropped by an integer shift, the subnormal branch's shift keeps mant below 2^mant_bits at the boundary, and the Inf/NaN spellings are unchanged. The e_biased > exp_max path still returns Inf for a finite literal, as before; I did not compare that spelling against spirv-as.
  • compileall, check_no_game_data.py, git diff --check: pass.

Synthetic literals only; no game data, no GPU, no spirv-as on this host (the PR's 600-literal comparison is the cross-check for that).

AI-assisted.

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.

Python SPIR-V assembler rounds hex-float constants to nearest; spirv-as truncates toward zero

5 participants