| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef _LIBCPP___PSTL_BACKENDS_STATPART_FIND_IF_H |
| 10 | #define _LIBCPP___PSTL_BACKENDS_STATPART_FIND_IF_H |
| 11 | |
| 12 | #include <__algorithm/find_if.h> |
| 13 | #include <__assert> |
| 14 | #include <__config> |
| 15 | #include <__execution/is_execution_policy.h> |
| 16 | #include <__functional/operations.h> |
| 17 | #include <__iterator/concepts.h> |
| 18 | #include <__iterator/iterator_traits.h> |
| 19 | #include <__optional/nullopt_t.h> |
| 20 | #include <__optional/optional.h> |
| 21 | #include <__pstl/backend_fwd.h> |
| 22 | #include <__pstl/backends/statpart/basis.h> |
| 23 | #include <__utility/empty.h> |
| 24 | #include <__utility/move.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 | #if _LIBCPP_STD_VER >= 17 |
| 34 | |
| 35 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 36 | _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS |
| 37 | namespace __pstl { |
| 38 | namespace __statpart { |
| 39 | |
| 40 | template <class _Index, class _DifferenceType, class _Compare> |
| 41 | _LIBCPP_HIDE_FROM_ABI _Index |
| 42 | __simd_first(_Index __first, _DifferenceType __begin, _DifferenceType __end, _Compare __comp) noexcept { |
| 43 | // Experiments show good block sizes like this |
| 44 | const _DifferenceType __block_size = 8; |
| 45 | alignas(__statpart::__lane_size) _DifferenceType __lane[__block_size] = {0}; |
| 46 | while (__end - __begin >= __block_size) { |
| 47 | _DifferenceType __found = 0; |
| 48 | _PSTL_PRAGMA_SIMD_REDUCTION(| : __found) for (_DifferenceType __i = __begin; __i < __begin + __block_size; ++__i) { |
| 49 | const _DifferenceType __t = __comp(__first, __i); |
| 50 | __lane[__i - __begin] = __t; |
| 51 | __found |= __t; |
| 52 | } |
| 53 | if (__found) { |
| 54 | _DifferenceType __i; |
| 55 | // This will vectorize |
| 56 | for (__i = 0; __i < __block_size; ++__i) { |
| 57 | if (__lane[__i]) { |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | return __first + __begin + __i; |
| 62 | } |
| 63 | __begin += __block_size; |
| 64 | } |
| 65 | |
| 66 | // Keep remainder scalar |
| 67 | while (__begin != __end) { |
| 68 | if (__comp(__first, __begin)) { |
| 69 | return __first + __begin; |
| 70 | } |
| 71 | ++__begin; |
| 72 | } |
| 73 | return __first + __end; |
| 74 | } |
| 75 | |
| 76 | } // namespace __statpart |
| 77 | |
| 78 | template <class _ExecutionPolicy> |
| 79 | struct __find_if<__statpart_backend_tag, _ExecutionPolicy> { |
| 80 | template <class _Policy, class _ForwardIterator, class _Predicate> |
| 81 | _LIBCPP_HIDE_FROM_ABI optional<_ForwardIterator> |
| 82 | operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) const noexcept { |
| 83 | if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> && |
| 84 | __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) { |
| 85 | return __statpart::__find( |
| 86 | std::move(__first), |
| 87 | std::move(__last), |
| 88 | [&__policy, &__pred](_ForwardIterator __brick_first, _ForwardIterator __brick_last) { |
| 89 | using _FindIfUnseq = |
| 90 | __pstl::__find_if<__statpart_backend_tag, __remove_parallel_policy_t<_ExecutionPolicy>>; |
| 91 | auto __res = _FindIfUnseq()(std::__remove_parallel_policy(__policy), __brick_first, __brick_last, __pred); |
| 92 | _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!" ); |
| 93 | return *std::move(__res); |
| 94 | }, |
| 95 | less<>{}, |
| 96 | true); |
| 97 | } else if constexpr (__is_unsequenced_execution_policy_v<_ExecutionPolicy> && |
| 98 | __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) { |
| 99 | using __diff_t = __iterator_difference_type<_ForwardIterator>; |
| 100 | return __statpart::__simd_first( |
| 101 | __first, __diff_t(0), __last - __first, [&__pred](_ForwardIterator __iter, __diff_t __i) { |
| 102 | return __pred(__iter[__i]); |
| 103 | }); |
| 104 | } else { |
| 105 | return std::find_if(std::move(__first), std::move(__last), std::move(__pred)); |
| 106 | } |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | } // namespace __pstl |
| 111 | _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS |
| 112 | _LIBCPP_END_NAMESPACE_STD |
| 113 | |
| 114 | #endif // _LIBCPP_STD_VER >= 17 |
| 115 | |
| 116 | _LIBCPP_POP_MACROS |
| 117 | |
| 118 | #endif // _LIBCPP___PSTL_BACKENDS_STATPART_FIND_IF_H |
| 119 | |