The eight bit-level macros in asm/ are now synthesised instead of hand written.
Their tables were not typed in by hand: r8forge_derive (new, src/derive.cc)
runs the OLD implementation over every input combination and reads the cells
back, so the replacement is bit-for-bit identical, quirks included. big two
findings:
* a synth block assumes the parameters that do not take part in the
computation start at 0 (that is what compress_stat_space means). These
macros are called repeatedly inside a loop, so S/Co/tmp arrive dirty; each
one now has a thin %macro wrapper that clears them and then calls the
synthesised body. With that, bit_raw_add derives to exactly the old table
(0 9 10 19 12 21 22 31 repeated) and bit_raw_and to 0 1 2 7 repeated.
* clb_bit_and stays hand written. Three parameters leave no scratch bit and
"and" needs both inputs alive at once, so it needs nested biz; the searcher
exhausts nine layers over three planes without finding anything. That is
the model's expressive limit, not a shallow search.
asm/test.r8asm assembles to 1674 characters, down from 1714, with identical
block semantics.
%synth results are cached in .r8forge_cache/ keyed by the table, so the
twenty minute tape search happens once per table rather than once per build.
Note on verification: r8forge_derive validates one block at a time, which is
what the tables above are. The little VM inside it is not solid enough to
vouch for whole programs (fibonacci prints two numbers instead of ten), so
"the whole library still computes the right answer end to end" is NOT verified
here -- that needs the real ROT-8 virtual machine.
206 lines
7.3 KiB
C++
206 lines
7.3 KiB
C++
#include "r8forge/r8asm.hh"
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#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::vector<int> &stat,
|
|
const std::string &name, const std::vector<int> &layout,
|
|
int mode) {
|
|
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;
|
|
typename V::stat start{}, target{};
|
|
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);
|
|
|
|
// ---- 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) + "|";
|
|
for (int val : stat)
|
|
key += std::to_string(val) + ",";
|
|
key += "#";
|
|
for (int val : layout)
|
|
key += std::to_string(val) + ",";
|
|
uint64_t h = 0xcbf29ce484222325ULL;
|
|
for (unsigned char ch : key) {
|
|
h ^= ch;
|
|
h *= 0x100000001b3ULL;
|
|
}
|
|
const std::string cache_dir = ".r8forge_cache";
|
|
char hex[32];
|
|
std::snprintf(hex, sizeof hex, "%016llx", (unsigned long long)h);
|
|
const std::string cache_path = cache_dir + "/" + hex + ".txt";
|
|
|
|
auto read_cache = [&](std::vector<std::array<int, 3>> &dst) {
|
|
std::ifstream in(cache_path);
|
|
if (!in)
|
|
return false;
|
|
int sac, mask, or_mode;
|
|
while (in >> sac >> mask >> or_mode)
|
|
dst.push_back({sac, mask, or_mode});
|
|
return !dst.empty();
|
|
};
|
|
auto write_cache = [&](const std::vector<std::array<int, 3>> &src) {
|
|
std::error_code ec;
|
|
std::filesystem::create_directories(cache_dir, ec);
|
|
std::ofstream out(cache_path);
|
|
for (const auto &i : src)
|
|
out << i[0] << " " << i[1] << " " << i[2] << "\n";
|
|
};
|
|
|
|
std::vector<std::array<int, 3>> cached;
|
|
if (read_cache(cached)) {
|
|
std::cerr << "r8asm: %synth " << name << ": cached, "
|
|
<< cached.size() << " blocks\n";
|
|
return cached;
|
|
}
|
|
|
|
// 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";
|
|
|
|
if (mode == 1) { // fewest layers 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);
|
|
return out_ins;
|
|
}
|
|
|
|
// 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)});
|
|
write_cache(out_ins);
|
|
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)});
|
|
write_cache(out_ins);
|
|
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::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 (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, stat, name, layout, mode);
|
|
case 2:
|
|
return r8asm_synth_run<2>(in, out, map, stat, name, layout, mode);
|
|
case 3:
|
|
return r8asm_synth_run<3>(in, out, map, stat, name, layout, mode);
|
|
case 4:
|
|
return r8asm_synth_run<4>(in, out, map, stat, name, layout, mode);
|
|
case 5:
|
|
return r8asm_synth_run<5>(in, out, map, stat, name, layout, mode);
|
|
case 6:
|
|
return r8asm_synth_run<6>(in, out, map, stat, name, layout, mode);
|
|
case 7:
|
|
return r8asm_synth_run<7>(in, out, map, stat, name, layout, mode);
|
|
default:
|
|
return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode);
|
|
}
|
|
}
|
|
|