fmax.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // (C) Copyright Matt Borland 2022.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_CCMATH_FMAX_HPP
  6. #define BOOST_MATH_CCMATH_FMAX_HPP
  7. #include <boost/math/ccmath/detail/config.hpp>
  8. #ifdef BOOST_MATH_NO_CCMATH
  9. #error "The header <boost/math/fmax.hpp> can only be used in C++17 and later."
  10. #endif
  11. #include <boost/math/tools/promotion.hpp>
  12. #include <boost/math/ccmath/isnan.hpp>
  13. namespace boost::math::ccmath {
  14. namespace detail {
  15. template <typename T>
  16. constexpr T fmax_impl(const T x, const T y) noexcept
  17. {
  18. if (x > y)
  19. {
  20. return x;
  21. }
  22. else
  23. {
  24. return y;
  25. }
  26. }
  27. } // Namespace detail
  28. template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
  29. constexpr Real fmax(Real x, Real y) noexcept
  30. {
  31. if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))
  32. {
  33. if (boost::math::ccmath::isnan(x))
  34. {
  35. return y;
  36. }
  37. else if (boost::math::ccmath::isnan(y))
  38. {
  39. return x;
  40. }
  41. return boost::math::ccmath::detail::fmax_impl(x, y);
  42. }
  43. else
  44. {
  45. using std::fmax;
  46. return fmax(x, y);
  47. }
  48. }
  49. template <typename T1, typename T2>
  50. constexpr auto fmax(T1 x, T2 y) noexcept
  51. {
  52. if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))
  53. {
  54. using promoted_type = boost::math::tools::promote_args_2_t<T1, T2>;
  55. return boost::math::ccmath::fmax(static_cast<promoted_type>(x), static_cast<promoted_type>(y));
  56. }
  57. else
  58. {
  59. using std::fmax;
  60. return fmax(x, y);
  61. }
  62. }
  63. constexpr float fmaxf(float x, float y) noexcept
  64. {
  65. return boost::math::ccmath::fmax(x, y);
  66. }
  67. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  68. constexpr long double fmaxl(long double x, long double y) noexcept
  69. {
  70. return boost::math::ccmath::fmax(x, y);
  71. }
  72. #endif
  73. } // Namespace boost::math::ccmath
  74. #endif // BOOST_MATH_CCMATH_FMAX_HPP