123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #ifndef BOOST_NUMERIC_CPP_HPP
- #define BOOST_NUMERIC_CPP_HPP
- #include <type_traits> // integral constant, remove_cv, conditional
- #include <limits>
- #include <boost/integer.hpp> // integer type selection
- #include "safe_common.hpp"
- #include "checked_result.hpp"
- namespace boost {
- namespace safe_numerics {
- template<
- int CharBits,
- int ShortBits,
- int IntBits,
- int LongBits,
- int LongLongBits
- >
- struct cpp {
- public:
- using local_char_type = typename boost::int_t<CharBits>::exact;
- using local_short_type = typename boost::int_t<ShortBits>::exact;
- using local_int_type = typename boost::int_t<IntBits>::exact;
- using local_long_type = typename boost::int_t<LongBits>::exact;
- using local_long_long_type = typename boost::int_t<LongLongBits>::exact;
- template<class T>
- using rank =
- typename std::conditional<
- std::is_same<local_char_type, typename std::make_signed<T>::type>::value,
- std::integral_constant<int, 1>,
- typename std::conditional<
- std::is_same<local_short_type, typename std::make_signed<T>::type>::value,
- std::integral_constant<int, 2>,
- typename std::conditional<
- std::is_same<local_int_type, typename std::make_signed<T>::type>::value,
- std::integral_constant<int, 3>,
- typename std::conditional<
- std::is_same<local_long_type, typename std::make_signed<T>::type>::value,
- std::integral_constant<int, 4>,
- typename std::conditional<
- std::is_same<local_long_long_type, typename std::make_signed<T>::type>::value,
- std::integral_constant<int, 5>,
- std::integral_constant<int, 6>
- >::type >::type >::type >::type >::type;
-
-
- template<class T, class U>
- using higher_ranked_type = typename std::conditional<
- (rank<T>::value < rank<U>::value),
- U,
- T
- >::type;
- template<class T, class U>
- using copy_sign = typename std::conditional<
- std::is_signed<U>::value,
- typename std::make_signed<T>::type,
- typename std::make_unsigned<T>::type
- >::type;
- template<class T>
- using integral_promotion = copy_sign<
- higher_ranked_type<local_int_type, T>,
- T
- >;
-
-
- template<class T, class U>
- using select_signed = typename std::conditional<
- std::numeric_limits<T>::is_signed,
- T,
- U
- >::type;
- template<class T, class U>
- using select_unsigned = typename std::conditional<
- std::numeric_limits<T>::is_signed,
- U,
- T
- >::type;
-
- template<typename T, typename U>
- using usual_arithmetic_conversions =
-
- typename std::conditional<
- std::is_same<T, U>::value,
-
- T,
-
- typename std::conditional<
- std::numeric_limits<T>::is_signed
- == std::numeric_limits<U>::is_signed,
-
- higher_ranked_type<T, U>,
-
-
- typename std::conditional<
- rank<select_unsigned<T, U>>::value
- >= rank< select_signed<T, U>>::value,
-
- select_unsigned<T, U>,
-
-
- typename std::conditional<
- std::numeric_limits< select_signed<T, U>>::digits >=
- std::numeric_limits< select_unsigned<T, U>>::digits,
-
- select_signed<T, U>,
-
- std::make_signed< select_signed<T, U>>
- >::type >::type >::type
- >;
- template<typename T, typename U>
- using result_type = typename usual_arithmetic_conversions<
- integral_promotion<typename base_type<T>::type>,
- integral_promotion<typename base_type<U>::type>
- >::type;
- public:
- template<typename T, typename U>
- struct addition_result {
- using type = result_type<T, U>;
- };
- template<typename T, typename U>
- struct subtraction_result {
- using type = result_type<T, U>;
- };
- template<typename T, typename U>
- struct multiplication_result {
- using type = result_type<T, U>;
- };
- template<typename T, typename U>
- struct division_result {
- using type = result_type<T, U>;
- };
- template<typename T, typename U>
- struct modulus_result {
- using type = result_type<T, U>;
- };
-
-
-
- template<typename T, typename U>
- struct comparison_result {
- using type = result_type<T, U>;
- };
- template<typename T, typename U>
- struct left_shift_result {
- using type = result_type<T, U>;
- };
- template<typename T, typename U>
- struct right_shift_result {
- using type = result_type<T, U>;
- };
- template<typename T, typename U>
- struct bitwise_and_result {
- using type = result_type<T, U>;
- };
- template<typename T, typename U>
- struct bitwise_or_result {
- using type = result_type<T, U>;
- };
- template<typename T, typename U>
- struct bitwise_xor_result {
- using type = result_type<T, U>;
- };
- };
- }
- }
- #endif
|