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