The layer search still runs first (7.7 seconds) but only to get a bound; the cost search then takes over and its answer is what goes on the tape. A caller that wants its own layout can say 'layout 0 2 4 6 8 10' inside the block. examples/full_add.r8asm now assembles to 90 characters of tape instead of 95: cost 75 in 6 blocks after 415M states and about 21 minutes, against the 82 the five layer program needs. Assembly getting slower is the whole point -- it is the price of the shortest tape. For scale: one bit_raw_add slice is 179 characters with the cells adjacent, and the synthesised slice is 82. A whole raw_add call is 1650 characters today, so eight synthesised slices would land near 700.
136 lines
4.8 KiB
C++
136 lines
4.8 KiB
C++
#include "r8forge/r8asm.hh"
|
|
#include <chrono>
|
|
|
|
// ---------------------------------------------------------------- %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 <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) {
|
|
using V = r8veritas<P>;
|
|
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<std::array<int, 3>> 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<double>(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<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) {
|
|
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);
|
|
}
|
|
}
|
|
|