is_subranged.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // (c) Copyright Fernando Luis Cacciola Carballal 2000-2004
  2. // Use, modification, and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See library home page at http://www.boost.org/libs/numeric/conversion
  6. //
  7. // Contact the author at: fernando_cacciola@hotmail.com
  8. //
  9. #ifndef BOOST_NUMERIC_CONVERSION_DETAIL_IS_SUBRANGED_FLC_12NOV2002_HPP
  10. #define BOOST_NUMERIC_CONVERSION_DETAIL_IS_SUBRANGED_FLC_12NOV2002_HPP
  11. #include "boost/config.hpp"
  12. #include "boost/limits.hpp"
  13. #include "boost/mpl/int.hpp"
  14. #include "boost/mpl/multiplies.hpp"
  15. #include "boost/mpl/less.hpp"
  16. #include "boost/mpl/equal_to.hpp"
  17. #include "boost/type_traits/is_same.hpp"
  18. #include "boost/numeric/conversion/detail/meta.hpp"
  19. #include "boost/numeric/conversion/detail/int_float_mixture.hpp"
  20. #include "boost/numeric/conversion/detail/sign_mixture.hpp"
  21. #include "boost/numeric/conversion/detail/udt_builtin_mixture.hpp"
  22. namespace boost { namespace numeric { namespace convdetail
  23. {
  24. //---------------------------------------------------------------
  25. // Implementations of the compile time predicate "T is subranged"
  26. //---------------------------------------------------------------
  27. // for integral to integral conversions
  28. template<class T,class S>
  29. struct subranged_Sig2Unsig
  30. {
  31. // Signed to unsigned conversions are 'subranged' because of possible loose
  32. // of negative values.
  33. typedef mpl::true_ type ;
  34. } ;
  35. // for unsigned integral to signed integral conversions
  36. template<class T,class S>
  37. struct subranged_Unsig2Sig
  38. {
  39. // IMPORTANT NOTE:
  40. //
  41. // This code assumes that signed/unsigned integral values are represented
  42. // such that:
  43. //
  44. // numeric_limits<signed T>::digits + 1 == numeric_limits<unsigned T>::digits
  45. //
  46. // The '+1' is required since numeric_limits<>::digits gives 1 bit less for signed integral types.
  47. //
  48. // This fact is used by the following logic:
  49. //
  50. // if ( (numeric_limits<T>::digits+1) < (2*numeric_limits<S>::digits) )
  51. // then the conversion is subranged.
  52. //
  53. typedef mpl::int_< ::std::numeric_limits<S>::digits > S_digits ;
  54. typedef mpl::int_< ::std::numeric_limits<T>::digits > T_digits ;
  55. // T is signed, so take digits+1
  56. typedef typename T_digits::next u_T_digits ;
  57. typedef mpl::int_<2> Two ;
  58. typedef typename mpl::multiplies<S_digits,Two>::type S_digits_times_2 ;
  59. typedef typename mpl::less<u_T_digits,S_digits_times_2>::type type ;
  60. } ;
  61. // for integral to integral conversions of the same sign.
  62. template<class T,class S>
  63. struct subranged_SameSign
  64. {
  65. // An integral conversion of the same sign is subranged if digits(T) < digits(S).
  66. typedef mpl::int_< ::std::numeric_limits<S>::digits > S_digits ;
  67. typedef mpl::int_< ::std::numeric_limits<T>::digits > T_digits ;
  68. typedef typename mpl::less<T_digits,S_digits>::type type ;
  69. } ;
  70. // for integral to float conversions
  71. template<class T,class S>
  72. struct subranged_Int2Float
  73. {
  74. typedef mpl::false_ type ;
  75. } ;
  76. // for float to integral conversions
  77. template<class T,class S>
  78. struct subranged_Float2Int
  79. {
  80. typedef mpl::true_ type ;
  81. } ;
  82. // for float to float conversions
  83. template<class T,class S>
  84. struct subranged_Float2Float
  85. {
  86. // If both T and S are floats,
  87. // compare exponent bits and if they match, mantisa bits.
  88. typedef mpl::int_< ::std::numeric_limits<S>::digits > S_mantisa ;
  89. typedef mpl::int_< ::std::numeric_limits<T>::digits > T_mantisa ;
  90. typedef mpl::int_< ::std::numeric_limits<S>::max_exponent > S_exponent ;
  91. typedef mpl::int_< ::std::numeric_limits<T>::max_exponent > T_exponent ;
  92. typedef typename mpl::less<T_exponent,S_exponent>::type T_smaller_exponent ;
  93. typedef typename mpl::equal_to<T_exponent,S_exponent>::type equal_exponents ;
  94. typedef mpl::less<T_mantisa,S_mantisa> T_smaller_mantisa ;
  95. typedef mpl::eval_if<equal_exponents,T_smaller_mantisa,mpl::false_> not_bigger_exponent_case ;
  96. typedef typename
  97. mpl::eval_if<T_smaller_exponent,mpl::true_,not_bigger_exponent_case>::type
  98. type ;
  99. } ;
  100. // for Udt to built-in conversions
  101. template<class T,class S>
  102. struct subranged_Udt2BuiltIn
  103. {
  104. typedef mpl::true_ type ;
  105. } ;
  106. // for built-in to Udt conversions
  107. template<class T,class S>
  108. struct subranged_BuiltIn2Udt
  109. {
  110. typedef mpl::false_ type ;
  111. } ;
  112. // for Udt to Udt conversions
  113. template<class T,class S>
  114. struct subranged_Udt2Udt
  115. {
  116. typedef mpl::false_ type ;
  117. } ;
  118. //-------------------------------------------------------------------
  119. // Selectors for the implementations of the subranged predicate
  120. //-------------------------------------------------------------------
  121. template<class T,class S>
  122. struct get_subranged_Int2Int
  123. {
  124. typedef subranged_SameSign<T,S> Sig2Sig ;
  125. typedef subranged_Sig2Unsig<T,S> Sig2Unsig ;
  126. typedef subranged_Unsig2Sig<T,S> Unsig2Sig ;
  127. typedef Sig2Sig Unsig2Unsig ;
  128. typedef typename get_sign_mixture<T,S>::type sign_mixture ;
  129. typedef typename
  130. for_sign_mixture<sign_mixture, Sig2Sig, Sig2Unsig, Unsig2Sig, Unsig2Unsig>::type
  131. type ;
  132. } ;
  133. template<class T,class S>
  134. struct get_subranged_BuiltIn2BuiltIn
  135. {
  136. typedef get_subranged_Int2Int<T,S> Int2IntQ ;
  137. typedef subranged_Int2Float <T,S> Int2Float ;
  138. typedef subranged_Float2Int <T,S> Float2Int ;
  139. typedef subranged_Float2Float<T,S> Float2Float ;
  140. typedef mpl::identity<Int2Float > Int2FloatQ ;
  141. typedef mpl::identity<Float2Int > Float2IntQ ;
  142. typedef mpl::identity<Float2Float> Float2FloatQ ;
  143. typedef typename get_int_float_mixture<T,S>::type int_float_mixture ;
  144. typedef for_int_float_mixture<int_float_mixture, Int2IntQ, Int2FloatQ, Float2IntQ, Float2FloatQ> for_ ;
  145. typedef typename for_::type selected ;
  146. typedef typename selected::type type ;
  147. } ;
  148. template<class T,class S>
  149. struct get_subranged
  150. {
  151. typedef get_subranged_BuiltIn2BuiltIn<T,S> BuiltIn2BuiltInQ ;
  152. typedef subranged_BuiltIn2Udt<T,S> BuiltIn2Udt ;
  153. typedef subranged_Udt2BuiltIn<T,S> Udt2BuiltIn ;
  154. typedef subranged_Udt2Udt<T,S> Udt2Udt ;
  155. typedef mpl::identity<BuiltIn2Udt> BuiltIn2UdtQ ;
  156. typedef mpl::identity<Udt2BuiltIn> Udt2BuiltInQ ;
  157. typedef mpl::identity<Udt2Udt > Udt2UdtQ ;
  158. typedef typename get_udt_builtin_mixture<T,S>::type udt_builtin_mixture ;
  159. typedef typename
  160. for_udt_builtin_mixture<udt_builtin_mixture, BuiltIn2BuiltInQ, BuiltIn2UdtQ, Udt2BuiltInQ, Udt2UdtQ>::type
  161. selected ;
  162. typedef typename selected::type selected2 ;
  163. typedef typename selected2::type type ;
  164. } ;
  165. //-------------------------------------------------------------------
  166. // Top level implementation selector.
  167. //-------------------------------------------------------------------
  168. template<class T, class S>
  169. struct get_is_subranged
  170. {
  171. typedef get_subranged<T,S> non_trivial_case ;
  172. typedef mpl::identity<mpl::false_> trivial_case ;
  173. typedef is_same<T,S> is_trivial ;
  174. typedef typename mpl::if_<is_trivial,trivial_case,non_trivial_case>::type selected ;
  175. typedef typename selected::type type ;
  176. } ;
  177. } } } // namespace boost::numeric::convdetail
  178. #endif