interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / json / gen / gen_tables.py

87 lines · 3529 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Generate the data-table text literals embedded in om/f64.om and om/uni_rust.om.
3
4Verification/generation tooling only (not in the product path).
5 ryu tables : parsed from ryu-1.0.23/src/d2s_full_table.rs and re-derived with exact
6 integer arithmetic (both must agree, else abort).
7 POW10 : IEEE bits of the 309 serde_json POW10 literals 1e0..1e308 (Python float() is
8 correctly rounded, as are Rust float literals).
9 Unicode : uni.txt written by ugen (tests/json/gen/ugen_main.rs) built with rustc 1.98.1;
10 it queries str::to_lowercase / char::is_whitespace / <str as Debug> black-box.
11Usage: gen_tables.py RYU_TABLE_RS UNI_TXT -> prints the generated om text lines on stdout.
12"""
13import re, struct, sys
14
15def ryu_tables(path):
16 src = open(path).read()
17 def grab(name):
18 body = src.split('pub static %s' % name)[1].split('];')[0]
19 return [(int(a), int(b)) for a, b in re.findall(r'\((\d+),\s*(\d+)\)', body)]
20 inv, pos = grab('DOUBLE_POW5_INV_SPLIT'), grab('DOUBLE_POW5_SPLIT')
21 assert len(inv) == 342 and len(pos) == 326
22 for i, (lo, hi) in enumerate(pos):
23 p = 5 ** i
24 b = p.bit_length()
25 v = p >> (b - 125) if b > 125 else p << (125 - b)
26 assert (hi << 64 | lo) == v, ('pos', i)
27 for i, (lo, hi) in enumerate(inv):
28 p = 5 ** i
29 b = p.bit_length()
30 v = (1 << (b - 1 + 125)) // p + 1
31 assert (hi << 64 | lo) == v, ('inv', i)
32 return inv, pos
33
34def pow10_bits():
35 return [struct.unpack('<Q', struct.pack('<d', float('1e%d' % i)))[0] for i in range(309)]
36
37def uni(path):
38 lower, cls, dbg = [], {}, []
39 for ln in open(path):
40 p = ln.split()
41 if p[0] == 'L':
42 if len(p) == 3:
43 lower.append((int(p[1]), int(p[2])))
44 else:
45 assert p[1:] == ['304', '105', '775'], p # U+0130 -> "i\u0307", coded by hand
46 elif p[0] == 'K':
47 cls[int(p[1])] = p[2]
48 elif p[0] == 'D':
49 dbg.append(int(p[1]))
50 elif p[0] == 'W':
51 pass
52 ranges, i = [], 0
53 while i < len(lower):
54 s, t = lower[i]; d = t - s; best, bs = 1, 1
55 for st in (1, 2):
56 n = 1
57 while i + n < len(lower) and lower[i + n][0] == s + st * n and lower[i + n][1] - lower[i + n][0] == d:
58 n += 1
59 if n > best:
60 best, bs = n, st
61 ranges.append((s, best, d, bs)); i += best
62 def iv(c):
63 out = []
64 for cp in sorted(k for k, v in cls.items() if v == c):
65 if out and out[-1][1] == cp - 1: out[-1][1] = cp
66 else: out.append([cp, cp])
67 return out
68 di = []
69 for cp in dbg:
70 if di and di[-1][1] == cp - 1: di[-1][1] = cp
71 else: di.append([cp, cp])
72 return ranges, iv('C'), iv('I'), di
73
74def main():
75 inv, pos = ryu_tables(sys.argv[1])
76 flat = lambda xs: ' '.join(str(v) for t in xs for v in t)
77 print('text jtpi "%s"' % flat(inv))
78 print('text jtps "%s"' % flat(pos))
79 print('text jtp10 "%s"' % ' '.join(str(b) for b in pow10_bits()))
80 ranges, ci, ii, di = uni(sys.argv[2])
81 print('text jtlr "%s"' % flat(ranges))
82 print('text jtkc "%s"' % flat(ci))
83 print('text jtki "%s"' % flat(ii))
84 print('text jtdbg "%s"' % flat(di))
85 print('note counts: inv %d pos %d pow10 309 lower-ranges %d cased %d ignorable %d debug-escaped %d' % (len(inv), len(pos), len(ranges), len(ci), len(ii), len(di)), file=sys.stderr)
86
87main()