Skip to content

Commit cfa322f

Browse files
[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.
1 parent 60b1d44 commit cfa322f

File tree

2 files changed

+4
-25
lines changed

2 files changed

+4
-25
lines changed

libcxx/include/__algorithm/sort.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <__algorithm/partial_sort.h>
1818
#include <__algorithm/unwrap_iter.h>
1919
#include <__assert>
20+
#include <__bit/bit_log2.h>
2021
#include <__bit/blsr.h>
2122
#include <__bit/countl.h>
2223
#include <__bit/countr.h>
@@ -34,6 +35,7 @@
3435
#include <__type_traits/is_constant_evaluated.h>
3536
#include <__type_traits/is_same.h>
3637
#include <__type_traits/is_trivially_copyable.h>
38+
#include <__type_traits/make_unsigned.h>
3739
#include <__utility/move.h>
3840
#include <__utility/pair.h>
3941
#include <climits>
@@ -826,25 +828,6 @@ void __introsort(_RandomAccessIterator __first,
826828
}
827829
}
828830

829-
template <typename _Number>
830-
inline _LIBCPP_HIDE_FROM_ABI _Number __log2i(_Number __n) {
831-
if (__n == 0)
832-
return 0;
833-
if (sizeof(__n) <= sizeof(unsigned))
834-
return sizeof(unsigned) * CHAR_BIT - 1 - __libcpp_clz(static_cast<unsigned>(__n));
835-
if (sizeof(__n) <= sizeof(unsigned long))
836-
return sizeof(unsigned long) * CHAR_BIT - 1 - __libcpp_clz(static_cast<unsigned long>(__n));
837-
if (sizeof(__n) <= sizeof(unsigned long long))
838-
return sizeof(unsigned long long) * CHAR_BIT - 1 - __libcpp_clz(static_cast<unsigned long long>(__n));
839-
840-
_Number __log2 = 0;
841-
while (__n > 1) {
842-
__log2++;
843-
__n >>= 1;
844-
}
845-
return __log2;
846-
}
847-
848831
template <class _Comp, class _RandomAccessIterator>
849832
void __sort(_RandomAccessIterator, _RandomAccessIterator, _Comp);
850833

@@ -878,7 +861,7 @@ template <class _AlgPolicy, class _RandomAccessIterator, class _Comp>
878861
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
879862
__sort_dispatch(_RandomAccessIterator __first, _RandomAccessIterator __last, _Comp& __comp) {
880863
typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
881-
difference_type __depth_limit = 2 * std::__log2i(__last - __first);
864+
difference_type __depth_limit = 2 * std::__bit_log2(std::__to_unsigned_like(__last - __first));
882865

883866
// Only use bitset partitioning for arithmetic types. We should also check
884867
// that the default comparator is in use so that we are sure that there are no

libcxx/include/__bit/bit_log2.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@
2020

2121
_LIBCPP_BEGIN_NAMESPACE_STD
2222

23-
#if _LIBCPP_STD_VER >= 14
24-
2523
template <class _Tp>
26-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __bit_log2(_Tp __t) noexcept {
24+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __bit_log2(_Tp __t) _NOEXCEPT {
2725
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
2826
return numeric_limits<_Tp>::digits - 1 - std::__countl_zero(__t);
2927
}
3028

31-
#endif // _LIBCPP_STD_VER >= 14
32-
3329
_LIBCPP_END_NAMESPACE_STD
3430

3531
#endif // _LIBCPP___BIT_BIT_LOG2_H

0 commit comments

Comments
 (0)