From fa38e393069ab53bb0fb5ac9b022e397b4fb7215 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 15 Jan 2023 22:25:05 +0800 Subject: [PATCH] feat: new tiny decoder for ShortCode --- src/main.cc | 34 ++++++++------- src/short_code/data_loader.cc | 46 +++++++++++++++++++++ src/short_code/offset/all_cases_offset.h | 10 +++++ src/short_code/offset/basic_ranges_offset.h | 2 +- src/short_code/offset/range_prefix_offset.h | 2 +- src/short_code/short_code.h | 4 +- 6 files changed, 78 insertions(+), 20 deletions(-) create mode 100644 src/short_code/offset/all_cases_offset.h diff --git a/src/main.cc b/src/main.cc index 5d80f2e..e038dd2 100644 --- a/src/main.cc +++ b/src/main.cc @@ -269,14 +269,16 @@ int main() { // } // } - auto br = BasicRanges::fetch(); - for (auto &range : br) { - range = Common::range_reverse(range); - } + printf("%09lX\n", ShortCode::tiny_decode_demo(14323231)); + +// auto br = BasicRanges::fetch(); +// for (auto &range : br) { +// range = Common::range_reverse(range); +// } /// 1017983: 0FFFFFFF /// 1017984: 10000000 /// 1017985: 1000000C - int sum = 0; +// int sum = 0; // printf(" "); // for (uint32_t prefix = 0; prefix < 0x1000; ++prefix) { // printf("%7td, ", std::lower_bound(br.begin(), br.end(), prefix << 20) - br.begin()); @@ -285,17 +287,17 @@ int main() { // } // } - for (int head = 0; head < 16; ++head) { - auto &a = AllCases::fetch()[head]; - printf("\n/// --------------------------------- 0x%X ---------------------------------\n ", head); - for (uint32_t prefix = 0; prefix < 0x1000; ++prefix) { - printf("%7td, ", std::lower_bound(a.begin(), a.end(), prefix << 20) - a.begin()); - if (sum++ % 8 == 7 and prefix != 0xFFF) { - printf("\n "); - } - } - } - printf("\n"); +// for (int head = 0; head < 16; ++head) { +// auto &a = AllCases::fetch()[head]; +// printf("\n/// --------------------------------- 0x%X ---------------------------------\n ", head); +// for (uint32_t prefix = 0; prefix < 0x1000; ++prefix) { +// printf("%7td, ", std::lower_bound(a.begin(), a.end(), prefix << 20) - a.begin()); +// if (sum++ % 8 == 7 and prefix != 0xFFF) { +// printf("\n "); +// } +// } +// } +// printf("\n"); // printf("%09lX\n", ShortCode::fast_decode(14323231)); // std::cout << ShortCode::fast_encode(0x6EC0F8800) << std::endl; diff --git a/src/short_code/data_loader.cc b/src/short_code/data_loader.cc index 41b598e..4c07e8d 100644 --- a/src/short_code/data_loader.cc +++ b/src/short_code/data_loader.cc @@ -66,3 +66,49 @@ uint32_t ShortCode::fast_encode(uint64_t common_code) { auto offset = std::lower_bound(ranges.begin(), ranges.end(), (uint32_t)common_code) - ranges.begin(); return ALL_CASES_OFFSET[head] + offset; // release short code } + +#include "basic_ranges_offset.h" +#include "range_prefix_offset.h" + +#include +#include "common.h" + +uint64_t ShortCode::tiny_decode_demo(uint32_t short_code) { + /// match head index + auto offset = std::upper_bound( // binary search + ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code + ) - 1; + uint64_t head = offset - ALL_CASES_OFFSET; // head index + short_code -= *offset; + + /// match range prefix + offset = std::upper_bound( // binary search + RANGE_PREFIX_OFFSET_[head], RANGE_PREFIX_OFFSET_[head] + 4096, short_code + ) - 1; + uint32_t prefix = offset - RANGE_PREFIX_OFFSET_[head]; // range prefix + short_code -= *offset; + + uint32_t start_point = BASIC_RANGES_OFFSET_[prefix]; + + printf("prefix: %X\n", prefix); + printf("start at %d\n", start_point); + printf("next %d valid case\n", short_code); + + uint32_t range; + + for (auto index = start_point; index < BasicRanges::fetch().size(); ++index) { // traverse basic ranges + range = BasicRanges::fetch()[index]; + if (Common::check_case(head, range)) { // search for valid cases + + if (!short_code--) { // short code approximate + break; // found target range + } + + } + } + + printf("range -> %08X\n", Common::range_reverse(range)); + + return (uint64_t)head << 32 | Common::range_reverse(range); + +} diff --git a/src/short_code/offset/all_cases_offset.h b/src/short_code/offset/all_cases_offset.h new file mode 100644 index 0000000..442e692 --- /dev/null +++ b/src/short_code/offset/all_cases_offset.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +const uint32_t ALL_CASES_OFFSET_[16] = { + 0, 2942906, 5203298, 8146204, + 8146204, 10468254, 12345199, 14667249, + 14667249, 16989299, 18866244, 21188294, + 21188294, 24131200, 26391592, 29334498, +}; diff --git a/src/short_code/offset/basic_ranges_offset.h b/src/short_code/offset/basic_ranges_offset.h index cc75586..bb711b8 100644 --- a/src/short_code/offset/basic_ranges_offset.h +++ b/src/short_code/offset/basic_ranges_offset.h @@ -2,7 +2,7 @@ #include -const uint32_t BASIC_RANGES_OFFSET[4096] = { +const uint32_t BASIC_RANGES_OFFSET_[4096] = { 0, 18272, 24960, 31648, 49920, 56608, 59056, 61504, 68192, 74880, 77328, 79776, 86464, 104736, 111424, 118112, 136384, 143072, 145520, 147968, 154656, 157104, 158000, 158896, diff --git a/src/short_code/offset/range_prefix_offset.h b/src/short_code/offset/range_prefix_offset.h index d45e17d..e9f3183 100644 --- a/src/short_code/offset/range_prefix_offset.h +++ b/src/short_code/offset/range_prefix_offset.h @@ -2,7 +2,7 @@ #include -const uint32_t RANGE_PREFIX_OFFSET[16][4096] = { +const uint32_t RANGE_PREFIX_OFFSET_[16][4096] = { /// --------------------------------- 0x0 --------------------------------- 0, 9527, 12991, 16067, 25594, 29058, 30527, 31657, 35121, 38525, 39766, 41041, 44445, 53972, 57436, 60512, diff --git a/src/short_code/short_code.h b/src/short_code/short_code.h index d43b783..5e5c92f 100644 --- a/src/short_code/short_code.h +++ b/src/short_code/short_code.h @@ -37,8 +37,8 @@ public: static uint64_t fast_decode(uint32_t short_code); static uint32_t fast_encode(uint64_t common_code); - static uint64_t tidy_decode_demo(uint32_t short_code); - static uint32_t tidy_encode_demo(uint64_t common_code); + static uint64_t tiny_decode_demo(uint32_t short_code); + static uint32_t tiny_encode_demo(uint64_t common_code); private: uint32_t code;