clb_ones: unary expansion, a count becomes that many ones
New macro in asm/ones.r8asm: clb_ones A B T F leaves B = 2^A - 1, i.e. A
consecutive ones starting at the low bit, and eats A (it is spent as the
counter). The contract only holds for A <= _CELL_SIZE and is not checked, as
asked: 0 -> 0, 1 -> 1, 4 -> 0x0F, 7 -> 0x7F, 11 -> 0x7FF (all ones).
The loop is "shift and stuff a one in":
B = 0
while A != 0: B = 2B + 1 ; A -= 1
In this machine `2B + 1` is exactly `rol` + `set_1`: the left rotation brings
B's top bit round to the low bit and set_1 overwrites it. A is spent as the
counter instead of being copied (a copy is 495 characters), the loop body
appears once so the length does not depend on A, and the run time is O(A) with
A <= cell size.
The other sizing option was measured and rejected: start from 1, rotate A
times, then clb_dec_ip once. That drops set_1 from the body but pays 132
characters for the decrement and 44 for the erase, so it comes out 128 longer.
* tests/byte_op_test.cc: test_ones walks every A the contract covers
(0..kCellSize), which is the whole domain once the range is the caller's
job; 742 characters with the driver, 828 with a minimal one.
* drv/ones.r8asm: the driver the test uses.
* SPEC.md 7.5: the contract, the algorithm, and the two identities worth
remembering -- clb_inc_ip turns n ones into 2^n, and building it the other
way round costs more than it saves. Macro counts bumped to 42.
* AGENTS.md: the tables, plus the naming row (destructive computation whose
result lives in another cell, so no _ip suffix).
This commit is contained in:
1 parent
54ddf9b0e7
commit
bfad4b5d34
6 files changed
+132
-4
No files matched your search
@@ -79,7 +79,7 @@ r8forge asm/test.r8asm # 应 exit 0,28729 字符(
|
||||
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 # 字节级库:17 个操作穷举/抽样,1.5 秒
|
||||
r8forge_byte_op_test # 字节级库:18 个操作穷举/抽样,1.5 秒
|
||||
r8forge_macro_docs_test # asm/ 里每个宏的注释格式
|
||||
```
|
||||
|
||||
@@ -210,6 +210,7 @@ inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellSize) - 1u;
|
||||
| **`clb_nonzero`(a!=0 的标志,原地)** | `drv/nonzero_byte.r8asm` | **284 字符** | **256/256** |
|
||||
| **`clb_add_ip`(a+=b,原地,4 cell)** | `drv/add_ip_byte.r8asm` | **726 字符** | **2048 抽样全过**(O(b) 运行) |
|
||||
| **`clb_mul`(mc×mul → 两 cell 积,10 cell)** | `drv/mul_byte.r8asm` | **3977 字符** | **65536/65536**(O(位宽) 运行) |
|
||||
| **`clb_ones`(数量 → 从末位起 N 个 1,4 cell)** | `drv/ones.r8asm` | **742 字符**(带 output)/ **828**(最小驱动) | **12 个计数全对**(N=0..11) |
|
||||
| `asm/fibonacci.r8asm` | — | 11959 字符 | 跑出 13 项 `1 1 2 3 5 8 13 21 34 55 89 144 233` |
|
||||
|
||||
`raw_*` 保留操作数(A、B 原样还回来),`clb_*_nc` 把 A、B 吃干净 —— 所以后者能省掉
|
||||
@@ -414,6 +415,7 @@ W 轮之后 `(PH:PL)` 就是积;`PL`(原来的 Q)被移空,所以 **MUL
|
||||
| `clb_nonzero`(非零标志) | **284 字符**,256 全对,B 保持原样 |
|
||||
| `clb_add_ip`(4 cell 原地加) | **726 字符**,2048 抽样全对;`raw_add` 2199 |
|
||||
| `clb_mul`(10 cell 乘法,积占两个 cell) | **3977 字符**,65536 组穷举全对;轮数只跟位宽有关(最坏 26123 步),跟 MUL 的值无关 |
|
||||
| `clb_ones`(4 cell,数量 → 从末位起 N 个 1) | **828 字符**(最小驱动),N=0..11 全对;`rol` + `set_1` 就是 `B = 2B + 1`,A 当计数器吃干净 |
|
||||
| `bit_raw_add`(6 cell 全加器切片) | 保留 A、B;字节级 `raw_add` 2199 字符,65536 组合全对 |
|
||||
| `raw_and` / `raw_or` / `raw_xor` / `raw_inc` | 870 / 782 / 628 / 1619 字符,各 65536(inc 512)全对 |
|
||||
| `asm/fibonacci.r8asm` | 11959 字符,跑出 13 项(`… 89 144 233`),比 8 位时代多 3 项 |
|
||||
@@ -473,6 +475,7 @@ SPEC 第 136-150 行的标准宏前缀:
|
||||
| `clb_neg_nc`(3 cell)/ `clb_sub_nc`(4 cell) | 补码取负/减法,同样吃 A(和 B) |
|
||||
| `clb_not_ip` / `clb_inc_ip` / `clb_neg_ip` / `clb_dec_ip` / `clb_add_ip` | 同上再加 `_ip`(in place):结果回到原来的 cell,草稿 cell 由调用者传 |
|
||||
| `clb_nonzero` | 破坏性的纯计算(把 B 抄成"非零"标志),不带 `_ip` 后缀 |
|
||||
| `clb_ones` | 破坏性的纯计算(把"数量"展开成从末位起的一串 1,输入被吃干净);结果写在另一个 cell 里,所以也不带 `_ip` |
|
||||
| `clb_bit_mov` | 位级搬运,并且把源吃掉 |
|
||||
| `bit_copy` / `copy` | 位级/字节级拷贝,缓冲由调用者传(历史命名,少了 `raw`) |
|
||||
| `set_0` / `set_1` / `erase` / `rol` / `step` / `back` / `output` | 基础操作,不带前缀 |
|
||||
|
||||
@@ -467,7 +467,7 @@ cost(layer) = 2 # biz + rnz
|
||||
那些都是失败换来的结论,别重试。
|
||||
|
||||
要查"某个宏到底怎么调、吃什么、留什么、占几个 cell、纸带多长、谁验证的",看
|
||||
[`SPEC.md`](SPEC.md):`%synth` 的完整语法 + `asm/` 里 41 个宏的契约表。
|
||||
[`SPEC.md`](SPEC.md):`%synth` 的完整语法 + `asm/` 里 42 个宏的契约表。
|
||||
|
||||
## 构建
|
||||
|
||||
|
||||
@@ -321,6 +321,33 @@ R8Forge 唯一的语言扩展:在汇编期现场搜出一段最短的块序列
|
||||
**轮转驱动不吃亏**:`clb_*_nc` 和 `raw_*` 的区别只在"要不要还操作数",两者都是
|
||||
"读走 LSB → 丢掉 → `ror`" 的轮转驱动,吃掉操作数完全没问题。
|
||||
|
||||
### 7.5 `clb_ones`:把数量展开成一串 1
|
||||
|
||||
`clb_ones A B T F`:**`B = 2^A - 1`,也就是从末位起 A 个连续的 1**;A 当计数器被吃干净
|
||||
(结束为 0),T、F 结束也都是 0。**前提 `A <= _CELL_SIZE`,不做检查**(A 更大时 2^A−1 会绕回)。
|
||||
|
||||
| 输入 A | 0 | 1 | 4 | 7 | 11 |
|
||||
|---|---|---|---|---|---|
|
||||
| 输出 B | 0 | 1 | `0x0F` | `0x7F` | `0x7FF`(全 1) |
|
||||
|
||||
4 个 cell、纸带 **828 字符**(`drv/ones.r8asm` 是 742,因为它的布局更近),
|
||||
A 的 12 个取值全对(`r8forge_byte_op_test` 的 `test_ones`)。
|
||||
|
||||
算法就是"边转边在低位补 1":
|
||||
|
||||
B = 0
|
||||
while A != 0: B = 2B + 1 ; A -= 1
|
||||
|
||||
`2B+1` 在这台机器上等于 **`rol` + `set_1`**:左旋把 B 的最高位带回末位,紧接着的 `set_1`
|
||||
正好把它盖成 1,于是等价于"左移补 1"。A 直接当计数器吃掉(省一次 `copy`,那是 495 字符),
|
||||
循环体只出现一次,所以长度和 A 无关;代价是运行 O(A),而 A ≤ `_CELL_SIZE`。
|
||||
|
||||
两个可以拿来写别的东西的恒等式:
|
||||
|
||||
* `clb_inc_ip` 作用在"n 个 1"上得到 `2^n`(末位起第 n+1 位是 1);
|
||||
* 反过来先摆一个 `2^n` 再 `clb_dec_ip`,也得到 n 个 1(这条比上面的循环短 128 字符的
|
||||
`clb_dec_ip` 反而更长,因为循环体里要一直 `set_1`,见 `asm/ones.r8asm` 的注释)。
|
||||
|
||||
---
|
||||
|
||||
## 8. 原地 `_ip` 家族(结果回到原来的 cell)
|
||||
@@ -402,12 +429,12 @@ clb_add_ip PH Ci S F ; 把进位加进高字节(Ci 被吃)
|
||||
|
||||
| 测试 | 覆盖 | 断言口径 |
|
||||
|---|---|---|
|
||||
| `r8forge_byte_op_test` | 17 个测试项 = 16 个宏(`clb_neg_nc` 的 Ci=1 / Ci=0 各算一项),见 §7.1、§7.2、§8、§9 | **按返回值**:穷举 0..255(`clb_add_ip` 2048 抽样、`clb_mul` 65536 全穷举),任一不符即退出码非 0 |
|
||||
| `r8forge_byte_op_test` | 18 个测试项 = 17 个宏(`clb_neg_nc` 的 Ci=1 / Ci=0 各算一项),见 §7.1、§7.2、§7.5、§8、§9 | **按返回值**:穷举 0..255(`clb_add_ip` 2048 抽样、`clb_mul` 65536 全穷举、`clb_ones` 12 个计数全穷举),任一不符即退出码非 0 |
|
||||
| `r8forge_synth_test examples/full_add.r8asm` | `%synth` 端到端:汇编 + 跑 8 种输入 | `all eight combinations ok` |
|
||||
| `r8forge verify_clb_bit_add_nc.r8asm` | 4 cell 加法切片(进位进→出) | 单点:`0+0+Ci` |
|
||||
| `r8forge verify_clb_bit_inc_nc.r8asm` | 3 cell 自增切片 | 单点 |
|
||||
| `r8forge verify_set_0.r8asm` | `set_0` 只清 LSB | `-1 0=255 → 254`、`0=128 → 128` |
|
||||
| `r8forge_macro_docs_test` | `asm/` 里 41 个宏的注释格式 | 全绿或报错行号 |
|
||||
| `r8forge_macro_docs_test` | `asm/` 里 42 个宏的注释格式 | 全绿或报错行号 |
|
||||
| `r8forge_tape_run <tape.rot8>` | 第二个解释器,直接跑纸带 | 与 `r8forge_derive` 对答案 |
|
||||
| `r8forge_derive <src> -1/-2` | 拿汇编源码跑,打印 `out` / 非零 cell | 人工/脚本比对 |
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
%include "asm/add_ip.r8asm"
|
||||
|
||||
; 把"数量"展开成"一串 1":B = 2^A - 1,也就是**从末位起 A 个连续的 1**。
|
||||
;
|
||||
; 0 -> 0
|
||||
; 1 -> 1
|
||||
; 4 -> 1111 (0x0F)
|
||||
; 7 -> 1111111 (0x7F)
|
||||
;
|
||||
; 前提:A <= _CELL_SIZE,调用者保证,这里不检查(A 更大时 2^A-1 会绕回)。
|
||||
;
|
||||
; 算法是"边转边补 1":
|
||||
;
|
||||
; B = 0
|
||||
; while A != 0: B = 2B + 1 ; A -= 1
|
||||
;
|
||||
; `2B + 1` 在这台机器上就是 **`rol` + `set_1`**:左旋一格会把 B 的最高位带回到
|
||||
; 末位,紧随其后的 `set_1` 正好把它盖成 1,于是等价于"左移、低位补 1"。
|
||||
; A 直接当计数器吃掉(省一次 copy:copy 要 495 字符),循环体在纸带里只出现一次,
|
||||
; 所以长度和 A 的大小无关;代价是运行 O(A),A <= _CELL_SIZE 所以最多十来轮。
|
||||
;
|
||||
; 为什么不用"B = 1,转 A 圈,再减一"那种写法:那样循环体里只剩 `rol`(省 4 字符),
|
||||
; 但末尾要多一次 `clb_dec_ip`(132 字符),还多一次 `set_1` 打底 —— 反而更长。
|
||||
;
|
||||
; 另一个能用的恒等式(写在这里省得下次重推):`clb_inc_ip B` 会把 n 个 1 变成
|
||||
; 2^n(末位起第 n+1 位是 1),所以"n 个 1 -> 2^n -> 单 1"和它的逆都有现成的路。
|
||||
|
||||
; Macro clb_ones
|
||||
; %1: Cell A
|
||||
; %2: Cell B
|
||||
; %3: Cell T
|
||||
; %4: Cell F
|
||||
;
|
||||
; B = 2^A - 1(从末位起 A 个 1);A 被吃干净(结束为 0),T 和 F 结束也都是 0。
|
||||
; 前提:A <= _CELL_SIZE,不做检查。
|
||||
%macro clb_ones 4
|
||||
.anchor before_clb_ones
|
||||
|
||||
; B 得先清干净:下面每轮只负责在低位补 1,高位留着原样的垃圾会脏在结果里
|
||||
erase %2
|
||||
|
||||
clb_nonzero %1 %4 %3
|
||||
|
||||
.tp %4
|
||||
biz
|
||||
.tp %2
|
||||
rol
|
||||
set_1
|
||||
clb_dec_ip %1 %3
|
||||
clb_nonzero %1 %4 %3
|
||||
.tp %4
|
||||
rnz
|
||||
|
||||
.tp before_clb_ones
|
||||
%endmacro
|
||||
@@ -0,0 +1,17 @@
|
||||
%include "asm/ones.r8asm"
|
||||
|
||||
; clb_ones 的驱动:0 号 cell 放数量,B 是展开结果。
|
||||
;
|
||||
; r8forge_derive drv/ones.r8asm -2 0=7 # B 应该是 0x7F = 127
|
||||
; r8forge_derive drv/ones.r8asm -2 0=0 # B 应该是 0
|
||||
;
|
||||
; out 依次是 A(被吃成 0)和 B(从末位起 A 个 1)。
|
||||
.var A 0
|
||||
.var B 2
|
||||
.var T 1
|
||||
.var F 3
|
||||
|
||||
clb_ones A B T F
|
||||
|
||||
output A
|
||||
output B
|
||||
@@ -439,6 +439,31 @@ int test_nonzero(const std::string &file) {
|
||||
return bad;
|
||||
}
|
||||
|
||||
// clb_ones: B = 2^A - 1, i.e. A consecutive ones from the low bit, A is eaten.
|
||||
// Only A <= cell width is in the contract, so that is the whole domain.
|
||||
int test_ones(const std::string &file) {
|
||||
const std::string tape = assemble(file);
|
||||
std::cout << "clb_ones: " << tape.size() << " characters\n";
|
||||
int bad = 0, n = 0;
|
||||
for (int a = 0; a <= kCellSize; a++) {
|
||||
machine m;
|
||||
m.cells[0] = uint16_t(a); // A, the count
|
||||
m.run(tape, 5'000'000);
|
||||
n++;
|
||||
const int want = (1 << a) - 1;
|
||||
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == 0 &&
|
||||
m.out[1] == want;
|
||||
if (!ok) {
|
||||
bad++;
|
||||
if (bad <= 3)
|
||||
report("clb_ones", a, 0, m, std::to_string(want));
|
||||
}
|
||||
}
|
||||
std::cout << " " << (bad ? "FAILED" : "all " + std::to_string(n) + " counts ok")
|
||||
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
||||
return bad;
|
||||
}
|
||||
|
||||
// clb_add_ip: A = A + B in place, B is eaten. The loop runs B times, so this
|
||||
// one is sampled rather than exhaustive -- 256 addends x 8 accumulators.
|
||||
int test_add_ip(const std::string &file) {
|
||||
@@ -519,6 +544,7 @@ int main(int argc, char **argv) {
|
||||
test_neg_ip(drv + "neg_ip_byte.r8asm") +
|
||||
test_dec_ip(drv + "dec_ip_byte.r8asm") +
|
||||
test_nonzero(drv + "nonzero_byte.r8asm") +
|
||||
test_ones(drv + "ones.r8asm") +
|
||||
test_add_ip(drv + "add_ip_byte.r8asm") +
|
||||
test_mul(drv + "mul_byte.r8asm") +
|
||||
test_sub_nc(drv + "raw_sub_nc_byte.r8asm");
|
||||
|
||||
Reference in new issue
Block a user