xref: /llvm-project/pstl/include/pstl/internal/memory_impl.h (revision 5856f202057cd375cd9599fe44cfa7e78239c625)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
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 "pstl_config.h"
16 #include "unseq_backend_simd.h"
17 
18 _PSTL_HIDE_FROM_ABI_PUSH
19 
20 namespace __pstl
21 {
22 namespace __internal
23 {
24 
25 //------------------------------------------------------------------------
26 // uninitialized_move
27 //------------------------------------------------------------------------
28 
29 template <typename _ForwardIterator, typename _OutputIterator>
30 _OutputIterator
__brick_uninitialized_move(_ForwardIterator __first,_ForwardIterator __last,_OutputIterator __result,std::false_type)31 __brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
32                            /*vector=*/std::false_type) noexcept
33 {
34     using _ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
35     for (; __first != __last; ++__first, ++__result)
36     {
37         ::new (std::addressof(*__result)) _ValueType(std::move(*__first));
38     }
39     return __result;
40 }
41 
42 template <typename _RandomAccessIterator, typename _OutputIterator>
43 _OutputIterator
__brick_uninitialized_move(_RandomAccessIterator __first,_RandomAccessIterator __last,_OutputIterator __result,std::true_type)44 __brick_uninitialized_move(_RandomAccessIterator __first, _RandomAccessIterator __last, _OutputIterator __result,
45                            /*vector=*/std::true_type) noexcept
46 {
47     using __ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
48     using _ReferenceType1 = typename std::iterator_traits<_RandomAccessIterator>::reference;
49     using _ReferenceType2 = typename std::iterator_traits<_OutputIterator>::reference;
50 
51     return __unseq_backend::__simd_walk_2(
52         __first, __last - __first, __result,
53         [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType(std::move(__x)); });
54 }
55 
56 template <typename _Iterator>
57 void
__brick_destroy(_Iterator __first,_Iterator __last,std::false_type)58 __brick_destroy(_Iterator __first, _Iterator __last, /*vector*/ std::false_type) noexcept
59 {
60     using _ValueType = typename std::iterator_traits<_Iterator>::value_type;
61 
62     for (; __first != __last; ++__first)
63         __first->~_ValueType();
64 }
65 
66 template <typename _RandomAccessIterator>
67 void
__brick_destroy(_RandomAccessIterator __first,_RandomAccessIterator __last,std::true_type)68 __brick_destroy(_RandomAccessIterator __first, _RandomAccessIterator __last, /*vector*/ std::true_type) noexcept
69 {
70     using _ValueType = typename std::iterator_traits<_RandomAccessIterator>::value_type;
71     using _ReferenceType = typename std::iterator_traits<_RandomAccessIterator>::reference;
72 
73     __unseq_backend::__simd_walk_1(__first, __last - __first, [](_ReferenceType __x) { __x.~_ValueType(); });
74 }
75 
76 //------------------------------------------------------------------------
77 // uninitialized copy
78 //------------------------------------------------------------------------
79 
80 template <typename _ForwardIterator, typename _OutputIterator>
81 _OutputIterator
__brick_uninitialized_copy(_ForwardIterator __first,_ForwardIterator __last,_OutputIterator __result,std::false_type)82 __brick_uninitialized_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
83                            /*vector=*/std::false_type) noexcept
84 {
85     using _ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
86     for (; __first != __last; ++__first, ++__result)
87     {
88         ::new (std::addressof(*__result)) _ValueType(*__first);
89     }
90     return __result;
91 }
92 
93 template <typename _RandomAccessIterator, typename _OutputIterator>
94 _OutputIterator
__brick_uninitialized_copy(_RandomAccessIterator __first,_RandomAccessIterator __last,_OutputIterator __result,std::true_type)95 __brick_uninitialized_copy(_RandomAccessIterator __first, _RandomAccessIterator __last, _OutputIterator __result,
96                            /*vector=*/std::true_type) noexcept
97 {
98     using __ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
99     using _ReferenceType1 = typename std::iterator_traits<_RandomAccessIterator>::reference;
100     using _ReferenceType2 = typename std::iterator_traits<_OutputIterator>::reference;
101 
102     return __unseq_backend::__simd_walk_2(
103         __first, __last - __first, __result,
104         [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType(__x); });
105 }
106 
107 } // namespace __internal
108 } // namespace __pstl
109 
110 _PSTL_HIDE_FROM_ABI_POP
111 
112 #endif /* _PSTL_MEMORY_IMPL_H */
113