build: the 32 bit toolchain builds, and 8/16 bit byte tests really pass

Two separate things hid behind "32 bit works":

* tests/byte_op_test.cc could not compile at 32 bits at all (`1 << 32` in a
  constexpr plus an int typed mask), so the whole 32 bit build stopped there --
  only `r8forge` and the core library came out.  kCellMod is now clamped at
  31+ bits and main() prints "skipped" above 16 bits (every sweep there is 2^W
  rows) and exits green.  All ten targets build at 32 bits with MSVC, and the
  same tree builds clean on a Gentoo box with GCC 16 -- the missing <cstring>
  in asm_test.cc that stopped GCC first is in the previous commit.
* the digit drivers hardcoded a 4 cell window, so byte_op_test's digits /
  digits_signed / undigits failed at every other width (16 bits wants 5 digits,
  8 bits 3).  drv/digits.r8asm, drv/digits_signed.r8asm and
  drv/undigits_byte.r8asm now derive D = _CELL_SIZE * 30103 / 100000 + 1 and
  space their cells by it, and the C++ side uses the same kDigits instead of a
  literal 4 (including the T/F cells clb_digits_signed leaves behind).

With that, byte_op_test passes at 8, 11 and 16 bits (the 16 bit run takes about
450 s) and above 16 it skips.  Two more width bugs fell out on the way: the
divmod divisor sweep used 1000 and 2047 unconditionally (they wrap at 8 bits),
and the undigits garbage guard OR'ed in bits above the cell, which at 8 bits
turned '0'+digit into a value over 255.  test_divmod_signed also divided by the
recovered divisor without re-checking it, and at 8 bits the boundary value 1024
wraps to 0 -- that was a hard crash (divide by zero), not a failure.

CMakeLists.txt now refuses -DR8FORGE_CELL_SIZE outside 1..32 at configure time;
the range used to be a comment, so an out of range width was discovered as a
compiler error in whichever file shifted by it first.  geometry.hh and SPEC
section 13 record that 32 is the ceiling because every VM stores a cell in
uint32_t -- going further means widening all five VMs, the assembler's immediate
type and ROT-8-viz's rot8_data.

Also recorded there: the %synth cache is keyed by the truth table, not the
width, so a 32 bit build hits the committed entries (0.4 s for the flagship
tape of 227075 characters).  A full cold rebuild takes ~500 s on a 44 thread
machine, 479 s of which is the single 6 parameter full adder table.
This commit is contained in:
dsh committed 2026-11-19 13:41:59 +08:00
1 parent d2da61a255
commit d0fd127048
8 files changed
+124 -52

No files matched your search

+8 -1
View File
@@ -40,7 +40,7 @@ cmake --build build --config Release
# 当前基线:都通过,别弄坏(`ctest --test-dir build -C Release -j 4` 一次跑完 8 条,约 12 秒。
# VS 是多配置生成器,**`-C Release` 不能省**,否则 8 条全报 "Not Run")
r8forge asm/test.r8asm # 62351 字符(除两个有符号包装宏外每个宏一次 + 每个语言特性)
r8forge_byte_op_test # 23 项(divmod 16384 组、digits 2048、digits_signed 2048、divmod_signed 4202、undigits 4096)
r8forge_byte_op_test # 23 项(divmod 16384 组、digits 2048、digits_signed 2048、divmod_signed 4202、undigits 4096);8/11/16 位真跑,17 位以上打印 skipped
r8forge_examples_test # 19 个 example + asm/test.r8asm,核对头部 `; out`/`; in`
r8forge_macro_docs_test # 55 macros in 23 files
r8forge --opt asm/test.r8asm # 纸带窥孔优化 62351 -> 56338(>< 净位移、+ mod W、- mod 2)
@@ -49,8 +49,15 @@ r8forge_tape_run t.rot8 --profile # 剖面:指针移动占 66~73% 步
r8forge_synth_test examples/full_add.r8asm # all eight combinations ok
r8forge_derive verify_loop.r8asm -1 0=5 # out 5 0
powershell -File tests\widths.ps1 # 位宽矩阵:每个位宽一套工具链 + 断言(约 20 分钟)
powershell -File tests\widths.ps1 -Widths 32 # 32 位:整套 10 个 target 能建,byte_ops 只打印 skipped
```
**位宽上限是 32**(每份 VM 的 cell 都是 `uint32_t`;CMake 里 `-DR8FORGE_CELL_SIZE` 越界直接
`FATAL_ERROR`,所以别再指望 C++ 报错才发现)。想往上要按 SPEC §13 的清单同时改五份 VM、
`geometry.hh`、汇编器的立即数类型和 ROT-8-viz 的 `rot8_data`。`drv/` 里的驱动**必须**
用 `.var D _CELL_SIZE * 30103 / 100000 + 1` 算位数 —— 写死 4 格导致 digits 三项在 8/16 位
一直挂着,2026-11 才修。
`asm/test.r8asm` 的纸带**必须留在 65536 条指令以下**(ROT-8-viz 的 `INS_SIZE`;现在的
虚拟机缓冲是按纸带长度现算的,这条是历史口径 + 交叉验证,别指望它)所以里面只有"每个宏
一次 + 两轮 hash",现在离上限只剩 ~3.2k。**两个有符号包装宏不在里面**:它们各自要把
+9
View File
@@ -8,7 +8,16 @@ include_directories(include)
# The width of a cell. 11 is the project default; the tests and the VMs follow
# it, so `-DR8FORGE_CELL_SIZE=16` builds an entirely 16 bit toolchain.
# 32 is the ceiling: the VMs (and ROT-8-viz) store a cell in uint32_t, and the
# assembler's immediate type is narrower than that anyway. Refuse anything else
# here so a wrong -DR8FORGE_CELL_SIZE fails at configure time instead of as a
# C++ error in whichever file happens to shift by the width first.
set(R8FORGE_CELL_SIZE 11 CACHE STRING "bits per cell (1..32)")
if(R8FORGE_CELL_SIZE LESS 1 OR R8FORGE_CELL_SIZE GREATER 32)
message(FATAL_ERROR
"R8FORGE_CELL_SIZE=${R8FORGE_CELL_SIZE} is out of range: a cell is stored in "
"uint32_t in every VM, so the supported range is 1..32")
endif()
add_compile_definitions(R8FORGE_CELL_SIZE=${R8FORGE_CELL_SIZE})
find_package(Threads REQUIRED)
+19 -5
View File
@@ -37,7 +37,9 @@ inline constexpr size_t kDefaultMemoryCells = 259;
inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellSize) - 1u;
```
位宽可以用构建选项换:`cmake -S . -B build16 -DR8FORGE_CELL_SIZE=16` 会造出一整套 16 位的工具链(汇编器、库、五份 VM 都跟着走);实测 11 / 16 / 32 位下 `r8forge_synth_test` 都是 8/8 通过、`byte_op_test` 在 16 位下也全过(32 位的穷举是 2^64 组,跑不了)。\n\n汇编器把三个名字注入 `.r8asm` 程序的作用域,宏库靠它们与位宽解耦:
位宽可以用构建选项换:`cmake -S . -B build16 -DR8FORGE_CELL_SIZE=16` 会造出一整套 16 位的工具链(汇编器、库、五份 VM 都跟着走)。**上限是 32**(每份 VM 的 cell 都存在 `uint32_t` 里,CMake 里越界直接 `FATAL_ERROR`,见 §13)。实测:11 / 16 位下 `r8forge_synth_test` 8/8 通过;`byte_op_test` 在 8 / 11 / 16 位下全过(16 位那轮约 7.5 分钟,穷举 65536 个值 × 十余项),**17 位以上打印 `skipped` 并返回 0**(每轮 2^W 行,跑不动,但文件得在 1..32 位都编得过);32 位下整套工具链 10 个 target 全部可建、可跑。
汇编器把三个名字注入 `.r8asm` 程序的作用域,宏库靠它们与位宽解耦:
| 名字 | 值(11 位) | 用途 |
|---|---|---|
@@ -910,13 +912,25 @@ r8forge_tape_run t.rot8 # 打印 out 和步数
## 13. 维护
- **改位宽/纸带长度**:`include/r8forge/geometry.hh` 两个常量 + 四份虚拟机
(`tests/synth_test.cc`、`tests/byte_op_test.cc`、`src/derive.cc`、`tests/tape_run.cc`)
一起改;`asm/` 里的宏靠 `_CELL_SIZE` 自动跟随,不用动。
- **改位宽/纸带长度**:`include/r8forge/geometry.hh` 两个常量 + 五份虚拟机
(`tests/synth_test.cc`、`tests/byte_op_test.cc`、`tests/examples_test.cc`、`src/derive.cc`、
`tests/tape_run.cc`)一起改;`asm/` 里的宏靠 `_CELL_SIZE` 自动跟随,不用动。
- **位宽上限是 32**:每份 VM 的 cell 都存在 `uint32_t` 里,`geometry.hh` 的 mask 特判了 32,
CMake 里加守卫(`-DR8FORGE_CELL_SIZE` 越界直接 `FATAL_ERROR`)。想上 64 位得同时改五份 VM 的
cell、`geometry.hh`、汇编器的立即数类型(`r8asm.hh` 的 `data` 现在还是 `int16_t`)以及
`ROT-8-viz` 的 `rot8_data` / `ROT8_MAX_CELL_SIZE`,否则交叉验证没法做。
`drv/` 里的驱动必须用 `.var D _CELL_SIZE * 30103 / 100000 + 1` 算位数,
**不能把窗口写死 4 格** —— 写死过,`byte_op_test` 的 digits / digits_signed / undigits
三项因此在 16 位下一直挂着(16 位要 5 位数字),2026-11 才修。
- **`byte_op_test` 在 17 位以上打印 `skipped`**:它每一轮都是 2^W 行,跑不动;但文件本身要在
1..32 位都编得过(`1 << 32` 是未定义行为,所以 `kCellMod` 在 31/32 位被夹成 0)。
8 / 11 / 16 位下它是真跑的(16 位一轮约 7.5 分钟)。
- **加宏**:先按 §4.1 选前缀/后缀,再写 §4.2 的注释块,跑 `r8forge_macro_docs_test`;
能进 `byte_op_test` 就进(那才有按返回值的验证)。
- **改指令模型**(合成器的块集合、代价):必须 bump `kModelVersion`,否则
`.r8forge_cache/` 里的旧结果会被当成最优。
`.r8forge_cache/` 里的旧结果会被当成最优。**缓存按真值表存,不含位宽**,所以换位宽不用重算
(实测 32 位命中现成条目只要 0.4 s);只有 bump 版本号、换 `--synth-chars` 口径或删目录才要
重跑,全量一轮约 8 分钟(44 线程机器上 497 s,其中 479 s 是全加器那张 6 参数表)。
- **改本文的数字**:照 §1.3 的口径重新量,别把驱动长度和最小驱动长度混在一列。
---
+13 -9
View File
@@ -1,18 +1,22 @@
%include "asm/digits.r8asm"
; clb_digits 的驱动:0 号 cell 放值,1..4 是输出区域,5 起是草稿。
; clb_digits 的驱动:0 号 cell 放值,1 起是 D 位输出区域,再往后是草稿。
;
; r8forge_derive drv/digits.r8asm -1 0=2047 # 50 48 52 55 = "2047"
; r8forge_derive drv/digits.r8asm -1 0=7 # 48 48 48 55 = "0007"
; r8forge_derive drv/digits.r8asm -1 0=2047 # 11 位: 50 48 52 55 = "2047"
; r8forge_derive drv/digits.r8asm -1 0=7 # 11 位: 48 48 48 55 = "0007"
;
; out 依次是区域四格(ASCII 码,所以 48 是 '0')。
; D = _CELL_SIZE * 30103 / 100000 + 1 是十进制位数(11 位下 4、16 位下 5、8 位下 3),
; 所以地址跟着位宽走:REG 占 1..1+D-1,草稿从 1+D 开始。**别把 D 写死 4** ——
; 写死了 16 位下就会拿 5 位数字去撞草稿区(r8forge_byte_op_test 的 16 位那一轮
; 以前就是这么错的)。
; out 依次是区域 D 格(ASCII 码,所以 48 是 '0')。
.var D _CELL_SIZE * 30103 / 100000 + 1
.var N 0
.var REG 1
.var S 5
.var S 1 + D
clb_digits N REG S
output REG+0
output REG+1
output REG+2
output REG+3
%for k D
output REG + k
%endfor
+14 -13
View File
@@ -1,22 +1,23 @@
%include "asm/digits.r8asm"
; clb_digits_signed 的驱动:0 号 cell 放值,1..4 是数字区,5 起是 clb_digits 的
; 13 格草稿,18 是 T,19 是符号标志 F。
; clb_digits_signed 的驱动:0 号 cell 放值,1 起是 D 位数字区,之后是 clb_digits 的
; 13 格草稿、取负草稿 T、符号标志 F。
;
; r8forge_derive drv/digits_signed.r8asm -1 0=7 # 48 48 48 55 "0007"
; r8forge_derive drv/digits_signed.r8asm -1 0=2047 # 45 48 48 48 49 "-0001"
; r8forge_derive drv/digits_signed.r8asm -1 0=1024 # 45 49 48 50 52 "-1024"
; r8forge_derive drv/digits_signed.r8asm -1 0=7 # 11 位: 48 48 48 55 "0007"
; r8forge_derive drv/digits_signed.r8asm -1 0=2047 # 11 位: 45 48 48 48 49 "-0001"
; r8forge_derive drv/digits_signed.r8asm -1 0=1024 # 11 位: 45 49 48 50 52 "-1024"
;
; out 先可能是 45('-'),再是区域四格。全扫在 r8forge_byte_op_test 里。
; D = _CELL_SIZE * 30103 / 100000 + 1(11 位下 4、16 位下 5),地址跟着位宽走。
; out 先可能是 45('-'),再是区域 D 格。全扫在 r8forge_byte_op_test 里。
.var D _CELL_SIZE * 30103 / 100000 + 1
.var N 0
.var REG 1
.var S 5
.var T 18
.var F 19
.var S 1 + D
.var T S + 13
.var F T + 1
clb_digits_signed N REG S T F
output REG+0
output REG+1
output REG+2
output REG+3
%for k D
output REG + k
%endfor
+8 -6
View File
@@ -1,14 +1,16 @@
; clb_undigits 的驱动:0..3 是四个数字格(低 7 位 ASCII),4 是结果,5 起是草稿。
; clb_undigits 的驱动:0 起是 D 个数字格(低 7 位 ASCII),紧跟着是结果格,再往后是草稿。
;
; r8forge_derive drv/undigits_byte.r8asm -1 0=49 1=50 2=51 3=52 # out 1234
; r8forge_derive drv/undigits_byte.r8asm -1 0=48 1=48 2=48 3=55 # out 7
; r8forge_derive drv/undigits_byte.r8asm -1 0=49 1=50 2=51 3=52 # 11 位: out 1234
; r8forge_derive drv/undigits_byte.r8asm -1 0=48 1=48 2=48 3=55 # 11 位: out 7
;
; 位数和 clb_digits 一致(_CELL_SIZE*30103/100000+1,11 位下 4 位)。
; 位数和 clb_digits 一致(_CELL_SIZE*30103/100000+1,11 位下 4、16 位下 5),
; 所以 REG、N、S 的地址都跟着 D 走 —— 写死 4 格的话 16 位下数字会撞进结果格。
%include "asm/undigits.r8asm"
.var D _CELL_SIZE * 30103 / 100000 + 1
.var REG 0
.var N 4
.var S 5
.var N D
.var S D + 1
clb_undigits REG N S
+5 -1
View File
@@ -10,6 +10,10 @@
inline constexpr size_t kDefaultCellSize = R8FORGE_CELL_SIZE; // build option: -DR8FORGE_CELL_SIZE=<1..32>
inline constexpr size_t kDefaultMemoryCells = 259;
// The VMs store a cell in uint32_t, so 32 bits is the ceiling -- and `1u << 32`
// would be undefined, hence the special case instead of a plain shift.
// would be undefined, hence the special case instead of a plain shift. CMake
// refuses anything outside 1..32 at configure time, and the exhaustive byte
// level test prints "skipped" instead of running above 16 bits (its sweeps are
// 2^W rows). Going past 32 would mean widening the cell in all five VMs *and*
// in ROT-8-viz's rot8_data -- SPEC §13 has the checklist.
inline constexpr uint32_t kDefaultCellMask =
kDefaultCellSize >= 32 ? 0xFFFFFFFFu : (1u << kDefaultCellSize) - 1u;
+48 -17
View File
@@ -21,9 +21,18 @@ namespace {
// written in terms of it instead of in terms of a byte: kCellMod is one past
// the largest value a cell holds. The sweeps walk the first 256 values, which
// is a sample of the cell range rather than all of it.
//
// The exhaustive sweeps are 2^W rows, so they only run up to 16 bits; the file
// still has to COMPILE at every width the toolchain supports (1..32, see the
// guard in CMakeLists.txt), and `1 << 32` is undefined, so the constant is
// clamped at the top instead of being shifted. main() bails out above 16 bits.
constexpr int kCellSize = int(kDefaultCellSize);
constexpr int kCellMod = 1 << kCellSize;
constexpr int kCellMod = kCellSize >= 31 ? 0 : 1 << kCellSize;
constexpr int kCellMask = int(kDefaultCellMask);
// Digits in a decimal rendering of a cell: the formula clb_digits / clb_undigits
// use, and the one the drv/ drivers lay their cells out by (D = 4 at 11 bits,
// 5 at 16, 3 at 8). Anything that hardcoded 4 used to break the 16 bit run.
constexpr int kDigits = kCellSize * 30103 / 100000 + 1;
struct machine {
std::vector<uint32_t> cells = std::vector<uint32_t>(kDefaultMemoryCells, 0);
@@ -477,11 +486,16 @@ int test_ones(const std::string &file) {
// repeated subtraction this replaced ran N/D times and crossed the 5M step
// budget outright for D = 1, so it could only be sampled. Now every dividend
// can be swept for a spread of divisors, and the divisor set is a superset of
// the old sampled table's.
// the old sampled table's. Divisors that do not fit a narrow cell (1000 and
// 2047 at 8 bits) are dropped instead of wrapping into a different value.
int test_divmod(const std::string &file) {
const std::string tape = assemble(file);
std::cout << "clb_divmod: " << tape.size() << " characters\n";
const int divs[] = {1, 2, 3, 7, 10, 100, 1000, 2047};
const int all_divs[] = {1, 2, 3, 7, 10, 100, 1000, 2047};
std::vector<int> divs;
for (int d : all_divs)
if (d < kCellMod)
divs.push_back(d);
int bad = 0, n = 0;
size_t worst = 0;
for (int d : divs)
@@ -507,9 +521,9 @@ int test_divmod(const std::string &file) {
return bad;
}
// clb_digits: the value becomes four ASCII digits, most significant first. The
// divmod underneath is now O(_CELL_SIZE) instead of O(N/10), so the whole cell
// range fits in the budget: every value is swept.
// clb_digits: the value becomes kDigits ASCII digits, most significant first.
// The divmod underneath is now O(_CELL_SIZE) instead of O(N/10), so the whole
// cell range fits in the budget: every value is swept.
int test_digits(const std::string &file) {
const std::string tape = assemble(file);
std::cout << "clb_digits: " << tape.size() << " characters\n";
@@ -522,9 +536,9 @@ int test_digits(const std::string &file) {
n++;
if (m.steps > worst) worst = m.steps;
std::string want = std::to_string(v);
while (want.size() < 4) want.insert(want.begin(), '0');
bool ok = m.finished && m.out.size() == 4;
for (int i = 0; ok && i < 4; i++) {
while (int(want.size()) < kDigits) want.insert(want.begin(), '0');
bool ok = m.finished && m.out.size() == size_t(kDigits);
for (int i = 0; ok && i < kDigits; i++) {
ok = m.out[size_t(i)] == (uint32_t)(unsigned char)want[size_t(i)];
}
if (!ok) {
@@ -540,13 +554,18 @@ int test_digits(const std::string &file) {
// clb_digits_signed: the two's complement reading of the same values, with a
// leading '-' (45) in front of the digits when the value is negative. The
// interesting corner is 1024, which IS negative (-1024) but whose magnitude is
// out of the signed range -- it still has to print "-1024", so the wrapper must
// not compute |N| as "clear the sign bit".
// interesting corner is 2^(W-1), which IS negative (-HALF) but whose magnitude
// is out of the signed range -- it still has to print "-HALF", so the wrapper
// must not compute |N| as "clear the sign bit".
int test_digits_signed(const std::string &file) {
const std::string tape = assemble(file);
std::cout << "clb_digits_signed: " << tape.size() << " characters\n";
const int half = kCellMod / 2;
// The driver puts clb_digits' 13 cell scratch right after the digit window,
// then T, then F -- so these two move with the width (they were 18/19 when
// the window was hardcoded at 4 cells).
const size_t t_cell = size_t(1 + kDigits + 13);
const size_t f_cell = t_cell + 1;
int bad = 0, n = 0;
size_t worst = 0;
for (int v = 0; v < kCellMod; v++) {
@@ -557,7 +576,7 @@ int test_digits_signed(const std::string &file) {
if (m.steps > worst) worst = m.steps;
const int signed_v = v >= half ? v - kCellMod : v;
std::string digits = std::to_string(signed_v < 0 ? -signed_v : signed_v);
while (digits.size() < 4) digits.insert(digits.begin(), '0');
while (int(digits.size()) < kDigits) digits.insert(digits.begin(), '0');
std::vector<uint32_t> want;
std::string text;
if (signed_v < 0) {
@@ -567,8 +586,8 @@ int test_digits_signed(const std::string &file) {
for (char c : digits)
want.push_back(uint32_t((unsigned char)c));
text += digits;
const bool ok = m.finished && m.out == want &&
m.cells[18] == 0 && m.cells[19] == 0;
const bool ok = m.finished && m.out == want && m.cells[t_cell] == 0 &&
m.cells[f_cell] == 0;
if (!ok) {
bad++;
if (bad <= 3)
@@ -614,6 +633,8 @@ int test_divmod_signed(const std::string &file) {
for (size_t i = 0; i + 1 < pairs.size(); i += 2) {
const int a = signed_of(int(pairs[i]));
const int b = signed_of(int(pairs[i + 1]));
if (b == 0)
continue; // narrow cells wrap 1024 (a boundary value) to 0: not a divisor
machine m;
m.cells[0] = pairs[i]; // N
m.cells[1] = pairs[i + 1]; // D
@@ -647,7 +668,7 @@ int test_divmod_signed(const std::string &file) {
int test_undigits(const std::string &file) {
const std::string tape = assemble(file);
std::cout << "clb_undigits: " << tape.size() << " characters\n";
const int digs = int(kCellSize) * 30103 / 100000 + 1;
const int digs = kDigits;
int bad = 0, n = 0;
size_t worst = 0;
for (int v = 0; v < kCellMod; v++) {
@@ -658,7 +679,7 @@ int test_undigits(const std::string &file) {
int digit = rest % 10;
rest /= 10;
if (garbage)
digit |= 5 << 7; // bits the macro must ignore
digit |= (5 << 7) & kCellMask; // bits above the 7 the macro reads
m.cells[size_t(i)] = uint32_t('0' + digit);
}
m.run(tape, 500'000);
@@ -742,6 +763,16 @@ int test_mul(const std::string &file) {
} // namespace
int main(int argc, char **argv) {
// Every sweep here is exhaustive over the cell range (2^W rows per value
// loop), which is fine up to 16 bits and impossible above it. 32 bit builds
// still have to work (r8forge, derive, tape_run and the rest do), so this
// exits green instead of not building at all.
if (kCellSize > 16) {
std::cout << "cell width " << kCellSize
<< " > 16: exhaustive sweeps would be 2^" << kCellSize
<< " rows, skipped\n";
return 0;
}
const std::string root = argc > 1 ? argv[1] : ".";
const std::string drv = root + "/drv/";
const int bad = test_add(drv + "raw_add_byte.r8asm") +