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

39 lines
932 B
Plaintext

; raw_sub:保留 A、B 的减法 S = A - B;Bo = 1 表示 A >= B(没借位)。
; 契约见 SPEC §7.8。
;
%include "asm/raw_add.r8asm"
%include "asm/not_ip.r8asm"
;
; Macro raw_sub
; %1: Cell A
; %2: Cell B
; %3: Cell S
; %4: Cell Bo
; %5: Cell Bc
; %6: Cell Ci
; %7: Cell buf
;
; S = A - B(mod 2^W),A、B 保持原样,Bo = 1 表示 A >= B(没有借位)。
; Bc、Ci、buf 是草稿(进来要干净),结束时 Ci 和 buf 是垃圾、Bc 是 ~B。
%macro raw_sub 7
.anchor before_raw_sub
; Bc = ~B
.tp %2
copy %5 %7
clb_not_ip %5
; Ci = 1,做的是"加补码"
.tp %6
set_1
; S = A + Bc + 1,进位出到 Bo
raw_add %1 %5 %6 %3 %4 %7
; 注意:这里**不能**再 `clb_not_ip %4`。`clb_not_ip` 是整格取反(~carry),
; 得到的是 2046/2047 而不是 0/1(实测)。所以 Bo 和 `clb_sub_nc` 的进位一样,
; 语义是"**没有借位**":Bo = 1 表示 A >= B。
.tp before_raw_sub
%endmacro