Files
R8Forge/asm/divmod.r8asm
T
dsh 1f3f453e88 signed: the two two's complement wrappers, with exhaustive sweeps
clb_digits_signed N REG S T F prints '-' (45) and then |N| as the same four
zero padded digits; clb_divmod_signed N D Q R S T1 T2 F is the C style
truncated division (q = trunc(n/d), r = n - q*d, remainder follows the
dividend).  Both are wrappers: clb_digits / clb_divmod still do the work, so
the tape only grows by the sign handling -- 572 characters for the display
(12151 vs 11579 in the same driver layout) and 1191 for the division
(6018 vs 4827).

The sign bit is read with rol + bit_copy + ror, 49 characters.  clb_bit_mov is
deliberately NOT used: it eats the source bit, so N comes back as N - HALF and
1024 -- whose magnitude 1024 only fits the unsigned range -- prints as 0000.
A clb_sub_nc against a HALF constant was the other candidate: 765 characters
for the call plus ~60 for the constant cell.

A flag has to outlive the block that consumes it, because biz/rnz only exit
once the body clears the condition bit.  So sign(N) is moved into T2, sign(D)
into F, and the remainder block flips F into sign(N)^sign(D) on its way out;
a single conditional negation of the quotient then carries the xor.  The
negation scratch is S+6: the only cell in S with a "clean on entry" contract,
and clb_neg_ip leaves it zero again for clb_divmod.

Verified by r8forge_byte_op_test: all 2048 values for the display (worst
117606 steps) and 4202 division pairs -- every |a|,|b| <= 32 plus the
0/+-1/+-1023/+-1024 boundary combinations -- compared against C's / and %.
examples/signed_demo.r8asm is the single point regression that runs as a
program (the flagship cannot carry them: +12.1k / +5.6k on a 62351 tape).
2026-11-18 21:13:52 +08:00

192 lines
6.8 KiB
Plaintext

; clb_divmod:二进制长除法(移位-恢复),商 Q、余数 R 各占一格,N、D 保留。
; 契约见 SPEC §7.6,设计推理和实测见 docs/divmod.md。
;
%include "asm/raw_sub.r8asm"
%include "asm/raw_or.r8asm"
%include "asm/inc_ip.r8asm" ; clb_neg_ip(带符号除法要用)
;
; 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 是草稿:进来**没有一格要求是干净的**。buf 现在在 S+6,
; 宏开头先 `erase` 它 —— 它是 copy/bit_copy 的缓冲,缓冲带垃圾时拷贝结果是 0。
; 其它格子脏了没关系:要么整格重写(raw_add 逐位写 Sd/Bo),要么只按一位用
; (Rhi/Cm/Bo/Ci),NR 每次进来都会重建成 W 个 1。
; (`clb_digits` 就是这么反复用同一块草稿的:第二轮起它是脏的。)
; 前提 D >= 1(除零没有定义,这里不检查)。
%macro clb_divmod 5
.anchor before_clb_divmod
; s+0 Rhi : 本轮左移出来的高位(轮内布尔)
; s+1 NR : 轮数计数器
; s+2 Cm : 提交标志 = Rhi | Bo(借 raw_sub 的 Bc 那个草稿格用)
; s+3 Ci : raw_sub 的进位草稿
; s+4 Bo : raw_sub 的"没借位"(1 表示 Rlo >= D)
; s+5 Sd : 减法结果
; s+6 buf : 拷贝缓冲(bit_copy / copy 共用,先后用完都是干净的),入口先 erase
;
; 这张表是 5040 种"角色 -> s+k"排列全部汇编、在最小驱动上跑 0=2047 量出来的
; (docs/divmod.md 第 6 节)。buf 不在 S+5 的排列多付一条入口 erase,成本已经
; 算进搜索里;换来的好处是草稿区从此不需要调用者保证任何一格干净。
erase %4
erase %3
erase %5+6
; 轮数:_CELL_SIZE 个 1,每轮 set_0 + ror 吃掉一个。一串 1 的长度就是剩余轮数,
; 所以"做几轮"不占纸带长度,只占运行时间(和 clb_mul 一样的计数法子)。
.tp %5+1
%rep _CELL_SIZE
set_1
ror
%endrep
.tp %5+1
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+6 ; Rlo 的 bit0 = N 的 LSB(N 保留)
; ---- 2. S = Rlo - D,Bo = (Rlo >= D)。D 不能被吃,所以走 raw_sub
raw_sub %4 %2 %5+5 %5+4 %5+2 %5+3 %5+6
; ---- 3. 提交条件 = Rhi | Bo(clb_bit_or 把两个输入都吃掉,Rhi 因此归零)
clb_bit_or %5+0 %5+4 %5+2
; ---- 4. 商左旋一位;提交时 R = S,商位记 1
.tp %3
rol ; rol 之后 Q 的 bit0 一定是 0(还没写满 W 位)
.tp %5+2
biz
.tp %5+5
copy %4 %5+6 ; R = S
.tp %3
set_1 ; 商位 = 1
.tp %5+2
flb ; 把条件吃掉(biz/rnz 是循环对)
rnz
; ---- 5. 这一轮结束,轮数吃掉一个
.tp %5+1
set_0
ror
rnz
.tp before_clb_divmod
%endmacro
; Macro clb_divmod_signed
; %1: Cell N
; %2: Cell D
; %3: Cell Q
; %4: Cell R
; %5: Cell S
; %6: Cell T1
; %7: Cell T2
; %8: CellBit F
;
; 带符号的 clb_divmod:把 N、D 按二补码解释,给出 **C 语义的截断除法**
; (q = trunc(n/d)、r = n - q*d,余数与被除数同号,商向零取整):
;
; 7 2 -> Q= 3 R= 1 -7 2 -> Q=-3 R=-1
; 7 -2 -> Q=-3 R= 1 -7 -2 -> Q= 3 R=-1
; -7 2 的两位十进制就是 "-0003" / "-0001"(配 clb_digits_signed)
;
; N、D 被吃成各自的**绝对值**(|N|、|D|);Q 是商、R 是余数(都带符号,宏自己覆盖)。
; S 起的 7 格草稿只要 S+6(buf)干净(和 clb_divmod 一样);T1、T2 进来必须干净、
; 结束时都是 0;F 只用最低位,进来最低位必须是 0,结束时也是 0。
;
; ---- 做法:取两个符号位 -> 各取绝对值 -> 无符号长除 -> 回填符号 ----
;
; 截断语义不需要额外判断:对绝对值做 clb_divmod 得到的 |q|、|r|,余数的符号就是
; 被除数的符号,商的符号是两个符号位的异或 —— 这正好是 C 的 trunc 语义。
;
; 符号位怎么读和 clb_digits_signed 一样:`rol` 把 bit W-1 转到 bit0,`bit_copy`
; (保留源)抄进标志格,再 `ror` 转回去。**不能**用 clb_bit_mov 抄,它会吃掉源位,
; 于是 N 少了符号位、取绝对值时会差一个 HALF。
;
; ---- 四个条件块和"标志位怎么活过 clb_divmod" ----
;
; 符号位一共产出两次用途:除法**前**(决定要不要取负)和除法**后**(决定 Q、R 的
; 符号)。而 `biz`/`rnz` 是循环对,块的身体必须把它那个条件位清掉才能退出 ——
; 所以标志位用完就没了。于是"用完还要留"的那一位要去另一格存着:
;
; T2 = sign(N) (第 2 步 clb_bit_mov 搬进去)
; F = sign(D) (第 3 步搬进去)
; S+6 = 取负用的草稿格
;
; **为什么草稿格用 S+6**:clb_neg_ip 要求草稿格进来是干净的,而 S 里只有 S+6
; (buf)有"干净"这个契约;clb_neg_ip 用完它就归零,所以 clb_divmod 进场时它照样干净。
;
; 第 5 步顺便把 F 从 sign(D) 翻成 sign(N)^sign(D):那一支进来就说明 sign(N)=1,
; `flb` 一下正好;sign(N)=0 时整支跳过,F 保持 sign(D) = 0^sign(D),还是异或。
;
; ---- 代价 ----
;
; 宏本体(最小驱动)≈ 700 字符,大头是四次 clb_neg_ip(每次 110)。没有再省:
; Q、R 的符号各要一次条件取负,N、D 各要一次,四次里没有一次是多余的。
%macro clb_divmod_signed 8
.anchor before_clb_divmod_signed
; ---- 1) 两个符号位:F = sign(N)、T1 = sign(D);N、D 原样还回去
.tp %1
rol
.tp %1
bit_copy %8 %5+6 ; F = sign(N),N 保留(S+6 当缓冲,结束是干净的)
.tp %1
ror
.tp %2
rol
.tp %2
bit_copy %6 %5+6 ; T1 = sign(D),D 保留
.tp %2
ror
; ---- 2) |N|:负数取负;sign(N) 搬进 T2 存着(余数还要用)
.tp %8
biz
clb_neg_ip %1 %5+6 ; N = |N|
clb_bit_mov %7 ; T2 = sign(N),F 归零(身体结束在条件格 F 上)
rnz
; ---- 3) |D|:同上,sign(D) 搬进 F 存着
.tp %6
biz
clb_neg_ip %2 %5+6 ; D = |D|
clb_bit_mov %8 ; F = sign(D),T1 归零
rnz
; ---- 4) 绝对值上的无符号长除法
clb_divmod %1 %2 %3 %4 %5
; ---- 5) 余数取被除数的符号;顺手把 F 变成商的符号
.tp %7
biz
clb_neg_ip %4 %6 ; R = -R(T1 这时是干净的)
flb ; 吃掉条件 T2
.tp %8
flb ; F = sign(D) ^ sign(N)
.tp %7 ; 身体回到条件格 T2
rnz
; ---- 6) 商取异或符号
.tp %8
biz
clb_neg_ip %3 %6 ; Q = -Q
flb ; 吃掉条件 F
rnz
.tp before_clb_divmod_signed
%endmacro