promotion.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // boost\math\tools\promotion.hpp
  2. // Copyright John Maddock 2006.
  3. // Copyright Paul A. Bristow 2006.
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt
  7. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. // Promote arguments functions to allow math functions to have arguments
  9. // provided as integer OR real (floating-point, built-in or UDT)
  10. // (called ArithmeticType in functions that use promotion)
  11. // that help to reduce the risk of creating multiple instantiations.
  12. // Allows creation of an inline wrapper that forwards to a foo(RT, RT) function,
  13. // so you never get to instantiate any mixed foo(RT, IT) functions.
  14. #ifndef BOOST_MATH_PROMOTION_HPP
  15. #define BOOST_MATH_PROMOTION_HPP
  16. #ifdef _MSC_VER
  17. #pragma once
  18. #endif
  19. #include <boost/math/tools/config.hpp>
  20. #include <type_traits>
  21. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  22. #include <boost/static_assert.hpp>
  23. #endif
  24. namespace boost
  25. {
  26. namespace math
  27. {
  28. namespace tools
  29. {
  30. // If either T1 or T2 is an integer type,
  31. // pretend it was a double (for the purposes of further analysis).
  32. // Then pick the wider of the two floating-point types
  33. // as the actual signature to forward to.
  34. // For example:
  35. // foo(int, short) -> double foo(double, double);
  36. // foo(int, float) -> double foo(double, double);
  37. // Note: NOT float foo(float, float)
  38. // foo(int, double) -> foo(double, double);
  39. // foo(double, float) -> double foo(double, double);
  40. // foo(double, float) -> double foo(double, double);
  41. // foo(any-int-or-float-type, long double) -> foo(long double, long double);
  42. // but ONLY float foo(float, float) is unchanged.
  43. // So the only way to get an entirely float version is to call foo(1.F, 2.F),
  44. // But since most (all?) the math functions convert to double internally,
  45. // probably there would not be the hoped-for gain by using float here.
  46. // This follows the C-compatible conversion rules of pow, etc
  47. // where pow(int, float) is converted to pow(double, double).
  48. template <class T>
  49. struct promote_arg
  50. { // If T is integral type, then promote to double.
  51. using type = typename std::conditional<std::is_integral<T>::value, double, T>::type;
  52. };
  53. // These full specialisations reduce std::conditional usage and speed up
  54. // compilation:
  55. template <> struct promote_arg<float> { using type = float; };
  56. template <> struct promote_arg<double>{ using type = double; };
  57. template <> struct promote_arg<long double> { using type = long double; };
  58. template <> struct promote_arg<int> { using type = double; };
  59. template <class T1, class T2>
  60. struct promote_args_2
  61. { // Promote, if necessary, & pick the wider of the two floating-point types.
  62. // for both parameter types, if integral promote to double.
  63. using T1P = typename promote_arg<T1>::type; // T1 perhaps promoted.
  64. using T2P = typename promote_arg<T2>::type; // T2 perhaps promoted.
  65. using type = typename std::conditional<
  66. std::is_floating_point<T1P>::value && std::is_floating_point<T2P>::value, // both T1P and T2P are floating-point?
  67. #ifdef BOOST_MATH_USE_FLOAT128
  68. typename std::conditional<std::is_same<__float128, T1P>::value || std::is_same<__float128, T2P>::value, // either long double?
  69. __float128,
  70. #endif
  71. typename std::conditional<std::is_same<long double, T1P>::value || std::is_same<long double, T2P>::value, // either long double?
  72. long double, // then result type is long double.
  73. typename std::conditional<std::is_same<double, T1P>::value || std::is_same<double, T2P>::value, // either double?
  74. double, // result type is double.
  75. float // else result type is float.
  76. >::type
  77. #ifdef BOOST_MATH_USE_FLOAT128
  78. >::type
  79. #endif
  80. >::type,
  81. // else one or the other is a user-defined type:
  82. typename std::conditional<!std::is_floating_point<T2P>::value && std::is_convertible<T1P, T2P>::value, T2P, T1P>::type>::type;
  83. }; // promote_arg2
  84. // These full specialisations reduce std::conditional usage and speed up
  85. // compilation:
  86. template <> struct promote_args_2<float, float> { using type = float; };
  87. template <> struct promote_args_2<double, double>{ using type = double; };
  88. template <> struct promote_args_2<long double, long double> { using type = long double; };
  89. template <> struct promote_args_2<int, int> { using type = double; };
  90. template <> struct promote_args_2<int, float> { using type = double; };
  91. template <> struct promote_args_2<float, int> { using type = double; };
  92. template <> struct promote_args_2<int, double> { using type = double; };
  93. template <> struct promote_args_2<double, int> { using type = double; };
  94. template <> struct promote_args_2<int, long double> { using type = long double; };
  95. template <> struct promote_args_2<long double, int> { using type = long double; };
  96. template <> struct promote_args_2<float, double> { using type = double; };
  97. template <> struct promote_args_2<double, float> { using type = double; };
  98. template <> struct promote_args_2<float, long double> { using type = long double; };
  99. template <> struct promote_args_2<long double, float> { using type = long double; };
  100. template <> struct promote_args_2<double, long double> { using type = long double; };
  101. template <> struct promote_args_2<long double, double> { using type = long double; };
  102. template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
  103. struct promote_args
  104. {
  105. using type = typename promote_args_2<
  106. typename std::remove_cv<T1>::type,
  107. typename promote_args_2<
  108. typename std::remove_cv<T2>::type,
  109. typename promote_args_2<
  110. typename std::remove_cv<T3>::type,
  111. typename promote_args_2<
  112. typename std::remove_cv<T4>::type,
  113. typename promote_args_2<
  114. typename std::remove_cv<T5>::type, typename std::remove_cv<T6>::type
  115. >::type
  116. >::type
  117. >::type
  118. >::type
  119. >::type;
  120. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  121. //
  122. // Guard against use of long double if it's not supported:
  123. //
  124. BOOST_STATIC_ASSERT_MSG((0 == std::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.");
  125. #endif
  126. };
  127. //
  128. // This struct is the same as above, but has no static assert on long double usage,
  129. // it should be used only on functions that can be implemented for long double
  130. // even when std lib support is missing or broken for that type.
  131. //
  132. template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
  133. struct promote_args_permissive
  134. {
  135. using type = typename promote_args_2<
  136. typename std::remove_cv<T1>::type,
  137. typename promote_args_2<
  138. typename std::remove_cv<T2>::type,
  139. typename promote_args_2<
  140. typename std::remove_cv<T3>::type,
  141. typename promote_args_2<
  142. typename std::remove_cv<T4>::type,
  143. typename promote_args_2<
  144. typename std::remove_cv<T5>::type, typename std::remove_cv<T6>::type
  145. >::type
  146. >::type
  147. >::type
  148. >::type
  149. >::type;
  150. };
  151. } // namespace tools
  152. } // namespace math
  153. } // namespace boost
  154. #endif // BOOST_MATH_PROMOTION_HPP