`metric chars|flat|blocks` inside %synth meant the same truth table could be
searched twice for two call sites, and every library block had to spell out
`metric flat` to avoid the default. The cost model belongs to the whole
assembly, so it is one now:
r8forge <file.r8asm> flat (layout free), the default
r8forge --synth-chars <file.r8asm> charge the real .tp walk
flat stays the default because a library block is expanded at dozens of call
sites with different layouts: only a layout free result can share one cache
entry, and it is usually within a few percent (full_add: 95 vs 90 characters).
The chars search is far more expensive (full_add: 415M states, ~21 minutes,
~10 GB), which is exactly why it should be asked for on the command line.
`metric blocks` goes with it: block count is a poor proxy for tape length (and
it was only another way to ask for mode layers).
Implementation: the metric lives in the assembler (ast::synth_metric, default
1) and is set through r8asm::set_synth_metric() before import_file; the CLI in
tests/asm_test.cc parses --synth-chars / --synth-flat. The cache key already
carried metric, so the committed entries still hit: assembling the library and
asm/test.r8asm gives byte identical tapes (28729 characters, byte_op_test all
green, all 13 examples green, synth_test still 8/8).
A `metric` line now fails with a message that names the flag instead of being
silently ignored. examples/full_add_flat.r8asm and full_add_flat_spaced.r8asm
are removed: they only existed to demonstrate the per block metric (the flat
default makes them copies of full_add.r8asm).
Also repairs the mojibake in README's metric section: an earlier tool wrote
\x0c / \x08 / \x0d where \f, \b and \r belonged, which ate characters out of
"flat", "blocks", "biz/flb/rnz" and "rnz". The section is rewritten for the
flag, and the %synth numbers in README/SPEC/AGENTS now match what the tree
prints.
New cache entry c16bf083bb0ccbc8.txt is full_add's stat table with mode layers
+ flat, which full_add_stat.r8asm now produces.
raw_add_bits8 A0..A7, B0..B7, S0..S7, carry = 25 cells
raw_inc_bits8 a0..a7, s0..s7, carry = 17 cells
Both follow the same protocol as the four bit version: no rotation, every cell
holds a single bit, one slice per bit, and the final carry is dropped because
the caller promises the result fits.
The increment slices down to 3 cells -- a, carry (in -> out), s -- and %synth
finds cost 25 in 4 blocks for it: s = a^Ci, carry = a&Ci. Its table was written
as 8 entries because a 3 parameter stat line wants 2^3 values, and the s bit in
the index does not matter, so the four entry pattern repeats.
One mistake worth recording: set_1 takes no arguments and acts on the current
cell, so 'set_1 %17' silently did nothing and the carry started at 0 instead of
1 -- which made 1+1 give 1 instead of 2 while 42+1 and 200+1 looked fine, since
only the low bit was affected. The driver now does '.tp %17' then 'set_1'.
Verified on the built in machine:
raw_add_bits8 9 sums (100+50, 128+127, 200+55, 254+1, ...) 9/9
raw_inc_bits8 9 values (0, 1, 5, 15, 127, 200, 253, 254, ...) 9/9
Tapes: 2550 and 1514 characters.
Asked why the slice still needs 5 cells. It does not: the scratch cell was
doing nothing the carry could not do, so the 4 cell version is
p1..p4 = A, B, carry (in -> out), S
and the table says S = A^B^Ci, carry = AB|AC|BC, A and B consumed. %synth
finds cost 27 in 5 blocks for it -- the same tape length as the 5 cell version
(cell count is not part of the cost metric, only the instructions are) but one
cell cheaper, 73 characters for a single slice.
All eight input combinations verified through the built in machine.
Diagnosis of the chained hang, from dumping the cells after one slice plus the
ror rotation:
A=1 B=0 -> cells: 4=80 (only the rotated sum bit survives)
A=1 B=1 -> cells: 1=1 (only the carry)
so a slice leaves A and B fully clear, and the carry is a clean 0/1. The dirty
one is S: after the rotation its bit 7 is set, and set_0 in the next slice does
not remove it -- the dump after two slices shows 4=81 and, worse, the tape then
runs off the end instead of reaching 'output S'.
So the fix is not in the slice but in the driver: it must not rotate the sum
into a cell that the next slice clears, or must clear the whole cell (set_cell,
not set_0 over the modelled bits). That is the next step, together with
re-checking whether set_0 is the right primitive here at all.
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.
There were two unrelated things under one name:
r8asm::ast::CELL_BITS the machine word width in bits, a run time number
(import_file(path, 8)), used by %rep and the .xor
argument masking
r8veritas<PARAMS> how many parameter cells a synth block has, a
compile time template argument selected by 'params N'
The first is now CELL_BITS, the second PARAMS. What a .r8asm program sees does
not change: _CELL_SIZE / _CELL_MAX / _ROL_STEP keep their names, because
lib.r8asm uses them.
Answering the actual question along the way: the machine word width is a run
time parameter (import_file's second argument, hardcoded to 8 by the CLI), not a
template argument -- the comment in r8asm.hh even apologises for that. The
synth block's 'params N' is the other one, and only that goes through the
template switch (1..8).
The first attempt at the rename also rewrote the _CELL_SIZE string used by %rep
into _CELL_BITS, so the lookup threw and the assembler died with a fastfail on
any file containing .xor or %rep. Caught by regressing test.r8asm (exit
0xC0000409, no tape); the string is restored and asm/test.r8asm is back to 1666
characters with all seven blocks cached.
Asked whether the cache survives a change of CELL_SIZE, and looking at the key
turned up a real problem.
The key used to be params | mode | metric | the stat line's values | layout.
Every block in asm/ uses a stat line, which is why nothing had gone wrong yet,
but a block written with map leaves stat empty -- so two map blocks with the
same params/mode/metric hashed to the same entry, and the second one would have
been served the first one's program.
The key is now the resolved target table itself (every entry, per plane), which
is exactly what the search is given:
* params / CELL_SIZE is part of it, and the table covers all 2^params entries,
so blocks of different sizes cannot collide, and changing params in a block
simply misses and recomputes
* two blocks that describe the same table (map or stat, either spelling) now
share one entry
Also added kModelVersion to the key. Without it, changing the instruction
model later (say adding conjunctions) would keep serving programs that were
optimal for the old model. Bump it whenever the block set changes.
Cache files now start with a readable header, e.g.
# params 3 mode 0 metric 1
0 2 0
1 4 0
Re-warmed after the change: 7 entries, 885s (bit_raw_add again, 990M states).
asm/test.r8asm still 1666 characters, all seven derived tables still match the
old implementations bit for bit, and a second assembly hits the cache for all
seven and produces an identical tape.
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.