spirv_asm: float literals and DebugPrintf as spirv-as reads them (OLD-3911, OLD-4237, OLD-4286, OLD-4322, OLD-4330) #78

Manually merged
github merged 5 commits from gh-pr/78/head into gh-pr/78/base 2026-11-26 08:43:24 +00:00
Owner

@⁠ItsMrDubey on GitHub · 2026-11-24 02:13 UTC · original
ItsMrDubey:spirv-asm-float-fixes → main · +208 −37 · 3 files · 5 commits · merged 2026-11-24 03:19 UTC

Claimed in the coordination issue as: OLD-3911, OLD-4237, OLD-4286, OLD-4322, OLD-4330 (#14)

What and why

Five float-literal and extended-instruction differences between tools/spirv_asm.py and spirv-as. There is one commit per issue, and each comes with a regression test in tests/test_spirv_asm.py. I reproduced all five on origin/main before fixing them. No open PR or old-pr branch addressed them (the only spirv_asm branch in #17, old-pr/10025, is about output aliasing), so nothing was cherry-picked.

spirv-as is not installed here (where spirv-as finds nothing), so the reference behaviour comes from reading SPIRV-Tools' source/util/parse_number.cpp (ParseAndEncodeFloatingPointNumber) and source/util/hex_float.h (operator>> for HexFloat, ParseNormalFloat and its Float16 and BFloat16 specialisations, and castTo). The tests assert the values that code produces.

  • old #4330 (@⁠mrbean56): hex-floats are truncated, not rounded. I checked the claim against HexFloat's operator>> and it is correct. That parser drops the significand bits that don't fit, both normal and subnormal, so the result is truncated toward zero. A value too large for the type becomes infinity, and the exponent-all-ones spelling still carries a NaN payload. spirv_asm used to round to nearest through float.fromhex and struct.pack. For example, f32 0x1.0000018p0 gave 0x3F800001 and now gives 0x3F800000. f32 0x1.fffffffp127 used to round up to +Inf and now gives 0x7F7FFFFF.
  • old #3911 (@⁠lumenpearson): decimal f32 and f16 bits. An f32 decimal used to be rounded twice, first to a double and then to f32. It is now rounded once, to nearest-even, as strtof does. Where the double lands exactly on an f32 tie, the exact decimal decides the direction. For example, 1.00000005960464477539062500000001 now gives 0x3F800001; it used to give 0x3F800000.
    Note that f16 is not round-to-nearest in spirv-as, even though the issue says it is. ParseNormalFloat<Float16> reads the decimal as an f32 and then narrows it with castTo(..., round_direction::kToZero). spirv_asm now does the same thing, so 1.0006 gives 0x3C00, not the nearer 0x3C01, and 65535 gives 0x7BFF instead of +Inf.
  • old #4286: a non-finite decimal is now refused. spirv-as fails a decimal that overflows its type: the stream sets failbit, and the f16 and bf16 overflow checks do the same. spirv_asm used to emit infinity instead, for example f64 1e400, f32 1e39 and f16 65536. It also took Python-only spellings such as inf, nan, infinity and 1_0. All of these are now refused, and the error names the line. Underflow still rounds to zero, as strtod does. A hex-float can still spell infinity, as it can in spirv-as.
  • old #4237: BF16 constants. spirv_asm ignored OpTypeFloat 16 BFloat16KHR and encoded its constants as IEEE halves, so 1.5 gave 0x3E00. They now use the bfloat16 layout (8 exponent bits, 7 fraction bits), so 1.5 gives 0x3FC0. As SPIRV-Tools does, a decimal is read as an f32 and truncated, and a hex-float is truncated.
  • old #4322: DebugPrintf by name. OpExtInst %void %set DebugPrintf ..., the spelling spirv-dis prints for NonSemantic.DebugPrintf, used to fail with "invalid integer literal 'DebugPrintf'". The set now has a one-entry table (NONSEMANTIC_DEBUGPRINTF, from extinst.nonsemantic.debugprintf.grammar.json), and the numeric form 1 still works.

Tested

Windows 11, Python 3.11.9, no GPU needed. spirv-as and spirv-val are not installed, so the tests that compare against spirv-as, and the _validate call, skip.

  • python -I tests/test_spirv_asm.py: 36 tests OK (1 skipped: the comparison against spirv-as). With tools/ reverted to origin/main, the new tests fail: 10 of the hex-float cases, 8 decimal cases, all 12 overflow and non-finite cases, 10 BF16 cases plus its overflow check, and the DebugPrintf test with an error.
  • python -I tests/test_spirv_asm_tables.py: OK
  • python -I tests/test_desc_cache.py, and python -m unittest discover -s tests/semantic -p test_<name>.py for each semantic test that imports spirv_asm (alu_div_fmas, alu_ldexp_f64, global_sub_dword, lds_atomics_cas, scmp_u64_mask_pairs, wave64_emulate_opt, wave64_mov_b32_cross_half): all OK
  • python -I scripts/run_semantic_tests.py tests/semantic: 463 files, 6075 tests; 4 files fail (test_exec_computed 2, test_image_atomics_guard 47, test_sopc_validation 1, test_unary_f16_validation 15 failures), and with tools/ reverted to origin/main the same 4 files fail with the same counts, so the failures were already on main
  • python -m compileall -q make_game.py tools tests, and python -I tests/test_text_write_line_endings.py: OK
  • python -I scripts/check_no_game_data.py tools/spirv_asm.py tools/spirv_asm_tables.py tests/test_spirv_asm.py: clean

AI-assisted: yes

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • No new translator rule, so nothing to add to the contract table of docs/design.md (assembler only).
**@⁠ItsMrDubey** on GitHub · 2026-11-24 02:13 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/78) `ItsMrDubey:spirv-asm-float-fixes` → `main` · +208 −37 · 3 files · 5 commits · merged 2026-11-24 03:19 UTC Claimed in the coordination issue as: OLD-3911, OLD-4237, OLD-4286, OLD-4322, OLD-4330 (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why Five float-literal and extended-instruction differences between `tools/spirv_asm.py` and spirv-as. There is one commit per issue, and each comes with a regression test in `tests/test_spirv_asm.py`. I reproduced all five on origin/main before fixing them. No open PR or old-pr branch addressed them (the only spirv_asm branch in #17, `old-pr/10025`, is about output aliasing), so nothing was cherry-picked. spirv-as is not installed here (`where spirv-as` finds nothing), so the reference behaviour comes from reading SPIRV-Tools' `source/util/parse_number.cpp` (`ParseAndEncodeFloatingPointNumber`) and `source/util/hex_float.h` (`operator>>` for `HexFloat`, `ParseNormalFloat` and its `Float16` and `BFloat16` specialisations, and `castTo`). The tests assert the values that code produces. - **old #4330 (@⁠mrbean56): hex-floats are truncated, not rounded.** I checked the claim against `HexFloat`'s `operator>>` and it is correct. That parser drops the significand bits that don't fit, both normal and subnormal, so the result is truncated toward zero. A value too large for the type becomes infinity, and the exponent-all-ones spelling still carries a NaN payload. spirv_asm used to round to nearest through `float.fromhex` and `struct.pack`. For example, f32 `0x1.0000018p0` gave 0x3F800001 and now gives 0x3F800000. f32 `0x1.fffffffp127` used to round up to +Inf and now gives 0x7F7FFFFF. - **old #3911 (@⁠lumenpearson): decimal f32 and f16 bits.** An f32 decimal used to be rounded twice, first to a double and then to f32. It is now rounded once, to nearest-even, as strtof does. Where the double lands exactly on an f32 tie, the exact decimal decides the direction. For example, `1.00000005960464477539062500000001` now gives 0x3F800001; it used to give 0x3F800000. Note that **f16 is not round-to-nearest in spirv-as**, even though the issue says it is. `ParseNormalFloat<Float16>` reads the decimal as an f32 and then narrows it with `castTo(..., round_direction::kToZero)`. spirv_asm now does the same thing, so `1.0006` gives 0x3C00, not the nearer 0x3C01, and `65535` gives 0x7BFF instead of +Inf. - **old #4286: a non-finite decimal is now refused.** spirv-as fails a decimal that overflows its type: the stream sets failbit, and the f16 and bf16 overflow checks do the same. spirv_asm used to emit infinity instead, for example f64 `1e400`, f32 `1e39` and f16 `65536`. It also took Python-only spellings such as `inf`, `nan`, `infinity` and `1_0`. All of these are now refused, and the error names the line. Underflow still rounds to zero, as strtod does. A hex-float can still spell infinity, as it can in spirv-as. - **old #4237: BF16 constants.** spirv_asm ignored `OpTypeFloat 16 BFloat16KHR` and encoded its constants as IEEE halves, so `1.5` gave 0x3E00. They now use the bfloat16 layout (8 exponent bits, 7 fraction bits), so `1.5` gives 0x3FC0. As SPIRV-Tools does, a decimal is read as an f32 and truncated, and a hex-float is truncated. - **old #4322: DebugPrintf by name.** `OpExtInst %void %set DebugPrintf ...`, the spelling spirv-dis prints for `NonSemantic.DebugPrintf`, used to fail with "invalid integer literal 'DebugPrintf'". The set now has a one-entry table (`NONSEMANTIC_DEBUGPRINTF`, from `extinst.nonsemantic.debugprintf.grammar.json`), and the numeric form `1` still works. ### Tested Windows 11, Python 3.11.9, no GPU needed. spirv-as and spirv-val are not installed, so the tests that compare against spirv-as, and the `_validate` call, skip. - `python -I tests/test_spirv_asm.py`: 36 tests OK (1 skipped: the comparison against spirv-as). With `tools/` reverted to origin/main, the new tests fail: 10 of the hex-float cases, 8 decimal cases, all 12 overflow and non-finite cases, 10 BF16 cases plus its overflow check, and the DebugPrintf test with an error. - `python -I tests/test_spirv_asm_tables.py`: OK - `python -I tests/test_desc_cache.py`, and `python -m unittest discover -s tests/semantic -p test_<name>.py` for each semantic test that imports spirv_asm (alu_div_fmas, alu_ldexp_f64, global_sub_dword, lds_atomics_cas, scmp_u64_mask_pairs, wave64_emulate_opt, wave64_mov_b32_cross_half): all OK - `python -I scripts/run_semantic_tests.py tests/semantic`: 463 files, 6075 tests; 4 files fail (test_exec_computed 2, test_image_atomics_guard 47, test_sopc_validation 1, test_unary_f16_validation 15 failures), and with `tools/` reverted to origin/main the same 4 files fail with the same counts, so the failures were already on main - `python -m compileall -q make_game.py tools tests`, and `python -I tests/test_text_write_line_endings.py`: OK - `python -I scripts/check_no_game_data.py tools/spirv_asm.py tools/spirv_asm_tables.py tests/test_spirv_asm.py`: clean AI-assisted: yes ### Checklist - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] No new translator rule, so nothing to add to the contract table of `docs/design.md` (assembler only).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:43:23 +00:00
github manually merged commit c389dbc95e into gh-pr/78/base 2026-11-26 08:43:24 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.