Vc  1.1.0
SIMD Vector Classes for C++
gatherinterface.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2014-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_CURRENT_CLASS_NAME
30 #error "incorrect use of common/gatherinterface.h: Vc_CURRENT_CLASS_NAME must be defined to the current class name for declaring constructors."
31 #endif
32 
34 // gathers
35 // A gather takes the following arguments:
36 // 1. A const pointer to memory of any type that can convert to EntryType
37 // 2. An indexes “vector”. The requirement is that the type implements the subscript operator,
38 // stores «Size» valid index values, and each offset to the pointer above yields a valid
39 // memory location for reading.
40 // 3. Optionally the third argument may be a mask. The mask disables several memory reads and
41 // thus removes the requirements in (2.) for the disabled entries.
42 
43 private:
54  // enable_if<std::can_convert<MT, EntryType>::value &&
55  // has_subscript_operator<IT>::value>
56  template <typename MT, typename IT>
57  inline void gatherImplementation(const MT *mem, IT &&indexes);
58 
63  template <typename MT, typename IT>
64  inline void gatherImplementation(const MT *mem, IT &&indexes, MaskArgument mask);
65 
74  template <typename IT, typename = enable_if<std::is_pointer<IT>::value ||
75  Traits::is_simd_vector<IT>::value>>
76  static Vc_INTRINSIC IT adjustIndexParameter(IT &&indexes)
77  {
78  return std::forward<IT>(indexes);
79  }
80 
91  template <typename IT,
92  typename = enable_if<
93  !std::is_pointer<IT>::value && !Traits::is_simd_vector<IT>::value &&
94  std::is_lvalue_reference<decltype(std::declval<IT>()[0])>::value>>
95  static Vc_INTRINSIC decltype(std::addressof(std::declval<IT>()[0]))
96  adjustIndexParameter(IT &&i)
97  {
98  return std::addressof(i[0]);
99  }
100 
108  template <typename IT>
109  static Vc_INTRINSIC
110  enable_if<!std::is_pointer<IT>::value && !Traits::is_simd_vector<IT>::value &&
111  !std::is_lvalue_reference<decltype(std::declval<IT>()[0])>::value,
112  IT>
113  adjustIndexParameter(IT &&i)
114  {
115  return std::forward<IT>(i);
116  }
117 
118 public:
119 #define Vc_ASSERT_GATHER_PARAMETER_TYPES_ \
120  static_assert( \
121  std::is_convertible<MT, EntryType>::value, \
122  "The memory pointer needs to point to a type that can be converted to the " \
123  "EntryType of this SIMD vector type."); \
124  static_assert( \
125  Vc::Traits::has_subscript_operator<IT>::value, \
126  "The indexes argument must be a type that implements the subscript operator."); \
127  static_assert( \
128  !Traits::is_simd_vector<IT>::value || \
129  Traits::simd_vector_size<IT>::value >= Size, \
130  "If you use a SIMD vector for the indexes parameter, the index vector must " \
131  "have at least as many entries as this SIMD vector."); \
132  static_assert( \
133  !std::is_array<T>::value || \
134  (std::rank<T>::value == 1 && \
135  (std::extent<T>::value == 0 || std::extent<T>::value >= Size)), \
136  "If you use a simple array for the indexes parameter, the array must have " \
137  "at least as many entries as this SIMD vector.")
138 
178 
181  template <typename MT, typename IT,
182  typename = enable_if<Traits::has_subscript_operator<IT>::value>>
183  Vc_INTRINSIC Vc_CURRENT_CLASS_NAME(const MT *mem, IT &&indexes)
184  {
185  Vc_ASSERT_GATHER_PARAMETER_TYPES_;
186  gatherImplementation(mem, adjustIndexParameter(std::forward<IT>(indexes)));
187  }
188 
190  template <typename MT, typename IT,
191  typename = enable_if<Vc::Traits::has_subscript_operator<IT>::value>>
192  Vc_INTRINSIC Vc_CURRENT_CLASS_NAME(const MT *mem, IT &&indexes, MaskArgument mask)
193  {
194  Vc_ASSERT_GATHER_PARAMETER_TYPES_;
195  gatherImplementation(mem, adjustIndexParameter(std::forward<IT>(indexes)), mask);
196  }
197 
199  template <typename MT,
200  typename IT,
201  typename = enable_if<Vc::Traits::has_subscript_operator<IT>::value>>
202  Vc_INTRINSIC void gather(const MT *mem, IT &&indexes)
203  {
204  Vc_ASSERT_GATHER_PARAMETER_TYPES_;
205  gatherImplementation(mem, adjustIndexParameter(std::forward<IT>(indexes)));
206  }
207 
209  template <typename MT,
210  typename IT,
211  typename = enable_if<Vc::Traits::has_subscript_operator<IT>::value>>
212  Vc_INTRINSIC void gather(const MT *mem, IT &&indexes, MaskArgument mask)
213  {
214  Vc_ASSERT_GATHER_PARAMETER_TYPES_;
215  gatherImplementation(mem, adjustIndexParameter(std::forward<IT>(indexes)), mask);
216  }
217 
219  template <typename S1, typename IT>
220  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector instead.")
221  Vc_CURRENT_CLASS_NAME(const S1 *array, const EntryType S1::*member1,
222  Vc_ALIGNED_PARAMETER(IT) indexes)
223  {
224  gather(Common::SubscriptOperation<S1, IT, std::ratio<1, 1>, true>(
225  array, indexes)[member1]
226  .gatherArguments());
227  }
229  template <typename S1, typename IT>
230  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector instead.")
231  Vc_CURRENT_CLASS_NAME(const S1 *array, const EntryType S1::*member1,
232  Vc_ALIGNED_PARAMETER(IT) indexes, MaskArgument mask)
233  {
234  gather(Common::SubscriptOperation<S1, IT, std::ratio<1, 1>, true>(
235  array, indexes)[member1]
236  .gatherArguments(),
237  mask);
238  }
240  template <typename S1, typename S2, typename IT>
241  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector instead.")
242  Vc_CURRENT_CLASS_NAME(const S1 *array, const S2 S1::*member1,
243  const EntryType S2::*member2,
244  Vc_ALIGNED_PARAMETER(IT) indexes)
245  {
246  gather(Common::SubscriptOperation<S1, IT, std::ratio<1, 1>, true>(
247  array, indexes)[member1][member2]
248  .gatherArguments());
249  }
251  template <typename S1, typename S2, typename IT>
252  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector instead.")
253  Vc_CURRENT_CLASS_NAME(const S1 *array, const S2 S1::*member1,
254  const EntryType S2::*member2,
255  Vc_ALIGNED_PARAMETER(IT) indexes, MaskArgument mask)
256  {
257  gather(Common::SubscriptOperation<S1, IT, std::ratio<1, 1>, true>(
258  array, indexes)[member1][member2]
259  .gatherArguments(),
260  mask);
261  }
263  template <typename S1, typename IT1, typename IT2>
264  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector instead.")
265  Vc_CURRENT_CLASS_NAME(const S1 *array, const EntryType *const S1::*ptrMember1,
266  Vc_ALIGNED_PARAMETER(IT1) outerIndexes,
267  Vc_ALIGNED_PARAMETER(IT2) innerIndexes)
268  {
269  gather(Common::SubscriptOperation<S1, IT1, std::ratio<1, 1>, true>(
270  array, outerIndexes)[ptrMember1][innerIndexes]
271  .gatherArguments());
272  }
274  template <typename S1, typename IT1, typename IT2>
275  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector instead.")
276  Vc_CURRENT_CLASS_NAME(const S1 *array, const EntryType *const S1::*ptrMember1,
277  Vc_ALIGNED_PARAMETER(IT1) outerIndexes,
278  Vc_ALIGNED_PARAMETER(IT2) innerIndexes, MaskArgument mask)
279  {
280  gather(Common::SubscriptOperation<S1, IT1, std::ratio<1, 1>, true>(
281  array, outerIndexes)[ptrMember1][innerIndexes]
282  .gatherArguments(),
283  mask);
284  }
286  template <typename S1, typename IT>
287  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector "
288  "instead.") void gather(const S1 *array,
289  const EntryType S1::*member1,
290  Vc_ALIGNED_PARAMETER(IT) indexes)
291  {
292  gather(Common::SubscriptOperation<S1, IT, std::ratio<1, 1>, true>(
293  array, indexes)[member1]
294  .gatherArguments());
295  }
297  template <typename S1, typename IT>
298  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector "
299  "instead.") void gather(const S1 *array,
300  const EntryType S1::*member1,
301  Vc_ALIGNED_PARAMETER(IT) indexes,
302  MaskArgument mask)
303  {
304  gather(Common::SubscriptOperation<S1, IT, std::ratio<1, 1>, true>(
305  array, indexes)[member1]
306  .gatherArguments(),
307  mask);
308  }
310  template <typename S1, typename S2, typename IT>
311  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector "
312  "instead.") void gather(const S1 *array, const S2 S1::*member1,
313  const EntryType S2::*member2,
314  Vc_ALIGNED_PARAMETER(IT) indexes)
315  {
316  gather(Common::SubscriptOperation<S1, IT, std::ratio<1, 1>, true>(
317  array, indexes)[member1][member2]
318  .gatherArguments());
319  }
321  template <typename S1, typename S2, typename IT>
322  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector "
323  "instead.") void gather(const S1 *array, const S2 S1::*member1,
324  const EntryType S2::*member2,
325  Vc_ALIGNED_PARAMETER(IT) indexes,
326  MaskArgument mask)
327  {
328  gather(Common::SubscriptOperation<S1, IT, std::ratio<1, 1>, true>(
329  array, indexes)[member1][member2]
330  .gatherArguments(),
331  mask);
332  }
334  template <typename S1, typename IT1, typename IT2>
335  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector "
336  "instead.") void gather(const S1 *array,
337  const EntryType *const S1::*ptrMember1,
338  Vc_ALIGNED_PARAMETER(IT1) outerIndexes,
339  Vc_ALIGNED_PARAMETER(IT2) innerIndexes)
340  {
341  gather(Common::SubscriptOperation<S1, IT1, std::ratio<1, 1>, true>(
342  array, outerIndexes)[ptrMember1][innerIndexes]
343  .gatherArguments());
344  }
346  template <typename S1, typename IT1, typename IT2>
347  inline Vc_DEPRECATED("use the subscript operator to Vc::array or Vc::vector "
348  "instead.") void gather(const S1 *array,
349  const EntryType *const S1::*ptrMember1,
350  Vc_ALIGNED_PARAMETER(IT1) outerIndexes,
351  Vc_ALIGNED_PARAMETER(IT2) innerIndexes,
352  MaskArgument mask)
353  {
354  gather(Common::SubscriptOperation<S1, IT1, std::ratio<1, 1>, true>(
355  array, outerIndexes)[ptrMember1][innerIndexes]
356  .gatherArguments(),
357  mask);
358  }
360 
367  template <typename MT, typename IT>
369  Vc_INTRINSIC void gather(const Common::GatherArguments<MT, IT> &args)
370  {
371  gather(args.address, adjustIndexParameter(args.indexes));
372  }
373 
374  template <typename MT, typename IT>
375  Vc_INTRINSIC void gather(const Common::GatherArguments<MT, IT> &args, MaskArgument mask)
376  {
377  gather(args.address, adjustIndexParameter(args.indexes), mask);
378  }
380 
381 #undef Vc_ASSERT_GATHER_PARAMETER_TYPES_