SPEC 14: the shift-and-restore divider, worked out before it is built

The divider rewrite is a multi-round build (the cross cell shift cost clb_mul two
reworks), so the design went into SPEC first.  The one non obvious part is the
comparison: R needs W+1 bits, but it does not need a W+1 bit subtraction.

With R = (Rhi:Rlo) and D < 2^W, compute S = Rlo - D and Bo = (Rlo >= D) with
raw_sub, and then

    commit (R >= D)      is  Rhi | Bo
    the new high bit is      Rhi & Bo

All four cases check out (Rhi = 0, Bo = 0 is exactly R < D, no commit).  So a
W bit subtract plus one bitwise OR and one AND replaces the W+1 bit machinery.

Also recorded: the state layout, the per round skeleton, why N is rotated with
rol (it hands out the bits most significant first and restores itself after W
rounds), the four pitfalls, and the acceptance checklist (same 76 sample pairs
against the current clb_divmod, step counts before/after, and clb_digits going
from 1.48M steps to tens of thousands).

raw_sub (the previous commit) is what makes this possible at all: the divisor
has to survive every round.
This commit is contained in:
dsh committed 2026-11-07 20:29:51 +08:00
1 parent 9a9ca48d9a
commit a2154ee9ac
1 file changed
+56
+56
View File
@@ -606,3 +606,59 @@ clb_add_ip PH Ci S F ; 把进位加进高字节(Ci 被吃)
- **改指令模型**(合成器的块集合、代价):必须 bump `kModelVersion`,否则
`.r8forge_cache/` 里的旧结果会被当成最优。
- **改本文的数字**:照 §1.3 的口径重新量,别把驱动长度和最小驱动长度混在一列。
---
## 14. 下一步:把 `clb_divmod` 换成移位-恢复除法(设计已推完,未实现)
现在 `clb_divmod` 是重复减法,O(N/D)。`2047 / 10` 要 205 轮、**148 万步**;
`clb_digits` 的 1.48M 步几乎全花在这儿。换成二进制长除法是 **W 轮**(11 轮),
预计 **~35k 步(快 40 倍)**,纸带大体相当(~4k)。
### 14.1 每轮的状态
N 被除数(W 位)。每轮 `rol N`,于是 N 的 LSB 就是**最高位起**的下一位
(rol 把 bit W-1 转到 bit0) —— 转满 W 轮 N 自动复原,不被消耗
R 部分余数,需要 **W+1 位** → 两格:Rlo(低 W 位)+ Rhi(只有 bit0)
D 除数(W 位),**每轮都要拿它去比/减 → 必须保留** —— 这就是 `raw_sub` 存在的理由
S 减法结果(W 位,Rlo - D)
Bo raw_sub 给的"没借位"(1 表示 Rlo >= D)
每轮: `R = (R << 1) | 位`,然后 `R >= D ? (R -= D, 商位 = 1) : (商位 = 0)`。
### 14.2 关键推导:W+1 位比较拆成"一个 W 位减 + 两个布尔"
`R = (Rhi:Rlo)`、`D < 2^W`。先算 `S = Rlo - D` 与 `Bo = (Rlo >= D)`,那么
提交(R >= D) ⟺ Rhi == 1 或 Bo == 1 → **commit = Rhi | Bo**
提交后的新高位 ⟺ Rhi == 1 且 Bo == 1 → **Shi = Rhi & Bo**
四种情况都对得上(`Rhi = 0, Bo = 0` 就是 `R < D`,不提交)。所以**不需要真的做
W+1 位减法**,只要一个 W 位 `raw_sub`(顺带给出 Bo)+ 一个按位或 + 一个按位与。
### 14.3 每轮骨架
rol N ; 下一位进 N 的 LSB
<R 跨格左移一格,并把 N 的 LSB 注入 Rlo 的 bit0> ; bit_copy(38,保留源)
raw_sub Rlo D S Bo Bc Ci buf ; ≈2200 步
clb_bit_or Rhi Bo Commit
clb_bit_and Rhi Bo Shi
.tp Commit ; biz ; copy S -> Rlo ; copy Shi -> Rhi ; <吃掉条件> ; rnz
rol Q
.tp Commit ; clb_bit_mov Q ; 商位 = commit(顺带清 Q 的 bit0)
### 14.4 风险与坑(按 `clb_mul` 的经验)
1. **R 的跨格左移是唯一会反复的地方**。`clb_mul` 当年在这里返工两轮:`rol` 会把
Rlo 的最高位绕回 bit0,所以必须**先把它搬进 Rhi 的 bit0,再清掉 Rlo 的绕回位**,
顺序错了就丢位。
2. **`biz` 是循环对**:门控提交的 body 最后必须把条件吃掉(`flb`),否则 `rnz` 跳回去。
3. **Rhi 只有 bit0 有意义**,它当布尔用,别当字节。
4. 轮数计数器:`%rep _CELL_SIZE { set_1; ror }` 造 W 个 1(那串 1 落在高位,要补一条
`rol` 让 bit0 也是 1),或者用 `%for` + `clb_ones`。
### 14.5 验收
* 和现在的 `clb_divmod` 跑同一张抽样表(76 组),逐对比 Q/R;
* 步数对比:`2047/10`、`2047/2`、`999/7`、`2047/1`(最后这个现在会 HANG);
* `clb_digits` 换上新除法器后,11 位下 11 个值全对、步数从 1.48M 降到几万。