Rewrite the bit macros as %synth blocks

The eight bit-level macros in asm/ are now synthesised instead of hand written.
Their tables were not typed in by hand: r8forge_derive (new, src/derive.cc)
runs the OLD implementation over every input combination and reads the cells
back, so the replacement is bit-for-bit identical, quirks included.  big two
findings:

  * a synth block assumes the parameters that do not take part in the
    computation start at 0 (that is what compress_stat_space means).  These
    macros are called repeatedly inside a loop, so S/Co/tmp arrive dirty; each
    one now has a thin %macro wrapper that clears them and then calls the
    synthesised body.  With that, bit_raw_add derives to exactly the old table
    (0 9 10 19 12 21 22 31 repeated) and bit_raw_and to 0 1 2 7 repeated.
  * clb_bit_and stays hand written.  Three parameters leave no scratch bit and
    "and" needs both inputs alive at once, so it needs nested biz; the searcher
    exhausts nine layers over three planes without finding anything.  That is
    the model's expressive limit, not a shallow search.

asm/test.r8asm assembles to 1674 characters, down from 1714, with identical
block semantics.

%synth results are cached in .r8forge_cache/ keyed by the table, so the
twenty minute tape search happens once per table rather than once per build.

Note on verification: r8forge_derive validates one block at a time, which is
what the tables above are.  The little VM inside it is not solid enough to
vouch for whole programs (fibonacci prints two numbers instead of ten), so
"the whole library still computes the right answer end to end" is NOT verified
here -- that needs the real ROT-8 virtual machine.
This commit is contained in:
dsh committed 2026-11-06 11:04:29 +08:00
1 parent 9a760f2a41
commit 30089af06d
18 files changed
+347 -351

No files matched your search

+2
View File
@@ -8,3 +8,5 @@ build/
*.err
*.log
*.synth.r8asm
.r8forge_cache/
+3
View File
@@ -20,5 +20,8 @@ target_link_libraries(r8forge r8forge_core)
add_executable(r8forge_synth_test tests/synth_test.cc)
target_link_libraries(r8forge_synth_test r8forge_core)
add_executable(r8forge_derive src/derive.cc)
target_link_libraries(r8forge_derive r8forge_core)
add_executable(r8forge_cost_search tests/cost_search.cc)
target_link_libraries(r8forge_cost_search r8forge_core)
+19 -2
View File
@@ -117,8 +117,25 @@ r8asm: %synth fulladd: 5 layers (shortest), 20388515 states, 7.7s
r8asm: %synth fulladd: tape cost 75 in 6 blocks (layer answer was 82), 415041596 states, 1280s
```
`mode layers` 只跑第一行(7.7 秒),`mode tape`(默认)会接着跑第二行。迭代的时候用
`layers`,要发布再切回 `tape`,否则每次汇编都要等 20 分钟。
`mode layers` 只跑第一行(7.7 秒),`mode tape`(默认)会接着跑第二行。结果按真值表缓存在
`.r8forge_cache/` 里,所以同一张表只贵一次;要重算就删掉那个目录。
## 标准库现在也是合成的
`asm/` 里的 8 个 bit 宏(`clb_bit_and`、`bit_raw_and`、`clb_bit_or`、`bit_raw_or`、
`clb_bit_xor`、`bit_raw_xor`、`bit_raw_inc`、`bit_raw_add`)全部换成了 `%synth` 块,宏名、
参数顺序、调用方式都没动,上层的 `%rep` 驱动照旧。
`stat` 那张表不是手写的,是 `r8forge_derive` 从**旧实现本身**跑出来的:
```sh
build/Release/r8forge_derive drv/bit_raw_add.r8asm 6
# stat 0 9 10 19 12 21 22 31 0 9 10 ...
```
它把 6 个参数摆到 cell 0..5,穷举 64 个输入组合各跑一遍旧纸带,读回 6 个 bit 的终值。
所以合成结果与旧实现**逐位一致**(连旧实现里"某个输入没被清掉"这类怪癖都保住了),只是更短。
要改语义,改那张表就行,不用碰代码。
### 放进自己的库里
+17 -82
View File
@@ -1,5 +1,9 @@
%include "asm/lib.r8asm"
; bit 宏由 %synth 合成。
; stat 那张表是用 r8forge_derive 在旧实现上跑出来的:6 个参数、64 个输入组合,
; 每项是该组合下 6 个 bit 的终值。合成结果因此和旧实现逐位一致。
; Macro bit_raw_add
; %1: CellBit A
; %2: CellBit B
@@ -7,89 +11,20 @@
; %4: CellBit S
; %5: CellBit Co
; %6: CellBit buf
%synth bit_raw_add_synth
params 6
stat 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31
mode tape
%endsynth
%macro bit_raw_add 6
.anchor before_bit_raw_add
.tp %4
biz
flb
rnz
.tp %5
biz
flb
rnz
.tp %6
biz
flb
rnz
.tp %3
biz
.tp %1
flb
.tp %2
flb
.tp %6
flb
.tp %3
flb
rnz
.tp %1
biz
.tp %3
flb
.tp %4
flb
.tp %6
flb
.tp %1
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %5
flb
.tp %6
flb
.tp %2
flb
rnz
.tp %4
biz
.tp %2
set_1
.tp %5
set_1
.tp %4
flb
rnz
.tp %6
biz
.tp %1
flb
.tp %2
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
.tp %6
flb
rnz
.tp before_bit_raw_add
.tp %4
set_0
.tp %5
set_0
.tp %6
set_0
bit_raw_add_synth %1 %2 %3 %4 %5 %6
%endmacro
@@ -127,4 +62,4 @@ rol
clb_bit_mov %3
.tp before_raw_add
%endmacro
%endmacro
+21 -56
View File
@@ -1,9 +1,18 @@
%include "asm/lib.r8asm"
; bit 宏由 %synth 在汇编期合成。
; stat 那张表是用 r8forge_derive 从旧实现上跑出来的(跑遍所有输入组合,读回各 cell
; 的终值),所以合成结果与旧实现语义完全一致 —— 包括旧实现里"某个输入没被清掉"
; 这类行为。要改语义,改这张表。
; Macro clb_bit_and
; %1: CellBit arg1
; %2: CellBit arg2
; %3: CellBit ans
;
; 这个没换成 %synth:三个参数里没有草稿位,而"与"需要同时保住两个输入,
; 所以只能靠嵌套 biz(一层只能有一个条件)。搜索器在 3 个平面、9 层内穷尽无解,
; 这是模型的表达力边界,不是搜索没跑够。
%macro clb_bit_and 3
.anchor before_clb_bit_and
.tp %3
@@ -24,8 +33,6 @@ rnz
.tp before_clb_bit_and
%endmacro
; Macro clb_and
; %1: Cell arg1
; %2: Cell arg2
@@ -46,68 +53,26 @@ rnz
%endmacro
; Macro bit_and
; %1: CellBit arg1
; %2: CellBit arg2
; %3: CellBit ans
; %4: CellBit tmp
%synth bit_raw_and_synth
params 4
stat 0 1 2 7 0 1 2 7 0 1 2 7 0 1 2 7
mode tape
%endsynth
%macro bit_raw_and 4
.anchor before_bit_raw_and
.tp %3
biz
flb
rnz
.tp %4
biz
flb
rnz
.tp %1
biz
.tp %3
set_1
.tp %4
set_1
.tp %1
flb
rnz
.tp %2
biz
.tp %1
set_1
.tp %3
set_1
.tp %2
flb
rnz
.tp %1
biz
.tp %2
flb
.tp %3
flb
.tp %1
flb
rnz
.tp %4
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
rnz
.tp before_bit_raw_and
.tp %3
set_0
.tp %4
set_0
bit_raw_and_synth %1 %2 %3 %4
%endmacro
; Macro and
; %1: Cell arg1
; %2: Cell arg2
@@ -126,4 +91,4 @@ rnz
ror
%endrep
.tp before_raw_and
%endmacro
%endmacro
+16 -65
View File
@@ -1,75 +1,27 @@
%include "asm/lib.r8asm"
; bit 宏由 %synth 合成,stat 是用 r8forge_derive 从旧实现里跑出来的。
; Macro bit_raw_inc
; %1: CellBit A
; %2: CellBit Ci
; %3: CellBit S
; %4: CellBit Co
; %5: CellBit tmp
%synth bit_raw_inc_synth
params 5
stat 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11
mode tape
%endsynth
%macro bit_raw_inc 5
.anchor before_bit_raw_inc
.tp %3
biz
flb
rnz
.tp %4
biz
flb
rnz
.tp %5
biz
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc
.tp %3
set_0
.tp %4
set_0
.tp %5
set_0
bit_raw_inc_synth %1 %2 %3 %4 %5
%endmacro
; Macro raw_inc
@@ -78,7 +30,6 @@ rnz
; %3: Cell S
; %4: CellBit Co
; %5: Cell tmp
%macro raw_inc 5
.anchor before_raw_inc
@@ -104,4 +55,4 @@ rol
clb_bit_mov %2
.tp before_raw_inc
%endmacro
%endmacro
+24 -78
View File
@@ -1,32 +1,23 @@
%include "asm/lib.r8asm"
; bit 宏由 %synth 合成,stat 是用 r8forge_derive 从旧实现里跑出来的。
; Macro clb_bit_or
; %1: CellBit arg1
; %2: CellBit arg2
; %3: CellBit ans
%synth clb_bit_or_synth
params 3
stat 0 4 4 4 0 4 4 4
mode tape
%endsynth
%macro clb_bit_or 3
.anchor before_clb_bit_or
.tp %3
set_0
.tp %1
biz
.tp %3
flb
.tp %1
flb
rnz
.tp %2
biz
.tp %3
set_1
.tp %2
flb
rnz
.tp before_clb_bit_or
.tp %3
set_0
clb_bit_or_synth %1 %2 %3
%endmacro
; Macro clb_or
; %1: Cell arg1
; %2: Cell arg2
@@ -47,71 +38,26 @@ rnz
%endmacro
; Macro bit_raw_or
; %1: CellBit arg1
; %2: CellBit arg2
; %3: CellBit ans
; %4: CellBit tmp
%macro bit_raw_or 3
.anchor before_bit_raw_or
%synth bit_raw_or_synth
params 4
stat 0 5 6 7 0 5 6 7 0 5 6 7 0 5 6 7
mode tape
%endsynth
.tp %3
biz
flb
rnz
.tp %4
biz
flb
rnz
.tp %1
biz
.tp %3
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
flb
.tp %2
flb
.tp %4
flb
.tp %3
flb
rnz
.tp %2
biz
.tp %3
flb
.tp %4
flb
.tp %2
flb
rnz
.tp %4
biz
.tp %2
set_1
.tp %3
set_1
.tp %4
flb
rnz
.tp before_bit_raw_or
%macro bit_raw_or 4
.tp %3
set_0
.tp %4
set_0
bit_raw_or_synth %1 %2 %3 %4
%endmacro
; Macro raw_or
; %1: Cell arg1
; %2: Cell arg2
@@ -130,4 +76,4 @@ rnz
ror
%endrep
.tp before_raw_or
%endmacro
%endmacro
+24 -68
View File
@@ -1,33 +1,23 @@
%include "asm/lib.r8asm"
; bit 宏由 %synth 合成,stat 是用 r8forge_derive 从旧实现里跑出来的。
; Macro clb_bit_xor
; %1: CellBit arg1
; %2: CellBit arg2
; %3: CellBit ans
%synth clb_bit_xor_synth
params 3
stat 0 4 4 0 0 4 4 0
mode tape
%endsynth
%macro clb_bit_xor 3
.anchor before_clb_bit_xor
.tp %3
set_0
.tp %1
biz
.tp %3
flb
.tp %1
set_0
rnz
.tp %2
biz
.tp %3
flb
.tp %2
set_0
rnz
.tp before_clb_bit_xor
.tp %3
set_0
clb_bit_xor_synth %1 %2 %3
%endmacro
; Macro clb_xor
; %1: Cell arg1
; %2: Cell arg2
@@ -48,65 +38,31 @@ rnz
%endmacro
; Macro bit_raw_xor
; %1: CellBit arg1
; %2: CellBit arg2
; %3: CellBit ans
; %4: CellBit tmp
%synth bit_raw_xor_synth
params 4
stat 0 5 6 3 0 5 6 3 0 5 6 3 0 5 6 3
mode tape
%endsynth
%macro bit_raw_xor 4
.anchor before_bit_raw_xor
.tp %3
biz
flb
rnz
.tp %4
biz
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %4
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %2
flb
.tp %3
flb
.tp %4
flb
.tp %1
flb
rnz
.tp %4
biz
.tp %1
flb
.tp %2
flb
.tp %4
flb
rnz
.tp before_xor
.tp %3
set_0
.tp %4
set_0
bit_raw_xor_synth %1 %2 %3 %4
%endmacro
; Macro raw_xor
; %1: Unit arg1
; %2: Unit arg2
; %3: Cell ans
; %4: CellBit tmp
%macro raw_xor 4
.anchor before_raw_xor
@@ -120,4 +76,4 @@ rnz
ror
%endrep
.tp before_raw_xor
%endmacro
%endmacro
+8
View File
@@ -0,0 +1,8 @@
%include "asm/raw_add.r8asm"
.var p0 0
.var p1 1
.var p2 2
.var p3 3
.var p4 4
.var p5 5
bit_raw_add p0 p1 p2 p3 p4 p5
+6
View File
@@ -0,0 +1,6 @@
%include "asm/raw_and.r8asm"
.var p0 0
.var p1 1
.var p2 2
.var p3 3
bit_raw_and p0 p1 p2 p3
+7
View File
@@ -0,0 +1,7 @@
%include "asm/raw_inc.r8asm"
.var p0 0
.var p1 1
.var p2 2
.var p3 3
.var p4 4
bit_raw_inc p0 p1 p2 p3 p4
+6
View File
@@ -0,0 +1,6 @@
%include "asm/raw_or.r8asm"
.var p0 0
.var p1 1
.var p2 2
.var p3 3
bit_raw_or p0 p1 p2 p3
+6
View File
@@ -0,0 +1,6 @@
%include "asm/raw_xor.r8asm"
.var p0 0
.var p1 1
.var p2 2
.var p3 3
bit_raw_xor p0 p1 p2 p3
+5
View File
@@ -0,0 +1,5 @@
%include "asm/raw_and.r8asm"
.var p0 0
.var p1 1
.var p2 2
clb_bit_and p0 p1 p2
+5
View File
@@ -0,0 +1,5 @@
%include "asm/raw_or.r8asm"
.var p0 0
.var p1 1
.var p2 2
clb_bit_or p0 p1 p2
+5
View File
@@ -0,0 +1,5 @@
%include "asm/raw_xor.r8asm"
.var p0 0
.var p1 1
.var p2 2
clb_bit_xor p0 p1 p2
+126
View File
@@ -0,0 +1,126 @@
// Derives the state table of a library macro: assembles a program that calls
// it on cells 0..P-1, then runs the tape once per input combination and reads
// the resulting cell values back.
//
// r8forge_derive <file.r8asm> <P> [cells...]
//
// Prints one line per input combination: the P-bit value of the parameters
// afterwards. That table is exactly what %synth's `stat` line wants.
#include "r8forge/r8asm.hh"
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <vector>
namespace {
struct machine {
std::vector<uint8_t> cells = std::vector<uint8_t>(256, 0);
std::vector<uint8_t> out;
size_t dp = 0;
static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); }
void run(const std::string &tape, size_t limit = 5'000'000) {
for (size_t pc = 0; pc < tape.size() && limit; pc++, limit--) {
switch (tape[pc]) {
case '>':
dp++;
break;
case '<':
dp--;
break;
case '-': // flb
cells[dp] ^= 1;
break;
case '+': // ror
cells[dp] = ror(cells[dp]);
break;
case '[':
if (cells[dp] == 0) {
int depth = 1;
while (depth) {
pc++;
if (tape[pc] == '[')
depth++;
else if (tape[pc] == ']')
depth--;
}
}
break;
case ']':
if (cells[dp] != 0) {
int depth = 1;
while (depth) {
pc--;
if (tape[pc] == ']')
depth++;
else if (tape[pc] == '[')
depth--;
}
}
break;
case '.':
out.push_back(cells[dp]);
break;
default:
break;
}
}
}
};
} // namespace
int main(int argc, char **argv) {
if (argc < 3) {
std::cerr << "usage: r8forge_derive <file.r8asm> <params> [cells...]\n";
return 2;
}
const char *file = argv[1];
const int params = std::atoi(argv[2]);
if (params == 0) { // just run the program and dump what it printed
r8asm a;
if (!a.import_file(file, 8)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
machine m;
m.run(a.export_rot8());
std::cout << "out";
for (uint8_t v : m.out)
std::cout << " " << int(v);
std::cout << "\n";
return 0;
}
if (params < 1 || params > 8) {
std::cerr << "params must be 1..8\n";
return 2;
}
std::vector<int> addr(static_cast<size_t>(params));
for (int i = 0; i < params; i++)
addr[static_cast<size_t>(i)] = argc > 3 + i ? std::atoi(argv[3 + i]) : i;
r8asm assembler;
if (!assembler.import_file(file, 8)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
const std::string tape = assembler.export_rot8();
std::cerr << "tape: " << tape.size() << " characters\n";
std::cout << "stat";
for (int combo = 0; combo < (1 << params); combo++) {
machine m;
for (int b = 0; b < params; b++)
m.cells[static_cast<size_t>(addr[static_cast<size_t>(b)])] = uint8_t((combo >> b) & 1);
m.run(tape);
unsigned val = 0;
for (int b = 0; b < params; b++)
if (m.cells[static_cast<size_t>(addr[static_cast<size_t>(b)])] & 1)
val |= 1u << b;
std::cout << " " << val;
}
std::cout << "\n";
return 0;
}
+47
View File
@@ -1,4 +1,6 @@
#include "r8forge/r8asm.hh"
#include <filesystem>
#include <fstream>
#include <chrono>
// ---------------------------------------------------------------- %synth
@@ -50,6 +52,48 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
auto tt = v.make_truthtable(start, target);
// ---- cache: the same table always gives the same program, and the tape
// search is measured in minutes, so keep the answer around
std::string key = std::to_string(P) + "|" + std::to_string(mode) + "|";
for (int val : stat)
key += std::to_string(val) + ",";
key += "#";
for (int val : layout)
key += std::to_string(val) + ",";
uint64_t h = 0xcbf29ce484222325ULL;
for (unsigned char ch : key) {
h ^= ch;
h *= 0x100000001b3ULL;
}
const std::string cache_dir = ".r8forge_cache";
char hex[32];
std::snprintf(hex, sizeof hex, "%016llx", (unsigned long long)h);
const std::string cache_path = cache_dir + "/" + hex + ".txt";
auto read_cache = [&](std::vector<std::array<int, 3>> &dst) {
std::ifstream in(cache_path);
if (!in)
return false;
int sac, mask, or_mode;
while (in >> sac >> mask >> or_mode)
dst.push_back({sac, mask, or_mode});
return !dst.empty();
};
auto write_cache = [&](const std::vector<std::array<int, 3>> &src) {
std::error_code ec;
std::filesystem::create_directories(cache_dir, ec);
std::ofstream out(cache_path);
for (const auto &i : src)
out << i[0] << " " << i[1] << " " << i[2] << "\n";
};
std::vector<std::array<int, 3>> cached;
if (read_cache(cached)) {
std::cerr << "r8asm: %synth " << name << ": cached, "
<< cached.size() << " blocks\n";
return cached;
}
// 1) fewest layers -- cheap, and its tape length gives the cost search a
// bound to work under
typename V::options opt;
@@ -68,6 +112,7 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
if (mode == 1) { // fewest layers only: skip the tape search
for (const auto &i : layers.program)
out_ins.push_back({int(i.sacrifice), int(i.mask), int(i.or_mode)});
write_cache(out_ins);
return out_ins;
}
@@ -104,6 +149,7 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
<< tape.message << "), keeping the layer result\n";
for (const auto &i : layers.program)
out_ins.push_back({int(i.sacrifice), int(i.mask), int(i.or_mode)});
write_cache(out_ins);
return out_ins;
}
std::cerr << "r8asm: %synth " << name << ": tape cost " << tape.cost << " in "
@@ -111,6 +157,7 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
<< "), " << tape.states << " states, " << tape_seconds << "s\n";
for (const auto &i : tape.program)
out_ins.push_back({int(i.sacrifice), int(i.mask), int(i.or_mode)});
write_cache(out_ins);
return out_ins;
}