memory_impl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef _PSTL_MEMORY_IMPL_H
  10. #define _PSTL_MEMORY_IMPL_H
  11. #include <iterator>
  12. #include "unseq_backend_simd.h"
  13. namespace __pstl
  14. {
  15. namespace __internal
  16. {
  17. //------------------------------------------------------------------------
  18. // uninitialized_move
  19. //------------------------------------------------------------------------
  20. template <typename _ForwardIterator, typename _OutputIterator>
  21. _OutputIterator
  22. __brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
  23. /*vector=*/std::false_type) noexcept
  24. {
  25. using _ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
  26. for (; __first != __last; ++__first, ++__result)
  27. {
  28. ::new (std::addressof(*__result)) _ValueType(std::move(*__first));
  29. }
  30. return __result;
  31. }
  32. template <typename _ForwardIterator, typename _OutputIterator>
  33. _OutputIterator
  34. __brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
  35. /*vector=*/std::true_type) noexcept
  36. {
  37. using __ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
  38. using _ReferenceType1 = typename std::iterator_traits<_ForwardIterator>::reference;
  39. using _ReferenceType2 = typename std::iterator_traits<_OutputIterator>::reference;
  40. return __unseq_backend::__simd_walk_2(
  41. __first, __last - __first, __result,
  42. [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType(std::move(__x)); });
  43. }
  44. template <typename _Iterator>
  45. void
  46. __brick_destroy(_Iterator __first, _Iterator __last, /*vector*/ std::false_type) noexcept
  47. {
  48. using _ValueType = typename std::iterator_traits<_Iterator>::value_type;
  49. for (; __first != __last; ++__first)
  50. __first->~_ValueType();
  51. }
  52. template <typename _Iterator>
  53. void
  54. __brick_destroy(_Iterator __first, _Iterator __last, /*vector*/ std::true_type) noexcept
  55. {
  56. using _ValueType = typename std::iterator_traits<_Iterator>::value_type;
  57. using _ReferenceType = typename std::iterator_traits<_Iterator>::reference;
  58. __unseq_backend::__simd_walk_1(__first, __last - __first, [](_ReferenceType __x) { __x.~_ValueType(); });
  59. }
  60. //------------------------------------------------------------------------
  61. // uninitialized copy
  62. //------------------------------------------------------------------------
  63. template <typename _ForwardIterator, typename _OutputIterator>
  64. _OutputIterator
  65. __brick_uninitialized_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
  66. /*vector=*/std::false_type) noexcept
  67. {
  68. using _ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
  69. for (; __first != __last; ++__first, ++__result)
  70. {
  71. ::new (std::addressof(*__result)) _ValueType(*__first);
  72. }
  73. return __result;
  74. }
  75. template <typename _ForwardIterator, typename _OutputIterator>
  76. _OutputIterator
  77. __brick_uninitialized_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
  78. /*vector=*/std::true_type) noexcept
  79. {
  80. using __ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
  81. using _ReferenceType1 = typename std::iterator_traits<_ForwardIterator>::reference;
  82. using _ReferenceType2 = typename std::iterator_traits<_OutputIterator>::reference;
  83. return __unseq_backend::__simd_walk_2(
  84. __first, __last - __first, __result,
  85. [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType(__x); });
  86. }
  87. } // namespace __internal
  88. } // namespace __pstl
  89. #endif /* _PSTL_MEMORY_IMPL_H */