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.
53 lines
1.8 KiB
Plaintext
53 lines
1.8 KiB
Plaintext
%include "asm/raw_add.r8asm"
|
|
%include "asm/not_ip.r8asm"
|
|
|
|
; 保留操作数的减法:S = A - B,而且 **A、B 都不动**,另外给出借位 Bo。
|
|
;
|
|
; 为什么需要它:`clb_sub_nc` 会把 A 和 B 都吃干净,所以"重复减法"的除法器
|
|
; 每一轮都得 copy 出两个副本(每个 495 字符、每次约五百步),这也是
|
|
; `clb_divmod` 跑到 2047 要 148 万步的原因。二进制长除法每一轮都要拿同一个
|
|
; 除数去比/减,必须有个"不破坏操作数"的减法。
|
|
;
|
|
; 做法就是把 `raw_add`(它本来就保留 A、B)套上补码:
|
|
;
|
|
; S = A + ~B + 1 = A - B
|
|
;
|
|
; 于是进位输出 Co 就是"没有借位"——`clb_sub_nc` 也是这样,但那个是破坏性的。
|
|
; **Bo 就是 Co,不取反**:Bo = 1 表示 A >= B(实测 15-10 -> Bo=1、10-15 -> Bo=0)。
|
|
; (早先这里写过"Bo = 1 表示 A < B",是错的;底下那段注释解释了为什么不能取反。)
|
|
;
|
|
; 前提:A、B 的值 < 2^_CELL_SIZE(B 取反后和 A 相加不会溢出到第 W+1 位以外的解释)。
|
|
|
|
; Macro raw_sub
|
|
; %1: Cell A
|
|
; %2: Cell B
|
|
; %3: Cell S
|
|
; %4: Cell Bo
|
|
; %5: Cell Bc
|
|
; %6: Cell Ci
|
|
; %7: Cell buf
|
|
;
|
|
; S = A - B(mod 2^W),A、B 保持原样,Bo = 1 表示 A >= B(没有借位)。
|
|
; Bc、Ci、buf 是草稿(进来要干净),结束时 Ci 和 buf 是垃圾、Bc 是 ~B。
|
|
%macro raw_sub 7
|
|
.anchor before_raw_sub
|
|
|
|
; Bc = ~B
|
|
.tp %2
|
|
copy %5 %7
|
|
clb_not_ip %5
|
|
|
|
; Ci = 1,做的是"加补码"
|
|
.tp %6
|
|
set_1
|
|
|
|
; S = A + Bc + 1,进位出到 Bo
|
|
raw_add %1 %5 %6 %3 %4 %7
|
|
|
|
; 注意:这里**不能**再 `clb_not_ip %4`。`clb_not_ip` 是整格取反(~carry),
|
|
; 得到的是 2046/2047 而不是 0/1(实测)。所以 Bo 和 `clb_sub_nc` 的进位一样,
|
|
; 语义是"**没有借位**":Bo = 1 表示 A >= B。
|
|
|
|
.tp before_raw_sub
|
|
%endmacro
|