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)
6
// See library home page at http://www.boost.org/libs/numeric/conversion
8
// Contact the author at: fernando_cacciola@hotmail.com
10
#ifndef BOOST_NUMERIC_CONVERSION_DETAIL_IS_SUBRANGED_FLC_12NOV2002_HPP
11
#define BOOST_NUMERIC_CONVERSION_DETAIL_IS_SUBRANGED_FLC_12NOV2002_HPP
13
#include "boost/config.hpp"
14
#include "boost/limits.hpp"
16
#include "boost/mpl/int.hpp"
17
#include "boost/mpl/multiplies.hpp"
18
#include "boost/mpl/less.hpp"
19
#include "boost/mpl/equal_to.hpp"
21
#include "boost/type_traits/is_same.hpp"
23
#include "boost/numeric/conversion/detail/meta.hpp"
24
#include "boost/numeric/conversion/detail/int_float_mixture.hpp"
25
#include "boost/numeric/conversion/detail/sign_mixture.hpp"
26
#include "boost/numeric/conversion/detail/udt_builtin_mixture.hpp"
28
namespace boost { namespace numeric { namespace convdetail
30
//---------------------------------------------------------------
31
// Implementations of the compile time predicate "T is subranged"
32
//---------------------------------------------------------------
34
// for integral to integral conversions
35
template<class T,class S>
36
struct subranged_Sig2Unsig
38
// Signed to unsigned conversions are 'subranged' because of possible loose
39
// of negative values.
40
typedef mpl::true_ type ;
43
// for unsigned integral to signed integral conversions
44
template<class T,class S>
45
struct subranged_Unsig2Sig
49
// This code assumes that signed/unsigned integral values are represented
52
// numeric_limits<signed T>::digits + 1 == numeric_limits<unsigned T>::digits
54
// The '+1' is required since numeric_limits<>::digits gives 1 bit less for signed integral types.
56
// This fact is used by the following logic:
58
// if ( (numeric_limits<T>::digits+1) < (2*numeric_limits<S>::digits) )
59
// then the conversion is subranged.
62
typedef mpl::int_< ::std::numeric_limits<S>::digits > S_digits ;
63
typedef mpl::int_< ::std::numeric_limits<T>::digits > T_digits ;
65
// T is signed, so take digits+1
66
typedef typename T_digits::next u_T_digits ;
68
typedef mpl::int_<2> Two ;
70
typedef typename mpl::multiplies<S_digits,Two>::type S_digits_times_2 ;
72
typedef typename mpl::less<u_T_digits,S_digits_times_2>::type type ;
75
// for integral to integral conversions of the same sign.
76
template<class T,class S>
77
struct subranged_SameSign
79
// An integral conversion of the same sign is subranged if digits(T) < digits(S).
81
typedef mpl::int_< ::std::numeric_limits<S>::digits > S_digits ;
82
typedef mpl::int_< ::std::numeric_limits<T>::digits > T_digits ;
84
typedef typename mpl::less<T_digits,S_digits>::type type ;
87
// for integral to float conversions
88
template<class T,class S>
89
struct subranged_Int2Float
91
typedef mpl::false_ type ;
94
// for float to integral conversions
95
template<class T,class S>
96
struct subranged_Float2Int
98
typedef mpl::true_ type ;
101
// for float to float conversions
102
template<class T,class S>
103
struct subranged_Float2Float
105
// If both T and S are floats,
106
// compare exponent bits and if they match, mantisa bits.
108
typedef mpl::int_< ::std::numeric_limits<S>::digits > S_mantisa ;
109
typedef mpl::int_< ::std::numeric_limits<T>::digits > T_mantisa ;
111
typedef mpl::int_< ::std::numeric_limits<S>::max_exponent > S_exponent ;
112
typedef mpl::int_< ::std::numeric_limits<T>::max_exponent > T_exponent ;
114
typedef typename mpl::less<T_exponent,S_exponent>::type T_smaller_exponent ;
116
typedef typename mpl::equal_to<T_exponent,S_exponent>::type equal_exponents ;
118
typedef mpl::less<T_mantisa,S_mantisa> T_smaller_mantisa ;
120
typedef mpl::eval_if<equal_exponents,T_smaller_mantisa,mpl::false_> not_bigger_exponent_case ;
123
mpl::eval_if<T_smaller_exponent,mpl::true_,not_bigger_exponent_case>::type
127
// for Udt to built-in conversions
128
template<class T,class S>
129
struct subranged_Udt2BuiltIn
131
typedef mpl::true_ type ;
134
// for built-in to Udt conversions
135
template<class T,class S>
136
struct subranged_BuiltIn2Udt
138
typedef mpl::false_ type ;
141
// for Udt to Udt conversions
142
template<class T,class S>
143
struct subranged_Udt2Udt
145
typedef mpl::false_ type ;
148
//-------------------------------------------------------------------
149
// Selectors for the implementations of the subranged predicate
150
//-------------------------------------------------------------------
152
template<class T,class S>
153
struct get_subranged_Int2Int
155
typedef subranged_SameSign<T,S> Sig2Sig ;
156
typedef subranged_Sig2Unsig<T,S> Sig2Unsig ;
157
typedef subranged_Unsig2Sig<T,S> Unsig2Sig ;
158
typedef Sig2Sig Unsig2Unsig ;
160
typedef typename get_sign_mixture<T,S>::type sign_mixture ;
163
for_sign_mixture<sign_mixture, Sig2Sig, Sig2Unsig, Unsig2Sig, Unsig2Unsig>::type
167
template<class T,class S>
168
struct get_subranged_BuiltIn2BuiltIn
170
typedef get_subranged_Int2Int<T,S> Int2IntQ ;
172
typedef subranged_Int2Float <T,S> Int2Float ;
173
typedef subranged_Float2Int <T,S> Float2Int ;
174
typedef subranged_Float2Float<T,S> Float2Float ;
176
typedef mpl::identity<Int2Float > Int2FloatQ ;
177
typedef mpl::identity<Float2Int > Float2IntQ ;
178
typedef mpl::identity<Float2Float> Float2FloatQ ;
180
typedef typename get_int_float_mixture<T,S>::type int_float_mixture ;
182
typedef for_int_float_mixture<int_float_mixture, Int2IntQ, Int2FloatQ, Float2IntQ, Float2FloatQ> for_ ;
184
typedef typename for_::type selected ;
186
typedef typename selected::type type ;
189
template<class T,class S>
192
typedef get_subranged_BuiltIn2BuiltIn<T,S> BuiltIn2BuiltInQ ;
194
typedef subranged_BuiltIn2Udt<T,S> BuiltIn2Udt ;
195
typedef subranged_Udt2BuiltIn<T,S> Udt2BuiltIn ;
196
typedef subranged_Udt2Udt<T,S> Udt2Udt ;
198
typedef mpl::identity<BuiltIn2Udt> BuiltIn2UdtQ ;
199
typedef mpl::identity<Udt2BuiltIn> Udt2BuiltInQ ;
200
typedef mpl::identity<Udt2Udt > Udt2UdtQ ;
202
typedef typename get_udt_builtin_mixture<T,S>::type udt_builtin_mixture ;
205
for_udt_builtin_mixture<udt_builtin_mixture, BuiltIn2BuiltInQ, BuiltIn2UdtQ, Udt2BuiltInQ, Udt2UdtQ>::type
208
typedef typename selected::type selected2 ;
210
typedef typename selected2::type type ;
214
//-------------------------------------------------------------------
215
// Top level implementation selector.
216
//-------------------------------------------------------------------
217
template<class T, class S>
218
struct get_is_subranged
220
typedef get_subranged<T,S> non_trivial_case ;
221
typedef mpl::identity<mpl::false_> trivial_case ;
223
typedef is_same<T,S> is_trivial ;
225
typedef typename mpl::if_<is_trivial,trivial_case,non_trivial_case>::type selected ;
227
typedef typename selected::type type ;
230
} } } // namespace boost::numeric::convdetail