#include "r8forge/r8asm.hh" #include // ---------------------------------------------------------------- %synth // // A synth block is a macro whose body the assembler synthesises. The search // runs once while the file is parsed; the resulting block sequence becomes // ordinary macro body lines with %1..%n parameter references, so everything // after that goes through the normal, well tested macro expansion. 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) { using V = r8veritas

; V v; v.initialize_nicknames(); v.initialize_tp_paths(); // 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); } auto tt = v.make_truthtable(start, target); // 1) fewest layers -- cheap, and its tape length gives the cost search a // bound to work under typename V::options opt; opt.max_depth = 5; const auto layers = v.solve(tt, opt); std::vector> out_ins; if (!layers.found) { std::cerr << "r8asm: %synth " << name << ": no program up to " << opt.max_depth << " layers\n"; return {}; } std::cerr << "r8asm: %synth " << name << ": " << layers.program.size() << " layers" << (layers.proven_optimal ? " (shortest)" : "") << ", " << layers.states << " states, " << layers.seconds << "s\n"; // 2) shortest tape -- exact, so it stops as soon as the goal is popped typename V::cost_options copt; copt.max_states = 450'000'000; if (int(layout.size()) == P) for (int i = 0; i < P; i++) copt.addr[i] = layout[size_t(i)]; else for (int i = 0; i < P; i++) copt.addr[i] = i; int bound = 0; for (const auto &i : layers.program) { int lo = copt.addr[i.sacrifice], hi = lo, cnt = 0; for (int b = 0; b < P; b++) if (i.mask & (1 << b)) { cnt++; lo = std::min(lo, copt.addr[b]); hi = std::max(hi, copt.addr[b]); } bound += 3 + 2 * (hi - lo) + cnt * (i.or_mode ? 4 : 1); } copt.max_cost = bound; const auto t0 = std::chrono::steady_clock::now(); const auto tape = v.solve_cost(tt, copt); const double tape_seconds = std::chrono::duration(std::chrono::steady_clock::now() - t0) .count(); if (!tape.found) { std::cerr << "r8asm: %synth " << name << ": tape search failed (" << 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)}); return out_ins; } std::cerr << "r8asm: %synth " << name << ": tape cost " << tape.cost << " in " << tape.program.size() << " blocks (layer answer was " << bound << "), " << 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)}); return out_ins; } 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) { 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"; return {}; } switch (params) { case 1: return r8asm_synth_run<1>(in, out, map, name, layout); case 2: return r8asm_synth_run<2>(in, out, map, name, layout); case 3: return r8asm_synth_run<3>(in, out, map, name, layout); case 4: return r8asm_synth_run<4>(in, out, map, name, layout); case 5: return r8asm_synth_run<5>(in, out, map, name, layout); case 6: return r8asm_synth_run<6>(in, out, map, name, layout); case 7: return r8asm_synth_run<7>(in, out, map, name, layout); default: return r8asm_synth_run<8>(in, out, map, name, layout); } }