Files
R8VeritasCC/include/r8veritas.hh
T
2026-08-24 17:18:10 +08:00

333 lines
9.4 KiB
C++

#ifndef R8VERITAS
#define R8VERITAS
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iostream>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE))
#define INS_SIZE (CELL_SIZE + 1)
// mask and opcode
#define INS_BYTECODE_SIZE (INS_SIZE + CELL_SIZE_BITWIDTH)
// mask,opcode and sacrifice
template <int CELL_SIZE> class r8veritas {
public:
using cell = uint8_t;
using stat = std::array<r8veritas::cell, (1 << CELL_SIZE)>;
using stat_ref = const stat *;
using truthtable = std::pair<r8veritas::stat_ref, r8veritas::stat_ref>;
using op = std::function<r8veritas::cell(r8veritas::cell)>;
using index = size_t;
using influence = size_t;
using ins_body = size_t;
/*
e.g. cell_size=3:
0b aaa b
^--mask ^opcode
*/
using ins_bytecode = size_t;
enum ins_opcode {
SET1 = 0,
FLIP = 1,
};
struct ins {
r8veritas::ins_body bc;
r8veritas::index sacrifice;
operator const ins_bytecode() const {
return (bc << CELL_SIZE_BITWIDTH) | sacrifice;
}
};
using path =
std::stack<std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>>;
std::array<std::string, CELL_SIZE> nicknames;
std::array<std::string, CELL_SIZE> tp_paths;
private:
std::array<r8veritas::influence, CELL_SIZE> all_influences;
struct compare_ins {
bool operator()(const ins &a, const ins &b) const {
auto pop_a = std::popcount(a.bc >> 1);
auto pop_b = std::popcount(b.bc >> 1);
if (pop_a != pop_b)
return pop_a < pop_b;
if (a.bc != b.bc)
return a.bc < b.bc;
return a.sacrifice < b.sacrifice;
}
};
const std::set<r8veritas::ins, compare_ins> all_ins =
[]() -> std::set<r8veritas::ins, compare_ins> {
std::set<r8veritas::ins, compare_ins> s;
for (r8veritas::index sacrifice_it = 0; sacrifice_it < CELL_SIZE;
sacrifice_it++) {
for (r8veritas::index bc_it = 0; bc_it < (1 << INS_SIZE); bc_it++) {
auto tmp = bc_it & (~(1 << (sacrifice_it + 1)));
// sacrifice shouldn't be operated,
// which means that bit in bc's mask can't be 1.
// so here I forcefully change it to 0,
// and use set to ensure it won't duplicate.
// for here's the initialization part,it's acceptable
s.insert({((tmp == 0) ? 1 : tmp), sacrifice_it});
// both bc == 0b0 and bc == 0b1 mean erasing sacrifice,so here I skip
// 0b0
}
}
return s;
}();
std::unordered_map<r8veritas::stat_ref,
std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>>
visited_op;
struct stat_hash {
size_t operator()(const stat &s) const {
size_t h = 0;
for (auto i : s) {
h ^= std::hash<r8veritas::cell>{}(i) + 0x9e3779b9 + (h << 6) + (h >> 2);
// Magic,from Boost's hash_combine.
// An explanation from MCMC here: https://zhuanlan.zhihu.com/p/574573421
}
return h;
}
};
std::unordered_set<r8veritas::stat, stat_hash> stat_pool;
std::queue<r8veritas::stat_ref> stats;
public:
r8veritas::ins to_ins(const r8veritas::ins_bytecode &bc) {
return {bc >> CELL_SIZE_BITWIDTH, bc & ((1 << CELL_SIZE_BITWIDTH) - 1)};
}
void initialize_nicknames() {
for (r8veritas::index i = 0; i < CELL_SIZE; i++) {
nicknames[i] = "%" + std::to_string(i + 1);
}
return;
}
void initialize_tp_paths() {
for (r8veritas::index i = 0; i < CELL_SIZE; i++) {
tp_paths[i] = ".tp " + nicknames[i];
}
return;
}
r8veritas::stat_ref generate_empty_stat() {
r8veritas::stat arr;
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
arr[i] = 0;
}
auto tmp_ptr = stat_pool.insert(std::move(arr)).first;
return &(*tmp_ptr);
}
// WARNING:ST WILL BE DESTROYED
std::pair<r8veritas::stat_ref, bool> save_stat(r8veritas::stat &&st) {
auto t = stat_pool.insert(std::move(st));
return {&(*(t.first)), !t.second};
}
r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) {
r8veritas::stat inp = *generate_empty_stat();
r8veritas::stat out = *generate_empty_stat();
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
inp[i] = i;
out[i] = operation(r8veritas::cell(i));
}
return (r8veritas::truthtable(save_stat(std::move(inp)).first,
save_stat(std::move(out)).first));
}
std::pair<r8veritas::stat_ref,
bool> // true means the next stat has been visited.
operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins) {
auto key = std::pair(last_stat, r8veritas::ins_bytecode(ins));
r8veritas::stat next_stat = *last_stat;
int opcode = ins.bc & 1;
r8veritas::cell cell_mask = (ins.bc >> 1);
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
if ((next_stat[i] & (1 << ins.sacrifice)) != 0) {
next_stat[i] ^= (1 << ins.sacrifice);
if (opcode == ins_opcode::SET1) {
next_stat[i] |= cell_mask;
} else if (opcode == ins_opcode::FLIP) {
next_stat[i] ^= cell_mask;
}
}
}
auto tmp = save_stat(std::move(next_stat));
visited_op.insert(std::pair(tmp.first, std::move(key)));
return tmp;
}
void output_stat(const r8veritas::stat_ref &s) {
for (auto i : *s) {
std::cout << std::bitset<CELL_SIZE>(i).to_string() << " ";
}
}
void output_ins(const r8veritas::ins &s, bool enable_debug_output = true) {
auto str = std::bitset<INS_SIZE>(s.bc).to_string();
std::queue<r8veritas::index> arg_list;
bool just_sacrifice = ((s.bc == 0b0) || (s.bc == 0b1));
std::reverse(&str[0], &str[CELL_SIZE]);
/* e.g.
CELL_SIZE = 4
(0b)00101 -> 01001
^ ^str[CELL_SIZE]
*/
if (!just_sacrifice) {
for (r8veritas::index i = 0; i < str.length() - 1; i++) {
if (str[i] == '1') {
arg_list.push(i);
}
}
}
bool is_set1 = ((s.bc & 1) == SET1);
if (enable_debug_output) {
std::cout << "SACRIFICE: " << s.sacrifice;
if (!just_sacrifice) {
if (is_set1)
std::cout << ", SET1: {";
else
std::cout << ", FLIP: {";
while (!arg_list.empty()) {
std::cout << arg_list.front() << ',';
arg_list.pop();
}
std::cout << "\b}";
}
std::cout << ';';
/* e.g.
SACRIFICE: a, SET1: {b,c};
*/
} else {
std::cout << tp_paths[s.sacrifice] << std::endl;
std::cout << "biz" << std::endl;
if (!just_sacrifice) {
while (!arg_list.empty()) {
std::cout << '\t' << tp_paths[arg_list.front()] << std::endl;
if (is_set1)
std::cout << "\tset_1" << std::endl;
else
std::cout << "\tflb" << std::endl;
arg_list.pop();
}
std::cout << '\t' << tp_paths[s.sacrifice] << std::endl;
}
std::cout << "\tflb" << std::endl;
std::cout << "rnz" << std::endl;
/* e.g.
.tp a
biz
.tp b
set_1
.tp c
set_1
.tp a
flb
rnz
*/
}
}
r8veritas::truthtable
compress_stat_space(const r8veritas::truthtable &raw_truthtable) {
// raw means input must be 0 ~ (1<<CELL_SIZE),
// which makes reaching the output of after-xor input easier.
std::array<r8veritas::influence, CELL_SIZE> 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)]) {
// when [j] can influence the ans
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);
}
bool bfs(const r8veritas::truthtable &truthtable,
bool enable_debug_output = false, r8veritas::index layer = 1) {
if (layer == 1) {
stats.push(truthtable.first);
}
if (stats.empty()) {
return (false);
}
r8veritas::index i = stats.size();
if (enable_debug_output)
std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl;
r8veritas::stat_ref tmp_stat_ref;
std::pair<stat_ref, bool> ans;
for (; i > 0; i--) {
tmp_stat_ref = stats.front();
for (auto j : r8veritas::all_ins) {
ans = operate_ins(tmp_stat_ref, j);
if (ans.first == truthtable.second) {
while (!stats.empty())
stats.pop();
return (true);
}
if (!ans.second) {
stats.push(ans.first);
}
}
stats.pop();
}
return (bfs(truthtable, enable_debug_output, 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;
}
return p;
}
};
#undef CELL_SIZE_BITWIDTH
#undef INS_SIZE
#undef INS_BYTECODE_SIZE
#endif