| 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 |
|
| 6 |
#ifndef BOOST_MATH_CCMATH_FMAX_HPP |
| 7 |
#define BOOST_MATH_CCMATH_FMAX_HPP |
| 8 |
|
| 9 |
#include <boost/math/ccmath/detail/config.hpp> |
| 10 |
|
| 11 |
#ifdef BOOST_MATH_NO_CCMATH |
| 12 |
#error "The header <boost/math/fmax.hpp> can only be used in C++17 and later." |
| 13 |
#endif |
| 14 |
|
| 15 |
#include <boost/math/tools/promotion.hpp> |
| 16 |
#include <boost/math/ccmath/isnan.hpp> |
| 17 |
|
| 18 |
namespace boost::math::ccmath { |
| 19 |
|
| 20 |
namespace detail { |
| 21 |
|
| 22 |
template <typename T> |
| 23 |
constexpr T fmax_impl(const T x, const T y) noexcept |
| 24 |
{ |
| 25 |
if (x > y) |
| 26 |
{ |
| 27 |
return x; |
| 28 |
} |
| 29 |
else |
| 30 |
{ |
| 31 |
return y; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
} // Namespace detail |
| 36 |
|
| 37 |
template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true> |
| 38 |
constexpr Real fmax(Real x, Real y) noexcept |
| 39 |
{ |
| 40 |
if (BOOST_MATH_IS_CONSTANT_EVALUATED(x)) |
| 41 |
{ |
| 42 |
if (boost::math::ccmath::isnan(x)) |
| 43 |
{ |
| 44 |
return y; |
| 45 |
} |
| 46 |
else if (boost::math::ccmath::isnan(y)) |
| 47 |
{ |
| 48 |
return x; |
| 49 |
} |
| 50 |
|
| 51 |
return boost::math::ccmath::detail::fmax_impl(x, y); |
| 52 |
} |
| 53 |
else |
| 54 |
{ |
| 55 |
using std::fmax; |
| 56 |
return fmax(x, y); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
template <typename T1, typename T2> |
| 61 |
constexpr auto fmax(T1 x, T2 y) noexcept |
| 62 |
{ |
| 63 |
if (BOOST_MATH_IS_CONSTANT_EVALUATED(x)) |
| 64 |
{ |
| 65 |
using promoted_type = boost::math::tools::promote_args_t<T1, T2>; |
| 66 |
return boost::math::ccmath::fmax(static_cast<promoted_type>(x), static_cast<promoted_type>(y)); |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
using std::fmax; |
| 71 |
return fmax(x, y); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
constexpr float fmaxf(float x, float y) noexcept |
| 76 |
{ |
| 77 |
return boost::math::ccmath::fmax(x, y); |
| 78 |
} |
| 79 |
|
| 80 |
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS |
| 81 |
constexpr long double fmaxl(long double x, long double y) noexcept |
| 82 |
{ |
| 83 |
return boost::math::ccmath::fmax(x, y); |
| 84 |
} |
| 85 |
#endif |
| 86 |
|
| 87 |
} // Namespace boost::math::ccmath |
| 88 |
|
| 89 |
#endif // BOOST_MATH_CCMATH_FMAX_HPP |