From a515e823d1762eafc50f5fa8cf0645803e4a2a2c Mon Sep 17 00:00:00 2001 From: dsh Date: Sat, 7 Nov 2026 17:18:02 +0800 Subject: [PATCH] clb_digits: integers become a four digit ASCII string clb_digits N REG S writes '2' '0' '4' '7' into REG..REG+3 for 2047 -- ASCII digits, most significant first, fixed width four, zero padded, which is what was asked for after the ASCII/order/source questions. divmod appears once, wrapped in a loop that runs four times. The first version dispatched each digit to its own cell with four branches and a one-hot flag handoff, and only the first branch ever fired; the digits themselves were right (checked by printing N after every division: 204, 20, 2, 0). Rather than keep chasing that, the dispatch is gone: each round shifts the digits already written one cell up (high to low, so nothing gets clobbered) and the new digit always goes to REG+0. Four rounds later REG+0..3 are thousands, hundreds, tens, ones in that order. Simpler, two nesting levels less, four cells less, and it works: 0/1/7/10/42/99/100/512/1000/1234/2047 all correct. Two real traps found on the way, both now in the macro comments and SPEC: * copy's buffer cell must be *clean*: a dirty buffer makes the copy produce 0 (measured: Q = 4, buffer = 0x7f8 -> N = 0). The library only says the buffer is borrowed, never that it has to be zeroed. * building a constant cannot be done as 'set a bit, rotate, set another, rotate': the second rotation carries the first bit away. Walk the ring once instead -- W rotations, set_1 at each target bit, then back home. 10829 characters with a minimal driver (13743 with the four outputs), 16 cells in total, and r8forge_byte_op_test's test_digits covers all 11 values. --- AGENTS.md | 4 +- SPEC.md | 35 +++++++++++++- asm/digits.r8asm | 104 ++++++++++++++++++++++++++++++++++++++++++ drv/digits.r8asm | 18 ++++++++ tests/byte_op_test.cc | 28 ++++++++++++ 5 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 asm/digits.r8asm create mode 100644 drv/digits.r8asm diff --git a/AGENTS.md b/AGENTS.md index 233615f..d0694d3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -80,7 +80,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_derive verify_loop.r8asm -1 0=5 # 动态循环:跑 5 次,out 应该是 5 0 -r8forge_byte_op_test # 字节级库:19 个操作穷举/抽样,1.5 秒 +r8forge_byte_op_test # 字节级库:20 个操作穷举/抽样,1.5 秒 r8forge_macro_docs_test # asm/ 里每个宏的注释格式 ``` @@ -213,6 +213,7 @@ inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellSize) - 1u; | **`clb_mul`(mc×mul → 两 cell 积,10 cell)** | `drv/mul_byte.r8asm` | **3977 字符** | **65536/65536**(O(位宽) 运行) | | **`clb_ones`(数量 → 从末位起 N 个 1,4 cell)** | `drv/ones.r8asm` | **742 字符**(带 output)/ **828**(最小驱动) | **12 个计数全对**(N=0..11) | | **`clb_divmod`(N div D / N mod D,7 cell)** | `drv/divmod.r8asm` | **4189 字符**(最小驱动)/ 4195(带 output) | **76 组抽样全对**(O(N/D) 运行,`2047/1` 会撞步数上限) | +| **`clb_digits`(整数 → 4 位 ASCII 十进制,16 cell)** | `drv/digits.r8asm` | **10829 字符**(最小驱动)/ 13743(带 output) | **11 个值全对**(0/7/10/99/100/512/1000/1234/2047 等) | | `asm/fibonacci.r8asm` | — | 11959 字符 | 跑出 13 项 `1 1 2 3 5 8 13 21 34 55 89 144 233` | `raw_*` 保留操作数(A、B 原样还回来),`clb_*_nc` 把 A、B 吃干净 —— 所以后者能省掉 @@ -480,6 +481,7 @@ SPEC 第 136-150 行的标准宏前缀: | `clb_nonzero` | 破坏性的纯计算(把 B 抄成"非零"标志),不带 `_ip` 后缀 | | `clb_ones` | 破坏性的纯计算(把"数量"展开成从末位起的一串 1,输入被吃干净);结果写在另一个 cell 里,所以也不带 `_ip` | | `clb_divmod` | 破坏性的纯计算(除/取模,商和余数各落在自己的 cell 里);N、D 都被吃干净,所以不带 `_ip` | +| `clb_digits` | 破坏性的纯计算(整数 → 一串 ASCII 数字),输入被吃干净、结果写在连续区域里,所以不带 `_ip` | | `loop` / `endloop` | 结构性的**宏对**(开/收一个运行时循环),不带前缀;不开 anchor 所以能嵌套,收口后 dp 停在计数器上 | | `clb_bit_mov` | 位级搬运,并且把源吃掉 | | `bit_copy` / `copy` | 位级/字节级拷贝,缓冲由调用者传(历史命名,少了 `raw`) | diff --git a/SPEC.md b/SPEC.md index b098712..56e97f9 100644 --- a/SPEC.md +++ b/SPEC.md @@ -421,6 +421,37 @@ A 的 12 个取值全对(`r8forge_byte_op_test` 的 `test_ones`)。 所以 **N/D 最多到 1500 左右**;`2047 / 2` 是 1023 轮,正常返回 `1023 1`。 除零没有定义,调用者保证 `D >= 1`。 +### 7.7 `clb_digits`:整数转十进制字符串 + +`clb_digits N REG S`:把 N 展开成 **`REG..REG+3` 四个连续 cell,每格一个 ASCII 数字 +(`'0'..'9'`),高位在前、定长 4 位、左边补 `'0'`**(11 位 cell 最大 2047,正好 4 位)。 +N 被吃干净;`REG` 起 4 格、`S` 起 8 格必须连续且干净。纸带 **10829 字符**(最小驱动), +11 个值全对(`r8forge_byte_op_test` 的 `test_digits`)。 + + 2047 -> REG = '2' '0' '4' '7' + 7 -> REG = '0' '0' '0' '7' + +做法:**divmod 只出现一次**,外面套一个跑 4 次的运行期循环;而"写哪一格"不做分派 —— +每轮把已经写好的数字**整体往高地址挪一格**,新数字永远写 `REG+0`: + + 第 1 轮: 挪(空) ; REG+0 = 个位 + 第 2 轮: 挪(个位 -> REG+1) ; REG+0 = 十位 + ... + +挪的顺序必须**从高到低**(`REG+2 -> REG+3` 先做),否则会把还没搬的高位盖掉。 +4 轮之后 `REG+0..3` 正好是千、百、十、个。 + +第一版是"4 个分支 + one-hot flag 分派",**只写对了第一支**(其余三支没触发),换成 +整体挪位后不仅正确,还少了两层嵌套、4 个 cell 和一堆 flag 交接。教训: +**在这台只有 `biz`/`rnz` 的机器上,"选一个格写"最好用位置移动表达,别用索引分派。** + +两个搭这条宏时踩出来的坑(都写进了宏注释): + +* **`copy` 的缓冲格必须是干净的**:缓冲里带垃圾时拷贝结果是 **0**(实测 `Q=4`、缓冲 + `0x7f8` → `N=0`)。库注释只说了"`b` 是借来的缓冲",没提这条。 +* **造常数不能分两次"置位再转"**:第二次旋转会把第一次放好的位一起带走。正确做法是 + **绕整圈走一次**(`W` 次 `ror`,转到目标位就 `set_1`,最后转回原位)。 + --- ## 8. 原地 `_ip` 家族(结果回到原来的 cell) @@ -502,13 +533,13 @@ clb_add_ip PH Ci S F ; 把进位加进高字节(Ci 被吃) | 测试 | 覆盖 | 断言口径 | |---|---|---| -| `r8forge_byte_op_test` | 19 个测试项 = 18 个宏(`clb_neg_nc` 的 Ci=1 / Ci=0 各算一项),见 §7.1、§7.2、§7.5、§7.6、§8、§9 | **按返回值**:穷举 0..255(`clb_add_ip` 2048 抽样、`clb_mul` 65536 全穷举、`clb_ones` 12 个计数全穷举、`clb_divmod` 76 组抽样),任一不符即退出码非 0 | +| `r8forge_byte_op_test` | 20 个测试项 = 19 个宏(`clb_neg_nc` 的 Ci=1 / Ci=0 各算一项),见 §7.1、§7.2、§7.5、§7.6、§7.7、§8、§9 | **按返回值**:穷举 0..255(`clb_add_ip` 2048 抽样、`clb_mul` 65536 全穷举、`clb_ones` 12 个计数全穷举、`clb_divmod` 76 组抽样、`clb_digits` 11 个值全穷举),任一不符即退出码非 0 | | `r8forge_synth_test examples/full_add.r8asm` | `%synth` 端到端:汇编 + 跑 8 种输入 | `all eight combinations ok` | | `r8forge verify_clb_bit_add_nc.r8asm` | 4 cell 加法切片(进位进→出) | 单点:`0+0+Ci` | | `r8forge verify_clb_bit_inc_nc.r8asm` | 3 cell 自增切片 | 单点 | | `r8forge_derive verify_loop.r8asm -1 0=N` | 动态循环:`N=0..11` 时循环体跑 `N` 次(`A = N`、`C = 0`) | 逐值 `out` | | `r8forge verify_set_0.r8asm` | `set_0` 只清 LSB | `-1 0=255 → 254`、`0=128 → 128` | -| `r8forge_macro_docs_test` | `asm/` 里 45 个宏的注释格式 | 全绿或报错行号 | +| `r8forge_macro_docs_test` | `asm/` 里 46 个宏的注释格式 | 全绿或报错行号 | | `r8forge_tape_run ` | 第二个解释器,直接跑纸带 | 与 `r8forge_derive` 对答案 | | `r8forge_derive -1/-2` | 拿汇编源码跑,打印 `out` / 非零 cell | 人工/脚本比对 | diff --git a/asm/digits.r8asm b/asm/digits.r8asm new file mode 100644 index 0000000..bce755d --- /dev/null +++ b/asm/digits.r8asm @@ -0,0 +1,104 @@ +%include "asm/divmod.r8asm" + +; 整数 -> 十进制字符串:把值展开成 4 个连续 cell,每格低 8 位放一个 ASCII 数字。 +; +; clb_digits N REG S ; REG..REG+3 得到 "2047" 那样的 4 个字节 +; +; 目标形态:**ASCII '0'..'9'、高位在前、定长 4 位、左边补 '0'**。 +; 11 位 cell 装得下 0..2047,也就是 4 位十进制,所以 L 写死 4。 +; +; 朴素做法是 4 份 divmod(4x4189 ≈ 17k)。这里让 **divmod 只出现一次**:外面套一个 +; 跑 4 次的运行期循环,而且**不做"写哪一格"的分派** —— 每轮把已经写好的数字整体 +; 往高地址挪一格,新数字永远写 REG+0: +; +; 第 1 轮: 挪(空的) ; REG+0 = 个位 +; 第 2 轮: 挪(个位 -> REG+1) ; REG+0 = 十位 +; ... +; 第 4 轮: 挪(三位 -> 高位) ; REG+0 = 千位 +; +; 挪的顺序必须**从高到低**(REG+2 -> REG+3、REG+1 -> REG+2、REG+0 -> REG+1), +; 也就是"高位在前"的字符串,不用倒序、不用 4 个分支。 +; +; (第一版是 4 个分支 + one-hot flag 分派,只跑对了第一支 —— 甩掉分派之后不仅 +; 正确,还少了两层嵌套和 4 个 cell。) +; +; 三个踩过的坑: +; * **`copy` 的缓冲格必须是干净的**:缓冲带垃圾时拷贝结果是 0(实测)。 +; * **`%synth` 那句老话反过来**:造常数不能"置一位转到位、再置一位转到位" —— +; 第二次旋转会把第一次放好的位一起带走;要**绕整圈走一次**。 +; * 区域格、S 起的草稿格进来时必须都是干净的(0)。 +; +; 前提:N = 值(结束时为 0);REG 起 4 格、S 起 8 格连续且干净。没有负数。 + +; Macro clb_digits +; %1: Cell N +; %2: Cell REG +; %3: Cell S +; +; N 是值(被吃干净);REG..REG+3 是输出,ASCII 数字,高位在前;S 起的 8 个连续 +; cell 是草稿(S=10、S+1=商、S+2..S+5=divmod 的草稿块、S+6=轮数、S+7=拷贝缓冲), +; 进来要求干净。 +%macro clb_digits 3 +.anchor before_clb_digits + +; ---- K = 4 个连续的 1(第 0..3 位),给 loop 用 +.tp %3+6 +set_1 +rol +set_1 +rol +set_1 +rol +set_1 + +; ---- 四位,一轮一位 +loop %3+6 + ; 重建 TEN = 10(位 1、3):绕整圈,在 1 和 3 各置一个 1 + erase %3+0 + .tp %3+0 + ror + set_1 + %rep 2 + ror + %endrep + set_1 + %rep 8 + ror + %endrep + + ; N = 这位数字(0..9),Q = 商 + clb_divmod %1 %3+0 %3+1 %3+2 + + ; ---- 已有数字整体上移一格:从高到低搬 + erase %3+7 + .tp %2+2 + copy %2+3 %3+7 + erase %3+7 + .tp %2+1 + copy %2+2 %3+7 + erase %3+7 + .tp %2+0 + copy %2+1 %3+7 + + ; ---- REG+0 = '0' + 数字(位 4、5) + erase %2+0 + .tp %2+0 + %rep 4 + ror + %endrep + set_1 + ror + set_1 + %rep 6 + ror + %endrep + clb_add_ip %2+0 %1 %3+2 %3+3 + + ; ---- 下一位的被除数(缓冲要干净) + erase %3+7 + .tp %3+1 + copy %1 %3+7 +endloop %3+6 + +.tp before_clb_digits +%endmacro diff --git a/drv/digits.r8asm b/drv/digits.r8asm new file mode 100644 index 0000000..c255d7a --- /dev/null +++ b/drv/digits.r8asm @@ -0,0 +1,18 @@ +%include "asm/digits.r8asm" + +; clb_digits 的驱动:0 号 cell 放值,1..4 是输出区域,5 起是草稿。 +; +; r8forge_derive drv/digits.r8asm -1 0=2047 # 50 48 52 55 = "2047" +; r8forge_derive drv/digits.r8asm -1 0=7 # 48 48 48 55 = "0007" +; +; out 依次是区域四格(ASCII 码,所以 48 是 '0')。 +.var N 0 +.var REG 1 +.var S 5 + +clb_digits N REG S + +output REG+0 +output REG+1 +output REG+2 +output REG+3 diff --git a/tests/byte_op_test.cc b/tests/byte_op_test.cc index a82234b..0abfcd0 100644 --- a/tests/byte_op_test.cc +++ b/tests/byte_op_test.cc @@ -494,6 +494,33 @@ int test_divmod(const std::string &file) { return bad; } +// clb_digits: the value becomes four ASCII digits, most significant first. +int test_digits(const std::string &file) { + const std::string tape = assemble(file); + std::cout << "clb_digits: " << tape.size() << " characters\n"; + const int nums[] = {0, 1, 7, 10, 42, 99, 100, 512, 1000, 1234, 2047}; + int bad = 0, n = 0; + for (int v : nums) { + machine m; + m.cells[0] = uint16_t(v); + m.run(tape, 5'000'000); + n++; + std::string want = std::to_string(v); + while (want.size() < 4) want.insert(want.begin(), '0'); + bool ok = m.finished && m.out.size() == 4; + for (int i = 0; ok && i < 4; i++) { + ok = m.out[size_t(i)] == (uint16_t)(unsigned char)want[size_t(i)]; + } + if (!ok) { + bad++; + if (bad <= 3) report("clb_digits", v, 0, m, want); + } + } + std::cout << " " << (bad ? "FAILED" : "all " + std::to_string(n) + " values ok") + << (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n"; + return bad; +} + // clb_add_ip: A = A + B in place, B is eaten. The loop runs B times, so this // one is sampled rather than exhaustive -- 256 addends x 8 accumulators. int test_add_ip(const std::string &file) { @@ -576,6 +603,7 @@ int main(int argc, char **argv) { test_nonzero(drv + "nonzero_byte.r8asm") + test_ones(drv + "ones.r8asm") + test_divmod(drv + "divmod.r8asm") + + test_digits(drv + "digits.r8asm") + test_add_ip(drv + "add_ip_byte.r8asm") + test_mul(drv + "mul_byte.r8asm") + test_sub_nc(drv + "raw_sub_nc_byte.r8asm");