Browse Source

update: enhance AllCases module

master
Dnomd343 2 years ago
parent
commit
67142e7678
  1. 151
      src/all_cases/all_cases.cc
  2. 2
      src/all_cases/all_cases.h
  3. 7
      src/main.cc

151
src/all_cases/all_cases.cc

@ -5,6 +5,13 @@ std::mutex AllCases::building;
bool AllCases::available = false;
std::vector<uint32_t> AllCases::data[];
const std::vector<uint32_t> (*AllCases::fetch())[16] { // get all cases content
if (status() != AVAILABLE) {
AllCases::build(); // all cases initialize
}
return &AllCases::data; // return const ptr
}
AllCases::Status AllCases::status() { // get all cases status
if (AllCases::available) {
return AVAILABLE; // all cases already built
@ -16,13 +23,6 @@ AllCases::Status AllCases::status() { // get all cases status
return NO_INIT;
}
const std::vector<uint32_t> (*AllCases::fetch())[16] { // get all cases content
if (status() != AVAILABLE) {
AllCases::build(); // all cases initialize
}
return &AllCases::data; // return const ptr
}
void AllCases::build() { // ensure that all cases available
if (!AllCases::available) {
if (AllCases::building.try_lock()) { // mutex lock success
@ -35,116 +35,59 @@ void AllCases::build() { // ensure that all cases available
}
}
void AllCases::build_data() { // find all cases
auto basic_ranges = BasicRanges::fetch();
for (uint32_t head = 0; head < 15; ++head) { // address of 2x2 block
if ((head & 0b11) == 0b11) {
++head; // skip invalid address
}
/// head -> 0/1/2 / 4/5/6 / 8/9/10 / 12/13/14
data[head].reserve(ALL_CASES_SIZE[head]); // memory pre-allocated
for (auto index = 0; index < basic_ranges->size(); ++index) {
uint32_t broken = check_case(head, (*basic_ranges)[index]); // check and get broken address
auto range_rev = Common::range_reverse((*basic_ranges)[index]); // reversed range
if (broken) { // invalid case
auto delta = (uint32_t)1 << (32 - broken * 2); // this <--delta--> next possible range
auto min_next = (range_rev & ~(delta - 1)) + delta;
while (Common::range_reverse((*basic_ranges)[++index]) < min_next); // located next range
--index;
} else {
AllCases::data[head].emplace_back(range_rev); // release valid cases
}
}
}
}
//int32_t AllCases::check_case(uint32_t head, uint32_t range) { // whether the head and range is valid
//
// uint32_t mask = 0b110011 << head; // fill 2x2 block
//
// for (int addr = 0; range; range >>= 2) { // traverse every 2-bits
//
// while (mask >> addr & 0b1) {
// ++addr; // search next not filled block
// }
//
// switch (range & 0b11) {
// case 0b00: // space block
// case 0b11: // 1x1 block
// mask |= 0b1 << addr; // fill 1x1 block
// break;
// case 0b10: // 2x1 block
// if (addr > 15 || mask >> (addr + 4) & 0b1) { // invalid address
// return false;
// }
// mask |= 0b10001 << addr; // fill 2x1 block
// break;
// case 0b01: // 1x2 block
// if ((addr & 0b11) == 0b11 || mask >> (addr + 1) & 0b1) { // invalid address
// return false;
// }
// mask |= 0b11 << addr; // fill 1x2 block
// break;
// }
// }
//
// return true; // valid case
//
//}
uint32_t AllCases::check_case(uint32_t head, uint32_t range) { // whether the head and range is valid
uint32_t num = 1;
uint32_t mask = 0b110011 << head; // fill 2x2 block
for (int addr = 0; range; range >>= 2, ++num) { // traverse every 2-bits
int AllCases::check_case(uint32_t head, uint32_t range) { // check the head and range
constexpr uint32_t M_1x1 = 0b1;
constexpr uint32_t M_1x2 = 0b11;
constexpr uint32_t M_2x1 = 0b10001;
constexpr uint32_t M_2x2 = 0b110011;
while (mask >> addr & 0b1) {
int block_num = 1;
uint32_t cache = M_2x2 << head; // fill 2x2 block
for (int addr = 0; range; range >>= 2, ++block_num) { // traverse every 2-bits
while (cache >> addr & 0b1) {
++addr; // search next not filled block
}
switch (range & 0b11) {
case 0b00: // space block
case 0b00: // space
case 0b11: // 1x1 block
mask |= 0b1 << addr; // fill 1x1 block
cache |= M_1x1 << addr; // fill space or 1x1 block
break;
case 0b10: // 2x1 block
if (addr > 15 || mask >> (addr + 4) & 0b1) { // invalid address
return num;
if (addr > 15 || cache >> (addr + 4) & 0b1) { // invalid address
return block_num; // broken block number
}
mask |= 0b10001 << addr; // fill 2x1 block
cache |= M_2x1 << addr; // fill 2x1 block
break;
case 0b01: // 1x2 block
if ((addr & 0b11) == 0b11 || mask >> (addr + 1) & 0b1) { // invalid address
return num;
if ((addr & 0b11) == 0b11 || cache >> (addr + 1) & 0b1) { // invalid address
return block_num; // broken block number
}
mask |= 0b11 << addr; // fill 1x2 block
cache |= M_1x2 << addr; // fill 1x2 block
break;
}
}
return 0; // valid case
}
#include <iostream>
void AllCases::build_data() { // find all cases
uint32_t sum = 0;
for (uint32_t head = 0; head < 16; ++head) { // address of 2x2 block
if ((head & 0b11) == 0b11) {
continue; // invalid 2x2 address
}
/// head -> 0/1/2 / 4/5/6 / 8/9/10 / 12/13/14
data[head].reserve(ALL_CASES_SIZE[head]); // memory pre-allocated
uint32_t ret;
auto br = BasicRanges::fetch();
for (auto i = 0; i < br->size(); ++i) {
auto rev = Common::range_reverse((*br)[i]);
if ((ret = check_case(head, (*br)[i])) == 0) {
++sum;
// printf("%08X\n", Common::range_reverse(br->at(i)));
} else {
auto delta = (uint32_t)1 << (32 - ret * 2);
auto next = (rev & ~(delta - 1)) + delta;
while (Common::range_reverse((*br)[++i]) < next);
--i;
}
}
}
std::cout << sum << std::endl;
return 0; // pass
}

2
src/all_cases/all_cases.h

@ -25,6 +25,6 @@ private:
static void build_data();
static uint32_t check_case(uint32_t head, uint32_t range);
static int check_case(uint32_t head, uint32_t range);
};

7
src/main.cc

@ -249,6 +249,13 @@ int main() {
AllCases::build();
for (auto head = 0; head < 16; ++head) {
uint64_t prefix = (uint64_t)head << 32;
for (const auto &range : (*AllCases::fetch())[head]) {
printf("%09lX\n", prefix | range);
}
}
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