1// boost\math\tools\promotion.hpp
2
3// Copyright John Maddock 2006.
4// Copyright Paul A. Bristow 2006.
5
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)
10
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.
17
18#ifndef BOOST_MATH_PROMOTION_HPP
19#define BOOST_MATH_PROMOTION_HPP
20
21#ifdef _MSC_VER
22#pragma once
23#endif
24
25// Boost type traits:
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.
37
38#ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
39#include <boost/static_assert.hpp>
40#endif
41
42namespace boost
43{
44 namespace math
45 {
46 namespace tools
47 {
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.
52 // For example:
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.
64
65 // This follows the C-compatible conversion rules of pow, etc
66 // where pow(int, float) is converted to pow(double, double).
67
68 template <class T>
69 struct promote_arg
70 { // If T is integral type, then promote to double.
71 typedef typename mpl::if_<is_integral<T>, double, T>::type type;
72 };
73 // These full specialisations reduce mpl::if_ usage and speed up
74 // compilation:
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; };
79
80 template <class T1, class T2>
81 struct promote_args_2
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.
86
87 typedef typename mpl::if_c<
88 is_floating_point<T1P>::value && is_floating_point<T2P>::value, // both T1P and T2P are floating-point?
89#ifdef BOOST_MATH_USE_FLOAT128
90 typename mpl::if_c<is_same<__float128, T1P>::value || is_same<__float128, T2P>::value, // either long double?
91 __float128,
92#endif
93 typename mpl::if_c<is_same<long double, T1P>::value || is_same<long double, T2P>::value, // either long double?
94 long double, // then result type is long double.
95 typename mpl::if_c<is_same<double, T1P>::value || is_same<double, T2P>::value, // either double?
96 double, // result type is double.
97 float // else result type is float.
98 >::type
99#ifdef BOOST_MATH_USE_FLOAT128
100 >::type
101#endif
102 >::type,
103 // else one or the other is a user-defined type:
104 typename mpl::if_c<!is_floating_point<T2P>::value && ::boost::is_convertible<T1P, T2P>::value, T2P, T1P>::type>::type type;
105 }; // promote_arg2
106 // These full specialisations reduce mpl::if_ usage and speed up
107 // compilation:
108 template <> struct promote_args_2<float, float> { typedef float type; };
109 template <> struct promote_args_2<double, double>{ typedef double type; };
110 template <> struct promote_args_2<long double, long double> { typedef long double type; };
111 template <> struct promote_args_2<int, int> { typedef double type; };
112 template <> struct promote_args_2<int, float> { typedef double type; };
113 template <> struct promote_args_2<float, int> { typedef double type; };
114 template <> struct promote_args_2<int, double> { typedef double type; };
115 template <> struct promote_args_2<double, int> { typedef double type; };
116 template <> struct promote_args_2<int, long double> { typedef long double type; };
117 template <> struct promote_args_2<long double, int> { typedef long double type; };
118 template <> struct promote_args_2<float, double> { typedef double type; };
119 template <> struct promote_args_2<double, float> { typedef double type; };
120 template <> struct promote_args_2<float, long double> { typedef long double type; };
121 template <> struct promote_args_2<long double, float> { typedef long double type; };
122 template <> struct promote_args_2<double, long double> { typedef long double type; };
123 template <> struct promote_args_2<long double, double> { typedef long double type; };
124
125 template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
126 struct promote_args
127 {
128 typedef typename promote_args_2<
129 typename remove_cv<T1>::type,
130 typename promote_args_2<
131 typename remove_cv<T2>::type,
132 typename promote_args_2<
133 typename remove_cv<T3>::type,
134 typename promote_args_2<
135 typename remove_cv<T4>::type,
136 typename promote_args_2<
137 typename remove_cv<T5>::type, typename remove_cv<T6>::type
138 >::type
139 >::type
140 >::type
141 >::type
142 >::type type;
143
144#ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
145 //
146 // Guard against use of long double if it's not supported:
147 //
148 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.");
149#endif
150 };
151
152 //
153 // This struct is the same as above, but has no static assert on long double usage,
154 // it should be used only on functions that can be implemented for long double
155 // even when std lib support is missing or broken for that type.
156 //
157 template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
158 struct promote_args_permissive
159 {
160 typedef typename promote_args_2<
161 typename remove_cv<T1>::type,
162 typename promote_args_2<
163 typename remove_cv<T2>::type,
164 typename promote_args_2<
165 typename remove_cv<T3>::type,
166 typename promote_args_2<
167 typename remove_cv<T4>::type,
168 typename promote_args_2<
169 typename remove_cv<T5>::type, typename remove_cv<T6>::type
170 >::type
171 >::type
172 >::type
173 >::type
174 >::type type;
175 };
176
177 } // namespace tools
178 } // namespace math
179} // namespace boost
180
181#endif // BOOST_MATH_PROMOTION_HPP
182
183

source code of include/boost/math/tools/promotion.hpp