okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
150c2394f
main
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 8strata_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 10in the bit pattern (previous bits start at 0) 11 12python -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 : 23x = D (v ) 24s ,t ,k = D (0 ),x ,1 25while abs (t )> D (10 )** - 48 : 26s += t 27t = - t * x * x / ((k + 1 )* (k + 2 )) 28k += 2 29return float (s )# Decimal -> float is correctly rounded 30 31 32def dbits (d :float )-> int : 33return struct .unpack ("<q" ,struct .pack ("<d" ,d ))[0 ] 34 35 36def fval (bits :int )-> float : 37return struct .unpack ("<f" ,struct .pack ("<I" ,bits ))[0 ] 38 39 40def pack (entries )-> bytes : 41out ,prev = bytearray (MAGIC + struct .pack ("<I" ,len (entries ))),0 42for bits ,down in entries : 43v = ((bits - prev )<< 1 )| down 44prev = bits 45while True : 46b = v & 0x7F 47v >>= 7 48out .append (b | (0x80 if v else 0 )) 49if not v : 50break 51return bytes (out ) 52 53 54def unpack (data :bytes ): 55assert data [:8 ]== MAGIC 56n = struct .unpack ("<I" ,data [8 :12 ])[0 ] 57i ,prev ,out = 12 ,0 , [] 58for _ in range (n ): 59v = shift = 0 60while True : 61b = data [i ] 62i += 1 63v |= (b & 0x7F )<< shift 64shift += 7 65if not b & 0x80 : 66break 67prev += v >> 1 68out .append ((prev ,v & 1 )) 69assert i == len (data ) 70return out 71 72 73def main (): 74entries = [] 75for line in open (sys .argv [1 ]): 76vb ,gb = line .split () 77g = struct .unpack ("<q" ,struct .pack ("<Q" ,int (gb ,16 )))[0 ] 78delta = g - dbits (cr_sin (fval (int (vb ,16 )))) 79if delta not in (1 ,- 1 ): 80raise SystemExit (f"v bits { vb } : glibc differs by { delta } units" ) 81entries .append ((int (vb ,16 ),1 if delta < 0 else 0 )) 82entries .sort () 83data = pack (entries ) 84assert unpack (data )== entries 85 (Path (__file__ ).resolve ().parents [2 ]/ "sdcpp_noise" / "strata_sin_exc.bin" ).write_bytes (data ) 86len (entries ),"entries," ,len (data ),"bytes" ) 87 88 89if __name__ == "__main__" : 90main ()