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