"""tools/image/gate/noise/make_sin_exceptions.py - TEST-ONLY: strata_sin_exc.bin from check_domain's exc_sin.txt. glibc's double sin (x86-64, the library sd.cpp's Linux x86 build calls) is not correctly rounded for some inputs. check_domain.cpp (mode glibc) lists every reachable v where it differs from our correctly rounded value. For each, the correctly rounded reference is recomputed here with Python's decimal module (50 digits) and the difference is checked to be exactly one unit in the last place; then the list is packed: strata_sin_exc.bin = b"OWN5SEX1", uint32 count (little endian), then per entry (sorted by v's float bits) one varint (LEB128) of ((bits - previous bits) << 1 | down), down = 1 when glibc's double is one unit below ours in the bit pattern (previous bits start at 0) python -I -B make_sin_exceptions.py exc_sin.txt (writes tools/image/sdcpp_noise/strata_sin_exc.bin)""" import struct import sys from decimal import Decimal as D, getcontext from pathlib import Path getcontext().prec = 50 MAGIC = b"OWN5SEX1" def cr_sin(v: float) -> float: x = D(v) s, t, k = D(0), x, 1 while abs(t) > D(10) ** -48: s += t t = -t * x * x / ((k + 1) * (k + 2)) k += 2 return float(s) # Decimal -> float is correctly rounded def dbits(d: float) -> int: return struct.unpack(" float: return struct.unpack(" bytes: out, prev = bytearray(MAGIC + struct.pack(">= 7 out.append(b | (0x80 if v else 0)) if not v: break return bytes(out) def unpack(data: bytes): assert data[:8] == MAGIC n = struct.unpack("> 1 out.append((prev, v & 1)) assert i == len(data) return out def main(): entries = [] for line in open(sys.argv[1]): vb, gb = line.split() g = struct.unpack("