Skip to content

W<n> machine-word type + surface add_into prim (ADR-0047) - #8

Merged
chessai merged 2 commits into
masterfrom
add-into-prim
Aug 5, 2026
Merged

W<n> machine-word type + surface add_into prim (ADR-0047)#8
chessai merged 2 commits into
masterfrom
add-into-prim

Conversation

@chessai

@chessai chessai commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Makes reversible word arithmetic writable in surface theseus and compilable to the TISC ROM, and uses a real byte-wide (U8) Fibonacci as the README energy benchmark.

What

Step 1 — U8 Fibonacci in the energy figure. Replace the 1-bit fib in benchmark-energy.svg with a byte-wide Fibonacci: 10 reversible steps of (a, b) |-> (b, a + b mod 256), each compiling to the two-word datapath EXCH ; ADD. Priced end to end: reversible 2.58e-9 J vs CMOS-equivalent 9.10e-8 J (~35x).

Step 2 — the language feature (ADR-0047).

  • W<n> machine-word type (W1/W2/W4/W8): lowers to a single-register Flat(n) leaf, distinct from the tree-encoded numeric U<n>. One ISA register => byte-capped.
  • Builtin prim registry: builtin_prim(key, in, out) resolves a declared prim key to a PrimDef, and a surface prim ... = "key" reference now elaborates to Iso::Prim (was unsupported()). The one key today, theseus.arith.add_into, is (a, b) |-> (a, a + b mod 2^w), addend-preserving. It ships as a symbolic ripple-carry PrimSpec, not a table (a 2^w x 2^w table of deep-thermometer Flat values is heap-pathological — a debug build peaked ~45 GB; the spec is ~2 GB / 19 s).

At w = 8 the compiler recognizes add_into as a single reversible Add, so fibStep = swap2 ; add_into unrolls to a clean EXCH;ADD ROM — the same shape tests/fib_cpu.rs runs. The figure now compiles fib (u8) from this real surface source (dogfood).

Verification

  • nix flake check green (fmt + clippy --all-targets --deny warnings + nextest).
  • Conformance 0027-fib8 (forward + backward, carry wrap) and crates/theseus-tisc/tests/add_into_prim.rs (single-Add recognition, EXCH;ADD shape, fib evals fwd/back, mutual-inverse BVEQ at all widths, non-word-type rejected).
  • Fresh-context cross-model adversarial review: verified the ripple spec exhaustively both directions at every width and the register mapping by execution. Pre-existing checker gaps it exposes (notably: = expr iso bodies aren't checked against their declared signature) are documented in ADR-0047 §3 as follow-ups.

chessai added 2 commits August 4, 2026 22:35
Replace the 1-bit iter fib in the benchmark-energy figure with a
byte-wide (U8) Fibonacci: 10 unrolled reversible steps of
(a,b) |-> (b, a+b mod 256), each compiling to the two-word datapath
EXCH ; ADD. The addInto8 prim (a Flat(8) forward table) is recognized
by the TISC compiler as a single reversible Add, so fib8 lowers to a
clean 20-instruction ROM (the same shape tests/fib_cpu.rs runs) rather
than a bit-blasted gate tree.

Priced end to end: reversible 2.58e-9 J vs CMOS-equivalent 9.10e-8 J
(~35x). Refactor price_benchmark to delegate to a source-independent
price_iso so the Rust-constructed fib8 shares the exact pipeline.
Regenerate benchmark-energy.svg; update README prose + alt-text.
Make reversible word arithmetic writable in surface theseus and compilable
to the TISC ROM. Two pieces:

- W<n> (W1/W2/W4/W8): a machine-word type lowering to a single-register
  Flat(n) leaf, distinct from the tree-encoded numeric U<n>. One ISA
  register => byte-capped. (theseus-check env + theseus-cli value marshaling.)

- A builtin prim registry: builtin_prim(key, in, out) resolves a declared
  prim key to a PrimDef, and a surface `prim ... = "key"` reference now
  elaborates to Iso::Prim (was unsupported). The one key today,
  theseus.arith.add_into, is (a, b) |-> (a, a + b mod 2^w), addend-
  preserving. It ships as a SYMBOLIC ripple-carry PrimSpec, not a table:
  Flat values are deep-thermometer trees, so a 2^w x 2^w table is heap-
  pathological (a debug build peaked ~45 GB; the spec is ~2 GB / 19 s).

At w=8 the compiler recognizes add_into as a single reversible Add, so a
byte-wide Fibonacci `fibStep = swap2 ; add_into` unrolls to a clean
EXCH;ADD ROM -- the same shape tests/fib_cpu.rs runs. The README energy
figure now compiles fib (u8) from this real surface source (dogfood).

Tests: conformance 0027-fib8 (fwd+back, carry wrap) and tests/
add_into_prim.rs (single-Add recognition, EXCH;ADD shape, fib evals,
mutual-inverse BVEQ at all widths, non-word-type rejected). Adversarial
review (fresh-context, cross-model) verified the ripple spec exhaustively
both directions at every width and the register mapping by execution;
pre-existing checker gaps it exposes are documented in ADR-0047 §3.

nix flake check green (fmt + clippy --all-targets --deny warnings + nextest).
@chessai
chessai merged commit 00197b3 into master Aug 5, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant