| 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_TRANSFORM_H |
| 10 | #define _LIBCPP___PSTL_BACKENDS_STATPART_TRANSFORM_H |
| 11 | |
| 12 | #include <__algorithm/transform.h> |
| 13 | #include <__config> |
| 14 | #include <__execution/is_execution_policy.h> |
| 15 | #include <__iterator/concepts.h> |
| 16 | #include <__iterator/iterator_traits.h> |
| 17 | #include <__optional/nullopt_t.h> |
| 18 | #include <__optional/optional.h> |
| 19 | #include <__pstl/backend_fwd.h> |
| 20 | #include <__pstl/backends/statpart/basis.h> |
| 21 | #include <__utility/empty.h> |
| 22 | #include <__utility/move.h> |
| 23 | |
| 24 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 25 | # pragma GCC system_header |
| 26 | #endif |
| 27 | |
| 28 | _LIBCPP_PUSH_MACROS |
| 29 | #include <__undef_macros> |
| 30 | |
| 31 | #if _LIBCPP_STD_VER >= 17 |
| 32 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 33 | _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS |
| 34 | namespace __pstl { |
| 35 | namespace __statpart { |
| 36 | |
| 37 | template <class _Iterator1, class _DifferenceType, class _Iterator2, class _Function> |
| 38 | _LIBCPP_HIDE_FROM_ABI _Iterator2 |
| 39 | __simd_transform(_Iterator1 __first1, _DifferenceType __n, _Iterator2 __first2, _Function __f) noexcept { |
| 40 | _PSTL_PRAGMA_SIMD |
| 41 | for (_DifferenceType __i = 0; __i < __n; ++__i) |
| 42 | __f(__first1[__i], __first2[__i]); |
| 43 | return __first2 + __n; |
| 44 | } |
| 45 | |
| 46 | template <class _Iterator1, class _DifferenceType, class _Iterator2, class _Iterator3, class _Function> |
| 47 | _LIBCPP_HIDE_FROM_ABI _Iterator3 __simd_transform( |
| 48 | _Iterator1 __first1, _DifferenceType __n, _Iterator2 __first2, _Iterator3 __first3, _Function __f) noexcept { |
| 49 | _PSTL_PRAGMA_SIMD |
| 50 | for (_DifferenceType __i = 0; __i < __n; ++__i) |
| 51 | __f(__first1[__i], __first2[__i], __first3[__i]); |
| 52 | return __first3 + __n; |
| 53 | } |
| 54 | |
| 55 | } // namespace __statpart |
| 56 | |
| 57 | template <class _ExecutionPolicy> |
| 58 | struct __transform<__statpart_backend_tag, _ExecutionPolicy> { |
| 59 | template <class _Policy, class _ForwardIterator, class _ForwardOutIterator, class _UnaryOperation> |
| 60 | _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> |
| 61 | operator()(_Policy&& __policy, |
| 62 | _ForwardIterator __first, |
| 63 | _ForwardIterator __last, |
| 64 | _ForwardOutIterator __result, |
| 65 | _UnaryOperation __op) const noexcept { |
| 66 | if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> && |
| 67 | __has_random_access_iterator_category_or_concept<_ForwardIterator>::value && |
| 68 | __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) { |
| 69 | auto res = __statpart::__for_each( |
| 70 | __first, |
| 71 | __last, |
| 72 | [&__policy, __op, __first, __result](_ForwardIterator __brick_first, _ForwardIterator __brick_last) { |
| 73 | using _TransformUnseq = |
| 74 | __pstl::__transform<__statpart_backend_tag, __remove_parallel_policy_t<_ExecutionPolicy>>; |
| 75 | _TransformUnseq()( |
| 76 | std::__remove_parallel_policy(__policy), |
| 77 | __brick_first, |
| 78 | __brick_last, |
| 79 | __result + (__brick_first - __first), |
| 80 | __op); |
| 81 | }); |
| 82 | if (!res) |
| 83 | return nullopt; // Failed to perform "__for_each", propagate the error upwards |
| 84 | return __result + (__last - __first); |
| 85 | } else if constexpr (__is_unsequenced_execution_policy_v<_ExecutionPolicy> && |
| 86 | __has_random_access_iterator_category_or_concept<_ForwardIterator>::value && |
| 87 | __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) { |
| 88 | return __statpart::__simd_transform( |
| 89 | __first, |
| 90 | __last - __first, |
| 91 | __result, |
| 92 | [&](__iterator_reference<_ForwardIterator> __in_value, |
| 93 | __iterator_reference<_ForwardOutIterator> __out_value) { __out_value = __op(__in_value); }); |
| 94 | } else { |
| 95 | return std::transform(__first, __last, __result, __op); |
| 96 | } |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | template <class _ExecutionPolicy> |
| 101 | struct __transform_binary<__statpart_backend_tag, _ExecutionPolicy> { |
| 102 | template <class _Policy, |
| 103 | class _ForwardIterator1, |
| 104 | class _ForwardIterator2, |
| 105 | class _ForwardOutIterator, |
| 106 | class _BinaryOperation> |
| 107 | _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> |
| 108 | operator()(_Policy&& __policy, |
| 109 | _ForwardIterator1 __first1, |
| 110 | _ForwardIterator1 __last1, |
| 111 | _ForwardIterator2 __first2, |
| 112 | _ForwardOutIterator __result, |
| 113 | _BinaryOperation __op) const noexcept { |
| 114 | if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> && |
| 115 | __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value && |
| 116 | __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value && |
| 117 | __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) { |
| 118 | auto __res = __statpart::__for_each( |
| 119 | __first1, |
| 120 | __last1, |
| 121 | [&__policy, __op, __first1, __first2, __result]( |
| 122 | _ForwardIterator1 __brick_first, _ForwardIterator1 __brick_last) { |
| 123 | using _TransformBinaryUnseq = |
| 124 | __pstl::__transform_binary<__statpart_backend_tag, __remove_parallel_policy_t<_ExecutionPolicy>>; |
| 125 | _TransformBinaryUnseq()( |
| 126 | std::__remove_parallel_policy(__policy), |
| 127 | __brick_first, |
| 128 | __brick_last, |
| 129 | __first2 + (__brick_first - __first1), |
| 130 | __result + (__brick_first - __first1), |
| 131 | __op); |
| 132 | }); |
| 133 | if (!__res) |
| 134 | return nullopt; |
| 135 | return __result + (__last1 - __first1); |
| 136 | } else if constexpr (__is_unsequenced_execution_policy_v<_ExecutionPolicy> && |
| 137 | __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value && |
| 138 | __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value && |
| 139 | __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) { |
| 140 | return __statpart::__simd_transform( |
| 141 | __first1, |
| 142 | __last1 - __first1, |
| 143 | __first2, |
| 144 | __result, |
| 145 | [&](__iterator_reference<_ForwardIterator1> __in1, |
| 146 | __iterator_reference<_ForwardIterator2> __in2, |
| 147 | __iterator_reference<_ForwardOutIterator> __out_value) { __out_value = __op(__in1, __in2); }); |
| 148 | } else { |
| 149 | return std::transform(__first1, __last1, __first2, __result, __op); |
| 150 | } |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | } // namespace __pstl |
| 155 | _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS |
| 156 | _LIBCPP_END_NAMESPACE_STD |
| 157 | |
| 158 | #endif // _LIBCPP_STD_VER >= 17 |
| 159 | |
| 160 | _LIBCPP_POP_MACROS |
| 161 | |
| 162 | #endif // _LIBCPP___PSTL_BACKENDS_STATPART_TRANSFORM_H |
| 163 | |