summaryrefslogtreecommitdiff
path: root/pstl/include/pstl/internal/memory_impl.h
blob: 1eb2fe4e267bc1dca3501c33c39b8714c2a43894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _PSTL_MEMORY_IMPL_H
#define _PSTL_MEMORY_IMPL_H

#include <iterator>

#include "pstl_config.h"
#include "unseq_backend_simd.h"

_PSTL_HIDE_FROM_ABI_PUSH

namespace __pstl
{
namespace __internal
{

//------------------------------------------------------------------------
// uninitialized_move
//------------------------------------------------------------------------

template <typename _ForwardIterator, typename _OutputIterator>
_OutputIterator
__brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
                           /*vector=*/std::false_type) noexcept
{
    using _ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
    for (; __first != __last; ++__first, ++__result)
    {
        ::new (std::addressof(*__result)) _ValueType(std::move(*__first));
    }
    return __result;
}

template <typename _ForwardIterator, typename _OutputIterator>
_OutputIterator
__brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
                           /*vector=*/std::true_type) noexcept
{
    using __ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
    using _ReferenceType1 = typename std::iterator_traits<_ForwardIterator>::reference;
    using _ReferenceType2 = typename std::iterator_traits<_OutputIterator>::reference;

    return __unseq_backend::__simd_walk_2(
        __first, __last - __first, __result,
        [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType(std::move(__x)); });
}

template <typename _Iterator>
void
__brick_destroy(_Iterator __first, _Iterator __last, /*vector*/ std::false_type) noexcept
{
    using _ValueType = typename std::iterator_traits<_Iterator>::value_type;

    for (; __first != __last; ++__first)
        __first->~_ValueType();
}

template <typename _Iterator>
void
__brick_destroy(_Iterator __first, _Iterator __last, /*vector*/ std::true_type) noexcept
{
    using _ValueType = typename std::iterator_traits<_Iterator>::value_type;
    using _ReferenceType = typename std::iterator_traits<_Iterator>::reference;

    __unseq_backend::__simd_walk_1(__first, __last - __first, [](_ReferenceType __x) { __x.~_ValueType(); });
}

//------------------------------------------------------------------------
// uninitialized copy
//------------------------------------------------------------------------

template <typename _ForwardIterator, typename _OutputIterator>
_OutputIterator
__brick_uninitialized_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
                           /*vector=*/std::false_type) noexcept
{
    using _ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
    for (; __first != __last; ++__first, ++__result)
    {
        ::new (std::addressof(*__result)) _ValueType(*__first);
    }
    return __result;
}

template <typename _ForwardIterator, typename _OutputIterator>
_OutputIterator
__brick_uninitialized_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
                           /*vector=*/std::true_type) noexcept
{
    using __ValueType = typename std::iterator_traits<_OutputIterator>::value_type;
    using _ReferenceType1 = typename std::iterator_traits<_ForwardIterator>::reference;
    using _ReferenceType2 = typename std::iterator_traits<_OutputIterator>::reference;

    return __unseq_backend::__simd_walk_2(
        __first, __last - __first, __result,
        [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType(__x); });
}

} // namespace __internal
} // namespace __pstl

_PSTL_HIDE_FROM_ABI_POP

#endif /* _PSTL_MEMORY_IMPL_H */