Files
R8Forge/asm/raw_add_bits4.r8asm
T
dsh 7cf2fabeb7 A bit per cell adder, and it works
The chained version kept every bit in one cell and rotated, which broke the rule
that set_0 needs a cell holding 0 or 1.  This one gives each bit its own cell
instead:

    %1..%4  A0..A3      %5..%8  B0..B3
    %9..%12 S0..S3      %13     carry

    erase carry
    bit_add_nc4 A0 B0 carry S0
    ... one slice per bit, no rotation at all
    last carry dropped (assumes A+B fits)

No rotation means every cell stays a bit, which is the invariant set_0 needs.
Verified against the built in machine over eight sums including 0+0, 1+1,
15+0 and 9+6: 8/8 correct, and the cell dump after a run shows only the S bits
set, so the operands and the carry are consumed as the table says.

Cost: it needs three cells per bit plus the carry (13 for 4 bits, 25 for 8)
instead of 4 rotating cells, and the expansion is longer because each slice now
has a fixed pair of operands.  That is the price of not breaking the invariant,
and it is why the original hand written library rotates inside the macro instead:
it keeps whole 8 bit cells and loops over the bits itself.
2026-11-06 22:06:58 +08:00

25 lines
792 B
Plaintext

%include "asm/lib.r8asm"
%include "asm/raw_add_nc4.r8asm"
; 4 位加法器,每一位独占一个干净 cell,不做轮转:
; %1..%4 = A0..A3
; %5..%8 = B0..B3
; %9..%12 = S0..S3
; %13 = 进位
;
; 这样每个 cell 始终只放一位(0 或 1),set_0 的前提才成立 —— 这正是之前 8 位
; 轮转版卡死的原因:ror 把和挪到第 7 位之后,set_0(= 只要非零就翻最低位)再也
; 收敛不了。代价是每多一位要多两个 cell,好处是彻底没有这个坑。
;
; 最高位产生的进位丢掉:前提是 A+B <= 2^n-1,不进位输出。
%macro raw_add_bits4 13
.anchor before_raw_add_bits4
erase %13
bit_add_nc4 %1 %5 %13 %9
bit_add_nc4 %2 %6 %13 %10
bit_add_nc4 %3 %7 %13 %11
bit_add_nc4 %4 %8 %13 %12
.tp before_raw_add_bits4
%endmacro