Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 18 | #ifndef NDNBOOST_MATH_PROMOTION_HPP |
| 19 | #define NDNBOOST_MATH_PROMOTION_HPP |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 20 | |
| 21 | #ifdef _MSC_VER |
| 22 | #pragma once |
| 23 | #endif |
| 24 | |
| 25 | // Boost type traits: |
| 26 | #include <ndnboost/math/tools/config.hpp> |
| 27 | #include <ndnboost/type_traits/is_floating_point.hpp> // for ndnboost::is_floating_point; |
| 28 | #include <ndnboost/type_traits/is_integral.hpp> // for ndnboost::is_integral |
| 29 | #include <ndnboost/type_traits/is_convertible.hpp> // for ndnboost::is_convertible |
| 30 | #include <ndnboost/type_traits/is_same.hpp>// for ndnboost::is_same |
| 31 | #include <ndnboost/type_traits/remove_cv.hpp>// for ndnboost::remove_cv |
| 32 | // Boost Template meta programming: |
| 33 | #include <ndnboost/mpl/if.hpp> // for ndnboost::mpl::if_c. |
| 34 | #include <ndnboost/mpl/and.hpp> // for ndnboost::mpl::if_c. |
| 35 | #include <ndnboost/mpl/or.hpp> // for ndnboost::mpl::if_c. |
| 36 | #include <ndnboost/mpl/not.hpp> // for ndnboost::mpl::if_c. |
| 37 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 38 | #ifdef NDNBOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 39 | #include <ndnboost/static_assert.hpp> |
| 40 | #endif |
| 41 | |
| 42 | namespace ndnboost |
| 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_< |
| 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. |
| 94 | >::type |
| 95 | >::type, |
| 96 | // else one or the other is a user-defined type: |
| 97 | typename mpl::if_< typename mpl::and_<mpl::not_<is_floating_point<T2P> >, ::ndnboost::is_convertible<T1P, T2P> >, T2P, T1P>::type>::type type; |
| 98 | }; // promote_arg2 |
| 99 | // These full specialisations reduce mpl::if_ usage and speed up |
| 100 | // compilation: |
| 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; }; |
| 117 | |
| 118 | template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float> |
| 119 | struct promote_args |
| 120 | { |
| 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 |
| 131 | >::type |
| 132 | >::type |
| 133 | >::type |
| 134 | >::type |
| 135 | >::type type; |
| 136 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 137 | #ifdef NDNBOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 138 | // |
| 139 | // Guard against use of long double if it's not supported: |
| 140 | // |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 141 | NDNBOOST_STATIC_ASSERT((0 == ::ndnboost::is_same<type, long double>::value)); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 142 | #endif |
| 143 | }; |
| 144 | |
| 145 | } // namespace tools |
| 146 | } // namespace math |
| 147 | } // namespace ndnboost |
| 148 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 149 | #endif // NDNBOOST_MATH_PROMOTION_HPP |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 150 | |