Chris@16: // boost\math\tools\promotion.hpp Chris@16: Chris@16: // Copyright John Maddock 2006. Chris@16: // Copyright Paul A. Bristow 2006. Chris@16: Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt Chris@16: // or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // Promote arguments functions to allow math functions to have arguments Chris@16: // provided as integer OR real (floating-point, built-in or UDT) Chris@16: // (called ArithmeticType in functions that use promotion) Chris@16: // that help to reduce the risk of creating multiple instantiations. Chris@16: // Allows creation of an inline wrapper that forwards to a foo(RT, RT) function, Chris@16: // so you never get to instantiate any mixed foo(RT, IT) functions. Chris@16: Chris@16: #ifndef BOOST_MATH_PROMOTION_HPP Chris@16: #define BOOST_MATH_PROMOTION_HPP Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: // Boost type traits: Chris@16: #include Chris@16: #include // for boost::is_floating_point; Chris@16: #include // for boost::is_integral Chris@16: #include // for boost::is_convertible Chris@16: #include // for boost::is_same Chris@16: #include // for boost::remove_cv Chris@16: // Boost Template meta programming: Chris@16: #include // for boost::mpl::if_c. Chris@16: #include // for boost::mpl::if_c. Chris@16: #include // for boost::mpl::if_c. Chris@16: #include // for boost::mpl::if_c. Chris@16: Chris@16: #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS Chris@16: #include Chris@16: #endif Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace math Chris@16: { Chris@16: namespace tools Chris@16: { Chris@16: // If either T1 or T2 is an integer type, Chris@16: // pretend it was a double (for the purposes of further analysis). Chris@16: // Then pick the wider of the two floating-point types Chris@16: // as the actual signature to forward to. Chris@16: // For example: Chris@16: // foo(int, short) -> double foo(double, double); Chris@16: // foo(int, float) -> double foo(double, double); Chris@16: // Note: NOT float foo(float, float) Chris@16: // foo(int, double) -> foo(double, double); Chris@16: // foo(double, float) -> double foo(double, double); Chris@16: // foo(double, float) -> double foo(double, double); Chris@16: // foo(any-int-or-float-type, long double) -> foo(long double, long double); Chris@16: // but ONLY float foo(float, float) is unchanged. Chris@16: // So the only way to get an entirely float version is to call foo(1.F, 2.F), Chris@16: // But since most (all?) the math functions convert to double internally, Chris@16: // probably there would not be the hoped-for gain by using float here. Chris@16: Chris@16: // This follows the C-compatible conversion rules of pow, etc Chris@16: // where pow(int, float) is converted to pow(double, double). Chris@16: Chris@16: template Chris@16: struct promote_arg Chris@16: { // If T is integral type, then promote to double. Chris@16: typedef typename mpl::if_, double, T>::type type; Chris@16: }; Chris@16: // These full specialisations reduce mpl::if_ usage and speed up Chris@16: // compilation: Chris@16: template <> struct promote_arg { typedef float type; }; Chris@16: template <> struct promote_arg{ typedef double type; }; Chris@16: template <> struct promote_arg { typedef long double type; }; Chris@16: template <> struct promote_arg { typedef double type; }; Chris@16: Chris@16: template Chris@16: struct promote_args_2 Chris@16: { // Promote, if necessary, & pick the wider of the two floating-point types. Chris@16: // for both parameter types, if integral promote to double. Chris@16: typedef typename promote_arg::type T1P; // T1 perhaps promoted. Chris@16: typedef typename promote_arg::type T2P; // T2 perhaps promoted. Chris@16: Chris@16: typedef typename mpl::if_< Chris@16: typename mpl::and_, is_floating_point >::type, // both T1P and T2P are floating-point? Chris@16: typename mpl::if_< typename mpl::or_, is_same >::type, // either long double? Chris@16: long double, // then result type is long double. Chris@16: typename mpl::if_< typename mpl::or_, is_same >::type, // either double? Chris@16: double, // result type is double. Chris@16: float // else result type is float. Chris@16: >::type Chris@16: >::type, Chris@16: // else one or the other is a user-defined type: Chris@16: typename mpl::if_< typename mpl::and_ >, ::boost::is_convertible >, T2P, T1P>::type>::type type; Chris@16: }; // promote_arg2 Chris@16: // These full specialisations reduce mpl::if_ usage and speed up Chris@16: // compilation: Chris@16: template <> struct promote_args_2 { typedef float type; }; Chris@16: template <> struct promote_args_2{ typedef double type; }; Chris@16: template <> struct promote_args_2 { typedef long double type; }; Chris@16: template <> struct promote_args_2 { typedef double type; }; Chris@16: template <> struct promote_args_2 { typedef double type; }; Chris@16: template <> struct promote_args_2 { typedef double type; }; Chris@16: template <> struct promote_args_2 { typedef double type; }; Chris@16: template <> struct promote_args_2 { typedef double type; }; Chris@16: template <> struct promote_args_2 { typedef long double type; }; Chris@16: template <> struct promote_args_2 { typedef long double type; }; Chris@16: template <> struct promote_args_2 { typedef double type; }; Chris@16: template <> struct promote_args_2 { typedef double type; }; Chris@16: template <> struct promote_args_2 { typedef long double type; }; Chris@16: template <> struct promote_args_2 { typedef long double type; }; Chris@16: template <> struct promote_args_2 { typedef long double type; }; Chris@16: template <> struct promote_args_2 { typedef long double type; }; Chris@16: Chris@16: template Chris@16: struct promote_args Chris@16: { Chris@16: typedef typename promote_args_2< Chris@16: typename remove_cv::type, Chris@16: typename promote_args_2< Chris@16: typename remove_cv::type, Chris@16: typename promote_args_2< Chris@16: typename remove_cv::type, Chris@16: typename promote_args_2< Chris@16: typename remove_cv::type, Chris@16: typename promote_args_2< Chris@16: typename remove_cv::type, typename remove_cv::type Chris@16: >::type Chris@16: >::type Chris@16: >::type Chris@16: >::type Chris@16: >::type type; Chris@16: Chris@16: #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS Chris@16: // Chris@16: // Guard against use of long double if it's not supported: Chris@16: // Chris@16: BOOST_STATIC_ASSERT_MSG((0 == ::boost::is_same::value), "Sorry, but this platform does not have sufficient long double support for the special functions to be reliably implemented."); Chris@16: #endif Chris@16: }; Chris@16: Chris@16: // Chris@16: // This struct is the same as above, but has no static assert on long double usage, Chris@16: // it should be used only on functions that can be implemented for long double Chris@16: // even when std lib support is missing or broken for that type. Chris@16: // Chris@16: template Chris@16: struct promote_args_permissive Chris@16: { Chris@16: typedef typename promote_args_2< Chris@16: typename remove_cv::type, Chris@16: typename promote_args_2< Chris@16: typename remove_cv::type, Chris@16: typename promote_args_2< Chris@16: typename remove_cv::type, Chris@16: typename promote_args_2< Chris@16: typename remove_cv::type, Chris@16: typename promote_args_2< Chris@16: typename remove_cv::type, typename remove_cv::type Chris@16: >::type Chris@16: >::type Chris@16: >::type Chris@16: >::type Chris@16: >::type type; Chris@16: }; Chris@16: Chris@16: } // namespace tools Chris@16: } // namespace math Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_MATH_PROMOTION_HPP Chris@16: