Drop the inputs/outputs declarations: the table already says it

A synth block used to spell out which parameter is an input and which is an
output.  That was redundant: with map, 2^K entries already mean %1..%K are the
inputs and the value's bits land on the parameters after them; with a raw
table, nothing needs saying at all.  So 'inputs' and 'outputs' now fail with a
message pointing at map/stat, and the map form derives both.

Added 'stat' as the fully explicit form: 2^params values, one per input
combination, which is exactly the state table the search consumes.
examples/full_add_stat.r8asm is the same adder written that way and produces a
byte-identical tape to the map version (95 characters), both pass the eight
combination check.
This commit is contained in:
dsh committed 2026-11-06 10:33:34 +08:00
1 parent 6f2ecf0e22
commit 9a760f2a41
7 files changed
+129 -56

No files matched your search

+23 -13
View File
@@ -13,9 +13,7 @@ R8ASM 汇编器 + R8Veritas 搜索器,合成一个仓库。
%synth fulladd
params 6
inputs %1 %2 %3 ; 输入位,必须排在最前面
outputs %4 %5 ; 要算出来的输出位
map 0 1 1 2 1 2 2 3 ; 每个输入组合对应的输出位取值(bit0 = %4)
map 0 1 1 2 1 2 2 3 ; 8 个输入组合各自的输出位取值,%1..%3 是输入
layout 0 1 2 3 4 5 ; 可选:各参数所在 cell 的地址(默认 0..n-1)
%endsynth
@@ -57,8 +55,8 @@ tape: 90 characters
all eight combinations ok
```
注意 `inputs` / `outputs` 不能写成 `in` / `out`:`out` 和 ROT-8 助记符撞了,词法会先认成
`rot8_out`,整个 block 解析错位。
`map` 的项数决定输入个数:2^K 项就是 `%1..%K` 为输入,值的第 j 位落到 `%(K+j+1)`,剩下
的参数是必须结束为 0 的草稿。想要完全显式(或输入不在最前面)就改用 `stat`,那是整张状态表。
## 从零开始
@@ -74,8 +72,6 @@ cmake --build build --config Release
%synth fulladd
params 6
inputs %1 %2 %3 ; 3 个输入位,必须是最前面的参数
outputs %4 %5 ; 2 个输出位
map 0 1 1 2 1 2 2 3 ; 8 个输入组合各自的输出位取值
mode tape ; tape = 最短纸带(默认),layers = 只求层数最少(快)
%endsynth
@@ -92,6 +88,22 @@ output S
output Co
```
`map` 一项就说明了谁是谁:**项数是 2^K,就意味着 `%1..%K` 是输入**;值里的第 j 位
落到 `%(K+j+1)`;剩下的参数都是必须结束为 0 的草稿。所以不需要再声明 inputs/outputs。
要完全显式也可以,直接给整张状态表(每个输入组合下 6 个参数的取值),一个声明都不用:
```asm
%synth fulladd_stat
params 6
stat 0 1 2 3 4 5 6 ...
mode layers
%endsynth
```
`examples/full_add.r8asm` 是 `map` 版,`examples/full_add_stat.r8asm` 是 `stat` 版,
两者合成的纸带逐字节相同(95 字符)。
汇编:
```sh
@@ -116,8 +128,6 @@ r8asm: %synth fulladd: tape cost 75 in 6 blocks (layer answer was 82), 415041596
; asm/my_adder.r8asm
%synth bit_add
params 6
inputs %1 %2 %3
outputs %4 %5
map 0 1 1 2 1 2 2 3
%endsynth
```
@@ -126,10 +136,10 @@ r8asm: %synth fulladd: tape cost 75 in 6 blocks (layer answer was 82), 415041596
### 三条要注意的
1. **输入参数必须排在最前面**(`%1..%K`)。搜索器把真值表索引直接当输入值,靠的就是这个顺序。
2. **`inputs` / `outputs` 不能写成 `in` / `out`**:`out` 和 ROT-8 助记符撞了,词法会先认成
`rot8_out`。
3. `map` 的长度必须是 `2^输入个数`,每一项是那个组合下输出位拼成的整数(bit0 = 第一个输出)。
1. **输入永远是排在最前面的参数**(`%1..%K`,K 由 `map` 项数得出)。搜索器把真值表索引直接
当输入值,靠的就是这个顺序;输入不在最前面就用 `stat`。
2. `map` 一项里的 bit0 落在 `%(K+1)`、bit1 落在 `%(K+2)`……超出 `params` 会直接报错。
3. `stat` 必须给 `2^params` 项,每项是那个输入组合下所有参数的取值。
## 实现方式
-2
View File
@@ -9,8 +9,6 @@
; 顺序是输入组合 000,001,...,111。
%synth fulladd
params 6
inputs %1 %2 %3
outputs %4 %5
map 0 1 1 2 1 2 2 3
%endsynth
+1 -3
View File
@@ -1,9 +1,7 @@
%include "asm/lib.r8asm"
%include "asm/lib.r8asm"
%synth fulladd_fast
params 6
inputs %1 %2 %3
outputs %4 %5
map 0 1 1 2 1 2 2 3
mode layers
%endsynth
+18
View File
@@ -0,0 +1,18 @@
%include "asm/lib.r8asm"
%synth fulladd_stat
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 layers
%endsynth
.var A 0
.var B 1
.var Ci 2
.var S 3
.var Co 4
.var T 5
fulladd_stat A B Ci S Co T
output S
output Co
+8 -5
View File
@@ -13,13 +13,15 @@
#include "r8forge/r8veritas.hh"
// %synth: run the searcher on a truth table and return the block list as
// {sacrifice, mask, or_mode} triples. `layout` is the cell address of each
// parameter (empty means adjacent, 0..n-1).
// {sacrifice, mask, or_mode} triples. Either `map` (2^K output values, the
// inputs implied to be %1..%K and the outputs the ones after them) or `stat`
// (the whole 2^params state table) says what to compute. `layout` is the cell
// address of each parameter (empty means adjacent, 0..n-1).
std::vector<std::array<int, 3>>
r8asm_synth_program(int params, const std::vector<int> &in,
const std::vector<int> &out, const std::vector<int> &map,
const std::string &name, const std::vector<int> &layout,
int mode);
const std::vector<int> &stat, const std::string &name,
const std::vector<int> &layout, int mode);
class r8asm {
using data = int16_t;
@@ -122,7 +124,8 @@ private:
int params = 0;
std::vector<int> in; // param index per input bit, bit 0 first
std::vector<int> out; // param index per output bit, bit 0 first
std::vector<int> map; // 2^|in| entries: output bits of that input
std::vector<int> map; // 2^K entries: output bits of that input
std::vector<int> stat; // or the whole table: 2^params values
std::vector<int> layout; // optional: cell address of each parameter
int mode = 0; // 0 = shortest tape, 1 = fewest layers only
};
+33 -4
View File
@@ -461,9 +461,11 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
if (kw == "params") {
spec.params = std::stoi(it->tokens[1].value);
} else if (kw == "inputs" || kw == "outputs") {
std::vector<int> &dst = (kw == "inputs") ? spec.in : spec.out;
for (size_t i = 1; i < it->tokens.size(); i++)
dst.push_back(std::stoi(it->tokens[i].value) - 1);
// implied by the table now: with map the inputs are %1..%K (K from
// the number of entries) and the outputs follow them in bit order
main_tree.fail("'" + kw +
"' is gone: map already says which parameter is "
"which, use stat for the raw table");
} else if (kw == "mode") {
const std::string &m = it->tokens[1].value;
if (m == "layers")
@@ -478,12 +480,39 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
} else if (kw == "map") {
for (size_t i = 1; i < it->tokens.size(); i++)
spec.map.push_back(std::stoi(it->tokens[i].value));
} else if (kw == "stat") {
// the whole state table: one P-bit value per input combination
for (size_t i = 1; i < it->tokens.size(); i++)
spec.stat.push_back(std::stoi(it->tokens[i].value));
} else {
main_tree.fail("unknown line '" + kw + "' inside %synth");
}
}
// map alone says which parameter is which: 2^K entries mean %1..%K are
// the inputs and the value's bits land on the parameters after them
if (!spec.map.empty() && spec.in.empty()) {
size_t k = 0;
while ((size_t(1) << k) < spec.map.size())
k++;
if ((size_t(1) << k) != spec.map.size()) {
main_tree.fail("%synth '" + name + "': map needs 2^inputs entries");
} else {
for (size_t i = 0; i < k; i++)
spec.in.push_back(int(i));
int width = 0;
for (int val : spec.map)
for (int b = 0; b < 31; b++)
if ((val >> b) & 1)
width = std::max(width, b + 1);
for (int i = 0; i < width; i++)
spec.out.push_back(int(k) + i);
if (int(k) + width > spec.params)
main_tree.fail("%synth '" + name + "': map does not fit in " +
std::to_string(spec.params) + " parameters");
}
}
auto prog = r8asm_synth_program(spec.params, spec.in, spec.out,
spec.map, name, spec.layout,
spec.map, spec.stat, name, spec.layout,
spec.mode);
if (prog.empty()) {
main_tree.fail("%synth '" + name + "': nothing emitted");
+46 -29
View File
@@ -11,8 +11,9 @@
template <int P>
std::vector<std::array<int, 3>>
r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
const std::vector<int> &map, const std::string &name,
const std::vector<int> &layout, int mode) {
const std::vector<int> &map, const std::vector<int> &stat,
const std::string &name, const std::vector<int> &layout,
int mode) {
using V = r8veritas<P>;
V v;
v.initialize_nicknames();
@@ -21,16 +22,30 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
// the input cells are the low parameters, so the stat index already is the
// input combination
const size_t n = size_t(1) << P;
const unsigned in_mask = (1u << in.size()) - 1u;
typename V::stat start{}, target{};
for (size_t i = 0; i < n; i++) {
const unsigned c = unsigned(i) & in_mask;
start[i] = typename V::cell(i);
unsigned val = c; // input cells keep their value
for (size_t k = 0; k < out.size(); k++)
if ((unsigned(map[c]) >> k) & 1u)
val |= 1u << out[k];
target[i] = typename V::cell(val);
if (!stat.empty()) {
// raw state table: one P-bit value per input combination, nothing to
// derive -- this is the whole specification
if (stat.size() != n) {
std::cerr << "r8asm: %synth " << name << ": stat wants " << n
<< " values for " << P << " parameters\n";
return {};
}
for (size_t i = 0; i < n; i++) {
start[i] = typename V::cell(i);
target[i] = typename V::cell(stat[i]);
}
} else {
const unsigned in_mask = (1u << in.size()) - 1u;
for (size_t i = 0; i < n; i++) {
const unsigned c = unsigned(i) & in_mask;
start[i] = typename V::cell(i);
unsigned val = c; // input cells keep their value
for (size_t k = 0; k < out.size(); k++)
if ((unsigned(map[c]) >> k) & 1u)
val |= 1u << out[k];
target[i] = typename V::cell(val);
}
}
auto tt = v.make_truthtable(start, target);
@@ -102,40 +117,42 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
std::vector<std::array<int, 3>>
r8asm_synth_program(int params, const std::vector<int> &in,
const std::vector<int> &out, const std::vector<int> &map,
const std::string &name,
const std::vector<int> &stat, const std::string &name,
const std::vector<int> &layout, int mode) {
if (params < 1 || params > 8) {
std::cerr << "r8asm: %synth " << name << ": supports 1..8 parameters\n";
return {};
}
if (in.empty() || map.size() != (size_t(1) << in.size())) {
std::cerr << "r8asm: %synth " << name
<< ": map must have 2^inputs entries\n";
return {};
}
for (size_t i = 0; i < in.size(); i++)
if (in[i] != int(i)) {
std::cerr << "r8asm: %synth " << name << ": input %" << i + 1
<< " must be %" << i + 1 << " (inputs come first)\n";
if (stat.empty()) { // with a raw table there is nothing to check here
if (in.empty() || map.size() != (size_t(1) << in.size())) {
std::cerr << "r8asm: %synth " << name
<< ": map must have 2^inputs entries\n";
return {};
}
for (size_t i = 0; i < in.size(); i++)
if (in[i] != int(i)) {
std::cerr << "r8asm: %synth " << name << ": input %" << i + 1
<< " must be %" << i + 1 << " (inputs come first)\n";
return {};
}
}
switch (params) {
case 1:
return r8asm_synth_run<1>(in, out, map, name, layout, mode);
return r8asm_synth_run<1>(in, out, map, stat, name, layout, mode);
case 2:
return r8asm_synth_run<2>(in, out, map, name, layout, mode);
return r8asm_synth_run<2>(in, out, map, stat, name, layout, mode);
case 3:
return r8asm_synth_run<3>(in, out, map, name, layout, mode);
return r8asm_synth_run<3>(in, out, map, stat, name, layout, mode);
case 4:
return r8asm_synth_run<4>(in, out, map, name, layout, mode);
return r8asm_synth_run<4>(in, out, map, stat, name, layout, mode);
case 5:
return r8asm_synth_run<5>(in, out, map, name, layout, mode);
return r8asm_synth_run<5>(in, out, map, stat, name, layout, mode);
case 6:
return r8asm_synth_run<6>(in, out, map, name, layout, mode);
return r8asm_synth_run<6>(in, out, map, stat, name, layout, mode);
case 7:
return r8asm_synth_run<7>(in, out, map, name, layout, mode);
return r8asm_synth_run<7>(in, out, map, stat, name, layout, mode);
default:
return r8asm_synth_run<8>(in, out, map, name, layout, mode);
return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode);
}
}