commit 2358c9a3e62b99137245fe7a22dd955516abef0b Author: StarCells Date: Thu Jul 23 12:40:40 2026 +0800 [err]202607231240 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..109e128 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build/ +.cache/ +.continue/ +.vscode/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..95f3945 --- /dev/null +++ b/CMakeLists.txt @@ -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) diff --git a/include/r8veritas.hh b/include/r8veritas.hh new file mode 100644 index 0000000..2a8eb19 --- /dev/null +++ b/include/r8veritas.hh @@ -0,0 +1,97 @@ +#ifndef R8VERITAS +#define R8VERITAS + +#include +#include +#include +#include +#include +#include +#include + +#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; +using cell = uint16_t; +using stat = std::array; +using truthtable = std::pair; +using op = std::function; +using index = size_t; + +enum ins_opcode { + SET1 = 0, + FLIP = 1, +}; + +/* +using ins_body = std::bitset; +using ins_bytecode = std::bitset; +*/ +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 { + // 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{}(iter) + 0x9e3779b9 + (h << 6) + (h >> 2); + } + return (h); + } +}; +template <> struct hash> { + // Yep,magic again. + size_t + operator()(const pair &p) const { + size_t h = + hash{}(p.first) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= hash{}(p.second) + 0x9e3779b9 + (h << 6) + + (h >> 2); + return h; + } +}; + +} // namespace std + +#define OPCODE_SET1 (static_cast(r8veritas::ins_opcode::SET1)) +#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); + +static std::unordered_map, + r8veritas::stat> + visited_stat; + +static std::unordered_map> + visited_op; + +#endif \ No newline at end of file diff --git a/src/r8veritas.cc b/src/r8veritas.cc new file mode 100644 index 0000000..7722199 --- /dev/null +++ b/src/r8veritas.cc @@ -0,0 +1,47 @@ +#include "r8veritas.hh" +#include +#include +#include + +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 in visited_stat + // DEBUG + std::cout + << "Visited:" + << std::hash>{}(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); +} diff --git a/tests/r8_test.cc b/tests/r8_test.cc new file mode 100644 index 0000000..7ad2cb7 --- /dev/null +++ b/tests/r8_test.cc @@ -0,0 +1,31 @@ +#include "r8veritas.hh" +#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 +} + +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(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(tab.second[i]).to_string() << " "; + } + std::cout << std::endl; + return (0); +}