okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-5 fccdb48a3 (image noise switch, step 1 only) into f-own-int150c2394f

main
2.9 KiB90 linesraw
1"""tools/image/gate/noise/make_sin_exceptions.py - TEST-ONLY: strata_sin_exc.bin from check_domain's exc_sin.txt.
2
3glibc's double sin (x86-64, the library sd.cpp's Linux x86 build calls) is not correctly rounded for some inputs.
4check_domain.cpp (mode glibc) lists every reachable v where it differs from our correctly rounded value.  For each,
5the correctly rounded reference is recomputed here with Python's decimal module (50 digits) and the difference is
6checked to be exactly one unit in the last place; then the list is packed:
7
8  strata_sin_exc.bin = b"OWN5SEX1", uint32 count (little endian), then per entry (sorted by v's float bits) one varint
9                (LEB128) of ((bits - previous bits) << 1 | down), down = 1 when glibc's double is one unit below ours
10                in the bit pattern (previous bits start at 0)
11
12    python -I -B make_sin_exceptions.py exc_sin.txt         (writes tools/image/sdcpp_noise/strata_sin_exc.bin)"""
13import struct
14import sys
15from decimal import Decimal as D, getcontext
16from pathlib import Path
17
18getcontext().prec = 50
19MAGIC = b"OWN5SEX1"
20
21
22def cr_sin(v: float) -> float:
23    x = D(v)
24    s, t, k = D(0), x, 1
25    while abs(t) > D(10) ** -48:
26        s += t
27        t = -t * x * x / ((k + 1) * (k + 2))
28        k += 2
29    return float(s)                      # Decimal -> float is correctly rounded
30
31
32def dbits(d: float) -> int:
33    return struct.unpack("<q", struct.pack("<d", d))[0]
34
35
36def fval(bits: int) -> float:
37    return struct.unpack("<f", struct.pack("<I", bits))[0]
38
39
40def pack(entries) -> bytes:
41    out, prev = bytearray(MAGIC + struct.pack("<I", len(entries))), 0
42    for bits, down in entries:
43        v = ((bits - prev) << 1) | down
44        prev = bits
45        while True:
46            b = v & 0x7F
47            v >>= 7
48            out.append(b | (0x80 if v else 0))
49            if not v:
50                break
51    return bytes(out)
52
53
54def unpack(data: bytes):
55    assert data[:8] == MAGIC
56    n = struct.unpack("<I", data[8:12])[0]
57    i, prev, out = 12, 0, []
58    for _ in range(n):
59        v = shift = 0
60        while True:
61            b = data[i]
62            i += 1
63            v |= (b & 0x7F) << shift
64            shift += 7
65            if not b & 0x80:
66                break
67        prev += v >> 1
68        out.append((prev, v & 1))
69    assert i == len(data)
70    return out
71
72
73def main():
74    entries = []
75    for line in open(sys.argv[1]):
76        vb, gb = line.split()
77        g = struct.unpack("<q", struct.pack("<Q", int(gb, 16)))[0]
78        delta = g - dbits(cr_sin(fval(int(vb, 16))))
79        if delta not in (1, -1):
80            raise SystemExit(f"v bits {vb}: glibc differs by {delta} units")
81        entries.append((int(vb, 16), 1 if delta < 0 else 0))
82    entries.sort()
83    data = pack(entries)
84    assert unpack(data) == entries
85    (Path(__file__).resolve().parents[2] / "sdcpp_noise" / "strata_sin_exc.bin").write_bytes(data)
86    print(len(entries), "entries,", len(data), "bytes")
87
88
89if __name__ == "__main__":
90    main()