Skip to content

Commit ab12211

Browse files
alexrpxtexx
authored andcommitted
libcxx: backport llvm/llvm-project#155476
https: //github.com/llvm/llvm-project/pull/155476 (cherry picked from commit 820dc9d) Change-Id: I01c204c23df89dec6f7be874a079ddc54e376b36
1 parent fc8ca4d commit ab12211

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

lib/libcxx/include/__algorithm/sort.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,9 @@ __sort<__less<long double>&, long double*>(long double*, long double*, __less<lo
879879
template <class _AlgPolicy, class _RandomAccessIterator, class _Comp>
880880
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
881881
__sort_dispatch(_RandomAccessIterator __first, _RandomAccessIterator __last, _Comp& __comp) {
882+
if (__first == __last) // log(0) is undefined, so don't try computing the depth
883+
return;
884+
882885
typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
883886
difference_type __depth_limit = 2 * std::__log2i(__last - __first);
884887

lib/libcxx/include/__bit/bit_log2.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef _LIBCPP___BIT_BIT_LOG2_H
1010
#define _LIBCPP___BIT_BIT_LOG2_H
1111

12+
#include <__assert>
1213
#include <__bit/countl.h>
1314
#include <__config>
1415
#include <__type_traits/is_unsigned_integer.h>
@@ -25,6 +26,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2526
template <class _Tp>
2627
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __bit_log2(_Tp __t) noexcept {
2728
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
29+
_LIBCPP_ASSERT_INTERNAL(__t != 0, "logarithm of 0 is undefined");
2830
return numeric_limits<_Tp>::digits - 1 - std::__countl_zero(__t);
2931
}
3032

lib/libcxx/src/algorithm.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
1313

1414
template <class Comp, class RandomAccessIterator>
1515
void __sort(RandomAccessIterator first, RandomAccessIterator last, Comp comp) {
16+
if (first == last) // log(0) is undefined, so don't try computing the depth
17+
return;
18+
1619
auto depth_limit = 2 * std::__bit_log2(static_cast<size_t>(last - first));
1720

1821
// Only use bitset partitioning for arithmetic types. We should also check

0 commit comments

Comments
 (0)