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