Added debug output format.

This commit is contained in:
starcells committed 2026-07-29 17:51:18 +08:00
1 parent 09871417c9
commit eb8051206e
3 files changed
+86 -42

No files matched your search

+10 -5
View File
@@ -13,7 +13,7 @@
#include <unordered_set>
#include <utility>
#define CELL_SIZE 5
#define CELL_SIZE 3
#define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE))
#define INS_SIZE (CELL_SIZE + 1)
@@ -55,9 +55,9 @@ inline r8veritas::ins to_ins(const r8veritas::ins_bytecode &bc) {
using path =
std::stack<std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>>;
static std::array<r8veritas::influence, CELL_SIZE> all_influences;
inline std::array<r8veritas::influence, CELL_SIZE> all_influences;
static const std::set<r8veritas::ins> all_ins =
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;
@@ -76,25 +76,30 @@ static const std::set<r8veritas::ins> all_ins =
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;
}
};
// Overload hash to make some unordered_maps work.
template <> struct hash<pair<r8veritas::stat_ref, r8veritas::ins_bytecode>> {
// Yep,magic again.
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;
+66 -23
View File
@@ -8,6 +8,12 @@
#include <string>
#include <unordered_set>
#include <utility>
void initialize_nicknames() {
for (r8veritas::index i = 0; i < CELL_SIZE; i++) {
nicknames[i] = "%" + std::to_string(i + 1);
@@ -86,34 +92,70 @@ std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s) {
std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s) {
auto str = std::bitset<INS_SIZE>(s.bc).to_string();
os << tp_paths[s.sacrifice] << std::endl
<< "biz" << std::endl
<< "\tflb" << std::endl;
/*
.tp xxx
biz
flb
*/
std::queue<r8veritas::index> arg_list;
bool just_sacrifice = ((s.bc == 0b0) || (s.bc == 0b1));
std::reverse(&str[0], &str[CELL_SIZE]);
if ((s.bc != 0b0) && (s.bc != 0b1)) {
if (!just_sacrifice) {
for (r8veritas::index i = 0; i < str.length() - 1; i++) {
if (str[i] == '1') {
os << "\t" << tp_paths[i] << std::endl;
/*
.tp yyy
*/
if (str[CELL_SIZE] == '1')
os << "\tflb" << std::endl;
else
os << "\tset_1" << std::endl;
arg_list.push(i);
}
}
os << "\t" << tp_paths[s.sacrifice] << std::endl;
}
os << "rnz" << std::endl;
/*
rnz
*/
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);
}
@@ -152,7 +194,8 @@ bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) {
}
r8veritas::index i = stats.size();
// std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl;
if (r8veritas::debug_output)
std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl;
r8veritas::stat_ref tmp_stat_ref;
std::pair<r8veritas::stat_ref, bool> tmp_ans;
auto operated = false;
+10 -14
View File
@@ -29,11 +29,19 @@ r8veritas::cell func_full_add_S(r8veritas::cell inp) {
return (S << 3) | (inp & 0b111);
}
r8veritas::cell func_half_add(r8veritas::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool S = A ^ B;
bool Co = A & B;
return (S << 1) | Co;
}
int main() {
initialize_nicknames();
initialize_tp_paths();
auto tab1 = generate_truthtable(func_full_add_Co);
r8veritas::debug_output = true;
auto tab1 = generate_truthtable(func_half_add);
auto new_tab1 = compress_stat_space(tab1);
if (bfs(new_tab1)) {
r8veritas::path p = track_path(tab1);
@@ -42,17 +50,5 @@ int main() {
p.pop();
}
}
nicknames[4] = "bit_add_buf_1";
initialize_tp_paths();
auto tab2 = generate_truthtable(func_full_add_S);
auto new_tab2 = compress_stat_space(tab2);
if (bfs(new_tab2)) {
r8veritas::path p = track_path(tab2);
while (!p.empty()) {
std::cout << r8veritas::to_ins(p.top().second) << std::endl;
p.pop();
}
}
return (0);
}