The cell width was CELL_SIZE on the C++ side, and the searcher's CellBit count
was CELL_ARGC. Both now match what the code and the docs around them already
called them:
* include/r8forge/r8asm.hh + src/r8asm.cc: CELL_SIZE -> cell_size
(set_cell_size assigns this->cell_size, its parameter already had that
name);
* include/r8forge/r8veritas.hh: the template parameter CELL_ARGC ->
cell_argc, 34 uses, so the searcher reads r8veritas<cell_argc>;
* AGENTS.md says which name means what, and why the name .r8asm programs
see is still _CELL_SIZE: that one belongs to the macro library, not to
the C++ side, and changing it would rewrite every macro in asm/;
* the cross check entry now says which ROT-8-viz is which: the author's fork
with --cell/--mem, and the upstream starcells/ROT-8 tree in the workspace,
whose geometry is compile time cell_size/mem_size -- it is 11 / 259 too
now, and `rot8_test <tape>` against `r8forge_derive ... -1` still gives
1322 1786 1600 5 94 127 on the hash tape.
All six self checks pass unchanged: 28729 character hash tape, synth_test 8/8,
both verify_ tapes, byte_op_test 65536/65536 for clb_mul and the rest of the
library, macro docs test green.
238 lines
8.8 KiB
C++
238 lines
8.8 KiB
C++
#include "r8forge/r8asm.hh"
|
|
#include "r8forge/r8veritas_phase_tt.hh"
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#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, int metric) {
|
|
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 target table is what the search is given, so it is the
|
|
// identity of the problem. Keying on it (rather than on the map or the stat
|
|
// line the author wrote) means two blocks that describe the same table share
|
|
// one entry, and a table of a different cell_size can never collide with it
|
|
// because the key covers every entry.
|
|
// bump kModelVersion whenever the instruction model changes, otherwise a
|
|
// table could be served a program that was optimal for the old model
|
|
static constexpr int kModelVersion = 1; // classic blocks: sacrifice + mask
|
|
std::string key = "v" + std::to_string(kModelVersion) + "|" +
|
|
std::to_string(P) + "|" + std::to_string(mode) + "|" +
|
|
std::to_string(metric) + "|";
|
|
for (int b = 0; b < P; b++) {
|
|
unsigned val = 0;
|
|
for (size_t j = 0; j < n; j++)
|
|
if ((*tt.second)[j] & (1u << b))
|
|
val |= 1u << j;
|
|
key += std::to_string(val) + ",";
|
|
}
|
|
key += "#";
|
|
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;
|
|
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;
|
|
std::string line;
|
|
while (std::getline(in, line)) {
|
|
if (line.empty() || line[0] == '#')
|
|
continue;
|
|
std::istringstream is(line);
|
|
int sac, mask, or_mode;
|
|
if (is >> 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);
|
|
out << "# params " << P << " mode " << mode << " metric " << metric << "\n";
|
|
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 = 8;
|
|
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 || 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);
|
|
return out_ins;
|
|
}
|
|
|
|
// 2) shortest tape -- exact, so it stops as soon as the goal is popped
|
|
typename V::cost_options copt;
|
|
// ~22 bytes per state. The layout free metric goes deeper than the exact one
|
|
// (415M states): bit_raw_add needed 990M, which is ~22 GB, so this ceiling
|
|
// leaves room on a 64 GB box
|
|
copt.max_states = 1'500'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;
|
|
|
|
std::array<int, P> addr{};
|
|
for (int i = 0; i < P; i++)
|
|
addr[size_t(i)] = copt.addr[size_t(i)];
|
|
|
|
const unsigned threads = std::thread::hardware_concurrency();
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
const auto tape = r8par::search_tt<P>(tt, addr, bound, threads,
|
|
copt.max_states, metric != 1);
|
|
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.prog.size() << " blocks (layer answer was " << bound << "), "
|
|
<< tape.states << " states, " << tape_seconds << "s\n";
|
|
for (const auto &i : tape.prog)
|
|
out_ins.push_back({i[0], i[1], i[2]});
|
|
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, int metric) {
|
|
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, metric);
|
|
case 2:
|
|
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, metric);
|
|
case 4:
|
|
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, metric);
|
|
case 6:
|
|
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, metric);
|
|
default:
|
|
return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode, metric);
|
|
}
|
|
}
|
|
|