#!/usr/bin/env python3 """0029: compare the assembly's per-signature trace with 0028's FLINT-engine trace.""" import re from pathlib import Path HERE = Path(__file__).resolve().parent REF = HERE / "my_shell_trace224.txt" OURS = HERE / "0029_trapz.log" sig_re = re.compile(r"sig (\([^)]*\))\s+contribution ([-\d.e+]+)") our_re = re.compile(r"sig (\([^)]*\))\s+= ([-\d.e+]+)") shell_re = re.compile(r"shell contribution = ([-\d.e+]+)") our_shell_re = re.compile(r"cap \d+ shell contribution = ([-\d.e+]+)") def _read(path): b = Path(path).read_bytes() if b[:2] in (b"\xff\xfe", b"\xfe\xff"): return b.decode("utf-16") return b.decode("utf-8", "replace") def parse_sig(path, rx): out = [] for line in _read(path).splitlines(): m = rx.search(line) if m: out.append((m.group(1), float(m.group(2)))) return out ref = parse_sig(REF, sig_re) ours = parse_sig(OURS, our_re) print("reference signature entries:", len(ref), " ours:", len(ours)) worst = 0.0 worst_sig = None for a, b in zip(ref, ours): if a[0] != b[0]: print("ORDER MISMATCH", a[0], b[0]) break d = abs(a[1] - b[1]) / max(abs(a[1]), 1e-300) if d > worst: worst, worst_sig = d, a[0] print("worst relative deviation on a per-signature value: %.3g (sig %s)" % (worst, worst_sig)) print() ref_shells = [float(x) for x in shell_re.findall(_read(REF))] our_shells = [float(x) for x in our_shell_re.findall(_read(OURS))] print("shell totals:") for i, (a, b) in enumerate(zip(ref_shells, our_shells)): print(" shell %d reference %.14g ours %.14g abs diff %.3g rel %.3g" % (i, a, b, abs(a - b), abs(a - b) / abs(a))) print(" total reference %.14g" % sum(ref_shells))