[err]202607231240
This commit is contained in:
commit
2358c9a3e6
5 files changed
+190
No files matched your search
@@ -0,0 +1,4 @@
|
||||
build/
|
||||
.cache/
|
||||
.continue/
|
||||
.vscode/
|
||||
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(R8Veritas)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
include_directories(include)
|
||||
|
||||
add_library(r8veritas_core src/r8veritas.cc)
|
||||
|
||||
add_executable(r8veritas_test tests/r8_test.cc)
|
||||
target_link_libraries(r8veritas_test r8veritas_core)
|
||||
@@ -0,0 +1,97 @@
|
||||
#ifndef R8VERITAS
|
||||
#define R8VERITAS
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#define CELL_SIZE 5
|
||||
#define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE))
|
||||
|
||||
#define INS_SIZE (1 + CELL_SIZE)
|
||||
#define INS_BYTECODE_SIZE (1 + CELL_SIZE + CELL_SIZE_BITWIDTH)
|
||||
|
||||
namespace r8veritas {
|
||||
// using cell = std::bitset<CELL_SIZE>;
|
||||
using cell = uint16_t;
|
||||
using stat = std::array<r8veritas::cell, 1 << CELL_SIZE>;
|
||||
using truthtable = std::pair<r8veritas::stat, r8veritas::stat>;
|
||||
using op = std::function<r8veritas::cell(r8veritas::cell)>;
|
||||
using index = size_t;
|
||||
|
||||
enum ins_opcode {
|
||||
SET1 = 0,
|
||||
FLIP = 1,
|
||||
};
|
||||
|
||||
/*
|
||||
using ins_body = std::bitset<INS_SIZE>;
|
||||
using ins_bytecode = std::bitset<INS_BYTECODE_SIZE>;
|
||||
*/
|
||||
using ins_body = uint16_t;
|
||||
using ins_bytecode = uint16_t;
|
||||
using ins = struct ins {
|
||||
r8veritas::ins_body bc;
|
||||
r8veritas::index sacrifice;
|
||||
operator const ins_bytecode() const {
|
||||
return (bc << CELL_SIZE_BITWIDTH) | sacrifice;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace r8veritas
|
||||
|
||||
inline r8veritas::stat generate_empty_stat() {
|
||||
r8veritas::stat arr;
|
||||
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
|
||||
arr[i] = 0;
|
||||
}
|
||||
return (arr);
|
||||
}
|
||||
|
||||
namespace std {
|
||||
// Overload hash to make some unordered_maps work.
|
||||
template <> struct hash<r8veritas::stat> {
|
||||
// Magic,from Boost's hash_combine.
|
||||
// An explanation from MCMC here: https://zhuanlan.zhihu.com/p/574573421
|
||||
size_t operator()(const r8veritas::stat &p) const {
|
||||
size_t h = 0;
|
||||
for (auto iter : p) {
|
||||
h ^= hash<r8veritas::cell>{}(iter) + 0x9e3779b9 + (h << 6) + (h >> 2);
|
||||
}
|
||||
return (h);
|
||||
}
|
||||
};
|
||||
template <> struct hash<pair<r8veritas::stat, r8veritas::ins_bytecode>> {
|
||||
// Yep,magic again.
|
||||
size_t
|
||||
operator()(const pair<r8veritas::stat, r8veritas::ins_bytecode> &p) const {
|
||||
size_t h =
|
||||
hash<r8veritas::stat>{}(p.first) + 0x9e3779b9 + (h << 6) + (h >> 2);
|
||||
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_FLIP (static_cast<int>(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);
|
||||
|
||||
static std::unordered_map<std::pair<r8veritas::stat, r8veritas::ins_bytecode>,
|
||||
r8veritas::stat>
|
||||
visited_stat;
|
||||
|
||||
static std::unordered_map<r8veritas::stat,
|
||||
std::pair<r8veritas::stat, r8veritas::ins_bytecode>>
|
||||
visited_op;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "r8veritas.hh"
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
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(inp, out));
|
||||
}
|
||||
|
||||
r8veritas::stat 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 <last_stat,ins> in visited_stat
|
||||
// DEBUG
|
||||
std::cout
|
||||
<< "Visited:"
|
||||
<< std::hash<std::pair<r8veritas::stat, r8veritas::ins_bytecode>>{}(key)
|
||||
<< std::endl;
|
||||
return (it->second);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
visited_stat.insert_or_assign(key, next_stat);
|
||||
visited_op.insert_or_assign(next_stat, key);
|
||||
|
||||
return (next_stat);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include "r8veritas.hh"
|
||||
#include <bitset>
|
||||
#include <iostream>
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto tab = generate_truthtable(func_and);
|
||||
r8veritas::ins i = {0, 2};
|
||||
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
|
||||
if (!(i % 4))
|
||||
std::cout << std::endl;
|
||||
std::cout << std::bitset<CELL_SIZE>(tab.first[i]).to_string() << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
r8veritas::ins ins = {0b010, 2};
|
||||
|
||||
tab.second = operate_ins(tab.first, ins);
|
||||
tab.second = operate_ins(tab.first, ins);
|
||||
|
||||
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
|
||||
if (!(i % 4))
|
||||
std::cout << std::endl;
|
||||
std::cout << std::bitset<CELL_SIZE>(tab.second[i]).to_string() << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
return (0);
|
||||
}
|
||||
Reference in new issue
Block a user