diff options
| author | Yash Dubey <yash.dubey803at@gmail.com> | 2026-11-24 07:37:00 +0530 |
|---|---|---|
| committer | Yash Dubey <yash.dubey803at@gmail.com> | 2026-11-24 08:26:53 +0530 |
| commit | 4e8aa910feb63fbf2d9beb621bbd7bae2fae51b2 (patch) | |
| tree | e927ba522649a55c1c2108147eba472c670b9d3c /tools | |
| parent | ba49ba26a1013c5977c11037457a05bccbe203f0 (diff) | |
spirv_asm: a decimal f32 constant is rounded once, and an f16 one is that f32 cut toward zero, as spirv-as reads them
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/spirv_asm.py | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/tools/spirv_asm.py b/tools/spirv_asm.py index 46ca360fc..693ffc959 100644 --- a/tools/spirv_asm.py +++ b/tools/spirv_asm.py | |||
| @@ -18,10 +18,12 @@ Usage: `python -I tools/spirv_asm.py --help` lists the options. | |||
| 18 | """ | 18 | """ |
| 19 | import argparse | 19 | import argparse |
| 20 | import array | 20 | import array |
| 21 | import math | ||
| 21 | import os | 22 | import os |
| 22 | import re | 23 | import re |
| 23 | import struct | 24 | import struct |
| 24 | import sys | 25 | import sys |
| 26 | from fractions import Fraction | ||
| 25 | 27 | ||
| 26 | _HERE = os.path.dirname(os.path.abspath(__file__)) | 28 | _HERE = os.path.dirname(os.path.abspath(__file__)) |
| 27 | if _HERE not in sys.path: | 29 | if _HERE not in sys.path: |
| @@ -324,12 +326,29 @@ def _parse_float_bits(tok: str, width: int, lineno: int) -> int: | |||
| 324 | fv = float(tok) | 326 | fv = float(tok) |
| 325 | except ValueError as exc: | 327 | except ValueError as exc: |
| 326 | raise SpirvAsmError(f"line {lineno}: invalid float literal {tok!r}") from exc | 328 | raise SpirvAsmError(f"line {lineno}: invalid float literal {tok!r}") from exc |
| 327 | pack_fmt, unpack_fmt = {16: ("<e", "<H"), 32: ("<f", "<I"), 64: ("<d", "<Q")}[width] | 329 | if width == 64 or not math.isfinite(fv): |
| 328 | try: | 330 | pack_fmt, unpack_fmt = {16: ("<e", "<H"), 32: ("<f", "<I"), 64: ("<d", "<Q")}[width] |
| 329 | return struct.unpack(unpack_fmt, struct.pack(pack_fmt, fv))[0] | 331 | return struct.unpack(unpack_fmt, struct.pack(pack_fmt, fv))[0] |
| 330 | except OverflowError: | 332 | # float() rounds the decimal once to a double, as strtod does. A narrower type is rounded from that double, |
| 331 | sign = 1 if tok.startswith("-") else 0 | 333 | # except where the double sits on a tie of the narrower type: the exact decimal breaks it, so the result is the |
| 332 | return (sign << (exp_bits + mant_bits)) | (exp_max << mant_bits) | 334 | # decimal rounded once to the nearest-even f32, as strtof (and so spirv-as) gives it. |
| 335 | negative = math.copysign(1.0, fv) < 0 | ||
| 336 | sig, den = abs(fv).as_integer_ratio() | ||
| 337 | bits = _round_float(negative, sig, 1 - den.bit_length(), 32, True, lambda: _compare_decimal(tok, fv)) | ||
| 338 | if bits is not None and width == 16: | ||
| 339 | # SPIRV-Tools reads a 16-bit decimal as that f32 and narrows it toward zero (ParseNormalFloat<Float16>), | ||
| 340 | # so 1.0006 is 0x3C00, not the nearer 0x3C01. | ||
| 341 | sig, den = abs(struct.unpack("<f", struct.pack("<I", bits))[0]).as_integer_ratio() | ||
| 342 | bits = _round_float(negative, sig, 1 - den.bit_length(), 16, False) | ||
| 343 | if bits is None: | ||
| 344 | return (int(negative) << (exp_bits + mant_bits)) | (exp_max << mant_bits) | ||
| 345 | return bits | ||
| 346 | |||
| 347 | |||
| 348 | def _compare_decimal(tok: str, approx: float) -> int: | ||
| 349 | """1 when the decimal literal `tok` is larger in magnitude than the double `approx` read from it, -1 when smaller.""" | ||
| 350 | exact, near = abs(Fraction(tok)), Fraction(abs(approx)) | ||
| 351 | return (exact > near) - (exact < near) | ||
| 333 | 352 | ||
| 334 | 353 | ||
| 335 | def _encode_context_number(tok: str, type_info, lineno: int) -> list[int]: | 354 | def _encode_context_number(tok: str, type_info, lineno: int) -> list[int]: |