Obtain an integer type from its width, or from some values that must be representable. More...
#include <cstdint>#include <limits>#include <string_view>#include <type_traits>#include "ugmisc/bitops.hpp"#include "ugmisc/features.hpp"#include "ugmisc/member.hpp"#include "ugmisc/quote.hpp"#include "ugmisc/sfinae_helpers.hpp"#include "ugmisc/templated_callable_loop.hpp"#include "ugmisc/typelist.hpp"Go to the source code of this file.
Classes | |
| struct | ugmisc::get_int_types< N > |
| class | ugmisc::least_required_int_type_finder< V1 > |
Macros | |
| #define | UGMISC_INT_FINDER_MAX_INT_WIDTH (1U << 12); |
Typedefs | |
| template<unsigned N> | |
| using | ugmisc::exact_unsigned_type = typename intfind_::get_int_types<N>::unsigned_type |
| template<unsigned N> | |
| using | ugmisc::exact_signed_type = typename intfind_::get_int_types<N>::signed_type |
| template<unsigned N, int_sign S> | |
| using | ugmisc::exact_int_type |
| template<unsigned N, int_sign Sign> | |
| using | ugmisc::least_int_type |
| template<unsigned N> | |
| using | ugmisc::least_unsigned_type = least_int_type<N, UNSIGNED> |
| template<unsigned N> | |
| using | ugmisc::least_signed_type = least_int_type<N, SIGNED> |
| template<auto... V> | |
| using | ugmisc::least_required_int_type |
| template<auto... V> | |
| using | ugmisc::least_required_int_type_multi = least_required_int_type<V...> |
| template<auto... V> | |
| using | ugmisc::least_required_int_std_sequence |
| template<auto... V> | |
| using | ugmisc::least_required_int_type_list |
Enumerations | |
| enum | ugmisc::int_sign { INVALID_SIGN = 0 , UNSIGNED = 1 , SIGNED = 2 } |
| Used to select signed or unsigned types. More... | |
| enum | ugmisc::sign_opt : std::uint8_t { SELECT_SIGN_OPTION_NONE = 0 , ugmisc::SELECT_UNSIGNED = 1 , ugmisc::SELECT_SIGNED = 2 , ugmisc::SELECT_TYPE_SIGN = 3 , ugmisc::SELECT_VALUE_SIGN = 4 , SELECT_SIGN_OPTION_MAX = SELECT_VALUE_SIGN , SELECT_SIGN_OPTION_MASK = 7 } |
| enum | ugmisc::sign_flag : std::uint8_t { SELECT_SIGN_FLAGS_NONE , ugmisc::SELECT_SIGN_SMALLEST = 1 << 3 , ugmisc::SELECT_SIGN_ALLOW_VALUE_SIGN_LOSS = 1 << 4 , SELECT_SIGN_FLAGS_MASK = SELECT_SIGN_SMALLEST|SELECT_SIGN_ALLOW_VALUE_SIGN_LOSS } |
Functions | |
| constexpr int_sign | ugmisc::flip (int_sign s) |
| std::string_view | ugmisc::get_name (sign_opt opt) |
| template<class T> | |
| constexpr int_sign | ugmisc::sign (sign_opt opt, T v) |
| constexpr sign_flag | ugmisc::operator~ (sign_flag f) |
| constexpr sign_flag | ugmisc::operator| (sign_flag a, sign_flag b) |
| constexpr bool | ugmisc::smallest (sign_flag s) |
| constexpr bool | ugmisc::allow_value_sign_loss (sign_flag s) |
| std::string_view | ugmisc::get_name (sign_flag flags) |
| template<bool IncludingSignBit, Int T> | |
| constexpr unsigned | ugmisc::signed_bitwidth (T v) |
| template<auto... V> | |
| constexpr auto | ugmisc::to_least_required_int_type () |
Variables | |
| template<auto... FlagOpts> | |
| constexpr auto | ugmisc::combined = intfind_::combine_flags_opts<FlagOpts...>() |
| constexpr unsigned | ugmisc::max_int_width = UGMISC_INT_FINDER_MAX_INT_WIDTH |
| template<unsigned N, int_sign Sign> | |
| constexpr bool | ugmisc::has_int_type |
| template<unsigned N> | |
| constexpr bool | ugmisc::has_unsigned_type |
| template<unsigned N> | |
| constexpr bool | ugmisc::has_signed_type = has_int_type<N, SIGNED> |
| template<unsigned N> | |
| constexpr bool | ugmisc::has_both_int_types |
| template<unsigned N> | |
| constexpr bool | ugmisc::has_either_int_type |
| template<unsigned N, int_sign Sign> | |
| constexpr bool | ugmisc::has_sufficient_int_type |
| template<auto... V> | |
| constexpr auto | ugmisc::as_least_required_int_type |
Obtain an integer type from its width, or from some values that must be representable.
To support non-standard ints (like a 128 bit int), just specialise ugmisc::get_int_types.
It has to have a member called "unsigned_type" and a member called "signed_type", if both are supported. You may provide just one of them if only the signed or unsigned type exists.
Using the ugmisc::exact_int_type template type alias, a type of a required number of bits and signedness can be obtained, if it exists, including types the user added by specialising ugmisc::get_int_types.
There are boolean template variables to determine if a type exists:
The ugmisc::least_int_type template type alias can be used to find a signed or unsigned integral type with at least a specified number of bits.
It is possible to find the smallest integral type which can encode all of a list of specified numbers, using ugmisc::least_required_int_type, or convert a single value to its selected type using the template variable ugmisc::as_least_required_int_type, or the template function ugmisc::to_least_required_int_type.
The numbers can be converted into a std::integer_sequence or a ugmisc::typelist containing std::integral_constant types, using ugmisc::least_required_int_std_sequence and ugmisc::least_required_int_type_list respectively.
Definition in file int_finder.hpp.
| #define UGMISC_INT_FINDER_MAX_INT_WIDTH (1U << 12); |
This may be predefined for testing purposes, but should normally be left alone.
Definition at line 467 of file int_finder.hpp.
| using ugmisc::exact_int_type |
An integer type with exactly N bits.
Definition at line 674 of file int_finder.hpp.
| using ugmisc::exact_signed_type = typename intfind_::get_int_types<N>::signed_type |
Definition at line 638 of file int_finder.hpp.
| using ugmisc::exact_unsigned_type = typename intfind_::get_int_types<N>::unsigned_type |
Definition at line 635 of file int_finder.hpp.
| using ugmisc::least_int_type |
A type with at least N bits.
Definition at line 727 of file int_finder.hpp.
| using ugmisc::least_required_int_std_sequence |
Finds the type T = ugmisc::least_required_int_type<V...>, then returns a std::integer_sequence in which T is the integer type used, and the values are all of the V parameters which are integer values (rather than options and flags), cast to T.
Definition at line 1273 of file int_finder.hpp.
| using ugmisc::least_required_int_type |
An integer type with enough bits to represent all the values in V.
| V | Includes values that must be representable, and the options and flags which determines how the type should be selected. This should include one ugmisc::sign_opt parameter and optionally some ugmisc::sign_flag parameters. sign_flag parameters may be provided separately or combined using the bitwise or operator. |
Definition at line 1254 of file int_finder.hpp.
| using ugmisc::least_required_int_type_list |
Finds the type T = ugmisc::least_required_int_type<V...>, then returns a ugmisc::type_list in which each type is a std::integral_constant<T, (T)value>, and each value is a V parameter which is an integer value (rather than an option or flag).
Definition at line 1285 of file int_finder.hpp.
| using ugmisc::least_required_int_type_multi = least_required_int_type<V...> |
Definition at line 1263 of file int_finder.hpp.
| using ugmisc::least_signed_type = least_int_type<N, SIGNED> |
Definition at line 754 of file int_finder.hpp.
| using ugmisc::least_unsigned_type = least_int_type<N, UNSIGNED> |
Definition at line 747 of file int_finder.hpp.
| enum ugmisc::int_sign |
Used to select signed or unsigned types.
Definition at line 72 of file int_finder.hpp.
| enum ugmisc::sign_flag : std::uint8_t |
Definition at line 157 of file int_finder.hpp.
| enum ugmisc::sign_opt : std::uint8_t |
Used to select signed or unsigned types based on values.
Definition at line 89 of file int_finder.hpp.
|
constexpr |
Definition at line 196 of file int_finder.hpp.
|
constexpr |
Definition at line 77 of file int_finder.hpp.
|
inline |
Definition at line 208 of file int_finder.hpp.
|
inline |
This function is not declared constexpr because there wouldn't be much point while the other overload, ugmisc::get_name(sign_flag), is not allowed to be constexpr.
Definition at line 115 of file int_finder.hpp.
Definition at line 186 of file int_finder.hpp.
|
constexpr |
Definition at line 181 of file int_finder.hpp.
|
constexpr |
Definition at line 137 of file int_finder.hpp.
|
constexpr |
Like ugmisc::bitwidth but also allows signed integer parameters.
This is useful for defining ugmisc::least_required_int_type, but is considered part of the public interface because it may be generally useful.
| IncludingSignBit | If true, and T is a signed type, the returned value will be one larger, to account for the space needed for a sign bit. Otherwise only the bits needed to distinguish one magnitude from another will be kept. |
Definition at line 780 of file int_finder.hpp.
|
constexpr |
Definition at line 191 of file int_finder.hpp.
|
constexpr |
| v | Exactly one integral value and at least one ugmisc::sign_opt, plus any number of ugmisc::sign_flag values. |
Definition at line 1220 of file int_finder.hpp.
|
inlineconstexpr |
Definition at line 1235 of file int_finder.hpp.
|
constexpr |
Combine ugmisc::sign_flag and ugmisc::sign_opt parameters into a single value which can be passed in their place.
Definition at line 457 of file int_finder.hpp.
|
inlineconstexpr |
|
inlineconstexpr |
Definition at line 628 of file int_finder.hpp.
|
inlineconstexpr |
Find out if a certain integral type is supported.
| N | Number of bits. |
| Sign | ugmisc::UNSIGNED or ugmisc::SIGNED. |
Support for an integral type is provided by specialising ugmisc::get_int_types. The standard 8, 16, 32, and 64 bit signed and unsigned types have support built in, but the user may add more.
| true | The type is supported. |
| false | The type is not supported. |
Definition at line 609 of file int_finder.hpp.
|
inlineconstexpr |
Definition at line 621 of file int_finder.hpp.
|
constexpr |
True if at least one int type of at least N bits and the requeseted sign exists.
Definition at line 739 of file int_finder.hpp.
|
inlineconstexpr |
|
inlineconstexpr |
This is usually 4096 bits. If you need more than that (or less, for testing) the macro UGMISC_INT_FINDER_MAX_INT_WIDTH may be predefined before including ugmisc/int_finder.hpp.
Definition at line 474 of file int_finder.hpp.