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
1.6 KiB41 linesraw
1"""tools/image/gate/noise/make_tables.py - TEST-ONLY: write strata_noise_tables.inc (in tools/image/sdcpp_noise), the Q8.120 constants of
2strata_noise.hpp (ln c_j for the log table, sin and cos of i/64 for the sin table), with Python's decimal module at 70
3digits, each rounded to the nearest unit of 2^-120.   python -I -B make_tables.py [output file]"""
4import sys
5from decimal import Decimal as D, getcontext
6from pathlib import Path
7
8getcontext().prec = 70
9S = D(2) ** 120
10
11
12def fx(v):
13    q = int((v * S).to_integral_value())
14    return "{0x%016xull, 0x%016xull}" % (q >> 64, q & ((1 << 64) - 1))
15
16
17def sincos(x):
18    s, c, t, k = D(0), D(0), D(1), 0
19    while True:
20        if k % 2 == 0:
21            c += t if (k // 2) % 2 == 0 else -t
22        else:
23            s += t if (k // 2) % 2 == 0 else -t
24        k += 1
25        t = t * x / k
26        if abs(t) < D(10) ** -60:
27            break
28    return s, c
29
30
31out = ["// generated by tools/image/gate/noise/make_tables.py (Python decimal, 70 digits): Q8.120, rounded to nearest",
32       "static const U128 LNC[64] = {  // ln(c_j), c_j = (2^23 + j*2^17 + 2^16) / 2^23"]
33for j in range(64):
34    out.append("    " + fx((D(2 ** 23 + j * 2 ** 17 + 2 ** 16) / D(2 ** 23)).ln()) + ",")
35out += ["};", "static const U128 SINA[52] = {  // sin(i/64)"]
36out += ["    " + fx(sincos(D(i) / 64)[0]) + "," for i in range(52)]
37out += ["};", "static const U128 COSA[52] = {  // cos(i/64)"]
38out += ["    " + fx(sincos(D(i) / 64)[1]) + "," for i in range(52)]
39out.append("};")
40OUT = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).resolve().parents[2] / "sdcpp_noise" / "strata_noise_tables.inc"
41OUT.write_bytes(("\n".join(out) + "\n").encode())