diff --git a/README.md b/README.md index c80b4d7..cab31c1 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,31 @@ all eight combinations ok `map` 的项数决定输入个数:2^K 项就是 `%1..%K` 为输入,值的第 j 位落到 `%(K+j+1)`,剩下 的参数是必须结束为 0 的草稿。想要完全显式(或输入不在最前面)就改用 `stat`,那是整张状态表。 +## 三种代价口径 + +%synth 里用 metric 选优化目标: + +| metric | 优化什么 | 依赖 cell 布局? | 缓存能否跨调用点复用 | +|---|---|---|---| +| chars(默认) | 真实纸带字符数,含 .tp 行走 | 是(可用 layout 指定) | 同一个布局才能 | +| lat | 字符数,但**不计 .tp 行走** | 否 | **能** | +| locks | 块数 | 否 | 能 | + + lat 就是"步长无关模式":它只数 iz/ lb/ nz 和 op 本身,不看 cell 之间隔多远, +所以同一张真值表在任何布局下都用同一份结果、同一份缓存。代价是纸带会比按实际布局最优的 +长一点 —— full_add 上 chars 是 90 字符, lat 是 95 字符。 + +` +%synth fulladd_flat + params 6 + map 0 1 1 2 1 2 2 3 + metric flat +%endsynth +` + +同一个 metric flat 的块配上 layout 0 2 4 6 8 10 再汇编,会直接命中同一份缓存并且 +发射出**完全相同**的纸带(已验证)。 + ## 从零开始 ```sh diff --git a/examples/full_add_flat.r8asm b/examples/full_add_flat.r8asm new file mode 100644 index 0000000..448a11e --- /dev/null +++ b/examples/full_add_flat.r8asm @@ -0,0 +1,20 @@ +%include "asm/lib.r8asm" + +; ????:metric flat ?? .tp ?????,??? cell ????, +; ??????????????(?????????????????)? +%synth fulladd_flat + params 6 + map 0 1 1 2 1 2 2 3 + metric flat +%endsynth + +.var A 0 +.var B 1 +.var Ci 2 +.var S 3 +.var Co 4 +.var T 5 + +fulladd_flat A B Ci S Co T +output S +output Co diff --git a/examples/full_add_flat_spaced.r8asm b/examples/full_add_flat_spaced.r8asm new file mode 100644 index 0000000..461d821 --- /dev/null +++ b/examples/full_add_flat_spaced.r8asm @@ -0,0 +1,22 @@ +%include "asm/lib.r8asm" + +; ????:metric flat ?? .tp ?????,??? cell ????, +; ??????????????(?????????????????)? +%synth fulladd_flat + params 6 + map 0 1 1 2 1 2 2 3 + layout 0 2 4 6 8 10 + metric flat +%endsynth + +.var A 0 +.var B 1 +.var Ci 2 +.var S 3 +.var Co 4 +.var T 5 + +fulladd_flat A B Ci S Co T +output S +output Co + diff --git a/include/r8forge/r8asm.hh b/include/r8forge/r8asm.hh index 1b14499..2e8e708 100644 --- a/include/r8forge/r8asm.hh +++ b/include/r8forge/r8asm.hh @@ -21,7 +21,7 @@ std::vector> r8asm_synth_program(int params, const std::vector &in, const std::vector &out, const std::vector &map, const std::vector &stat, const std::string &name, - const std::vector &layout, int mode); + const std::vector &layout, int mode, int metric); class r8asm { using data = int16_t; @@ -127,7 +127,9 @@ private: 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 + int mode = 0; // 0 = shortest tape, 1 = fewest layers only + int metric = 0; // 0 = tape chars (needs the layout), 1 = chars without + // the .tp walk (any layout), 2 = blocks }; private: diff --git a/include/r8forge/r8veritas_phase.hh b/include/r8forge/r8veritas_phase.hh index 99513ca..2e7efb4 100644 --- a/include/r8forge/r8veritas_phase.hh +++ b/include/r8forge/r8veritas_phase.hh @@ -70,7 +70,7 @@ template struct engine { // refuses to convert that to an int non-type parameter (MSVC lets it pass) template inline int block_cost(const std::array &addr, int sacrifice, int mask, - bool or_mode) { + bool or_mode, bool count_travel) { int lo = addr[size_t(sacrifice)], hi = lo, cnt = 0; for (int b = 0; b < N; b++) if (mask & (1 << b)) { @@ -78,7 +78,10 @@ inline int block_cost(const std::array &addr, int sacrifice, int mask, lo = std::min(lo, addr[size_t(b)]); hi = std::max(hi, addr[size_t(b)]); } - return 3 + 2 * (hi - lo) + cnt * (or_mode ? 4 : 1); + // count_travel = false drops the .tp walk, which is the only part of the + // price that depends on where the cells sit: the result then works for any + // layout and can be cached once for all call sites + return 3 + (count_travel ? 2 * (hi - lo) : 0) + cnt * (or_mode ? 4 : 1); } struct result_t { @@ -96,7 +99,7 @@ result_t search(const typename engine::key &start, const typename engine::key &goal, const std::vector> &ins, const std::array &addr, int limit, unsigned nthreads, - size_t max_states) { + size_t max_states, bool count_travel = true) { using E = engine; using key = typename E::key; result_t r; @@ -112,7 +115,7 @@ result_t search(const typename engine::key &start, std::vector w(ni); int wmax = 0; for (size_t i = 0; i < ni; i++) { - w[i] = block_cost(addr, ins[i][0], ins[i][1], ins[i][2] != 0); + w[i] = block_cost(addr, ins[i][0], ins[i][1], ins[i][2] != 0, count_travel); if (w[i] > wmax) wmax = w[i]; } diff --git a/include/r8forge/r8veritas_phase_tt.hh b/include/r8forge/r8veritas_phase_tt.hh index f444402..2c43c5f 100644 --- a/include/r8forge/r8veritas_phase_tt.hh +++ b/include/r8forge/r8veritas_phase_tt.hh @@ -34,7 +34,8 @@ template result_t search_k(const typename r8veritas::truthtable &tt, const std::vector &bit_of, const std::array &addr, int limit, - unsigned threads, size_t max_states) { + unsigned threads, size_t max_states, + bool count_travel = true) { using E = engine; using key = typename E::key; const size_t entries = size_t(1) << K; @@ -54,13 +55,13 @@ result_t search_k(const typename r8veritas::truthtable &tt, } return search(start, goal, classic_blocks(), addr, limit, threads, - max_states); + max_states, count_travel); } template result_t search_tt(const typename r8veritas::truthtable &tt, const std::array &addr, int limit, unsigned threads, - size_t max_states) { + size_t max_states, bool count_travel = true) { std::vector bit_of; for (int i = 0; i < N; i++) for (size_t j = 0; j < (size_t(1) << N); j++) @@ -70,17 +71,17 @@ result_t search_tt(const typename r8veritas::truthtable &tt, } switch (bit_of.size()) { case 1: - return search_k(tt, bit_of, addr, limit, threads, max_states); + return search_k(tt, bit_of, addr, limit, threads, max_states, count_travel); case 2: - return search_k(tt, bit_of, addr, limit, threads, max_states); + return search_k(tt, bit_of, addr, limit, threads, max_states, count_travel); case 3: - return search_k(tt, bit_of, addr, limit, threads, max_states); + return search_k(tt, bit_of, addr, limit, threads, max_states, count_travel); case 4: - return search_k(tt, bit_of, addr, limit, threads, max_states); + return search_k(tt, bit_of, addr, limit, threads, max_states, count_travel); case 5: - return search_k(tt, bit_of, addr, limit, threads, max_states); + return search_k(tt, bit_of, addr, limit, threads, max_states, count_travel); case 6: - return search_k(tt, bit_of, addr, limit, threads, max_states); + return search_k(tt, bit_of, addr, limit, threads, max_states, count_travel); default: { result_t r; r.message = "the answer depends on " + std::to_string(bit_of.size()) + diff --git a/src/r8asm.cc b/src/r8asm.cc index b33427d..858aaab 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -466,6 +466,17 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { main_tree.fail("'" + kw + "' is gone: map already says which parameter is " "which, use stat for the raw table"); + } else if (kw == "metric") { + const std::string &m = it->tokens[1].value; + if (m == "chars") + spec.metric = 0; + else if (m == "flat") + spec.metric = 1; + else if (m == "blocks") + spec.metric = 2; + else + main_tree.fail("unknown metric '" + m + + "' (chars, flat or blocks)"); } else if (kw == "mode") { const std::string &m = it->tokens[1].value; if (m == "layers") @@ -513,7 +524,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { } auto prog = r8asm_synth_program(spec.params, spec.in, spec.out, spec.map, spec.stat, name, spec.layout, - spec.mode); + spec.mode, spec.metric); if (prog.empty()) { main_tree.fail("%synth '" + name + "': nothing emitted"); } else { diff --git a/src/synth.cc b/src/synth.cc index 72a8b95..6f21fa8 100644 --- a/src/synth.cc +++ b/src/synth.cc @@ -16,7 +16,7 @@ std::vector> r8asm_synth_run(const std::vector &in, const std::vector &out, const std::vector &map, const std::vector &stat, const std::string &name, const std::vector &layout, - int mode) { + int mode, int metric) { using V = r8veritas

; V v; v.initialize_nicknames(); @@ -55,12 +55,14 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, // ---- 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) + "|"; + std::string key = std::to_string(P) + "|" + std::to_string(mode) + "|" + + std::to_string(metric) + "|"; for (int val : stat) key += std::to_string(val) + ","; key += "#"; - for (int val : layout) - key += std::to_string(val) + ","; + if (metric == 0) // only the character metric cares where the cells are + for (int val : layout) + key += std::to_string(val) + ","; uint64_t h = 0xcbf29ce484222325ULL; for (unsigned char ch : key) { h ^= ch; @@ -110,7 +112,7 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, << " layers" << (layers.proven_optimal ? " (shortest)" : "") << ", " << layers.states << " states, " << layers.seconds << "s\n"; - if (mode == 1) { // fewest layers only: skip the tape search + if (mode == 1 || metric == 2) { // blocks 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); @@ -146,8 +148,8 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, const unsigned threads = std::thread::hardware_concurrency(); const auto t0 = std::chrono::steady_clock::now(); - const auto tape = - r8par::search_tt

(tt, addr, bound, threads, copt.max_states); + const auto tape = r8par::search_tt

(tt, addr, bound, threads, + copt.max_states, metric != 1); const double tape_seconds = std::chrono::duration(std::chrono::steady_clock::now() - t0) .count(); @@ -172,7 +174,7 @@ std::vector> r8asm_synth_program(int params, const std::vector &in, const std::vector &out, const std::vector &map, const std::vector &stat, const std::string &name, - const std::vector &layout, int mode) { + const std::vector &layout, int mode, int metric) { if (params < 1 || params > 8) { std::cerr << "r8asm: %synth " << name << ": supports 1..8 parameters\n"; return {}; @@ -192,21 +194,21 @@ r8asm_synth_program(int params, const std::vector &in, } switch (params) { case 1: - return r8asm_synth_run<1>(in, out, map, stat, name, layout, mode); + return r8asm_synth_run<1>(in, out, map, stat, name, layout, mode, metric); case 2: - return r8asm_synth_run<2>(in, out, map, stat, name, layout, mode); + return r8asm_synth_run<2>(in, out, map, stat, name, layout, mode, metric); case 3: - return r8asm_synth_run<3>(in, out, map, stat, name, layout, mode); + return r8asm_synth_run<3>(in, out, map, stat, name, layout, mode, metric); case 4: - return r8asm_synth_run<4>(in, out, map, stat, name, layout, mode); + return r8asm_synth_run<4>(in, out, map, stat, name, layout, mode, metric); case 5: - return r8asm_synth_run<5>(in, out, map, stat, name, layout, mode); + return r8asm_synth_run<5>(in, out, map, stat, name, layout, mode, metric); case 6: - return r8asm_synth_run<6>(in, out, map, stat, name, layout, mode); + return r8asm_synth_run<6>(in, out, map, stat, name, layout, mode, metric); case 7: - return r8asm_synth_run<7>(in, out, map, stat, name, layout, mode); + return r8asm_synth_run<7>(in, out, map, stat, name, layout, mode, metric); default: - return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode); + return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode, metric); } }