1  // -*- C++ -*-
       2  //===-- memory_impl.h -----------------------------------------------------===//
       3  //
       4  // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
       5  // See https://llvm.org/LICENSE.txt for license information.
       6  // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
       7  //
       8  //===----------------------------------------------------------------------===//
       9  
      10  #ifndef _PSTL_MEMORY_IMPL_H
      11  #define _PSTL_MEMORY_IMPL_H
      12  
      13  #include <iterator>
      14  
      15  #include "unseq_backend_simd.h"
      16  
      17  namespace __pstl
      18  {
      19  namespace __internal
      20  {
      21  
      22  //------------------------------------------------------------------------
      23  // uninitialized_move
      24  //------------------------------------------------------------------------
      25  
      26  template <typename _ForwardIterator, typename _OutputIterator>
      27  _OutputIterator
      28  __brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
      29                             /*vector=*/std::false_type) noexcept
      30  {
      31      using _ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
      32      for (; __first != __last; ++__first, ++__result)
      33      {
      34          ::new (std::addressof(*__result)) _ValueType(std::move(*__first));
      35      }
      36      return __result;
      37  }
      38  
      39  template <typename _ForwardIterator, typename _OutputIterator>
      40  _OutputIterator
      41  __brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
      42                             /*vector=*/std::true_type) noexcept
      43  {
      44      using __ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
      45      using _ReferenceType1 = typename std::iterator_traits<_ForwardIterator>::reference;
      46      using _ReferenceType2 = typename std::iterator_traits<_OutputIterator>::reference;
      47  
      48      return __unseq_backend::__simd_walk_2(
      49          __first, __last - __first, __result,
      50          [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType(std::move(__x)); });
      51  }
      52  
      53  template <typename _Iterator>
      54  void
      55  __brick_destroy(_Iterator __first, _Iterator __last, /*vector*/ std::false_type) noexcept
      56  {
      57      using _ValueType = typename std::iterator_traits<_Iterator>::value_type;
      58  
      59      for (; __first != __last; ++__first)
      60          __first->~_ValueType();
      61  }
      62  
      63  template <typename _Iterator>
      64  void
      65  __brick_destroy(_Iterator __first, _Iterator __last, /*vector*/ std::true_type) noexcept
      66  {
      67      using _ValueType = typename std::iterator_traits<_Iterator>::value_type;
      68      using _ReferenceType = typename std::iterator_traits<_Iterator>::reference;
      69  
      70      __unseq_backend::__simd_walk_1(__first, __last - __first, [](_ReferenceType __x) { __x.~_ValueType(); });
      71  }
      72  
      73  //------------------------------------------------------------------------
      74  // uninitialized copy
      75  //------------------------------------------------------------------------
      76  
      77  template <typename _ForwardIterator, typename _OutputIterator>
      78  _OutputIterator
      79  __brick_uninitialized_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
      80                             /*vector=*/std::false_type) noexcept
      81  {
      82      using _ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
      83      for (; __first != __last; ++__first, ++__result)
      84      {
      85          ::new (std::addressof(*__result)) _ValueType(*__first);
      86      }
      87      return __result;
      88  }
      89  
      90  template <typename _ForwardIterator, typename _OutputIterator>
      91  _OutputIterator
      92  __brick_uninitialized_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
      93                             /*vector=*/std::true_type) noexcept
      94  {
      95      using __ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
      96      using _ReferenceType1 = typename std::iterator_traits<_ForwardIterator>::reference;
      97      using _ReferenceType2 = typename std::iterator_traits<_OutputIterator>::reference;
      98  
      99      return __unseq_backend::__simd_walk_2(
     100          __first, __last - __first, __result,
     101          [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType(__x); });
     102  }
     103  
     104  } // namespace __internal
     105  } // namespace __pstl
     106  
     107  #endif /* _PSTL_MEMORY_IMPL_H */