bit_raw_add_nc is now bit_add_nc, and here is a 3 cell increment slice

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.
This commit is contained in:
dsh committed 2026-11-07 08:53:25 +08:00
1 parent 87ab064121
commit adb3c8d71a
6 files changed
+70 -20

No files matched your search

+8 -5
View File
@@ -54,7 +54,8 @@ clang-cl /nologo /std:c++20 /EHsc /O2 /I include src/r8asm.cc src/synth.cc tests
```sh
r8forge asm/test.r8asm # 应 exit 0,1666 字符,7/7 命中缓存
r8forge_synth_test examples/full_add.r8asm # all eight combinations ok
r8forge verify_raw_add_nc.r8asm # 4 cell 加法切片驱动
r8forge verify_add_nc.r8asm # 4 cell 加法切片(进位进->出)
r8forge verify_inc_nc.r8asm # 3 cell 自增切片,进位同样进->出
r8forge_byte_op_test # 字节级库:65536×4 + 512 全过,0.5 秒
r8forge_macro_docs_test # asm/ 里每个宏的注释格式
```
@@ -137,7 +138,7 @@ void rot8_rnz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 1) { ... } }
### 另一位一 cell 的实现在这里被删过一次(别再加回来)
"每个 bit 一个 cell、用 `bit_raw_add_nc` 串成 8 位加法器"这条路能跑通(32 cell,
"每个 bit 一个 cell、用 `bit_add_nc` 串成 8 位加法器"这条路能跑通(32 cell,
1308 字符,65536 组合全对),但它是上面那个错误诊断的产物:既然一个 cell 能放一个字节、
位读得出来,就没必要为每个 bit 花一个 cell。R8Forge 之前也删过一次同样的东西
(交接文档原话:"8 位加法 25 个 cell,没有实用价值")。**要拼字节级运算就用 `raw_*`。**
@@ -160,7 +161,8 @@ void rot8_rnz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 1) { ... } }
| 东西 | 结论 |
|---|---|
| 库里的 7 个位运算块(and/or/xor/inc/add) | 合成版比手写短:`asm/test.r8asm` 1714 → 1666 字符 |
| `bit_raw_add_nc`(4 cell 忽略进位切片) | flat 代价 **27**,完整全加器切片是 38;8/8 组合正确 |
| `bit_add_nc`(4 cell 忽略进位切片) | flat 代价 **27**,完整全加器切片是 38;8/8 组合正确 |
| `bit_inc_nc`(3 cell 自增切片) | 表 `0 4 4 2 0 4 4 2`(3 参数)缓存里就有,4 blocks;纸带 54 字符,4/4 组合正确 |
| `bit_raw_add`(6 cell 全加器切片) | 保留 A、B;字节级 `raw_add` 1647 字符,65536 组合全对 |
| `raw_and` / `raw_or` / `raw_xor` / `raw_inc` | 639 / 575 / 463 / 1214 字符,各 65536(inc 512)全对 |
| `asm/fibonacci.r8asm` | 8761 字符,跑出 10 项斐波那契,和注释一致 |
@@ -201,7 +203,8 @@ SPEC 第 136-150 行的标准宏前缀:
|---|---|
| `raw_add` / `raw_and` / `raw_or` / `raw_xor` / `raw_inc` | 字节级,且草稿 cell 由调用者传 |
| `bit_raw_add`(6 cell) | 位级,且 `%6: buf` 是真正的草稿:内容无关,结束是垃圾 |
| `bit_raw_add_nc`(4 cell) | **四个 cell 都有意义**,没有草稿 cell:第 3 个是"进→出"的进位(`A=0 B=0 进位入=1` 时 `S=1`,实测过)。名字里的 `raw` 是历史遗留,按 SPEC 严格读不该有 —— 但"调用者得多给一个工作 cell"这个宽松读法又说得通,所以先不改名 |
| `bit_add_nc`(4 cell) | **四个 cell 都有意义**,没有草稿 cell:第 3 个是"进→出"的进位(`A=0 B=0 进位入=1` 时 `S=1`,实测过)。所以名字里没有 `raw` |
| `bit_inc_nc`(3 cell) | 同样:A 被吃掉,进位进→出,没有草稿 cell |
| `clb_bit_mov` | 位级搬运,并且把源吃掉 |
| `bit_copy` / `copy` | 位级/字节级拷贝,缓冲由调用者传(历史命名,少了 `raw`) |
| `set_0` / `set_1` / `erase` / `rol` / `step` / `back` / `output` | 基础操作,不带前缀 |
@@ -211,7 +214,7 @@ SPEC 第 136-150 行的标准宏前缀:
宏里的注释:
```asm
; Macro bit_raw_add_nc
; Macro bit_add_nc
; %1: CellBit A
; %2: CellBit B
```
+6 -4
View File
@@ -14,22 +14,24 @@
; 因为 A、B 被吃掉,它配不了轮转式的字节级驱动(那种驱动每轮还要读回操作数);
; 字节级的 `raw_add` 用的是保留 A、B 的 `bit_raw_add`(6 cell)。
; Macro bit_raw_add_nc
; Macro bit_add_nc
; %1: CellBit A
; %2: CellBit B
; %3: CellBit Ci
; %4: CellBit S
;
; 四个 cell 都有意义(A、B 是输入,进位进->出,S 是输出),没有草稿 cell ——
; 名字里不带 raw 就是为这个(SPEC:`raw` 指"草稿 cell 要调用者传")。
; 表:S = A^B^Ci,进位 = AB|ACi|BCi;A、B 结束为 0,S 和进位只落在 0/1 上。
%synth bit_raw_add_nc_synth
%synth bit_add_nc_synth
params 4
stat 0 8 8 4 8 4 4 12 0 8 8 4 8 4 4 12
metric flat
mode tape
%endsynth
%macro bit_raw_add_nc 4
%macro bit_add_nc 4
.tp %4
set_0
bit_raw_add_nc_synth %1 %2 %3 %4
bit_add_nc_synth %1 %2 %3 %4
%endmacro
+33
View File
@@ -0,0 +1,33 @@
%include "asm/lib.r8asm"
; 忽略独立进位的自增切片:一个操作数 + 一个"进->出"的进位 cell + 和。
;
; 和 bit_add_nc 的关系:自增就是"加一个 1",但 1 不用 cell 装,所以少一个参数。
; 切片占 3 个 cell:A、进位、S。A 被吃掉(结束为 0),进位就地在自己的 cell 里更新。
;
; 两条要知道的:
; 1. %synth 出来的程序只碰每个 cell 的低 K 位,高位不追踪 —— 这是个位级切片,
; 喂进来的 A、进位都必须是 0/1;
; 2. set_0 清的是最低位(`biz`/`rnz` 判 `cell & 1`,见 AGENTS),不会碰 S 的其余位。
;
; 同样因为 A 被吃掉,它配不了轮转式的字节级驱动;要字节级自增用 raw_inc
; (那是保留 A 的 bit_raw_inc,5 cell)。
; Macro bit_inc_nc
; %1: CellBit A
; %2: CellBit Ci
; %3: CellBit S
;
; 表:S = A^Ci,进位 = A&Ci;A 结束为 0。
%synth bit_inc_nc_synth
params 3
stat 0 4 4 2 0 4 4 2
metric flat
mode tape
%endsynth
%macro bit_inc_nc 3
.tp %3
set_0
bit_inc_nc_synth %1 %2 %3
%endmacro
+12
View File
@@ -0,0 +1,12 @@
%include "asm/raw_add_nc.r8asm"
; bit_add_nc 的单点验证:A、B、进位各喂一位,打出和与进位。
; 进位是"进->出"同一个 cell,所以 0=0 1=0 2=1 就是 0+0+1。
.var A 0
.var B 1
.var Ci 2
.var S 3
bit_add_nc A B Ci S
output S
output Ci
+11
View File
@@ -0,0 +1,11 @@
%include "asm/raw_inc_nc.r8asm"
; bit_inc_nc 的单点验证:A、进位各喂一位,打出和与进位输出。
; out 依次是 S、进位(进->出同一个 cell)。
.var A 0
.var Ci 1
.var S 2
bit_inc_nc A Ci S
output S
output Ci
-11
View File
@@ -1,11 +0,0 @@
%include "asm/raw_add_nc.r8asm"
; bit_raw_add_nc 的单点验证:A、B、进位各喂一位,打出和与进位。
.var A 0
.var B 1
.var Ci 2
.var S 3
bit_raw_add_nc A B Ci S
output S
output Ci