mismatch.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___ALGORITHM_MISMATCH_H
  10. #define _LIBCPP___ALGORITHM_MISMATCH_H
  11. #include <__algorithm/comp.h>
  12. #include <__algorithm/min.h>
  13. #include <__algorithm/simd_utils.h>
  14. #include <__algorithm/unwrap_iter.h>
  15. #include <__config>
  16. #include <__functional/identity.h>
  17. #include <__type_traits/invoke.h>
  18. #include <__type_traits/is_constant_evaluated.h>
  19. #include <__type_traits/is_equality_comparable.h>
  20. #include <__type_traits/is_integral.h>
  21. #include <__type_traits/operation_traits.h>
  22. #include <__utility/move.h>
  23. #include <__utility/pair.h>
  24. #include <__utility/unreachable.h>
  25. #include <cstddef>
  26. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  27. # pragma GCC system_header
  28. #endif
  29. _LIBCPP_PUSH_MACROS
  30. #include <__undef_macros>
  31. _LIBCPP_BEGIN_NAMESPACE_STD
  32. template <class _Iter1, class _Sent1, class _Iter2, class _Pred, class _Proj1, class _Proj2>
  33. _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter1, _Iter2>
  34. __mismatch_loop(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
  35. while (__first1 != __last1) {
  36. if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
  37. break;
  38. ++__first1;
  39. ++__first2;
  40. }
  41. return std::make_pair(std::move(__first1), std::move(__first2));
  42. }
  43. template <class _Iter1, class _Sent1, class _Iter2, class _Pred, class _Proj1, class _Proj2>
  44. _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter1, _Iter2>
  45. __mismatch(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
  46. return std::__mismatch_loop(__first1, __last1, __first2, __pred, __proj1, __proj2);
  47. }
  48. #if _LIBCPP_VECTORIZE_ALGORITHMS
  49. template <class _Tp,
  50. class _Pred,
  51. class _Proj1,
  52. class _Proj2,
  53. __enable_if_t<is_integral<_Tp>::value && __desugars_to<__equal_tag, _Pred, _Tp, _Tp>::value &&
  54. __is_identity<_Proj1>::value && __is_identity<_Proj2>::value,
  55. int> = 0>
  56. _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Tp*, _Tp*>
  57. __mismatch(_Tp* __first1, _Tp* __last1, _Tp* __first2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
  58. constexpr size_t __unroll_count = 4;
  59. constexpr size_t __vec_size = __native_vector_size<_Tp>;
  60. using __vec = __simd_vector<_Tp, __vec_size>;
  61. if (!__libcpp_is_constant_evaluated()) {
  62. auto __orig_first1 = __first1;
  63. auto __last2 = __first2 + (__last1 - __first1);
  64. while (static_cast<size_t>(__last1 - __first1) >= __unroll_count * __vec_size) [[__unlikely__]] {
  65. __vec __lhs[__unroll_count];
  66. __vec __rhs[__unroll_count];
  67. for (size_t __i = 0; __i != __unroll_count; ++__i) {
  68. __lhs[__i] = std::__load_vector<__vec>(__first1 + __i * __vec_size);
  69. __rhs[__i] = std::__load_vector<__vec>(__first2 + __i * __vec_size);
  70. }
  71. for (size_t __i = 0; __i != __unroll_count; ++__i) {
  72. if (auto __cmp_res = __lhs[__i] == __rhs[__i]; !std::__all_of(__cmp_res)) {
  73. auto __offset = __i * __vec_size + std::__find_first_not_set(__cmp_res);
  74. return {__first1 + __offset, __first2 + __offset};
  75. }
  76. }
  77. __first1 += __unroll_count * __vec_size;
  78. __first2 += __unroll_count * __vec_size;
  79. }
  80. // check the remaining 0-3 vectors
  81. while (static_cast<size_t>(__last1 - __first1) >= __vec_size) {
  82. if (auto __cmp_res = std::__load_vector<__vec>(__first1) == std::__load_vector<__vec>(__first2);
  83. !std::__all_of(__cmp_res)) {
  84. auto __offset = std::__find_first_not_set(__cmp_res);
  85. return {__first1 + __offset, __first2 + __offset};
  86. }
  87. __first1 += __vec_size;
  88. __first2 += __vec_size;
  89. }
  90. if (__last1 - __first1 == 0)
  91. return {__first1, __first2};
  92. // Check if we can load elements in front of the current pointer. If that's the case load a vector at
  93. // (last - vector_size) to check the remaining elements
  94. if (static_cast<size_t>(__first1 - __orig_first1) >= __vec_size) {
  95. __first1 = __last1 - __vec_size;
  96. __first2 = __last2 - __vec_size;
  97. auto __offset =
  98. std::__find_first_not_set(std::__load_vector<__vec>(__first1) == std::__load_vector<__vec>(__first2));
  99. return {__first1 + __offset, __first2 + __offset};
  100. } // else loop over the elements individually
  101. }
  102. return std::__mismatch_loop(__first1, __last1, __first2, __pred, __proj1, __proj2);
  103. }
  104. #endif // _LIBCPP_VECTORIZE_ALGORITHMS
  105. template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
  106. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InputIterator1, _InputIterator2>
  107. mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred) {
  108. __identity __proj;
  109. auto __res = std::__mismatch(
  110. std::__unwrap_iter(__first1), std::__unwrap_iter(__last1), std::__unwrap_iter(__first2), __pred, __proj, __proj);
  111. return std::make_pair(std::__rewrap_iter(__first1, __res.first), std::__rewrap_iter(__first2, __res.second));
  112. }
  113. template <class _InputIterator1, class _InputIterator2>
  114. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InputIterator1, _InputIterator2>
  115. mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) {
  116. return std::mismatch(__first1, __last1, __first2, __equal_to());
  117. }
  118. #if _LIBCPP_STD_VER >= 14
  119. template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
  120. [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter1, _Iter2> __mismatch(
  121. _Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
  122. while (__first1 != __last1 && __first2 != __last2) {
  123. if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
  124. break;
  125. ++__first1;
  126. ++__first2;
  127. }
  128. return {std::move(__first1), std::move(__first2)};
  129. }
  130. template <class _Tp, class _Pred, class _Proj1, class _Proj2>
  131. [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Tp*, _Tp*>
  132. __mismatch(_Tp* __first1, _Tp* __last1, _Tp* __first2, _Tp* __last2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
  133. auto __len = std::min(__last1 - __first1, __last2 - __first2);
  134. return std::__mismatch(__first1, __first1 + __len, __first2, __pred, __proj1, __proj2);
  135. }
  136. template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
  137. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InputIterator1, _InputIterator2>
  138. mismatch(_InputIterator1 __first1,
  139. _InputIterator1 __last1,
  140. _InputIterator2 __first2,
  141. _InputIterator2 __last2,
  142. _BinaryPredicate __pred) {
  143. __identity __proj;
  144. auto __res = std::__mismatch(
  145. std::__unwrap_iter(__first1),
  146. std::__unwrap_iter(__last1),
  147. std::__unwrap_iter(__first2),
  148. std::__unwrap_iter(__last2),
  149. __pred,
  150. __proj,
  151. __proj);
  152. return {std::__rewrap_iter(__first1, __res.first), std::__rewrap_iter(__first2, __res.second)};
  153. }
  154. template <class _InputIterator1, class _InputIterator2>
  155. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InputIterator1, _InputIterator2>
  156. mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
  157. return std::mismatch(__first1, __last1, __first2, __last2, __equal_to());
  158. }
  159. #endif
  160. _LIBCPP_END_NAMESPACE_STD
  161. _LIBCPP_POP_MACROS
  162. #endif // _LIBCPP___ALGORITHM_MISMATCH_H