123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- #ifndef BOOST_GEOMETRY_UTIL_PROMOTE_INTEGRAL_HPP
- #define BOOST_GEOMETRY_UTIL_PROMOTE_INTEGRAL_HPP
- #define BOOST_GEOMETRY_NO_MULTIPRECISION_INTEGER
- #include <climits>
- #include <cstddef>
- #include <type_traits>
- #if !defined(BOOST_GEOMETRY_NO_MULTIPRECISION_INTEGER)
- #include <boost/multiprecision/cpp_int.hpp>
- #endif
- namespace boost { namespace geometry
- {
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail { namespace promote_integral
- {
- template
- <
- typename T,
- bool IsFundamental = std::is_fundamental<T>::value
- >
- struct bit_size
- {};
- template <typename T>
- struct bit_size<T, true>
- : std::integral_constant<std::size_t, (CHAR_BIT * sizeof(T))>
- {};
- #if !defined(BOOST_GEOMETRY_NO_MULTIPRECISION_INTEGER)
- template
- <
- unsigned MinSize,
- unsigned MaxSize,
- boost::multiprecision::cpp_integer_type SignType,
- boost::multiprecision::cpp_int_check_type Checked,
- typename Allocator,
- boost::multiprecision::expression_template_option ExpressionTemplates
- >
- struct bit_size
- <
- boost::multiprecision::number
- <
- boost::multiprecision::cpp_int_backend
- <
- MinSize, MaxSize, SignType, Checked, Allocator
- >,
- ExpressionTemplates
- >,
- false
- >
- : std::integral_constant<std::size_t, MaxSize>
- {};
- #endif
- template <typename T, std::size_t MinSize, typename ...Ts>
- struct promote_to_larger
- {
-
-
- typedef T type;
- };
- template <typename T, std::size_t MinSize, typename CurrentT, typename ...Ts>
- struct promote_to_larger<T, MinSize, CurrentT, Ts...>
- {
- typedef std::conditional_t
- <
- (bit_size<CurrentT>::value >= MinSize),
- CurrentT,
- typename promote_to_larger<T, MinSize, Ts...>::type
- > type;
- };
- template <typename ...Ts>
- struct integral_types {};
- template <typename T, std::size_t MinSize, typename ...Ts>
- struct promote_to_larger<T, MinSize, integral_types<Ts...>>
- : promote_to_larger<T, MinSize, Ts...>
- {};
- }}
- #endif
- template
- <
- typename T,
- bool PromoteUnsignedToUnsigned = false,
- bool UseCheckedInteger = false,
- bool IsIntegral = std::is_integral<T>::value
- >
- class promote_integral
- {
- private:
- static bool const is_unsigned = std::is_unsigned<T>::value;
- typedef detail::promote_integral::bit_size<T> bit_size_type;
- #if !defined(BOOST_GEOMETRY_NO_MULTIPRECISION_INTEGER)
-
- typedef std::conditional_t
- <
- UseCheckedInteger,
- std::integral_constant
- <
- boost::multiprecision::cpp_int_check_type,
- boost::multiprecision::checked
- >,
- std::integral_constant
- <
- boost::multiprecision::cpp_int_check_type,
- boost::multiprecision::unchecked
- >
- > check_policy_type;
-
-
- template
- <
- unsigned int Size,
- boost::multiprecision::cpp_integer_type SignType
- >
- struct multiprecision_integer_type
- {
- typedef boost::multiprecision::number
- <
- boost::multiprecision::cpp_int_backend
- <
- Size,
- Size,
- SignType,
- check_policy_type::value,
- void
- >
- > type;
- };
- #endif
-
-
-
-
-
-
-
- typedef std::conditional_t
- <
- (PromoteUnsignedToUnsigned && is_unsigned),
- std::integral_constant<std::size_t, (2 * bit_size_type::value)>,
- std::conditional_t
- <
- is_unsigned,
- std::integral_constant<std::size_t, (2 * bit_size_type::value + 1)>,
- std::integral_constant<std::size_t, (2 * bit_size_type::value - 1)>
- >
- > min_bit_size_type;
-
-
- typedef detail::promote_integral::integral_types
- <
- short,
- int,
- long,
- long long
- #if defined(BOOST_HAS_INT128) && defined(BOOST_GEOMETRY_ENABLE_INT128)
- , boost::int128_type
- #endif
- #if !defined(BOOST_GEOMETRY_NO_MULTIPRECISION_INTEGER)
- , typename multiprecision_integer_type
- <
- min_bit_size_type::value,
- boost::multiprecision::signed_magnitude
- >::type
- #endif
- > signed_integral_types;
-
-
- typedef detail::promote_integral::integral_types
- <
- unsigned short,
- unsigned int,
- unsigned long,
- std::size_t,
- unsigned long long
- #if defined(BOOST_HAS_INT128) && defined(BOOST_GEOMETRY_ENABLE_INT128)
- , boost::uint128_type
- #endif
- #if !defined(BOOST_GEOMETRY_NO_MULTIPRECISION_INTEGER)
- , typename multiprecision_integer_type
- <
- min_bit_size_type::value,
- boost::multiprecision::unsigned_magnitude
- >::type
- #endif
- > unsigned_integral_types;
-
-
-
- typedef std::conditional_t
- <
- (is_unsigned && PromoteUnsignedToUnsigned),
- unsigned_integral_types,
- signed_integral_types
- > integral_types;
- public:
- typedef typename detail::promote_integral::promote_to_larger
- <
- T,
- min_bit_size_type::value,
- integral_types
- >::type type;
- };
- template <typename T, bool PromoteUnsignedToUnsigned, bool UseCheckedInteger>
- class promote_integral
- <
- T, PromoteUnsignedToUnsigned, UseCheckedInteger, false
- >
- {
- public:
- typedef T type;
- };
- }}
- #endif
|