Commit Graph
13 Commits
Author SHA1 Message Date
dsh 43390efe8a Byte level add, one bit per cell: 65536 combinations ok
The rotation based byte layer never ran.  Measured with r8forge_derive -2:

    raw_add   A=0,B=0 / 1+1 / 3+5 / 1+0   out empty (step limit hit)
    raw_add   A=127,B=130                 out 0 0 0 0 0, everything destroyed
    raw_and   0xF0 and 0xCC, 1 and 1, 0 and 5   out empty
    copy      A=5                         out empty (A=0 returns fine)

Not "only operand preservation is missing": set_0 (= [-]) cannot clear a cell
that holds more than one bit, and biz only answers "is this whole cell zero",
because ror does not change zero-ness.  So the bits of a packed byte are
unreadable, and the whole ror + set_0 layer has no future.  The docs now say
that instead of blaming operand preservation.

What replaces it is one bit per cell:

  asm/byte_add.r8asm   8 bit adder.  Bit i owns four adjacent cells at base+4i:
                       A, B, carry (in -> out), scratch.  The synthesised four
                       cell slice bit_raw_add_nc does the bit, clb_bit_mov moves
                       the carry into the next quartet.  A doubles as the
                       accumulator, so calling byte_add again continues the sum.
  asm/pack_byte.r8asm  eight flags -> one byte, the only direction that works.
                       bit_raw_flip_if consumes a flag into flipping bit 0 of an
                       accumulator that may already hold bits (no set_0 there).
  examples/add8.r8asm  a+b, 1308 characters.  A shared carry cell layout needs
                       only 25 cells but was measured at 2517: paying .tp travel
                       eight times beats saving seven carry cells.
  examples/add8_inc.r8asm   add 1 three times, proving the block composes.
  tests/byte_add_test.cc    r8forge_byte_add_test: runs the tapes on the ROT-8
                       machine over every input pair, 0.3 s.

Also: AGENTS.md gains the measurement table, the third instruction set
constraint (packed bytes are unreadable), the new files, and the note that
build/ has to be rebuilt with the VS bundled cmake -- scoop's 4.4 reconfigures
it and dies with "No preprocessor test for PellesC".
2026-11-06 23:40:14 +08:00
dsh 505ff509eb Rotation and synthesised slices do not mix; naming fixed
Tried the rotating byte level driver with the 4 cell slice.  It cannot work, and
the search says why: rotation needs A and B preserved, because every round reads
their current bit, while a synthesised program consumes its operands.  Rewriting
the table to keep A and B and letting %synth search it gives

    no program up to 8 layers

so 'read without destroying' does not exist in this block model at four cells.
The hand written bit_raw_add can do it (loops), which is exactly why the original
library rotates inside the macro and the synthesised one cannot.

What is committed instead:

  asm/raw_add_nc.r8asm   bit_raw_add_nc, the destroying 4 cell slice, flat cost
                         27 against bit_raw_add's 38, with the naming and the
                         comment format of lib.r8asm:

                            ;   Macro bit_raw_add_nc
                            ;   %1: CellBit A
                            ;   %2: CellBit B
                            ;   %3: CellBit Ci
                            ;   %4: CellBit S

                         The synth body is named bit_raw_add_nc_synth, so the
                         'raw' in the name marks the macro that takes cells.

  verify_raw_add_nc.r8asm  feeds A, B, Ci to one slice and prints sum and carry:
                         8/8 combinations correct, 78 characters.

The rotation limitation is written into the file header so it is not retried
blind, and the library still assembles from cache (1666 characters, 7/7).
2026-11-06 23:24:27 +08:00
dsh c6bfaca349 Drop the bit per cell adders, keep the slice
Splitting every bit into its own cell bought nothing: the 25 cell adder was a
workaround for a workaround, and it made the cell count worse than the hand
written library it was meant to improve on.

Removed: raw_add_nc (5 cell, broken driver), raw_add_nc3 (the 3 cell dead end),
raw_add_bits4, raw_add_bits8, raw_inc_bits8, verify_add8, verify_inc8.

Kept:
  asm/raw_add_nc4.r8asm            the 4 cell slice, flat cost 27 against the
                                   6 cell full adder's 38, with the two
                                   constraints written down (only the low K bits
                                   are modelled, and set_0 only converges on 0/1)
  verify_bit_add_nc4.r8asm         driver that feeds A, B and the carry to one
                                   slice and prints sum and carry, 8/8 correct

The byte level stays with the hand written library, which rotates inside the
macro and keeps whole bytes in a cell; that is 6 cells for an 8 bit add instead
of 25, at the price of not being synthesised.  %synth earns its keep on the small
bit macros (asm/test.r8asm 1714 -> 1666 characters), not on byte level tables
where the 2^16 entry truth table would be hopeless anyway.
2026-11-06 23:17:19 +08:00
dsh d480c001b1 Three cells for the destructive slice: tried, impossible here
Writing the sum back into A's cell would drop the separate S cells and cost no
extra instructions -- the driver would even get simpler.  So it was worth
trying, and it does not work: %synth reports

    no program up to 5 layers

and raising the layer bound to 8 does not help either:

    no program up to 8 layers

So the obstacle is not runtime cost, it is feasibility: three cells cannot do
' two inputs plus a carry in -> a sum plus a new carry' in this instruction
model, the same class of limit that stopped clb_bit_and.  Keeping the four cell
slice (and the 25 cell bit-per-cell adder) is the right call, and the file is
committed with that conclusion written into it so nobody retries it blind.

The layer bound is now 8 rather than 5 (src/synth.cc), since 5 was low enough to
call some feasible tables impossible; a feasible table still costs the same,
only a hopeless search takes 60% longer before giving up.

Cache untouched: restored from git after the experiments, asm/test.r8asm still
assembles from it in a fraction of a second, 7/7 hits, 1666 characters.
2026-11-06 23:09:45 +08:00
dsh 32b16eb077 Base addresses instead of 25 parameters
The eight bit adder took 25 macro arguments because every cell address was
passed in.  That was laziness: .tp accepts expressions, so the macro can take
four base addresses and add the offsets itself.

    raw_add_bits8 0 8 16 24     ; A0..7 at 0, B0..7 at 8, S0..7 at 16, carry at 24
    raw_inc_bits8 0 8 16        ; a0..7 at 0, s0..7 at 8, carry at 16

The bodies are unchanged apart from '%1 + 1' style addresses, so the tapes and
the behaviour must be identical, and they are: add8 still 2550 characters, inc8
still 1514.

Re-verified after the change, on the built in machine:
  add8  7 sums (100+50, 128+127, 200+55, 43+199, 255+0, 1+1, 0+0)  7/7
  inc8  7 values (0, 1, 5, 127, 200, 253, 254)                     7/7

Also committed two verify_*.r8asm drivers so the numbers are reproducible.
2026-11-06 23:07:09 +08:00
dsh 1b8ff08455 Eight bit adders and increments, one bit per cell
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.
2026-11-06 23:02:09 +08:00
dsh 7cf2fabeb7 A bit per cell adder, and it works
The chained version kept every bit in one cell and rotated, which broke the rule
that set_0 needs a cell holding 0 or 1.  This one gives each bit its own cell
instead:

    %1..%4  A0..A3      %5..%8  B0..B3
    %9..%12 S0..S3      %13     carry

    erase carry
    bit_add_nc4 A0 B0 carry S0
    ... one slice per bit, no rotation at all
    last carry dropped (assumes A+B fits)

No rotation means every cell stays a bit, which is the invariant set_0 needs.
Verified against the built in machine over eight sums including 0+0, 1+1,
15+0 and 9+6: 8/8 correct, and the cell dump after a run shows only the S bits
set, so the operands and the carry are consumed as the table says.

Cost: it needs three cells per bit plus the carry (13 for 4 bits, 25 for 8)
instead of 4 rotating cells, and the expansion is longer because each slice now
has a fixed pair of operands.  That is the price of not breaking the invariant,
and it is why the original hand written library rotates inside the macro instead:
it keeps whole 8 bit cells and loops over the bits itself.
2026-11-06 22:06:58 +08:00
dsh cbadc3936a Document what set_0 really is
Checked instead of assuming, and it explains the chained hang:

    '-'  flb  flips bit 0                    cells[dp] ^= 1
    '['  biz  if the cell is zero, jump past the matching ']'
    ']'  rnz  if the cell is not zero, jump back to the matching '['

so set_0 = [ - ] is 'while the cell is not zero, flip its lowest bit', which only
terminates for 0 and 1.  Measured with the cell dump: X=5 goes 5 -> 4 -> 5 -> 4
until the step limit, X=0x80 never changes, and in both cases the tape never
reaches the next instruction.  erase = %rep 8 { set_0; ror } hangs on the same
set_0, so it cannot clear an arbitrary cell either.

That is the whole story of the 8 bit chain: after a slice and a ror the S cell
holds 0x80, the next wrapper calls set_0 on it, and there is no way out.  The
slice itself is fine, as the dump showed (A and B fully cleared, carry a clean
0/1).

Consequence: a cell always holds one bit in this world, so clearing has to
happen while it still is a bit, not after it has been rotated out.
2026-11-06 22:04:46 +08:00
dsh 13b831ec8b A 4 cell adder slice: the carry borrows the scratch cell
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.
2026-11-06 21:31:04 +08:00
dsh 18bf78273e A 5 cell adder that ignores carry in and out
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.
2026-11-06 21:19:19 +08:00
dsh 14af0ca6ff Standard library: flat metric, cache pre-warmed and committed
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.
2026-11-06 18:55:18 +08:00
dsh 30089af06d Rewrite the bit macros as %synth blocks
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.
2026-11-06 11:04:29 +08:00
dsh 19cbfb36f6 R8Forge: assembler and searcher in one repo, %synth directive
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.
2026-11-05 23:59:15 +08:00