Byte level add, one bit per cell: 65536 combinations ok
The rotation based byte layer never ran. Measured with r8forge_derive -2:
raw_add A=0,B=0 / 1+1 / 3+5 / 1+0 out empty (step limit hit)
raw_add A=127,B=130 out 0 0 0 0 0, everything destroyed
raw_and 0xF0 and 0xCC, 1 and 1, 0 and 5 out empty
copy A=5 out empty (A=0 returns fine)
Not "only operand preservation is missing": set_0 (= [-]) cannot clear a cell
that holds more than one bit, and biz only answers "is this whole cell zero",
because ror does not change zero-ness. So the bits of a packed byte are
unreadable, and the whole ror + set_0 layer has no future. The docs now say
that instead of blaming operand preservation.
What replaces it is one bit per cell:
asm/byte_add.r8asm 8 bit adder. Bit i owns four adjacent cells at base+4i:
A, B, carry (in -> out), scratch. The synthesised four
cell slice bit_raw_add_nc does the bit, clb_bit_mov moves
the carry into the next quartet. A doubles as the
accumulator, so calling byte_add again continues the sum.
asm/pack_byte.r8asm eight flags -> one byte, the only direction that works.
bit_raw_flip_if consumes a flag into flipping bit 0 of an
accumulator that may already hold bits (no set_0 there).
examples/add8.r8asm a+b, 1308 characters. A shared carry cell layout needs
only 25 cells but was measured at 2517: paying .tp travel
eight times beats saving seven carry cells.
examples/add8_inc.r8asm add 1 three times, proving the block composes.
tests/byte_add_test.cc r8forge_byte_add_test: runs the tapes on the ROT-8
machine over every input pair, 0.3 s.
Also: AGENTS.md gains the measurement table, the third instruction set
constraint (packed bytes are unreadable), the new files, and the note that
build/ has to be rebuilt with the VS bundled cmake -- scoop's 4.4 reconfigures
it and dies with "No preprocessor test for PellesC".
This commit is contained in:
1 parent
9041034b6e
commit
43390efe8a
8 files changed
+502
-7
No files matched your search
@@ -0,0 +1,82 @@
|
||||
%include "asm/raw_add_nc.r8asm"
|
||||
|
||||
; 8 位加法器,一位一 cell。A 既是操作数也是累加器:算完 A = (A + B) & 0xFF,
|
||||
; 所以连着加第二个数时,只要把新的操作数摆进 B 的八个 cell 再叫一次就行。
|
||||
;
|
||||
; 为什么不是"一个 cell 放一个字节再轮转":ROT-8 没有移位,只有循环右移(ror),
|
||||
; 而 ror 不改变"这个 cell 是不是 0",所以 `biz` 对整字节永远只会回答"是不是 0"。
|
||||
; 一个带多个位的 cell,它的位在程序里读不出来 —— 而且 set_0 对多字节 cell 不收敛。
|
||||
; 实测 `asm/test.r8asm` 里的字节级 raw_add 就是这么死的(连 0+0 都跑不完)。
|
||||
; 所以字节级运算只能是一位一 cell。
|
||||
;
|
||||
; 每个 bit 用 4 个 cell 的忽略进位切片(bit_raw_add_nc):A、B 被吃掉,进位就地在
|
||||
; 第 3 个 cell 里更新。8 个 bit 各自占一整块、块内四个 cell 紧邻,切片内部的来回
|
||||
; 跳跃就只有 1~2 步;进位和"和"各自用一条 clb_bit_mov 收到下一格。
|
||||
;
|
||||
; 第 i 位(i = 0..7)占 %1 + 4i 起的四个 cell:
|
||||
;
|
||||
; %1 + 4i + 0 A_i 操作数 -> 和的第 i 位
|
||||
; %1 + 4i + 1 B_i 操作数,被吃掉(结束时 0)
|
||||
; %1 + 4i + 2 进位 进 -> 出,结束时是第 i 位的进位输出
|
||||
; %1 + 4i + 3 草稿 切片先把和写在这里,再搬进 A_i
|
||||
;
|
||||
; 第 7 位的进位就是整个加法器的进位输出,留在 %1 + 30 上。
|
||||
; B 的位必须由调用者摆;B 用完变 0,可以直接摆下一轮的操作数。
|
||||
|
||||
; Macro byte_add
|
||||
; %1: Cell base, bit i occupies %1 + 4i .. %1 + 4i + 3 as A, B, carry, scratch
|
||||
%macro byte_add 1
|
||||
.anchor before_byte_add
|
||||
|
||||
; 第 0 位的进位输入清零
|
||||
.tp %1 + 2
|
||||
set_0
|
||||
|
||||
bit_raw_add_nc %1 + 0 %1 + 1 %1 + 2 %1 + 3
|
||||
.tp %1 + 3
|
||||
clb_bit_mov %1 + 0
|
||||
.tp %1 + 2
|
||||
clb_bit_mov %1 + 6
|
||||
|
||||
bit_raw_add_nc %1 + 4 %1 + 5 %1 + 6 %1 + 7
|
||||
.tp %1 + 7
|
||||
clb_bit_mov %1 + 4
|
||||
.tp %1 + 6
|
||||
clb_bit_mov %1 + 10
|
||||
|
||||
bit_raw_add_nc %1 + 8 %1 + 9 %1 + 10 %1 + 11
|
||||
.tp %1 + 11
|
||||
clb_bit_mov %1 + 8
|
||||
.tp %1 + 10
|
||||
clb_bit_mov %1 + 14
|
||||
|
||||
bit_raw_add_nc %1 + 12 %1 + 13 %1 + 14 %1 + 15
|
||||
.tp %1 + 15
|
||||
clb_bit_mov %1 + 12
|
||||
.tp %1 + 14
|
||||
clb_bit_mov %1 + 18
|
||||
|
||||
bit_raw_add_nc %1 + 16 %1 + 17 %1 + 18 %1 + 19
|
||||
.tp %1 + 19
|
||||
clb_bit_mov %1 + 16
|
||||
.tp %1 + 18
|
||||
clb_bit_mov %1 + 22
|
||||
|
||||
bit_raw_add_nc %1 + 20 %1 + 21 %1 + 22 %1 + 23
|
||||
.tp %1 + 23
|
||||
clb_bit_mov %1 + 20
|
||||
.tp %1 + 22
|
||||
clb_bit_mov %1 + 26
|
||||
|
||||
bit_raw_add_nc %1 + 24 %1 + 25 %1 + 26 %1 + 27
|
||||
.tp %1 + 27
|
||||
clb_bit_mov %1 + 24
|
||||
.tp %1 + 26
|
||||
clb_bit_mov %1 + 30
|
||||
|
||||
bit_raw_add_nc %1 + 28 %1 + 29 %1 + 30 %1 + 31
|
||||
.tp %1 + 31
|
||||
clb_bit_mov %1 + 28
|
||||
|
||||
.tp before_byte_add
|
||||
%endmacro
|
||||
@@ -0,0 +1,75 @@
|
||||
; 把 8 个一位 cell 压成一个字节。
|
||||
;
|
||||
; 这是"位 -> 字节"方向的唯一一条路:反过来(从一个带多个位的 cell 里读第 i 位)在
|
||||
; ROT-8 里做不到 —— 没有移位,而 ror 不改变"这个 cell 是不是 0",所以 `biz` 只能回答
|
||||
; "整字节是不是 0"。所以压好的字节是只写的:算得出来、能 out 出来,但没法和 1 相加,
|
||||
; 也没法拆回一位一 cell。
|
||||
|
||||
; Macro bit_raw_flip_if
|
||||
; %this: CellBit flag (0/1; 被吃掉,结束时为 0)
|
||||
; %1: CellBit destination (flag 为 1 时翻转它的最低位,其余位不动)
|
||||
;
|
||||
; 注意 %1 不能用 set_0 清:它可能已经放了好几位,只允许 flb。
|
||||
%macro bit_raw_flip_if 1
|
||||
.anchor before_bit_raw_flip_if
|
||||
.tp before_bit_raw_flip_if
|
||||
biz
|
||||
.tp %1
|
||||
flb
|
||||
.tp before_bit_raw_flip_if
|
||||
flb
|
||||
rnz
|
||||
%endmacro
|
||||
|
||||
; Macro pack_byte
|
||||
; %1: Cell base of the eight bits
|
||||
; %2: Cell stride between them (1 means contiguous)
|
||||
; %3: Cell destination, must be all zero on entry; ends up holding the byte
|
||||
;
|
||||
; 第 i 轮:flag 为 1 就翻转 %3 的最低位,然后把 %3 右旋一格。放进去的位每轮都被
|
||||
; 往高位推一格,总共推 8 - i 格,所以第 i 位正好落到第 i 位上。
|
||||
%macro pack_byte 3
|
||||
.anchor before_pack_byte
|
||||
|
||||
.tp %1 + 0 * %2
|
||||
bit_raw_flip_if %3
|
||||
.tp %3
|
||||
ror
|
||||
|
||||
.tp %1 + 1 * %2
|
||||
bit_raw_flip_if %3
|
||||
.tp %3
|
||||
ror
|
||||
|
||||
.tp %1 + 2 * %2
|
||||
bit_raw_flip_if %3
|
||||
.tp %3
|
||||
ror
|
||||
|
||||
.tp %1 + 3 * %2
|
||||
bit_raw_flip_if %3
|
||||
.tp %3
|
||||
ror
|
||||
|
||||
.tp %1 + 4 * %2
|
||||
bit_raw_flip_if %3
|
||||
.tp %3
|
||||
ror
|
||||
|
||||
.tp %1 + 5 * %2
|
||||
bit_raw_flip_if %3
|
||||
.tp %3
|
||||
ror
|
||||
|
||||
.tp %1 + 6 * %2
|
||||
bit_raw_flip_if %3
|
||||
.tp %3
|
||||
ror
|
||||
|
||||
.tp %1 + 7 * %2
|
||||
bit_raw_flip_if %3
|
||||
.tp %3
|
||||
ror
|
||||
|
||||
.tp before_pack_byte
|
||||
%endmacro
|
||||
Reference in new issue
Block a user