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.
clb_neg_nc A -> -A 3 cells 471 chars 256/256
clb_sub_nc A - B 4 cells 730 chars 65536/65536
Both are "invert, then add", which is all two's complement needs:
-A = ~A + 1
A - B = A + ~B + 1
so the carry cell starts at 1 (that bit is the +1) and each bit flips its input
with one flb right before the slice reads it. Subtraction's carry out turns out
to be "no borrow": 1 when A >= B, so the same macro answers an unsigned
comparison for free. Negation's carry is 1 only for A == 0, where -0 is 0 but
does carry out.
The bit level wrappers come along too: clb_bit_neg_nc (flb + clb_bit_inc_nc) and
clb_bit_sub_nc (flb + clb_bit_add_nc).
Tried first, and it does not work: a slice that inverts its own input. The
table S = ~A ^ Ci, carry = ~A & Ci with 3 params answers
r8asm: %synth clb_bit_neg_nc_synth: no program up to 8 layers
That is a model limit, not a search budget problem: a synthesised block
"sacrifices one cell, conditionally flips others", so when A is 0 the whole
block is skipped and S can only keep the 0 the wrapper gave it -- while inverting
needs S = 1 exactly then. Unconditional flip is not expressible, so the flip has
to stay outside the slice. Both facts are in the file comment and in AGENTS.md.
byte_op_test now covers nine operations, exhaustive, 0.72 s:
raw_add/and/or/xor 65536 each raw_inc 512
clb_add_nc 65536 clb_inc_nc 256
clb_neg_nc 256 clb_sub_nc 65536
macro_docs_test: 36 macros in 12 files, all in the house format.
The nc slices do drive a rotation, so two more library macros fall out of them:
clb_add_nc A + B -> S, carry in/out in one cell 4 cells 693 chars
clb_inc_nc A + 1 -> S, carry in/out in one cell 3 cells 433 chars
against raw_add (6 cells, 1647) and raw_inc (5, 1214). Both are in
r8forge_byte_op_test now, exhaustive: 65536 and 256 combinations, all pass.
clb_inc_nc has to start its carry cell at 1 -- that bit is the "+1"; the add
version starts at 0. A rotation driver reads the low bit, throws it away and
rotates, so eating the operands is exactly what it wants.
This retracts a sentence that has been in the tree for a while: "rotation needs
A and B preserved". It does not. Preserving them is a separate wish (raw_*
grants it, with two more cells). AGENTS.md said the wrong thing in the do not
retry list; that bullet now says what was really measured, which is only that a
4 cell slice asked to *preserve* A and B has no solution up to 8 layers.
Renamed for the same reason: bit_add_nc -> clb_bit_add_nc, bit_inc_nc ->
clb_bit_inc_nc, verify_add_nc.r8asm -> verify_clb_bit_add_nc.r8asm,
verify_inc_nc.r8asm -> verify_clb_bit_inc_nc.r8asm. Measured what the prefixes
actually separate here:
bit_raw_and A=1 B=1 -> cells 0=1 1=1 2=1 operands preserved, has tmp
bit_raw_add A=1 B=1 -> cells 0=1 1=1 4=1 operands preserved, has buf
clb_bit_and A=1 B=1 -> cells 2=1 operands eaten, no tmp
clb_bit_add_nc A=1 B=1 -> cells 2=1 3=1 operands eaten, no tmp
so raw means "the caller passes the scratch cell" and those variants happen to
keep their inputs, while clb means the inputs are gone. These slices eat A and
B, so they are clb.
One self inflicted mess to own up to: I rewrote AGENTS.md with a PowerShell
Get-Content / Set-Content round trip, which on this machine decodes UTF-8 as GBK
and wrote the file back as mojibake. Restored it with git checkout -- AGENTS.md
and redid every edit through the editor. The work rule in AGENTS.md now names
that exact trap instead of only warning about -Encoding ascii.
Green: test.r8asm 1666, full_add 8/8, both verify_*.r8asm, byte_op_test
(65536x4 + 512 + 65536 + 256), macro_docs_test 32 macros in 10 files, fibonacci.
The rename: the four cells of that slice all carry meaning -- A and B are
inputs, the third is a carry that is read on the way in and written on the way
out, the fourth is the sum. There is no scratch cell, so SPEC.md's `raw`
("the caller passes the scratch cell") does not apply and the name now says so.
Measured before renaming: A=0 B=0 carry-in=1 gives S=1, so the cell is an input,
not a spare.
The cache survived the rename because the key is model version + params + mode +
metric + the resolved table (src/synth.cc), never the block name:
"bit_add_nc_synth: cached, 5 blocks".
verify_raw_add_nc.r8asm is now verify_add_nc.r8asm.
New asm/raw_inc_nc.r8asm, asked as an experiment ("can we even make one"):
bit_inc_nc, a 3 cell increment slice -- A, carry in/out, S -- three parameters
instead of raw_inc's five. Table 0 4 4 2 0 4 4 2 resolved to a 4 block program
that was already in the cache, emitted tape is 54 characters, and all four input
combinations check out:
A=0 carry=0 -> S=0 carry=0 A=1 carry=0 -> S=1 carry=0
A=0 carry=1 -> S=1 carry=0 A=1 carry=1 -> S=0 carry=1
so S = A ^ Ci and the carry becomes A & Ci, with A eaten -- the same shape as
bit_add_nc, and like that one it cannot drive a byte level rotation (the operand
is consumed). verify_inc_nc.r8asm drives it.
AGENTS.md: the two slices are in the self check list, the verified table and the
naming table; the "every macro" doc check now counts 30 macros in 10 files.
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.
schannel fails here with SEC_E_NO_CREDENTIALS, so fetch and push both need the openssl backend; that is already set in this repo's local config. The credential helper additionally needs a one time escalation, because it opens a named pipe and the sandbox refuses it (couldn't create signal pipe, Win32 error 5). Fetch works without it: the repository is readable anonymously.
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
The old text said "a macro that exposes cells as parameters must have raw in its
name". SPEC.md (r8asm_rev-fork/SPEC.md, identical in r8asm_rev) says something
else:
clb clobber destroys its input (usually clears it)
bit bit works on the LSB of a cell
raw raw unwrapped; the caller passes the scratch cell
So raw_add is called raw because you hand it the Co and tmp cells, not because
it takes cells at all, and bit means every CellBit parameter is a low bit -- the
same fact as biz/rnz testing & 1, which is what went wrong last round.
Record the three prefixes, apply them to the existing library, note the two
macros that predate the rule (bit_copy and copy take a buffer without raw in the
name), and point at the two documents that actually define the language: SPEC.md
and the instruction table in ROT-8-viz/README.md. %synth is not in the spec --
it is this repository's own extension, documented only in README.md.
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.
verify_set_0.r8asm is four characters of tape: set_0 only converges on 0 and 1,
so A=128 hangs. drv/raw_add_byte.r8asm drives raw_add with operands that carry
no baked in constants -- asm/test.r8asm XORs 127 and 130 into the cells, which
made a first reading of the symptom easy to get wrong.
Measured with both: raw_add survives 0+0 and nothing else (1+0, 0+1, 1+1, 2+2,
5+3, 10+20, 128+0, 128+128, 255+1, 255+255 all hit the step limit).
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