From 9a760f2a41650e669a825cb4970ce1dd0eea7733 Mon Sep 17 00:00:00 2001 From: dsh Date: Fri, 6 Nov 2026 10:33:34 +0800 Subject: [PATCH] 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. --- README.md | 36 ++++++++++------ examples/full_add.r8asm | 2 - examples/full_add_layers.r8asm | 4 +- examples/full_add_stat.r8asm | 18 ++++++++ include/r8forge/r8asm.hh | 13 +++--- src/r8asm.cc | 37 +++++++++++++++-- src/synth.cc | 75 +++++++++++++++++++++------------- 7 files changed, 129 insertions(+), 56 deletions(-) create mode 100644 examples/full_add_stat.r8asm diff --git a/README.md b/README.md index cfec427..3676fe2 100644 --- a/README.md +++ b/README.md @@ -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` 项,每项是那个输入组合下所有参数的取值。 ## 实现方式 diff --git a/examples/full_add.r8asm b/examples/full_add.r8asm index 255857b..9c3c361 100644 --- a/examples/full_add.r8asm +++ b/examples/full_add.r8asm @@ -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 diff --git a/examples/full_add_layers.r8asm b/examples/full_add_layers.r8asm index 372814a..82763e8 100644 --- a/examples/full_add_layers.r8asm +++ b/examples/full_add_layers.r8asm @@ -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 diff --git a/examples/full_add_stat.r8asm b/examples/full_add_stat.r8asm new file mode 100644 index 0000000..76d1ba2 --- /dev/null +++ b/examples/full_add_stat.r8asm @@ -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 diff --git a/include/r8forge/r8asm.hh b/include/r8forge/r8asm.hh index 3085089..1b14499 100644 --- a/include/r8forge/r8asm.hh +++ b/include/r8forge/r8asm.hh @@ -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> r8asm_synth_program(int params, const std::vector &in, const std::vector &out, const std::vector &map, - const std::string &name, const std::vector &layout, - int mode); + const std::vector &stat, const std::string &name, + const std::vector &layout, int mode); class r8asm { using data = int16_t; @@ -122,7 +124,8 @@ private: int params = 0; std::vector in; // param index per input bit, bit 0 first std::vector out; // param index per output bit, bit 0 first - std::vector map; // 2^|in| entries: output bits of that input + std::vector map; // 2^K entries: output bits of that input + std::vector stat; // or the whole table: 2^params values std::vector layout; // optional: cell address of each parameter int mode = 0; // 0 = shortest tape, 1 = fewest layers only }; diff --git a/src/r8asm.cc b/src/r8asm.cc index efa758d..b33427d 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -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 &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"); diff --git a/src/synth.cc b/src/synth.cc index 693e009..ea87721 100644 --- a/src/synth.cc +++ b/src/synth.cc @@ -11,8 +11,9 @@ template std::vector> r8asm_synth_run(const std::vector &in, const std::vector &out, - const std::vector &map, const std::string &name, - const std::vector &layout, int mode) { + const std::vector &map, const std::vector &stat, + const std::string &name, const std::vector &layout, + int mode) { using V = r8veritas

; V v; v.initialize_nicknames(); @@ -21,16 +22,30 @@ r8asm_synth_run(const std::vector &in, const std::vector &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 &in, const std::vector &out, std::vector> r8asm_synth_program(int params, const std::vector &in, const std::vector &out, const std::vector &map, - const std::string &name, + const std::vector &stat, const std::string &name, const std::vector &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); } }