Asked why an in place complement does not exist, given that in place inversion
does. The inversion half is now hoisted out of the per bit loop, which is both
simpler and shorter:
clb_neg_nc A S Ci = clb_not_ip A ; clb_inc_nc A S Ci 468 -> 448
clb_sub_nc A B S Ci = clb_not_ip B ; clb_add_nc A B S Ci 725 -> 707
and the unsigned complement through the same macro (carry in at 0) drops 467 ->
447. B is eaten by the add anyway, so inverting it in place costs nothing. The
per bit `flb` wrappers (clb_bit_neg_nc, clb_bit_sub_nc) are gone with them, so
macro_docs_test now counts 35 macros.
What does not work is the other half: there is no in place increment, so the sum
still has to land in S. Two independent reasons, both written down:
* A synthesised block has exactly one condition cell, so it cannot tell
(A=1, Ci=0) from (A=1, Ci=1) -- and that distinction is the whole carry rule.
All four in place tables answer "no program up to 8 layers".
* Hand writing cannot do it with two cells either: `[ ... ]` is the only one shot
conditional this ISA has, and it eats the bit it tests, while in place
increment needs both cells' bits alive at the same time.
So one cell buys the ones' complement only; a two's complement negate costs three
(A, S, carry). tests unchanged and green: ten byte level operations, 1666
character test tape, 8/8 full adder, fibonacci.
Also: the docs quoted clb_neg_nc at 468 and clb_sub_nc at 725, now 448 and 707.
30 lines
803 B
Plaintext
30 lines
803 B
Plaintext
%include "asm/not_ip.r8asm"
|
|
%include "asm/raw_add_nc.r8asm"
|
|
|
|
; 减法:S = A + ~B + Ci。
|
|
;
|
|
; 同上,取反挪到循环外面:先把 B 整个字节原地取反(clb_not_ip),再走 clb_add_nc。
|
|
; B 本来就会被加法吃掉,所以在原地取反它没有任何代价。
|
|
;
|
|
; Ci 是真正的进位输入:
|
|
; Ci = 1 -> S = A - B
|
|
; Ci = 0 -> S = A - B - 1
|
|
; 最后一轮之后 Ci 是"没有借位"(A >= B 时为 1)—— 顺带就是无符号比较,
|
|
; 而且多字节减法可以把这一位直接接到下一字节的进位输入。
|
|
|
|
; Macro clb_sub_nc
|
|
; %1: Cell A
|
|
; %2: Cell B
|
|
; %3: Cell S
|
|
; %4: CellBit Ci
|
|
;
|
|
; S = A + ~B + Ci,A、B 都被吃干净。
|
|
%macro clb_sub_nc 4
|
|
.anchor before_clb_sub_nc
|
|
|
|
clb_not_ip %2
|
|
clb_add_nc %1 %2 %3 %4
|
|
|
|
.tp before_clb_sub_nc
|
|
%endmacro
|