"""tools/image/gate/noise/make_tables.py - TEST-ONLY: write strata_noise_tables.inc (in tools/image/sdcpp_noise), the Q8.120 constants of strata_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 digits, each rounded to the nearest unit of 2^-120. python -I -B make_tables.py [output file]""" import sys from decimal import Decimal as D, getcontext from pathlib import Path getcontext().prec = 70 S = D(2) ** 120 def fx(v): q = int((v * S).to_integral_value()) return "{0x%016xull, 0x%016xull}" % (q >> 64, q & ((1 << 64) - 1)) def sincos(x): s, c, t, k = D(0), D(0), D(1), 0 while True: if k % 2 == 0: c += t if (k // 2) % 2 == 0 else -t else: s += t if (k // 2) % 2 == 0 else -t k += 1 t = t * x / k if abs(t) < D(10) ** -60: break return s, c out = ["// generated by tools/image/gate/noise/make_tables.py (Python decimal, 70 digits): Q8.120, rounded to nearest", "static const U128 LNC[64] = { // ln(c_j), c_j = (2^23 + j*2^17 + 2^16) / 2^23"] for j in range(64): out.append(" " + fx((D(2 ** 23 + j * 2 ** 17 + 2 ** 16) / D(2 ** 23)).ln()) + ",") out += ["};", "static const U128 SINA[52] = { // sin(i/64)"] out += [" " + fx(sincos(D(i) / 64)[0]) + "," for i in range(52)] out += ["};", "static const U128 COSA[52] = { // cos(i/64)"] out += [" " + fx(sincos(D(i) / 64)[1]) + "," for i in range(52)] out.append("};") OUT = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).resolve().parents[2] / "sdcpp_noise" / "strata_noise_tables.inc" OUT.write_bytes(("\n".join(out) + "\n").encode())