The multiplier, 3729 characters, by repeated addition

clb_mul  MC MUL PL PH MCA S Ci T F NR    (PH:PL) = MC * MUL
    10 cells, 3729 characters, 64 sampled cases, 255*255 = 0xFE01 included

For scale: fibonacci is 8761 characters and does not multiply.  The tape length
does not depend on MUL at all, because the loop body appears once.

The algorithm is repeated addition, not shift-and-add:

    (PH:PL) = 0
    while MUL != 0:  (PH:PL) += MC ; MUL -= 1

which is the same lever as clb_add_ip: length is the metric, steps are not.  Worst
case is about a million steps against the five million limit and that is fine.

The 16 bit accumulate uses clb_add_nc for the low byte because it is the one that
reports a carry out; clb_add_ip does not, so it cannot do that step.  clb_add_nc
eats its operands, so each round copies the multiplicand first.  The carry then
goes into the high byte with clb_add_ip, which costs nothing extra since it is
0 or 1.

Worth recording what this did to my own plan: I had said the multiplier needed
"cross byte shifts" and "conditional accumulate" first, and sketched a
shift-and-add design around them.  Repeated addition needs neither -- the shift
disappears entirely.  The primitive that actually mattered was the one I called
missing two messages earlier: an add that keeps the addend alive, which is what
pushed out clb_add_ip and clb_nonzero.  Look at the objective function before
copying the shape of a hardware multiplier.

byte_op_test is seventeen operations, still 1.4 s, with test_mul sampled over
8 multipliers x 8 multiplicands.
This commit is contained in:
dsh committed 2026-11-07 09:56:03 +08:00
1 parent a6560b72cf
commit 185b4bf24a
5 files changed
+155 -1

No files matched your search

+25 -1
View File
@@ -59,7 +59,7 @@ r8forge asm/test.r8asm # 应 exit 0,1666 字符,7
r8forge_synth_test examples/full_add.r8asm # all eight combinations ok
r8forge verify_clb_bit_add_nc.r8asm # 4 cell 加法切片(进位进->出)
r8forge verify_clb_bit_inc_nc.r8asm # 3 cell 自增切片,进位同样进->出
r8forge_byte_op_test # 字节级库:16 个操作穷举/抽样,1.5 秒
r8forge_byte_op_test # 字节级库:17 个操作穷举/抽样,1.5 秒
r8forge_macro_docs_test # asm/ 里每个宏的注释格式
```
@@ -135,6 +135,7 @@ void rot8_rnz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 1) { ... } }
| **`clb_dec_ip`(a-1,原地,2 cell)** | `drv/dec_ip_byte.r8asm` | **106 字符** | **256/256** |
| **`clb_nonzero`(a!=0 的标志,原地)** | `drv/nonzero_byte.r8asm` | **209 字符** | **256/256** |
| **`clb_add_ip`(a+=b,原地,4 cell)** | `drv/add_ip_byte.r8asm` | **558 字符** | **2048 抽样全过**(O(b) 运行) |
| **`clb_mul`(mc×mul → 16 位积,10 cell)** | `drv/mul_byte.r8asm` | **3729 字符** | **64 抽样全过**(O(mul) 运行) |
| `asm/fibonacci.r8asm` | — | 8761 字符 | 跑出 `1 1 2 3 5 8 13 21 34 55`,和文件注释一致 |
`raw_*` 保留操作数(A、B 原样还回来),`clb_*_nc` 把 A、B 吃干净 —— 所以后者能省掉
@@ -225,6 +226,28 @@ B 被吃干净。对比:`raw_add` 1647(6 cell,保留操作数)、`clb_ad
转一格看下一位;八轮后 B 原样、F = (B != 0)。注意是 `set_1` 不是 `flb` —— 这里踩过:
`flb` 是翻转,偶数个 1 位会把标志翻回 0(3、5、255 都测错过)。
### 乘法器(`asm/mul.r8asm`)
`clb_mul MC MUL PL PH MCA S Ci T F NR`:`(PH:PL) = MC * MUL`,10 个 cell,**3729 字符**,
`r8forge_byte_op_test` 抽 64 组全对(含 `255*255 = 0xFE01`),最坏运行约 100 万步。
用的是**重复加法**,不是移位-加法:
(PH:PL) = 0
while MUL != 0: (PH:PL) += MC ; MUL -= 1
理由还是「只有纸带长度是目标」:循环体在纸带里只出现一次,所以长度和 MUL 无关,
`fibonacci`(8761)都比它长。
16 位累加靠 `clb_add_nc` 的进位输出:低字节相加拿到进位,再用 `clb_add_ip` 把这一位加进高字节。
`clb_add_nc` 会把操作数吃干净,所以每轮先 `copy` 一份被乘数(`clb_add_ip` 不输出进位,
所以低字节那一步只能用会吃操作数的那条)。
**设计上的教训**:我一开始以为乘法器缺"跨字节移位"和"条件累加"两件,实际上一件都不需要 ——
换成重复加法之后,移位整个消失了。缺的那件(保留加数的原地加)反而是关键,它逼出了
`clb_add_ip` 和 `clb_nonzero`。**先看目标函数(长度)再选算法,别先照着硬件乘法的样子拼。**
顺带纠正一个流传下来的说法:`raw_add` **不**吃掉操作数。实测 `5+3 → A=5 B=3 S=8`,
`255+255 → A=255 B=255 S=254 Co=1`。`asm/fibonacci.r8asm` 里"每圈都要重新复印"的注释
是过度保守(它确实复印了,但不复印也对)。
@@ -282,6 +305,7 @@ B 被吃干净。对比:`raw_add` 1647(6 cell,保留操作数)、`clb_ad
| `clb_dec_ip`(2 cell 原地自减) | **106 字符** = `neg` + `not`(`B-1 = ~(-B)`),256 全对 |
| `clb_nonzero`(非零标志) | **209 字符**,256 全对,B 保持原样 |
| `clb_add_ip`(4 cell 原地加) | **558 字符**,2048 抽样全对;`raw_add` 1647 / `clb_add_nc`+copy 1052 |
| `clb_mul`(10 cell 8 位乘法,16 位积) | **3729 字符**,64 抽样全对;比 `fibonacci`(8761)短,和 MUL 大小无关 |
| `bit_raw_add`(6 cell 全加器切片) | 保留 A、B;字节级 `raw_add` 1647 字符,65536 组合全对 |
| `raw_and` / `raw_or` / `raw_xor` / `raw_inc` | 639 / 575 / 463 / 1214 字符,各 65536(inc 512)全对 |
| `asm/fibonacci.r8asm` | 8761 字符,跑出 10 项斐波那契,和注释一致 |
+19
View File
@@ -244,6 +244,7 @@ byte level library ok
| `clb_dec_ip A T` | `A = A - 1`(**原地**;`B-1 = ~(-B)`) | 2 | **106** |
| `clb_nonzero B F T` | `F = (B != 0)`,B 不动 | 3 | **209** |
| `clb_add_ip A B T F` | `A += B`(**原地**,B 被吃掉) | 4 | **558** |
| `clb_mul MC MUL PL PH …` | `(PH:PL) = MC * MUL`,8 位乘法,16 位积 | 10 | **3729** |
**进位 cell 是真正的进位输入**:宏不动它的初值,由调用者给。所以可以串 —— 上一字节的
进位输出直接接到下一字节的进位输入,多字节加法/减法/取负都能这么拼。
@@ -281,6 +282,24 @@ while B != 0: A += 1 ; B -= 1
上限 500 万)。`clb_nonzero` 负责"B != 0"的判断(`biz` 只测最低位,得把八位都看一遍,
还不能把 B 吃掉)。
### 乘法器:8 位 × 8 位 → 16 位
`clb_mul` 用的是**重复加法**,不是移位-加法:
```
(PH:PL) = 0
while MUL != 0: (PH:PL) += MC ; MUL -= 1
```
**3729 字符、10 个 cell**,比 `fibonacci`(8761)短,而且长度和 MUL 多大无关 ——
循环体在纸带里只出现一次。16 位累加靠 `clb_add_nc` 的进位输出:低字节相加拿到进位,
再用 `clb_add_ip` 把它加进高字节;`clb_add_nc` 会吃操作数,所以每轮先 `copy` 一份被乘数。
`255 × 255 = 65025 = 0xFE01` ✓,`r8forge_byte_op_test` 抽 64 组全对。
**这个设计本身是一条经验**:一开始以为乘法器缺"跨字节移位"和"条件累加",换成重复加法之后
两样都不需要了。先看目标函数(纸带长度),再选算法,别照着硬件乘法的样子拼。
`asm/fibonacci.r8asm` 也是这条路上的:8761 字符,跑出 `1 1 2 3 5 8 13 21 34 55`。
### 一个坑:`biz`/`rnz` 看的是最低位
+56
View File
@@ -0,0 +1,56 @@
%include "asm/add_ip.r8asm"
%include "asm/raw_add_nc.r8asm"
; 8 位乘法:MC * MUL -> (PH:PL),16 位积。
;
; 算法是**重复加法**,不是移位-加法:
;
; 积 = 0
; while MUL != 0: (PH:PL) += MC ; MUL -= 1
;
; 为什么敢这么写 —— 和 clb_add_ip 同一个理由:**这里只有纸带长度是目标,运行步数不是**。
; 循环体在纸带里只出现一次,所以长度不随 MUL 变化;代价是运行 O(MUL)(255 时约 100 万步,
; 上限 500 万)。
;
; 16 位累加靠 clb_add_nc 的进位输出:低字节相加拿到进位,再用 clb_add_ip 把这一位加进高字节。
; clb_add_nc 会把两个操作数吃掉,所以每轮先把被乘数复印一份(clb_add_ip 是原地加法,但它
; 不输出进位,所以这一步只能用会吃操作数的那条)。
; Macro clb_mul
; %1: Cell MC
; %2: Cell MUL
; %3: Cell PL
; %4: Cell PH
; %5: Cell MCA
; %6: Cell S
; %7: Cell Ci
; %8: Cell T
; %9: Cell F
; %10: Cell NR
;
; (PH:PL) = MC * MUL。MC 保持原样,MUL 结束为 0。
%macro clb_mul 10
.anchor before_clb_mul
erase %3
erase %4
clb_nonzero %2 %10 %8
.tp %10
biz
.tp %1
copy %5 %8
.tp %7
set_0
clb_add_nc %3 %5 %6 %7
.tp %6
copy %3 %8
clb_add_ip %4 %7 %8 %9
clb_dec_ip %2 %8
clb_nonzero %2 %10 %8
.tp %10
rnz
.tp before_clb_mul
%endmacro
+23
View File
@@ -0,0 +1,23 @@
%include "asm/mul.r8asm"
; clb_mul 的驱动:MC * MUL -> (PH:PL)。
;
; r8forge_derive drv/mul_byte.r8asm -2 0=6 1=7 # 42
;
; out 依次是 PL、PH(低位在前)。MC 应还是 6,MUL 应被吃成 0。
.var MC 0
.var MUL 1
.var PL 2
.var PH 3
.var MCA 4
.var S 5
.var Ci 6
.var T 7
.var F 8
.var NR 9
clb_mul MC MUL PL PH MCA S Ci T F NR
output PL
output PH
output MC
+32
View File
@@ -457,6 +457,37 @@ int test_add_ip(const std::string &file) {
return bad;
}
// clb_mul: (PH:PL) = MC * MUL, MC preserved. Repeated addition, so whatever
// MUL says is how many times the loop runs -- sampled, not exhaustive.
int test_mul(const std::string &file) {
const std::string tape = assemble(file);
std::cout << "clb_mul: " << tape.size() << " characters (sampled)\n";
const int vals[] = {0, 1, 2, 3, 7, 13, 100, 255};
int bad = 0, n = 0;
for (int mc : vals)
for (int mu : vals) {
machine m;
m.cells[0] = uint8_t(mc); // MC
m.cells[1] = uint8_t(mu); // MUL
m.run(tape, 5'000'000);
n++;
const int p = mc * mu;
const bool ok = m.finished && m.out.size() == 3 &&
m.out[0] == (p & 0xff) && m.out[1] == ((p >> 8) & 0xff) &&
m.out[2] == mc;
if (!ok) {
bad++;
if (bad <= 3)
report("clb_mul", mc, mu, m,
std::to_string(p & 0xff) + " " + std::to_string((p >> 8) & 0xff) +
" " + std::to_string(mc));
}
}
std::cout << " " << (bad ? "FAILED" : "all " + std::to_string(n) + " sampled combinations ok")
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
return bad;
}
} // namespace
int main(int argc, char **argv) {
@@ -477,6 +508,7 @@ int main(int argc, char **argv) {
test_dec_ip(drv + "dec_ip_byte.r8asm") +
test_nonzero(drv + "nonzero_byte.r8asm") +
test_add_ip(drv + "add_ip_byte.r8asm") +
test_mul(drv + "mul_byte.r8asm") +
test_sub_nc(drv + "raw_sub_nc_byte.r8asm");
std::cout << (bad ? "FAILED\n" : "byte level library ok\n");
return bad ? 1 : 0;