Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:59

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___PSTL_CPU_ALGOS_TRANSFORM_H
0010 #define _LIBCPP___PSTL_CPU_ALGOS_TRANSFORM_H
0011 
0012 #include <__algorithm/transform.h>
0013 #include <__assert>
0014 #include <__config>
0015 #include <__iterator/concepts.h>
0016 #include <__iterator/iterator_traits.h>
0017 #include <__pstl/backend_fwd.h>
0018 #include <__pstl/cpu_algos/cpu_traits.h>
0019 #include <__type_traits/is_execution_policy.h>
0020 #include <__utility/move.h>
0021 #include <optional>
0022 
0023 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0024 #  pragma GCC system_header
0025 #endif
0026 
0027 _LIBCPP_PUSH_MACROS
0028 #include <__undef_macros>
0029 
0030 #if _LIBCPP_STD_VER >= 17
0031 
0032 _LIBCPP_BEGIN_NAMESPACE_STD
0033 namespace __pstl {
0034 
0035 template <class _Iterator1, class _DifferenceType, class _Iterator2, class _Function>
0036 _LIBCPP_HIDE_FROM_ABI _Iterator2
0037 __simd_transform(_Iterator1 __first1, _DifferenceType __n, _Iterator2 __first2, _Function __f) noexcept {
0038   _PSTL_PRAGMA_SIMD
0039   for (_DifferenceType __i = 0; __i < __n; ++__i)
0040     __f(__first1[__i], __first2[__i]);
0041   return __first2 + __n;
0042 }
0043 
0044 template <class _Iterator1, class _DifferenceType, class _Iterator2, class _Iterator3, class _Function>
0045 _LIBCPP_HIDE_FROM_ABI _Iterator3 __simd_transform(
0046     _Iterator1 __first1, _DifferenceType __n, _Iterator2 __first2, _Iterator3 __first3, _Function __f) noexcept {
0047   _PSTL_PRAGMA_SIMD
0048   for (_DifferenceType __i = 0; __i < __n; ++__i)
0049     __f(__first1[__i], __first2[__i], __first3[__i]);
0050   return __first3 + __n;
0051 }
0052 
0053 template <class _Backend, class _RawExecutionPolicy>
0054 struct __cpu_parallel_transform {
0055   template <class _Policy, class _ForwardIterator, class _ForwardOutIterator, class _UnaryOperation>
0056   _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
0057   operator()(_Policy&& __policy,
0058              _ForwardIterator __first,
0059              _ForwardIterator __last,
0060              _ForwardOutIterator __result,
0061              _UnaryOperation __op) const noexcept {
0062     if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
0063                   __has_random_access_iterator_category_or_concept<_ForwardIterator>::value &&
0064                   __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
0065       __cpu_traits<_Backend>::__for_each(
0066           __first,
0067           __last,
0068           [&__policy, __op, __first, __result](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
0069             using _TransformUnseq = __pstl::__transform<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
0070             auto __res            = _TransformUnseq()(
0071                 std::__remove_parallel_policy(__policy),
0072                 __brick_first,
0073                 __brick_last,
0074                 __result + (__brick_first - __first),
0075                 __op);
0076             _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!");
0077             return *std::move(__res);
0078           });
0079       return __result + (__last - __first);
0080     } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
0081                          __has_random_access_iterator_category_or_concept<_ForwardIterator>::value &&
0082                          __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
0083       return __pstl::__simd_transform(
0084           __first,
0085           __last - __first,
0086           __result,
0087           [&](__iter_reference<_ForwardIterator> __in_value, __iter_reference<_ForwardOutIterator> __out_value) {
0088             __out_value = __op(__in_value);
0089           });
0090     } else {
0091       return std::transform(__first, __last, __result, __op);
0092     }
0093   }
0094 };
0095 
0096 template <class _Backend, class _RawExecutionPolicy>
0097 struct __cpu_parallel_transform_binary {
0098   template <class _Policy,
0099             class _ForwardIterator1,
0100             class _ForwardIterator2,
0101             class _ForwardOutIterator,
0102             class _BinaryOperation>
0103   _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
0104   operator()(_Policy&& __policy,
0105              _ForwardIterator1 __first1,
0106              _ForwardIterator1 __last1,
0107              _ForwardIterator2 __first2,
0108              _ForwardOutIterator __result,
0109              _BinaryOperation __op) const noexcept {
0110     if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
0111                   __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&
0112                   __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value &&
0113                   __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
0114       auto __res = __cpu_traits<_Backend>::__for_each(
0115           __first1,
0116           __last1,
0117           [&__policy, __op, __first1, __first2, __result](
0118               _ForwardIterator1 __brick_first, _ForwardIterator1 __brick_last) {
0119             using _TransformBinaryUnseq =
0120                 __pstl::__transform_binary<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
0121             return _TransformBinaryUnseq()(
0122                 std::__remove_parallel_policy(__policy),
0123                 __brick_first,
0124                 __brick_last,
0125                 __first2 + (__brick_first - __first1),
0126                 __result + (__brick_first - __first1),
0127                 __op);
0128           });
0129       if (!__res)
0130         return nullopt;
0131       return __result + (__last1 - __first1);
0132     } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
0133                          __has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&
0134                          __has_random_access_iterator_category_or_concept<_ForwardIterator2>::value &&
0135                          __has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {
0136       return __pstl::__simd_transform(
0137           __first1,
0138           __last1 - __first1,
0139           __first2,
0140           __result,
0141           [&](__iter_reference<_ForwardIterator1> __in1,
0142               __iter_reference<_ForwardIterator2> __in2,
0143               __iter_reference<_ForwardOutIterator> __out_value) { __out_value = __op(__in1, __in2); });
0144     } else {
0145       return std::transform(__first1, __last1, __first2, __result, __op);
0146     }
0147   }
0148 };
0149 
0150 } // namespace __pstl
0151 _LIBCPP_END_NAMESPACE_STD
0152 
0153 #endif // _LIBCPP_STD_VER >= 17
0154 
0155 _LIBCPP_POP_MACROS
0156 
0157 #endif // _LIBCPP___PSTL_CPU_ALGOS_TRANSFORM_H