Browse Source

update: enhance klotski core

master
Dnomd343 2 years ago
parent
commit
533247b881
  1. 20
      src/core/core.cc
  2. 2
      src/core/core.h
  3. 10
      src/main.cc

20
src/core/core.cc

@ -1,33 +1,40 @@
#include "core.h"
#include "common.h"
/// block direction limit
#define ALLOW_UP (filter != -UP)
#define ALLOW_DOWN (filter != -DOWN)
#define ALLOW_LEFT (filter != -LEFT)
#define ALLOW_RIGHT (filter != -RIGHT)
#define NOT_COLUMN_0 ((addr & 3) != 0)
#define NOT_COLUMN_2 ((addr & 3) != 2)
#define NOT_COLUMN_3 ((addr & 3) != 1)
/// horizontal restraints
#define NOT_COLUMN_0 ((addr & 3) != 0b00)
#define NOT_COLUMN_2 ((addr & 3) != 0b10)
#define NOT_COLUMN_3 ((addr & 3) != 0b01)
/// try to move block
#define MOVE_UP (next_addr = addr + UP)
#define MOVE_DOWN (next_addr = addr + DOWN)
#define MOVE_LEFT (next_addr = addr + LEFT)
#define MOVE_RIGHT (next_addr = addr + RIGHT)
/// vertical restraints
#define TOP_LIMIT(ADDR) (addr >= ADDR * 3)
#define BOTTOM_LIMIT(ADDR) (addr <= ADDR * 3)
/// check if the block can move
#define CHECK_UP(MASK) !(code >> MOVE_UP & MASK)
#define CHECK_DOWN(MASK) !(code >> MOVE_DOWN & MASK)
#define CHECK_LEFT(MASK) !(code >> MOVE_LEFT & MASK)
#define CHECK_RIGHT(MASK) !(code >> MOVE_RIGHT & MASK)
/// release next code
#define RELEASE_1x1(FILTER) RELEASE(NEXT_CODE_1x1, FILTER)
#define RELEASE_1x2(FILTER) RELEASE(NEXT_CODE_1x2, FILTER)
#define RELEASE_2x1(FILTER) RELEASE(NEXT_CODE_2x1, FILTER)
#define RELEASE_2x2(FILTER) RELEASE(NEXT_CODE_2x2, FILTER)
/// calculate next code
#define NEXT_CODE_1x1 ((code & ~(F_1x1 << addr)) | (C_1x1 << next_addr))
#define NEXT_CODE_1x2 ((code & ~(F_1x2 << addr)) | (C_1x2 << next_addr))
#define NEXT_CODE_2x1 ((code & ~(F_2x1 << addr)) | (C_2x1 << next_addr))
@ -36,13 +43,12 @@
////////////////////////////////////////
#define RELEASE(NEXT_CODE, FILTER) \
cache_t next_case = { \
cache_insert(cache_t { \
.code = NEXT_CODE, \
.mask = F_1x1 << next_addr, \
.filter = FILTER, \
.addr = next_addr \
}; \
cache_insert(next_case);
});
////////////////////////////////////////
@ -60,7 +66,7 @@ int filter = cache[current++].filter;
////////////////////////////////////////
inline void Core::cache_insert(Core::cache_t &next_case) { // try to insert into cache
inline void Core::cache_insert(Core::cache_t next_case) { // try to insert into cache
auto *cache_ptr = cache;
for (; cache_ptr < cache + cache_size; ++cache_ptr) {
if (cache_ptr->code == next_case.code) {

2
src/core/core.h

@ -34,5 +34,5 @@ private:
void move_1x2(uint64_t code, int addr);
void move_2x1(uint64_t code, int addr);
void move_2x2(uint64_t code, int addr);
inline void cache_insert(Core::cache_t &next_case);
inline void cache_insert(Core::cache_t next_case);
};

10
src/main.cc

@ -23,12 +23,10 @@ int main() {
// std::cout << "start benchmark" << std::endl;
auto start_time = clock();
for (uint32_t common_code = 0x10000000; common_code < 0x20000000; ++common_code) {
CommonCode::check(common_code);
}
// AllCases::build();
auto f = FastCal();
f.fast_cal(RawCode::from_common_code("1a9bf0c").unwrap());
// auto raw_code = CommonCode("1a9bf0c").to_raw_code().unwrap();
// auto raw_code = CommonCode("A5D3AF0").to_raw_code().unwrap();
@ -86,8 +84,8 @@ int main() {
// std::cerr << (clock() - start_time) / CLOCKS_PER_SEC << "s" << std::endl;
std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;
// std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl;
// std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;
std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl;
// std::cout << "complete benchmark" << std::endl;

Loading…
Cancel
Save