clb_mul was repeated addition -- add MC into the product, MUL times. That is
the shortest thing to write, but the runtime is proportional to MUL's value:
255 * 255 took 733428 steps, and 2047 * 2047 ran into the 5000000 step ceiling
and hung outright (tape_run: HANG after 5000000 steps). So the multiplier was
a ceiling on what any program could compute.
The new body is the plain binary multiplication from school: hold the
multiplicand, look at the multiplier's low bit, add the multiplicand into the
high half of the product when that bit is 1, then shift the whole product one
place right -- the multiplier lives in the low half of that register, so its
bits fall out one at a time. The loop runs _CELL_SIZE rounds whatever the
operands are, and the tape got shorter as well:
82 * 141 358586 -> 8374 steps
255 * 255 733428 -> 17785 steps
2047 * 2047 HANG -> 26123 steps
tape 5007 -> 3977 characters
(Step counts are the tape's own, with no trailing newline -- tape_run counts
one if the file has one, which is where the first draft of this message got
numbers two out.)
Counter, carry and cross cell shift are the three pieces that needed care:
* the round counter is _CELL_SIZE ones in NR, one eaten per round by
set_0 + ror -- the same trick clb_inc_ip counts rotations with, so "how
many rounds" costs no tape length, only runtime;
* the carry out of the W bit add is not scratch: it is the top bit of the
2W+1 bit shift register, and the next shift moves it into PH's top bit;
* (F:PH:PL) >>= 1 is a cross cell shift. ror all three, bring the bit to
inject down to bit 0 with rol, move it with clb_bit_mov, ror back. The
low pair (PH -> PL) moves first, otherwise PH's bit 0 has already been
overwritten by the carry.
And biz/rnz is a loop pair, not an if/else: a one shot conditional has to eat
the bit it tested (a trailing flb) or rnz jumps straight back. The first
version of this hung exactly there -- no output at all, counter still 0x7ff.
The bit eaten is PL's low bit, which the shift discards anyway.
r8forge_byte_op_test now sweeps 0..255 x 0..255 exhaustively (65536/65536 ok,
6.7s instead of 1.5s) instead of sampling 64 combinations: the cost no longer
grows with the multiplier, so sampling was the only reason not to. asm/
test.r8asm still prints 1322 1786 1600 5 94 127, now as 28729 characters and
57330 steps (was 31901 and about two million), and derive and tape_run agree.
AGENTS.md and README record all of it, plus one correction: the old note said
shift and add had been dismissed because "only tape length is the target". It
is in fact 1000 characters shorter, so the reason given was wrong -- the
tradeoff was not. What changed is the target function: the 5000000 step
ceiling is part of it, and a loop whose length does not depend on its operands
can still have a runtime that does.
126 lines
3.9 KiB
Plaintext
126 lines
3.9 KiB
Plaintext
%include "asm/lib.r8asm"
|
||
%include "asm/raw_add_nc.r8asm"
|
||
|
||
; 一个 cell × 一个 cell -> 两个 cell 的积(11 位下是 22 位)。
|
||
;
|
||
; 算法是**二进制里最朴素的那条:移位-加法**,从乘数的最低位往上走一位一位串:
|
||
;
|
||
; A:PH = 0 ; C:F = 0 ; Q:PL = MUL
|
||
; while 还剩一位没看:
|
||
; if Q 的最低位 == 1: (C:PH) += MC ; 一次 W 位加法,进位出到 F
|
||
; (C:PH:PL) 右移一位 ; 最高位补 0,C 移进 PH 的最高位
|
||
;
|
||
; 这就是竖式乘法:被乘数不动,每轮只看乘数的一位,是 1 就把被乘数加到积的高半上,
|
||
; 然后把整个积往右挪一位,乘数也一起挪。W 轮之后 (PH:PL) 就是积,Q 的位被吃干净
|
||
; (所以 MUL 结束为 0,和原来的约定一样)。
|
||
;
|
||
; **为什么换掉原来的重复加法**:重复加法的轮数 = MUL 的值,所以运行代价 ∝ MUL
|
||
; (MUL=255 时约 100 万步,2047 时直接撞 500 万步上限,HANG)。移位-加法固定做
|
||
; _CELL_SIZE 轮,每轮一次 W 位加法(乘数那一位是 0 时整段跳过),所以总步数有个
|
||
; 上界:11 位下最坏 26123 步(2047 * 2047),**不随操作数的大小增长** ——
|
||
; 乘数再大也跑得完。代价是小乘数(0、1)反而比旧的重复加法慢一点点。
|
||
;
|
||
; 两个 cell 的细节:
|
||
; * (C:PH:PL) 得当成一个 2W+1 位的移位寄存器,**跨格移位**这么拼:先 ror 三格,
|
||
; 再把各自要注入的那一位搬到 bit0 用 clb_bit_mov 搬过去,最后 ror 回来 ——
|
||
; F 的那一位进 PH 的最高位,PH 的最低位进 PL 的最高位,PL 的最低位(刚看过的
|
||
; 乘数位)丢掉。顺序必须是"低位那对先搬",否则搬完 PH 的 bit0 就没了。
|
||
; * 进位不能丢:W 位加法的进位出到 F,下一轮右移时它正好被移进 PH 的最高位。
|
||
; 所以 F 不是草稿,是移位寄存器最高那一位。
|
||
;
|
||
; 计数器 NR:_CELL_SIZE 个 1,每轮 set_0 + ror 吃掉一个(和 clb_inc_ip 的计数法子一样),
|
||
; 一串 1 的长度就是剩余轮数 —— 于是"做几轮"这件事不占纸带长度,只占纸带的运行时间。
|
||
|
||
; Macro clb_mul
|
||
; %1: Cell MC
|
||
; %2: Cell MUL
|
||
; %3: Cell PL
|
||
; %4: Cell PH
|
||
; %5: Cell MCA
|
||
; %6: Cell S
|
||
; %7: Cell Ci
|
||
; %8: Cell T
|
||
; %9: Cell F
|
||
; %10: Cell NR
|
||
;
|
||
; (PH:PL) = MC * MUL。MC 保持原样,MUL 结束为 0。
|
||
; MCA、S、Ci、T 是草稿;F 是移位寄存器的最高位(进位),NR 是轮数计数器。
|
||
%macro clb_mul 10
|
||
.anchor before_clb_mul
|
||
|
||
; A = PH = 0, C = F = 0, Q = PL = MUL
|
||
.tp %4
|
||
erase %4
|
||
.tp %3
|
||
erase %3
|
||
.tp %9
|
||
erase %9
|
||
.tp %2
|
||
copy %3 %8
|
||
.tp %2
|
||
erase %2
|
||
|
||
; 轮数计数器:_CELL_SIZE 个 1,每轮吃掉一个
|
||
.tp %10
|
||
%rep _CELL_SIZE
|
||
set_1
|
||
ror
|
||
%endrep
|
||
|
||
.tp %10
|
||
.anchor mul_iter
|
||
biz
|
||
; ---- 1. 乘数这一位是 1 就把被乘数加到积的高半:(C:PH) += MC
|
||
; 进位出到 Ci,再搬进 F(搬之前 F 一定是 0)。
|
||
; `biz`/`rnz` 是一对循环,不是 if/else:body 必须把被测位吃掉(最后那个
|
||
; flb),否则 rnz 看到还是 1 就跳回去。被测的正好是 PL 的最低位,
|
||
; 而它本来就要在右移时被丢掉,吃掉它不影响移位。
|
||
.tp %3
|
||
biz
|
||
.tp %1
|
||
copy %5 %8
|
||
.tp %7
|
||
set_0
|
||
clb_add_nc %4 %5 %6 %7
|
||
.tp %6
|
||
copy %4 %8
|
||
.tp %7
|
||
clb_bit_mov %9
|
||
.tp %3
|
||
flb
|
||
rnz
|
||
|
||
; ---- 2. (F:PH:PL) 右移一位:C 补进 PH 的最高位,PH 的最低位补进 PL 的最高位,
|
||
; PL 的最低位(这一轮刚看过的乘数位)丢掉。先低位那对,再高位那对。
|
||
.tp %9
|
||
ror
|
||
.tp %4
|
||
ror
|
||
.tp %3
|
||
ror
|
||
.tp %4
|
||
rol
|
||
.tp %3
|
||
rol
|
||
.tp %4
|
||
clb_bit_mov %3
|
||
.tp %9
|
||
rol
|
||
clb_bit_mov %4
|
||
.tp %9
|
||
ror
|
||
.tp %4
|
||
ror
|
||
.tp %3
|
||
ror
|
||
|
||
; ---- 3. 这一轮结束,计数器吃掉一个 1
|
||
.tp %10
|
||
set_0
|
||
ror
|
||
.tp mul_iter
|
||
rnz
|
||
|
||
.tp before_clb_mul
|
||
%endmacro
|