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