Files
R8Forge/asm/digits.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

120 lines
4.4 KiB
Plaintext

%include "asm/divmod.r8asm"
%include "asm/add_ip.r8asm"
; 整数 -> 十进制字符串:把值展开成 4 个连续 cell,每格低 8 位放一个 ASCII 数字。
;
; clb_digits N REG S ; REG..REG+3 得到 "2047" 那样的 4 个字节
;
; 目标形态:**ASCII '0'..'9'、高位在前、定长 4 位、左边补 '0'**。
; 11 位 cell 装得下 0..2047,也就是 4 位十进制,所以 L 写死 4。
;
; 朴素做法是 4 份 divmod(4x4499 ≈ 18k)。这里让 **divmod 只出现一次**:外面套一个
; 跑 4 次的运行期循环,而且**不做"写哪一格"的分派** —— 每轮把已经写好的数字整体
; 往高地址挪一格,新数字永远写 REG+0:
;
; 第 1 轮: 挪(空的) ; REG+0 = 个位
; 第 2 轮: 挪(个位 -> REG+1) ; REG+0 = 十位
; ...
; 第 4 轮: 挪(三位 -> 高位) ; REG+0 = 千位
;
; 挪的顺序必须**从高到低**(REG+2 -> REG+3、REG+1 -> REG+2、REG+0 -> REG+1),
; 否则先把低位搬上去会盖掉还没搬的高位。这样最后 REG+0..3 正好是千、百、十、个,
; 也就是"高位在前"的字符串,不用倒序、不用 4 个分支。
;
; **位数、挪位链、常数构建三者都跟着 `_CELL_SIZE` 走**:位数和链长用
; `%for k <count>`(见 SPEC §3.5),常数构建的尾部转数写成 `_CELL_SIZE - 5` /
; `_CELL_SIZE - 3`。11 位 -> 4 位十进制 / 3 轮挪位;16 位 -> 5 位 / 4 轮
; (实测 16 位下 `4660 -> "04660"`、`7 -> "00007"`)。
;
; (第一版是 4 个分支 + one-hot flag 分派,只跑对了第一支 —— 甩掉分派之后不仅
; 正确,还少了两层嵌套和 4 个 cell。)
;
; 三个踩过的坑:
; * **`copy` 的缓冲格必须是干净的**:缓冲带垃圾时拷贝结果是 0(实测)。
; * **`%synth` 那句老话反过来**:造常数不能"置一位转到位、再置一位转到位" ——
; 第二次旋转会把第一次放好的位一起带走;要**绕整圈走一次**。
; * **`clb_divmod` 的 N、D 现在都保留**,余数是单独一格;下一轮的 N 还是靠
; `copy` 从商搬回去(旧版是"余数就在 N 里",这里多一格、少一次搬)。
;
; 前提:N = 值(结束时为 0);REG 起 4 格、S 起 13 格连续且干净。没有负数。
;
; S 起的格子是这么排的(divmod 要一块 7 格的连续草稿,所以它挪到 S+3):
; S+0 TEN(除数) ; S+1 商 ; S+2 余数(这一位的数字)
; S+3..S+9 clb_divmod 的草稿块(Rhi、Sd、Bo、Cm、Ci、buf、NR)
; S+10 轮数计数器(loop) ; S+11/S+12 clb_add_ip 的 T、F
; Macro clb_digits
; %1: Cell N
; %2: Cell REG
; %3: Cell S
;
; N 是值(被吃干净);REG..REG+3 是输出,ASCII 数字,高位在前;S 起的 13 个连续
; cell 是草稿(S=10、S+1=商、S+2=余数、S+3..S+9=divmod 草稿块、S+10=轮数、
; S+11/S+12=add_ip 的草稿),进来要求干净。
%macro clb_digits 3
.anchor before_clb_digits
; ---- K = L 个连续的 1(第 0..L-1 位),给 loop 用
;
; L = 十进制位数 = floor(_CELL_SIZE * log10(2)) + 1,用 30103/100000 做整数近似:
; 11 位 -> 4、16 位 -> 5、8 位 -> 3、32 位 -> 10,和 2^W-1 的真实位数一致。
; **轮数因此自动跟着位宽走**,不用手改。
.tp %3+10
set_1
%rep _CELL_SIZE * 30103 / 100000
rol
set_1
%endrep
; ---- 四位,一轮一位
loop %3+10
; 重建 TEN = 10(位 1、3):绕整圈,在 1 和 3 各置一个 1
erase %3+0
.tp %3+0
ror
set_1
%rep 2
ror
%endrep
set_1
%rep _CELL_SIZE - 3
ror
%endrep
; N = 这位数字(R,余数),Q = 商
clb_divmod %1 %3+0 %3+1 %3+2 %3+3
; ---- 已有数字整体上移一格:从高到低搬。
; 轮数 = L-1,用 %for 跟着位宽自动来(11 位 3 轮、16 位 4 轮)。
%for k _CELL_SIZE * 30103 / 100000
erase %3+8
.tp %2 + _CELL_SIZE * 30103 / 100000 - 1 - k
copy %2 + _CELL_SIZE * 30103 / 100000 - k %3+8
%endfor
; ---- REG+0 = '0' + 数字(位 4、5)。
; 走一圈:转 4 格置位、再转 1 格置位,最后转回原点。
; **尾部的转数必须写成 `_CELL_SIZE - 5`**,不能写死 6 —— 16 位下
; 写死会让 0x30 建错(实测 16 位时得到 0x600)。
erase %2+0
.tp %2+0
%rep 4
ror
%endrep
set_1
ror
set_1
%rep _CELL_SIZE - 5
ror
%endrep
clb_add_ip %2+0 %3+2 %3+11 %3+12
; ---- 下一位的被除数(缓冲要干净)
erase %3+8
.tp %3+1
copy %1 %3+8
endloop %3+10
.tp before_clb_digits
%endmacro