From 557e2a1c38df0eff6e474b89e154e30dcfb1f806 Mon Sep 17 00:00:00 2001 From: dsh Date: Sat, 7 Nov 2026 09:16:44 +0800 Subject: [PATCH] In place: the ones' complement takes one cell and 17 characters Asked whether two's complement can be done in place. The negate cannot, but the inversion half can and is absurdly cheap, so it is a macro now: clb_not_ip A A = ~A, one cell, 17 characters, 256/256 `%rep 8 { flb; ror }` -- flip the low bit, rotate the next one down, eight times puts the byte back where it started. The same result through clb_neg_nc with its carry at 0 costs 467 characters, because that path runs a synthesised slice once per bit. The in place two's complement itself: tried four tables and none of them exist. A <- ~A ^ Ci A <- A ^ Ci A <- A ^ B ^ Ci A <- A ^ ~B ^ Ci no program up to 8 layers (all four) Two separate reasons, both worth keeping: * Synthesis cannot: a block has exactly one condition cell. In place increment has to tell (A=1, Ci=0) -> (1,0) from (A=1, Ci=1) -> (0,1); A is the same in both and only Ci differs, and one condition cannot look at two cells. * Hand writing cannot either (2 cells): `[ ... ]` is a while loop and the only one shot conditional this ISA has, and it eats the bit it tests. In place negate wants "flip the low bit every round, then decide the carry from it", so both cells hold live bits and the test destroys one. So a negate still has to land in a separate S (clb_neg_nc, 3 cells); one cell buys the ones' complement only. byte_op_test is ten operations now, AGENTS.md and README.md carry the distinction and the two reasons. --- AGENTS.md | 22 ++++++++++++++++++++++ README.md | 6 ++++++ asm/not_ip.r8asm | 28 ++++++++++++++++++++++++++++ drv/not_ip_byte.r8asm | 12 ++++++++++++ tests/byte_op_test.cc | 24 ++++++++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 asm/not_ip.r8asm create mode 100644 drv/not_ip_byte.r8asm diff --git a/AGENTS.md b/AGENTS.md index 628d11b..e3a27b6 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -123,6 +123,7 @@ void rot8_rnz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 1) { ... } } | **`clb_neg_nc`(~a+进位,3 cell)** | `drv/raw_neg_nc_byte.r8asm` | **468 字符** | **256/256**(取负) | | 同上,进位 0 | `drv/raw_not_nc_byte.r8asm` | **467 字符** | **256/256**(反码 `~a`) | | **`clb_sub_nc`(a-b,4 cell)** | `drv/raw_sub_nc_byte.r8asm` | **725 字符** | **65536/65536** | +| **`clb_not_ip`(~a,原地,1 cell)** | `drv/not_ip_byte.r8asm` | **17 字符** | **256/256** | | `asm/fibonacci.r8asm` | — | 8761 字符 | 跑出 `1 1 2 3 5 8 13 21 34 55`,和文件注释一致 | `raw_*` 保留操作数(A、B 原样还回来),`clb_*_nc` 把 A、B 吃干净 —— 所以后者能省掉 @@ -150,6 +151,23 @@ void rot8_rnz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 1) { ... } } 报 `no program up to 8 layers`。原因是模型只会"牺牲一个 cell → 条件翻转别的 cell", A=0 时整块跳过、S 只能留在 0,而取反要求 A=0 时 S=1 —— "无条件翻转"表达不出来。 +### 原地(不额外占 cell)能做到哪一步 + +**能:原地取反(反码)**。`clb_not_ip A` 只要一个 cell、17 字符: +`%rep _CELL_SIZE { flb; ror }` —— `flb` 翻最低位、`ror` 把下一位转下来,转八次正好回原位。 +同样结果走 `clb_neg_nc`(Ci=0)要 467 字符,因为那条路每 bit 都跑一遍合成切片。 + +**不能:原地取补码**。四个原地表(`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)→(1,0)` 与 + `(A=1,Ci=1)→(0,1)` —— A 相同、只有 Ci 不同,一个条件分不开。 +* **手写也做不到(2 cell)**:`[ ... ]` 是这个 ISA 里唯一的一次性条件手段,而它必然 + 消耗被测的那一位;原地取负要求"每轮无条件翻 A 的最低位,再按 Ci 定进位",两个 cell 的位 + 都是活的,一测就被吃掉。 + +所以补码取负只能落在一个单独的 S 上(`clb_neg_nc`,3 cell);单 cell 的只有反码。 + 顺带纠正一个流传下来的说法:`raw_add` **不**吃掉操作数。实测 `5+3 → A=5 B=3 S=8`, `255+255 → A=255 B=255 S=254 Co=1`。`asm/fibonacci.r8asm` 里"每圈都要重新复印"的注释 是过度保守(它确实复印了,但不复印也对)。 @@ -209,6 +227,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 字符)。 * **让切片自己在输入上取反**(表 `S=~A^Ci`、进位 `~A&Ci`,3 参数):`no program up to 8 layers`。 模型只会"牺牲一个 cell → 条件翻转别的 cell",输入为 0 时整块被跳过、S 只能保持 0, 表达不出"无条件翻转"。取反只能放在切片外面(一条 `flb`)。 diff --git a/README.md b/README.md index 6470f83..ef83f30 100644 --- a/README.md +++ b/README.md @@ -238,11 +238,17 @@ byte level library ok | `clb_inc_nc A S Ci` | `S = A + Ci`(`Ci=1` 就是自增) | 3 | 430 | | `clb_neg_nc A S Ci` | `S = ~A + Ci`(`Ci=1` → `-A`,`Ci=0` → `~A`) | 3 | 468 | | `clb_sub_nc A B S Ci` | `S = A + ~B + Ci`(`Ci=1` → `A-B`) | 4 | 725 | +| `clb_not_ip A` | `A = ~A`(反码,**原地**,不占额外 cell) | 1 | **17** | **进位 cell 是真正的进位输入**:宏不动它的初值,由调用者给。所以可以串 —— 上一字节的 进位输出直接接到下一字节的进位输入,多字节加法/减法/取负都能这么拼。 `clb_sub_nc` 的进位输出是"没有借位"(`A >= B` 时为 1),顺带就是无符号比较。 +反码可以原地做(`flb; ror` 重复八次就够,17 字符);**补码不行** —— 2 cell 的原地取负 +要"每轮无条件翻最低位、再按进位决定下一个进位",而 ROT-8 里唯一的一次性条件 `[ ... ]` +必然吃掉被测的那一位,两个 cell 的位都是活的。所以补码取负的结果只能落在单独的 S 上 +(`clb_neg_nc`,3 cell)。 + `asm/fibonacci.r8asm` 也是这条路上的:8761 字符,跑出 `1 1 2 3 5 8 13 21 34 55`。 ### 一个坑:`biz`/`rnz` 看的是最低位 diff --git a/asm/not_ip.r8asm b/asm/not_ip.r8asm new file mode 100644 index 0000000..c6e6164 --- /dev/null +++ b/asm/not_ip.r8asm @@ -0,0 +1,28 @@ +%include "asm/lib.r8asm" + +; 原地取反(反码):A <- ~A,只要一个 cell,16 条指令。 +; +; 做法就是"翻最低位 + 转一格"重复八次:flb 翻当前位,ror 把下一位转到最低位。 +; 转八次正好回到原位,所以别的 cell 一位都不用。 +; +; 注意这是**反码**(逐位取反),不是补码。补码要再加 1,而"原地加 1"需要进位链, +; 那个做不了 —— 见 AGENTS.md: +; * 合成的块只有一个条件 cell,原地写回却要同时看 A 和进位; +; * `[ ... ]` 是一次性条件唯一的手段,而它必然消耗被测的那一位,两个 cell 的位都是活的。 +; 所以补码取负用 clb_neg_nc(结果在 S,3 个 cell)。 + +; Macro clb_not_ip +; %1: Cell A +; +; A <- ~A(反码),原地,不需要额外的 cell。 +%macro clb_not_ip 1 +.anchor before_clb_not_ip + +.tp %1 +%rep _CELL_SIZE + flb + ror +%endrep + +.tp before_clb_not_ip +%endmacro diff --git a/drv/not_ip_byte.r8asm b/drv/not_ip_byte.r8asm new file mode 100644 index 0000000..232af42 --- /dev/null +++ b/drv/not_ip_byte.r8asm @@ -0,0 +1,12 @@ +%include "asm/not_ip.r8asm" + +; clb_not_ip 的驱动:A 原地变成 ~A。 +; +; r8forge_derive drv/not_ip_byte.r8asm -2 0=5 # 得 250 +; +; out 只有 A。 +.var A 0 + +clb_not_ip A + +output A diff --git a/tests/byte_op_test.cc b/tests/byte_op_test.cc index 37c86fb..2a6e64c 100644 --- a/tests/byte_op_test.cc +++ b/tests/byte_op_test.cc @@ -316,6 +316,29 @@ int test_not_nc(const std::string &file) { return bad; } +// clb_not_ip: the ones' complement done in place, one cell, no carry. +int test_not_ip(const std::string &file) { + const std::string tape = assemble(file); + std::cout << "clb_not_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 inv = (~a) & 0xff; + const bool ok = + m.finished && m.out.size() == 1 && m.out[0] == inv; + if (!ok) { + bad++; + if (bad <= 3) + report("clb_not_ip", a, 0, m, std::to_string(inv)); + } + } + std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok") + << (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n"; + return bad; +} + } // namespace int main(int argc, char **argv) { @@ -330,6 +353,7 @@ int main(int argc, char **argv) { test_inc_nc(drv + "raw_inc_nc_byte.r8asm") + 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_sub_nc(drv + "raw_sub_nc_byte.r8asm"); std::cout << (bad ? "FAILED\n" : "byte level library ok\n"); return bad ? 1 : 0;