Asked whether two's complement can be done in place. The negate cannot, but the
inversion half can and is absurdly cheap, so it is a macro now:
clb_not_ip A A = ~A, one cell, 17 characters, 256/256
`%rep 8 { flb; ror }` -- flip the low bit, rotate the next one down, eight times
puts the byte back where it started. The same result through clb_neg_nc with its
carry at 0 costs 467 characters, because that path runs a synthesised slice once
per bit.
The in place two's complement itself: tried four tables and none of them exist.
A <- ~A ^ Ci A <- A ^ Ci A <- A ^ B ^ Ci A <- A ^ ~B ^ Ci
no program up to 8 layers (all four)
Two separate reasons, both worth keeping:
* Synthesis cannot: a block has exactly one condition cell. In place increment
has to tell (A=1, Ci=0) -> (1,0) from (A=1, Ci=1) -> (0,1); A is the same in
both and only Ci differs, and one condition cannot look at two cells.
* Hand writing cannot either (2 cells): `[ ... ]` is a while loop and the only
one shot conditional this ISA has, and it eats the bit it tests. In place
negate wants "flip the low bit every round, then decide the carry from it",
so both cells hold live bits and the test destroys one.
So a negate still has to land in a separate S (clb_neg_nc, 3 cells); one cell
buys the ones' complement only. byte_op_test is ten operations now, AGENTS.md
and README.md carry the distinction and the two reasons.
29 lines
914 B
Plaintext
29 lines
914 B
Plaintext
%include "asm/lib.r8asm"
|
|
|
|
; 原地取反(反码):A <- ~A,只要一个 cell,16 条指令。
|
|
;
|
|
; 做法就是"翻最低位 + 转一格"重复八次:flb 翻当前位,ror 把下一位转到最低位。
|
|
; 转八次正好回到原位,所以别的 cell 一位都不用。
|
|
;
|
|
; 注意这是**反码**(逐位取反),不是补码。补码要再加 1,而"原地加 1"需要进位链,
|
|
; 那个做不了 —— 见 AGENTS.md:
|
|
; * 合成的块只有一个条件 cell,原地写回却要同时看 A 和进位;
|
|
; * `[ ... ]` 是一次性条件唯一的手段,而它必然消耗被测的那一位,两个 cell 的位都是活的。
|
|
; 所以补码取负用 clb_neg_nc(结果在 S,3 个 cell)。
|
|
|
|
; Macro clb_not_ip
|
|
; %1: Cell A
|
|
;
|
|
; A <- ~A(反码),原地,不需要额外的 cell。
|
|
%macro clb_not_ip 1
|
|
.anchor before_clb_not_ip
|
|
|
|
.tp %1
|
|
%rep _CELL_SIZE
|
|
flb
|
|
ror
|
|
%endrep
|
|
|
|
.tp before_clb_not_ip
|
|
%endmacro
|