From b89fcb0588149ac7dc11aef9e5b6752875ae5aa2 Mon Sep 17 00:00:00 2001 From: dsh Date: Sat, 7 Nov 2026 16:37:15 +0800 Subject: [PATCH] SPEC 5.1: do not replace an assembly time counter with clb_ones Asked for, then stopped mid-flight: the tempting refactor was to build clb_mul's round counter with clb_ones + loop/endloop instead of %rep _CELL_SIZE { set_1; ror }. It is the wrong direction and worth writing down so the next attempt does not get as far: assembly time %rep _CELL_SIZE { set_1; ror } 55 characters run time clb_ones (~770) + loop/endloop (~10) + building _CELL_SIZE as a value, about 850 characters, 4 more cells, tens of thousands of extra steps Same for the alignment trap: %rep N { set_1; ror } leaves the run of ones at the top of the cell, so biz sees bit0 = 0 and the loop never starts -- N < W needs one rol after the %endrep. clb_mul only escapes it because its N is _CELL_SIZE, i.e. the whole cell is ones. --- SPEC.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/SPEC.md b/SPEC.md index b124e8f..504905d 100644 --- a/SPEC.md +++ b/SPEC.md @@ -243,6 +243,22 @@ endloop C > 在 `.r8asm` 源码里循环要写 `biz` / `rnz`,**不是** `[` / `]` —— 后者是纸带上的编码。 > 直接手写 `[` 会报 `unknown macro '['`(`lib.r8asm` 里就是这么踩的)。 +**别拿它去替代汇编期常数**。`clb_mul` 的轮数计数器是 `_CELL_SIZE` 个 1,汇编期就能做: + +``` +%rep _CELL_SIZE { set_1; ror } ; 55 字符(4+1 各 11 份) +``` + +换成运行期那条路要 `clb_ones`(≈770)+ `loop`/`endloop`(≈10)+ 先把常数 +`_CELL_SIZE` 做成运行期的值,**约 850 字符** —— 15 倍,还多 4 个 cell 和几万步运行时间。 +所以规则是:**次数在汇编期就知道 → 汇编期造计数器;只有次数来自数据才用 `clb_ones`。** +(`loop`/`endloop` 两种都吃:它们只要求"某个 cell 里现在有 n 个 1",不关心这些 1 是谁造的。) + +顺带一个坑:`%rep N { set_1; ror }` 造出来的 N 个 1 落在**高位**(bit `W-1` 往下), +`biz` 看到的 bit0 是 0,循环一圈都不跑 —— `N < W` 时要在 `%endrep` 后面补一条 `rol` +(10 字符),把那串 1 转到含 bit0 的位置。`clb_mul` 没踩这个坑,是因为它的 N 正好等于 +`_CELL_SIZE`,整格全 1,bit0 天然是 1。 + --- ## 6. 位级宏(一个 cell 装一位)