Files
R8Forge/asm/divmod.r8asm
T
dsh c2cd0e827f clb_divmod: shift-and-restore division instead of repeated subtraction
Repeated subtraction ran N/D rounds and hit the 5M step ceiling for D = 1
(2047/1 hangs outright); it also cost 2.46M steps for 2047/2.  Binary long
division is fixed at _CELL_SIZE rounds whatever the operands are:

    R = 0 ; Q = 0
    repeat _CELL_SIZE:  R = (R << 1) | next bit   (rol N hands the bits out
                                                   MSB first and restores N
                                                   after W rounds)
                        if R >= D:  R -= D ; quotient bit = 1

The one non obvious part is the comparison.  R needs W+1 bits after the shift,
but the high bit (H, the bit rol wraps out of Rlo) only has to decide the
commit: H = 1 means R >= 2^W > D, so commit = H | Bo, where Bo is raw_sub's "no
borrow" (Rlo >= D).  SPEC 14's Shi = H & Bo is always 0 under the invariant "R
< D at the start of every round" (H = 1 and Bo = 1 together would need R >= 2D),
so that clb_bit_and and its copy do not exist in the built version.

Signature is now clb_divmod N D Q R S: N and D survive, Q and R each get their
own cell (the old version ate N into the remainder), and S is a 7 cell scratch
block (Rhi, Sd, Bo, Cm, Ci, buf, NR).  Only S+5 (the copy buffer) has to be
clean on entry; the rest is rewritten or used as a single bit, which is what
lets clb_digits pass the same dirty block back in every round.

The order that cost clb_mul two reworks is the delicate bit: rol Rlo wraps its
top bit into bit 0, so clb_bit_mov has to move that bit into Rhi (which also
clears it) before bit_copy injects N's bit.  bit_copy (not clb_bit_mov) is what
keeps N intact.

Measured on drv/divmod.r8asm (11 bit):

              repeated subtraction     shift-and-restore
    2047/10        495,986                 28,593
    2047/2       2,461,650                 31,997
    999/7          340,086                 28,661
    2047/1         HANG (>5M)              32,383

Small N/D get slower (fixed W rounds) -- the same trade clb_mul made.  Worst
case over the sweep is 32,383 steps; the tape grows 4189 -> 4499 characters.

About the numbers in SPEC 14 / AGENTS: the 1.48M steps claimed there do not
reproduce on this tree.  Measured before the change, 2047/10 was 495,986 and
clb_digits(2047) was 740,695, so the docs now carry the measured values and say
where the old figure came from.

clb_digits keeps its shape (one divmod in a 4 round loop, written digits shift
up by one cell), with S grown to 13 cells: TEN, quotient, remainder, the 7
divmod scratch cells, the loop counter and add_ip's T/F.  2047 goes from
740,695 to 145,138 steps (worst 164,867) and 10829 -> 13717 characters.  It
needs its own %include "asm/add_ip.r8asm" now: that used to arrive through the
old divmod.

Tests: test_divmod sweeps 8 divisors x all 2048 dividends (16384 pairs; the old
76 sample table is a subset) and also asserts N and D come back unchanged;
test_digits sweeps all 2048 values.  Both print the worst step count.

Two traps found on the way:

* r8forge_derive (and byte_op_test/synth_test) checked the assembler error flag
  before export_rot8(), but an unknown macro or a bad .tp target is found while
  the tree is *expanded*: a missing %include left a truncated tape that still
  ran and printed a plausible wrong answer (all four digits came out '0').  All
  three now check after the export.
* raw_sub's comment claimed Bo = 1 means A < B; the code (and clb_sub_nc) mean
  A >= B.  Comment fixed, behaviour untouched.

Also adds drv/div_shift.r8asm, the single step check for the cross cell shift
plus injection, and records the measurements in SPEC 7.6/7.7/14 and AGENTS.
2026-11-07 22:22:03 +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
; (所以 §14.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