Skip to content

Commit 68c2eb6

Browse files
committed
[libc++] Forward to memcmp in lexicographical_compare
1 parent 69fecaa commit 68c2eb6

File tree

8 files changed

+139
-86
lines changed

8 files changed

+139
-86
lines changed

libcxx/include/__algorithm/comp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <__config>
1313
#include <__type_traits/desugars_to.h>
14+
#include <__type_traits/is_integral.h>
1415

1516
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1617
# pragma GCC system_header
@@ -42,7 +43,7 @@ struct __less<void, void> {
4243
};
4344

4445
template <class _Tp>
45-
inline const bool __desugars_to_v<__less_tag, __less<>, _Tp, _Tp> = true;
46+
inline const bool __desugars_to_v<__totally_ordered_less_tag, __less<>, _Tp, _Tp> = is_integral<_Tp>::value;
4647

4748
_LIBCPP_END_NAMESPACE_STD
4849

libcxx/include/__algorithm/lexicographical_compare.h

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,96 @@
1111

1212
#include <__algorithm/comp.h>
1313
#include <__algorithm/comp_ref_type.h>
14+
#include <__algorithm/max.h>
15+
#include <__algorithm/mismatch.h>
16+
#include <__algorithm/simd_utils.h>
1417
#include <__config>
18+
#include <__functional/identity.h>
1519
#include <__iterator/iterator_traits.h>
20+
#include <__string/constexpr_c_functions.h>
21+
#include <__type_traits/desugars_to.h>
22+
#include <__type_traits/invoke.h>
23+
#include <__type_traits/is_equality_comparable.h>
24+
#include <__type_traits/is_integral.h>
25+
#include <__type_traits/is_volatile.h>
26+
#include <cstring>
27+
#include <cwchar>
1628

1729
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1830
# pragma GCC system_header
1931
#endif
2032

2133
_LIBCPP_BEGIN_NAMESPACE_STD
2234

23-
template <class _Compare, class _InputIterator1, class _InputIterator2>
35+
template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Proj1, class _Proj2, class _Comp>
2436
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __lexicographical_compare(
25-
_InputIterator1 __first1,
26-
_InputIterator1 __last1,
27-
_InputIterator2 __first2,
28-
_InputIterator2 __last2,
29-
_Compare __comp) {
30-
for (; __first2 != __last2; ++__first1, (void)++__first2) {
31-
if (__first1 == __last1 || __comp(*__first1, *__first2))
37+
_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Comp& __comp, _Proj1& __proj1, _Proj2& __proj2) {
38+
while (__first2 != __last2) {
39+
if (__first1 == __last1 ||
40+
std::__invoke(__comp, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
3241
return true;
33-
if (__comp(*__first2, *__first1))
42+
if (std::__invoke(__comp, std::__invoke(__proj2, *__first2), std::__invoke(__proj1, *__first1)))
3443
return false;
44+
++__first1;
45+
++__first2;
3546
}
3647
return false;
3748
}
3849

50+
#if _LIBCPP_VECTORIZE_ALGORITHMS
51+
52+
template <class _Tp,
53+
class _Proj1,
54+
class _Proj2,
55+
class _Comp,
56+
__enable_if_t<__desugars_to_v<__totally_ordered_less_tag, _Comp, _Tp, _Tp> && !is_volatile<_Tp>::value &&
57+
__libcpp_is_trivially_equality_comparable<_Tp, _Tp>::value &&
58+
__is_identity<_Proj1>::value && __is_identity<_Proj2>::value,
59+
int> = 0>
60+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __lexicographical_compare(
61+
_Tp* __first1, _Tp* __last1, _Tp* __first2, _Tp* __last2, _Comp& __comp, _Proj1& __proj1, _Proj2& __proj2) {
62+
if constexpr (__is_trivially_lexicographically_comparable_v<_Tp, _Tp>) {
63+
auto __res =
64+
std::__constexpr_memcmp(__first1, __first2, __element_count(std::min(__last1 - __first1, __last2 - __first2)));
65+
if (__res == 0)
66+
return __last1 - __first1 < __last2 - __first2;
67+
return __res < 0;
68+
}
69+
# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
70+
else if constexpr (is_same<__remove_cv_t<_Tp>, wchar_t>::value) {
71+
auto __res = std::__constexpr_wmemcmp(__first1, __first2, std::min(__last1 - __first1, __last2 - __first2));
72+
if (__res == 0)
73+
return __last1 - __first1 < __last2 - __first2;
74+
return __res < 0;
75+
}
76+
# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
77+
else {
78+
auto __res = std::mismatch(__first1, __last1, __first2, __last2);
79+
if (__res.second == __last2)
80+
return false;
81+
if (__res.first == __last1)
82+
return true;
83+
return *__res.first < *__res.second;
84+
}
85+
}
86+
#endif // _LIBCPP_VECTORIZE_ALGORITHMS
87+
3988
template <class _InputIterator1, class _InputIterator2, class _Compare>
4089
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool lexicographical_compare(
4190
_InputIterator1 __first1,
4291
_InputIterator1 __last1,
4392
_InputIterator2 __first2,
4493
_InputIterator2 __last2,
4594
_Compare __comp) {
46-
return std::__lexicographical_compare<__comp_ref_type<_Compare> >(__first1, __last1, __first2, __last2, __comp);
95+
__identity __proj;
96+
return std::__lexicographical_compare(
97+
std::__unwrap_iter(__first1),
98+
std::__unwrap_iter(__last1),
99+
std::__unwrap_iter(__first2),
100+
std::__unwrap_iter(__last2),
101+
__comp,
102+
__proj,
103+
__proj);
47104
}
48105

49106
template <class _InputIterator1, class _InputIterator2>

libcxx/include/__algorithm/ranges_lexicographical_compare.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef _LIBCPP___ALGORITHM_RANGES_LEXICOGRAPHICAL_COMPARE_H
1010
#define _LIBCPP___ALGORITHM_RANGES_LEXICOGRAPHICAL_COMPARE_H
1111

12+
#include <__algorithm/lexicographical_compare.h>
13+
#include <__algorithm/unwrap_range.h>
1214
#include <__config>
1315
#include <__functional/identity.h>
1416
#include <__functional/invoke.h>
@@ -34,23 +36,24 @@ namespace ranges {
3436
namespace __lexicographical_compare {
3537
struct __fn {
3638
template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Proj1, class _Proj2, class _Comp>
37-
_LIBCPP_HIDE_FROM_ABI constexpr static bool __lexicographical_compare_impl(
39+
static _LIBCPP_HIDE_FROM_ABI constexpr bool __lexicographical_compare_unwrap(
3840
_Iter1 __first1,
3941
_Sent1 __last1,
4042
_Iter2 __first2,
4143
_Sent2 __last2,
4244
_Comp& __comp,
4345
_Proj1& __proj1,
4446
_Proj2& __proj2) {
45-
while (__first2 != __last2) {
46-
if (__first1 == __last1 || std::invoke(__comp, std::invoke(__proj1, *__first1), std::invoke(__proj2, *__first2)))
47-
return true;
48-
if (std::invoke(__comp, std::invoke(__proj2, *__first2), std::invoke(__proj1, *__first1)))
49-
return false;
50-
++__first1;
51-
++__first2;
52-
}
53-
return false;
47+
auto [__first1_un, __last1_un] = std::__unwrap_range(std::move(__first1), std::move(__last1));
48+
auto [__first2_un, __last2_un] = std::__unwrap_range(std::move(__first2), std::move(__last2));
49+
return std::__lexicographical_compare(
50+
std::move(__first1_un),
51+
std::move(__last1_un),
52+
std::move(__first2_un),
53+
std::move(__last2_un),
54+
__comp,
55+
__proj1,
56+
__proj2);
5457
}
5558

5659
template <input_iterator _Iter1,
@@ -68,7 +71,7 @@ struct __fn {
6871
_Comp __comp = {},
6972
_Proj1 __proj1 = {},
7073
_Proj2 __proj2 = {}) const {
71-
return __lexicographical_compare_impl(
74+
return __lexicographical_compare_unwrap(
7275
std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __comp, __proj1, __proj2);
7376
}
7477

@@ -80,7 +83,7 @@ struct __fn {
8083
_Comp = ranges::less>
8184
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
8285
_Range1&& __range1, _Range2&& __range2, _Comp __comp = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
83-
return __lexicographical_compare_impl(
86+
return __lexicographical_compare_unwrap(
8487
ranges::begin(__range1),
8588
ranges::end(__range1),
8689
ranges::begin(__range2),

libcxx/include/__functional/ranges_operations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ template <class _Tp, class _Up>
100100
inline const bool __desugars_to_v<__equal_tag, ranges::equal_to, _Tp, _Up> = true;
101101

102102
template <class _Tp, class _Up>
103-
inline const bool __desugars_to_v<__less_tag, ranges::less, _Tp, _Up> = true;
103+
inline const bool __desugars_to_v<__totally_ordered_less_tag, ranges::less, _Tp, _Up> = true;
104104

105105
#endif // _LIBCPP_STD_VER >= 20
106106

libcxx/include/__string/constexpr_c_functions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 size_t __constexpr_st
6464
return __builtin_strlen(reinterpret_cast<const char*>(__str));
6565
}
6666

67-
// Because of __libcpp_is_trivially_lexicographically_comparable we know that comparing the object representations is
67+
// Because of __is_trivially_lexicographically_comparable_v we know that comparing the object representations is
6868
// equivalent to a std::memcmp. Since we have multiple objects contiguously in memory, we can call memcmp once instead
6969
// of invoking it on every object individually.
7070
template <class _Tp, class _Up>
7171
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int
7272
__constexpr_memcmp(const _Tp* __lhs, const _Up* __rhs, __element_count __n) {
73-
static_assert(__libcpp_is_trivially_lexicographically_comparable<_Tp, _Up>::value,
73+
static_assert(__is_trivially_lexicographically_comparable_v<_Tp, _Up>,
7474
"_Tp and _Up have to be trivially lexicographically comparable");
7575

7676
auto __count = static_cast<size_t>(__n);

libcxx/include/__type_traits/desugars_to.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2020
// Tags to represent the canonical operations
2121
struct __equal_tag {};
2222
struct __plus_tag {};
23-
struct __less_tag {};
23+
struct __totally_ordered_less_tag {};
2424

2525
// This class template is used to determine whether an operation "desugars"
2626
// (or boils down) to a given canonical operation.

libcxx/include/__type_traits/is_trivially_lexicographically_comparable.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <__type_traits/remove_cv.h>
1717
#include <__type_traits/void_t.h>
1818
#include <__utility/declval.h>
19+
#include <cstddef>
1920

2021
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2122
# pragma GCC system_header
@@ -40,13 +41,22 @@ _LIBCPP_BEGIN_NAMESPACE_STD
4041
// unsigned integer types with sizeof(T) > 1: depending on the endianness, the LSB might be the first byte to be
4142
// compared. This means that when comparing unsigned(129) and unsigned(2)
4243
// using memcmp(), the result would be that 2 > 129.
43-
// TODO: Do we want to enable this on big-endian systems?
44+
45+
template <class _Tp>
46+
inline const bool __is_std_byte_v = false;
47+
48+
#if _LIBCPP_STD_VER >= 17
49+
template <>
50+
inline const bool __is_std_byte_v<byte> = true;
51+
#endif
4452

4553
template <class _Tp, class _Up>
46-
struct __libcpp_is_trivially_lexicographically_comparable
47-
: integral_constant<bool,
48-
is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up> >::value && sizeof(_Tp) == 1 &&
49-
is_unsigned<_Tp>::value> {};
54+
inline const bool __is_trivially_lexicographically_comparable_v =
55+
is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up> >::value &&
56+
#ifdef _LIBCPP_LITTLE_ENDIAN
57+
sizeof(_Tp) == 1 &&
58+
#endif
59+
(is_unsigned<_Tp>::value || __is_std_byte_v<_Tp>);
5060

5161
_LIBCPP_END_NAMESPACE_STD
5262

libcxx/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,66 +20,48 @@
2020
#include "test_macros.h"
2121
#include "test_iterators.h"
2222

23-
#if TEST_STD_VER > 17
24-
TEST_CONSTEXPR bool test_constexpr() {
25-
int ia[] = {1, 2, 3};
26-
int ib[] = {1, 3, 5, 2, 4, 6};
23+
template <class T, class Iter1>
24+
struct Test {
25+
template <class Iter2>
26+
TEST_CONSTEXPR_CXX20 void operator()() {
27+
T ia[] = {1, 2, 3, 4};
28+
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
29+
T ib[] = {1, 2, 3};
30+
assert(!std::lexicographical_compare(Iter1(ia), Iter1(ia + sa), Iter2(ib), Iter2(ib + 2)));
31+
assert(std::lexicographical_compare(Iter1(ib), Iter1(ib + 2), Iter2(ia), Iter2(ia + sa)));
32+
assert(!std::lexicographical_compare(Iter1(ia), Iter1(ia + sa), Iter2(ib), Iter2(ib + 3)));
33+
assert(std::lexicographical_compare(Iter1(ib), Iter1(ib + 3), Iter2(ia), Iter2(ia + sa)));
34+
assert(std::lexicographical_compare(Iter1(ia), Iter1(ia + sa), Iter2(ib + 1), Iter2(ib + 3)));
35+
assert(!std::lexicographical_compare(Iter1(ib + 1), Iter1(ib + 3), Iter2(ia), Iter2(ia + sa)));
36+
}
37+
};
2738

28-
return std::lexicographical_compare(std::begin(ia), std::end(ia), std::begin(ib), std::end(ib))
29-
&& !std::lexicographical_compare(std::begin(ib), std::end(ib), std::begin(ia), std::end(ia))
30-
;
31-
}
32-
#endif
33-
34-
template <class Iter1, class Iter2>
35-
void
36-
test()
37-
{
38-
int ia[] = {1, 2, 3, 4};
39-
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
40-
int ib[] = {1, 2, 3};
41-
assert(!std::lexicographical_compare(Iter1(ia), Iter1(ia+sa), Iter2(ib), Iter2(ib+2)));
42-
assert( std::lexicographical_compare(Iter1(ib), Iter1(ib+2), Iter2(ia), Iter2(ia+sa)));
43-
assert(!std::lexicographical_compare(Iter1(ia), Iter1(ia+sa), Iter2(ib), Iter2(ib+3)));
44-
assert( std::lexicographical_compare(Iter1(ib), Iter1(ib+3), Iter2(ia), Iter2(ia+sa)));
45-
assert( std::lexicographical_compare(Iter1(ia), Iter1(ia+sa), Iter2(ib+1), Iter2(ib+3)));
46-
assert(!std::lexicographical_compare(Iter1(ib+1), Iter1(ib+3), Iter2(ia), Iter2(ia+sa)));
47-
}
39+
template <class T>
40+
struct TestIter {
41+
template <class Iter1>
42+
TEST_CONSTEXPR_CXX20 bool operator()() {
43+
types::for_each(types::cpp17_input_iterator_list<T*>(), Test<T, Iter1>());
4844

49-
int main(int, char**)
50-
{
51-
test<cpp17_input_iterator<const int*>, cpp17_input_iterator<const int*> >();
52-
test<cpp17_input_iterator<const int*>, forward_iterator<const int*> >();
53-
test<cpp17_input_iterator<const int*>, bidirectional_iterator<const int*> >();
54-
test<cpp17_input_iterator<const int*>, random_access_iterator<const int*> >();
55-
test<cpp17_input_iterator<const int*>, const int*>();
45+
return true;
46+
}
47+
};
5648

57-
test<forward_iterator<const int*>, cpp17_input_iterator<const int*> >();
58-
test<forward_iterator<const int*>, forward_iterator<const int*> >();
59-
test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
60-
test<forward_iterator<const int*>, random_access_iterator<const int*> >();
61-
test<forward_iterator<const int*>, const int*>();
62-
63-
test<bidirectional_iterator<const int*>, cpp17_input_iterator<const int*> >();
64-
test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
65-
test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
66-
test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
67-
test<bidirectional_iterator<const int*>, const int*>();
49+
TEST_CONSTEXPR_CXX20 bool test() {
50+
types::for_each(types::cpp17_input_iterator_list<const int*>(), TestIter<const int>());
51+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
52+
types::for_each(types::cpp17_input_iterator_list<const wchar_t*>(), TestIter<const wchar_t>());
53+
#endif
54+
types::for_each(types::cpp17_input_iterator_list<const char*>(), TestIter<const char>());
55+
types::for_each(types::cpp17_input_iterator_list<unsigned char*>(), TestIter<unsigned char>());
6856

69-
test<random_access_iterator<const int*>, cpp17_input_iterator<const int*> >();
70-
test<random_access_iterator<const int*>, forward_iterator<const int*> >();
71-
test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
72-
test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
73-
test<random_access_iterator<const int*>, const int*>();
57+
return true;
58+
}
7459

75-
test<const int*, cpp17_input_iterator<const int*> >();
76-
test<const int*, forward_iterator<const int*> >();
77-
test<const int*, bidirectional_iterator<const int*> >();
78-
test<const int*, random_access_iterator<const int*> >();
79-
test<const int*, const int*>();
60+
int main(int, char**) {
61+
test();
8062

81-
#if TEST_STD_VER > 17
82-
static_assert(test_constexpr());
63+
#if TEST_STD_VER >= 20
64+
static_assert(test());
8365
#endif
8466

8567
return 0;

0 commit comments

Comments
 (0)