Vc  1.3.2-dev
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 <Vc/vector.h>
32 #include <initializer_list>
33 #include "macros.h"
34 
35 namespace Vc_VERSIONED_NAMESPACE
36 {
37 
38  namespace
39  {
40  template<typename Container, typename T> struct make_container_helper
41  {
42  static constexpr Container help(std::initializer_list<T> list) { return { list }; }
43  };
44 
45  template <typename T_, typename Abi, typename Alloc,
46  template <class, class> class Container>
47  struct make_container_helper<Container<Vector<T_, Abi>, Alloc>,
48  typename Vector<T_, Abi>::EntryType> {
49  typedef Vector<T_, Abi> 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  Vc_ASSERT(all_of(*containerIt == V::Zero()));
63  int j = 0;
64  while (init != initEnd) {
65  (*containerIt)[j++] = *init++;
66  }
67  return v;
68  }
69  };
70 
71  template <typename T_, typename Abi, std::size_t N,
72  template <class, std::size_t> class Container>
73  struct make_container_helper<Container<Vector<T_, Abi>, N>,
74  typename Vector<T_, Abi>::EntryType> {
75  typedef Vector<T_, Abi> V;
76  typedef typename V::EntryType T;
77  static constexpr std::size_t size = (N + (V::Size - 1)) / V::Size;
78  typedef Container<
79  V,
80 #if defined Vc_CLANG && Vc_CLANG < 0x30700 // TODO: when did Vc_APPLECLANG fix it?
81  // clang before 3.7.0 has a bug when returning std::array<__m256x, 1>. So
82  // increase it to std::array<__m256x, 2> and fill it with zeros. Better
83  // than returning garbage.
84  (size == 1 && std::is_same<Abi, VectorAbi::Avx>::value) ? 2 :
85 #endif
86  size> C;
87  static inline C help(std::initializer_list<T> list) {
88  Vc_ASSERT(N == list.size())
89  Vc_ASSERT(size == (list.size() + (V::Size - 1)) / V::Size)
90  C v;
91  auto containerIt = v.begin();
92  auto init = std::begin(list);
93  const auto initEnd = std::end(list);
94  for (std::size_t i = 0; i < size - 1; ++i) {
95  *containerIt++ = V(init, Vc::Unaligned);
96  init += V::Size;
97  }
98  Vc_ASSERT(all_of(*containerIt == V::Zero()));
99  int j = 0;
100  while (init != initEnd) {
101  (*containerIt)[j++] = *init++;
102  }
103  return v;
104  }
105  };
106  } // anonymous namespace
107 
137  template<typename Container, typename T>
138  constexpr auto makeContainer(std::initializer_list<T> list) -> decltype(make_container_helper<Container, T>::help(list))
139  {
140  return make_container_helper<Container, T>::help(list);
141  }
142 
143  template<typename Container, typename T>
144  constexpr auto make_container(std::initializer_list<T> list) -> decltype(makeContainer<Container, T>(list))
145  {
146  return makeContainer<Container, T>(list);
147  }
148 
149 } // namespace Vc
150 
151 #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:44
constexpr VectorSpecialInitializerZero Zero
The special object Vc::Zero can be used to construct Vector and Mask objects initialized to zero/fals...
Definition: types.h:84
constexpr UnalignedTag Unaligned
Use this object for a flags parameter to request unaligned loads and stores.