5#ifndef UGMISC_INT_FINDER_HPP
6#define UGMISC_INT_FINDER_HPP
57#include "ugmisc/templated_callable_loop.hpp"
64enum int_sign { INVALID_SIGN = 0, UNSIGNED = 1, SIGNED = 2 };
69constexpr int_sign flip( int_sign s ) {
70 return static_cast<int_sign
>(((unsigned)s) ^ 3);
82 SELECT_SIGN_OPTION_NONE = 0,
90 SELECT_SIGN_OPTION_MASK = 7,
96constexpr std::string_view get_name(sign_opt opt) {
98 case SELECT_SIGN_OPTION_NONE:
100 case SELECT_UNSIGNED:
101 return "SELECT_UNSIGNED";
103 return "SELECT_SIGNED";
104 case SELECT_TYPE_SIGN:
105 return "SELECT_TYPE_SIGN";
106 case SELECT_VALUE_SIGN:
107 return "SELECT_VALUE_SIGN";
121 case SELECT_UNSIGNED:
return UNSIGNED;
122 case SELECT_SIGNED:
return SIGNED;
124 return std::is_signed_v<T> ? SIGNED : UNSIGNED;
126 return v < 0 ? SIGNED : UNSIGNED;
139 SELECT_SIGN_FLAGS_NONE,
162constexpr sign_flag operator~(sign_flag f) {
163 return static_cast<sign_flag
>((~(std::uint8_t)f)&(std::uint8_t)SELECT_SIGN_FLAGS_MASK);
168 return static_cast<sign_flag>((std::uint8_t)a | (std::uint8_t)b);
177constexpr bool allow_value_sign_loss(
sign_flag s) {
188constexpr std::string_view get_name(
sign_flag flags) {
189 bool smol = smallest(flags);
190 bool signloss = allow_value_sign_loss(flags);
191 if ( smol && signloss ) {
192 return "SELECT_SIGN_SMALLEST|SELECT_SIGN_ALLOW_VALUE_SIGN_LOSS";
194 return "SELECT_SIGN_SMALLEST";
195 }
else if ( signloss ) {
196 return "SELECT_SIGN_ALLOW_VALUE_SIGN_LOSS";
212inline constexpr bool is_opt = std::is_convertible_v<T, sign_opt>;
215inline constexpr bool is_flag = std::is_convertible_v<T, sign_flag>;
226constexpr sign_opt sign_opt_or_not(T t) {
227 if constexpr ( is_opt<T> ) {
229 }
else if constexpr ( is_flag<T> ) {
230 return SELECT_SIGN_OPTION_NONE;
232 static_assert( false );
237template<
class T>
constexpr bool is_non_null_opt(T t) {
238 return (
unsigned)sign_opt_or_not(t);
242template<
class T>
constexpr sign_flag sign_flag_or_not(T t) {
243 if constexpr ( is_flag<T> ) {
245 }
else if constexpr ( is_opt<T> ) {
246 return SELECT_SIGN_FLAGS_NONE;
250 "sign_flag_or_not takes sign_flag and sign_opt, "
251 "and types convertible to them."
265constexpr sign_opt combine_sign_opts_f()
267 constexpr sign_opt opt_array[
sizeof...(Opts)] = { sign_opt_or_not(Opts)... };
268 sign_opt found_opt = SELECT_SIGN_OPTION_NONE;
269 for( sign_opt opt: opt_array ) {
270 if ( opt == SELECT_SIGN_OPTION_NONE ) {
273 if ( found_opt != SELECT_SIGN_OPTION_NONE && opt != found_opt ) {
274 return SELECT_SIGN_OPTION_NONE;
284template<
auto...Opts>
static constexpr sign_opt combined_sign_opts
285 = combine_sign_opts_f<Opts...>();
288template<
auto...Flags>
static constexpr sign_flag combined_sign_flags
289 = (SELECT_SIGN_FLAGS_NONE | ... | sign_flag_or_not(Flags));
295static constexpr bool valid_sign_opts = (unsigned)combined_sign_opts<Opt...>;
301static constexpr bool any_sign_opts = (... || is_non_null_opt(Opt));
314class flag_opt_combo {
318 flag_opt_combo(
const flag_opt_combo&) =
default;
319 constexpr flag_opt_combo(sign_flag sflags) : m_flags(sflags) {}
321 constexpr sign_flag flags()
const {
return m_flags; }
322 constexpr sign_opt safe_opt()
const {
return SELECT_SIGN_OPTION_NONE; }
326 "This flag_opt_combo does not have an option."
331 constexpr operator sign_flag()
const {
return flags(); }
336class flag_opt_combo<true> :
public flag_opt_combo<false> {
340 flag_opt_combo(
const flag_opt_combo&) =
default;
341 constexpr flag_opt_combo(sign_opt sopt, sign_flag sflags)
342 : flag_opt_combo<false>(sflags), m_opt(sopt)
345 constexpr sign_opt opt()
const {
return m_opt; }
346 constexpr sign_opt safe_opt()
const {
return opt(); }
348 constexpr operator sign_opt()
const {
return opt(); }
355constexpr auto combine_flags_opts()
357 constexpr sign_flag flags = combined_sign_flags<args...>;
358 constexpr sign_opt opt = combined_sign_opts<args...>;
359 constexpr bool valid_opts = valid_sign_opts<args...>;
360 constexpr bool any_opts = any_sign_opts<args...>;
362 if constexpr ( any_opts ) {
363 static_assert( valid_opts );
364 return flag_opt_combo<true>{opt, flags};
366 return flag_opt_combo<false>{flags};
381#ifndef UGMISC_INT_FINDER_MAX_INT_WIDTH
386# define UGMISC_INT_FINDER_MAX_INT_WIDTH (1U << 12);
411 using unsigned_type = std::uint8_t;
412 using signed_type = std::int8_t;
415template<>
struct get_int_types<16> {
416 using unsigned_type = std::uint16_t;
417 using signed_type = std::int16_t;
421#ifndef UGMISC_TEST_INT_FINDER_SKIP_U32
422 using unsigned_type = std::uint32_t;
424 using signed_type = std::int32_t;
428 using unsigned_type = std::uint64_t;
429 using signed_type = std::int64_t;
441constexpr unsigned round_up_to_pow2(
unsigned x) {
443 unsigned lo = hi >> 1;
444 return x ? (lo == x ? lo : hi) : 1;
469constexpr bool check_member_int_types(get_int_types<N> t) {
471 "Integer width must be no more than "
475 using T =
decltype(t);
477 if constexpr ( member_type_access<utype, T>::has_member ) {
478 using I =
typename T::unsigned_type;
479 using limits = std::numeric_limits<I>;
480 constexpr unsigned bits = limits::digits;
481 static_assert( limits::radix == 2 );
482 static_assert( limits::is_integer );
483 static_assert( ! limits::is_signed );
484 static_assert( bits == N );
487 if constexpr ( member_type_access<stype, T>::has_member ) {
488 using I =
typename T::signed_type;
489 using limits = std::numeric_limits<I>;
490 constexpr unsigned bits = limits::digits + 1;
491 static_assert( limits::radix == 2 );
492 static_assert( limits::is_integer );
493 static_assert( limits::is_signed );
494 static_assert( bits == N );
507template<
unsigned N>
struct get_int_types :
public ::ugmisc::get_int_types<N> {
509 static constexpr bool _check =
510 check_member_int_types(::ugmisc::get_int_types<N>{});
536template<
unsigned N,
int_sign Sign>
580template<
unsigned N,
int_sign S>
struct get_exact_int_type;
584struct get_exact_int_type<N, UNSIGNED> {
589struct get_exact_int_type<N, SIGNED> {
603 typename intfind_::get_exact_int_type<N, S>::type;
614static constexpr unsigned find_inner_loop_max = 100;
622template<
int_sign Sign>
625 constexpr decltype(
auto)
operator() (
626 std::integral_constant<unsigned, V> I
632 return template_find_continue;
654template<
unsigned N,
int_sign Sign>
657 intfind_::template_callable_find<N, max_int_width, intfind_::type_finder<Sign>>()
666template<
unsigned N,
int_sign Sign>
668 intfind_::template_callable_find_success<N, max_int_width, intfind_::type_finder<Sign>>();
706#ifdef UGMISC_USE_CONCEPT_DECLS
707template<
bool IncludingSignBit, Int T >
710template<
bool IncludingSignBit,
class T >
715 constexpr bool use_sign_bit = std::is_signed_v<T> && IncludingSignBit;
716 constexpr unsigned extra_bits = use_sign_bit ? 1 : 0;
717 const U w = v >= 0 ? (U)v : ~(U)v;
733template<
unsigned LeastBits,
int_sign Sign>
734constexpr auto find_type() {
735 if constexpr ( has_sufficient_int_type<LeastBits, Sign> ) {
736 using int_type = least_int_type<LeastBits, Sign>;
747struct range_result_compare {
749 range_result_compare(
const range_result_compare&) =
default;
750 constexpr range_result_compare(T v) : value(v) {}
751 constexpr bool valid()
const {
return value; }
752 constexpr unsigned size()
const {
return sizeof(T); }
753 constexpr unsigned used_bits()
const {
754 unsigned sign_bit = std::numeric_limits<T>::is_signed ? 1 : 0;
755 return std::numeric_limits<T>::digits + sign_bit;
766template<
class T,
class U>
767constexpr bool operator < (range_result_compare<T> a, range_result_compare<U> b) {
768 if ( !a.valid() ) {
return false; }
769 if ( !b.valid() ) {
return true; }
770 if ( a.size() < b.size() ) {
return true; }
771 if ( a.size() > b.size() ) {
return false; }
772 return a.used_bits() < b.used_bits();
778template<
auto V,
auto...SelectArgs>
779constexpr auto as_least_required_type() {
780 constexpr auto S = intfind_::combine_flags_opts<SelectArgs...>();
781 constexpr int_sign pref_sign = sign(S, V);
782 constexpr int_sign other_sign = flip(pref_sign);
783 constexpr int_sign value_sign = V < 0 ? SIGNED : UNSIGNED;
785 constexpr bool may_be_unsigned =
786 value_sign == UNSIGNED
787 || allow_value_sign_loss(S)
790 constexpr unsigned value_bits = signed_bitwidth<true>(V);
791 constexpr bool choose_smaller = smallest(S);
796 constexpr auto pref_value =
797 intfind_::find_type<value_bits, pref_sign>();
798 constexpr auto other_value =
799 intfind_::find_type<value_bits, other_sign>();
801 using pref_t =
decltype(pref_value);
802 using other_t =
decltype(other_value);
804 constexpr bool pref_valid = pref_value && (std::numeric_limits<pref_t>::is_signed || may_be_unsigned);
810 constexpr bool other_valid = other_value && (std::numeric_limits<other_t>::is_signed || may_be_unsigned) && choose_smaller;
813 pref_valid || other_valid,
814 "least_required_type could not find a type that satisfies all "
819 constexpr bool use_pref = [=]() ->
bool {
824 if constexpr ( ! other_valid ) {
827 }
else if constexpr ( ! pref_valid ) {
830 }
else if constexpr (
831 choose_smaller && other_valid
833 range_result_compare{other_value} < range_result_compare{pref_value}
845 if constexpr ( use_pref ) {
848 "This is Larry's fault."
849 " An earlier static assertion should have failed already."
851 return static_cast<pref_t
>(V);
855 "This is Larry's fault."
856 " An earlier static assertion should have failed already."
858 return static_cast<other_t
>(V);
886template<
auto V,
auto...S>
888 intfind_::as_least_required_type<V, S...>();
901template<
auto V,
auto...S>
Provides constexpr bit counting functions.
constexpr auto bitwidth(T) noexcept -> UGMISC_BITWISE_UINT_RETURN(T, int)
Definition bitops.hpp:317
sign_opt
Definition int_finder.hpp:81
@ SELECT_VALUE_SIGN
Select a signed type iff the value is negative.
Definition int_finder.hpp:87
@ SELECT_TYPE_SIGN
Definition int_finder.hpp:85
constexpr bool has_unsigned_type
Definition int_finder.hpp:545
decltype(as_least_required_int_type< V, S... >) least_required_int_type
Definition int_finder.hpp:902
constexpr unsigned signed_bitwidth(T v)
Definition int_finder.hpp:708
#define UGMISC_INT_FINDER_MAX_INT_WIDTH
Definition int_finder.hpp:386
constexpr bool has_either_int_type
Definition int_finder.hpp:556
sign_flag
Definition int_finder.hpp:138
@ SELECT_SIGN_ALLOW_VALUE_SIGN_LOSS
Definition int_finder.hpp:156
@ SELECT_SIGN_SMALLEST
Definition int_finder.hpp:149
least_int_type< N, UNSIGNED > least_unsigned_type
Definition int_finder.hpp:675
typename intfind_::get_int_types< N >::signed_type exact_signed_type
Definition int_finder.hpp:566
least_int_type< N, SIGNED > least_signed_type
Definition int_finder.hpp:682
constexpr bool has_sufficient_int_type
Definition int_finder.hpp:667
typename intfind_::get_int_types< N >::unsigned_type exact_unsigned_type
Definition int_finder.hpp:563
decltype( intfind_::template_callable_find< N, max_int_width, intfind_::type_finder< Sign > >()) least_int_type
Definition int_finder.hpp:655
int_sign
Used to select signed or unsigned types.
Definition int_finder.hpp:64
constexpr auto as_least_required_int_type
Definition int_finder.hpp:887
constexpr bool has_int_type
Definition int_finder.hpp:537
constexpr bool has_both_int_types
Definition int_finder.hpp:552
typename intfind_::get_exact_int_type< N, S >::type exact_int_type
Definition int_finder.hpp:602
constexpr bool has_signed_type
Definition int_finder.hpp:549
constexpr unsigned max_int_width
Definition int_finder.hpp:393
Testing for and using named static and non static members of types.
#define UGMISC_DECL_MEMBER_ACCESS(TNAME, NAME)
Definition member.hpp:148
#define UGMISC_QQ(...)
Definition quote.hpp:34
std::enable_if_t< std::numeric_limits< T >::is_integer, R > int_only
Definition sfinae_helpers.hpp:212
Definition int_finder.hpp:407
Definition member.hpp:1459