[err]202608141801

This commit is contained in:
starcells committed 2026-08-14 18:01:06 +08:00
1 parent 9346a5ad89
commit 03889e7874
3 files changed
+300 -331

No files matched your search

+289 -108
View File
@@ -1,12 +1,16 @@
#ifndef R8VERITAS #ifndef R8VERITAS
#define R8VERITAS #define R8VERITAS
#include <algorithm>
#include <array> #include <array>
#include <bit> #include <bit>
#include <bitset>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <iostream>
#include <ostream> #include <ostream>
#include <queue>
#include <set> #include <set>
#include <stack> #include <stack>
#include <string> #include <string>
@@ -14,7 +18,6 @@
#include <unordered_set> #include <unordered_set>
#include <utility> #include <utility>
#define CELL_SIZE 5
#define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE)) #define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE))
#define INS_SIZE (CELL_SIZE + 1) #define INS_SIZE (CELL_SIZE + 1)
@@ -22,119 +25,297 @@
#define INS_BYTECODE_SIZE (INS_SIZE + CELL_SIZE_BITWIDTH) #define INS_BYTECODE_SIZE (INS_SIZE + CELL_SIZE_BITWIDTH)
// mask,opcode and sacrifice // mask,opcode and sacrifice
namespace r8veritas {
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;
using ins_bytecode = size_t;
/*
using ins_body = std::bitset<INS_SIZE>;
using ins_bytecode = std::bitset<INS_BYTECODE_SIZE>;
*/
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;
}
};
inline r8veritas::ins to_ins(const r8veritas::ins_bytecode &bc) {
return {bc >> CELL_SIZE_BITWIDTH, bc & ((1 << CELL_SIZE_BITWIDTH) - 1)};
}
using path =
std::stack<std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>>;
inline std::array<r8veritas::influence, CELL_SIZE> all_influences;
inline const std::set<r8veritas::ins> all_ins =
[]() -> std::set<r8veritas::ins> {
std::set<r8veritas::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;
}();
inline bool debug_output = false;
} // namespace r8veritas
namespace std {
// Overload hash to make some unordered_maps work.
template <> struct hash<r8veritas::stat> {
size_t operator()(const r8veritas::stat &s) const {
size_t h = 0;
for (auto i : s) {
h ^= 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;
}
};
template <> struct hash<pair<r8veritas::stat_ref, r8veritas::ins_bytecode>> {
size_t operator()(
const pair<r8veritas::stat_ref, r8veritas::ins_bytecode> &p) const {
size_t h = hash<r8veritas::stat_ref>{}(p.first) + 0x9e3779b9;
// Yep,magic again.
h ^= hash<r8veritas::ins_bytecode>{}(p.second) + 0x9e3779b9 + (h << 6) +
(h >> 2);
return h;
}
};
} // namespace std
#define OPCODE_SET1 (static_cast<int>(r8veritas::ins_opcode::SET1)) #define OPCODE_SET1 (static_cast<int>(r8veritas::ins_opcode::SET1))
#define OPCODE_FLIP (static_cast<int>(r8veritas::ins_opcode::FLIP)) #define OPCODE_FLIP (static_cast<int>(r8veritas::ins_opcode::FLIP))
r8veritas::truthtable generate_truthtable(const r8veritas::op &operation); template <int CELL_SIZE> class r8veritas {
r8veritas::stat_ref operate_ins(const r8veritas::stat_ref &last_stat, public:
const r8veritas::ins &ins); using cell = uint8_t;
inline std::unordered_map< using stat = std::array<r8veritas::cell, (1 << CELL_SIZE)>;
r8veritas::stat_ref, using stat_ref = const stat *;
std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>> using truthtable = std::pair<r8veritas::stat_ref, r8veritas::stat_ref>;
visited_op; using op = std::function<r8veritas::cell(r8veritas::cell)>;
using index = size_t;
using influence = size_t;
using ins_body = size_t;
using ins_bytecode = size_t;
enum ins_opcode {
SET1 = 0,
FLIP = 1,
};
inline std::unordered_set<r8veritas::stat> stat_pool; struct ins {
inline std::array<std::string, CELL_SIZE> nicknames; r8veritas::ins_body bc;
inline std::array<std::string, CELL_SIZE> tp_paths; 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::ostream &operator<<(std::ostream &os, const r8veritas::stat &s); private:
std::array<r8veritas::influence, CELL_SIZE> all_influences;
r8veritas::truthtable const std::set<r8veritas::ins> all_ins = []() -> std::set<r8veritas::ins> {
compress_stat_space(const r8veritas::truthtable &raw_truthtable); std::set<r8veritas::ins> s;
// raw means input must be 0 ~ (1<<CELL_SIZE), for (r8veritas::index sacrifice_it = 0; sacrifice_it < CELL_SIZE;
// which makes reaching the output of after-xor input easier. 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;
}();
bool debug_output = false;
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::array<std::string, CELL_SIZE> nicknames;
std::array<std::string, CELL_SIZE> tp_paths;
std::queue<r8veritas::stat_ref> stats;
std::unordered_set<r8veritas::stat_ref> remembered_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
r8veritas::stat_ref save_stat(r8veritas::stat &&st) {
return &(*stat_pool.insert(std::move(st)).first);
}
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)),
save_stat(std::move(out))));
}
r8veritas::stat_ref 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 == OPCODE_SET1) {
next_stat[i] |= cell_mask;
} else if (opcode == OPCODE_FLIP) {
next_stat[i] ^= cell_mask;
}
}
}
r8veritas::stat_ref tmp_next_stat_ref = save_stat(std::move(next_stat));
visited_op.insert(std::pair(tmp_next_stat_ref, std::move(key)));
return tmp_next_stat_ref;
}
void output_stat(std::ostream &os, const r8veritas::stat_ref &s) {
for (auto i : *s) {
os << std::bitset<CELL_SIZE>(i).to_string() << " ";
}
}
void output_ins(std::ostream &os, const r8veritas::ins &s) {
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 = (str[CELL_SIZE] == '1');
if (r8veritas::debug_output) {
os << "SACRIFICE: " << s.sacrifice;
if (!just_sacrifice) {
if (is_set1)
os << ", SET1: {";
else
os << ", FLIP: {";
while (!arg_list.empty()) {
os << arg_list.front() << ',';
arg_list.pop();
}
os << "\b}";
}
os << ';';
/* e.g.
SACRIFICE: a, SET1: {b,c};
*/
} else {
os << tp_paths[s.sacrifice] << std::endl;
os << "biz" << std::endl;
if (!just_sacrifice) {
while (!arg_list.empty()) {
os << '\t' << tp_paths[arg_list.front()] << std::endl;
if (is_set1)
os << "\tset_1" << std::endl;
else
os << "\tflb" << std::endl;
arg_list.pop();
}
os << '\t' << tp_paths[s.sacrifice] << std::endl;
}
os << "\tflb" << std::endl;
os << "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});
}
}
return (new_truthtable);
}
bool bfs(const r8veritas::truthtable &truthtable,
r8veritas::index layer = 1) {
if (layer == 1) {
stats.push(truthtable.first);
}
if (stats.empty()) {
return (false);
}
r8veritas::index i = stats.size();
if (r8veritas::debug_output)
std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl;
r8veritas::stat_ref tmp_stat_ref;
r8veritas::stat_ref tmp_ans;
auto operated = false;
for (; i > 0; i--) {
tmp_stat_ref = stats.front();
for (auto j : r8veritas::all_ins) {
tmp_ans = operate_ins(tmp_stat_ref, j);
if (tmp_ans == truthtable.second) {
while (!stats.empty())
stats.pop();
remembered_stats.clear();
return (true);
}
if (!remembered_stats.contains(tmp_ans)) {
stats.push(tmp_ans);
remembered_stats.insert(tmp_ans);
}
}
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;
}
return p;
}
void enable_debug_output() { debug_output = true; }
void disable_debug_output() { debug_output = false; }
};
bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer = 1);
r8veritas::path track_path(const r8veritas::truthtable &truthtable);
std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s);
std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s);
void initialize_nicknames();
void initialize_tp_paths();
#endif #endif
-215
View File
@@ -8,218 +8,3 @@
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
#include <utility> #include <utility>
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
r8veritas::stat_ref save_stat(r8veritas::stat &&st) {
return &(*stat_pool.insert(std::move(st)).first);
}
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)),
save_stat(std::move(out))));
}
r8veritas::stat_ref 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 == OPCODE_SET1) {
next_stat[i] |= cell_mask;
} else if (opcode == OPCODE_FLIP) {
next_stat[i] ^= cell_mask;
}
}
}
r8veritas::stat_ref tmp_next_stat_ref = save_stat(std::move(next_stat));
visited_op.insert(std::pair(tmp_next_stat_ref, std::move(key)));
return tmp_next_stat_ref;
}
std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s) {
for (auto i : *s) {
os << std::bitset<CELL_SIZE>(i).to_string() << " ";
}
return (os);
}
std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s) {
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 = (str[CELL_SIZE] == '1');
if (r8veritas::debug_output) {
os << "SACRIFICE: " << s.sacrifice;
if (!just_sacrifice) {
if (is_set1)
os << ", SET1: {";
else
os << ", FLIP: {";
while (!arg_list.empty()) {
os << arg_list.front() << ',';
arg_list.pop();
}
os << "\b}";
}
os << ';';
/* e.g.
SACRIFICE: a, SET1: {b,c};
*/
} else {
os << tp_paths[s.sacrifice] << std::endl;
os << "biz" << std::endl;
if (!just_sacrifice) {
while (!arg_list.empty()) {
os << '\t' << tp_paths[arg_list.front()] << std::endl;
if (is_set1)
os << "\tset_1" << std::endl;
else
os << "\tflb" << std::endl;
arg_list.pop();
}
os << '\t' << tp_paths[s.sacrifice] << std::endl;
}
os << "\tflb" << std::endl;
os << "rnz" << std::endl;
/* e.g.
.tp a
biz
.tp b
set_1
.tp c
set_1
.tp a
flb
rnz
*/
}
return (os);
}
r8veritas::truthtable
compress_stat_space(const r8veritas::truthtable &raw_truthtable) {
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});
}
}
return (new_truthtable);
}
static std::queue<r8veritas::stat_ref> stats;
static std::unordered_set<r8veritas::stat_ref> remembered_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();
if (r8veritas::debug_output)
std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl;
r8veritas::stat_ref tmp_stat_ref;
r8veritas::stat_ref tmp_ans;
auto operated = false;
for (; i > 0; i--) {
tmp_stat_ref = stats.front();
for (auto j : r8veritas::all_ins) {
tmp_ans = operate_ins(tmp_stat_ref, j);
if (tmp_ans == truthtable.second) {
while (!stats.empty())
stats.pop();
remembered_stats.clear();
return (true);
}
if (!remembered_stats.contains(tmp_ans)) {
stats.push(tmp_ans);
remembered_stats.insert(tmp_ans);
}
}
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;
}
return p;
}
+11 -8
View File
@@ -2,6 +2,8 @@
#include <iostream> #include <iostream>
#include <ostream> #include <ostream>
#define r8veritas r8veritas<6>
r8veritas::cell func_copy(r8veritas::cell inp) { r8veritas::cell func_copy(r8veritas::cell inp) {
bool A = inp & 0b1; bool A = inp & 0b1;
return (A << 1) | A; return (A << 1) | A;
@@ -49,15 +51,16 @@ r8veritas::cell func_half_add(r8veritas::cell inp) {
} }
int main() { int main() {
initialize_nicknames(); static r8veritas problem;
initialize_tp_paths(); problem.initialize_nicknames();
r8veritas::debug_output = true; problem.initialize_tp_paths();
auto tab1 = generate_truthtable(func_full_add_Co); problem.disable_debug_output();
auto new_tab1 = compress_stat_space(tab1); auto tab1 = problem.generate_truthtable(func_full_add_Co);
if (bfs(new_tab1)) { auto new_tab1 = problem.compress_stat_space(tab1);
r8veritas::path p = track_path(tab1); if (problem.bfs(new_tab1)) {
auto p = problem.track_path(tab1);
while (!p.empty()) { while (!p.empty()) {
std::cout << r8veritas::to_ins(p.top().second) << std::endl; std::cout << problem.to_ins(p.top().second) << std::endl;
p.pop(); p.pop();
} }
} else } else