Files
R8Forge/asm/raw_add_nc.r8asm
dsh b4ea79ba91 %synth: the cost model is a run option, not a per block metric line
`metric chars|flat|blocks` inside %synth meant the same truth table could be
searched twice for two call sites, and every library block had to spell out
`metric flat` to avoid the default.  The cost model belongs to the whole
assembly, so it is one now:

    r8forge <file.r8asm>                  flat (layout free), the default
    r8forge --synth-chars <file.r8asm>    charge the real .tp walk

flat stays the default because a library block is expanded at dozens of call
sites with different layouts: only a layout free result can share one cache
entry, and it is usually within a few percent (full_add: 95 vs 90 characters).
The chars search is far more expensive (full_add: 415M states, ~21 minutes,
~10 GB), which is exactly why it should be asked for on the command line.

`metric blocks` goes with it: block count is a poor proxy for tape length (and
it was only another way to ask for mode layers).

Implementation: the metric lives in the assembler (ast::synth_metric, default
1) and is set through r8asm::set_synth_metric() before import_file; the CLI in
tests/asm_test.cc parses --synth-chars / --synth-flat.  The cache key already
carried metric, so the committed entries still hit: assembling the library and
asm/test.r8asm gives byte identical tapes (28729 characters, byte_op_test all
green, all 13 examples green, synth_test still 8/8).

A `metric` line now fails with a message that names the flag instead of being
silently ignored.  examples/full_add_flat.r8asm and full_add_flat_spaced.r8asm
are removed: they only existed to demonstrate the per block metric (the flat
default makes them copies of full_add.r8asm).

Also repairs the mojibake in README's metric section: an earlier tool wrote
\x0c / \x08 / \x0d where \f, \b and \r belonged, which ate characters out of
"flat", "blocks", "biz/flb/rnz" and "rnz".  The section is rewritten for the
flag, and the %synth numbers in README/SPEC/AGENTS now match what the tree
prints.

New cache entry c16bf083bb0ccbc8.txt is full_add's stat table with mode layers
+ flat, which full_add_stat.r8asm now produces.
2026-11-07 23:44:01 +08:00

65 lines
2.3 KiB
Plaintext

%include "asm/lib.r8asm"
; 忽略进位的加法切片:两个操作数,没有独立的进位输入/输出,
; 进位在自己的 cell 里就地更新(进->出),最高位的进位由调用者丢掉。
;
; 切片占 4 个 cell:A、B、进位、S。A、B 被吃掉(结束为 0),这和 bit_raw_add
; 不一样 —— 那个是保留 A、B 的,代价是多一个 Co 和一个 buf。
;
; 两条要知道的:
; 1. %synth 出来的程序只碰每个 cell 的低 K 位,高位不追踪 —— 这是个位级切片,
; 喂进来的 A、B、进位都必须是 0/1;
; 2. set_0 清的是最低位(`biz`/`rnz` 判 `cell & 1`,见 AGENTS),不会碰 S 的其余位。
;
; 它**能**配轮转式字节级驱动:每轮读走最低位、丢掉,再 ror —— 正好对上 A、B
; 被吃掉这件事。`clb_add_nc` 就是这么拼的(4 cell、693 字符、65536 组合全过)。
; `raw_add` 那版保留 A、B(6 cell),是留给"想把操作数要回去"的调用者的。
; Macro clb_bit_add_nc
; %1: CellBit A
; %2: CellBit B
; %3: CellBit Ci
; %4: CellBit S
;
; 四个 cell 都有意义(A、B 是输入,进位进->出,S 是输出),没有草稿 cell ——
; 所以不带 `raw`(SPEC:`raw` 指"草稿 cell 要调用者传");又因为 A、B 被吃掉,
; 所以带 `clb`(clobber)。`bit_raw_and`/`bit_raw_add` 那种保留输入的变体才需要草稿 cell。
; 表:S = A^B^Ci,进位 = AB|ACi|BCi;A、B 结束为 0,S 和进位只落在 0/1 上。
%synth clb_bit_add_nc_synth
params 4
stat 0 8 8 4 8 4 4 12 0 8 8 4 8 4 4 12
mode tape
%endsynth
%macro clb_bit_add_nc 4
.tp %4
set_0
clb_bit_add_nc_synth %1 %2 %3 %4
%endmacro
; Macro clb_add_nc
; %1: Cell A
; %2: Cell B
; %3: Cell S
; %4: CellBit Ci
;
; 字节级加法:S = A + B + Ci。进位 cell 是**真正的进位输入** —— 调用者决定初值
; (普通加法给 0;多字节串起来时给上一字节的进位输出),宏只负责把它变成进位输出。
; 每轮把 A、B 的当前最低位喂进 clb_bit_add_nc,和写进 S 的最低位,
; 然后 A、B、S 各 ror 一格。A、B 结束为 0。
%macro clb_add_nc 4
.anchor before_clb_add_nc
%rep _CELL_SIZE
clb_bit_add_nc %1 %2 %4 %3
.tp %1
ror
.tp %2
ror
.tp %3
ror
%endrep
.tp before_clb_add_nc
%endmacro