1 // boost\math\tools\promotion.hpp
3 // Copyright John Maddock 2006.
4 // Copyright Paul A. Bristow 2006.
6 // Use, modification and distribution are subject to the
7 // Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt
9 // or copy at http://www.boost.org/LICENSE_1_0.txt)
11 // Promote arguments functions to allow math functions to have arguments
12 // provided as integer OR real (floating-point, built-in or UDT)
13 // (called ArithmeticType in functions that use promotion)
14 // that help to reduce the risk of creating multiple instantiations.
15 // Allows creation of an inline wrapper that forwards to a foo(RT, RT) function,
16 // so you never get to instantiate any mixed foo(RT, IT) functions.
18 #ifndef BOOST_MATH_PROMOTION_HPP
19 #define BOOST_MATH_PROMOTION_HPP
26 #include <boost/math/tools/config.hpp>
27 #include <boost/type_traits/is_floating_point.hpp> // for boost::is_floating_point;
28 #include <boost/type_traits/is_integral.hpp> // for boost::is_integral
29 #include <boost/type_traits/is_convertible.hpp> // for boost::is_convertible
30 #include <boost/type_traits/is_same.hpp>// for boost::is_same
31 #include <boost/type_traits/remove_cv.hpp>// for boost::remove_cv
32 // Boost Template meta programming:
33 #include <boost/mpl/if.hpp> // for boost::mpl::if_c.
34 #include <boost/mpl/and.hpp> // for boost::mpl::if_c.
35 #include <boost/mpl/or.hpp> // for boost::mpl::if_c.
36 #include <boost/mpl/not.hpp> // for boost::mpl::if_c.
38 #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
39 #include <boost/static_assert.hpp>
48 // If either T1 or T2 is an integer type,
49 // pretend it was a double (for the purposes of further analysis).
50 // Then pick the wider of the two floating-point types
51 // as the actual signature to forward to.
53 // foo(int, short) -> double foo(double, double);
54 // foo(int, float) -> double foo(double, double);
55 // Note: NOT float foo(float, float)
56 // foo(int, double) -> foo(double, double);
57 // foo(double, float) -> double foo(double, double);
58 // foo(double, float) -> double foo(double, double);
59 // foo(any-int-or-float-type, long double) -> foo(long double, long double);
60 // but ONLY float foo(float, float) is unchanged.
61 // So the only way to get an entirely float version is to call foo(1.F, 2.F),
62 // But since most (all?) the math functions convert to double internally,
63 // probably there would not be the hoped-for gain by using float here.
65 // This follows the C-compatible conversion rules of pow, etc
66 // where pow(int, float) is converted to pow(double, double).
70 { // If T is integral type, then promote to double.
71 typedef typename mpl::if_<is_integral<T>, double, T>::type type;
73 // These full specialisations reduce mpl::if_ usage and speed up
75 template <> struct promote_arg<float> { typedef float type; };
76 template <> struct promote_arg<double>{ typedef double type; };
77 template <> struct promote_arg<long double> { typedef long double type; };
78 template <> struct promote_arg<int> { typedef double type; };
80 template <class T1, class T2>
82 { // Promote, if necessary, & pick the wider of the two floating-point types.
83 // for both parameter types, if integral promote to double.
84 typedef typename promote_arg<T1>::type T1P; // T1 perhaps promoted.
85 typedef typename promote_arg<T2>::type T2P; // T2 perhaps promoted.
87 typedef typename mpl::if_<
88 typename mpl::and_<is_floating_point<T1P>, is_floating_point<T2P> >::type, // both T1P and T2P are floating-point?
89 typename mpl::if_< typename mpl::or_<is_same<long double, T1P>, is_same<long double, T2P> >::type, // either long double?
90 long double, // then result type is long double.
91 typename mpl::if_< typename mpl::or_<is_same<double, T1P>, is_same<double, T2P> >::type, // either double?
92 double, // result type is double.
93 float // else result type is float.
96 // else one or the other is a user-defined type:
97 typename mpl::if_< typename mpl::and_<mpl::not_<is_floating_point<T2P> >, ::boost::is_convertible<T1P, T2P> >, T2P, T1P>::type>::type type;
99 // These full specialisations reduce mpl::if_ usage and speed up
101 template <> struct promote_args_2<float, float> { typedef float type; };
102 template <> struct promote_args_2<double, double>{ typedef double type; };
103 template <> struct promote_args_2<long double, long double> { typedef long double type; };
104 template <> struct promote_args_2<int, int> { typedef double type; };
105 template <> struct promote_args_2<int, float> { typedef double type; };
106 template <> struct promote_args_2<float, int> { typedef double type; };
107 template <> struct promote_args_2<int, double> { typedef double type; };
108 template <> struct promote_args_2<double, int> { typedef double type; };
109 template <> struct promote_args_2<int, long double> { typedef long double type; };
110 template <> struct promote_args_2<long double, int> { typedef long double type; };
111 template <> struct promote_args_2<float, double> { typedef double type; };
112 template <> struct promote_args_2<double, float> { typedef double type; };
113 template <> struct promote_args_2<float, long double> { typedef long double type; };
114 template <> struct promote_args_2<long double, float> { typedef long double type; };
115 template <> struct promote_args_2<double, long double> { typedef long double type; };
116 template <> struct promote_args_2<long double, double> { typedef long double type; };
118 template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
121 typedef typename promote_args_2<
122 typename remove_cv<T1>::type,
123 typename promote_args_2<
124 typename remove_cv<T2>::type,
125 typename promote_args_2<
126 typename remove_cv<T3>::type,
127 typename promote_args_2<
128 typename remove_cv<T4>::type,
129 typename promote_args_2<
130 typename remove_cv<T5>::type, typename remove_cv<T6>::type
137 #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
139 // Guard against use of long double if it's not supported:
141 BOOST_STATIC_ASSERT_MSG((0 == ::boost::is_same<type, long double>::value), "Sorry, but this platform does not have sufficient long double support for the special functions to be reliably implemented.");
146 // This struct is the same as above, but has no static assert on long double usage,
147 // it should be used only on functions that can be implemented for long double
148 // even when std lib support is missing or broken for that type.
150 template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
151 struct promote_args_permissive
153 typedef typename promote_args_2<
154 typename remove_cv<T1>::type,
155 typename promote_args_2<
156 typename remove_cv<T2>::type,
157 typename promote_args_2<
158 typename remove_cv<T3>::type,
159 typename promote_args_2<
160 typename remove_cv<T4>::type,
161 typename promote_args_2<
162 typename remove_cv<T5>::type, typename remove_cv<T6>::type
174 #endif // BOOST_MATH_PROMOTION_HPP