From 2f79898e3d7bdf05cfbda4eecc3af91cdad58f5d Mon Sep 17 00:00:00 2001 From: dsh Date: Sat, 7 Nov 2026 09:40:05 +0800 Subject: [PATCH] In place increment in two cells, 74 characters, by counting rotations in the scratch cell's bits I said a two cell in place increment was impossible. It is not. The version in asm/inc_ip.r8asm does A = A + 1 with A and one scratch cell, 74 characters, 256/256 exhaustive, and there is a two cell in place negate on top of it: clb_not_ip A A = ~A 1 cell 17 chars 256/256 clb_inc_ip A T A = A + 1 2 cells 74 chars 256/256 clb_neg_ip A T A = -A 2 cells 90 chars 256/256 against clb_inc_nc (3 cells, 430) and clb_neg_nc (3 cells, 448). Five times shorter, one cell less. Both of my earlier impossibility arguments were about a *specific* algorithm, the per bit carry ripple, and I stated them as if they covered the problem. The one worth remembering is the synthesis one, which is still true: a synthesised block has exactly one condition cell, so it cannot tell (A=1, Ci=0) from (A=1, Ci=1), and all four in place tables answer "no program up to 8 layers". The hand written argument was simply wrong. The algorithm that works does not test the carry per bit at all: 1. `[ ... ]` already means "while the low bit is set", so clearing the run of low ones is a loop that falls out on the first zero (flb + ror); 2. flip that zero to one -- skip it when the input is all ones, which is exactly the wrap-around case; 3. rotate the rest of the way. What is left is "how many rotations remain", with only two cells. The counter goes into the scratch cell's bits: T starts as 0xFF, and every rotation does set_0 then ror, which eats one 1 -- the length of the run of ones is the number of rotations left. Three characters per step, and a full turn counts to eight. The wrap case falls out for free: when the input was 0xFF the counter is spent exactly then, so step 2 is skipped and the answer is 0. Two smaller notes. The dp rule from the last commit is what these macros had to respect -- every conditional body returns to its own condition cell -- and `[ ... ]` consuming its condition is only a problem when the condition is data; here it is the counter. And I mangled asm/inc_ip.r8asm once with a PowerShell Get-Content / Set-Content round trip (the trap AGENTS.md warns about) and had to rewrite it with the editor; no other file was touched. byte_op_test is twelve operations now, still exhaustive, still under a second. --- AGENTS.md | 62 +++++++++++++++++++++++---------- README.md | 24 +++++++++---- asm/inc_ip.r8asm | 81 +++++++++++++++++++++++++++++++++++++++++++ drv/inc_ip_byte.r8asm | 13 +++++++ drv/neg_ip_byte.r8asm | 13 +++++++ tests/byte_op_test.cc | 47 +++++++++++++++++++++++++ 6 files changed, 215 insertions(+), 25 deletions(-) create mode 100644 asm/inc_ip.r8asm create mode 100644 drv/inc_ip_byte.r8asm create mode 100644 drv/neg_ip_byte.r8asm diff --git a/AGENTS.md b/AGENTS.md index 62c7190..0503002 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -59,7 +59,7 @@ r8forge asm/test.r8asm # 应 exit 0,1666 字符,7 r8forge_synth_test examples/full_add.r8asm # all eight combinations ok r8forge verify_clb_bit_add_nc.r8asm # 4 cell 加法切片(进位进->出) r8forge verify_clb_bit_inc_nc.r8asm # 3 cell 自增切片,进位同样进->出 -r8forge_byte_op_test # 字节级库:9 个操作穷举,0.7 秒 +r8forge_byte_op_test # 字节级库:12 个操作穷举,0.8 秒 r8forge_macro_docs_test # asm/ 里每个宏的注释格式 ``` @@ -130,6 +130,8 @@ void rot8_rnz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 1) { ... } } | 同上,进位 0 | `drv/raw_not_nc_byte.r8asm` | **447 字符** | **256/256**(反码 `~a`) | | **`clb_sub_nc`(a-b,4 cell)** | `drv/raw_sub_nc_byte.r8asm` | **707 字符** | **65536/65536** | | **`clb_not_ip`(~a,原地,1 cell)** | `drv/not_ip_byte.r8asm` | **17 字符** | **256/256** | +| **`clb_inc_ip`(a+1,原地,2 cell)** | `drv/inc_ip_byte.r8asm` | **74 字符** | **256/256** | +| **`clb_neg_ip`(-a,原地,2 cell)** | `drv/neg_ip_byte.r8asm` | **90 字符** | **256/256** | | `asm/fibonacci.r8asm` | — | 8761 字符 | 跑出 `1 1 2 3 5 8 13 21 34 55`,和文件注释一致 | `raw_*` 保留操作数(A、B 原样还回来),`clb_*_nc` 把 A、B 吃干净 —— 所以后者能省掉 @@ -161,23 +163,39 @@ B 本来就会被加法吃掉,所以在原地取反它没有任何代价。 报 `no program up to 8 layers`。原因是模型只会"牺牲一个 cell → 条件翻转别的 cell", A=0 时整块跳过、S 只能留在 0,而取反要求 A=0 时 S=1 —— "无条件翻转"表达不出来。 -### 原地(不额外占 cell)能做到哪一步 +### 原地(结果回到原来的 cell) -**能:原地取反(反码)**。`clb_not_ip A` 只要一个 cell、17 字符: -`%rep _CELL_SIZE { flb; ror }` —— `flb` 翻最低位、`ror` 把下一位转下来,转八次正好回原位。 -`clb_neg_nc` / `clb_sub_nc` 就是拿它当第一步(见上表)。 +| 宏 | 干什么 | cell | 纸带 | 穷举 | +|---|---|---|---|---| +| `clb_not_ip A` | `A = ~A`(反码) | 1 | **17** | 256/256 | +| `clb_inc_ip A T` | `A = A + 1` | 2 | **74** | 256/256 | +| `clb_neg_ip A T` | `A = -A`(补码取负) | 2 | **90** | 256/256 | -**不能:原地补码**。`-A = ~A + 1` 的前半段(取反)确实原地就够,卡在 `+1`:**2 cell 的原地 -自增做不了**。四个原地表(`A←~A^Ci`、`A←A^Ci`、`A←A^B^Ci`、`A←A^~B^Ci`)全部报 -`no program up to 8 layers`,手写也不行,两条独立的原因: +对比写回 S 的切片版:`clb_inc_nc` 430(3 cell)、`clb_neg_nc` 448(3 cell)—— +**原地版短 5 倍、少一个 cell**。`clb_neg_ip` 就是 `clb_not_ip` + `clb_inc_ip`。 -* **合成做不到**:每个块只有**一个**条件 cell。原地自增要区分 `(A=1,Ci=0)→(1,0)` 与 - `(A=1,Ci=1)→(0,1)` —— A 相同、只有 Ci 不同,一个条件分不开。 -* **手写也做不到(2 cell)**:`[ ... ]` 是这个 ISA 里唯一的一次性条件手段,而它必然 - 消耗被测的那一位;原地自增要"若进位则翻本位,再把进位更新成本位的旧值",两个 cell 的位 - 都是活的,一测就被吃掉。 +**"原地补码不可能"这个结论是错的,这里记一下它是怎么错的**(别再复现同样的推理): -所以补码取负只能把"和"落在一个单独的 S 上(`clb_neg_nc`,3 cell);单 cell 买到的只有反码。 +* `%synth` 的四个原地表(`A←~A^Ci`、`A←A^Ci`、`A←A^B^Ci`、`A←A^~B^Ci`)确实全部报 + `no program up to 8 layers`,原因是**一个合成块只有一个条件 cell**,区分不了 + "A=1 且 Ci=0" 和 "A=1 且 Ci=1"。这一步是实测,没问题。 +* 错在把它当成了整条路的结论。手写**不一定要照逐位进位链写**。 + +真正可行的算法换个思路,不去逐位测进位: + +1. 用 `[ ... ]` 的自然语义(最低位非零就继续)把**最低位连续的 1 清掉**(`flb` + `ror`), + 遇到第一个 0 时退出循环; +2. 那个 0 翻成 1 —— 输入是全 1(0xFF)时跳过,正好就是回绕那一格; +3. 剩下的位置只旋转,转满八格为止。 + +卡点变成"还要转几格",而 cell 只有两个。解法是**把计数塞进 T 的位里**:T 先做成 0xFF, +每转一格就 `set_0` + `ror` 消掉一个 1 —— 一串 1 的长度就是剩余圈数(每步 3 字符的移位寄存器 +计数器,一圈正好数八格)。于是两个 cell 够用,而且比切片版短得多。 + +顺带纠正另一半:`[ ... ]` 的"跑了就必然消耗被测位"不是死路 —— 只要被测的是**计数器**, +不是数据。这条以前被写成"手写也做不到(2 cell)",错的。 + +原地取反仍然是最便宜的一个:`%rep _CELL_SIZE { flb; ror }`,一个 cell、17 字符。 顺带纠正一个流传下来的说法:`raw_add` **不**吃掉操作数。实测 `5+3 → A=5 B=3 S=8`, `255+255 → A=255 B=255 S=254 Co=1`。`asm/fibonacci.r8asm` 里"每圈都要重新复印"的注释 @@ -204,7 +222,7 @@ A=0 时整块跳过、S 只能留在 0,而取反要求 A=0 时 S=1 —— "无 1308 字符,65536 组合全对),但它是上面那个错误诊断的产物:既然一个 cell 能放一个字节、 位读得出来,就没必要为每个 bit 花一个 cell。R8Forge 之前也删过一次同样的东西 (交接文档原话:"8 位加法 25 个 cell,没有实用价值")。 -**要拼字节级运算就用 `raw_*` 或 `clb_*_nc`。** +**要拼字节级运算就用 `raw_*` / `clb_*_nc` / `clb_*_ip`。** ## %synth 相关 @@ -231,6 +249,8 @@ A=0 时整块跳过、S 只能留在 0,而取反要求 A=0 时 S=1 —— "无 | `clb_neg_nc`(3 cell 取负/取反) | 448 / 447 字符,各 256 组合全对(Ci=1 取负、Ci=0 反码) | | `clb_sub_nc`(4 cell 减法) | 707 字符,65536 组合全对;进位输出顺带是无符号比较(A >= B) | | `clb_not_ip`(1 cell 原地取反) | **17 字符**,256 组合全对;比走切片那条路快 26 倍 | +| `clb_inc_ip`(2 cell 原地自增) | **74 字符**,256 组合全对;切片版 `clb_inc_nc` 是 430 | +| `clb_neg_ip`(2 cell 原地取负) | **90 字符**,256 组合全对;切片版 `clb_neg_nc` 是 448 | | `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 项斐波那契,和注释一致 | @@ -239,10 +259,10 @@ A=0 时整块跳过、S 只能留在 0,而取反要求 A=0 时 S=1 —— "无 * **3 个 cell 做加法切片**:层数上限 5 和 8 都报 `no program up to N layers` (3 cell 的**自增/取负**切片是有解的,两个操作数塞不进 3 个 cell)。 -* **原地写回(结果回到被读的那个 cell)**:`A←~A^Ci`、`A←A^Ci`、`A←A^B^Ci`、`A←A^~B^Ci` - 四个表全部 `no program up to 8 layers`。合成侧原因:一个块只有一个条件 cell,区分不了 - "A=1 且 Ci=0"和"A=1 且 Ci=1"。手写侧原因:`[ ... ]` 必然消耗被测位,两个 cell 的位都是活的。 - **只有反码能原地做**(`clb_not_ip`,17 字符)。 +* **原地写回的 `%synth` 表**:`A←~A^Ci`、`A←A^Ci`、`A←A^B^Ci`、`A←A^~B^Ci` 四个表全部报 + `no program up to 8 layers`。原因是一个合成块只有一个条件 cell,区分不了 "A=1 且 Ci=0" + 和 "A=1 且 Ci=1"。**但手写可以**:换成"清连续 1 + 计数器旋转"的算法,2 cell 就够 + (`clb_inc_ip` 74 字符、`clb_neg_ip` 90 字符,见「原地」)。别把这条读成"手写也不行"。 * **让切片自己在输入上取反**(表 `S=~A^Ci`、进位 `~A&Ci`,3 参数):`no program up to 8 layers`。 模型只会"牺牲一个 cell → 条件翻转别的 cell",输入为 0 时整块被跳过、S 只能保持 0, 表达不出"无条件翻转"。取反只能放在切片外面(一条 `flb`)。 @@ -285,12 +305,16 @@ SPEC 第 136-150 行的标准宏前缀: | `clb_bit_inc_nc`(3 cell) | 同样:A 被吃掉,进位进→出,没有草稿 cell | | `clb_add_nc`(4 cell)/ `clb_inc_nc`(3 cell) | 字节级版本,同样吃 A、B | | `clb_neg_nc`(3 cell)/ `clb_sub_nc`(4 cell) | 补码取负/减法,同样吃 A(和 B) | +| `clb_not_ip` / `clb_inc_ip` / `clb_neg_ip` | 同上再加 `_ip`(in place):结果回到原来的 cell,只借一个草稿 cell | | `clb_bit_mov` | 位级搬运,并且把源吃掉 | | `bit_copy` / `copy` | 位级/字节级拷贝,缓冲由调用者传(历史命名,少了 `raw`) | | `set_0` / `set_1` / `erase` / `rol` / `step` / `back` / `output` | 基础操作,不带前缀 | `%synth` 块名 = `<宏名>_synth`,包装的 `%macro` 用不带 `_synth` 的名字。 +**后缀不是前缀**:`_nc` 是手写库留下的历史命名,`_ip` 是本仓库加的(in place,结果写回原来的 +cell)。选名字时先按 SPEC 的三个前缀定位,再挑后缀。 + 宏里的注释: ```asm diff --git a/README.md b/README.md index 80808ca..4046505 100644 --- a/README.md +++ b/README.md @@ -239,18 +239,30 @@ byte level library ok | `clb_neg_nc A S Ci` | `S = ~A + Ci`(`Ci=1` → `-A`,`Ci=0` → `~A`) | 3 | 448 | | `clb_sub_nc A B S Ci` | `S = A + ~B + Ci`(`Ci=1` → `A-B`) | 4 | 707 | | `clb_not_ip A` | `A = ~A`(反码,**原地**,不占额外 cell) | 1 | **17** | +| `clb_inc_ip A T` | `A = A + 1`(**原地**,T 是草稿) | 2 | **74** | +| `clb_neg_ip A T` | `A = -A`(**原地**补码取负) | 2 | **90** | **进位 cell 是真正的进位输入**:宏不动它的初值,由调用者给。所以可以串 —— 上一字节的 进位输出直接接到下一字节的进位输入,多字节加法/减法/取负都能这么拼。 `clb_sub_nc` 的进位输出是"没有借位"(`A >= B` 时为 1),顺带就是无符号比较。 -补码那两个宏是**先原地取反、再走加法/自增**:取反整个字节一次(`clb_not_ip`)比"每 bit -取反一次"短 20 来个字符,而且 B 本来就会被加法吃掉,取反它没有代价。 +补码那两个 `_nc` 宏是**先原地取反、再走加法/自增**:取反整个字节一次(`clb_not_ip`)比 +"每 bit 取反一次"短 20 来个字符,而且 B 本来就会被加法吃掉,取反它没有代价。 -**原地补码做不到**:`~A` 那半步原地就够了,卡在 `+1` —— 2 个 cell 的原地自增不成立。 -`[ ... ]` 是 ROT-8 里唯一的一次性条件,而它必然吃掉被测的那一位;原地自增要"若进位则翻 -本位、再把进位更新成本位的旧值",两个 cell 的位都是活的。所以补码取负的结果只能落在另外 -一个 S 上(`clb_neg_nc`,3 cell),单 cell 买到的只有反码。 +### 原地取补码:两个 cell,90 字符 + +`clb_neg_ip A T` 直接把 `A` 变成 `-A`,不需要单独的 S —— 比 `clb_neg_nc`(3 cell、448 字符) +**少一个 cell、短 5 倍**。`clb_inc_ip` 是它的核心: +`clb_inc_ip A T` 74 字符,`clb_inc_nc` 是 430。 + +做法不是逐位测进位(那在只有两个 cell 时确实不行),而是: + +1. 用 `[ ... ]` 的自然语义把**最低位连续的 1 清掉**(`flb` + `ror`),遇到第一个 0 退出; +2. 那个 0 翻成 1(全 1 输入时跳过,正好是回绕那一格); +3. 剩下的位置只旋转。 + +"还要转几格"用 **T 的位**计数:T 先做 0xFF,每转一格 `set_0` + `ror` 消掉一个 1, +一串 1 的长度就是剩余圈数。于是两个 cell 就够,而且没有"无条件翻转"那种表达不出的动作。 `asm/fibonacci.r8asm` 也是这条路上的:8761 字符,跑出 `1 1 2 3 5 8 13 21 34 55`。 diff --git a/asm/inc_ip.r8asm b/asm/inc_ip.r8asm new file mode 100644 index 0000000..80bf996 --- /dev/null +++ b/asm/inc_ip.r8asm @@ -0,0 +1,81 @@ +%include "asm/not_ip.r8asm" + +; 原地自增:A <- A + 1,结果留在 A 里,只要两个 cell(A 和草稿 T)。 +; +; 关键是把"还要转多少格"这个计数塞进 T 的低位那一串 1 里: +; T 先做成 0xFF,每转一格就 `set_0` + `ror` 消掉一个 1 —— 一串 1 的长度就是剩余圈数。 +; 于是不需要第三个 cell,也不需要"无条件翻转"(那在 ROT-8 里表达不出来)。 +; +; 三步: +; 1. 最低位连续的 1 逐个清掉(边清边转,同时消计数器);遇到第一个 0 停下; +; 2. 那个 0 翻成 1(输入是 0xFF 时计数器正好是 0,这一步跳过 —— 结果就是 0); +; 3. 剩下的位置只旋转,转到整圈走完为止。 +; 三段合起来 A 正好转八格,所以位置对得上。 + +; Macro clb_inc_ip +; %1: Cell A +; %2: Cell T +; +; A <- A + 1,原地。T 是草稿 cell,只要求原来是干净的;结束时是 0。 +%macro clb_inc_ip 2 +.anchor before_clb_inc_ip + +; 计数器:T = 0xFF +.tp %2 +%rep _CELL_SIZE + set_1 + ror +%endrep + +; 1) 清掉最低位连续的 1 +.tp %1 +biz + flb + ror + .tp %2 + set_0 + ror + .tp %1 +rnz + +; 2) 第一个 0 翻成 1(0xFF 时跳过) +.tp %2 +biz + .tp %1 + flb + ror + .tp %2 + set_0 +rnz + +; 计数器补一次转位(上一步只消了 1,没有转) +.tp %2 +ror + +; 3) 剩下的位置只旋转 +biz + .tp %1 + ror + .tp %2 + set_0 + ror +rnz + +.tp before_clb_inc_ip +%endmacro + +; Macro clb_neg_ip +; %1: Cell A +; %2: Cell T +; +; 原地取补码:A <- -A,就是"原地取反 + 原地自增",两个 cell 就够。 +; 比 clb_neg_nc 少一个 cell(不需要单独的 S),也短得多。T 是草稿 cell, +; 只要求原来是干净的。 +%macro clb_neg_ip 2 +.anchor before_clb_neg_ip + +clb_not_ip %1 +clb_inc_ip %1 %2 + +.tp before_clb_neg_ip +%endmacro diff --git a/drv/inc_ip_byte.r8asm b/drv/inc_ip_byte.r8asm new file mode 100644 index 0000000..511c032 --- /dev/null +++ b/drv/inc_ip_byte.r8asm @@ -0,0 +1,13 @@ +%include "asm/inc_ip.r8asm" + +; clb_inc_ip 的驱动:A 原地 +1。 +; +; r8forge_derive drv/inc_ip_byte.r8asm -1 0=255 +; +; out 只有 A。 +.var A 0 +.var T 1 + +clb_inc_ip A T + +output A diff --git a/drv/neg_ip_byte.r8asm b/drv/neg_ip_byte.r8asm new file mode 100644 index 0000000..0f7725d --- /dev/null +++ b/drv/neg_ip_byte.r8asm @@ -0,0 +1,13 @@ +%include "asm/inc_ip.r8asm" + +; clb_neg_ip 的驱动:A 原地变成 -A。 +; +; r8forge_derive drv/neg_ip_byte.r8asm -1 0=1 # 得 255 +; +; out 只有 A。 +.var A 0 +.var T 1 + +clb_neg_ip A T + +output A diff --git a/tests/byte_op_test.cc b/tests/byte_op_test.cc index 2a6e64c..f90bcb0 100644 --- a/tests/byte_op_test.cc +++ b/tests/byte_op_test.cc @@ -339,6 +339,51 @@ int test_not_ip(const std::string &file) { return bad; } +// clb_inc_ip: A = A + 1, both in place and in two cells. +int test_inc_ip(const std::string &file) { + const std::string tape = assemble(file); + std::cout << "clb_inc_ip: " << tape.size() << " characters\n"; + int bad = 0; + for (int a = 0; a < 256; a++) { + machine m; + m.cells[0] = uint8_t(a); // A + m.run(tape, 100'000); + const int want = (a + 1) & 0xff; + const bool ok = m.finished && m.out.size() == 1 && m.out[0] == want && + m.cells[1] == 0; // the counter cell is spent + if (!ok) { + bad++; + if (bad <= 3) + report("clb_inc_ip", a, 0, m, std::to_string(want)); + } + } + std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok") + << (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n"; + return bad; +} + +// clb_neg_ip: A = -A, in place, two cells. +int test_neg_ip(const std::string &file) { + const std::string tape = assemble(file); + std::cout << "clb_neg_ip: " << tape.size() << " characters\n"; + int bad = 0; + for (int a = 0; a < 256; a++) { + machine m; + m.cells[0] = uint8_t(a); // A + m.run(tape, 100'000); + const int want = (256 - a) & 0xff; + const bool ok = m.finished && m.out.size() == 1 && m.out[0] == want; + if (!ok) { + bad++; + if (bad <= 3) + report("clb_neg_ip", a, 0, m, std::to_string(want)); + } + } + std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok") + << (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n"; + return bad; +} + } // namespace int main(int argc, char **argv) { @@ -354,6 +399,8 @@ int main(int argc, char **argv) { test_neg_nc(drv + "raw_neg_nc_byte.r8asm") + test_not_nc(drv + "raw_not_nc_byte.r8asm") + test_not_ip(drv + "not_ip_byte.r8asm") + + test_inc_ip(drv + "inc_ip_byte.r8asm") + + test_neg_ip(drv + "neg_ip_byte.r8asm") + test_sub_nc(drv + "raw_sub_nc_byte.r8asm"); std::cout << (bad ? "FAILED\n" : "byte level library ok\n"); return bad ? 1 : 0;