summaryrefslogtreecommitdiff
path: root/libcxx/include/__algorithm
diff options
context:
space:
mode:
authorA. Jiang <de34@live.cn>2025-04-13 09:45:18 +0800
committerGitHub <noreply@github.com>2025-04-13 09:45:18 +0800
commitcfa322fa9a693d4ba652871fafc6dbb2039b55a2 (patch)
tree37a4478dd7eef7a7c331f1f9783c10bfef4bb55d /libcxx/include/__algorithm
parent60b1d44d708d5912897c7bdab681cbefe8f35e79 (diff)
[libc++][NFC] Reuse `__bit_log2` for `sort` (#135303)
The `__log2i` function template in `<algorithm/sort.h>` is basically equivalent to `__bit_log2` in `<__bit/bit_log2.h>`. It seems better to avoid duplication.
Diffstat (limited to 'libcxx/include/__algorithm')
-rw-r--r--libcxx/include/__algorithm/sort.h23
1 files changed, 3 insertions, 20 deletions
diff --git a/libcxx/include/__algorithm/sort.h b/libcxx/include/__algorithm/sort.h
index 4332b62544b4..d7bc1381ba5e 100644
--- a/libcxx/include/__algorithm/sort.h
+++ b/libcxx/include/__algorithm/sort.h
@@ -17,6 +17,7 @@
#include <__algorithm/partial_sort.h>
#include <__algorithm/unwrap_iter.h>
#include <__assert>
+#include <__bit/bit_log2.h>
#include <__bit/blsr.h>
#include <__bit/countl.h>
#include <__bit/countr.h>
@@ -34,6 +35,7 @@
#include <__type_traits/is_constant_evaluated.h>
#include <__type_traits/is_same.h>
#include <__type_traits/is_trivially_copyable.h>
+#include <__type_traits/make_unsigned.h>
#include <__utility/move.h>
#include <__utility/pair.h>
#include <climits>
@@ -826,25 +828,6 @@ void __introsort(_RandomAccessIterator __first,
}
}
-template <typename _Number>
-inline _LIBCPP_HIDE_FROM_ABI _Number __log2i(_Number __n) {
- if (__n == 0)
- return 0;
- if (sizeof(__n) <= sizeof(unsigned))
- return sizeof(unsigned) * CHAR_BIT - 1 - __libcpp_clz(static_cast<unsigned>(__n));
- if (sizeof(__n) <= sizeof(unsigned long))
- return sizeof(unsigned long) * CHAR_BIT - 1 - __libcpp_clz(static_cast<unsigned long>(__n));
- if (sizeof(__n) <= sizeof(unsigned long long))
- return sizeof(unsigned long long) * CHAR_BIT - 1 - __libcpp_clz(static_cast<unsigned long long>(__n));
-
- _Number __log2 = 0;
- while (__n > 1) {
- __log2++;
- __n >>= 1;
- }
- return __log2;
-}
-
template <class _Comp, class _RandomAccessIterator>
void __sort(_RandomAccessIterator, _RandomAccessIterator, _Comp);
@@ -878,7 +861,7 @@ template <class _AlgPolicy, class _RandomAccessIterator, class _Comp>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
__sort_dispatch(_RandomAccessIterator __first, _RandomAccessIterator __last, _Comp& __comp) {
typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
- difference_type __depth_limit = 2 * std::__log2i(__last - __first);
+ difference_type __depth_limit = 2 * std::__bit_log2(std::__to_unsigned_like(__last - __first));
// Only use bitset partitioning for arithmetic types. We should also check
// that the default comparator is in use so that we are sure that there are no