Vc  1.0.0-dev
SIMD Vector Classes for C++
interleavedmemory.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2012-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_INTERLEAVEDMEMORY_H_
30 #define VC_COMMON_INTERLEAVEDMEMORY_H_
31 
32 #include "macros.h"
33 
34 namespace Vc_VERSIONED_NAMESPACE
35 {
36 namespace Common
37 {
41 template<typename V, typename I, bool Readonly> struct InterleavedMemoryAccessBase
42 {
43  // Partial specialization doesn't work for functions without partial specialization of the whole
44  // class. Therefore we capture the contents of InterleavedMemoryAccessBase in a macro to easily
45  // copy it into its specializations.
46  typedef typename std::conditional<
47  Readonly, typename std::add_const<typename V::EntryType>::type,
48  typename V::EntryType>::type T;
49  typedef typename V::AsArg VArg;
50  typedef T Ta Vc_MAY_ALIAS;
51  const I m_indexes;
52  Ta *const m_data;
53 
54  Vc_ALWAYS_INLINE InterleavedMemoryAccessBase(typename I::AsArg indexes, Ta *data)
55  : m_indexes(indexes), m_data(data)
56  {
57  }
58 
59  // implementations of the following are in {scalar,sse,avx}/interleavedmemory.tcc
60  inline void deinterleave(V &v0, V &v1) const;
61  inline void deinterleave(V &v0, V &v1, V &v2) const;
62  inline void deinterleave(V &v0, V &v1, V &v2, V &v3) const;
63  inline void deinterleave(V &v0, V &v1, V &v2, V &v3, V &v4) const;
64  inline void deinterleave(V &v0, V &v1, V &v2, V &v3, V &v4, V &v5) const;
65  inline void deinterleave(V &v0, V &v1, V &v2, V &v3, V &v4, V &v5, V &v6) const;
66  inline void deinterleave(V &v0, V &v1, V &v2, V &v3, V &v4, V &v5, V &v6, V &v7) const;
67 
68  inline void interleave(VArg v0, VArg v1);
69  inline void interleave(VArg v0, VArg v1, VArg v2);
70  inline void interleave(VArg v0, VArg v1, VArg v2, VArg v3);
71  inline void interleave(VArg v0, VArg v1, VArg v2, VArg v3, VArg v4);
72  inline void interleave(VArg v0, VArg v1, VArg v2, VArg v3, VArg v4, VArg v5);
73  inline void interleave(VArg v0, VArg v1, VArg v2, VArg v3, VArg v4, VArg v5, VArg v6);
74  inline void interleave(VArg v0, VArg v1, VArg v2, VArg v3, VArg v4, VArg v5, VArg v6, VArg v7);
75 
76 protected:
77  template <typename T, std::size_t... Indexes>
78  Vc_INTRINSIC void callInterleave(T &&a, index_sequence<Indexes...>)
79  {
80  interleave(a[Indexes]...);
81  }
82 };
83 
87 // delay execution of the deinterleaving gather until operator=
88 template <size_t StructSize, typename V, typename I = typename V::IndexType,
89  bool Readonly>
90 struct InterleavedMemoryReadAccess : public InterleavedMemoryAccessBase<V, I, Readonly>
91 {
92  typedef InterleavedMemoryAccessBase<V, I, Readonly> Base;
93  typedef typename Base::Ta Ta;
94 
95  Vc_ALWAYS_INLINE InterleavedMemoryReadAccess(Ta *data, typename I::AsArg indexes)
96  : Base(
97  StructSize == 1 ? indexes : StructSize == 2
98  ? indexes << 1
99  : StructSize == 4
100  ? indexes << 2
101  : StructSize == 8
102  ? indexes << 3
103  : StructSize == 16
104  ? indexes << 4
105  : indexes * I(StructSize),
106  data)
107  {
108  }
109 
110  template <typename T, std::size_t... Indexes>
111  Vc_ALWAYS_INLINE T deinterleave_unpack(index_sequence<Indexes...>) const
112  {
113  T r;
114  this->deinterleave(std::get<Indexes>(r)...);
115  return r;
116  }
117 
118  template <typename T,
119  typename = enable_if<(std::is_default_constructible<T>::value &&
120  std::is_same<V, Traits::decay<decltype(std::get<0>(
121  std::declval<T &>()))>>::value)>>
122  Vc_ALWAYS_INLINE operator T() const
123  {
124  return deinterleave_unpack<T>(make_index_sequence<std::tuple_size<T>::value>());
125  }
126 };
127 
129 template<typename I> struct CheckIndexesUnique
130 {
131 #ifdef NDEBUG
132  static Vc_INTRINSIC void test(const I &) {}
133 #else
134  static void test(const I &indexes)
135  {
136  const I test = indexes.sorted();
137  Vc_ASSERT(I::Size == 1 || (test == test.rotated(1)).isEmpty())
138  }
139 #endif
140 };
142 template<size_t S> struct CheckIndexesUnique<SuccessiveEntries<S> >
143 {
144  static Vc_INTRINSIC void test(const SuccessiveEntries<S> &) {}
145 };
146 
150 template <size_t StructSize, typename V, typename I = typename V::IndexType>
151 struct InterleavedMemoryAccess : public InterleavedMemoryReadAccess<StructSize, V, I, false>
152 {
153  typedef InterleavedMemoryAccessBase<V, I, false> Base;
154  typedef typename Base::Ta Ta;
155 
156  Vc_ALWAYS_INLINE InterleavedMemoryAccess(Ta *data, typename I::AsArg indexes)
157  : InterleavedMemoryReadAccess<StructSize, V, I, false>(data, indexes)
158  {
159  CheckIndexesUnique<I>::test(indexes);
160  }
161 
162  template <int N> Vc_ALWAYS_INLINE void operator=(VectorReferenceArray<N, V> &&rhs)
163  {
164  static_assert(N <= StructSize,
165  "You_are_trying_to_scatter_more_data_into_the_struct_than_it_has");
166  this->callInterleave(std::move(rhs), make_index_sequence<N>());
167  }
168  template <int N> Vc_ALWAYS_INLINE void operator=(VectorReferenceArray<N, const V> &&rhs)
169  {
170  static_assert(N <= StructSize,
171  "You_are_trying_to_scatter_more_data_into_the_struct_than_it_has");
172  this->callInterleave(std::move(rhs), make_index_sequence<N>());
173  }
174 };
175 
187 template<typename S, typename V> class InterleavedMemoryWrapper
188 {
189  typedef typename std::conditional<std::is_const<S>::value,
190  const typename V::EntryType,
191  typename V::EntryType>::type T;
192  typedef typename V::IndexType I;
193  typedef typename V::AsArg VArg;
194  typedef const I &IndexType;
195  static constexpr std::size_t StructSize = sizeof(S) / sizeof(T);
196  typedef InterleavedMemoryAccess<StructSize, V> Access;
197  typedef InterleavedMemoryReadAccess<StructSize, V> ReadAccess;
198  typedef InterleavedMemoryAccess<StructSize, V, SuccessiveEntries<StructSize> > AccessSuccessiveEntries;
199  typedef InterleavedMemoryReadAccess<StructSize, V, SuccessiveEntries<StructSize> > ReadSuccessiveEntries;
200  typedef T Ta Vc_MAY_ALIAS;
201  Ta *const m_data;
202 
203  static_assert(StructSize * sizeof(T) == sizeof(S),
204  "InterleavedMemoryAccess_does_not_support_packed_structs");
205 
206 public:
212  Vc_ALWAYS_INLINE InterleavedMemoryWrapper(S *s)
213  : m_data(reinterpret_cast<Ta *>(s))
214  {
215  }
216 
269  template <typename IT>
270  Vc_ALWAYS_INLINE enable_if<
271  std::is_convertible<IT, IndexType>::value && !std::is_const<S>::value, Access>
272  operator[](IT indexes)
273  {
274  return Access(m_data, indexes);
275  }
276 
278  Vc_ALWAYS_INLINE ReadAccess operator[](IndexType indexes) const
279  {
280  return ReadAccess(m_data, indexes);
281  }
282 
284  Vc_ALWAYS_INLINE ReadAccess gather(IndexType indexes) const { return operator[](indexes); }
285 
319  Vc_ALWAYS_INLINE ReadSuccessiveEntries operator[](size_t first) const
320  {
321  return ReadSuccessiveEntries(m_data, first);
322  }
323 
324  Vc_ALWAYS_INLINE AccessSuccessiveEntries operator[](size_t first)
325  {
326  return AccessSuccessiveEntries(m_data, first);
327  }
328 
329  //Vc_ALWAYS_INLINE Access scatter(I indexes, VArg v0, VArg v1);
330 };
331 } // namespace Common
332 
333 using Common::InterleavedMemoryWrapper;
334 
335 template <typename V, typename S>
336 inline Common::InterleavedMemoryWrapper<S, V> make_interleave_wrapper(S *s)
337 {
338  return Common::InterleavedMemoryWrapper<S, V>(s);
339 }
340 } // namespace Vc
341 
342 #endif // VC_COMMON_INTERLEAVEDMEMORY_H_
Wraps a pointer to memory with convenience functions to access it via vectors.
ReadAccess operator[](IndexType indexes) const
const overload (gathers only) of the above function
InterleavedMemoryWrapper(S *s)
Constructs the wrapper object.
void deinterleave(V *a, V *b, const M *memory, A align)
Definition: deinterleave.h:77
ReadAccess gather(IndexType indexes) const
alias of the above function
enable_if< std::is_convertible< IT, IndexType >::value &&!std::is_const< S >::value, Access > operator[](IT indexes)
Interleaved scatter/gather access.
std::pair< V, V > interleave(const V &a, const V &b)
Interleaves the entries from a and b into two vectors of the same type.
Definition: interleave.h:56
ReadSuccessiveEntries operator[](size_t first) const
Interleaved access.