Browse Source

feat: enable mask speed up

master
Dnomd343 2 years ago
parent
commit
fe521aea03
  1. 13
      src/core/core.cc
  2. 5
      src/core/core.h
  3. 33
      src/fast_cal.cc
  4. 45
      src/main.cc

13
src/core/core.cc

@ -126,10 +126,15 @@ void Core::move_2x2(uint64_t code, int addr) { // try to move target 2x2 block
}
}
void Core::next_step(uint64_t raw_code, void (*release)(uint64_t, uint64_t)) { // search next step cases
auto code = raw_code;
for (int addr = 0; code; addr += 3, code >>= 3) { // traverse every 3-bits
switch (code & 0b111) { // match low 3-bits
void Core::next_step(uint64_t raw_code, uint64_t mask) { // search next step cases
// auto code = raw_code;
// TODO: make BFS root aka cache[0] static
auto range = raw_code | mask;
for (int addr = 0; range; addr += 3, range >>= 3) { // traverse every 3-bits
switch (range & 0b111) { // match low 3-bits
case B_1x1:
move_1x1(raw_code, addr); // try to move 1x1 block
break;

5
src/core/core.h

@ -20,7 +20,9 @@
class Core {
public:
// void next_step(uint64_t raw_code);
void next_step(uint64_t raw_code, void (*release)(uint64_t code, uint64_t mask));
void next_step(uint64_t raw_code, uint64_t mask);
Core(void (*release_func)(uint64_t code, uint64_t mask)) : release(release_func) {}
private:
struct cache_t {
@ -32,6 +34,7 @@ private:
int cache_size;
cache_t cache[16];
void (*release)(uint64_t code, uint64_t mask);
void move_1x1(uint64_t code, int addr);
void move_1x2(uint64_t code, int addr);

33
src/fast_cal.cc

@ -16,17 +16,27 @@ std::queue<fast_cal_t*> cal_temp;
void add_new_case(uint64_t code, uint64_t mask) {
// std::cout << RawCode(code).dump_case();
// std::cout << "~~~~~~~" << std::endl;
// auto temp_mask = mask;
// for (int n = 0; n < 20; ++n, temp_mask >>= 3) {
// std::cout << "+."[!(temp_mask & 0b111)] << " \n"[!(~n & 0b11)];
// }
// std::cout << std::endl;
auto exist_case = cal_data.find(code);
if (exist_case != cal_data.end()) { // find it
// mask update
exist_case->second.mask |= mask; // mask update
return;
}
// std::cout << RawCode(code).dump_case();
// std::cout << "~~~~~~~" << std::endl;
// for (int n = 0; n < 20; ++n, mask >>= 3) {
// std::cout << "+."[!(mask & 0b111)] << " \n"[!(~n & 0b11)];
// auto temp_mask = mask;
// for (int n = 0; n < 20; ++n, temp_mask >>= 3) {
// std::cout << "+."[!(temp_mask & 0b111)] << " \n"[!(~n & 0b11)];
// }
// std::cout << std::endl;
@ -45,7 +55,7 @@ void add_new_case(uint64_t code, uint64_t mask) {
uint32_t fast_cal(uint64_t start_raw_code) {
// std::cout << RawCode(start_raw_code).dump_case() << std::endl;
auto core = Core();
auto core = Core(add_new_case);
cal_data.empty();
@ -58,10 +68,23 @@ uint32_t fast_cal(uint64_t start_raw_code) {
cal_temp.emplace(&cal_data[start_raw_code]);
while (!cal_temp.empty()) {
core.next_step(cal_temp.front()->code, add_new_case);
if (((cal_temp.front()->code >> (3 * 0xD)) & 0b111) == B_2x2) {
std::cout << "get resolve" << std::endl;
std::cout << RawCode(cal_temp.front()->code).dump_case() << std::endl;
break;
}
core.next_step(cal_temp.front()->code, cal_temp.front()->mask);
cal_temp.pop();
}
// core.next_step(cal_temp.front()->code, cal_temp.front()->mask);
// cal_temp.pop();
//
// core.next_step(cal_temp.front()->code, cal_temp.front()->mask);
// cal_temp.pop();
// std::cout << "size = " << cal_data.size() << std::endl;
// std::cout << "queue size = " << cal_temp.size() << std::endl;

45
src/main.cc

@ -160,17 +160,17 @@ int main() {
// auto raw_code = RawCode(CommonCode("1A9bf0c")).unwrap();
// c.next_step(raw_code, release);
std::vector<uint64_t> all_cases_raw;
for (int head = 0; head < 16; ++head) {
uint64_t prefix = (uint64_t)head << 32;
for (auto const &range : (*AllCases::fetch())[head]) {
all_cases_raw.emplace_back(
RawCode(CommonCode::unsafe_create(prefix | range)).unwrap()
);
}
}
std::cout << "start benchmark" << std::endl;
// std::vector<uint64_t> all_cases_raw;
// for (int head = 0; head < 16; ++head) {
// uint64_t prefix = (uint64_t)head << 32;
// for (auto const &range : (*AllCases::fetch())[head]) {
// all_cases_raw.emplace_back(
// RawCode(CommonCode::unsafe_create(prefix | range)).unwrap()
// );
// }
// }
// std::cout << "start benchmark" << std::endl;
auto start_time = clock();
// auto raw_code = RawCode(CommonCode("4fea134")).unwrap();
@ -193,23 +193,24 @@ int main() {
// }
// }
// auto raw_code = CommonCode("1a9bf0c").to_raw_code().unwrap();
auto raw_code = CommonCode("1a9bf0c").to_raw_code().unwrap();
std::cout << fast_cal(raw_code) << std::endl;
int sum = 0;
for (auto const &raw_code : all_cases_raw) {
// int sum = 0;
// for (auto const &raw_code : all_cases_raw) {
// std::cout << RawCode(raw_code).dump_case();
fast_cal(raw_code);
// fast_cal(raw_code);
// std::cout << fast_cal(raw_code) << std::endl;
// std::cout << std::endl;
if (sum % 100000 == 0) {
std::cout << "sum = " << sum << std::endl;
}
++sum;
// if (sum % 100000 == 0) {
// std::cout << "sum = " << sum << std::endl;
// }
// ++sum;
}
// }
std::cout << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;
std::cout << "complete benchmark" << std::endl;
std::cout << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl;
// std::cout << "complete benchmark" << std::endl;
return 0;
}

Loading…
Cancel
Save