Files
R8Forge/asm/digits.r8asm
T
dsh 3543003a78 digits: reorder the scratch block -- 22% fewer steps, 15% less tape
The profile said 77% of clb_digits' steps are pointer moves, so the layout of its
13 cell scratch block (S..S+12) is the cost.  The block is one rigid 7 cell unit
(clb_divmod owns its internal order: Rhi,Sd,Bo,Cm=Bc,Ci,buf,NR) plus six free
units, so the legal space is 5040 permutations.  All 5040 were assembled and run
(minimal driver, value 2047):

  old: TEN Q R [block] counter T F
  new: T1 R [block] TEN Q F counter

   tape 13,717 -> 11,717 chars (-14.6%)
   steps 145,114 -> 113,264 (-21.9%)
   pointer moves 111,958 -> 80,108 (-28.4%)   non-move steps identical (33,156)

Full 2048 value sweep: worst 164,843 -> 123,731 (-24.9%), mean 144,904 -> 112,552
(-22.3%), better on all 2048 values.  The old layout ranked 3459/5040.

Two things worth remembering.  First, ordering the units by the wrapper's first
visit order is *worse* than the old layout (150,748 steps): first visit ignores
revisit frequency -- the counter is touched once per round, TEN is copied into Bc
every round, T1 is hammered by clb_add_ip's carry chain.  Second, the n-1 lower
bound is not reachable here: 74% of the steps still execute inside clb_divmod's
block, whose order is not clb_digits' to choose, so this lever is now exhausted
(that is what the exhaustive search proves).

Min tape is not min steps either: block-first BQTRT1F1C gives the shortest tape
(10,713) but 123,890 steps.  Tape counts a .tp once, steps count its execution.

Ripple: asm/test.r8asm 64,513 -> 62,513 chars, 236,439 -> 206,491 steps, INS_SIZE
headroom ~1k -> ~3k; --opt 58,060 -> 56,350; calc --opt 52,857; calc_op 77,356 ->
69,324 chars / 599,090 -> 476,170 steps.

Verified: ctest 8/8, byte_op_test 21 items (digits 2048/2048), examples_test 18,
--opt assembles, SPEC 7.7/10.2 + AGENTS + README + docs/calc.md + docs/divmod.md
numbers updated.  New driver drv/digits_layout.r8asm keeps the measurement honest
(one clb_digits call, no output, value fed as 0=2047).
2026-11-18 17:27:05 +08:00

106 lines
3.9 KiB
Plaintext

; clb_digits:整数 -> 4 位 ASCII 十进制(高位在前、定长 4 位)。
; 契约和纸带长度见 SPEC §7.7,设计说明见 docs/byte-ops.md。
;
%include "asm/divmod.r8asm"
%include "asm/add_ip.r8asm"
;
; Macro clb_digits
; %1: Cell N
; %2: Cell REG
; %3: Cell S
;
; N 是值(被吃干净);REG..REG+3 是输出,ASCII 数字,高位在前;S 起的 13 个连续
; cell 是草稿,进来要求干净。
;
; ---- 13 格草稿的排法(按"少走路"重排过,见下面"布局"一段) ----------------
;
; S+0 clb_add_ip 的 T S+1 余数 R(这一位的数字)
; S+2..S+8 clb_divmod 的草稿块(Rhi、Sd、Bo、Cm=Bc、Ci、buf、NR)
; S+9 TEN(除数) S+10 商 Q
; S+11 clb_add_ip 的 F S+12 轮数计数器(loop)
;
; ---- 布局:这台机器没有随机寻址,每次访问的代价就是 |Δ地址| --------------
;
; 旧排法是 TEN、商、余数、草稿块、轮数、add_ip 的 T、F 依次排下来,结果每轮都要
; 在块前面和块里面来回走:divmod 每轮拿 TEN 当 raw_sub 的被减数(copy TEN -> Bc,
; 缓冲就是块里的 buf),TEN 却离 buf 有 8 格。现在的三条依据:
;
; * 余数 R 紧贴在草稿块**前面**、TEN 紧贴在**后面**。R 是 raw_add 的 A,TEN 是
; raw_sub 的 B,这两个距离都乘进内层 11×11 的位循环里。实测:只把 TEN 从最前
; 面挪到块后面(其它单元的先后不变)就省 22,226 步,是这次改动的大头。
; * add_ip 的 T 紧挨着 R(clb_inc_ip / clb_dec_ip 的进位链每轮在这两格之间来回);
; 光把 T 和 R 换个位置就差 4,450 步。
; * 轮数计数器只被 loop 每轮碰一次,踢到最远的一头(S+12)。
;
; 效果(最小驱动,见 SPEC §7.7):纸带 13,717 -> 11,717 字符;2047 这个值
; 145,114 -> 113,264 步。2048 个值全扫:最坏 164,843 -> 123,731(值 1999)、
; 平均 144,904 -> 112,552。**契约一个字没改**:还是"REG 起 4 格、S 起 13 格
; 连续且干净",只有格子内部的排法变了。
%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+12 ; 轮数计数器
set_1
%rep _CELL_SIZE * 30103 / 100000
rol
set_1
%endrep
; ---- 四位,一轮一位
loop %3+12
; 重建 TEN = 10(位 1、3):绕整圈,在 1 和 3 各置一个 1
erase %3+9 ; TEN
.tp %3+9
ror
set_1
%rep 2
ror
%endrep
set_1
%rep _CELL_SIZE - 3
ror
%endrep
; N = 这位数字(R,余数),Q = 商
clb_divmod %1 %3+9 %3+10 %3+1 %3+2 ; TEN、Q、R、草稿块
; ---- 已有数字整体上移一格:从高到低搬。
; 轮数 = L-1,用 %for 跟着位宽自动来(11 位 3 轮、16 位 4 轮)。
; 缓冲借 divmod 的 buf(S+7),它在 divmod 结束时是干净的。
%for k _CELL_SIZE * 30103 / 100000
erase %3+7 ; buf
.tp %2 + _CELL_SIZE * 30103 / 100000 - 1 - k
copy %2 + _CELL_SIZE * 30103 / 100000 - k %3+7
%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+1 %3+0 %3+11 ; A=REG+0、B=R、T=T1、F
; ---- 下一位的被除数(缓冲要干净)
erase %3+7 ; buf
.tp %3+10 ; Q
copy %1 %3+7 ; N = Q
endloop %3+12
.tp before_clb_digits
%endmacro