"""Gen.fconst gives an OpConstant %float with the exact f32 bits, one per value, not an OpBitcast at every use. Until 2026-11-27 fconst bitcast the uint constant of the bits at every use (4.3% of the instructions of a large RaytracingResolve module). An OpConstant %float takes the 32-bit word literally, so the value is the same as long as the literal spells exactly those bits: rdna2spirv.float_literal writes the shortest decimal of a finite value (-0.0 and the denormals too) and the SPIRV-Tools hex-float spelling of an infinity or a NaN, payload as stored. Checked here through tools/spirv_asm.py, which reads float literals as spirv-as does, and the mini interpreter. """ import re import struct import sys import unittest import support from spirv_mini_interp import f32_bits, f32_literal, run_fragment import rdna2spirv sys.path.insert(0, support.TOOLS_DIR) import spirv_asm # noqa: E402 # +-0, the smallest and largest denormals, the smallest normal, 1 and its neighbour, the largest finite values, # +-infinity, quiet and signalling NaNs with payloads and both signs, and a few ordinary values. TRICKY = (0x00000000, 0x80000000, 0x00000001, 0x80000001, 0x007FFFFF, 0x807FFFFF, 0x00800000, 0x3F800000, 0x3F800001, 0x7F7FFFFF, 0xFF7FFFFF, 0x7F800000, 0xFF800000, 0x7FC00000, 0xFFC00000, 0x7F800001, 0xFF800001, 0x7FBFFFFF, 0xFFFFFFFF, 0x7FC00001, 0x40C90FDB, 0x1F800000, 0x5F800000, 0xBF000000) def bits_of(x): return struct.unpack(" -32767; three turns -> +0. self.assertEqual(out[0], 0) self.assertEqual(out[1] & 0xFFFF, (-32767) & 0xFFFF) self.assertEqual(out[2], 0) self.assertEqual(out[3], bits_of(0.5)) if __name__ == "__main__": unittest.main()