Vc  1.4.1
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 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6  * Redistributions of source code must retain the above copyright
7  notice, this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright
9  notice, this list of conditions and the following disclaimer in the
10  documentation and/or other materials provided with the distribution.
11  * Neither the names of contributing organizations nor the
12  names of its contributors may be used to endorse or promote products
13  derived from this software without specific prior written permission.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 }}}*/
27 
28 #ifndef VC_COMMON_MAKECONTAINER_H_
29 #define VC_COMMON_MAKECONTAINER_H_
30 
31 #include "../vector.h"
32 #include <initializer_list>
33 
34 namespace Vc_VERSIONED_NAMESPACE
35 {
36 
37  namespace
38  {
39  template<typename Container, typename T> struct make_container_helper
40  {
41  static constexpr Container help(std::initializer_list<T> list) { return { list }; }
42  };
43 
44  template <typename T_, typename Abi, typename Alloc,
45  template <class, class> class Container>
46  struct make_container_helper<Container<Vector<T_, Abi>, Alloc>,
47  typename Vector<T_, Abi>::EntryType> {
48  typedef Vector<T_, Abi> V;
49  typedef typename V::EntryType T;
50  typedef Container<V, Alloc> C;
51  static inline C help(std::initializer_list<T> list) {
52  const std::size_t size = (list.size() + (V::Size - 1)) / V::Size;
53  C v(size);
54  auto containerIt = v.begin();
55  auto init = std::begin(list);
56  const auto initEnd = std::end(list);
57  for (std::size_t i = 0; i < size - 1; ++i) {
58  *containerIt++ = V(init, Vc::Unaligned);
59  init += V::Size;
60  }
61  Vc_ASSERT(all_of(*containerIt == V::Zero()));
62  int j = 0;
63  while (init != initEnd) {
64  (*containerIt)[j++] = *init++;
65  }
66  return v;
67  }
68  };
69 
70  template <typename T_, typename Abi, std::size_t N,
71  template <class, std::size_t> class Container>
72  struct make_container_helper<Container<Vector<T_, Abi>, N>,
73  typename Vector<T_, Abi>::EntryType> {
74  typedef Vector<T_, Abi> V;
75  typedef typename V::EntryType T;
76  static constexpr std::size_t size = (N + (V::Size - 1)) / V::Size;
77  typedef Container<
78  V,
79 #if defined Vc_CLANG && Vc_CLANG < 0x30700 // TODO: when did Vc_APPLECLANG fix it?
80  // clang before 3.7.0 has a bug when returning std::array<__m256x, 1>. So
81  // increase it to std::array<__m256x, 2> and fill it with zeros. Better
82  // than returning garbage.
83  (size == 1 && std::is_same<Abi, VectorAbi::Avx>::value) ? 2 :
84 #endif
85  size> C;
86  static inline C help(std::initializer_list<T> list) {
87  Vc_ASSERT(N == list.size())
88  Vc_ASSERT(size == (list.size() + (V::Size - 1)) / V::Size)
89  C v;
90  auto containerIt = v.begin();
91  auto init = std::begin(list);
92  const auto initEnd = std::end(list);
93  for (std::size_t i = 0; i < size - 1; ++i) {
94  *containerIt++ = V(init, Vc::Unaligned);
95  init += V::Size;
96  }
97  Vc_ASSERT(all_of(*containerIt == V::Zero()));
98  int j = 0;
99  while (init != initEnd) {
100  (*containerIt)[j++] = *init++;
101  }
102  return v;
103  }
104  };
105  } // anonymous namespace
106 
136  template<typename Container, typename T>
137  constexpr auto makeContainer(std::initializer_list<T> list) -> decltype(make_container_helper<Container, T>::help(list))
138  {
139  return make_container_helper<Container, T>::help(list);
140  }
141 
142  template<typename Container, typename T>
143  constexpr auto make_container(std::initializer_list<T> list) -> decltype(makeContainer<Container, T>(list))
144  {
145  return makeContainer<Container, T>(list);
146  }
147 
148 } // namespace Vc
149 
150 #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: mask.h:397
constexpr VectorSpecialInitializerZero Zero
The special object Vc::Zero can be used to construct Vector and Mask objects initialized to zero/fals...
Definition: types.h:81
constexpr UnalignedTag Unaligned
Use this object for a flags parameter to request unaligned loads and stores.