diff options
Diffstat (limited to 'c++/core/templates.h')
-rw-r--r-- | c++/core/templates.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/c++/core/templates.h b/c++/core/templates.h index e704f37..e2851a0 100644 --- a/c++/core/templates.h +++ b/c++/core/templates.h @@ -102,4 +102,43 @@ struct ct_multiply<T, V0, VN...> { static constexpr T value = V0 * ct_multiply<T,VN...>::value; }; +namespace impl { +template<typename T, size_t i> +struct ct_convert_digits_table_helper { + static constexpr std::array<T, 16> table = { + '0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'A', 'B', + 'C', 'D', 'E', 'F' + }; + + static constexpr T value = table[i]; +}; + +template<uint64_t Num, uint64_t Base, uint64_t... Digs> +struct ct_convert_digits_helper { + static constexpr size_t size = ct_convert_digits_helper<Num / Base, Base, Num % Base, Digs...>::size; + static constexpr std::array<uint64_t, size> value = ct_convert_digits_helper<Num / Base, Base, Num % Base, Digs...>::value; +}; + +template<uint64_t Base, uint64_t... Digs> +struct ct_convert_digits_helper<0, Base, Digs...> { + static constexpr size_t size = sizeof...(Digs); + static constexpr std::array<uint64_t, size> value = {Digs...}; +}; + +template<uint64_t Base> +struct ct_convert_digits_helper<0, Base> { + static constexpr size_t size = 0; + static constexpr std::array<uint64_t, 1> value = {0}; +}; +} + +template<uint64_t Num, uint64_t Base> +struct ct_convert_to_digits { + static_assert(Base <= 16, "Only conversion up to hex is supported"); + + static constexpr size_t size = impl::ct_convert_digits_helper<Num, Base>::size; + static constexpr std::array<uint64_t, size> value = impl::ct_convert_digits_helper<Num, Base>::value; +}; } |