xref: /llvm-project/libcxx/include/__pstl/cpu_algos/any_of.h (revision bbff52bfd49336bc0fdc83d8dfc616266bc07cbf)
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_CPU_ALGOS_ANY_OF_H
10 #define _LIBCPP___PSTL_CPU_ALGOS_ANY_OF_H
11 
12 #include <__algorithm/any_of.h>
13 #include <__assert>
14 #include <__atomic/atomic.h>
15 #include <__atomic/memory_order.h>
16 #include <__config>
17 #include <__iterator/concepts.h>
18 #include <__pstl/backend_fwd.h>
19 #include <__pstl/cpu_algos/cpu_traits.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 _LIBCPP_PUSH_MACROS
27 #include <__undef_macros>
28 
29 #if _LIBCPP_STD_VER >= 17
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 namespace __pstl {
33 
34 template <class _Backend, class _Index, class _Brick>
35 _LIBCPP_HIDE_FROM_ABI optional<bool> __parallel_or(_Index __first, _Index __last, _Brick __f) {
36   std::atomic<bool> __found(false);
37   auto __ret = __cpu_traits<_Backend>::__for_each(__first, __last, [__f, &__found](_Index __i, _Index __j) {
38     if (!__found.load(std::memory_order_relaxed) && __f(__i, __j)) {
39       __found.store(true, std::memory_order_relaxed);
40       __cpu_traits<_Backend>::__cancel_execution();
41     }
42   });
43   if (!__ret)
44     return nullopt;
45   return static_cast<bool>(__found);
46 }
47 
48 // TODO: check whether __simd_first() can be used here
49 template <class _Index, class _DifferenceType, class _Pred>
50 _LIBCPP_HIDE_FROM_ABI bool __simd_or(_Index __first, _DifferenceType __n, _Pred __pred) noexcept {
51   _DifferenceType __block_size = 4 < __n ? 4 : __n;
52   const _Index __last          = __first + __n;
53   while (__last != __first) {
54     int32_t __flag = 1;
55     _PSTL_PRAGMA_SIMD_REDUCTION(& : __flag)
56     for (_DifferenceType __i = 0; __i < __block_size; ++__i)
57       if (__pred(*(__first + __i)))
58         __flag = 0;
59     if (!__flag)
60       return true;
61 
62     __first += __block_size;
63     if (__last - __first >= __block_size << 1) {
64       // Double the block _Size.  Any unnecessary iterations can be amortized against work done so far.
65       __block_size <<= 1;
66     } else {
67       __block_size = __last - __first;
68     }
69   }
70   return false;
71 }
72 
73 template <class _Backend, class _RawExecutionPolicy>
74 struct __cpu_parallel_any_of {
75   template <class _Policy, class _ForwardIterator, class _Predicate>
76   _LIBCPP_HIDE_FROM_ABI optional<bool>
77   operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) const noexcept {
78     if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
79                   __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
80       return __pstl::__parallel_or<_Backend>(
81           __first, __last, [&__policy, &__pred](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
82             using _AnyOfUnseq = __pstl::__any_of<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
83             auto __res = _AnyOfUnseq()(std::__remove_parallel_policy(__policy), __brick_first, __brick_last, __pred);
84             _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!");
85             return *std::move(__res);
86           });
87     } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
88                          __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
89       return __pstl::__simd_or(__first, __last - __first, __pred);
90     } else {
91       return std::any_of(__first, __last, __pred);
92     }
93   }
94 };
95 
96 } // namespace __pstl
97 _LIBCPP_END_NAMESPACE_STD
98 
99 #endif // _LIBCPP_STD_VER >= 17
100 
101 _LIBCPP_POP_MACROS
102 
103 #endif // _LIBCPP___PSTL_CPU_ALGOS_ANY_OF_H
104