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___ALGORITHM_PSTL_BACKENDS_CPU_BACKEND_ANY_OF_H
10 #define _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKEND_ANY_OF_H
11
12 #include <__algorithm/any_of.h>
13 #include <__algorithm/find_if.h>
14 #include <__algorithm/pstl_backends/cpu_backends/backend.h>
15 #include <__atomic/atomic.h>
16 #include <__atomic/memory_order.h>
17 #include <__config>
18 #include <__functional/operations.h>
19 #include <__iterator/concepts.h>
20 #include <__type_traits/is_execution_policy.h>
21 #include <__utility/move.h>
22 #include <__utility/pair.h>
23 #include <cstdint>
24 #include <optional>
25
26 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
27
28 _LIBCPP_PUSH_MACROS
29 # include <__undef_macros>
30
31 _LIBCPP_BEGIN_NAMESPACE_STD
32
33 template <class _Index, class _Brick>
__parallel_or(_Index __first,_Index __last,_Brick __f)34 _LIBCPP_HIDE_FROM_ABI optional<bool> __parallel_or(_Index __first, _Index __last, _Brick __f) {
35 std::atomic<bool> __found(false);
36 auto __ret = __par_backend::__parallel_for(__first, __last, [__f, &__found](_Index __i, _Index __j) {
37 if (!__found.load(std::memory_order_relaxed) && __f(__i, __j)) {
38 __found.store(true, std::memory_order_relaxed);
39 __par_backend::__cancel_execution();
40 }
41 });
42 if (!__ret)
43 return nullopt;
44 return static_cast<bool>(__found);
45 }
46
47 // TODO: check whether __simd_first() can be used here
48 template <class _Index, class _DifferenceType, class _Pred>
__simd_or(_Index __first,_DifferenceType __n,_Pred __pred)49 _LIBCPP_HIDE_FROM_ABI bool __simd_or(_Index __first, _DifferenceType __n, _Pred __pred) noexcept {
50 _DifferenceType __block_size = 4 < __n ? 4 : __n;
51 const _Index __last = __first + __n;
52 while (__last != __first) {
53 int32_t __flag = 1;
54 _PSTL_PRAGMA_SIMD_REDUCTION(& : __flag)
55 for (_DifferenceType __i = 0; __i < __block_size; ++__i)
56 if (__pred(*(__first + __i)))
57 __flag = 0;
58 if (!__flag)
59 return true;
60
61 __first += __block_size;
62 if (__last - __first >= __block_size << 1) {
63 // Double the block _Size. Any unnecessary iterations can be amortized against work done so far.
64 __block_size <<= 1;
65 } else {
66 __block_size = __last - __first;
67 }
68 }
69 return false;
70 }
71
72 template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate>
73 _LIBCPP_HIDE_FROM_ABI optional<bool>
__pstl_any_of(__cpu_backend_tag,_ForwardIterator __first,_ForwardIterator __last,_Predicate __pred)74 __pstl_any_of(__cpu_backend_tag, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {
75 if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> &&
76 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
77 return std::__parallel_or(
78 __first, __last, [&__pred](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
79 auto __res = std::__pstl_any_of<__remove_parallel_policy_t<_ExecutionPolicy>>(
80 __cpu_backend_tag{}, __brick_first, __brick_last, __pred);
81 _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!");
82 return *std::move(__res);
83 });
84 } else if constexpr (__is_unsequenced_execution_policy_v<_ExecutionPolicy> &&
85 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
86 return std::__simd_or(__first, __last - __first, __pred);
87 } else {
88 return std::any_of(__first, __last, __pred);
89 }
90 }
91
92 _LIBCPP_END_NAMESPACE_STD
93
94 _LIBCPP_POP_MACROS
95
96 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
97
98 #endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKEND_ANY_OF_H
99