In place increment in two cells, 74 characters, by counting rotations in the
scratch cell's bits
I said a two cell in place increment was impossible. It is not. The version in
asm/inc_ip.r8asm does A = A + 1 with A and one scratch cell, 74 characters,
256/256 exhaustive, and there is a two cell in place negate on top of it:
clb_not_ip A A = ~A 1 cell 17 chars 256/256
clb_inc_ip A T A = A + 1 2 cells 74 chars 256/256
clb_neg_ip A T A = -A 2 cells 90 chars 256/256
against clb_inc_nc (3 cells, 430) and clb_neg_nc (3 cells, 448). Five times
shorter, one cell less.
Both of my earlier impossibility arguments were about a *specific* algorithm, the
per bit carry ripple, and I stated them as if they covered the problem. The one
worth remembering is the synthesis one, which is still true: a synthesised block
has exactly one condition cell, so it cannot tell (A=1, Ci=0) from (A=1, Ci=1),
and all four in place tables answer "no program up to 8 layers". The hand
written argument was simply wrong.
The algorithm that works does not test the carry per bit at all:
1. `[ ... ]` already means "while the low bit is set", so clearing the run of
low ones is a loop that falls out on the first zero (flb + ror);
2. flip that zero to one -- skip it when the input is all ones, which is exactly
the wrap-around case;
3. rotate the rest of the way.
What is left is "how many rotations remain", with only two cells. The counter
goes into the scratch cell's bits: T starts as 0xFF, and every rotation does
set_0 then ror, which eats one 1 -- the length of the run of ones is the number
of rotations left. Three characters per step, and a full turn counts to eight.
The wrap case falls out for free: when the input was 0xFF the counter is spent
exactly then, so step 2 is skipped and the answer is 0.
Two smaller notes. The dp rule from the last commit is what these macros had to
respect -- every conditional body returns to its own condition cell -- and
`[ ... ]` consuming its condition is only a problem when the condition is data;
here it is the counter. And I mangled asm/inc_ip.r8asm once with a PowerShell
Get-Content / Set-Content round trip (the trap AGENTS.md warns about) and had to
rewrite it with the editor; no other file was touched.
byte_op_test is twelve operations now, still exhaustive, still under a second.
This commit is contained in:
1 parent
349b6d2748
commit
2f79898e3d
6 files changed
+215
-25
No files matched your search
@@ -0,0 +1,81 @@
|
||||
%include "asm/not_ip.r8asm"
|
||||
|
||||
; 原地自增:A <- A + 1,结果留在 A 里,只要两个 cell(A 和草稿 T)。
|
||||
;
|
||||
; 关键是把"还要转多少格"这个计数塞进 T 的低位那一串 1 里:
|
||||
; T 先做成 0xFF,每转一格就 `set_0` + `ror` 消掉一个 1 —— 一串 1 的长度就是剩余圈数。
|
||||
; 于是不需要第三个 cell,也不需要"无条件翻转"(那在 ROT-8 里表达不出来)。
|
||||
;
|
||||
; 三步:
|
||||
; 1. 最低位连续的 1 逐个清掉(边清边转,同时消计数器);遇到第一个 0 停下;
|
||||
; 2. 那个 0 翻成 1(输入是 0xFF 时计数器正好是 0,这一步跳过 —— 结果就是 0);
|
||||
; 3. 剩下的位置只旋转,转到整圈走完为止。
|
||||
; 三段合起来 A 正好转八格,所以位置对得上。
|
||||
|
||||
; Macro clb_inc_ip
|
||||
; %1: Cell A
|
||||
; %2: Cell T
|
||||
;
|
||||
; A <- A + 1,原地。T 是草稿 cell,只要求原来是干净的;结束时是 0。
|
||||
%macro clb_inc_ip 2
|
||||
.anchor before_clb_inc_ip
|
||||
|
||||
; 计数器:T = 0xFF
|
||||
.tp %2
|
||||
%rep _CELL_SIZE
|
||||
set_1
|
||||
ror
|
||||
%endrep
|
||||
|
||||
; 1) 清掉最低位连续的 1
|
||||
.tp %1
|
||||
biz
|
||||
flb
|
||||
ror
|
||||
.tp %2
|
||||
set_0
|
||||
ror
|
||||
.tp %1
|
||||
rnz
|
||||
|
||||
; 2) 第一个 0 翻成 1(0xFF 时跳过)
|
||||
.tp %2
|
||||
biz
|
||||
.tp %1
|
||||
flb
|
||||
ror
|
||||
.tp %2
|
||||
set_0
|
||||
rnz
|
||||
|
||||
; 计数器补一次转位(上一步只消了 1,没有转)
|
||||
.tp %2
|
||||
ror
|
||||
|
||||
; 3) 剩下的位置只旋转
|
||||
biz
|
||||
.tp %1
|
||||
ror
|
||||
.tp %2
|
||||
set_0
|
||||
ror
|
||||
rnz
|
||||
|
||||
.tp before_clb_inc_ip
|
||||
%endmacro
|
||||
|
||||
; Macro clb_neg_ip
|
||||
; %1: Cell A
|
||||
; %2: Cell T
|
||||
;
|
||||
; 原地取补码:A <- -A,就是"原地取反 + 原地自增",两个 cell 就够。
|
||||
; 比 clb_neg_nc 少一个 cell(不需要单独的 S),也短得多。T 是草稿 cell,
|
||||
; 只要求原来是干净的。
|
||||
%macro clb_neg_ip 2
|
||||
.anchor before_clb_neg_ip
|
||||
|
||||
clb_not_ip %1
|
||||
clb_inc_ip %1 %2
|
||||
|
||||
.tp before_clb_neg_ip
|
||||
%endmacro
|
||||
Reference in new issue
Block a user