From 1b73bd49fa574e8633faa492dae19d9955ea6d5b Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 22 Jul 2018 00:10:22 +0100 Subject: [PATCH] Optimize for base 10 conversions --- include/duckhandy/int_conv.hpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/include/duckhandy/int_conv.hpp b/include/duckhandy/int_conv.hpp index 2e71ddd..a975341 100644 --- a/include/duckhandy/int_conv.hpp +++ b/include/duckhandy/int_conv.hpp @@ -68,7 +68,9 @@ namespace dhandy { using CastedType = typename std::conditional::type; template + [[gnu::pure,gnu::always_inline]] static constexpr L abs(L in) { return (not BecomesUnsigned and std::numeric_limits::is_signed and in < L(0) ? -in : in); } + [[gnu::pure,gnu::always_inline]] static constexpr CastedType cast (I in) { return static_cast(in); } }; @@ -91,6 +93,38 @@ namespace dhandy { return arr; } }; + + template + struct IntConversion { + static constexpr ReversedSizedArray().to_digit(1))>, implem::int_info::max_len + 1> to_ary (I in) { + using RetType = ReversedSizedArray().to_digit(1))>, implem::int_info::max_len + 1>; + using Num = implem::NumberAdaptation; + + const constexpr char lookup[201] = "000102030405060708091011121314" + "1516171819202122232425262728293031323334353637383940414243" + "4445464748495051525354555657585960616263646566676869707172" + "737475767778798081828384858687888990919293949596979899"; + const bool was_negative = implem::is_negative(in); + + RetType arr; + arr.push_front('\0'); + while (Num::abs(in) >= static_cast(100)) { + arr.push_front(Tr::to_digit(static_cast(lookup[(Num::abs(in) % 100) * 2 + 1] - '0'))); + arr.push_front(Tr::to_digit(static_cast(lookup[(Num::abs(in) % 100) * 2 + 0] - '0'))); + in = static_cast(in / static_cast(100)); + }; + if (Num::abs(in) < static_cast(10)) { + arr.push_front(Tr::to_digit(static_cast(Num::abs(in)))); + } + else { + arr.push_front(Tr::to_digit(static_cast(lookup[Num::abs(in) * 2 + 1] - '0'))); + arr.push_front(Tr::to_digit(static_cast(lookup[Num::abs(in) * 2 + 0] - '0'))); + } + if (was_negative) + arr.push_front(Tr::minus()); + return arr; + } + }; } //namespace implem template