The idea: for an adder whose operand sum is known not to overflow, there is no
need for a separate carry in cell or a carry out cell -- the carry can live in
one cell and be overwritten each bit, and the final carry is simply dropped.
That takes the slice from 6 cells (A, B, Ci, S, Co, tmp) down to 5 (A, B,
carry, S, tmp).
The slice was not hand written: the table was derived from S = A^B^Ci and
carry = AB|AC|BC, and %synth searched it with metric flat:
bit_add_nc_synth: tape cost 27 in 5 blocks (1891406 states)
bit_raw_add_synth (the 6 cell full adder): tape cost 38 in 5 blocks
so 11 units of the metric cheaper, about 29%, plus one cell less per slice.
Verified: all eight input combinations of a single slice are correct through the
built in machine (A, B, Ci -> S), including the all zero one.
Known limitation, found rather than guessed: the synthesised program only
touches the low K bits of each cell, the bits the truth table knows about. The
caller therefore has to hand it cells whose high bits are zero. Chaining slices
with ror breaks that: the rotated cell keeps bit 7, and a block that loops until
its cell reaches zero can never get there. So the 8 bit raw_add_nc hangs; the
slice itself is fine, the driver needs to move each bit into a clean cell first.
Also added: r8forge_derive <file> -1 addr=value ... runs a tape with chosen cells
and prints OUT, which is how the slice was checked.
Every synth block in asm/ now says 'metric flat', so the seven block bodies are
optimised without the .tp walk and therefore do not depend on where the caller
puts its cells. The resulting bodies are cached in .r8forge_cache/ and the
cache is committed, so a fresh clone assembles the library without waiting for
a single search.
asm/test.r8asm: 1674 -> 1666 characters.
The seven tables were re-derived after the switch and all match the hand written
implementations bit for bit (bit_raw_add still 0 9 10 19 12 21 22 31 repeated,
bit_raw_inc 0 5 6 11, and so on), so this is a pure length win.
One caveat: bit_raw_add's flat search still runs out of state budget (700M, the
table stops fitting in a laptop) and falls back to the layer answer, which is
what got cached for it. The other six converge in milliseconds; the whole warm
up took 396 seconds, almost all of it that one block.
The eight bit-level macros in asm/ are now synthesised instead of hand written.
Their tables were not typed in by hand: r8forge_derive (new, src/derive.cc)
runs the OLD implementation over every input combination and reads the cells
back, so the replacement is bit-for-bit identical, quirks included. big two
findings:
* a synth block assumes the parameters that do not take part in the
computation start at 0 (that is what compress_stat_space means). These
macros are called repeatedly inside a loop, so S/Co/tmp arrive dirty; each
one now has a thin %macro wrapper that clears them and then calls the
synthesised body. With that, bit_raw_add derives to exactly the old table
(0 9 10 19 12 21 22 31 repeated) and bit_raw_and to 0 1 2 7 repeated.
* clb_bit_and stays hand written. Three parameters leave no scratch bit and
"and" needs both inputs alive at once, so it needs nested biz; the searcher
exhausts nine layers over three planes without finding anything. That is
the model's expressive limit, not a shallow search.
asm/test.r8asm assembles to 1674 characters, down from 1714, with identical
block semantics.
%synth results are cached in .r8forge_cache/ keyed by the table, so the
twenty minute tape search happens once per table rather than once per build.
Note on verification: r8forge_derive validates one block at a time, which is
what the tables above are. The little VM inside it is not solid enough to
vouch for whole programs (fibonacci prints two numbers instead of ten), so
"the whole library still computes the right answer end to end" is NOT verified
here -- that needs the real ROT-8 virtual machine.
The searcher stops being a separate program: %synth is an assembly-time
directive. It describes a truth table, the assembler runs the search while
parsing, and the block list comes back as ready-to-paste r8asm.
Merging is what makes the cost model honest. The price of a layer is
cost = 2 (biz + rnz) + sum(move distance + op cost)
and the move distance depends on the call site's cell layout, not on anything
the assembler knows in advance: the same five-layer adder is 87 characters
with the cells adjacent and 136 with them two apart. Only the side that sees
the layout can price it, which is now the assembler itself.
Working: the assembler (asm/test.r8asm still assembles to the same 1714
characters), the searcher (bit planes + backward one-step lookup, full_add in
5 layers, proven shortest in 7.2s), and %synth parsing plus the search itself.
Not working yet: splicing the synthesised body into the tape -- a synth call
fails loudly with 'unknown macro' instead of emitting anything, so nothing can
silently assemble wrong. The two ways to finish it are in the README.
Also carries over the CMake fix: no set(CMAKE_CXX_FLAGS ...), which used to
drop /EHsc, push -O3 into Debug and fight /RTC1.