Vc  1.4.1
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 } // namespace Traits
258 } // namespace Vc
259 
260 namespace std
261 {
262 template <class T, size_t Size>
263 inline
264 #ifdef Vc_MSVC
265  // MSVC fails to do SFINAE correctly and gets totally confused:
266  // error C2433: 'type': 'inline' not permitted on data declarations
267  // error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
268  // error C2061: syntax error: identifier 'swap'
269  void
270 #else
271  typename enable_if<is_same<void, decltype(swap(declval<T&>(), declval<T&>()))>::value,
272  void>::type
273 #endif
274  swap(const Vc::array<T, Size>& x_,
275  const Vc::array<T, Size>& y_) noexcept(swap(declval<T&>(), declval<T&>()))
276 {
277  x_.swap(y_);
278 }
279 
280 template <class T, size_t Size>
281 class tuple_size<Vc::array<T, Size>> : public integral_constant<size_t, Size>
282 {
283 };
284 
285 template <size_t I, class T, size_t Size> class tuple_element<I, Vc::array<T, Size>>
286 {
287 public:
288  typedef T type;
289 };
290 
291 template <size_t I, class T, size_t Size>
292 inline constexpr typename std::enable_if<(I < Size), T&>::type get(
293  Vc::array<T, Size>& a_) noexcept
294 {
295  return a_.elems_[I];
296 }
297 
298 template <size_t I, class T, size_t Size>
299 inline constexpr typename std::enable_if<(I < Size), const T&>::type get(
300  const Vc::array<T, Size>& a_) noexcept
301 {
302  return a_.elems_[I];
303 }
304 
305 template <size_t I, class T, size_t Size>
306 inline constexpr typename std::enable_if<(I < Size), T&&>::type get(
307  Vc::array<T, Size>&& a_) noexcept
308 {
309  return std::move(a_.elems_[I]);
310 }
311 } // namespace std
312 
313 #endif // VC_INCLUDE_VC_ARRAY_
314 
315 // vim: ft=cpp foldmethod=marker