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_ANY_OF_H
10#define _LIBCPP___PSTL_BACKENDS_STATPART_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 <__execution/is_execution_policy.h>
18#include <__iterator/concepts.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/move.h>
24#include <__utility/pair.h>
25#include <cstdint>
26
27#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28# pragma GCC system_header
29#endif
30
31_LIBCPP_PUSH_MACROS
32#include <__undef_macros>
33
34#if _LIBCPP_STD_VER >= 17
35
36_LIBCPP_BEGIN_NAMESPACE_STD
37_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
38namespace __pstl {
39namespace __statpart {
40
41template <class _Index, class _Brick>
42_LIBCPP_HIDE_FROM_ABI optional<bool> __parallel_or(_Index __first, _Index __last, _Brick __f) {
43 std::atomic<bool> __found(false);
44 auto __ret = __statpart::__for_each(__first, __last, [__f, &__found](_Index __i, _Index __j) {
45 if (!__found.load(m: std::memory_order_relaxed) && __f(__i, __j)) {
46 __found.store(d: true, m: std::memory_order_relaxed);
47 }
48 });
49 if (!__ret)
50 return nullopt;
51 return static_cast<bool>(__found);
52}
53
54// TODO: check whether __simd_first() can be used here
55template <class _Index, class _DifferenceType, class _Pred>
56_LIBCPP_HIDE_FROM_ABI bool __simd_or(_Index __first, _DifferenceType __n, _Pred __pred) noexcept {
57 _DifferenceType __block_size = 4 < __n ? 4 : __n;
58 const _Index __last = __first + __n;
59 while (__last != __first) {
60 int32_t __flag = 1;
61 _PSTL_PRAGMA_SIMD_REDUCTION(& : __flag)
62 for (_DifferenceType __i = 0; __i < __block_size; ++__i)
63 if (__pred(*(__first + __i)))
64 __flag = 0;
65 if (!__flag)
66 return true;
67
68 __first += __block_size;
69 if (__last - __first >= __block_size << 1) {
70 // Double the block _Size. Any unnecessary iterations can be amortized against work done so far.
71 __block_size <<= 1;
72 } else {
73 __block_size = __last - __first;
74 }
75 }
76 return false;
77}
78
79} // namespace __statpart
80
81template <class _ExecutionPolicy>
82struct __any_of<__statpart_backend_tag, _ExecutionPolicy> {
83 template <class _Policy, class _ForwardIterator, class _Predicate>
84 _LIBCPP_HIDE_FROM_ABI optional<bool>
85 operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) const noexcept {
86 if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> &&
87 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
88 return __statpart::__parallel_or(
89 __first, __last, [&__policy, &__pred](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
90 using _AnyOfUnseq = __pstl::__any_of<__statpart_backend_tag, __remove_parallel_policy_t<_ExecutionPolicy>>;
91 auto __res = _AnyOfUnseq()(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 } else if constexpr (__is_unsequenced_execution_policy_v<_ExecutionPolicy> &&
96 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
97 return __statpart::__simd_or(__first, __last - __first, __pred);
98 } else {
99 return std::any_of(__first, __last, __pred);
100 }
101 }
102};
103
104} // namespace __pstl
105_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
106_LIBCPP_END_NAMESPACE_STD
107
108#endif // _LIBCPP_STD_VER >= 17
109
110_LIBCPP_POP_MACROS
111
112#endif // _LIBCPP___PSTL_BACKENDS_STATPART_ANY_OF_H
113