SPEC/AGENTS: why the bit level wrappers must keep their set_0

A question that came up: the %synth stat tables already carry every parameter's
initial value, so shouldn't bit_raw_* stop clearing its output cells first?

No -- and the reason is in the searcher, not in the table.
compress_stat_space() drops the bits whose initial value never changes the
target and canonicalises them to 0 in the start state.  The bit slice tables
are periodic in exactly those bits (the old hand written implementation cleared
them before working, and the derived table records that), so the search never
had to handle "the output cell already holds a 1": the block is only proven for
a cleared cell.  The wrapper's set_0 is what puts the call site inside the
block's domain.

Measured by removing all nine of them (seven wrappers plus clb_bit_or and
clb_bit_xor):

    raw_add / raw_and / raw_or / raw_xor ...   still 65536/65536 ok
    clb_mul         62064/65536 wrong
    clb_divmod      14742/16384 wrong
    clb_digits       2048/2048  wrong (one of them HANGs)

The rotation driven macros survive because they write the result at bit 0 and
rotate it away immediately, so the output LSB is 0 anyway; mul and divmod copy
accumulators around and really do feed a 1 back into the slice.  Dropping the
set_0 would save three characters per cleared bit, which is exactly why someone
will try it again.

Reverted the experiment and wrote it down: SPEC gains 3.6 (compression
semantics and this trap, with the numbers), 6.1 gets the pointer line, and
AGENTS gets an entry in the "already confirmed dead ends" list.
This commit is contained in:
dsh committed 2026-11-07 23:31:23 +08:00
1 parent 3f4149c9d0
commit 66ed7bc263
2 files changed
+33

No files matched your search

+7
View File
@@ -500,6 +500,13 @@ W 轮之后 `(PH:PL)` 就是积;`PL`(原来的 Q)被移空,所以 **MUL
"读走最低位 → 丢掉 → `ror`",吃掉操作数完全没问题:`clb_add_nc`(4 cell)就是这么跑的,
65536 组合全过。
* 字节级(2^16 项真值表)交给 `%synth`:搜索空间不可行。
* **删掉位级包装宏里的 `set_0`**(想法是"`%synth` 的 `stat` 已经把初值算进去了,
输出位不用先清"):**错的**。`stat` 表对输出位的初值是周期的(旧实现先清再算),
而 `compress_stat_space()` 把"初值不影响终值"的位从搜索初态里清成 0 —— 搜出来的块
只保证**初值为 0** 的调用点。2026-11 实测删掉 9 处 `set_0`:`raw_add`/`raw_and`/
`raw_or`/`raw_xor` 照样全过(轮转驱动每轮把结果写在 bit0 上就转走,输出位的 LSB
一直是 0),但 `clb_mul` 62064/65536 错、`clb_divmod` 14742/16384 错、
`clb_digits` 2048/2048 错(其中一个 HANG)。见 SPEC §3.6。
## 命名与注释格式(照 `SPEC.md`,别自己发明)
+26
View File
@@ -161,6 +161,28 @@ R8Forge 唯一的语言扩展:在汇编期现场搜出一段最短的块序列
`REG+0→REG+1`),位宽一改就静默算错;现在是 `%for k L-1` 生成,**位数、链长、
常数构建的尾部转数全部跟着 `_CELL_SIZE` 走**。
### 3.6 "表里那位不影响答案" = "搜索器不保证那位的初值"
`compress_stat_space()` 会把**初值不影响终值**的位从搜索的初态里清成 0:只要
`target[j] == target[j ^ (1<<bit)]` 对所有 j 成立,搜索器就认为那一位"不是输入",把
它从状态空间里删掉。于是搜出来的块**只对"那一位初值是 0"的调用点成立** —— 不是
"块自己会处理任意初值"。
`bit_raw_*` / `clb_bit_*` 的**输出位**正好是这种位:它们原来的手写实现会先 `set_0`
再算,`r8forge_derive` 跑出来的表对它们就是周期的(`clb_bit_add_nc` 是
`0 8 8 4 8 4 4 12` ×2、`bit_raw_add` 是 `0 9 10 19 12 21 22 31` ×8、`bit_raw_and` 是
`0 1 2 7` ×4)。所以**包装宏里那个 `set_0` 不是冗余动作,是这套模型的必要前提**:
它把输出位放进块被验证过的定义域(初值 0)。
删掉会坏,而且坏得不均匀(2026-11 实测):`raw_add` / `raw_and` / `raw_or` / `raw_xor`
这些轮转驱动的宏**照样全过** —— 它们每轮把结果写在 bit0 上、紧接着 `ror` 就转走,
输出位的 LSB 永远是 0,`set_0` 只是在重做一次;但 `clb_mul`(62064/65536 错)、
`clb_divmod`(14742/16384 错)、`clb_digits`(2048/2048 错,其中一个直接 HANG)当场就挂
—— 它们的累加器是从别处 `copy` 来的,真的会带着 1 进切片。
要省掉那 3 个字符,只能让搜索器不压缩这些位(搜索空间 ×2^3、块还会更长),不划算。
---
## 4. 标准库 `asm/` —— 总则
@@ -299,6 +321,10 @@ endloop C
所有位级宏都**只碰低 1 位**,喂进来的输入必须是 0/1;`clb_bit_add_nc`/`clb_bit_inc_nc`
的进位 cell 是"进→出"的同一个 cell。
包装宏在调用合成块之前先对**输出位**(`S`、`Co`、`ans`、`tmp`/`buf`)`set_0` —— 那不是
冗余动作:这些表对输出位的初值是周期的,搜索器据此把它们当成"不是输入"、只保证初值为 0
的调用点。删掉会让 `clb_mul`/`clb_divmod`/`clb_digits` 当场挂掉,详见 §3.6。
### 6.2 为什么 `clb_bit_and` 是手写的
三个参数、没有草稿位,而"与"需要**同时**保住两个输入,只能靠嵌套 `biz`(一层一个条件)。