From a46246810cf8b6096d645320ac945191e132d9cd Mon Sep 17 00:00:00 2001 From: StarCells Date: Fri, 24 Jul 2026 20:43:34 +0800 Subject: [PATCH] [err]202607242043 --- include/r8veritas.hh | 39 ++++++++++++++++---- src/r8veritas.cc | 88 ++++++++++++++++++++++++++++++++++++++------ tests/r8_test.cc | 22 ++++++++--- 3 files changed, 125 insertions(+), 24 deletions(-) diff --git a/include/r8veritas.hh b/include/r8veritas.hh index 0791034..1653b55 100644 --- a/include/r8veritas.hh +++ b/include/r8veritas.hh @@ -1,10 +1,13 @@ #ifndef R8VERITAS #define R8VERITAS +#include #include #include #include +#include #include +#include #include #include @@ -23,26 +26,34 @@ using stat = std::array; using truthtable = std::pair; using op = std::function; using index = size_t; +using influence = size_t; +using ins_body = size_t; +using ins_bytecode = size_t; +/* +using ins_body = std::bitset; +using ins_bytecode = std::bitset; +*/ enum ins_opcode { SET1 = 0, FLIP = 1, }; -/* -using ins_body = std::bitset; -using ins_bytecode = std::bitset; -*/ -using ins_body = size_t; -using ins_bytecode = size_t; using ins = struct ins { r8veritas::ins_body bc; r8veritas::index sacrifice; operator const ins_bytecode() const { return (bc << CELL_SIZE_BITWIDTH) | sacrifice; } + r8veritas::ins operator()(const r8veritas::ins_bytecode &bc) { + return {bc >> CELL_SIZE_BITWIDTH, bc & ((1 << CELL_SIZE_BITWIDTH) - 1)}; + } }; +using path = std::stack>; + +static std::array all_influences; + static const std::set all_ins = []() -> std::set { std::set s; @@ -73,6 +84,7 @@ inline r8veritas::stat generate_empty_stat() { } namespace std { + // Overload hash to make some unordered_maps work. template <> struct hash { // Magic,from Boost's hash_combine. @@ -102,8 +114,9 @@ template <> struct hash> { #define OPCODE_FLIP (static_cast(r8veritas::ins_opcode::FLIP)) r8veritas::truthtable generate_truthtable(const r8veritas::op &operation); -r8veritas::stat operate_ins(const r8veritas::stat &last_stat, - const r8veritas::ins &ins); +std::pair operate_ins(const r8veritas::stat &last_stat, + const r8veritas::ins &ins); +// true means that {stat,ins} has been operated. static std::unordered_map, r8veritas::stat> @@ -113,4 +126,14 @@ static std::unordered_map> visited_op; +std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s); + +r8veritas::truthtable +compress_stat_space(const r8veritas::truthtable &raw_truthtable); +// raw means input must be 0 ~ (1< +#include +#include #include +#include #include r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) { @@ -13,18 +15,13 @@ r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) { return (r8veritas::truthtable(inp, out)); } -r8veritas::stat operate_ins(const r8veritas::stat &last_stat, - const r8veritas::ins &ins) { +std::pair operate_ins(const r8veritas::stat &last_stat, + const r8veritas::ins &ins) { auto key = std::pair(last_stat, r8veritas::ins_bytecode(ins)); r8veritas::stat next_stat = last_stat; auto it = visited_stat.find(key); if (it != visited_stat.end()) { // foud in visited_stat - // DEBUG - std::cout - << "Visited:" - << std::hash>{}(key) - << std::endl; - return (it->second); + return {it->second, true}; } int opcode = ins.bc & 1; @@ -42,6 +39,75 @@ r8veritas::stat operate_ins(const r8veritas::stat &last_stat, } visited_stat.insert_or_assign(key, next_stat); visited_op.insert_or_assign(next_stat, std::move(key)); - - return (next_stat); + return {next_stat, false}; } + +std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s) { + for (auto i : s) { + os << std::bitset(i).to_string() << " "; + } + return (os); +} + +r8veritas::truthtable +compress_stat_space(const r8veritas::truthtable &raw_truthtable) { + std::array influences; + r8veritas::truthtable new_truthtable = raw_truthtable; + for (r8veritas::index i = 0; i < CELL_SIZE; i++) { + influences[i] = 0; + for (r8veritas::index j = 0; j < (1 << CELL_SIZE); j++) { + if (raw_truthtable.second[j] != raw_truthtable.second[j ^ (1 << i)]) { + influences[i]++; + } + } + influences[i] >>= 1; + } + for (r8veritas::index i = 0; i < CELL_SIZE; i++) { + if (influences[i] == 0) { + new_truthtable.first = operate_ins(new_truthtable.first, {0, i}).first; + } + } + return (new_truthtable); +} + +static std::queue stats; +bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) { + + if (layer == 1) { + stats.push(truthtable.first); + } + + if (stats.empty()) { + return (false); + } + + r8veritas::index i = stats.size(); + std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl; + r8veritas::stat tmp_stat; + std::pair tmp_ans; + auto operated = false; + for (; i > 0; i--) { + tmp_stat = stats.front(); + for (auto j : r8veritas::all_ins) { + tmp_ans = operate_ins(tmp_stat, j); + if (tmp_ans.first == truthtable.second) { + return (true); + } + if (!(tmp_ans.second)) { + stats.push(tmp_ans.first); + } + } + stats.pop(); + } + return (bfs(truthtable, layer + 1)); +} + +r8veritas::path track_path(const r8veritas::truthtable &truthtable) { + r8veritas::path p; + for (auto tmp_stat = truthtable.second; tmp_stat != truthtable.first;) { + p.push(visited_op[tmp_stat]); + tmp_stat = p.top().first; + std::cout << tmp_stat << ";" << p.top().second << std::endl; + } + return p; +} \ No newline at end of file diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 689e3e2..75438e9 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -1,17 +1,29 @@ #include "r8veritas.hh" -#include #include +#include r8veritas::cell func_and(r8veritas::cell inp) { return ((((inp >> 0) & (inp >> 1)) & r8veritas::cell(0b1)) << 2) | (inp & 0b11); // inp[0]->arg1, inp[1]->arg2, inp[2]->ans } +r8veritas::cell func_copy(r8veritas::cell inp) { + return (((inp & 1) << 1) | (inp & 1)); // inp[0] -> from,inp[1]->to +} + +r8veritas::cell func_full_add(r8veritas::cell inp) { + bool A = inp & 0b1; + bool B = inp & 0b10; + bool C = inp & 0b100; + bool S = A ^ B ^ C; + bool Ci = (A & B) | (A & C) | (B & C); + return (Ci << 4 | S << 3) | (inp & 0b111); +} + int main() { auto tab = generate_truthtable(func_and); - for (auto i : r8veritas::all_ins) { - std::cout << std::bitset(i.bc).to_string() << "," << i.sacrifice - << ";"; - } + tab = compress_stat_space(tab); + std::cout << bfs(tab) << std::endl; + r8veritas::path p = track_path(tab); return (0); }