// // This file is part of // // CTBignum // // C++ Library for Compile-Time and Run-Time Multi-Precision and Modular Arithmetic // // // This file is distributed under the Apache License, Version 2.0. See the LICENSE // file for details. #ifndef CT_STRINGINIT_HPP #define CT_STRINGINIT_HPP #include #include #include #include #include #include #include namespace cbn { namespace detail { template //, std::size_t... Is> constexpr auto chars_to_big_int(std::integer_sequence) { // might return a 'non-tight' representation, meaning that there could be // leading zero-limbs constexpr size_t len = sizeof...(Chars); constexpr size_t N = std::max(L, 1 + (10 * len) / (3 * std::numeric_limits::digits)); std::array digits{Chars...}; big_int num{0}; big_int power_of_ten{1}; for (int i = len - 1; i >= 0; --i) { num = add_ignore_carry(num, partial_mul(big_int<1, T>{static_cast(digits[i]) - 48}, power_of_ten)); power_of_ten = partial_mul(big_int<1, T>{static_cast(10)}, power_of_ten); } return num; } template constexpr auto chars_to_integer_seq(std::integer_sequence, std::index_sequence) { constexpr auto num = detail::chars_to_big_int(std::integer_sequence{}); return std::integer_sequence{}; } } //end of detail namespace namespace literals { template constexpr auto operator"" _Z() { using T = uint64_t; // Question: How to elegantly expose the choice of this // type to the user? constexpr size_t len = sizeof...(Chars); constexpr size_t N = 1 + (10 * len) / (3 * std::numeric_limits::digits); auto num = detail::chars_to_integer_seq(std::integer_sequence{}, std::make_index_sequence{}); constexpr auto L = detail::tight_length(num) + (to_big_int(num) == big_int<1, T>{}); return detail::take_first(num, std::make_index_sequence{}); } } } // end of cbn namespace #endif