The profile said 77% of clb_digits' steps are pointer moves, so the layout of its
13 cell scratch block (S..S+12) is the cost. The block is one rigid 7 cell unit
(clb_divmod owns its internal order: Rhi,Sd,Bo,Cm=Bc,Ci,buf,NR) plus six free
units, so the legal space is 5040 permutations. All 5040 were assembled and run
(minimal driver, value 2047):
old: TEN Q R [block] counter T F
new: T1 R [block] TEN Q F counter
tape 13,717 -> 11,717 chars (-14.6%)
steps 145,114 -> 113,264 (-21.9%)
pointer moves 111,958 -> 80,108 (-28.4%) non-move steps identical (33,156)
Full 2048 value sweep: worst 164,843 -> 123,731 (-24.9%), mean 144,904 -> 112,552
(-22.3%), better on all 2048 values. The old layout ranked 3459/5040.
Two things worth remembering. First, ordering the units by the wrapper's first
visit order is *worse* than the old layout (150,748 steps): first visit ignores
revisit frequency -- the counter is touched once per round, TEN is copied into Bc
every round, T1 is hammered by clb_add_ip's carry chain. Second, the n-1 lower
bound is not reachable here: 74% of the steps still execute inside clb_divmod's
block, whose order is not clb_digits' to choose, so this lever is now exhausted
(that is what the exhaustive search proves).
Min tape is not min steps either: block-first BQTRT1F1C gives the shortest tape
(10,713) but 123,890 steps. Tape counts a .tp once, steps count its execution.
Ripple: asm/test.r8asm 64,513 -> 62,513 chars, 236,439 -> 206,491 steps, INS_SIZE
headroom ~1k -> ~3k; --opt 58,060 -> 56,350; calc --opt 52,857; calc_op 77,356 ->
69,324 chars / 599,090 -> 476,170 steps.
Verified: ctest 8/8, byte_op_test 21 items (digits 2048/2048), examples_test 18,
--opt assembles, SPEC 7.7/10.2 + AGENTS + README + docs/calc.md + docs/divmod.md
numbers updated. New driver drv/digits_layout.r8asm keeps the measurement honest
(one clb_digits call, no output, value fed as 0=2047).
clb_digits could turn a number into four ASCII digits but nothing could turn
them back, so a program that stored or received a decimal string had no way
home. clb_undigits REG N S is that way back:
clb_digits N REG S ; 1234 -> 49 50 51 52
clb_undigits REG N S ; 49 50 51 52 -> 1234
Same number of digit cells as clb_digits writes (floor(W*log10 2) + 1, four at
11 bits), each read as its low 7 bits, so leading zeros are fine and garbage in
bits 7 and above is ignored. No validity checking, exactly like clb_digits: a
cell that is not '0'..'9' yields a deterministic but meaningless number, and the
documentation says so.
Horner in a runtime loop, one clb_mul in the whole tape:
N = 0 ; repeat 4: N = N*10 + digit ; shift the digit cells down one
9642 characters (minimum driver) -- smaller than clb_digits, because the reverse
direction needs no division.
Three things worth remembering, all found by testing rather than reasoning:
* c ^ 48 == c - 48 for '0'..'9' (the top two bits cancel, the low four are
identical), so subtracting '0' is one assembly time `.xor 48`: no
subtraction, no constant cell.
* The low-7-bit mask is `ror` 7, then set_0+ror per bit for the rest: the total
rotation is W, so the cell ends up facing the same way. The first version
wrote `rol; set_0; ror` in a %rep and cleared the same bit four times -- it
looks right and is undetectable on inputs whose high bits are already 0
(measured: 0|5<<7 came through as 688).
* The round counter must be erased before it is built: set_1 only clears the
bit it rotates to, so high ones left in the cell turn into extra rounds
(measured: dirty scratch gave 1234 -> 1564).
Tests and docs:
* r8forge_byte_op_test gains test_undigits: all 2048 values x {clean, garbage
in bits 7+} = 4096 cases, worst 36,481 steps.
* examples/undigits_demo.r8asm: "1234" -> 1234, "0007" -> 7, and a
digits -> undigits round trip on 2047; examples_test runs it like the rest.
* asm/test.r8asm calls it too (round trip inside the digits section). That
pushed the file over ROT-8-viz's 65536 instruction limit, so the redundant
second raw_add case, the reversed raw_sub case and the standalone divmod
section are gone -- every macro is still called at least once, and the file
is 64513 characters / 236439 steps / 93 outputs now.
* SPEC 7.9 (contract + the three traps), 10.2, 10.3 and 11, the AGENTS byte
table / verified list / naming table / baselines, and the README example
table all updated.
The old test.r8asm was a three round hash: `clb_add_nc` + `clb_mul`, nothing
else. It proved "add and multiply are enough for a hash" and it was the file
every session was told to check, but a regression in, say, clb_divmod or raw_sub
could not show up there.
It is now the library's kitchen sink instead: one call per standard macro,
grouped from the smallest to the biggest
A lib set_0 set_1 erase rol step back .anchor copy bit_copy
copy_to_buffer bit_copy_to_buffer clb_bit_mov
B bit slices clb_bit_add_nc clb_bit_inc_nc bit_raw_add/inc/and/or/xor
clb_bit_and/or/xor
C raw_* raw_add raw_inc raw_and raw_or raw_xor raw_sub (operands kept)
D clb_* clb_add_nc clb_inc_nc clb_neg_nc clb_sub_nc clb_not_ip
clb_inc_ip clb_neg_ip clb_dec_ip clb_nonzero clb_add_ip
E big clb_ones + loop/endloop, clb_divmod, clb_digits
F language a user %macro that uses %rep and %for, .var arithmetic,
two inline %synth blocks (map and stat, both cache hits)
G the old hash, now two rounds
Each call is followed by `output`, so the OUT stream is the fingerprint.
examples_test now runs this file too and compares it with the `; out` line in
the header (it already did that for examples/), which makes the file an actual
assertion instead of a length in the docs: an example that stops printing what
its header says fails the test.
Numbers: 58722 characters, 215169 steps, 100 values. r8forge_derive,
r8forge_tape_run and ROT-8-viz's rot8_test produce the identical stream (the
last six values are the hash: 1322 5 82 0 1786 94).
Two layout decisions come from that cross check:
* The tape has to stay under 65536 instructions: that is ROT-8-viz's
INS_SIZE (include/rot8/rot8.h) and read_tape truncates silently past it.
An earlier version of this file was 76047 and the viz stopped after 99 of
its 104 outputs, which is how the limit was found.
* So the hash keeps two rounds and the two rounds share one clb_mul scratch
block (erased in between) instead of carrying three separate ones.
The hash's own rules are unchanged: the multiplier stays 141 because it is the
historical value, not because small operands must sit in MUL any more.
SPEC 10.2, the AGENTS baseline/self-check/table and the README build snippet
are updated to the new numbers and to the 65536 limit.
clb_mul was repeated addition -- add MC into the product, MUL times. That is
the shortest thing to write, but the runtime is proportional to MUL's value:
255 * 255 took 733428 steps, and 2047 * 2047 ran into the 5000000 step ceiling
and hung outright (tape_run: HANG after 5000000 steps). So the multiplier was
a ceiling on what any program could compute.
The new body is the plain binary multiplication from school: hold the
multiplicand, look at the multiplier's low bit, add the multiplicand into the
high half of the product when that bit is 1, then shift the whole product one
place right -- the multiplier lives in the low half of that register, so its
bits fall out one at a time. The loop runs _CELL_SIZE rounds whatever the
operands are, and the tape got shorter as well:
82 * 141 358586 -> 8374 steps
255 * 255 733428 -> 17785 steps
2047 * 2047 HANG -> 26123 steps
tape 5007 -> 3977 characters
(Step counts are the tape's own, with no trailing newline -- tape_run counts
one if the file has one, which is where the first draft of this message got
numbers two out.)
Counter, carry and cross cell shift are the three pieces that needed care:
* the round counter is _CELL_SIZE ones in NR, one eaten per round by
set_0 + ror -- the same trick clb_inc_ip counts rotations with, so "how
many rounds" costs no tape length, only runtime;
* the carry out of the W bit add is not scratch: it is the top bit of the
2W+1 bit shift register, and the next shift moves it into PH's top bit;
* (F:PH:PL) >>= 1 is a cross cell shift. ror all three, bring the bit to
inject down to bit 0 with rol, move it with clb_bit_mov, ror back. The
low pair (PH -> PL) moves first, otherwise PH's bit 0 has already been
overwritten by the carry.
And biz/rnz is a loop pair, not an if/else: a one shot conditional has to eat
the bit it tested (a trailing flb) or rnz jumps straight back. The first
version of this hung exactly there -- no output at all, counter still 0x7ff.
The bit eaten is PL's low bit, which the shift discards anyway.
r8forge_byte_op_test now sweeps 0..255 x 0..255 exhaustively (65536/65536 ok,
6.7s instead of 1.5s) instead of sampling 64 combinations: the cost no longer
grows with the multiplier, so sampling was the only reason not to. asm/
test.r8asm still prints 1322 1786 1600 5 94 127, now as 28729 characters and
57330 steps (was 31901 and about two million), and derive and tape_run agree.
AGENTS.md and README record all of it, plus one correction: the old note said
shift and add had been dismissed because "only tape length is the target". It
is in fact 1000 characters shorter, so the reason given was wrong -- the
tradeoff was not. What changed is the target function: the 5000000 step
ceiling is part of it, and a loop whose length does not depend on its operands
can still have a runtime that does.
The assembler already took the width as an argument (import_file(path, cell_size))
and exposed _CELL_SIZE / _CELL_MAX / _ROL_STEP to programs, so every macro that
writes %rep _CELL_SIZE follows the width on its own. What was missing was a
single place saying what the default is, and VMs that agree with it.
include/r8forge/geometry.hh now holds kDefaultCellBits = 11,
kDefaultMemoryCells = 259 and kDefaultCellMask, and the four VMs use it:
* cells are uint16_t, the tape is kDefaultMemoryCells long
* ROR is (v >> 1) | ((v & 1) << (kDefaultCellBits - 1)), not the 8 bit version
* input and output mask with kDefaultCellMask
The tests now state their expectations in terms of the width instead of a byte
(kCellMask, kBits, kCellMod). Two things were genuinely wrong for 11 bits and
are fixed: clb_neg_* and clb_inc_* were compared against 256 - a and 0xff, and
asm/test.r8asm set B with .xor (~126)+1 -- .xor takes its constant modulo the
cell width, so that is 1922 in an 11 bit cell, not 130.
Results at 11 x 259:
asm/test.r8asm 7262 characters, out 127 130 0 257 0 1088 19
(127+130 = 257, and 200*200 = 19*2048 + 1088)
r8forge_byte_op_test all 17 operations pass
r8forge_synth_test all eight combinations ok (bit level is width free)
r8forge_macro_docs_test 41 macros in 17 files
asm/fibonacci.r8asm 11959 characters, 13 terms now (no wrap at 256)
ROT-8-viz moved its defaults to 11 x 259 in the same change, so the tapes cross
check: both implementations print 127 130 0 257 0 1088 19 for asm/test.r8asm.
Tapes grow when the width does (raw_add 1647 -> 2199 characters), because the
%rep _CELL_SIZE loops are longer.
AGENTS.md gains a section on how the width flows through the tools, including
the trap that 2048 % 256 == 0 makes several 8 bit expectations still pass by
accident.
The file was the library smoke test: it set up a few cells, called raw_add and
printed five values. It now also calls clb_mul and prints the two product bytes,
so one tape exercises both of the expensive paths and the self check runs a
result rather than only an assembly.
127 + 130 = 257 -> S = 1, Co = 1
200 * 200 = 40000 -> PL = 0x40 = 64, PH = 0x9C = 156
whole tape 5417 characters (was 1666)
Both interpreters agree on `out 127 130 0 1 1 64 156`: r8forge_derive from the
source and r8forge_tape_run from the assembled tape, 427343 steps.
The multiply half lives at cells 16 and up so it cannot disturb the add half, and
the file's existing cell layout is untouched.
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.