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___ALGORITHM_PSTL_BACKENDS_CPU_BACKNEDS_FOR_EACH_H
10 #define _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKNEDS_FOR_EACH_H
11 
12 #include <__algorithm/for_each.h>
13 #include <__algorithm/pstl_backends/cpu_backends/backend.h>
14 #include <__config>
15 #include <__iterator/concepts.h>
16 #include <__type_traits/is_execution_policy.h>
17 #include <__utility/empty.h>
18 #include <optional>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 
28 template <class _Iterator, class _DifferenceType, class _Function>
__simd_walk(_Iterator __first,_DifferenceType __n,_Function __f)29 _LIBCPP_HIDE_FROM_ABI _Iterator __simd_walk(_Iterator __first, _DifferenceType __n, _Function __f) noexcept {
30   _PSTL_PRAGMA_SIMD
31   for (_DifferenceType __i = 0; __i < __n; ++__i)
32     __f(__first[__i]);
33 
34   return __first + __n;
35 }
36 
37 template <class _ExecutionPolicy, class _ForwardIterator, class _Functor>
38 _LIBCPP_HIDE_FROM_ABI optional<__empty>
__pstl_for_each(__cpu_backend_tag,_ForwardIterator __first,_ForwardIterator __last,_Functor __func)39 __pstl_for_each(__cpu_backend_tag, _ForwardIterator __first, _ForwardIterator __last, _Functor __func) {
40   if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> &&
41                 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
42     return std::__par_backend::__parallel_for(
43         __first, __last, [__func](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
44           [[maybe_unused]] auto __res = std::__pstl_for_each<__remove_parallel_policy_t<_ExecutionPolicy>>(
45               __cpu_backend_tag{}, __brick_first, __brick_last, __func);
46           _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!");
47         });
48   } else if constexpr (__is_unsequenced_execution_policy_v<_ExecutionPolicy> &&
49                        __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
50     std::__simd_walk(__first, __last - __first, __func);
51     return __empty{};
52   } else {
53     std::for_each(__first, __last, __func);
54     return __empty{};
55   }
56 }
57 
58 _LIBCPP_END_NAMESPACE_STD
59 
60 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
61 
62 #endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKNEDS_FOR_EACH_H
63