Browse Source

fix: SHR overflow

master
Dnomd343 2 years ago
parent
commit
9058e516ac
  1. 30
      src/main.cc
  2. 7
      src/raw_code/convert.cc
  3. 12
      src/raw_code/raw_code.cc

30
src/main.cc

@ -69,6 +69,17 @@
// std::cout << "[" << start << ", " << end << ")" << std::endl;
//}
void raw_code_check_verify(uint64_t index) {
uint64_t start = index * 0x100000000;
uint64_t end = start + 0x100000000;
for (uint64_t common_code = start; common_code < end; ++common_code) {
if (RawCode::check(RawCode::extract(common_code)) != CommonCode::check(common_code)) {
printf("Error -> %09lX\n", common_code);
}
}
std::cout << "verify ok: " << std::hex << "[" << start << ", " << end << ")" << std::endl;
}
int main() {
// AllCases::build();
@ -393,19 +404,30 @@ int main() {
// if (RawCode::check(RawCode::extract(common_code)) != CommonCode::check(common_code)) {
// printf("%09lX\n", common_code);
// }
// if (common_code % 0x1000 == 0) {
// std::cout << std::hex << common_code << std::endl;
// if (common_code % 0x1000000 == 0) {
// std::cout << (common_code / 0x1000000) << std::endl;
// }
// }
std::thread tasks[16];
std::cout << "verify start" << std::endl;
for (int i = 0; i < 16; ++i) {
tasks[i] = std::thread(raw_code_check_verify, i);
}
for (auto &t : tasks) {
t.join();
}
std::cout << "verify complete" << std::endl;
// TODO: why 0x555a4001 broken extract function?
// std::cout << RawCode(RawCode::extract(0x00000FEC4)).dump_case() << std::endl;
// std::cout << RawCode(RawCode::extract(0x000055480)).dump_case() << std::endl;
// std::cout << RawCode(RawCode::extract(0x0000FF004)).dump_case() << std::endl;
std::cout << RawCode::extract(0x555a4001) << std::endl;
// std::cout << RawCode::extract(0x555a4001) << std::endl;
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) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl;
// std::cout << "complete benchmark" << std::endl;
// pause();

7
src/raw_code/convert.cc

@ -47,14 +47,14 @@ uint64_t RawCode::compact(uint64_t raw_code) { // raw code --> common code
return head | (range << (unfilled << 1)); // fill low bits as zero
}
#include <iostream>
/// NOTE: ensure that input common code is valid !!!
uint64_t RawCode::extract(uint64_t common_code) { // common code --> raw code
auto code = C_2x2 << (common_code >> 32) * 3; // flag for 2x2 block
auto range = Common::range_reverse((uint32_t)common_code); // reversed range
for (int addr = 0; range; range >>= 2) {
while (0b111 & code >> addr) { // check low 3-bits -> next empty address
/// NOTE: (code >> 65) --> (code >> 1) may cause infinite loop
while ((code >> addr) & 0b111 && addr < 60) { // check low 3-bits -> next empty address
addr += 3; // found available address
}
@ -62,7 +62,6 @@ uint64_t RawCode::extract(uint64_t common_code) { // common code --> raw code
if (addr >= 60) {
return 0;
}
// std::cout << "addr = " << addr << " | block = " << (range & 0b11) << std::endl;
switch (range & 0b11) { // match low 2-bits
case 0b01: // 1x2 block

12
src/raw_code/raw_code.cc

@ -32,19 +32,11 @@ bool RawCode::check(uint64_t raw_code) { // check whether raw code is valid
constexpr uint64_t MASK_2x1 = MASK_1x1 << 12;
constexpr uint64_t MASK_2x2 = MASK_1x1 << 3 | MASK_1x1 << 12 | MASK_1x1 << 15;
// if (MASK_1x2 != 0b100000) {
// printf("error\n");
// }
// if (MASK_2x1 != 0b100000000000000) {
// printf("error\n");
// }
// if (MASK_2x2 != 0b100100000000100000) {
// printf("error\n");
// }
/// high 4-bits check
if (raw_code >> 60) {
return false; // high 4-bits must be zero
}
/// check each block
int head_num = 0, space_num = 0; // statistics for space and 2x2 number
for (int addr = 0; addr < 20; ++addr, raw_code >>= 3) {
switch (raw_code & 0b111) {

Loading…
Cancel
Save