From 3cd6a5e9bc8548bf4853d860e1a9351be81ce70c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 10 Jan 2023 13:37:49 +0800 Subject: [PATCH] feat: benchmark for moving 1x1 block --- src/core_demo.cc | 82 ++++++++++++++++++++++++++++++++++++++++++++++-- src/core_demo.h | 14 +++++++++ src/main.cc | 4 +-- 3 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/core_demo.cc b/src/core_demo.cc index 069c573..cc86fbb 100644 --- a/src/core_demo.cc +++ b/src/core_demo.cc @@ -1,9 +1,87 @@ #include #include "raw_code.h" +#include "core_demo.h" + +cache_t cache[16]; + +// TODO: use cache_t *move_cache_top? +int cache_size; + +inline bool cache_insert(uint64_t code) { + +// std::cout << "cache insert" << std::endl; + + auto *p = cache; + for (; p < cache + cache_size; ++p) { + if (*p == code) { + return false; // already exist -> insert failed + } + } + *p = code; +// p->code = code; +// p->filter = filter; + ++cache_size; + return true; +} + +/// input -> raw_code / block address +void move_1x1(uint64_t code, int addr) { + +// std::cout << "enter move_1x1 at " << addr << std::endl; + + int target_addr; + uint64_t ret_code; + + /// try to move up + if (addr >= 4 * 3 && !(code >> (target_addr = addr + UP) & F_1x1)) { + ret_code = code & ~(F_1x1 << addr) | C_1x1 << target_addr; + if (cache_insert(ret_code)) { + move_1x1(ret_code, target_addr); + } + } + + /// try to move down + if (addr <= 15 * 3 && !(code >> (target_addr = addr + DOWN) & F_1x1)) { + ret_code = code & ~(F_1x1 << addr) | C_1x1 << target_addr; + if (cache_insert(ret_code)) { + move_1x1(ret_code, target_addr); + } + } + + /// try to move left + if ((addr & 0b11) != 0 && !(code >> (target_addr = addr + LEFT) & F_1x1)) { + ret_code = code & ~(F_1x1 << addr) | C_1x1 << target_addr; + if (cache_insert(ret_code)) { + move_1x1(ret_code, target_addr); + } + } + + /// try to move right + if ((addr & 0x3) != 1 && !(code >> (target_addr = addr + RIGHT) & F_1x1)) { + ret_code = code & ~(F_1x1 << addr) | C_1x1 << target_addr; + if (cache_insert(ret_code)) { + move_1x1(ret_code, target_addr); + } + } +} void next_step(uint64_t raw_code, uint64_t mask) { - std::cout << RawCode(raw_code).dump_case(); - printf("mask -> %016lX\n", mask); +// std::cout << RawCode(raw_code).dump_case(); +// printf("mask -> %016lX\n", mask); + + // case 1 -> 8.8s + + for (int i = 0; i < 1000000000; ++i) { + cache_size = 1; + cache[0] = raw_code; + move_1x1(raw_code, 17 * 3); + } + +// std::cout << cache_size << std::endl; +// +// std::cout << RawCode(cache[0]).dump_case() << std::endl; +// std::cout << RawCode(cache[1]).dump_case() << std::endl; +// std::cout << RawCode(cache[2]).dump_case() << std::endl; } diff --git a/src/core_demo.h b/src/core_demo.h index 19ded5c..ae0f63c 100644 --- a/src/core_demo.h +++ b/src/core_demo.h @@ -2,4 +2,18 @@ #include +#define UP (-12) // -4 * 3-bits +#define LEFT (-3) // +1 * 3-bits +#define DOWN (+12) // +4 * 3-bits +#define DOWN_2 (+24) // +8 * 3-bits +#define RIGHT (+3) // +1 * 3-bits +#define RIGHT_2 (+6) // +2 * 3-bits + +//struct cache_t { +// uint64_t code; // raw code +// uint64_t mask; // only 000 or 111 +//}; + +typedef uint64_t cache_t; + void next_step(uint64_t raw_code, uint64_t mask); diff --git a/src/main.cc b/src/main.cc index e5e098c..89ac612 100644 --- a/src/main.cc +++ b/src/main.cc @@ -139,10 +139,10 @@ int main() { // auto r = RawCode(0x0E58FC85FFEBC4DD); // invalid raw code -> only for test // std::cout << r.dump_case() << std::endl; +// std::cout << CommonCode(RawCode(0x0E58FC85FFEBC4DB)).to_string() << std::endl; - next_step(0x0E58FC85FFEBC4DB, 0); // mask unset - std::cout << CommonCode(RawCode(0x0E58FC85FFEBC4DB)).to_string() << std::endl; + next_step(CommonCode("4FEA134").to_raw_code().unwrap(), 0); // mask unset return 0; }