1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <algorithm>
10#include <bit>
11
12_LIBCPP_BEGIN_NAMESPACE_STD
13_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
14
15template <class Comp, class RandomAccessIterator>
16void __sort(RandomAccessIterator first, RandomAccessIterator last, Comp comp) {
17 if (first == last) // log(0) is undefined, so don't try computing the depth
18 return;
19
20 auto depth_limit = 2 * std::__bit_log2(t: static_cast<size_t>(last - first));
21
22 // Only use bitset partitioning for arithmetic types. We should also check
23 // that the default comparator is in use so that we are sure that there are no
24 // branches in the comparator.
25 std::__introsort<_ClassicAlgPolicy,
26 ranges::less,
27 RandomAccessIterator,
28 __use_branchless_sort<ranges::less, RandomAccessIterator>>(first, last, ranges::less{}, depth_limit);
29}
30
31// clang-format off
32template void __sort<__less<char>&, char*>(char*, char*, __less<char>&);
33#if _LIBCPP_HAS_WIDE_CHARACTERS
34template void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&);
35#endif
36template void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&);
37template void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&);
38template void __sort<__less<short>&, short*>(short*, short*, __less<short>&);
39template void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&);
40template void __sort<__less<int>&, int*>(int*, int*, __less<int>&);
41template void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&);
42template void __sort<__less<long>&, long*>(long*, long*, __less<long>&);
43template void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&);
44template void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&);
45template void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&);
46template void __sort<__less<float>&, float*>(float*, float*, __less<float>&);
47template void __sort<__less<double>&, double*>(double*, double*, __less<double>&);
48template void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&);
49// clang-format on
50
51_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
52_LIBCPP_END_NAMESPACE_STD
53