Vc  1.0.0-dev
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(std::is_convertible<MT, EntryType>::value, \
121  "The memory pointer needs to point to a type that can be converted to the " \
122  "EntryType of this SIMD vector type."); \
123  static_assert( \
124  Vc::Traits::has_subscript_operator<IT>::value, \
125  "The indexes argument must be a type that implements the subscript operator."); \
126  static_assert(!Traits::is_simd_vector<IT>::value || Traits::simd_vector_size<IT>::value >= Size, \
127  "If you use a SIMD vector for the indexes parameter, the index vector must " \
128  "have at least as many entries as this SIMD vector."); \
129  static_assert(!std::is_array<T>::value || \
130  (std::rank<T>::value == 1 && \
131  (std::extent<T>::value == 0 || std::extent<T>::value >= Size)), \
132  "If you use a simple array for the indexes parameter, the array must have " \
133  "at least as many entries as this SIMD vector.")
134 
174 
177  template <typename MT, typename IT,
178  typename = enable_if<Traits::has_subscript_operator<IT>::value>>
179  Vc_INTRINSIC Vc_CURRENT_CLASS_NAME(const MT *mem, IT &&indexes)
180  {
181  Vc_ASSERT_GATHER_PARAMETER_TYPES__;
182  gatherImplementation(mem, adjustIndexParameter(std::forward<IT>(indexes)));
183  }
184 
186  template <typename MT, typename IT,
187  typename = enable_if<Vc::Traits::has_subscript_operator<IT>::value>>
188  Vc_INTRINSIC Vc_CURRENT_CLASS_NAME(const MT *mem, IT &&indexes, MaskArgument mask)
189  {
190  Vc_ASSERT_GATHER_PARAMETER_TYPES__;
191  gatherImplementation(mem, adjustIndexParameter(std::forward<IT>(indexes)), mask);
192  }
193 
195  template <typename MT,
196  typename IT,
197  typename = enable_if<Vc::Traits::has_subscript_operator<IT>::value>>
198  Vc_INTRINSIC void gather(const MT *mem, IT &&indexes)
199  {
200  Vc_ASSERT_GATHER_PARAMETER_TYPES__;
201  gatherImplementation(mem, adjustIndexParameter(std::forward<IT>(indexes)));
202  }
203 
205  template <typename MT,
206  typename IT,
207  typename = enable_if<Vc::Traits::has_subscript_operator<IT>::value>>
208  Vc_INTRINSIC void gather(const MT *mem, IT &&indexes, MaskArgument mask)
209  {
210  Vc_ASSERT_GATHER_PARAMETER_TYPES__;
211  gatherImplementation(mem, adjustIndexParameter(std::forward<IT>(indexes)), mask);
212  }
214 
221  template <typename MT, typename IT>
223  Vc_INTRINSIC void gather(const Common::GatherArguments<MT, IT> &args)
224  {
225  gather(args.address, adjustIndexParameter(args.indexes));
226  }
227 
228  template <typename MT, typename IT>
229  Vc_INTRINSIC void gather(const Common::GatherArguments<MT, IT> &args, MaskArgument mask)
230  {
231  gather(args.address, adjustIndexParameter(args.indexes), mask);
232  }
234 
235 #undef Vc_ASSERT_GATHER_PARAMETER_TYPES__