Vc  1.1.0
SIMD Vector Classes for C++
makeContainer.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2013-2015 Matthias Kretz <kretz@kde.org>
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the names of contributing organizations nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 }}}*/
28 
29 #ifndef VC_COMMON_MAKECONTAINER_H_
30 #define VC_COMMON_MAKECONTAINER_H_
31 
32 #include <Vc/vector.h>
33 #include <initializer_list>
34 #include "macros.h"
35 
36 namespace Vc_VERSIONED_NAMESPACE
37 {
38 
39  namespace
40  {
41  template<typename Container, typename T> struct make_container_helper
42  {
43  static constexpr Container help(std::initializer_list<T> list) { return { list }; }
44  };
45 
46  template<typename _T, typename Alloc, template<class, class> class Container>
47  struct make_container_helper<Container< ::Vc::Vector<_T>, Alloc>, typename ::Vc::Vector<_T>::EntryType>
48  {
49  typedef ::Vc::Vector<_T> V;
50  typedef typename V::EntryType T;
51  typedef Container<V, Alloc> C;
52  static inline C help(std::initializer_list<T> list) {
53  const std::size_t size = (list.size() + (V::Size - 1)) / V::Size;
54  C v(size);
55  auto containerIt = v.begin();
56  auto init = std::begin(list);
57  const auto initEnd = std::end(list);
58  for (std::size_t i = 0; i < size - 1; ++i) {
59  *containerIt++ = V(init, Vc::Unaligned);
60  init += V::Size;
61  }
62  *containerIt = V::Zero();
63  int j = 0;
64  while (init != initEnd) {
65  (*containerIt)[j++] = *init++;
66  }
67  return std::move(v);
68  }
69  };
70 
71  template<typename _T, std::size_t N, template<class, std::size_t> class Container>
72  struct make_container_helper<Container< ::Vc::Vector<_T>, N>, typename ::Vc::Vector<_T>::EntryType>
73  {
74  typedef ::Vc::Vector<_T> V;
75  typedef typename V::EntryType T;
76  static constexpr std::size_t size = (N + (V::Size - 1)) / V::Size;
77  typedef Container<V, size> C;
78  static inline C help(std::initializer_list<T> list) {
79  Vc_ASSERT(N == list.size())
80  Vc_ASSERT(size == (list.size() + (V::Size - 1)) / V::Size)
81  C v;
82  auto containerIt = v.begin();
83  auto init = std::begin(list);
84  const auto initEnd = std::end(list);
85  for (std::size_t i = 0; i < size - 1; ++i) {
86  *containerIt++ = V(init, Vc::Unaligned);
87  init += V::Size;
88  }
89  *containerIt = V::Zero();
90  int j = 0;
91  while (init != initEnd) {
92  (*containerIt)[j++] = *init++;
93  }
94  return std::move(v);
95  }
96  };
97  } // anonymous namespace
98 
121  template<typename Container, typename T>
122  constexpr auto makeContainer(std::initializer_list<T> list) -> decltype(make_container_helper<Container, T>::help(list))
123  {
124  return make_container_helper<Container, T>::help(list);
125  }
126 
127  template<typename Container, typename T>
128  constexpr auto make_container(std::initializer_list<T> list) -> decltype(makeContainer<Container, T>(list))
129  {
130  return makeContainer<Container, T>(list);
131  }
132 
133 } // namespace Vc
134 
135 #endif // VC_COMMON_MAKECONTAINER_H_
constexpr auto makeContainer(std::initializer_list< T > list) -> decltype(make_container_helper< Container, T >::help(list))
Construct a container of Vc vectors from a std::initializer_list of scalar entries.
The main vector class for expressing data parallelism.
Definition: types.h:45
constexpr VectorSpecialInitializerZero Zero
The special object Vc::Zero can be used to construct Vector and Mask objects initialized to zero/fals...
Definition: types.h:85
constexpr UnalignedTag Unaligned
Use this object for a flags parameter to request unaligned loads and stores.