Files
R8Forge/asm/inc_ip.r8asm
dsh d9689f81ba asm: drop the design-story comments, leave the contract (and a pointer)
Seven files still opened with a paragraph about how they came to be -- "the first
version cleared the same bit four times", "the older repeated-subtraction divmod
needed 1.48M steps", "last round rejected shift-and-add because it was longer",
nested with measured step counts.  None of that tells a caller how to use the
macro, and it is exactly the material that goes stale (raw_sub's figure had been
retracted, and the numbers are SPEC's job now).  The stories already live in
docs/divmod.md and docs/byte-ops.md, so:

    digits    119 -> 80 lines     mul      125 -> 98
    divmod    106 -> 80           inc_ip    81 -> 72
    undigits   95 -> 73           raw_sub   52 -> 38
    dec_ip     21 -> 18

Each now opens with two lines: what the macro computes and where the contract
lives (SPEC section, plus docs/ when there is a design write-up).  Everything
from the `;   Macro <name>` block down -- parameter types, preconditions,
what-is-eaten, the non-obvious invariants -- is untouched, which is what
r8forge_macro_docs_test checks and what a caller actually reads.

Baseline unchanged: tape 64513, 21 byte-op items, 15 examples, 48 macros.
2026-11-09 17:20:44 +08:00

73 lines
1.1 KiB
Plaintext

; clb_inc_ip:原地自增 A = A + 1(A、T 两个 cell)。
; 契约见 SPEC §8。
;
%include "asm/not_ip.r8asm"
;
; Macro clb_inc_ip
; %1: Cell A
; %2: Cell T
;
; A <- A + 1,原地。T 是草稿 cell,只要求原来是干净的;结束时是 0。
%macro clb_inc_ip 2
.anchor before_clb_inc_ip
; 计数器:T = 0xFF
.tp %2
%rep _CELL_SIZE
set_1
ror
%endrep
; 1) 清掉最低位连续的 1
.tp %1
biz
flb
ror
.tp %2
set_0
ror
.tp %1
rnz
; 2) 第一个 0 翻成 1(0xFF 时跳过)
.tp %2
biz
.tp %1
flb
ror
.tp %2
set_0
rnz
; 计数器补一次转位(上一步只消了 1,没有转)
.tp %2
ror
; 3) 剩下的位置只旋转
biz
.tp %1
ror
.tp %2
set_0
ror
rnz
.tp before_clb_inc_ip
%endmacro
; Macro clb_neg_ip
; %1: Cell A
; %2: Cell T
;
; 原地取补码:A <- -A,就是"原地取反 + 原地自增",两个 cell 就够。
; 比 clb_neg_nc 少一个 cell(不需要单独的 S),也短得多。T 是草稿 cell,
; 只要求原来是干净的。
%macro clb_neg_ip 2
.anchor before_clb_neg_ip
clb_not_ip %1
clb_inc_ip %1 %2
.tp before_clb_neg_ip
%endmacro