Skip to content

Commit

Permalink
Easily allow for custom limb type in literal
Browse files Browse the repository at this point in the history
Thanks @ Anjan Roy
  • Loading branch information
Niek Bouman committed Nov 28, 2021
1 parent c4a0ff4 commit d5bde6d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions include/ctbignum/decimal_literals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,28 @@ constexpr auto chars_to_integer_seq(std::integer_sequence<char, Chars...>,
} //end of detail namespace

namespace literals {
template <char... Chars> constexpr auto operator"" _Z() {

using T = uint64_t; // Question: How to elegantly expose the choice of this
// type to the user?

template <typename T, char... Chars> constexpr auto generic_limb_literal() {
constexpr size_t len = sizeof...(Chars);
constexpr size_t N = 1 + (10 * len) / (3 * std::numeric_limits<T>::digits);

auto num = detail::chars_to_integer_seq(std::integer_sequence<char, Chars...>{}, std::make_index_sequence<N>{});
constexpr auto L = detail::tight_length(num) + (to_big_int(num) == big_int<1, T>{});
return detail::take_first(num, std::make_index_sequence<L>{});
}

template <char... Chars> constexpr auto operator"" _Z() // for backwards compatibility
{
return generic_limb_literal<uint64_t, Chars...>();
}

template <char... Chars> constexpr auto operator"" _Z64() {
return generic_limb_literal<uint64_t, Chars...>();
}

template <char... Chars> constexpr auto operator"" _Z32() {
return generic_limb_literal<uint32_t, Chars...>();
}

} // namespace literals
} // end of cbn namespace

#endif

0 comments on commit d5bde6d

Please sign in to comment.