Vc  1.2.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 Abi, typename Alloc,
47  template <class, class> class Container>
48  struct make_container_helper<Container<Vector<T_, Abi>, Alloc>,
49  typename Vector<T_, Abi>::EntryType> {
50  typedef Vector<T_, Abi> V;
51  typedef typename V::EntryType T;
52  typedef Container<V, Alloc> C;
53  static inline C help(std::initializer_list<T> list) {
54  const std::size_t size = (list.size() + (V::Size - 1)) / V::Size;
55  C v(size);
56  auto containerIt = v.begin();
57  auto init = std::begin(list);
58  const auto initEnd = std::end(list);
59  for (std::size_t i = 0; i < size - 1; ++i) {
60  *containerIt++ = V(init, Vc::Unaligned);
61  init += V::Size;
62  }
63  Vc_ASSERT(all_of(*containerIt == V::Zero()));
64  int j = 0;
65  while (init != initEnd) {
66  (*containerIt)[j++] = *init++;
67  }
68  return v;
69  }
70  };
71 
72  template <typename T_, typename Abi, std::size_t N,
73  template <class, std::size_t> class Container>
74  struct make_container_helper<Container<Vector<T_, Abi>, N>,
75  typename Vector<T_, Abi>::EntryType> {
76  typedef Vector<T_, Abi> V;
77  typedef typename V::EntryType T;
78  static constexpr std::size_t size = (N + (V::Size - 1)) / V::Size;
79  typedef Container<
80  V,
81 #if defined Vc_CLANG && Vc_CLANG < 0x30700
82  // clang before 3.7.0 has a bug when returning std::array<__m256x, 1>. So
83  // increase it to std::array<__m256x, 2> and fill it with zeros. Better
84  // than returning garbage.
85  (size == 1 && std::is_same<Abi, VectorAbi::Avx>::value) ? 2 :
86 #endif
87  size> C;
88  static inline C help(std::initializer_list<T> list) {
89  Vc_ASSERT(N == list.size())
90  Vc_ASSERT(size == (list.size() + (V::Size - 1)) / V::Size)
91  C v;
92  auto containerIt = v.begin();
93  auto init = std::begin(list);
94  const auto initEnd = std::end(list);
95  for (std::size_t i = 0; i < size - 1; ++i) {
96  *containerIt++ = V(init, Vc::Unaligned);
97  init += V::Size;
98  }
99  Vc_ASSERT(all_of(*containerIt == V::Zero()));
100  int j = 0;
101  while (init != initEnd) {
102  (*containerIt)[j++] = *init++;
103  }
104  return v;
105  }
106  };
107  } // anonymous namespace
108 
138  template<typename Container, typename T>
139  constexpr auto makeContainer(std::initializer_list<T> list) -> decltype(make_container_helper<Container, T>::help(list))
140  {
141  return make_container_helper<Container, T>::help(list);
142  }
143 
144  template<typename Container, typename T>
145  constexpr auto make_container(std::initializer_list<T> list) -> decltype(makeContainer<Container, T>(list))
146  {
147  return makeContainer<Container, T>(list);
148  }
149 
150 } // namespace Vc
151 
152 #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.
constexpr bool all_of(const Mask &m)
Returns whether all entries in the mask m are true.
Definition: algorithms.h:46
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.