vms: raise the step cap from 5M to 20M

5 million steps was picked when the worst macro (clb_add_ip with B=2047) sat just
under a million, so it caught hangs quickly.  The interactive programs changed the
arithmetic: one printed line of the desk calculator costs about 225k steps (40k
for clb_undigits, up to 32k for the operation, ~141k for clb_digits), which is only
about twenty lines before the cap, and examples_test applies the same cap to every
example.

At roughly 2.3e8 steps per second a 20M cap costs 0.09 s when a program really does
run away, and byte_op_test (the only place that runs 465k programs) is unchanged --
so the safety net stays cheap while a session gets four times the room.

docs/calc.md, docs/divmod.md, docs/byte-ops.md and SPEC's "500 ?" mentions now say
2000 ?.
This commit is contained in:
dsh committed 2026-11-11 11:04:14 +08:00
1 parent b83f3b1b06
commit b2ba20b885
8 files changed
+16 -16

No files matched your search

+2 -2
View File
@@ -486,14 +486,14 @@ A 的 12 个取值全对(`r8forge_byte_op_test` 的 `test_ones`)。
if R >= D: R -= D ; 商位 = 1 else: 商位 = 0
轮数 = 位宽,**跟 N、D 的值无关**。旧版是重复减法 O(N/D),`2047/1` 要 2047 轮、
直接撞 500 万步上限(实测 HANG)。实测(同一条 `drv/divmod.r8asm`,11 位):
直接撞 2000 万步上限(实测 HANG)。实测(同一条 `drv/divmod.r8asm`,11 位):
| 算式 | 重复减法(旧) | 移位-恢复(现在) |
|---|---|---|
| `2047/10` | 495,986 | **28,593** |
| `2047/2` | 2,461,650 | **31,997** |
| `999/7` | 340,086 | **28,661** |
| `2047/1` | **HANG**(>500 万) | **32,383** |
| `2047/1` | **HANG**(>2000 万) | **32,383** |
| `7/10` | 3,014 | 25,315 |
| `100/7` | 34,940 | 27,460 |
+3 -3
View File
@@ -84,7 +84,7 @@ raw_add A B Ci S Co tmp ; S = A + B,Co = 进位,A、B 保留
while B != 0: A += 1 ; B -= 1
循环体在纸带里只出现一次,长度与 B 的大小无关;代价是运行 O(B)(B=255 时约 30 万步,
上限 500 万)。`clb_nonzero B F T` 负责"B != 0"的判断(`biz` 只测最低位,得把 W 位都看
上限 2000 万)。`clb_nonzero B F T` 负责"B != 0"的判断(`biz` 只测最低位,得把 W 位都看
一遍、还不能把 B 吃掉):每位搬进工作位 T、若 T 是 1 就把 F **置一**并还原 B 的那一位。
**注意是 `set_1` 不是 `flb`** —— 这里踩过:`flb` 是翻转,偶数个 1 位会把标志翻回 0
@@ -103,11 +103,11 @@ raw_add A B Ci S Co tmp ; S = A + B,Co = 进位,A、B 保留
**3977 字符、10 个 cell**,轮数 = 位宽、跟两个操作数的值都无关(11 位下封顶 26123 步)。
旧的重复加法轮数 = `MUL` 的值,代价随乘数线性长大:`255 × 255` 是 73 万步,
`2047 × 2047` 直接撞 500 万步上限(`HANG after 5000000 steps`)。
`2047 × 2047` 直接撞 2000 万步上限(`HANG after 5000000 steps`)。
**设计上的教训**:上一轮把移位-加法否掉了,理由是「重复加法更短」。实测反了 ——
移位-加法不但短 1000 字符(5007 → 3977),步数还少一到两个数量级,只有小乘数(0/1)
比旧版慢一点点。先看目标函数再选算法没错,但目标函数里得把「运行步数有 500 万上限」
比旧版慢一点点。先看目标函数再选算法没错,但目标函数里得把「运行步数有 2000 万上限」
这一条写进去。
拼法上踩过两个坑(细节在 `asm/mul.r8asm` 注释里):跨格移位要按"低位那对先搬",
+1 -1
View File
@@ -77,7 +77,7 @@ t = C - 0x2A ; 命令: 0..5 数字: 6..15
| `/`(`clb_divmod`) | ~28,600 |
| 打印(`clb_digits`) | ≤ 164,867 |
一行 `12 34 + .` ≈ 36k×2 + 2.5k + 165k ≈ **240k 步**。`examples_test` 的上限是 500 万步,
一行 `12 34 + .` ≈ 36k×2 + 2.5k + 165k ≈ **240k 步**。`examples_test` 的上限是 2000 万步,
所以会话控制在 10 行左右(或把打印换成"里程表"版本,见下)。
## 6. 已知的坑(都在库里踩过)
+2 -2
View File
@@ -5,7 +5,7 @@
## 1. 为什么换、每轮的状态
旧版 O(N/D):`2047/1` 要 2047 轮、每轮约 2.4k 步,直接撞 500 万步上限(实测 HANG)。
旧版 O(N/D):`2047/1` 要 2047 轮、每轮约 2.4k 步,直接撞 2000 万步上限(实测 HANG)。
新版固定 **W 轮**(11 轮),跟 N、D 的值无关。
N 被除数(W 位)。每轮 `rol N`,于是 N 的 LSB 就是**最高位起**的下一位
@@ -77,7 +77,7 @@ buf、NR。固定代价里最大的是 `raw_sub` 的 2793 字符和提交时那
| `2047/10` | 495,986 | **28,593** |
| `2047/2` | 2,461,650 | **31,997** |
| `999/7` | 340,086 | **28,661** |
| `2047/1` | **HANG**(>500 万) | **32,383** |
| `2047/1` | **HANG**(>2000 万) | **32,383** |
| `7/10` | 3,014 | 25,315 |
| `100/7` | 34,940 | 27,460 |
+1 -1
View File
@@ -30,7 +30,7 @@ struct machine {
return uint32_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
}
void run(const std::string &tape, size_t limit = 5'000'000) {
void run(const std::string &tape, size_t limit = 20'000'000) {
for (size_t pc = 0; pc < tape.size() && limit; pc++, limit--) {
switch (tape[pc]) {
case '>':
+5 -5
View File
@@ -456,7 +456,7 @@ int test_ones(const std::string &file) {
for (int a = 0; a <= kCellSize; a++) {
machine m;
m.cells[0] = uint32_t(a); // A, the count
m.run(tape, 5'000'000);
m.run(tape, 20'000'000);
n++;
const int want = (1 << a) - 1;
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == 0 &&
@@ -489,7 +489,7 @@ int test_divmod(const std::string &file) {
machine m;
m.cells[0] = uint32_t(a); // N
m.cells[1] = uint32_t(d); // D
m.run(tape, 5'000'000);
m.run(tape, 20'000'000);
n++;
if (m.steps > worst) worst = m.steps;
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == a / d &&
@@ -518,7 +518,7 @@ int test_digits(const std::string &file) {
for (int v = 0; v < kCellMod; v++) {
machine m;
m.cells[0] = uint32_t(v);
m.run(tape, 5'000'000);
m.run(tape, 20'000'000);
n++;
if (m.steps > worst) worst = m.steps;
std::string want = std::to_string(v);
@@ -589,7 +589,7 @@ int test_add_ip(const std::string &file) {
machine m;
m.cells[0] = uint32_t(a); // A
m.cells[1] = uint32_t(b); // B
m.run(tape, 5'000'000);
m.run(tape, 20'000'000);
n++;
const int want = (a + b) & kDefaultCellMask;
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == want &&
@@ -619,7 +619,7 @@ int test_mul(const std::string &file) {
machine m;
m.cells[0] = uint32_t(mc); // MC
m.cells[1] = uint32_t(mu); // MUL
m.run(tape, 5'000'000);
m.run(tape, 20'000'000);
n++;
const int p = mc * mu;
const bool ok = m.finished && m.out.size() == 3 &&
+1 -1
View File
@@ -39,7 +39,7 @@ struct machine {
return uint32_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
}
void run(const std::string &tape, size_t limit = 5'000'000) {
void run(const std::string &tape, size_t limit = 20'000'000) {
for (size_t pc = 0; pc < tape.size(); pc++) {
if (++steps > limit) {
finished = false;
+1 -1
View File
@@ -61,7 +61,7 @@ int main(int argc, char **argv) {
std::vector<uint32_t> out;
size_t dp = 0;
const size_t limit = 5'000'000; // same as r8forge_derive
const size_t limit = 20'000'000; // same as r8forge_derive
size_t pc = 0;
size_t steps = 0;
for (; pc < tape.size(); pc++) {