157 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___PSTL_CPU_ALGOS_TRANSFORM_H10#define _LIBCPP___PSTL_CPU_ALGOS_TRANSFORM_H11 12#include <__algorithm/transform.h>13#include <__assert>14#include <__config>15#include <__iterator/concepts.h>16#include <__iterator/iterator_traits.h>17#include <__pstl/backend_fwd.h>18#include <__pstl/cpu_algos/cpu_traits.h>19#include <__type_traits/is_execution_policy.h>20#include <__utility/move.h>21#include <optional>22 23#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)24# pragma GCC system_header25#endif26 27_LIBCPP_PUSH_MACROS28#include <__undef_macros>29 30#if _LIBCPP_STD_VER >= 1731 32_LIBCPP_BEGIN_NAMESPACE_STD33namespace __pstl {34 35template <class _Iterator1, class _DifferenceType, class _Iterator2, class _Function>36_LIBCPP_HIDE_FROM_ABI _Iterator237__simd_transform(_Iterator1 __first1, _DifferenceType __n, _Iterator2 __first2, _Function __f) noexcept {38 _PSTL_PRAGMA_SIMD39 for (_DifferenceType __i = 0; __i < __n; ++__i)40 __f(__first1[__i], __first2[__i]);41 return __first2 + __n;42}43 44template <class _Iterator1, class _DifferenceType, class _Iterator2, class _Iterator3, class _Function>45_LIBCPP_HIDE_FROM_ABI _Iterator3 __simd_transform(46 _Iterator1 __first1, _DifferenceType __n, _Iterator2 __first2, _Iterator3 __first3, _Function __f) noexcept {47 _PSTL_PRAGMA_SIMD48 for (_DifferenceType __i = 0; __i < __n; ++__i)49 __f(__first1[__i], __first2[__i], __first3[__i]);50 return __first3 + __n;51}52 53template <class _Backend, class _RawExecutionPolicy>54struct __cpu_parallel_transform {55 template <class _Policy, class _ForwardIterator, class _ForwardOutIterator, class _UnaryOperation>56 _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>57 operator()(_Policy&& __policy,58 _ForwardIterator __first,59 _ForwardIterator __last,60 _ForwardOutIterator __result,61 _UnaryOperation __op) const noexcept {62 if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&63 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value &&64 __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {65 __cpu_traits<_Backend>::__for_each(66 __first,67 __last,68 [&__policy, __op, __first, __result](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {69 using _TransformUnseq = __pstl::__transform<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;70 auto __res = _TransformUnseq()(71 std::__remove_parallel_policy(__policy),72 __brick_first,73 __brick_last,74 __result + (__brick_first - __first),75 __op);76 _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!");77 return *std::move(__res);78 });79 return __result + (__last - __first);80 } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&81 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value &&82 __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {83 return __pstl::__simd_transform(84 __first,85 __last - __first,86 __result,87 [&](__iterator_reference<_ForwardIterator> __in_value,88 __iterator_reference<_ForwardOutIterator> __out_value) { __out_value = __op(__in_value); });89 } else {90 return std::transform(__first, __last, __result, __op);91 }92 }93};94 95template <class _Backend, class _RawExecutionPolicy>96struct __cpu_parallel_transform_binary {97 template <class _Policy,98 class _ForwardIterator1,99 class _ForwardIterator2,100 class _ForwardOutIterator,101 class _BinaryOperation>102 _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>103 operator()(_Policy&& __policy,104 _ForwardIterator1 __first1,105 _ForwardIterator1 __last1,106 _ForwardIterator2 __first2,107 _ForwardOutIterator __result,108 _BinaryOperation __op) const noexcept {109 if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&110 __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&111 __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value &&112 __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {113 auto __res = __cpu_traits<_Backend>::__for_each(114 __first1,115 __last1,116 [&__policy, __op, __first1, __first2, __result](117 _ForwardIterator1 __brick_first, _ForwardIterator1 __brick_last) {118 using _TransformBinaryUnseq =119 __pstl::__transform_binary<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;120 return _TransformBinaryUnseq()(121 std::__remove_parallel_policy(__policy),122 __brick_first,123 __brick_last,124 __first2 + (__brick_first - __first1),125 __result + (__brick_first - __first1),126 __op);127 });128 if (!__res)129 return nullopt;130 return __result + (__last1 - __first1);131 } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&132 __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&133 __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value &&134 __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {135 return __pstl::__simd_transform(136 __first1,137 __last1 - __first1,138 __first2,139 __result,140 [&](__iterator_reference<_ForwardIterator1> __in1,141 __iterator_reference<_ForwardIterator2> __in2,142 __iterator_reference<_ForwardOutIterator> __out_value) { __out_value = __op(__in1, __in2); });143 } else {144 return std::transform(__first1, __last1, __first2, __result, __op);145 }146 }147};148 149} // namespace __pstl150_LIBCPP_END_NAMESPACE_STD151 152#endif // _LIBCPP_STD_VER >= 17153 154_LIBCPP_POP_MACROS155 156#endif // _LIBCPP___PSTL_CPU_ALGOS_TRANSFORM_H157