Skip to content

spirv_wave_interp: a 64-bit integer converts to float with one rounding - #7735

Open
fearkov wants to merge 2 commits into
ItsNotRexed:mainfrom
fearkov:fix/wave-interp-cvt64
Open

fearkov wants to merge 2 commits into
ItsNotRexed:mainfrom
fearkov:fix/wave-interp-cvt64

Conversation

@fearkov

@fearkov fearkov commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in #7496 as: #7496 (comment)

What and why

tests/semantic/spirv_wave_interp.py converted an integer to float as _f32_pack(float(v)). float(v) rounds a 64-bit value to binary64 first, and the binary32 rounding after that can then go the wrong way:

OpConvertUToF %float of main 042c5f3 RTX 2060 SUPER, driver 620.70.06 why main is off
2^62 + 2^38 + 1 0x5E800000 0x5E800001 binary64 gives the midpoint 2^62 + 2^38, which ties down
2^62 + 3·2^38 − 1 0x5E800002 0x5E800001 binary64 gives the midpoint 2^62 + 3·2^38, which ties up
2^63 + 2^39 + 1 0x5F000000 0x5F000001 as in the first row

OpConvertSToF of the negated values gives the same words with the sign bit set. Vulkan requires conversions to be correctly rounded, and the interpreter's own arithmetic rounds to nearest even. Main rounds the first two values in opposite directions, so no single rounding mode gives its words.

The fix rounds the integer to 24 significant bits (to nearest, ties to even) with integer arithmetic. float() is exact after that. Every 32-bit value is exact in binary64, so 32-bit conversions give the same words as before. spirv_mini_interp.py refuses 64-bit conversion operands, and tools/rdna2spirv.py only converts 32-bit integers, so no translator test relied on the double rounding.

Commits:

  1. spirv_wave_interp: a new _int_to_f32 rounds once, and OpConvertUToF / OpConvertSToF use it.
  2. Tests: WideIntegers.test_64bit_integer_to_float_rounds_once in test_spirv_wave_interp.py covers eight 64-bit conversions and checks the words the GPU gives. Three values per op round the wrong way through binary64; 2^64 − 1, −2^63 and 2^53 + 1 are controls.

Tested

Linux, Python 3.14.10 (standard library only), RTX 2060 SUPER with driver 620.70.06, on main 042c5f3:

  • I put the same eight conversions in fragment shaders, ran them through spirv-as, spirv-val --target-env vulkan1.2 and vk_harness, and the GPU gave the expected words. Those words are the ones the test checks.
  • python3 -I tests/semantic/test_spirv_wave_interp.py: 20 tests OK. With main's spirv_wave_interp.py put back, both subtests of the new test fail (0x5E800000, 0x5E800002, 0x5F000000, 0x5F800000 against 0x5E800001, 0x5E800001, 0x5F000001, 0x5F800000, and the same for the signed ones).
  • I checked _int_to_f32 against an exact reference over 57,618 values of every width up to 64 bits and their negations, with no difference. On 40,000 32-bit values it gives the same words as float().
  • All 28 tests/semantic files that use the wave interpreter pass, and so do tests/test_cf_diff.py, tests/test_wint_diff.py and tests/test_semantic_translator_use.py.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.

OpConvertUToF and OpConvertSToF went through float(v), which rounds a 64-bit value to binary64 before binary32: 2**62 + 2**38 + 1 gave 0x5E800000 and 2**62 + 3 * 2**38 - 1 gave 0x5E800002 instead of 0x5E800001. The integer is now rounded to 24 significant bits (to nearest, ties to even) first; 32-bit values give the same words as before.
Values that round the wrong way through binary64, with the words an RTX 2060 gives for them.
@CourtyardGames

Copy link
Copy Markdown

Independent Windows CPU check at 560d89d: the new integer-to-binary32 helper matched the Windows UCRT on all 50,950 distinct inputs. The previous conversion disagreed on 84 of the same inputs.

For the reference I passed each integer as an exact decimal string to ucrtbase!strtof and compared binary32 bit patterns. The inputs included signed and unsigned 64-bit limits, random values (Python seed 7735), and exact midpoints plus their immediate integer neighbors at exponents 24 through 63. I included adjacent significands of both parities and cases near the next power of two.

This checks the conversion helper directly. Windows / CPython 3.14, CPU only, synthetic inputs. AI-assisted.

@morodirule

Copy link
Copy Markdown

Tested 560d89d on Arch Linux, Python 3.14.10, CPU only: pass — first Linux check. test_spirv_wave_interp 20/20 (28 subtests) + related test_interp 52/52 (116 subtests) OK. Negative control: base file → exactly the 2 new 64-bit-rounding tests fail; restored, green again. No game files, no GPU. AI-assisted.

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Tested 560d89d6 on Linux, CPython 3.14, CPU only, merged onto main 867d922f: pass

  • python -m unittest discover -s tests/semantic -p test_spirv_wave_interp.py: Ran 20 tests, OK. With only the interpreter file(s) reverted to main: FAILED (failures=2), so the new tests exercise the change.

Beyond the PR's own tests, the agent ran a differential harness against an exact reference (Python integers and Fraction with independent rounding): 34 cases of 64-bit integer to float conversion against a single correct rounding, including the three inputs where double rounding gives a different word, 0 disagreements. That harness run is the agent's; I re-ran only the test commands above.

No conflict with #8449 or #8149 by its trial merges.

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting.

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.

4 participants