* [committed] libstdc++: Implement std::clamp with std::min and std::max [PR 96733]
@ 2021-10-01 19:40 Jonathan Wakely
0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2021-10-01 19:40 UTC (permalink / raw)
To: libstdc++, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 395 bytes --]
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.
Tested powerpc64le-linux. Committed to trunk.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2717 bytes --]
commit 741c7350c08b0884689466867b6c9e711c7b109e
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Sat Apr 17 22:34:09 2021
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.
diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h
index 5d12972ce2c..90f3162ff90 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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2021-10-01 19:40 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-01 19:40 [committed] libstdc++: Implement std::clamp with std::min and std::max [PR 96733] Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).