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