Vc  1.3.3-dev
SIMD Vector Classes for C++
array
1 /* This file is part of the Vc library. {{{
2 Copyright © 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 //===---------------------------- array -----------------------------------===//
28 //
29 // The LLVM Compiler Infrastructure
30 //
31 // This file is dual licensed under the MIT and the University of Illinois Open
32 // Source Licenses. See LICENSE.TXT for details.
33 //
34 //===----------------------------------------------------------------------===//
35 
36 #ifndef VC_INCLUDE_VC_ARRAY_
37 #define VC_INCLUDE_VC_ARRAY_
38 
39 #include <type_traits>
40 #include <utility>
41 #include <iterator>
42 #include <algorithm>
43 #include <stdexcept>
44 
45 #include "common/subscript.h"
46 
47 namespace Vc_VERSIONED_NAMESPACE
48 {
49 /**
50  * \ingroup Containers
51  * This is `std::array` with additional subscript operators supporting gather and scatter operations.
52  *
53  * The [std::array](https://en.cppreference.com/w/cpp/container/array) documentation applies.
54  *
55  * Gathers from structured data (AoS: arrays of struct) are possible via a special
56  * subscript operator.
57  * Example:
58  * \code
59  * Vc::array<float, 100> data;
60  * std::iota(data.begin(), data.end(), 0.f); // fill with values 0, 1, 2, ...
61  * auto indexes = float_v::IndexType::IndexesFromZero();
62  * float_v gathered = data[indexes]; // gathered == [0, 1, 2, ...]
63  * \endcode
64  *
65  * This also works for gathers into arrays of structures:
66  * \code
67  * struct Point { float x, y, z; };
68  * Vc::array<Point, 100> points;
69  * // fill points ...
70  * auto indexes = float_v::IndexType::IndexesFromZero();
71  * float_v xs = data[indexes][&Point::x]; // [points[0].x, points[1].x, points[2].x, ...]
72  * float_v ys = data[indexes][&Point::y]; // [points[0].y, points[1].y, points[2].y, ...]
73  * float_v zs = data[indexes][&Point::z]; // [points[0].z, points[1].z, points[2].z, ...]
74  * \endcode
75  *
76  * Arrays may also be nested:
77  * \code:
78  * Vc::array<Vc::array<float, 3>, 100> points;
79  * // fill points ...
80  * auto indexes = float_v::IndexType::IndexesFromZero();
81  * float_v xs = data[indexes][0]; // [points[0][0], points[1][0], points[2][0], ...]
82  * float_v ys = data[indexes][1]; // [points[0][1], points[1][1], points[2][1], ...]
83  * float_v zs = data[indexes][2]; // [points[0][2], points[1][2], points[2][2], ...]
84  * \endcode
85  */
86 template <class T, size_t Size> struct array {
87  // types:
88  typedef array self_;
89  typedef T value_type;
90  typedef value_type& reference;
91  typedef const value_type& const_reference;
92  typedef value_type* iterator;
93  typedef const value_type* const_iterator;
94  typedef value_type* pointer;
95  typedef const value_type* const_pointer;
96  typedef size_t size_type;
97  typedef ptrdiff_t difference_type;
98  typedef std::reverse_iterator<iterator> reverse_iterator;
99  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
100 
101  value_type elems_[Size > 0 ? Size : 1];
102 
103  // No explicit construct/copy/destroy for aggregate type
104  void fill(const value_type& u_) { std::fill_n(elems_, Size, u_); }
105  void swap(array& a_) noexcept(std::swap(std::declval<T &>(), std::declval<T &>()))
106  {
107  std::swap_ranges(elems_, elems_ + Size, a_.elems_);
108  }
109 
110  // iterators:
111  iterator begin() noexcept { return iterator(elems_); }
112  const_iterator begin() const noexcept { return const_iterator(elems_); }
113  iterator end() noexcept { return iterator(elems_ + Size); }
114  const_iterator end() const noexcept { return const_iterator(elems_ + Size); }
115  reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
116  const_reverse_iterator rbegin() const noexcept
117  {
118  return const_reverse_iterator(end());
119  }
120  reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
121  const_reverse_iterator rend() const noexcept
122  {
123  return const_reverse_iterator(begin());
124  }
125 
126  const_iterator cbegin() const noexcept { return begin(); }
127  const_iterator cend() const noexcept { return end(); }
128  const_reverse_iterator crbegin() const noexcept { return rbegin(); }
129  const_reverse_iterator crend() const noexcept { return rend(); }
130  // capacity:
131  constexpr size_type size() const noexcept { return Size; }
132  constexpr size_type max_size() const noexcept { return Size; }
133  constexpr bool empty() const noexcept { return Size == 0; }
134  // element access:
135  reference operator[](size_type n_) { return elems_[n_]; }
136  constexpr const_reference operator[](size_type n_) const { return elems_[n_]; }
137 
138  /**
139  * \name Data-Parallel Subscripting for Gather & Scatter
140  */
141  ///@{
142  template <typename I>
143  Vc_ALWAYS_INLINE auto operator[](I&& arg_)
144  -> decltype(subscript_operator(*this, std::forward<I>(arg_)))
145  {
146  return subscript_operator(*this, std::forward<I>(arg_));
147  }
148 
149  template <typename I>
150  Vc_ALWAYS_INLINE auto operator[](I&& arg_) const
151  -> decltype(subscript_operator(*this, std::forward<I>(arg_)))
152  {
153  return subscript_operator(*this, std::forward<I>(arg_));
154  }
155  ///@}
156 
157  reference at(size_type n_);
158  constexpr const_reference at(size_type n_) const;
159 
160  reference front() { return elems_[0]; }
161  constexpr const_reference front() const { return elems_[0]; }
162  reference back() { return elems_[Size > 0 ? Size - 1 : 0]; }
163  constexpr const_reference back() const { return elems_[Size > 0 ? Size - 1 : 0]; }
164  value_type* data() noexcept { return elems_; }
165  const value_type* data() const noexcept { return elems_; }
166 };
167 
168 template <class T, size_t Size>
169 typename array<T, Size>::reference array<T, Size>::at(size_type n_)
170 {
171  if (n_ >= Size) {
172  throw std::out_of_range("array::at");
173  }
174  return elems_[n_];
175 }
176 
177 template <class T, size_t Size>
178 constexpr typename array<T, Size>::const_reference array<T, Size>::at(size_type n_) const
179 {
180  return n_ >= Size ? (throw std::out_of_range("array::at"), elems_[0]) : elems_[n_];
181 }
182 
183 template <class T, size_t Size>
184 inline bool operator==(const array<T, Size>& x_, const array<T, Size>& y_)
185 {
186  return std::equal(x_.elems_, x_.elems_ + Size, y_.elems_);
187 }
188 
189 template <class T, size_t Size>
190 inline bool operator!=(const array<T, Size>& x_, const array<T, Size>& y_)
191 {
192  return !(x_ == y_);
193 }
194 
195 template <class T, size_t Size>
196 inline bool operator<(const array<T, Size>& x_, const array<T, Size>& y_)
197 {
198  return std::lexicographical_compare(x_.elems_, x_.elems_ + Size, y_.elems_,
199  y_.elems_ + Size);
200 }
201 
202 template <class T, size_t Size>
203 inline bool operator>(const array<T, Size>& x_, const array<T, Size>& y_)
204 {
205  return y_ < x_;
206 }
207 
208 template <class T, size_t Size>
209 inline bool operator<=(const array<T, Size>& x_, const array<T, Size>& y_)
210 {
211  return !(y_ < x_);
212 }
213 
214 template <class T, size_t Size>
215 inline bool operator>=(const array<T, Size>& x_, const array<T, Size>& y_)
216 {
217  return !(x_ < y_);
218 }
219 
220 /**\name non-member begin & end
221  * Implement the non-member begin & end functions in the %Vc namespace so that ADL works
222  * and `begin(some_vc_array)` always works.
223  */
224 ///@{
225 template <typename T, std::size_t N>
226 inline auto begin(array<T, N>& arr) -> decltype(arr.begin())
227 {
228  return arr.begin();
229 }
230 template <typename T, std::size_t N>
231 inline auto begin(const array<T, N>& arr) -> decltype(arr.begin())
232 {
233  return arr.begin();
234 }
235 template <typename T, std::size_t N>
236 inline auto end(array<T, N>& arr) -> decltype(arr.end())
237 {
238  return arr.end();
239 }
240 template <typename T, std::size_t N>
241 inline auto end(const array<T, N>& arr) -> decltype(arr.end())
242 {
243  return arr.end();
244 }
245 ///@}
246 
247 namespace Traits
248 {
249 template <typename T, std::size_t N>
250 struct has_no_allocated_data_impl<Vc::array<T, N>> : public std::true_type
251 {
252 };
253 template <typename T, std::size_t N>
254 struct has_contiguous_storage_impl<Vc::array<T, N>> : public std::true_type
255 {
256 };
257 
258 static_assert(has_no_allocated_data<const volatile Vc::array<int, 256> &>::value, "");
259 static_assert(has_no_allocated_data<const volatile Vc::array<int, 256>>::value, "");
260 static_assert(has_no_allocated_data<volatile Vc::array<int, 256> &>::value, "");
261 static_assert(has_no_allocated_data<volatile Vc::array<int, 256>>::value, "");
262 static_assert(has_no_allocated_data<const Vc::array<int, 256> &>::value, "");
263 static_assert(has_no_allocated_data<const Vc::array<int, 256>>::value, "");
264 static_assert(has_no_allocated_data<Vc::array<int, 256>>::value, "");
265 static_assert(has_no_allocated_data<Vc::array<int, 256> &>::value, "");
266 static_assert(has_no_allocated_data<Vc::array<int, 256> &&>::value, "");
267 
268 } // namespace Traits
269 } // namespace Vc
270 
271 namespace std
272 {
273 template <class T, size_t Size>
274 inline
275 #ifdef Vc_MSVC
276  // MSVC fails to do SFINAE correctly and gets totally confused:
277  // error C2433: 'type': 'inline' not permitted on data declarations
278  // error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
279  // error C2061: syntax error: identifier 'swap'
280  void
281 #else
282  typename enable_if<is_same<void, decltype(swap(declval<T&>(), declval<T&>()))>::value,
283  void>::type
284 #endif
285  swap(const Vc::array<T, Size>& x_,
286  const Vc::array<T, Size>& y_) noexcept(swap(declval<T&>(), declval<T&>()))
287 {
288  x_.swap(y_);
289 }
290 
291 template <class T, size_t Size>
292 class tuple_size<Vc::array<T, Size>> : public integral_constant<size_t, Size>
293 {
294 };
295 
296 template <size_t I, class T, size_t Size> class tuple_element<I, Vc::array<T, Size>>
297 {
298 public:
299  typedef T type;
300 };
301 
302 template <size_t I, class T, size_t Size>
303 inline constexpr typename std::enable_if<(I < Size), T&>::type get(
304  Vc::array<T, Size>& a_) noexcept
305 {
306  return a_.elems_[I];
307 }
308 
309 template <size_t I, class T, size_t Size>
310 inline constexpr typename std::enable_if<(I < Size), const T&>::type get(
311  const Vc::array<T, Size>& a_) noexcept
312 {
313  return a_.elems_[I];
314 }
315 
316 template <size_t I, class T, size_t Size>
317 inline constexpr typename std::enable_if<(I < Size), T&&>::type get(
318  Vc::array<T, Size>&& a_) noexcept
319 {
320  return std::move(a_.elems_[I]);
321 }
322 } // namespace std
323 
324 #endif // VC_INCLUDE_VC_ARRAY_
325 
326 // vim: ft=cpp foldmethod=marker