aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2021-04-17 22:34:09 +0100
committerJonathan Wakely <jwakely@redhat.com>2021-10-01 20:34:46 +0100
commit741c7350c08b0884689466867b6c9e711c7b109e (patch)
tree1b0c1a73481bc0fb4cabbed8d3d5ad96bbe8d35d
parent2a93d18da3bd79eb0be87f029eefc8b28bb66dec (diff)
downloadgcc-741c7350c08b0884689466867b6c9e711c7b109e.zip
gcc-741c7350c08b0884689466867b6c9e711c7b109e.tar.gz
gcc-741c7350c08b0884689466867b6c9e711c7b109e.tar.bz2
libstdc++: Implement std::clamp with std::min and std::max [PR 96733]
The compiler doesn't know about the precondition of std::clamp that (hi < lo) is false, and so can't optimize as well as we'd like. By using std::min and std::max we help the compiler. Signed-off-by: Jonathan Wakely <jwakely@redhat.com> libstdc++-v3/ChangeLog: PR libstdc++/96733 * include/bits/stl_algo.h (clamp): Use std::min and std::max.
-rw-r--r--libstdc++-v3/include/bits/stl_algo.h17
1 files changed, 11 insertions, 6 deletions
diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h
index 5d12972..90f3162 100644
--- a/libstdc++-v3/include/bits/stl_algo.h
+++ b/libstdc++-v3/include/bits/stl_algo.h
@@ -3621,7 +3621,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__gnu_cxx::__ops::__iter_comp_iter(__pred));
}
-#if __cplusplus > 201402L
+#if __cplusplus >= 201703L
#define __cpp_lib_clamp 201603
@@ -3631,14 +3631,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __val A value of arbitrary type.
* @param __lo A lower limit of arbitrary type.
* @param __hi An upper limit of arbitrary type.
- * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise.
+ * @retval `__lo` if `__val < __lo`
+ * @retval `__hi` if `__hi < __val`
+ * @retval `__val` otherwise.
+ * @pre `_Tp` is LessThanComparable and `(__hi < __lo)` is false.
*/
template<typename _Tp>
constexpr const _Tp&
clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi)
{
__glibcxx_assert(!(__hi < __lo));
- return (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val;
+ return std::min(std::max(__val, __lo), __hi);
}
/**
@@ -3648,15 +3651,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* @param __lo A lower limit of arbitrary type.
* @param __hi An upper limit of arbitrary type.
* @param __comp A comparison functor.
- * @return max(__val, __lo, __comp) if __comp(__val, __hi)
- * or min(__val, __hi, __comp) otherwise.
+ * @retval `__lo` if `__comp(__val, __lo)`
+ * @retval `__hi` if `__comp(__hi, __val)`
+ * @retval `__val` otherwise.
+ * @pre `__comp(__hi, __lo)` is false.
*/
template<typename _Tp, typename _Compare>
constexpr const _Tp&
clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
{
__glibcxx_assert(!__comp(__hi, __lo));
- return __comp(__val, __lo) ? __lo : __comp(__hi, __val) ? __hi : __val;
+ return std::min(std::max(__val, __lo, __comp), __hi, __comp);
}
#endif // C++17
#endif // C++14