scratch cell's bits
I said a two cell in place increment was impossible. It is not. The version in
asm/inc_ip.r8asm does A = A + 1 with A and one scratch cell, 74 characters,
256/256 exhaustive, and there is a two cell in place negate on top of it:
clb_not_ip A A = ~A 1 cell 17 chars 256/256
clb_inc_ip A T A = A + 1 2 cells 74 chars 256/256
clb_neg_ip A T A = -A 2 cells 90 chars 256/256
against clb_inc_nc (3 cells, 430) and clb_neg_nc (3 cells, 448). Five times
shorter, one cell less.
Both of my earlier impossibility arguments were about a *specific* algorithm, the
per bit carry ripple, and I stated them as if they covered the problem. The one
worth remembering is the synthesis one, which is still true: a synthesised block
has exactly one condition cell, so it cannot tell (A=1, Ci=0) from (A=1, Ci=1),
and all four in place tables answer "no program up to 8 layers". The hand
written argument was simply wrong.
The algorithm that works does not test the carry per bit at all:
1. `[ ... ]` already means "while the low bit is set", so clearing the run of
low ones is a loop that falls out on the first zero (flb + ror);
2. flip that zero to one -- skip it when the input is all ones, which is exactly
the wrap-around case;
3. rotate the rest of the way.
What is left is "how many rotations remain", with only two cells. The counter
goes into the scratch cell's bits: T starts as 0xFF, and every rotation does
set_0 then ror, which eats one 1 -- the length of the run of ones is the number
of rotations left. Three characters per step, and a full turn counts to eight.
The wrap case falls out for free: when the input was 0xFF the counter is spent
exactly then, so step 2 is skipped and the answer is 0.
Two smaller notes. The dp rule from the last commit is what these macros had to
respect -- every conditional body returns to its own condition cell -- and
`[ ... ]` consuming its condition is only a problem when the condition is data;
here it is the counter. And I mangled asm/inc_ip.r8asm once with a PowerShell
Get-Content / Set-Content round trip (the trap AGENTS.md warns about) and had to
rewrite it with the editor; no other file was touched.
byte_op_test is twelve operations now, still exhaustive, still under a second.
Asked why an in place complement does not exist, given that in place inversion
does. The inversion half is now hoisted out of the per bit loop, which is both
simpler and shorter:
clb_neg_nc A S Ci = clb_not_ip A ; clb_inc_nc A S Ci 468 -> 448
clb_sub_nc A B S Ci = clb_not_ip B ; clb_add_nc A B S Ci 725 -> 707
and the unsigned complement through the same macro (carry in at 0) drops 467 ->
447. B is eaten by the add anyway, so inverting it in place costs nothing. The
per bit `flb` wrappers (clb_bit_neg_nc, clb_bit_sub_nc) are gone with them, so
macro_docs_test now counts 35 macros.
What does not work is the other half: there is no in place increment, so the sum
still has to land in S. Two independent reasons, both written down:
* A synthesised block has exactly one condition cell, so it cannot tell
(A=1, Ci=0) from (A=1, Ci=1) -- and that distinction is the whole carry rule.
All four in place tables answer "no program up to 8 layers".
* Hand writing cannot do it with two cells either: `[ ... ]` is the only one shot
conditional this ISA has, and it eats the bit it tests, while in place
increment needs both cells' bits alive at the same time.
So one cell buys the ones' complement only; a two's complement negate costs three
(A, S, carry). tests unchanged and green: ten byte level operations, 1666
character test tape, 8/8 full adder, fibonacci.
Also: the docs quoted clb_neg_nc at 468 and clb_sub_nc at 725, now 448 and 707.
Asked whether two's complement can be done in place. The negate cannot, but the
inversion half can and is absurdly cheap, so it is a macro now:
clb_not_ip A A = ~A, one cell, 17 characters, 256/256
`%rep 8 { flb; ror }` -- flip the low bit, rotate the next one down, eight times
puts the byte back where it started. The same result through clb_neg_nc with its
carry at 0 costs 467 characters, because that path runs a synthesised slice once
per bit.
The in place two's complement itself: tried four tables and none of them exist.
A <- ~A ^ Ci A <- A ^ Ci A <- A ^ B ^ Ci A <- A ^ ~B ^ Ci
no program up to 8 layers (all four)
Two separate reasons, both worth keeping:
* Synthesis cannot: a block has exactly one condition cell. In place increment
has to tell (A=1, Ci=0) -> (1,0) from (A=1, Ci=1) -> (0,1); A is the same in
both and only Ci differs, and one condition cannot look at two cells.
* Hand writing cannot either (2 cells): `[ ... ]` is a while loop and the only
one shot conditional this ISA has, and it eats the bit it tests. In place
negate wants "flip the low bit every round, then decide the carry from it",
so both cells hold live bits and the test destroys one.
So a negate still has to land in a separate S (clb_neg_nc, 3 cells); one cell
buys the ones' complement only. byte_op_test is ten operations now, AGENTS.md
and README.md carry the distinction and the two reasons.
They used to write the carry cell themselves -- set_0 inside clb_add_nc, set_1
inside clb_inc_nc / clb_neg_nc / clb_sub_nc -- which made the parameter a lie: a
cell the caller passes and the callee overwrites is not a carry input, it is a
scratch cell wearing a carry's name. It also ruled out the thing a carry is for:
multi byte arithmetic. The carry out of the low byte could not be handed to the
high byte, because the high byte's call would zero (or one) it first.
Now the macros only read it, and the caller decides:
clb_add_nc A B S Ci S = A + B + Ci Ci = 0 plain add, or chained
clb_inc_nc A S Ci S = A + Ci Ci = 1 gives A + 1
clb_neg_nc A S Ci S = ~A + Ci Ci = 1 gives -A, Ci = 0 gives ~A
clb_sub_nc A B S Ci S = A + ~B + Ci Ci = 1 gives A - B, Ci = 0 A-B-1
which also turns the negate into two operations for free: two's complement
negation at Ci=1 and the ones' complement (~A) at Ci=0. The second one has its
own driver and test now (drv/raw_not_nc_byte.r8asm).
The drivers set the bit explicitly before the call, which is where that
instruction belongs -- the tapes end up the same length as before (688/430/468/725
instead of 693/433/471/730: one setup instruction moved out of the macro).
All nine byte level operations still pass, and the docs (AGENTS.md, README.md and
the four file headers) now state the carry in contract instead of describing a
constant.
asm/raw_add_nc4.r8asm carried the same 4 cell table as asm/raw_add_nc.r8asm --
same stat line, same metric, only the wrapper was named bit_add_nc4 -- and
nothing included it. Gone; raw_add_nc.r8asm stays, it is the one
verify_raw_add_nc.r8asm and the docs point at.
While looking at it: is the third parameter a scratch cell, and does the slice
have a carry input at all? Measured with verify_raw_add_nc.r8asm:
A=0 B=0 carry-in=0 -> S=0 carry-out=0
A=0 B=0 carry-in=1 -> S=1 carry-out=0
A=1 B=1 carry-in=1 -> S=1 carry-out=1
so the cell is read on the way in and written on the way out -- a carry input
and a carry output sharing one cell, not a scratch. All four parameters carry
meaning, which is why the `raw` in bit_raw_add_nc is history rather than
something SPEC.md asks for (raw = the caller passes the scratch cell). AGENTS.md
now spells that out instead of repeating "and the scratch cell is passed by the
caller", which was wrong for this one.
The macro count in the docs drops to "every macro" rather than a number, so
adding or removing one does not leave a stale count behind.
Everything still green: test.r8asm 1666, full_add 8/8, verify_raw_add_nc,
byte_op_test 65536x4 + 512, macro_docs_test 29 macros in 9 files, fibonacci.
Audited all 30 %macro in asm/. The format was already followed almost
everywhere -- the doc block sits above the %macro, or above the %synth block
that generates its body -- but four things were wrong:
raw_and.r8asm the two doc blocks still carried the old names (bit_and, and)
raw_xor.r8asm raw_xor's first two parameters were typed Unit, not Cell
lib.r8asm step / back / output had no type at all ("%1: step_count")
and two files still repeated the retracted claim that set_0 only converges on 0
and 1 (raw_add_nc.r8asm, raw_add_nc4.r8asm). Those now say what set_0 really
does -- it clears the low bit -- and stay on the point that matters for a bit
level slice: A, B and the carry must be single bits because the searched
program only tracks the low K bits.
Parameter types are now a closed set: Cell, CellBit, Unit, UnitBit, Count
(Count is for step/back's repeat count). It is written down in AGENTS.md
together with the format, so the next macro has something to copy.
New r8forge_macro_docs_test checks the format instead of trusting review: a doc
block must exist, its "; Macro <name>" line must match the macro, the number
of documented parameters must equal argc, and every type must be one of the
five. Negative tested by deleting a parameter line and by renaming a block --
it reports both and exits 1.
Also fixes a real annoyance found while running the checks: %synth writes its
generated macro to the system temp directory and imports it. That write fails
intermittently on this machine (same directory, cmd can write, r8forge cannot;
cause unknown), which turned every assembly into "cannot open ...synth.r8asm".
The body is now built in memory and written to the temp directory or, if that
open fails, to the working directory, with one stderr line saying so.
Full suite green: test.r8asm 1666 characters, full_add 8/8, verify_raw_add_nc,
byte_op_test 65536x4 + 512, macro_docs_test 30/30, fibonacci prints its ten
numbers.
It was written from a wrong diagnosis. Because the interpreters in this repo
read biz/rnz as a whole cell zero test, every packed byte program looked hung,
and the conclusion was "byte level arithmetic has to be one bit per cell".
With the interpreters fixed (86a54aa) that reason is gone: raw_add does the same
job with 6 cells instead of 32, and this repository had already deleted the same
approach once before for the same reason.
Removed: asm/byte_add.r8asm, asm/pack_byte.r8asm, examples/add8.r8asm,
examples/add8_inc.r8asm, tests/byte_add_test.cc and its CMake target.
AGENTS.md and README.md stop listing them and each keeps one paragraph saying
the direction was tried and dropped, so the next person does not add it back.
Still green after the deletion:
asm/test.r8asm 1666 characters, 7/7 cache hits
full_add 8/8 combinations
verify_raw_add_nc assembles
byte_op_test raw_add/and/or/xor 65536 each, raw_inc 512
fibonacci 1 1 2 3 5 8 13 21 34 55
Four interpreters here (tests/synth_test.cc, src/derive.cc and the two I added
earlier: tests/tape_run.cc, tests/byte_add_test.cc) implemented '[' and ']' as
a Brainfuck style "is this whole cell zero" test. The reference implementation
does not:
ROT-8-viz src/rot8.c
rot8_biz: if ((*mem & 1) == 0) skip to just past the matching ']'
rot8_rnz: if ((*mem & 1) == 1) jump back to the matching '['
That one difference is why every byte level program in the library looked like
an endless loop. It is invisible in the bit level world -- for a cell holding 0
or 1 the low bit test and the zero test agree -- which is why the synth tables,
asm/test.r8asm and full_add never noticed anything.
Cross checked against the reference before touching anything: the tape our
assembler produces for "A = 128, set_0, B = 3, raw_add" prints 128 3 0 131 0 on
ROT-8-viz/build/rot8_test.exe and hung in our derive.
With the fix, measured:
raw_add 65536/65536 pairs ok 1647 chars
raw_and 65536/65536 639
raw_or 65536/65536 575
raw_xor 65536/65536 463
raw_inc 512/512 1214
asm/fibonacci.r8asm prints 1 1 2 3 5 8 13 21 34 55, exactly what its
comment claims
So the byte level layer was never broken. Two commits ago it was declared dead
-- set_0 "only converges on 0 and 1", the bits of a packed byte "unreadable",
raw_add "only survives 0+0" -- entirely on the strength of the broken
interpreters. AGENTS.md and README.md now say the opposite and keep the wrong
diagnosis as a worked example: when byte level code looks hung, check the
interpreter first.
New:
tests/byte_op_test.cc + drv/raw_{add,and,or,xor,inc}_byte.r8asm
exhaustive runtime proof for the byte level library, 0.5 s
tests/tape_run.cc + r8forge_tape_run
a second interpreter that runs a .rot8 file directly -- cross checking
derive is now one command
verify_set_0.r8asm now demonstrates what set_0 really does instead of claiming
it hangs: 255 -> 254, 5 -> 4, 128 -> 128 (it clears the low bit, not the cell).
The one bit per cell layer (byte_add / pack_byte) stays as a documented
alternative: it is correct under the real semantics too, just redundant.
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".
AGENTS.md, pointed at from the README, so a new session does not have to
rediscover any of this:
* working habits: put anything over ~30s in the background, keep large stderr
out of the transcript (the transport failures), write Chinese only through
the editor, put conclusions in commits rather than in the chat
* build and self check commands for both MSVC and clang-cl
* r8forge_derive's three modes, including the detail that -1/-2 take assembly
source, not a .rot8 tape
* the instruction semantics and the two consequences: set_0 only converges on
0/1, and synthesised programs only touch the low K bits of each cell
* %synth syntax, metric flat, the cache key and kModelVersion, the 990M state
reference point and the layer bound of 8
* what is verified working, and the list of dead ends not to retry: three cell
slices, rotation with synthesised slices, bit per cell adders, byte level
tables through the search
* the naming and comment conventions of lib.r8asm
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.
%synth could optimise the real tape characters, but that price depends on where
the cells sit, so the answer was only good for one layout and the cache could
only be shared between call sites with the same addresses.
metric chars tape characters including the .tp walk layout dependent
metric flat characters without the walk layout independent
metric blocks block count layout independent
'flat' is the step-free mode: it counts biz/flb/rnz and the ops, ignores how
far apart the cells are, so one search result and one cache entry serve every
layout. The price is a slightly longer tape when the layout happens to suit a
different program: 90 characters for chars, 95 for flat on the full adder.
Checked: examples/full_add_flat.r8asm hits the cache on the second run, and the
same block with 'layout 0 2 4 6 8 10' added hits the *same* entry and emits a
byte-identical tape (the key drops the layout for the layout-free metrics).
Both pass the eight combination check.
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.
A synth block used to spell out which parameter is an input and which is an
output. That was redundant: with map, 2^K entries already mean %1..%K are the
inputs and the value's bits land on the parameters after them; with a raw
table, nothing needs saying at all. So 'inputs' and 'outputs' now fail with a
message pointing at map/stat, and the map form derives both.
Added 'stat' as the fully explicit form: 2^params values, one per input
combination, which is exactly the state table the search consumes.
examples/full_add_stat.r8asm is the same adder written that way and produces a
byte-identical tape to the map version (95 characters), both pass the eight
combination check.
%synth used to always run both searches, so every assembly of the full adder
sat there for twenty minutes. A block can now say 'mode layers' to stop after
the layer search (7.7 seconds) or 'mode tape' (the default) to pay for the
shortest tape. examples/full_add_layers.r8asm is the fast one, 95 characters
and still checked on the machine.
The lexer also no longer chokes on a UTF-8 BOM. Every Windows editor writes
one, and the first token came out as a garbage prefix, which crashed the
assembler with an access violation rather than saying anything useful.
README grew a getting-started walkthrough: build, a complete adder.r8asm, what
the two stderr lines mean, how to drop a synth block into a library, and the
three things that bite (inputs first, don't call them in/out, map length).
The layer search still runs first (7.7 seconds) but only to get a bound; the
cost search then takes over and its answer is what goes on the tape. A caller
that wants its own layout can say 'layout 0 2 4 6 8 10' inside the block.
examples/full_add.r8asm now assembles to 90 characters of tape instead of 95:
cost 75 in 6 blocks after 415M states and about 21 minutes, against the 82 the
five layer program needs. Assembly getting slower is the whole point -- it is
the price of the shortest tape.
For scale: one bit_raw_add slice is 179 characters with the cells adjacent, and
the synthesised slice is 82. A whole raw_add call is 1650 characters today, so
eight synthesised slices would land near 700.
Five layers cost 95 characters; six layers cost 90. Both are in examples/ and
both pass the eight-combination check on the ROT-8 machine in
tests/synth_test.cc (which now takes a file argument).
sacrifice B, FLIP {S,T}
sacrifice A, FLIP {B}
sacrifice B, FLIP {A,Ci,S}
sacrifice Ci, FLIP {B,Co,T}
sacrifice S, SET1 {Ci,Co}
sacrifice T, FLIP {B,Ci,S,Co}
Cost 75 in the model, 90 on the tape once the inter-block hops and the two
output lines are counted. The goal was popped at 75 only after everything
below 75 had been expanded, so 75 is the minimum of this cost model.
Trading a layer for a shorter tape is not a contradiction: the layer search
cannot see that a small, cheap block can be worth more than one wide, expensive
one.
First version scanned ahead for the next nonempty bucket but stopped one max
edge weight out, so a sparse frontier ended the search early and claimed
"nothing under the limit" while the queue still had work. Scanning further is
also wrong: the ring reuses a slot every ring.size steps, so a far bucket sits
where a near one would be. Plain Dial now: step one cost unit at a time.
The growth table in the README was measured with the broken version and was
wildly optimistic. Real numbers from the fixed build, full adder with
adjacent cells: cost 58 -> 80M states, 66 -> 220M, 70 -> 280M, about 27 bytes
per state and 650k states/s, so cost 75 / 450M states / 12GB is roughly the
ceiling on this machine.
Validated solve_cost on the small problems where the answer is known by hand:
copy comes out at cost 17 (sacrifice 0 into {2}, then sacrifice 2 into {0,1}),
which is the two block program, and it replays ok.
Layers are not characters. solve_cost runs Dijkstra over the same state graph
with an edge weight of what the block actually assembles to:
w = 3 (biz + flb + rnz) + 2 * span(cell addresses) + sum(op)
op = 1 for flb, 4 for set_1
Dial buckets, pop in cost order, the first pop of the goal is the shortest
tape. Give it max_cost and it becomes branch and bound: anything past the
bound never enters the queue.
Exhausted regions on the full adder with adjacent cells:
max_cost 20 1,086 states 0.0s
max_cost 40 128,528 states 0.1s
max_cost 50 999,448 states 0.7s
max_cost 60 7,671,626 states 12.8s
max_cost 65 20,153,459 states 47.6s
About 2.6x states per 5 cost, about 32 bytes per state, so max_cost 80 is
~350M states / 11GB and 82 would be ~600M / 19GB. The block weight leaves out
the hop from the previous sacrifice (that would make the cost depend on
history and Dijkstra would stop working); the emitter adds it when it reports
the real length -- the five layer program is 82 in the model, 87 on the tape.
The first version of the bucket ring only looked one max-edge-weight ahead for
work, so it declared 'nothing under the limit' while the queue still had
states further out. That made the small limits look sound when they were not;
advancing to the next nonempty bucket fixed it.
The synthesised body is written out as a macro file and imported, so the
generated code goes through the ordinary expansion path instead of being
spliced by hand. set_1 is spelled out as set_0 plus flb, so the generated file
needs nothing from lib.r8asm.
examples/full_add.r8asm now assembles: 7.7 seconds of search at assembly time,
then a 95 character tape. tests/synth_test.cc runs that tape on a small ROT-8
machine for all eight input combinations and checks S and Co -- all eight pass.
Two things that cost me time and are worth writing down:
* the opcode map is "><+-[].,", i.e. '-' is flb and '+' is ror. Guess it the
other way round and a perfectly good tape looks like an infinite loop.
* synth names must not collide with mnemonics: 'out' is lexed as rot8_out
before it can be a block keyword.
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.