mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 17:08:38 +00:00
spirv_asm: a decimal f32 constant is rounded once, and an f16 one is that f32 cut toward zero, as spirv-as reads them
This commit is contained in:
@ -740,6 +740,23 @@ class FloatLiteralTests(unittest.TestCase):
|
||||
("f64", "0x1.fffffffffffff8p1023", [0xFFFFFFFF, 0x7FEFFFFF]),
|
||||
))
|
||||
|
||||
def test_decimal_is_rounded_once_to_f32_and_f16_narrows_that_toward_zero(self):
|
||||
# old #3911 (@lumenpearson): each of these decimals reads as a double on an f32 tie, which rounding the double
|
||||
# again broke the wrong way; strtof rounds the decimal once. A 16-bit decimal is that f32 cut toward zero.
|
||||
self._check((
|
||||
("f32", "1.00000005960464477539062500000001", [0x3F800001]),
|
||||
("f32", "-1.00000005960464477539062500000001", [0xBF800001]),
|
||||
("f32", "1.000000059604644775390625", [0x3F800000]),
|
||||
("f32", "1.00000017881393432617187499999999", [0x3F800001]),
|
||||
("f32", "1.000000178813934326171875", [0x3F800002]),
|
||||
("f32", "7.00649232162408535461864791644958065640130970938257885878534141944895541342930300743319094181060791"
|
||||
"015625000001e-46", [0x00000001]),
|
||||
("f32", "0.1", [0x3DCCCCCD]), ("f32", "3.4028235e38", [0x7F7FFFFF]), ("f32", "-0.0", [0x80000000]),
|
||||
("f16", "1.0006", [0x3C00]), ("f16", "-1.0006", [0xBC00]), ("f16", "65535", [0x7BFF]),
|
||||
("f16", "5e-8", [0x0000]), ("f16", "0.1", [0x2E66]), ("f16", "1.5", [0x3E00]),
|
||||
("f64", "0.1", [0x9999999A, 0x3FB99999]),
|
||||
))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -18,10 +18,12 @@ Usage: `python -I tools/spirv_asm.py --help` lists the options.
|
||||
"""
|
||||
import argparse
|
||||
import array
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
import struct
|
||||
import sys
|
||||
from fractions import Fraction
|
||||
|
||||
_HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
if _HERE not in sys.path:
|
||||
@ -324,12 +326,29 @@ def _parse_float_bits(tok: str, width: int, lineno: int) -> int:
|
||||
fv = float(tok)
|
||||
except ValueError as exc:
|
||||
raise SpirvAsmError(f"line {lineno}: invalid float literal {tok!r}") from exc
|
||||
pack_fmt, unpack_fmt = {16: ("<e", "<H"), 32: ("<f", "<I"), 64: ("<d", "<Q")}[width]
|
||||
try:
|
||||
if width == 64 or not math.isfinite(fv):
|
||||
pack_fmt, unpack_fmt = {16: ("<e", "<H"), 32: ("<f", "<I"), 64: ("<d", "<Q")}[width]
|
||||
return struct.unpack(unpack_fmt, struct.pack(pack_fmt, fv))[0]
|
||||
except OverflowError:
|
||||
sign = 1 if tok.startswith("-") else 0
|
||||
return (sign << (exp_bits + mant_bits)) | (exp_max << mant_bits)
|
||||
# float() rounds the decimal once to a double, as strtod does. A narrower type is rounded from that double,
|
||||
# except where the double sits on a tie of the narrower type: the exact decimal breaks it, so the result is the
|
||||
# decimal rounded once to the nearest-even f32, as strtof (and so spirv-as) gives it.
|
||||
negative = math.copysign(1.0, fv) < 0
|
||||
sig, den = abs(fv).as_integer_ratio()
|
||||
bits = _round_float(negative, sig, 1 - den.bit_length(), 32, True, lambda: _compare_decimal(tok, fv))
|
||||
if bits is not None and width == 16:
|
||||
# SPIRV-Tools reads a 16-bit decimal as that f32 and narrows it toward zero (ParseNormalFloat<Float16>),
|
||||
# so 1.0006 is 0x3C00, not the nearer 0x3C01.
|
||||
sig, den = abs(struct.unpack("<f", struct.pack("<I", bits))[0]).as_integer_ratio()
|
||||
bits = _round_float(negative, sig, 1 - den.bit_length(), 16, False)
|
||||
if bits is None:
|
||||
return (int(negative) << (exp_bits + mant_bits)) | (exp_max << mant_bits)
|
||||
return bits
|
||||
|
||||
|
||||
def _compare_decimal(tok: str, approx: float) -> int:
|
||||
"""1 when the decimal literal `tok` is larger in magnitude than the double `approx` read from it, -1 when smaller."""
|
||||
exact, near = abs(Fraction(tok)), Fraction(abs(approx))
|
||||
return (exact > near) - (exact < near)
|
||||
|
||||
|
||||
def _encode_context_number(tok: str, type_info, lineno: int) -> list[int]:
|
||||
|
||||
Reference in New Issue
Block a user