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_BACKENDS_FILL_H
10 #define _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_FILL_H
11 
12 #include <__algorithm/fill.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 _Index, class _DifferenceType, class _Tp>
29 _LIBCPP_HIDE_FROM_ABI _Index __simd_fill_n(_Index __first, _DifferenceType __n, const _Tp& __value) noexcept {
30   _PSTL_USE_NONTEMPORAL_STORES_IF_ALLOWED
31   _PSTL_PRAGMA_SIMD
32   for (_DifferenceType __i = 0; __i < __n; ++__i)
33     __first[__i] = __value;
34   return __first + __n;
35 }
36 
37 template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
38 _LIBCPP_HIDE_FROM_ABI optional<__empty>
39 __pstl_fill(__cpu_backend_tag, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
40   if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> &&
41                 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
42     return __par_backend::__parallel_for(
43         __first, __last, [&__value](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
44           [[maybe_unused]] auto __res = std::__pstl_fill<__remove_parallel_policy_t<_ExecutionPolicy>>(
45               __cpu_backend_tag{}, __brick_first, __brick_last, __value);
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_fill_n(__first, __last - __first, __value);
51     return __empty{};
52   } else {
53     std::fill(__first, __last, __value);
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_BACKENDS_FILL_H
63