File indexing completed on 2026-05-03 08:13:58
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___PSTL_CPU_ALGOS_ANY_OF_H
0010 #define _LIBCPP___PSTL_CPU_ALGOS_ANY_OF_H
0011
0012 #include <__algorithm/any_of.h>
0013 #include <__assert>
0014 #include <__atomic/atomic.h>
0015 #include <__atomic/memory_order.h>
0016 #include <__config>
0017 #include <__iterator/concepts.h>
0018 #include <__pstl/backend_fwd.h>
0019 #include <__pstl/cpu_algos/cpu_traits.h>
0020 #include <__type_traits/is_execution_policy.h>
0021 #include <__utility/move.h>
0022 #include <__utility/pair.h>
0023 #include <cstdint>
0024 #include <optional>
0025
0026 _LIBCPP_PUSH_MACROS
0027 #include <__undef_macros>
0028
0029 #if _LIBCPP_STD_VER >= 17
0030
0031 _LIBCPP_BEGIN_NAMESPACE_STD
0032 namespace __pstl {
0033
0034 template <class _Backend, class _Index, class _Brick>
0035 _LIBCPP_HIDE_FROM_ABI optional<bool> __parallel_or(_Index __first, _Index __last, _Brick __f) {
0036 std::atomic<bool> __found(false);
0037 auto __ret = __cpu_traits<_Backend>::__for_each(__first, __last, [__f, &__found](_Index __i, _Index __j) {
0038 if (!__found.load(std::memory_order_relaxed) && __f(__i, __j)) {
0039 __found.store(true, std::memory_order_relaxed);
0040 __cpu_traits<_Backend>::__cancel_execution();
0041 }
0042 });
0043 if (!__ret)
0044 return nullopt;
0045 return static_cast<bool>(__found);
0046 }
0047
0048
0049 template <class _Index, class _DifferenceType, class _Pred>
0050 _LIBCPP_HIDE_FROM_ABI bool __simd_or(_Index __first, _DifferenceType __n, _Pred __pred) noexcept {
0051 _DifferenceType __block_size = 4 < __n ? 4 : __n;
0052 const _Index __last = __first + __n;
0053 while (__last != __first) {
0054 int32_t __flag = 1;
0055 _PSTL_PRAGMA_SIMD_REDUCTION(& : __flag)
0056 for (_DifferenceType __i = 0; __i < __block_size; ++__i)
0057 if (__pred(*(__first + __i)))
0058 __flag = 0;
0059 if (!__flag)
0060 return true;
0061
0062 __first += __block_size;
0063 if (__last - __first >= __block_size << 1) {
0064
0065 __block_size <<= 1;
0066 } else {
0067 __block_size = __last - __first;
0068 }
0069 }
0070 return false;
0071 }
0072
0073 template <class _Backend, class _RawExecutionPolicy>
0074 struct __cpu_parallel_any_of {
0075 template <class _Policy, class _ForwardIterator, class _Predicate>
0076 _LIBCPP_HIDE_FROM_ABI optional<bool>
0077 operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) const noexcept {
0078 if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
0079 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
0080 return __pstl::__parallel_or<_Backend>(
0081 __first, __last, [&__policy, &__pred](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
0082 using _AnyOfUnseq = __pstl::__any_of<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
0083 auto __res = _AnyOfUnseq()(std::__remove_parallel_policy(__policy), __brick_first, __brick_last, __pred);
0084 _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!");
0085 return *std::move(__res);
0086 });
0087 } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
0088 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
0089 return __pstl::__simd_or(__first, __last - __first, __pred);
0090 } else {
0091 return std::any_of(__first, __last, __pred);
0092 }
0093 }
0094 };
0095
0096 }
0097 _LIBCPP_END_NAMESPACE_STD
0098
0099 #endif
0100
0101 _LIBCPP_POP_MACROS
0102
0103 #endif