Files
R8Forge/tests/cost_search.cc
dsh 505fe8af86 Make it build under clang
Found with scoop's llvm 23.1.5 (clang-cl), which is stricter than MSVC about
non-type template arguments:

    candidate template ignored: substitution failure: deduced non-type
    template argument does not have the same type as the corresponding
    template parameter ('unsigned long long' vs 'int')

block_cost deduced its N from std::array<int, N>, and that deduces size_t;
clang refuses to convert size_t to the int parameter, MSVC allows it.  N is now
size_t.  Every target (r8forge, synth_test, derive, cost_par, cost_search)
builds clean with clang-cl and the clang-built binaries assemble test.r8asm to
the same 1674 characters, pass the eight combination check and derive the same
bit_raw_add table.

Two other portability fixes in the same pass:
  * cost_search used _strtoui64, which is MSVC only -- now std::strtoull
  * the CMake project now finds Threads and links Threads::Threads, which
    clang and MinGW need to pull in the thread runtime
  * if spawning a worker throws (no thread runtime available) the search now
    carries on with the threads it did get instead of failing
2026-11-06 13:54:46 +08:00

66 lines
2.2 KiB
C++

// Shortest *tape* instead of fewest layers: Dijkstra over the same state
// graph, where a block costs what it assembles to.
#include "r8forge/r8veritas.hh"
#include <cstdlib>
#include <iostream>
using V = r8veritas<6>;
int main(int argc, char **argv) {
const int limit = argc > 1 ? std::atoi(argv[1]) : 87;
const bool spaced = argc > 2 ? std::atoi(argv[2]) != 0 : false;
V v;
v.initialize_nicknames();
v.initialize_tp_paths();
const int problem = argc > 4 ? std::atoi(argv[4]) : 0;
auto raw = v.generate_truthtable([problem](V::cell inp) -> V::cell {
if (problem == 1) { // copy: A -> bit0,bit1
unsigned a = inp & 1;
return V::cell((a << 1) | a);
}
if (problem == 2) { // clb_and: A&B -> bit2
unsigned a = inp & 1, b = (inp >> 1) & 1;
return V::cell(((a & b) << 2));
}
unsigned a = inp & 1, b = (inp >> 1) & 1, c = (inp >> 2) & 1;
unsigned s = a ^ b ^ c;
unsigned co = (a & b) | (a & c) | (b & c);
return V::cell((co << 4) | (s << 3) | (inp & 0b111));
});
auto compressed = v.compress_stat_space(raw);
V::cost_options opt;
for (int i = 0; i < 6; i++)
opt.addr[i] = spaced ? 2 * i : i; // 0,1,2.. or 0,2,4..
opt.max_cost = limit;
opt.verbose = true;
if (argc > 3)
opt.max_states = size_t(std::strtoull(argv[3], nullptr, 10));
const auto res = v.solve_cost(compressed, opt);
std::cout << "limit " << limit << ", addresses ";
for (int i = 0; i < 6; i++)
std::cout << opt.addr[i] << (i == 5 ? "" : ",");
std::cout << "\n";
if (!res.found) {
std::cout << "no program with cost <= " << limit << " " << res.states
<< " states, " << res.seconds << "s " << res.message << "\n";
return res.proven ? 0 : 2;
}
std::cout << "cost " << res.cost << ", " << res.program.size() << " blocks, "
<< res.states << " states, " << res.seconds << "s\n";
for (const auto &i : res.program) {
std::cout << " sacrifice " << int(i.sacrifice) << " "
<< (i.or_mode ? "SET1" : "FLIP") << " {";
for (int b = 0; b < 6; b++)
if (i.mask & (1 << b))
std::cout << " " << b;
std::cout << " }\n";
}
std::cout << "replay: "
<< (v.run_program(compressed, res.program) ? "ok" : "MISMATCH")
<< "\n";
return 0;
}