Files
R8Forge/asm/raw_add_nc4.r8asm
T
dsh c6bfaca349 Drop the bit per cell adders, keep the slice
Splitting every bit into its own cell bought nothing: the 25 cell adder was a
workaround for a workaround, and it made the cell count worse than the hand
written library it was meant to improve on.

Removed: raw_add_nc (5 cell, broken driver), raw_add_nc3 (the 3 cell dead end),
raw_add_bits4, raw_add_bits8, raw_inc_bits8, verify_add8, verify_inc8.

Kept:
  asm/raw_add_nc4.r8asm            the 4 cell slice, flat cost 27 against the
                                   6 cell full adder's 38, with the two
                                   constraints written down (only the low K bits
                                   are modelled, and set_0 only converges on 0/1)
  verify_bit_add_nc4.r8asm         driver that feeds A, B and the carry to one
                                   slice and prints sum and carry, 8/8 correct

The byte level stays with the hand written library, which rotates inside the
macro and keeps whole bytes in a cell; that is 6 cells for an 8 bit add instead
of 25, at the price of not being synthesised.  %synth earns its keep on the small
bit macros (asm/test.r8asm 1714 -> 1666 characters), not on byte level tables
where the 2^16 entry truth table would be hopeless anyway.
2026-11-06 23:17:19 +08:00

32 lines
1.3 KiB
Plaintext

%include "asm/lib.r8asm"
; 4 个 cell 的忽略进位加法切片:两个操作数、进位、和,没有独立的进位输出,
; 进位就在它自己的 cell 里就地更新(进->出),最高位的进位由调用者丢掉。
;
; p1..p4 = A, B, 进位(进->出), S
;
; 目标:S = A^B^Ci,进位 = AB|AC|BC,A、B 结束为 0。
; 每个输入组合后的值只落在「进位」和 S 这两位,所以表里只出现 0/4/8/12。
;
; 与 6 cell 的完整全加器切片(bit_raw_add,flat 代价 38)相比,这个切片 flat 代价
; 只有 27,少一个 cell。代价是它只适用于"和不会溢出"的场合,而且调用者必须保证
; 喂进来的 cell 里只有一位(见下)。
;
; 已知约束(实测得出,别忽略):
; 1. 合成出来的程序只碰每个 cell 的低 K 位(K = 参与运算的输入位数),高位不追踪;
; 2. set_0 展开成 [ - ],只对 0 和 1 收敛(5 会 5->4->5->4 死循环,0x80 永远不变),
; 所以被 set_0 清的 cell 必须只放一位。
; 因此这个切片只能用在"每个 cell 放一位"的场合;把和用 ror 轮转到第 7 位再喂进来
; 会死循环。
%synth bit_add_nc4_synth
params 4
stat 0 8 8 4 8 4 4 12 0 8 8 4 8 4 4 12
metric flat
%endsynth
%macro bit_add_nc4 4
.tp %4
set_0
bit_add_nc4_synth %1 %2 %3 %4
%endmacro