Skip to content

Instantly share code, notes, and snippets.

@jepler
Created January 21, 2024 18:12
Show Gist options
  • Save jepler/4cc79eafe005de4a26cd4d4b7cddfa71 to your computer and use it in GitHub Desktop.
Save jepler/4cc79eafe005de4a26cd4d4b7cddfa71 to your computer and use it in GitHub Desktop.
// build with arm-non-eabi-g++ -std=c++20
#include <type_traits>
#include <array>
#include <stdio.h>
constexpr int decimal_to_bcd(int x) {
int scale = 1;
int result = 0;
for(size_t i = 0; i<5; i++) {
result += scale * (x % 10);
x /= 10;
scale *= 16;
}
return result;
}
template<int N>
consteval std::array<int, N> make_bcd_table() {
std::array<int, N> arr = {};
for(size_t i=0; i<N; i++) {
arr[i] = decimal_to_bcd(i);
}
return arr;
}
const std::array<int, 100000> bcd_table = make_bcd_table<100000>();
#if __linux__
int main() {
printf("%05x\n", bcd_table[1337]);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment