Seven files still opened with a paragraph about how they came to be -- "the first
version cleared the same bit four times", "the older repeated-subtraction divmod
needed 1.48M steps", "last round rejected shift-and-add because it was longer",
nested with measured step counts. None of that tells a caller how to use the
macro, and it is exactly the material that goes stale (raw_sub's figure had been
retracted, and the numbers are SPEC's job now). The stories already live in
docs/divmod.md and docs/byte-ops.md, so:
digits 119 -> 80 lines mul 125 -> 98
divmod 106 -> 80 inc_ip 81 -> 72
undigits 95 -> 73 raw_sub 52 -> 38
dec_ip 21 -> 18
Each now opens with two lines: what the macro computes and where the contract
lives (SPEC section, plus docs/ when there is a design write-up). Everything
from the `; Macro <name>` block down -- parameter types, preconditions,
what-is-eaten, the non-obvious invariants -- is untouched, which is what
r8forge_macro_docs_test checks and what a caller actually reads.
Baseline unchanged: tape 64513, 21 byte-op items, 15 examples, 48 macros.
81 lines
2.6 KiB
Plaintext
81 lines
2.6 KiB
Plaintext
; clb_divmod:二进制长除法(移位-恢复),商 Q、余数 R 各占一格,N、D 保留。
|
|
; 契约见 SPEC §7.6,设计推理和实测见 docs/divmod.md。
|
|
;
|
|
%include "asm/raw_sub.r8asm"
|
|
%include "asm/raw_or.r8asm"
|
|
;
|
|
; 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
|