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