81enum int_sign { INVALID_SIGN = 0, UNSIGNED = 1, SIGNED = 2 };
86constexpr int_sign flip( int_sign s ) {
87 return static_cast<int_sign
>(((unsigned)s) ^ 3);
103 SELECT_SIGN_OPTION_NONE = 0,
111 SELECT_SIGN_OPTION_MASK = 7,
117constexpr std::string_view get_name(sign_opt opt) {
119 case SELECT_SIGN_OPTION_NONE:
121 case SELECT_UNSIGNED:
122 return "SELECT_UNSIGNED";
124 return "SELECT_SIGNED";
125 case SELECT_TYPE_SIGN:
126 return "SELECT_TYPE_SIGN";
127 case SELECT_VALUE_SIGN:
128 return "SELECT_VALUE_SIGN";
142 case SELECT_UNSIGNED:
return UNSIGNED;
143 case SELECT_SIGNED:
return SIGNED;
145 return std::is_signed_v<T> ? SIGNED : UNSIGNED;
147 return v < 0 ? SIGNED : UNSIGNED;
157 SELECT_SIGN_FLAGS_NONE,
180constexpr sign_flag operator~(sign_flag f) {
181 return static_cast<sign_flag
>((~(std::uint8_t)f)&(std::uint8_t)SELECT_SIGN_FLAGS_MASK);
186 return static_cast<sign_flag>((std::uint8_t)a | (std::uint8_t)b);
195constexpr bool allow_value_sign_loss(
sign_flag s) {
206constexpr std::string_view get_name(
sign_flag flags) {
207 bool smol = smallest(flags);
208 bool signloss = allow_value_sign_loss(flags);
209 if ( smol && signloss ) {
210 return "SELECT_SIGN_SMALLEST|SELECT_SIGN_ALLOW_VALUE_SIGN_LOSS";
212 return "SELECT_SIGN_SMALLEST";
213 }
else if ( signloss ) {
214 return "SELECT_SIGN_ALLOW_VALUE_SIGN_LOSS";
230inline constexpr bool is_opt = std::is_convertible_v<T, sign_opt>;
233inline constexpr bool is_flag = std::is_convertible_v<T, sign_flag>;
244constexpr sign_opt sign_opt_or_not(T t) {
245 if constexpr ( is_opt<T> ) {
247 }
else if constexpr ( is_flag<T> ) {
248 return SELECT_SIGN_OPTION_NONE;
250 static_assert( false );
255template<
class T>
constexpr bool is_non_null_opt(T t) {
256 return (
unsigned)sign_opt_or_not(t);
260template<
class T>
constexpr sign_flag sign_flag_or_not(T t) {
261 if constexpr ( is_flag<T> ) {
263 }
else if constexpr ( is_opt<T> ) {
264 return SELECT_SIGN_FLAGS_NONE;
268 "sign_flag_or_not takes sign_flag and sign_opt, "
269 "and types convertible to them."
283constexpr sign_opt combine_sign_opts_f()
285 constexpr sign_opt opt_array[
sizeof...(Opts)] = { sign_opt_or_not(Opts)... };
286 sign_opt found_opt = SELECT_SIGN_OPTION_NONE;
287 for( sign_opt opt: opt_array ) {
288 if ( opt == SELECT_SIGN_OPTION_NONE ) {
291 if ( found_opt != SELECT_SIGN_OPTION_NONE && opt != found_opt ) {
292 return SELECT_SIGN_OPTION_NONE;
302template<
auto...Opts>
static constexpr sign_opt combined_sign_opts
303 = combine_sign_opts_f<Opts...>();
306template<
auto...Flags>
static constexpr sign_flag combined_sign_flags
307 = (SELECT_SIGN_FLAGS_NONE | ... | sign_flag_or_not(Flags));
313static constexpr bool valid_sign_opts = (unsigned)combined_sign_opts<Opt...>;
319static constexpr bool any_sign_opts = (... || is_non_null_opt(Opt));
332class flag_opt_combo {
336 flag_opt_combo(
const flag_opt_combo&) =
default;
337 constexpr flag_opt_combo(sign_flag sflags) : m_flags(sflags) {}
339 constexpr sign_flag flags()
const {
return m_flags; }
340 constexpr sign_opt safe_opt()
const {
return SELECT_SIGN_OPTION_NONE; }
344 "This flag_opt_combo does not have an option."
349 constexpr operator sign_flag()
const {
return flags(); }
354class flag_opt_combo<true> :
public flag_opt_combo<false> {
358 flag_opt_combo(
const flag_opt_combo&) =
default;
359 constexpr flag_opt_combo(sign_opt sopt, sign_flag sflags)
360 : flag_opt_combo<false>(sflags), m_opt(sopt)
363 constexpr sign_opt opt()
const {
return m_opt; }
364 constexpr sign_opt safe_opt()
const {
return opt(); }
366 constexpr operator sign_opt()
const {
return opt(); }
373constexpr auto combine_flags_opts()
375 constexpr sign_flag flags = combined_sign_flags<args...>;
376 constexpr sign_opt opt = combined_sign_opts<args...>;
377 constexpr bool valid_opts = valid_sign_opts<args...>;
378 constexpr bool any_opts = any_sign_opts<args...>;
380 if constexpr ( any_opts ) {
381 static_assert( valid_opts );
382 return flag_opt_combo<true>{opt, flags};
384 return flag_opt_combo<false>{flags};
399#ifndef UGMISC_INT_FINDER_MAX_INT_WIDTH
404# define UGMISC_INT_FINDER_MAX_INT_WIDTH (1U << 12);
439 using unsigned_type = std::uint8_t;
440 using signed_type = std::int8_t;
448 using unsigned_type = std::uint16_t;
449 using signed_type = std::int16_t;
457#ifndef UGMISC_TEST_INT_FINDER_SKIP_U32
458 using unsigned_type = std::uint32_t;
460 using signed_type = std::int32_t;
468 using unsigned_type = std::uint64_t;
469 using signed_type = std::int64_t;
481constexpr unsigned round_up_to_pow2(
unsigned x) {
483 unsigned lo = hi >> 1;
484 return x ? (lo == x ? lo : hi) : 1;
509constexpr bool check_member_int_types(get_int_types<N> t) {
510 static_assert( N <= max_int_width,
511 "Integer width must be no more than "
515 using T =
decltype(t);
517 if constexpr ( has_member_type_unsigned_type<T> ) {
518 using I =
typename T::unsigned_type;
519 using limits = std::numeric_limits<I>;
520 constexpr unsigned bits = limits::digits;
521 static_assert( limits::radix == 2 );
522 static_assert( limits::is_integer );
523 static_assert( ! limits::is_signed );
524 static_assert( bits == N );
527 if constexpr ( has_member_type_signed_type<T> ) {
528 using I =
typename T::signed_type;
529 using limits = std::numeric_limits<I>;
530 constexpr unsigned bits = limits::digits + 1;
531 static_assert( limits::radix == 2 );
532 static_assert( limits::is_integer );
533 static_assert( limits::is_signed );
534 static_assert( bits == N );
547template<
unsigned N>
struct get_int_types :
public ::ugmisc::get_int_types<N> {
549 static constexpr bool _check =
550 check_member_int_types(::ugmisc::get_int_types<N>{});
576template<
unsigned N,
int_sign Sign>
579 ? intfind_::has_member_type_unsigned_type< intfind_::get_int_types<N> >
580 : intfind_::has_member_type_signed_type< intfind_::get_int_types<N> >
620template<
unsigned N,
int_sign S>
struct get_exact_int_type;
624struct get_exact_int_type<N, UNSIGNED> {
629struct get_exact_int_type<N, SIGNED> {
643 typename intfind_::get_exact_int_type<N, S>::type;
654#ifdef UGMISC_TEST_INT_FINDER_INNER_LOOP_MAX
655static constexpr unsigned find_inner_loop_max = UGMISC_TEST_INT_FINDER_INNER_LOOP_MAX;
657static constexpr unsigned find_inner_loop_max = 100;
669template<
unsigned Min,
int_sign Sign,
unsigned Max>
670constexpr auto find_type_100() {
671 static_assert( Max - Min < find_inner_loop_max );
673 static_assert( Min <= Max );
674 static_assert( Sign == UNSIGNED || Sign == SIGNED );
678 }
else if constexpr ( Min == Max ) {
681 return find_type_100<Min+1, Sign, Max>();
700template<
unsigned Min,
int_sign Sign,
unsigned Max = max_
int_w
idth>
701constexpr auto find_type() {
703 static_assert(Max >= Min);
704 static_assert( Sign == UNSIGNED || Sign == SIGNED );
706 constexpr unsigned near_max = std::min(Min+find_inner_loop_max-1, Max);
707 constexpr bool last_iter = near_max == Max;
709 if constexpr ( find_type_100<Min, Sign, near_max>() ) {
710 return find_type_100<Min, Sign, near_max>();
711 }
else if constexpr ( last_iter ) {
714 return find_type<near_max+1, Sign, Max>();
734template<
unsigned N,
int_sign S>
737 intfind_::find_type<N, S>(),
738 decltype(intfind_::find_type<N, S>())
777template<
bool IncludingSignBit,
class T >
780 constexpr bool use_sign_bit = std::is_signed_v<T> && IncludingSignBit;
781 constexpr unsigned extra_bits = use_sign_bit ? 1 : 0;
782 const U w = v >= 0 ? (U)v : ~(U)v;
796struct range_result_compare {
798 range_result_compare(
const range_result_compare&) =
default;
799 constexpr range_result_compare(T v) : value(v) {}
800 constexpr bool valid()
const {
return value; }
801 constexpr unsigned size()
const {
return sizeof(T); }
802 constexpr unsigned used_bits()
const {
803 unsigned sign_bit = std::numeric_limits<T>::is_signed ? 1 : 0;
804 return std::numeric_limits<T>::digits + sign_bit;
815template<
class T,
class U>
816constexpr bool operator < (range_result_compare<T> a, range_result_compare<U> b) {
817 if ( !a.valid() ) {
return false; }
818 if ( !b.valid() ) {
return true; }
819 if ( a.size() < b.size() ) {
return true; }
820 if ( a.size() > b.size() ) {
return false; }
821 return a.used_bits() < b.used_bits();
827template<
auto V,
auto...SelectArgs>
828constexpr auto as_least_required_type() {
829 constexpr auto S = intfind_::combine_flags_opts<SelectArgs...>();
830 constexpr int_sign pref_sign = sign(S, V);
831 constexpr int_sign other_sign = flip(pref_sign);
832 constexpr int_sign value_sign = V < 0 ? SIGNED : UNSIGNED;
834 constexpr bool may_be_unsigned =
835 value_sign == UNSIGNED
836 || allow_value_sign_loss(S)
839 constexpr unsigned value_bits = signed_bitwidth<true>(V);
840 constexpr bool choose_smaller = smallest(S);
845 constexpr auto pref_value =
846 intfind_::find_type<value_bits, pref_sign>();
847 constexpr auto other_value =
848 intfind_::find_type<value_bits, other_sign>();
850 using pref_t =
decltype(pref_value);
851 using other_t =
decltype(other_value);
853 constexpr bool pref_valid = pref_value && (std::numeric_limits<pref_t>::is_signed || may_be_unsigned);
859 constexpr bool other_valid = other_value && (std::numeric_limits<other_t>::is_signed || may_be_unsigned) && choose_smaller;
862 pref_valid || other_valid,
863 "least_required_type could not find a type that satisfies all "
868 constexpr bool use_pref = [=]() ->
bool {
873 if constexpr ( ! other_valid ) {
876 }
else if constexpr ( ! pref_valid ) {
879 }
else if constexpr (
880 choose_smaller && other_valid
882 range_result_compare{other_value} < range_result_compare{pref_value}
894 if constexpr ( use_pref ) {
897 "This is Larry's fault."
898 " An earlier static assertion should have failed already."
900 return static_cast<pref_t
>(V);
904 "This is Larry's fault."
905 " An earlier static assertion should have failed already."
907 return static_cast<other_t
>(V);
935template<
auto V,
auto...S>
937 intfind_::as_least_required_type<V, S...>();
950template<
auto V,
auto...S>
Provides constexpr bit counting functions.
constexpr bitwise_uint_only< T > bitwidth(T) noexcept
Definition bitops.hpp:235
sign_opt
Definition int_finder.hpp:102
@ SELECT_VALUE_SIGN
Select a signed type iff the value is negative.
Definition int_finder.hpp:108
@ SELECT_TYPE_SIGN
Definition int_finder.hpp:106
typename intfind_::get_exact_int_type< N, S >::type exact_int_type
Definition int_finder.hpp:642
std::enable_if_t< intfind_::find_type< N, S >(), decltype(intfind_::find_type< N, S >()) > least_int_type
Definition int_finder.hpp:735
constexpr bool has_unsigned_type
Definition int_finder.hpp:585
decltype(as_least_required_int_type< V, S... >) least_required_int_type
Definition int_finder.hpp:951
#define UGMISC_INT_FINDER_MAX_INT_WIDTH
Definition int_finder.hpp:404
constexpr bool has_either_int_type
Definition int_finder.hpp:596
sign_flag
Definition int_finder.hpp:156
@ SELECT_SIGN_ALLOW_VALUE_SIGN_LOSS
Definition int_finder.hpp:174
@ SELECT_SIGN_SMALLEST
Definition int_finder.hpp:167
least_int_type< N, UNSIGNED > least_unsigned_type
Definition int_finder.hpp:746
typename intfind_::get_int_types< N >::signed_type exact_signed_type
Definition int_finder.hpp:606
least_int_type< N, SIGNED > least_signed_type
Definition int_finder.hpp:753
typename intfind_::get_int_types< N >::unsigned_type exact_unsigned_type
Definition int_finder.hpp:603
constexpr int_only< T, unsigned > signed_bitwidth(T v)
Definition int_finder.hpp:778
int_sign
Used to select signed or unsigned types.
Definition int_finder.hpp:81
constexpr auto as_least_required_int_type
Definition int_finder.hpp:936
constexpr bool has_int_type
Definition int_finder.hpp:577
constexpr bool has_both_int_types
Definition int_finder.hpp:592
constexpr bool has_signed_type
Definition int_finder.hpp:589
constexpr unsigned max_int_width
Definition int_finder.hpp:411
Macros which declare templates for testing named members of types.
#define UGMISC_MEMBER_TYPE_TEST(NAME)
Definition member.hpp:340
#define UGMISC_QQ(...)
Definition quote.hpp:52
std::enable_if_t< std::numeric_limits< T >::is_integer, R > int_only
Definition sfinae_helpers.hpp:157
Definition int_finder.hpp:422