Commit Graph
8 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 505fe8af86 Make it build under clang
Found with scoop's llvm 23.1.5 (clang-cl), which is stricter than MSVC about
non-type template arguments:

    candidate template ignored: substitution failure: deduced non-type
    template argument does not have the same type as the corresponding
    template parameter ('unsigned long long' vs 'int')

block_cost deduced its N from std::array<int, N>, and that deduces size_t;
clang refuses to convert size_t to the int parameter, MSVC allows it.  N is now
size_t.  Every target (r8forge, synth_test, derive, cost_par, cost_search)
builds clean with clang-cl and the clang-built binaries assemble test.r8asm to
the same 1674 characters, pass the eight combination check and derive the same
bit_raw_add table.

Two other portability fixes in the same pass:
  * cost_search used _strtoui64, which is MSVC only -- now std::strtoull
  * the CMake project now finds Threads and links Threads::Threads, which
    clang and MinGW need to pull in the thread runtime
  * if spawning a worker throws (no thread runtime available) the search now
    carries on with the threads it did get instead of failing
2026-11-06 13:54:46 +08:00
dsh 0bc5f1e6b8 Phase-parallel tape search: 22 minutes down to 2.7
The sequential search is memory bound on its hash table, so the split that
works is inside a Dial bucket, not across the graph:

  * the table is read only for the whole bucket, so threads probe it with no
    locking -- 99.7% of the work, since only about one edge in four hundred
    lands on an unseen state
  * misses and dist improvements are buffered per thread and applied by one
    serial pass after the bucket

Cost order is untouched, so the answer is identical.  Measured at limit 60:

    threads   wall      states
    1         12.40s    7,671,626
    8          2.68s    7,671,626
    16         2.20s    7,671,626
    32         2.14s    7,671,626

and at the full limit 82 (the run that used to take 1350s): 162s, 414,631,673
states, cost 75 in 6 blocks -- the same program.

%synth now uses this by default.  examples/full_add.r8asm assembles in 166s
instead of ~23 minutes and still produces the 90 character tape, checked on the
machine for all eight combinations.

The earlier attempt -- one worker per possible first block -- stays in
r8veritas.hh behind opt.threads but off by default; it duplicated the middle of
the graph and cost three to four times the work.
2026-11-06 13:24:09 +08:00
dsh 548c239a85 Tape-length search beats the layer search on the full adder
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.
2026-11-06 09:12:15 +08:00
dsh 230cf92159 Fix the Dial queue: two ways it silently dropped states
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.
2026-11-06 09:10:00 +08:00
dsh 98593b568c Add a tape-length search: Dijkstra with the real block cost
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.
2026-11-06 08:49:11 +08:00
dsh 79bc0a4068 Splice %synth into the tape, and check it on a ROT-8 machine
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.
2026-11-06 00:08: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