promotion.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. namespace boost
  22. {
  23. namespace math
  24. {
  25. namespace tools
  26. {
  27. // If either T1 or T2 is an integer type,
  28. // pretend it was a double (for the purposes of further analysis).
  29. // Then pick the wider of the two floating-point types
  30. // as the actual signature to forward to.
  31. // For example:
  32. // foo(int, short) -> double foo(double, double);
  33. // foo(int, float) -> double foo(double, double);
  34. // Note: NOT float foo(float, float)
  35. // foo(int, double) -> foo(double, double);
  36. // foo(double, float) -> double foo(double, double);
  37. // foo(double, float) -> double foo(double, double);
  38. // foo(any-int-or-float-type, long double) -> foo(long double, long double);
  39. // but ONLY float foo(float, float) is unchanged.
  40. // So the only way to get an entirely float version is to call foo(1.F, 2.F),
  41. // But since most (all?) the math functions convert to double internally,
  42. // probably there would not be the hoped-for gain by using float here.
  43. // This follows the C-compatible conversion rules of pow, etc
  44. // where pow(int, float) is converted to pow(double, double).
  45. template <class T>
  46. struct promote_arg
  47. { // If T is integral type, then promote to double.
  48. using type = typename std::conditional<std::is_integral<T>::value, double, T>::type;
  49. };
  50. // These full specialisations reduce std::conditional usage and speed up
  51. // compilation:
  52. template <> struct promote_arg<float> { using type = float; };
  53. template <> struct promote_arg<double>{ using type = double; };
  54. template <> struct promote_arg<long double> { using type = long double; };
  55. template <> struct promote_arg<int> { using type = double; };
  56. template <typename T>
  57. using promote_arg_t = typename promote_arg<T>::type;
  58. template <class T1, class T2>
  59. struct promote_args_2
  60. { // Promote, if necessary, & pick the wider of the two floating-point types.
  61. // for both parameter types, if integral promote to double.
  62. using T1P = typename promote_arg<T1>::type; // T1 perhaps promoted.
  63. using T2P = typename promote_arg<T2>::type; // T2 perhaps promoted.
  64. using type = typename std::conditional<
  65. std::is_floating_point<T1P>::value && std::is_floating_point<T2P>::value, // both T1P and T2P are floating-point?
  66. #ifdef BOOST_MATH_USE_FLOAT128
  67. typename std::conditional<std::is_same<__float128, T1P>::value || std::is_same<__float128, T2P>::value, // either long double?
  68. __float128,
  69. #endif
  70. typename std::conditional<std::is_same<long double, T1P>::value || std::is_same<long double, T2P>::value, // either long double?
  71. long double, // then result type is long double.
  72. typename std::conditional<std::is_same<double, T1P>::value || std::is_same<double, T2P>::value, // either double?
  73. double, // result type is double.
  74. float // else result type is float.
  75. >::type
  76. #ifdef BOOST_MATH_USE_FLOAT128
  77. >::type
  78. #endif
  79. >::type,
  80. // else one or the other is a user-defined type:
  81. typename std::conditional<!std::is_floating_point<T2P>::value && std::is_convertible<T1P, T2P>::value, T2P, T1P>::type>::type;
  82. }; // promote_arg2
  83. // These full specialisations reduce std::conditional usage and speed up
  84. // compilation:
  85. template <> struct promote_args_2<float, float> { using type = float; };
  86. template <> struct promote_args_2<double, double>{ using type = double; };
  87. template <> struct promote_args_2<long double, long double> { using type = long double; };
  88. template <> struct promote_args_2<int, int> { using type = double; };
  89. template <> struct promote_args_2<int, float> { using type = double; };
  90. template <> struct promote_args_2<float, int> { using type = double; };
  91. template <> struct promote_args_2<int, double> { using type = double; };
  92. template <> struct promote_args_2<double, int> { using type = double; };
  93. template <> struct promote_args_2<int, long double> { using type = long double; };
  94. template <> struct promote_args_2<long double, int> { using type = long double; };
  95. template <> struct promote_args_2<float, double> { using type = double; };
  96. template <> struct promote_args_2<double, float> { using type = double; };
  97. template <> struct promote_args_2<float, long double> { using type = long double; };
  98. template <> struct promote_args_2<long double, float> { using type = long double; };
  99. template <> struct promote_args_2<double, long double> { using type = long double; };
  100. template <> struct promote_args_2<long double, double> { using type = long double; };
  101. template <typename T, typename U>
  102. using promote_args_2_t = typename promote_args_2<T, U>::type;
  103. template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
  104. struct promote_args
  105. {
  106. using type = typename promote_args_2<
  107. typename std::remove_cv<T1>::type,
  108. typename promote_args_2<
  109. typename std::remove_cv<T2>::type,
  110. typename promote_args_2<
  111. typename std::remove_cv<T3>::type,
  112. typename promote_args_2<
  113. typename std::remove_cv<T4>::type,
  114. typename promote_args_2<
  115. typename std::remove_cv<T5>::type, typename std::remove_cv<T6>::type
  116. >::type
  117. >::type
  118. >::type
  119. >::type
  120. >::type;
  121. #if defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS)
  122. //
  123. // Guard against use of long double if it's not supported:
  124. //
  125. static_assert((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.");
  126. #endif
  127. };
  128. template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
  129. using promote_args_t = typename promote_args<T1, T2, T3, T4, T5, T6>::type;
  130. //
  131. // This struct is the same as above, but has no static assert on long double usage,
  132. // it should be used only on functions that can be implemented for long double
  133. // even when std lib support is missing or broken for that type.
  134. //
  135. template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
  136. struct promote_args_permissive
  137. {
  138. using type = typename promote_args_2<
  139. typename std::remove_cv<T1>::type,
  140. typename promote_args_2<
  141. typename std::remove_cv<T2>::type,
  142. typename promote_args_2<
  143. typename std::remove_cv<T3>::type,
  144. typename promote_args_2<
  145. typename std::remove_cv<T4>::type,
  146. typename promote_args_2<
  147. typename std::remove_cv<T5>::type, typename std::remove_cv<T6>::type
  148. >::type
  149. >::type
  150. >::type
  151. >::type
  152. >::type;
  153. };
  154. template <class T1, class T2=float, class T3=float, class T4=float, class T5=float, class T6=float>
  155. using promote_args_permissive_t = typename promote_args_permissive<T1, T2, T3, T4, T5, T6>::type;
  156. } // namespace tools
  157. } // namespace math
  158. } // namespace boost
  159. #endif // BOOST_MATH_PROMOTION_HPP