Browse Source

feat: try to speed up basic ranges build

master
Dnomd343 2 years ago
parent
commit
2fa1fc3ca1
  1. 3
      src/CMakeLists.txt
  2. 20
      src/all_cases/basic_ranges.cc
  3. 355
      src/basic_ranges_demo.cc
  4. 3
      src/basic_ranges_demo.h
  5. 30
      src/main.cc

3
src/CMakeLists.txt

@ -28,7 +28,8 @@ add_subdirectory(analyse)
################################
add_executable(klotski main.cc)
#add_executable(klotski main.cc)
add_executable(klotski main.cc basic_ranges_demo.cc)
################################

20
src/all_cases/basic_ranges.cc

@ -45,6 +45,8 @@ void BasicRanges::build() { // ensure that basic ranges available
}
}
#include <iostream>
void BasicRanges::build_data() { // build basic ranges
BasicRanges::data.reserve(BASIC_RANGES_SIZE); // memory pre-allocated
for (int n = 0; n <= 7; ++n) { // number of 1x2 and 2x1 block -> 0 ~ 7
@ -52,8 +54,26 @@ void BasicRanges::build_data() { // build basic ranges
for (int n_1x1 = 0; n_1x1 <= (14 - n * 2); ++n_1x1) { // number of 1x1 block -> 0 ~ (14 - 2n)
int n_1x2 = n - n_2x1;
int n_space = 16 - n * 2 - n_1x1;
// if (n_space == 7 && n_1x2 == 2 && n_2x1 == 1 && n_1x1 == 3) {
// std::cout << "Start: " << data.size() << std::endl;
// }
if (n_space == 6 && n_1x2 == 2 && n_2x1 == 1 && n_1x1 == 4) {
std::cout << "Start: " << data.size() << std::endl;
}
/// (7, 2, 1, 3) -> 102960
/// (6, 2, 1, 4) -> 180180
generate(n_space, n_1x2, n_2x1, n_1x1); // generate target ranges
/// 0x0 -> 00 | 1x2 -> 01 | 2x1 -> 10 | 1x1 -> 11
// if (n_space == 7 && n_1x2 == 2 && n_2x1 == 1 && n_1x1 == 3) {
// std::cout << "End: " << data.size() << std::endl;
// }
if (n_space == 6 && n_1x2 == 2 && n_2x1 == 1 && n_1x1 == 4) {
std::cout << "End: " << data.size() << std::endl;
}
}
}
}

355
src/basic_ranges_demo.cc

@ -0,0 +1,355 @@
#include "basic_ranges_demo.h"
#include <iostream>
struct br_t {
int n1;
int n2;
int n3;
int n4;
};
void show_br_t(br_t &dat) {
printf("(%d, %d, %d, %d)", dat.n1, dat.n2, dat.n3, dat.n4);
}
br_t dat[] = {
// {2, 0, 0, 1},
// {1, 0, 2, 0},
// {7, 2, 1, 3},
// {6, 2, 1, 4},
// {5, 2, 1, 5},
// {4, 2, 1, 6},
{16, 0, 0, 0},
{15, 0, 0, 1},
{14, 0, 0, 2},
{13, 0, 0, 3},
{12, 0, 0, 4},
{11, 0, 0, 5},
{10, 0, 0, 6},
{9, 0, 0, 7},
{8, 0, 0, 8},
{7, 0, 0, 9},
{6, 0, 0, 10},
{5, 0, 0, 11},
{4, 0, 0, 12},
{3, 0, 0, 13},
{2, 0, 0, 14},
{14, 1, 0, 0},
{13, 1, 0, 1},
{12, 1, 0, 2},
{11, 1, 0, 3},
{10, 1, 0, 4},
{9, 1, 0, 5},
{8, 1, 0, 6},
{7, 1, 0, 7},
{6, 1, 0, 8},
{5, 1, 0, 9},
{4, 1, 0, 10},
{3, 1, 0, 11},
{2, 1, 0, 12},
{14, 0, 1, 0},
{13, 0, 1, 1},
{12, 0, 1, 2},
{11, 0, 1, 3},
{10, 0, 1, 4},
{9, 0, 1, 5},
{8, 0, 1, 6},
{7, 0, 1, 7},
{6, 0, 1, 8},
{5, 0, 1, 9},
{4, 0, 1, 10},
{3, 0, 1, 11},
{2, 0, 1, 12},
{12, 2, 0, 0},
{11, 2, 0, 1},
{10, 2, 0, 2},
{9, 2, 0, 3},
{8, 2, 0, 4},
{7, 2, 0, 5},
{6, 2, 0, 6},
{5, 2, 0, 7},
{4, 2, 0, 8},
{3, 2, 0, 9},
{2, 2, 0, 10},
{12, 1, 1, 0},
{11, 1, 1, 1},
{10, 1, 1, 2},
{9, 1, 1, 3},
{8, 1, 1, 4},
{7, 1, 1, 5},
{6, 1, 1, 6},
{5, 1, 1, 7},
{4, 1, 1, 8},
{3, 1, 1, 9},
{2, 1, 1, 10},
{12, 0, 2, 0},
{11, 0, 2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, 4, 5},
{2, 0, 4, 6},
{6, 5, 0, 0},
{5, 5, 0, 1},
{4, 5, 0, 2},
{3, 5, 0, 3},
{2, 5, 0, 4},
{6, 4, 1, 0},
{5, 4, 1, 1},
{4, 4, 1, 2},
{3, 4, 1, 3},
{2, 4, 1, 4},
{6, 3, 2, 0},
{5, 3, 2, 1},
{4, 3, 2, 2},
{3, 3, 2, 3},
{2, 3, 2, 4},
{6, 2, 3, 0},
{5, 2, 3, 1},
{4, 2, 3, 2},
{3, 2, 3, 3},
{2, 2, 3, 4},
{6, 1, 4, 0},
{5, 1, 4, 1},
{4, 1, 4, 2},
{3, 1, 4, 3},
{2, 1, 4, 4},
{6, 0, 5, 0},
{5, 0, 5, 1},
{4, 0, 5, 2},
{3, 0, 5, 3},
{2, 0, 5, 4},
{4, 6, 0, 0},
{3, 6, 0, 1},
{2, 6, 0, 2},
{4, 5, 1, 0},
{3, 5, 1, 1},
{2, 5, 1, 2},
{4, 4, 2, 0},
{3, 4, 2, 1},
{2, 4, 2, 2},
{4, 3, 3, 0},
{3, 3, 3, 1},
{2, 3, 3, 2},
{4, 2, 4, 0},
{3, 2, 4, 1},
{2, 2, 4, 2},
{4, 1, 5, 0},
{3, 1, 5, 1},
{2, 1, 5, 2},
{4, 0, 6, 0},
{3, 0, 6, 1},
{2, 0, 6, 2},
{2, 7, 0, 0},
{2, 6, 1, 0},
{2, 5, 2, 0},
{2, 4, 3, 0},
{2, 3, 4, 0},
{2, 2, 5, 0},
{2, 1, 6, 0},
{2, 0, 7, 0},
};
//int dat_size = 4;
int dat_size = 204;
//int dat_size = 1;
//void func(uint32_t prefix, int offset, br_t &r) {
void func(uint32_t prefix, int offset) {
// std::cout << "build: ";
// show_br_t(dat[0]);
// std::cout << " ";
// show_br_t(dat[1]);
// printf(" | prefix = %08X | offset = %d\n", prefix, offset);
// if (dat[0].n1 == 0 && dat[0].n2 == 0 && dat[0].n3 == 0 && dat[0].n4 == 0) {
// printf("release -> %08X\n", prefix);
// return;
// }
// if (dat[1].n1 == 0 && dat[1].n2 == 0 && dat[1].n3 == 0 && dat[1].n4 == 0) {
// printf("release -> %08X\n", prefix);
// return;
// }
for (int i = 0; i < dat_size; ++i) {
if (dat[i].n1 == 0 && dat[i].n2 == 0 && dat[i].n3 == 0 && dat[i].n4 == 0) {
printf("release -> %08X\n", prefix);
return;
}
}
// if ((dat[0].n1 < 0 || dat[0].n2 < 0 || dat[0].n3 < 0 || dat[0].n4 < 0) && (dat[1].n1 < 0 || dat[1].n2 < 0 || dat[1].n3 < 0 || dat[1].n4 < 0)) {
// std::cout << "break" << std::endl;
// return;
// }
bool flag = false;
for (int i = 0; i < dat_size; ++i) {
if (!(dat[i].n1 < 0 || dat[i].n2 < 0 || dat[i].n3 < 0 || dat[i].n4 < 0)) {
flag = true;
}
}
if (!flag) {
return;
}
// generate start with 00
// --dat[0].n1;
// --dat[1].n1;
for (int i = 0; i < dat_size; ++i) {
--dat[i].n1;
}
func(prefix | ((uint32_t)0b00 << 30) >> offset, offset + 2);
// ++dat[0].n1;
// ++dat[1].n1;
for (int i = 0; i < dat_size; ++i) {
++dat[i].n1;
}
// generate start with 01
// --dat[0].n2;
// --dat[1].n2;
for (int i = 0; i < dat_size; ++i) {
--dat[i].n2;
}
func(prefix | ((uint32_t)0b01 << 30) >> offset, offset + 2);
// ++dat[0].n2;
// ++dat[1].n2;
for (int i = 0; i < dat_size; ++i) {
++dat[i].n2;
}
// generate start with 10
// --dat[0].n3;
// --dat[1].n3;
for (int i = 0; i < dat_size; ++i) {
--dat[i].n3;
}
func(prefix | ((uint32_t)0b10 << 30) >> offset, offset + 2);
// ++dat[0].n3;
// ++dat[1].n3;
for (int i = 0; i < dat_size; ++i) {
++dat[i].n3;
}
// generate start with 11
// --dat[0].n4;
// --dat[1].n4;
for (int i = 0; i < dat_size; ++i) {
--dat[i].n4;
}
func(prefix | ((uint32_t)0b11 << 30) >> offset, offset + 2);
// ++dat[0].n4;
// ++dat[1].n4;
for (int i = 0; i < dat_size; ++i) {
++dat[i].n4;
}
}
void load_ranges() {
// std::cout << "ok" << std::endl;
// for (int n = 0; n <= 7; ++n) { // number of 1x2 and 2x1 block -> 0 ~ 7
// for (int n_2x1 = 0; n_2x1 <= n; ++n_2x1) { // number of 2x1 block -> 0 ~ n
// for (int n_1x1 = 0; n_1x1 <= (14 - n * 2); ++n_1x1) { // number of 1x1 block -> 0 ~ (14 - 2n)
// int n_1x2 = n - n_2x1;
// int n_space = 16 - n * 2 - n_1x1;
// printf("(%d, %d, %d, %d)\n", n_space, n_1x2, n_2x1, n_1x1);
// printf("{%d, %d, %d, %d},\n", n_space, n_1x2, n_2x1, n_1x1);
/// 0x0 -> 00 | 1x2 -> 01 | 2x1 -> 10 | 1x1 -> 11
// }
// }
// }
func(0, 0);
// br_t demo = {2, 0, 0, 1};
// func(0, 0, demo);
// show_br_t(r);
}

3
src/basic_ranges_demo.h

@ -0,0 +1,3 @@
#pragma once
void load_ranges();

30
src/main.cc

@ -9,6 +9,7 @@
#include "analyse.h"
//#include "core_demo.h"
#include "basic_ranges_demo.h"
#include <thread>
#include <algorithm>
@ -196,13 +197,13 @@ int main() {
// }
// auto raw_code = CommonCode("1a9bf0c").to_raw_code().unwrap();
auto raw_code = CommonCode("A5D3AF0").to_raw_code().unwrap();
// auto raw_code = CommonCode("A5D3AF0").to_raw_code().unwrap();
// std::cout << fast_cal(raw_code) << std::endl;
// auto f = FastCal();
// f.fast_cal(raw_code);
auto a = Analyse();
a.start_analyse(raw_code);
// auto a = Analyse();
// a.start_analyse(raw_code);
// a.backtrack(0x07F87E0E5BFFF492);
@ -212,19 +213,19 @@ int main() {
// };
// a.backtrack(raw_codes);
std::vector<uint64_t> raw_codes = {
CommonCode("DE77D80").to_raw_code().unwrap(),
CommonCode("DF6DD80").to_raw_code().unwrap(),
CommonCode("DF69730").to_raw_code().unwrap(),
CommonCode("DE76D30").to_raw_code().unwrap(),
CommonCode("D5AF730").to_raw_code().unwrap(),
CommonCode("D5B43BC").to_raw_code().unwrap(),
};
// std::vector<uint64_t> raw_codes = {
// CommonCode("DE77D80").to_raw_code().unwrap(),
// CommonCode("DF6DD80").to_raw_code().unwrap(),
// CommonCode("DF69730").to_raw_code().unwrap(),
// CommonCode("DE76D30").to_raw_code().unwrap(),
// CommonCode("D5AF730").to_raw_code().unwrap(),
// CommonCode("D5B43BC").to_raw_code().unwrap(),
// };
// std::vector<uint64_t> raw_codes = {
// CommonCode("EF697C0").to_raw_code().unwrap(),
// };
// TODO: FATAL ERROR -> don't using mask in Analyse
a.backtrack(raw_codes);
// a.backtrack(raw_codes);
// int sum = 0;
@ -240,6 +241,11 @@ int main() {
// }
load_ranges();
// BasicRanges::build();
// 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