Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:42

0001 // (C) Copyright David Abrahams 2001, Howard Hinnant 2001.
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See
0004 // accompanying file LICENSE_1_0.txt or copy at
0005 // http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Template class numeric_traits<Number> --
0008 //
0009 //    Supplies:
0010 //
0011 //      typedef difference_type -- a type used to represent the difference
0012 //      between any two values of Number.
0013 //
0014 //    Support:
0015 //      1. Not all specializations are supplied
0016 //
0017 //      2. Use of specializations that are not supplied will cause a
0018 //      compile-time error
0019 //
0020 //      3. Users are free to specialize numeric_traits for any type.
0021 //
0022 //      4. Right now, specializations are only supplied for integer types.
0023 //
0024 //      5. On implementations which do not supply compile-time constants in
0025 //      std::numeric_limits<>, only specializations for built-in integer types
0026 //      are supplied.
0027 //
0028 //      6. Handling of numbers whose range of representation is at least as
0029 //      great as boost::intmax_t can cause some differences to be
0030 //      unrepresentable in difference_type:
0031 //
0032 //        Number    difference_type
0033 //        ------    ---------------
0034 //        signed    Number
0035 //        unsigned  intmax_t
0036 //
0037 // template <class Number> typename numeric_traits<Number>::difference_type
0038 // numeric_distance(Number x, Number y)
0039 //    computes (y - x), attempting to avoid overflows.
0040 //
0041 
0042 // See http://www.boost.org for most recent version including documentation.
0043 
0044 // Revision History
0045 // 11 Feb 2001 - Use BOOST_STATIC_CONSTANT (David Abrahams)
0046 // 11 Feb 2001 - Rolled back ineffective Borland-specific code
0047 //               (David Abrahams)
0048 // 10 Feb 2001 - Rolled in supposed Borland fixes from John Maddock, but
0049 //               not seeing any improvement yet (David Abrahams)
0050 // 06 Feb 2001 - Factored if_true out into boost/detail/select_type.hpp
0051 //               (David Abrahams)
0052 // 23 Jan 2001 - Fixed logic of difference_type selection, which was
0053 //               completely wack. In the process, added digit_traits<>
0054 //               to compute the number of digits in intmax_t even when
0055 //               not supplied by numeric_limits<>. (David Abrahams)
0056 // 21 Jan 2001 - Created (David Abrahams)
0057 
0058 #ifndef BOOST_NUMERIC_TRAITS_HPP_DWA20001901
0059 #define BOOST_NUMERIC_TRAITS_HPP_DWA20001901
0060 
0061 #include <cstddef>
0062 #include <boost/config.hpp>
0063 #include <boost/limits.hpp>
0064 #include <boost/cstdint.hpp>
0065 #include <boost/type_traits/is_signed.hpp>
0066 #include <boost/type_traits/conditional.hpp>
0067 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
0068 #include <boost/static_assert.hpp>
0069 #include <boost/type_traits/is_integral.hpp>
0070 #endif
0071 
0072 namespace boost { namespace detail {
0073 
0074 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
0075   // digit_traits - compute the number of digits in a built-in integer
0076   // type. Needed for implementations on which numeric_limits is not specialized
0077   // for some integer types, like __int128 in libstdc++ (gcc).
0078   template <class T, bool IsSpecialized = std::numeric_limits<T>::is_specialized>
0079   struct digit_traits
0080   {
0081       BOOST_STATIC_CONSTANT(int, digits = std::numeric_limits<T>::digits);
0082   };
0083 
0084   // numeric_limits is not specialized; compute digits from sizeof(T)
0085   template <class T>
0086   struct digit_traits<T, false>
0087   {
0088       BOOST_STATIC_CONSTANT(int, digits = (
0089           sizeof(T) * std::numeric_limits<unsigned char>::digits
0090           - (boost::is_signed<T>::value ? 1 : 0))
0091           );
0092   };
0093 #endif
0094 
0095   // Template class integer_traits<Integer> -- traits of various integer types
0096   // This should probably be rolled into boost::integer_traits one day, but I
0097   // need it to work without <limits>
0098   template <class Integer>
0099   struct integer_traits
0100   {
0101 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
0102    private:
0103       typedef Integer integer_type;
0104       typedef std::numeric_limits<integer_type> x;
0105    public:
0106       typedef typename boost::conditional<
0107         (int(x::is_signed)
0108           && (!int(x::is_bounded)
0109              // digits is the number of no-sign bits
0110              || (int(x::digits) + 1 >= digit_traits<boost::intmax_t>::digits))),
0111         Integer,
0112 
0113         typename boost::conditional<
0114           (int(x::digits) + 1 < digit_traits<signed int>::digits),
0115           signed int,
0116 
0117           typename boost::conditional<
0118             (int(x::digits) + 1 < digit_traits<signed long>::digits),
0119             signed long,
0120             boost::intmax_t
0121           >::type
0122         >::type
0123       >::type difference_type;
0124 #else
0125       BOOST_STATIC_ASSERT(boost::is_integral<Integer>::value);
0126 
0127       typedef typename boost::conditional<
0128         (sizeof(Integer) >= sizeof(intmax_t)),
0129 
0130         boost::conditional<
0131           (boost::is_signed<Integer>::value),
0132           Integer,
0133           boost::intmax_t
0134         >,
0135 
0136         boost::conditional<
0137           (sizeof(Integer) < sizeof(std::ptrdiff_t)),
0138           std::ptrdiff_t,
0139           boost::intmax_t
0140         >
0141       >::type::type difference_type;
0142 #endif
0143   };
0144 
0145   // Right now, only supports integers, but should be expanded.
0146   template <class Number>
0147   struct numeric_traits
0148   {
0149       typedef typename integer_traits<Number>::difference_type difference_type;
0150   };
0151 
0152   template <class Number>
0153   inline BOOST_CONSTEXPR typename numeric_traits<Number>::difference_type numeric_distance(Number x, Number y)
0154   {
0155       typedef typename numeric_traits<Number>::difference_type difference_type;
0156       return difference_type(y) - difference_type(x);
0157   }
0158 }}
0159 
0160 #endif // BOOST_NUMERIC_TRAITS_HPP_DWA20001901