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_FILL_H |
10 | #define _LIBCPP___PSTL_CPU_ALGOS_FILL_H |
11 | |
12 | #include <__algorithm/fill.h> |
13 | #include <__assert> |
14 | #include <__config> |
15 | #include <__iterator/concepts.h> |
16 | #include <__pstl/backend_fwd.h> |
17 | #include <__pstl/cpu_algos/cpu_traits.h> |
18 | #include <__type_traits/is_execution_policy.h> |
19 | #include <__utility/empty.h> |
20 | #include <optional> |
21 | |
22 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
23 | # pragma GCC system_header |
24 | #endif |
25 | |
26 | _LIBCPP_BEGIN_NAMESPACE_STD |
27 | namespace __pstl { |
28 | |
29 | template <class _Index, class _DifferenceType, class _Tp> |
30 | _LIBCPP_HIDE_FROM_ABI _Index __simd_fill_n(_Index __first, _DifferenceType __n, const _Tp& __value) noexcept { |
31 | _PSTL_USE_NONTEMPORAL_STORES_IF_ALLOWED |
32 | _PSTL_PRAGMA_SIMD |
33 | for (_DifferenceType __i = 0; __i < __n; ++__i) |
34 | __first[__i] = __value; |
35 | return __first + __n; |
36 | } |
37 | |
38 | template <class _Backend, class _RawExecutionPolicy> |
39 | struct __cpu_parallel_fill { |
40 | template <class _Policy, class _ForwardIterator, class _Tp> |
41 | _LIBCPP_HIDE_FROM_ABI optional<__empty> |
42 | operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) const noexcept { |
43 | if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> && |
44 | __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) { |
45 | return __cpu_traits<_Backend>::__for_each( |
46 | __first, __last, [&__policy, &__value](_ForwardIterator __brick_first, _ForwardIterator __brick_last) { |
47 | using _FillUnseq = __pstl::__fill<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>; |
48 | [[maybe_unused]] auto __res = |
49 | _FillUnseq()(std::__remove_parallel_policy(__policy), __brick_first, __brick_last, __value); |
50 | _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!" ); |
51 | }); |
52 | } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> && |
53 | __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) { |
54 | __pstl::__simd_fill_n(__first, __last - __first, __value); |
55 | return __empty{}; |
56 | } else { |
57 | std::fill(__first, __last, __value); |
58 | return __empty{}; |
59 | } |
60 | } |
61 | }; |
62 | |
63 | } // namespace __pstl |
64 | _LIBCPP_END_NAMESPACE_STD |
65 | |
66 | #endif // _LIBCPP___PSTL_CPU_ALGOS_FILL_H |
67 | |