numeric_traits.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // (C) Copyright David Abrahams 2001, Howard Hinnant 2001.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Template class numeric_traits<Number> --
  8. //
  9. // Supplies:
  10. //
  11. // typedef difference_type -- a type used to represent the difference
  12. // between any two values of Number.
  13. //
  14. // Support:
  15. // 1. Not all specializations are supplied
  16. //
  17. // 2. Use of specializations that are not supplied will cause a
  18. // compile-time error
  19. //
  20. // 3. Users are free to specialize numeric_traits for any type.
  21. //
  22. // 4. Right now, specializations are only supplied for integer types.
  23. //
  24. // 5. On implementations which do not supply compile-time constants in
  25. // std::numeric_limits<>, only specializations for built-in integer types
  26. // are supplied.
  27. //
  28. // 6. Handling of numbers whose range of representation is at least as
  29. // great as boost::intmax_t can cause some differences to be
  30. // unrepresentable in difference_type:
  31. //
  32. // Number difference_type
  33. // ------ ---------------
  34. // signed Number
  35. // unsigned intmax_t
  36. //
  37. // template <class Number> typename numeric_traits<Number>::difference_type
  38. // numeric_distance(Number x, Number y)
  39. // computes (y - x), attempting to avoid overflows.
  40. //
  41. // See http://www.boost.org for most recent version including documentation.
  42. // Revision History
  43. // 11 Feb 2001 - Use BOOST_STATIC_CONSTANT (David Abrahams)
  44. // 11 Feb 2001 - Rolled back ineffective Borland-specific code
  45. // (David Abrahams)
  46. // 10 Feb 2001 - Rolled in supposed Borland fixes from John Maddock, but
  47. // not seeing any improvement yet (David Abrahams)
  48. // 06 Feb 2001 - Factored if_true out into boost/detail/select_type.hpp
  49. // (David Abrahams)
  50. // 23 Jan 2001 - Fixed logic of difference_type selection, which was
  51. // completely wack. In the process, added digit_traits<>
  52. // to compute the number of digits in intmax_t even when
  53. // not supplied by numeric_limits<>. (David Abrahams)
  54. // 21 Jan 2001 - Created (David Abrahams)
  55. #ifndef BOOST_NUMERIC_TRAITS_HPP_DWA20001901
  56. #define BOOST_NUMERIC_TRAITS_HPP_DWA20001901
  57. #include <cstddef>
  58. #include <boost/config.hpp>
  59. #include <boost/limits.hpp>
  60. #include <boost/cstdint.hpp>
  61. #include <boost/type_traits/is_signed.hpp>
  62. #include <boost/type_traits/conditional.hpp>
  63. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  64. #include <boost/static_assert.hpp>
  65. #include <boost/type_traits/is_integral.hpp>
  66. #endif
  67. namespace boost { namespace detail {
  68. #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  69. // digit_traits - compute the number of digits in a built-in integer
  70. // type. Needed for implementations on which numeric_limits is not specialized
  71. // for some integer types, like __int128 in libstdc++ (gcc).
  72. template <class T, bool IsSpecialized = std::numeric_limits<T>::is_specialized>
  73. struct digit_traits
  74. {
  75. BOOST_STATIC_CONSTANT(int, digits = std::numeric_limits<T>::digits);
  76. };
  77. // numeric_limits is not specialized; compute digits from sizeof(T)
  78. template <class T>
  79. struct digit_traits<T, false>
  80. {
  81. BOOST_STATIC_CONSTANT(int, digits = (
  82. sizeof(T) * std::numeric_limits<unsigned char>::digits
  83. - (boost::is_signed<T>::value ? 1 : 0))
  84. );
  85. };
  86. #endif
  87. // Template class integer_traits<Integer> -- traits of various integer types
  88. // This should probably be rolled into boost::integer_traits one day, but I
  89. // need it to work without <limits>
  90. template <class Integer>
  91. struct integer_traits
  92. {
  93. #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  94. private:
  95. typedef Integer integer_type;
  96. typedef std::numeric_limits<integer_type> x;
  97. public:
  98. typedef typename boost::conditional<
  99. (int(x::is_signed)
  100. && (!int(x::is_bounded)
  101. // digits is the number of no-sign bits
  102. || (int(x::digits) + 1 >= digit_traits<boost::intmax_t>::digits))),
  103. Integer,
  104. typename boost::conditional<
  105. (int(x::digits) + 1 < digit_traits<signed int>::digits),
  106. signed int,
  107. typename boost::conditional<
  108. (int(x::digits) + 1 < digit_traits<signed long>::digits),
  109. signed long,
  110. boost::intmax_t
  111. >::type
  112. >::type
  113. >::type difference_type;
  114. #else
  115. BOOST_STATIC_ASSERT(boost::is_integral<Integer>::value);
  116. typedef typename boost::conditional<
  117. (sizeof(Integer) >= sizeof(intmax_t)),
  118. boost::conditional<
  119. (boost::is_signed<Integer>::value),
  120. Integer,
  121. boost::intmax_t
  122. >,
  123. boost::conditional<
  124. (sizeof(Integer) < sizeof(std::ptrdiff_t)),
  125. std::ptrdiff_t,
  126. boost::intmax_t
  127. >
  128. >::type::type difference_type;
  129. #endif
  130. };
  131. // Right now, only supports integers, but should be expanded.
  132. template <class Number>
  133. struct numeric_traits
  134. {
  135. typedef typename integer_traits<Number>::difference_type difference_type;
  136. };
  137. template <class Number>
  138. inline BOOST_CONSTEXPR typename numeric_traits<Number>::difference_type numeric_distance(Number x, Number y)
  139. {
  140. typedef typename numeric_traits<Number>::difference_type difference_type;
  141. return difference_type(y) - difference_type(x);
  142. }
  143. }}
  144. #endif // BOOST_NUMERIC_TRAITS_HPP_DWA20001901