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/iterator_traits.h>
16 #include <__type_traits/is_execution_policy.h>
17 #include <__utility/terminate_on_exception.h>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 template <class _Index, class _DifferenceType, class _Tp>
28 _LIBCPP_HIDE_FROM_ABI _Index __simd_fill_n(_Index __first, _DifferenceType __n, const _Tp& __value) noexcept {
29   _PSTL_USE_NONTEMPORAL_STORES_IF_ALLOWED
30   _PSTL_PRAGMA_SIMD
31   for (_DifferenceType __i = 0; __i < __n; ++__i)
32     __first[__i] = __value;
33   return __first + __n;
34 }
35 
36 template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
37 _LIBCPP_HIDE_FROM_ABI void
38 __pstl_fill(__cpu_backend_tag, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
39   if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> &&
40                 __has_random_access_iterator_category<_ForwardIterator>::value) {
41     std::__terminate_on_exception([&] {
42       __par_backend::__parallel_for(
43           __first, __last, [&__value](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
44             std::__pstl_fill<__remove_parallel_policy_t<_ExecutionPolicy>>(
45                 __cpu_backend_tag{}, __brick_first, __brick_last, __value);
46           });
47     });
48   } else if constexpr (__is_unsequenced_execution_policy_v<_ExecutionPolicy> &&
49                        __has_random_access_iterator_category<_ForwardIterator>::value) {
50     std::__simd_fill_n(__first, __last - __first, __value);
51   } else {
52     std::fill(__first, __last, __value);
53   }
54 }
55 
56 _LIBCPP_END_NAMESPACE_STD
57 
58 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
59 
60 #endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKENDS_FILL_H
61