]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
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_FMIN_HPP | |
7 | #define BOOST_MATH_CCMATH_FMIN_HPP | |
8 | ||
9 | #include <cmath> | |
10 | #include <limits> | |
11 | #include <type_traits> | |
12 | #include <boost/math/tools/is_constant_evaluated.hpp> | |
13 | #include <boost/math/ccmath/isnan.hpp> | |
14 | ||
15 | namespace boost::math::ccmath { | |
16 | ||
17 | namespace detail { | |
18 | ||
19 | template <typename T> | |
20 | inline constexpr T fmin_impl(const T x, const T y) noexcept | |
21 | { | |
22 | if (x < y) | |
23 | { | |
24 | return x; | |
25 | } | |
26 | else | |
27 | { | |
28 | return y; | |
29 | } | |
30 | } | |
31 | ||
32 | } // Namespace detail | |
33 | ||
34 | template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true> | |
35 | inline constexpr Real fmin(Real x, Real y) noexcept | |
36 | { | |
37 | if (BOOST_MATH_IS_CONSTANT_EVALUATED(x)) | |
38 | { | |
39 | return boost::math::ccmath::isnan(x) && boost::math::ccmath::isnan(y) ? std::numeric_limits<Real>::quiet_NaN() : | |
40 | boost::math::ccmath::isnan(x) ? y : | |
41 | boost::math::ccmath::isnan(y) ? x : | |
42 | boost::math::ccmath::detail::fmin_impl(x, y); | |
43 | } | |
44 | else | |
45 | { | |
46 | using std::fmin; | |
47 | return fmin(x, y); | |
48 | } | |
49 | } | |
50 | ||
51 | template <typename T1, typename T2> | |
52 | inline constexpr auto fmin(T1 x, T2 y) noexcept | |
53 | { | |
54 | if (BOOST_MATH_IS_CONSTANT_EVALUATED(x)) | |
55 | { | |
56 | // If the type is an integer (e.g. epsilon == 0) then set the epsilon value to 1 so that type is at a minimum | |
57 | // cast to double | |
58 | constexpr auto T1p = std::numeric_limits<T1>::epsilon() > 0 ? std::numeric_limits<T1>::epsilon() : 1; | |
59 | constexpr auto T2p = std::numeric_limits<T2>::epsilon() > 0 ? std::numeric_limits<T2>::epsilon() : 1; | |
60 | ||
61 | using promoted_type = | |
62 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
63 | std::conditional_t<T1p <= LDBL_EPSILON && T1p <= T2p, T1, | |
64 | std::conditional_t<T2p <= LDBL_EPSILON && T2p <= T1p, T2, | |
65 | #endif | |
66 | std::conditional_t<T1p <= DBL_EPSILON && T1p <= T2p, T1, | |
67 | std::conditional_t<T2p <= DBL_EPSILON && T2p <= T1p, T2, double | |
68 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
69 | >>>>; | |
70 | #else | |
71 | >>; | |
72 | #endif | |
73 | ||
74 | return boost::math::ccmath::fmin(promoted_type(x), promoted_type(y)); | |
75 | } | |
76 | else | |
77 | { | |
78 | using std::fmin; | |
79 | return fmin(x, y); | |
80 | } | |
81 | } | |
82 | ||
83 | inline constexpr float fminf(float x, float y) noexcept | |
84 | { | |
85 | return boost::math::ccmath::fmin(x, y); | |
86 | } | |
87 | ||
88 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
89 | inline constexpr long double fminl(long double x, long double y) noexcept | |
90 | { | |
91 | return boost::math::ccmath::fmin(x, y); | |
92 | } | |
93 | #endif | |
94 | ||
95 | } // Namespace boost::math::ccmath | |
96 | ||
97 | #endif // BOOST_MATH_CCMATH_FMIN_HPP |