Files
R8Forge/asm/divmod.r8asm
T
dsh a82d95658e docs: one home per fact -- README as the front door, SPEC as the contract, AGENTS as the manual
The three documents had grown to 2038 lines and quoted each other: 2199 appeared
seven times, 3977 ten times, AGENTS carried a 200-line copy of SPEC's byte-level
tables while README carried another 155-line copy still showing 8-bit-era numbers
(clb_add_nc 688, clb_not_ip 17, ...) that are wrong at 11 bits.

Split by responsibility, keep every fact exactly once:

  README.md   front door: what it is, 30-second build+run, directory map, where
              to read next                                     547 -> 143 lines
  SPEC.md     the contract: %synth, geometry, every macro's signature / cells /
              tape length / who verifies it, test matrix, boundaries, maintenance
                                                               901 -> 807 lines
  AGENTS.md   the manual: hard rules, build and self-check commands, the traps,
              current state, next candidates, do-not-touch list
                                                               590 -> 132 lines

Long-form reasoning moves into docs/ instead of being dropped:

  docs/divmod.md    the shift-restore division story (was SPEC 14, 102 lines;
                    renumbered 1..6, SPEC keeps a pointer and the references in
                    SPEC 7.6 and asm/divmod.r8asm now point there)
  docs/byte-ops.md  the byte-level design history (was README's 155-line section,
                    rewritten with 11-bit numbers since the old ones were
                    measured at 8 bits)

Also fixes what the dedupe exposed: SPEC no longer claims only the multiplier has
a step bound, and raw_sub's comment no longer repeats the retracted "1.48M steps"
figure. Every section reference in the tree resolves; the baseline still passes
(asm/test.r8asm 64513 characters, 21 byte-op items, 14 examples, 48 macros).
2026-11-09 16:34:15 +08:00

107 lines
4.2 KiB
Plaintext

%include "asm/raw_sub.r8asm"
%include "asm/raw_or.r8asm"
; 除法/取模:Q = N div D,R = N mod D。
;
; 算法是**二进制长除法(移位-恢复)**,从被除数的最高位往下一位一位串:
;
; R = 0 ; Q = 0
; while 还剩一位没看:
; R = (R << 1) | (N 的下一位)
; if R >= D: R -= D ; 商位 = 1 else: 商位 = 0
;
; 轮数 = 位宽(11 轮),**跟 N、D 的值无关** —— 这是它取代"重复减法"的唯一理由:
; 旧版 O(N/D),`2047/1` 要 2047 轮、直接撞 500 万步上限(实测 HANG;`2047/2` 是
; 246 万步)。现在 2047/10 和 2047/1 一样是十一轮。
;
; 三个细节(都是设计时推过、实测确认的):
;
; 1. **W+1 位的比较拆成"一个 W 位减法 + 一个布尔"**:R 左移一格之后最多 W+1 位,
; 最高位就是 Rlo 左旋绕回来的那一位(记 H)。H = 1 时 R >= 2^W > D 必然提交,
; 而且 R - D 的低 W 位正好就是 S = Rlo - D(mod 2^W);H = 0 时就是普通的
; "Rlo >= D 才提交"。所以**提交条件 = H | Bo**,一条 clb_bit_or 就够,不用真去
; 做 W+1 位减法。H 只在轮内活着:提交之后 R < D < 2^W,下一轮开头高位是 0
; (所以 docs/divmod.md §2 里那个"新高位 = H & Bo"恒为 0,不用算、也不用留 cell)。
; 2. **R 的跨格左移**:rol 把 Rlo 的最高位绕回 bit0,所以先 clb_bit_mov 把它搬进
; Rhi(搬的同时清掉绕回位),再 bit_copy 把 N 的 LSB 注进 Rlo 的 bit0。
; 顺序反了就丢位 —— `clb_mul` 当年在这里返工两轮。
; 3. **N 不被消耗**:每轮 rol 一次,转满 W 轮自己复原;注入用 bit_copy(保留源)。
; D 每轮都要拿去比/减,所以用保留操作数的 raw_sub(这就是它存在的理由)。
;
; 除零没有定义:调用者保证 D >= 1(D = 0 时 R >= 0 恒成立,商会变成全 1)。
; Macro clb_divmod
; %1: Cell N
; %2: Cell D
; %3: Cell Q
; %4: Cell R
; %5: Cell S
;
; Q = N div D,R = N mod D。**N 和 D 都保持原样**(旧版把 N 吃成余数)。
; Q 由宏自己清零后累加。
; S 起的 7 个**连续** cell 是草稿:**只有 S+5(buf) 进来必须干净** —— 它是 copy/bit_copy
; 的缓冲,缓冲带垃圾时拷贝结果是 0。其它格子脏了没关系:要么整格重写(raw_add 逐位写
; Sd/Bo),要么只按一位用(Rhi/Cm/Bo),NR 每次进来都会重建成 W 个 1。
; (`clb_digits` 就是这么反复用同一块草稿的:第二轮起它是脏的。)
; 前提 D >= 1(除零没有定义,这里不检查)。
%macro clb_divmod 5
.anchor before_clb_divmod
; s+0 Rhi : 本轮左移出来的高位(轮内布尔)
; s+1 Sd : 减法结果
; s+2 Bo : raw_sub 的"没借位"(1 表示 Rlo >= D)
; s+3 Cm : 提交标志 = Rhi | Bo(借 raw_sub 的 Bc 那个草稿格用)
; s+4 Ci : raw_sub 的进位草稿
; s+5 buf : 拷贝缓冲(bit_copy / copy 共用,先后用完都是干净的)
; s+6 NR : 轮数计数器
erase %4
erase %3
; 轮数:_CELL_SIZE 个 1,每轮 set_0 + ror 吃掉一个。一串 1 的长度就是剩余轮数,
; 所以"做几轮"不占纸带长度,只占运行时间(和 clb_mul 一样的计数法子)。
.tp %5+6
%rep _CELL_SIZE
set_1
ror
%endrep
.tp %5+6
biz
; ---- 1. R = (Rhi:Rlo) << 1 | (N 的下一位)
.tp %1
rol ; 下一位进 N 的 LSB
.tp %4
rol ; Rlo 左移一格,最高位绕回 bit0
clb_bit_mov %5+0 ; 绕回位 -> Rhi(先搬走,顺带清掉 Rlo 的 bit0)
.tp %1
bit_copy %4 %5+5 ; Rlo 的 bit0 = N 的 LSB(N 保留)
; ---- 2. S = Rlo - D,Bo = (Rlo >= D)。D 不能被吃,所以走 raw_sub
raw_sub %4 %2 %5+1 %5+2 %5+3 %5+4 %5+5
; ---- 3. 提交条件 = Rhi | Bo(clb_bit_or 把两个输入都吃掉,Rhi 因此归零)
clb_bit_or %5+0 %5+2 %5+3
; ---- 4. 商左旋一位;提交时 R = S,商位记 1
.tp %3
rol ; rol 之后 Q 的 bit0 一定是 0(还没写满 W 位)
.tp %5+3
biz
.tp %5+1
copy %4 %5+5 ; R = S
.tp %3
set_1 ; 商位 = 1
.tp %5+3
flb ; 把条件吃掉(biz/rnz 是循环对)
rnz
; ---- 5. 这一轮结束,轮数吃掉一个
.tp %5+6
set_0
ror
rnz
.tp before_clb_divmod
%endmacro