]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libc++/include/experimental/simd
Merge libc++ trunk r366426, resolve conflicts, and add FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / libc++ / include / experimental / simd
1 // -*- C++ -*-
2 //===------------------------------- simd ---------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP_EXPERIMENTAL_SIMD
10 #define _LIBCPP_EXPERIMENTAL_SIMD
11
12 /*
13     experimental/simd synopsis
14
15 namespace std::experimental {
16
17 inline namespace parallelism_v2 {
18
19 namespace simd_abi {
20
21 struct scalar {};
22 template <int N> struct fixed_size {};
23 template <typename T> inline constexpr int max_fixed_size = implementation-defined;
24 template <typename T> using compatible = implementation-defined;
25 template <typename T> using native = implementation-defined;
26
27 } // simd_abi
28
29 struct element_aligned_tag {};
30 struct vector_aligned_tag {};
31 template <size_t> struct overaligned_tag {};
32 inline constexpr element_aligned_tag element_aligned{};
33 inline constexpr vector_aligned_tag vector_aligned{};
34 template <size_t N> inline constexpr overaligned_tag<N> overaligned{};
35
36 // traits [simd.traits]
37 template <class T> struct is_abi_tag;
38 template <class T> inline constexpr bool is_abi_tag_v = is_abi_tag<T>::value;
39
40 template <class T> struct is_simd;
41 template <class T> inline constexpr bool is_simd_v = is_simd<T>::value;
42
43 template <class T> struct is_simd_mask;
44 template <class T> inline constexpr bool is_simd_mask_v = is_simd_mask<T>::value;
45
46 template <class T> struct is_simd_flag_type;
47 template <class T> inline constexpr bool is_simd_flag_type_v = is_simd_flag_type<T>::value;
48
49 template <class T, size_t N> struct abi_for_size { using type = see below; };
50 template <class T, size_t N> using abi_for_size_t = typename abi_for_size<T, N>::type;
51
52 template <class T, class Abi = simd_abi::compatible<T>> struct simd_size;
53 template <class T, class Abi = simd_abi::compatible<T>>
54 inline constexpr size_t simd_size_v = simd_size<T, Abi>::value;
55
56 template <class T, class U = typename T::value_type> struct memory_alignment;
57 template <class T, class U = typename T::value_type>
58 inline constexpr size_t memory_alignment_v = memory_alignment<T, U>::value;
59
60 // class template simd [simd.class]
61 template <class T, class Abi = simd_abi::compatible<T>> class simd;
62 template <class T> using native_simd = simd<T, simd_abi::native<T>>;
63 template <class T, int N> using fixed_size_simd = simd<T, simd_abi::fixed_size<N>>;
64
65 // class template simd_mask [simd.mask.class]
66 template <class T, class Abi = simd_abi::compatible<T>> class simd_mask;
67 template <class T> using native_simd_mask = simd_mask<T, simd_abi::native<T>>;
68 template <class T, int N> using fixed_size_simd_mask = simd_mask<T, simd_abi::fixed_size<N>>;
69
70 // casts [simd.casts]
71 template <class T, class U, class Abi> see below simd_cast(const simd<U, Abi>&);
72 template <class T, class U, class Abi> see below static_simd_cast(const simd<U, Abi>&);
73
74 template <class T, class Abi>
75 fixed_size_simd<T, simd_size_v<T, Abi>> to_fixed_size(const simd<T, Abi>&) noexcept;
76 template <class T, class Abi>
77 fixed_size_simd_mask<T, simd_size_v<T, Abi>> to_fixed_size(const simd_mask<T, Abi>&) noexcept;
78 template <class T, size_t N> native_simd<T> to_native(const fixed_size_simd<T, N>&) noexcept;
79 template <class T, size_t N>
80 native_simd_mask<T> to_native(const fixed_size_simd_mask<T, N>> &) noexcept;
81 template <class T, size_t N> simd<T> to_compatible(const fixed_size_simd<T, N>&) noexcept;
82 template <class T, size_t N> simd_mask<T> to_compatible(const fixed_size_simd_mask<T, N>&) noexcept;
83
84 template <size_t... Sizes, class T, class Abi>
85 tuple<simd<T, abi_for_size_t<Sizes>>...> split(const simd<T, Abi>&);
86 template <size_t... Sizes, class T, class Abi>
87 tuple<simd_mask<T, abi_for_size_t<Sizes>>...> split(const simd_mask<T, Abi>&);
88 template <class V, class Abi>
89 array<V, simd_size_v<typename V::value_type, Abi> / V::size()> split(
90 const simd<typename V::value_type, Abi>&);
91 template <class V, class Abi>
92 array<V, simd_size_v<typename V::value_type, Abi> / V::size()> split(
93 const simd_mask<typename V::value_type, Abi>&);
94
95 template <class T, class... Abis>
96 simd<T, abi_for_size_t<T, (simd_size_v<T, Abis> + ...)>> concat(const simd<T, Abis>&...);
97 template <class T, class... Abis>
98 simd_mask<T, abi_for_size_t<T, (simd_size_v<T, Abis> + ...)>> concat(const simd_mask<T, Abis>&...);
99
100 // reductions [simd.mask.reductions]
101 template <class T, class Abi> bool all_of(const simd_mask<T, Abi>&) noexcept;
102 template <class T, class Abi> bool any_of(const simd_mask<T, Abi>&) noexcept;
103 template <class T, class Abi> bool none_of(const simd_mask<T, Abi>&) noexcept;
104 template <class T, class Abi> bool some_of(const simd_mask<T, Abi>&) noexcept;
105 template <class T, class Abi> int popcount(const simd_mask<T, Abi>&) noexcept;
106 template <class T, class Abi> int find_first_set(const simd_mask<T, Abi>&);
107 template <class T, class Abi> int find_last_set(const simd_mask<T, Abi>&);
108
109 bool all_of(see below) noexcept;
110 bool any_of(see below) noexcept;
111 bool none_of(see below) noexcept;
112 bool some_of(see below) noexcept;
113 int popcount(see below) noexcept;
114 int find_first_set(see below) noexcept;
115 int find_last_set(see below) noexcept;
116
117 // masked assignment [simd.whereexpr]
118 template <class M, class T> class const_where_expression;
119 template <class M, class T> class where_expression;
120
121 // masked assignment [simd.mask.where]
122 template <class T> struct nodeduce { using type = T; }; // exposition only
123
124 template <class T> using nodeduce_t = typename nodeduce<T>::type; // exposition only
125
126 template <class T, class Abi>
127 where_expression<simd_mask<T, Abi>, simd<T, Abi>>
128 where(const typename simd<T, Abi>::mask_type&, simd<T, Abi>&) noexcept;
129
130 template <class T, class Abi>
131 const_where_expression<simd_mask<T, Abi>, const simd<T, Abi>>
132 where(const typename simd<T, Abi>::mask_type&, const simd<T, Abi>&) noexcept;
133
134 template <class T, class Abi>
135 where_expression<simd_mask<T, Abi>, simd_mask<T, Abi>>
136 where(const nodeduce_t<simd_mask<T, Abi>>&, simd_mask<T, Abi>&) noexcept;
137
138 template <class T, class Abi>
139 const_where_expression<simd_mask<T, Abi>, const simd_mask<T, Abi>>
140 where(const nodeduce_t<simd_mask<T, Abi>>&, const simd_mask<T, Abi>&) noexcept;
141
142 template <class T> where_expression<bool, T> where(see below k, T& d) noexcept;
143
144 template <class T>
145 const_where_expression<bool, const T> where(see below k, const T& d) noexcept;
146
147 // reductions [simd.reductions]
148 template <class T, class Abi, class BinaryOperation = std::plus<>>
149 T reduce(const simd<T, Abi>&, BinaryOperation = BinaryOperation());
150
151 template <class M, class V, class BinaryOperation>
152 typename V::value_type reduce(const const_where_expression<M, V>& x,
153 typename V::value_type neutral_element, BinaryOperation binary_op);
154
155 template <class M, class V>
156 typename V::value_type reduce(const const_where_expression<M, V>& x, plus<> binary_op = plus<>());
157
158 template <class M, class V>
159 typename V::value_type reduce(const const_where_expression<M, V>& x, multiplies<> binary_op);
160
161 template <class M, class V>
162 typename V::value_type reduce(const const_where_expression<M, V>& x, bit_and<> binary_op);
163
164 template <class M, class V>
165 typename V::value_type reduce(const const_where_expression<M, V>& x, bit_or<> binary_op);
166
167 template <class M, class V>
168 typename V::value_type reduce(const const_where_expression<M, V>& x, bit_xor<> binary_op);
169
170 template <class T, class Abi> T hmin(const simd<T, Abi>&);
171 template <class M, class V> T hmin(const const_where_expression<M, V>&);
172 template <class T, class Abi> T hmax(const simd<T, Abi>&);
173 template <class M, class V> T hmax(const const_where_expression<M, V>&);
174
175 // algorithms [simd.alg]
176 template <class T, class Abi> simd<T, Abi> min(const simd<T, Abi>&, const simd<T, Abi>&) noexcept;
177
178 template <class T, class Abi> simd<T, Abi> max(const simd<T, Abi>&, const simd<T, Abi>&) noexcept;
179
180 template <class T, class Abi>
181 std::pair<simd<T, Abi>, simd<T, Abi>> minmax(const simd<T, Abi>&, const simd<T, Abi>&) noexcept;
182
183 template <class T, class Abi>
184 simd<T, Abi> clamp(const simd<T, Abi>& v, const simd<T, Abi>& lo, const simd<T, Abi>& hi);
185
186 // [simd.whereexpr]
187 template <class M, class T>
188 class const_where_expression {
189   const M& mask; // exposition only
190   T& data; // exposition only
191 public:
192   const_where_expression(const const_where_expression&) = delete;
193   const_where_expression& operator=(const const_where_expression&) = delete;
194   remove_const_t<T> operator-() const &&;
195   template <class U, class Flags> void copy_to(U* mem, Flags f) const &&;
196 };
197
198 template <class M, class T>
199 class where_expression : public const_where_expression<M, T> {
200 public:
201   where_expression(const where_expression&) = delete;
202   where_expression& operator=(const where_expression&) = delete;
203   template <class U> void operator=(U&& x);
204   template <class U> void operator+=(U&& x);
205   template <class U> void operator-=(U&& x);
206   template <class U> void operator*=(U&& x);
207   template <class U> void operator/=(U&& x);
208   template <class U> void operator%=(U&& x);
209   template <class U> void operator&=(U&& x);
210   template <class U> void operator|=(U&& x);
211   template <class U> void operator^=(U&& x);
212   template <class U> void operator<<=(U&& x);
213   template <class U> void operator>>=(U&& x);
214   void operator++();
215   void operator++(int);
216   void operator--();
217   void operator--(int);
218   template <class U, class Flags> void copy_from(const U* mem, Flags);
219 };
220
221 // [simd.class]
222 template <class T, class Abi> class simd {
223 public:
224   using value_type = T;
225   using reference = see below;
226   using mask_type = simd_mask<T, Abi>;
227
228   using abi_type = Abi;
229   static constexpr size_t size() noexcept;
230   simd() = default;
231
232   // implicit type conversion constructor
233   template <class U> simd(const simd<U, simd_abi::fixed_size<size()>>&);
234
235   // implicit broadcast constructor (see below for constraints)
236   template <class U> simd(U&& value);
237
238   // generator constructor (see below for constraints)
239   template <class G> explicit simd(G&& gen);
240
241   // load constructor
242   template <class U, class Flags> simd(const U* mem, Flags f);
243
244   // loads [simd.load]
245   template <class U, class Flags> void copy_from(const U* mem, Flags f);
246
247   // stores [simd.store]
248   template <class U, class Flags> void copy_to(U* mem, Flags f) const;
249
250   // scalar access [simd.subscr]
251   reference operator[](size_t);
252   value_type operator[](size_t) const;
253
254   // unary operators [simd.unary]
255   simd& operator++();
256   simd operator++(int);
257   simd& operator--();
258   simd operator--(int);
259   mask_type operator!() const;
260   simd operator~() const; // see below
261   simd operator+() const;
262   simd operator-() const;
263
264   // binary operators [simd.binary]
265   friend simd operator+ (const simd&, const simd&);
266   friend simd operator- (const simd&, const simd&);
267   friend simd operator* (const simd&, const simd&);
268   friend simd operator/ (const simd&, const simd&);
269   friend simd operator% (const simd&, const simd&);
270   friend simd operator& (const simd&, const simd&);
271   friend simd operator| (const simd&, const simd&);
272   friend simd operator^ (const simd&, const simd&);
273   friend simd operator<<(const simd&, const simd&);
274   friend simd operator>>(const simd&, const simd&);
275   friend simd operator<<(const simd&, int);
276   friend simd operator>>(const simd&, int);
277
278   // compound assignment [simd.cassign]
279   friend simd& operator+= (simd&, const simd&);
280   friend simd& operator-= (simd&, const simd&);
281   friend simd& operator*= (simd&, const simd&);
282   friend simd& operator/= (simd&, const simd&);
283   friend simd& operator%= (simd&, const simd&);
284
285   friend simd& operator&= (simd&, const simd&);
286   friend simd& operator|= (simd&, const simd&);
287   friend simd& operator^= (simd&, const simd&);
288   friend simd& operator<<=(simd&, const simd&);
289   friend simd& operator>>=(simd&, const simd&);
290   friend simd& operator<<=(simd&, int);
291   friend simd& operator>>=(simd&, int);
292
293   // compares [simd.comparison]
294   friend mask_type operator==(const simd&, const simd&);
295   friend mask_type operator!=(const simd&, const simd&);
296   friend mask_type operator>=(const simd&, const simd&);
297   friend mask_type operator<=(const simd&, const simd&);
298   friend mask_type operator> (const simd&, const simd&);
299   friend mask_type operator< (const simd&, const simd&);
300 };
301
302 // [simd.math]
303 template <class Abi> using scharv = simd<signed char, Abi>; // exposition only
304 template <class Abi> using shortv = simd<short, Abi>; // exposition only
305 template <class Abi> using intv = simd<int, Abi>; // exposition only
306 template <class Abi> using longv = simd<long int, Abi>; // exposition only
307 template <class Abi> using llongv = simd<long long int, Abi>; // exposition only
308 template <class Abi> using floatv = simd<float, Abi>; // exposition only
309 template <class Abi> using doublev = simd<double, Abi>; // exposition only
310 template <class Abi> using ldoublev = simd<long double, Abi>; // exposition only
311 template <class T, class V> using samesize = fixed_size_simd<T, V::size()>; // exposition only
312
313 template <class Abi> floatv<Abi> acos(floatv<Abi> x);
314 template <class Abi> doublev<Abi> acos(doublev<Abi> x);
315 template <class Abi> ldoublev<Abi> acos(ldoublev<Abi> x);
316
317 template <class Abi> floatv<Abi> asin(floatv<Abi> x);
318 template <class Abi> doublev<Abi> asin(doublev<Abi> x);
319 template <class Abi> ldoublev<Abi> asin(ldoublev<Abi> x);
320
321 template <class Abi> floatv<Abi> atan(floatv<Abi> x);
322 template <class Abi> doublev<Abi> atan(doublev<Abi> x);
323 template <class Abi> ldoublev<Abi> atan(ldoublev<Abi> x);
324
325 template <class Abi> floatv<Abi> atan2(floatv<Abi> y, floatv<Abi> x);
326 template <class Abi> doublev<Abi> atan2(doublev<Abi> y, doublev<Abi> x);
327 template <class Abi> ldoublev<Abi> atan2(ldoublev<Abi> y, ldoublev<Abi> x);
328
329 template <class Abi> floatv<Abi> cos(floatv<Abi> x);
330 template <class Abi> doublev<Abi> cos(doublev<Abi> x);
331 template <class Abi> ldoublev<Abi> cos(ldoublev<Abi> x);
332
333 template <class Abi> floatv<Abi> sin(floatv<Abi> x);
334 template <class Abi> doublev<Abi> sin(doublev<Abi> x);
335 template <class Abi> ldoublev<Abi> sin(ldoublev<Abi> x);
336
337 template <class Abi> floatv<Abi> tan(floatv<Abi> x);
338 template <class Abi> doublev<Abi> tan(doublev<Abi> x);
339 template <class Abi> ldoublev<Abi> tan(ldoublev<Abi> x);
340
341 template <class Abi> floatv<Abi> acosh(floatv<Abi> x);
342 template <class Abi> doublev<Abi> acosh(doublev<Abi> x);
343 template <class Abi> ldoublev<Abi> acosh(ldoublev<Abi> x);
344
345 template <class Abi> floatv<Abi> asinh(floatv<Abi> x);
346 template <class Abi> doublev<Abi> asinh(doublev<Abi> x);
347 template <class Abi> ldoublev<Abi> asinh(ldoublev<Abi> x);
348
349 template <class Abi> floatv<Abi> atanh(floatv<Abi> x);
350 template <class Abi> doublev<Abi> atanh(doublev<Abi> x);
351 template <class Abi> ldoublev<Abi> atanh(ldoublev<Abi> x);
352
353 template <class Abi> floatv<Abi> cosh(floatv<Abi> x);
354 template <class Abi> doublev<Abi> cosh(doublev<Abi> x);
355 template <class Abi> ldoublev<Abi> cosh(ldoublev<Abi> x);
356
357 template <class Abi> floatv<Abi> sinh(floatv<Abi> x);
358 template <class Abi> doublev<Abi> sinh(doublev<Abi> x);
359 template <class Abi> ldoublev<Abi> sinh(ldoublev<Abi> x);
360
361 template <class Abi> floatv<Abi> tanh(floatv<Abi> x);
362 template <class Abi> doublev<Abi> tanh(doublev<Abi> x);
363 template <class Abi> ldoublev<Abi> tanh(ldoublev<Abi> x);
364
365 template <class Abi> floatv<Abi> exp(floatv<Abi> x);
366 template <class Abi> doublev<Abi> exp(doublev<Abi> x);
367 template <class Abi> ldoublev<Abi> exp(ldoublev<Abi> x);
368
369 template <class Abi> floatv<Abi> exp2(floatv<Abi> x);
370 template <class Abi> doublev<Abi> exp2(doublev<Abi> x);
371 template <class Abi> ldoublev<Abi> exp2(ldoublev<Abi> x);
372
373 template <class Abi> floatv<Abi> expm1(floatv<Abi> x);
374 template <class Abi> doublev<Abi> expm1(doublev<Abi> x);
375 template <class Abi> ldoublev<Abi> expm1(ldoublev<Abi> x);
376
377 template <class Abi> floatv<Abi> frexp(floatv<Abi> value, samesize<int, floatv<Abi>>* exp);
378 template <class Abi> doublev<Abi> frexp(doublev<Abi> value, samesize<int, doublev<Abi>>* exp);
379 template <class Abi> ldoublev<Abi> frexp(ldoublev<Abi> value, samesize<int, ldoublev<Abi>>* exp);
380
381 template <class Abi> samesize<int, floatv<Abi>> ilogb(floatv<Abi> x);
382 template <class Abi> samesize<int, doublev<Abi>> ilogb(doublev<Abi> x);
383 template <class Abi> samesize<int, ldoublev<Abi>> ilogb(ldoublev<Abi> x);
384
385 template <class Abi> floatv<Abi> ldexp(floatv<Abi> x, samesize<int, floatv<Abi>> exp);
386 template <class Abi> doublev<Abi> ldexp(doublev<Abi> x, samesize<int, doublev<Abi>> exp);
387 template <class Abi> ldoublev<Abi> ldexp(ldoublev<Abi> x, samesize<int, ldoublev<Abi>> exp);
388
389 template <class Abi> floatv<Abi> log(floatv<Abi> x);
390 template <class Abi> doublev<Abi> log(doublev<Abi> x);
391 template <class Abi> ldoublev<Abi> log(ldoublev<Abi> x);
392
393 template <class Abi> floatv<Abi> log10(floatv<Abi> x);
394 template <class Abi> doublev<Abi> log10(doublev<Abi> x);
395 template <class Abi> ldoublev<Abi> log10(ldoublev<Abi> x);
396
397 template <class Abi> floatv<Abi> log1p(floatv<Abi> x);
398 template <class Abi> doublev<Abi> log1p(doublev<Abi> x);
399 template <class Abi> ldoublev<Abi> log1p(ldoublev<Abi> x);
400
401 template <class Abi> floatv<Abi> log2(floatv<Abi> x);
402 template <class Abi> doublev<Abi> log2(doublev<Abi> x);
403 template <class Abi> ldoublev<Abi> log2(ldoublev<Abi> x);
404
405 template <class Abi> floatv<Abi> logb(floatv<Abi> x);
406 template <class Abi> doublev<Abi> logb(doublev<Abi> x);
407 template <class Abi> ldoublev<Abi> logb(ldoublev<Abi> x);
408
409 template <class Abi> floatv<Abi> modf(floatv<Abi> value, floatv<Abi>* iptr);
410 template <class Abi> doublev<Abi> modf(doublev<Abi> value, doublev<Abi>* iptr);
411 template <class Abi> ldoublev<Abi> modf(ldoublev<Abi> value, ldoublev<Abi>* iptr);
412
413 template <class Abi> floatv<Abi> scalbn(floatv<Abi> x, samesize<int, floatv<Abi>> n);
414 template <class Abi> doublev<Abi> scalbn(doublev<Abi> x, samesize<int, doublev<Abi>> n);
415 template <class Abi> ldoublev<Abi> scalbn(ldoublev<Abi> x, samesize<int, ldoublev<Abi>> n);
416 template <class Abi> floatv<Abi> scalbln(floatv<Abi> x, samesize<long int, floatv<Abi>> n);
417 template <class Abi> doublev<Abi> scalbln(doublev<Abi> x, samesize<long int, doublev<Abi>> n);
418 template <class Abi> ldoublev<Abi> scalbln(ldoublev<Abi> x, samesize<long int, ldoublev<Abi>> n);
419
420 template <class Abi> floatv<Abi> cbrt(floatv<Abi> x);
421 template <class Abi> doublev<Abi> cbrt(doublev<Abi> x);
422 template <class Abi> ldoublev<Abi> cbrt(ldoublev<Abi> x);
423
424 template <class Abi> scharv<Abi> abs(scharv<Abi> j);
425 template <class Abi> shortv<Abi> abs(shortv<Abi> j);
426 template <class Abi> intv<Abi> abs(intv<Abi> j);
427 template <class Abi> longv<Abi> abs(longv<Abi> j);
428 template <class Abi> llongv<Abi> abs(llongv<Abi> j);
429 template <class Abi> floatv<Abi> abs(floatv<Abi> j);
430 template <class Abi> doublev<Abi> abs(doublev<Abi> j);
431 template <class Abi> ldoublev<Abi> abs(ldoublev<Abi> j);
432
433 template <class Abi> floatv<Abi> hypot(floatv<Abi> x, floatv<Abi> y);
434 template <class Abi> doublev<Abi> hypot(doublev<Abi> x, doublev<Abi> y);
435 template <class Abi> ldoublev<Abi> hypot(doublev<Abi> x, doublev<Abi> y);
436 template <class Abi> floatv<Abi> hypot(floatv<Abi> x, floatv<Abi> y, floatv<Abi> z);
437 template <class Abi> doublev<Abi> hypot(doublev<Abi> x, doublev<Abi> y, doublev<Abi> z);
438 template <class Abi> ldoublev<Abi> hypot(ldoublev<Abi> x, ldoublev<Abi> y, ldoublev<Abi> z);
439
440 template <class Abi> floatv<Abi> pow(floatv<Abi> x, floatv<Abi> y);
441 template <class Abi> doublev<Abi> pow(doublev<Abi> x, doublev<Abi> y);
442 template <class Abi> ldoublev<Abi> pow(ldoublev<Abi> x, ldoublev<Abi> y);
443
444 template <class Abi> floatv<Abi> sqrt(floatv<Abi> x);
445 template <class Abi> doublev<Abi> sqrt(doublev<Abi> x);
446 template <class Abi> ldoublev<Abi> sqrt(ldoublev<Abi> x);
447
448 template <class Abi> floatv<Abi> erf(floatv<Abi> x);
449 template <class Abi> doublev<Abi> erf(doublev<Abi> x);
450 template <class Abi> ldoublev<Abi> erf(ldoublev<Abi> x);
451 template <class Abi> floatv<Abi> erfc(floatv<Abi> x);
452 template <class Abi> doublev<Abi> erfc(doublev<Abi> x);
453 template <class Abi> ldoublev<Abi> erfc(ldoublev<Abi> x);
454
455 template <class Abi> floatv<Abi> lgamma(floatv<Abi> x);
456 template <class Abi> doublev<Abi> lgamma(doublev<Abi> x);
457 template <class Abi> ldoublev<Abi> lgamma(ldoublev<Abi> x);
458
459 template <class Abi> floatv<Abi> tgamma(floatv<Abi> x);
460 template <class Abi> doublev<Abi> tgamma(doublev<Abi> x);
461 template <class Abi> ldoublev<Abi> tgamma(ldoublev<Abi> x);
462
463 template <class Abi> floatv<Abi> ceil(floatv<Abi> x);
464 template <class Abi> doublev<Abi> ceil(doublev<Abi> x);
465 template <class Abi> ldoublev<Abi> ceil(ldoublev<Abi> x);
466
467 template <class Abi> floatv<Abi> floor(floatv<Abi> x);
468 template <class Abi> doublev<Abi> floor(doublev<Abi> x);
469 template <class Abi> ldoublev<Abi> floor(ldoublev<Abi> x);
470
471 template <class Abi> floatv<Abi> nearbyint(floatv<Abi> x);
472 template <class Abi> doublev<Abi> nearbyint(doublev<Abi> x);
473 template <class Abi> ldoublev<Abi> nearbyint(ldoublev<Abi> x);
474
475 template <class Abi> floatv<Abi> rint(floatv<Abi> x);
476 template <class Abi> doublev<Abi> rint(doublev<Abi> x);
477 template <class Abi> ldoublev<Abi> rint(ldoublev<Abi> x);
478
479 template <class Abi> samesize<long int, floatv<Abi>> lrint(floatv<Abi> x);
480 template <class Abi> samesize<long int, doublev<Abi>> lrint(doublev<Abi> x);
481 template <class Abi> samesize<long int, ldoublev<Abi>> lrint(ldoublev<Abi> x);
482 template <class Abi> samesize<long long int, floatv<Abi>> llrint(floatv<Abi> x);
483 template <class Abi> samesize<long long int, doublev<Abi>> llrint(doublev<Abi> x);
484 template <class Abi> samesize<long long int, ldoublev<Abi>> llrint(ldoublev<Abi> x);
485
486 template <class Abi> floatv<Abi> round(floatv<Abi> x);
487 template <class Abi> doublev<Abi> round(doublev<Abi> x);
488 template <class Abi> ldoublev<Abi> round(ldoublev<Abi> x);
489 template <class Abi> samesize<long int, floatv<Abi>> lround(floatv<Abi> x);
490 template <class Abi> samesize<long int, doublev<Abi>> lround(doublev<Abi> x);
491 template <class Abi> samesize<long int, ldoublev<Abi>> lround(ldoublev<Abi> x);
492 template <class Abi> samesize<long long int, floatv<Abi>> llround(floatv<Abi> x);
493 template <class Abi> samesize<long long int, doublev<Abi>> llround(doublev<Abi> x);
494 template <class Abi> samesize<long long int, ldoublev<Abi>> llround(ldoublev<Abi> x);
495
496 template <class Abi> floatv<Abi> trunc(floatv<Abi> x);
497 template <class Abi> doublev<Abi> trunc(doublev<Abi> x);
498 template <class Abi> ldoublev<Abi> trunc(ldoublev<Abi> x);
499
500 template <class Abi> floatv<Abi> fmod(floatv<Abi> x, floatv<Abi> y);
501 template <class Abi> doublev<Abi> fmod(doublev<Abi> x, doublev<Abi> y);
502 template <class Abi> ldoublev<Abi> fmod(ldoublev<Abi> x, ldoublev<Abi> y);
503
504 template <class Abi> floatv<Abi> remainder(floatv<Abi> x, floatv<Abi> y);
505 template <class Abi> doublev<Abi> remainder(doublev<Abi> x, doublev<Abi> y);
506 template <class Abi> ldoublev<Abi> remainder(ldoublev<Abi> x, ldoublev<Abi> y);
507
508 template <class Abi> floatv<Abi> remquo(floatv<Abi> x, floatv<Abi> y, samesize<int, floatv<Abi>>* quo);
509 template <class Abi> doublev<Abi> remquo(doublev<Abi> x, doublev<Abi> y, samesize<int, doublev<Abi>>* quo);
510 template <class Abi> ldoublev<Abi> remquo(ldoublev<Abi> x, ldoublev<Abi> y, samesize<int, ldoublev<Abi>>* quo);
511
512 template <class Abi> floatv<Abi> copysign(floatv<Abi> x, floatv<Abi> y);
513 template <class Abi> doublev<Abi> copysign(doublev<Abi> x, doublev<Abi> y);
514 template <class Abi> ldoublev<Abi> copysign(ldoublev<Abi> x, ldoublev<Abi> y);
515
516 template <class Abi> doublev<Abi> nan(const char* tagp);
517 template <class Abi> floatv<Abi> nanf(const char* tagp);
518 template <class Abi> ldoublev<Abi> nanl(const char* tagp);
519
520 template <class Abi> floatv<Abi> nextafter(floatv<Abi> x, floatv<Abi> y);
521 template <class Abi> doublev<Abi> nextafter(doublev<Abi> x, doublev<Abi> y);
522 template <class Abi> ldoublev<Abi> nextafter(ldoublev<Abi> x, ldoublev<Abi> y);
523
524 template <class Abi> floatv<Abi> nexttoward(floatv<Abi> x, ldoublev<Abi> y);
525 template <class Abi> doublev<Abi> nexttoward(doublev<Abi> x, ldoublev<Abi> y);
526 template <class Abi> ldoublev<Abi> nexttoward(ldoublev<Abi> x, ldoublev<Abi> y);
527
528 template <class Abi> floatv<Abi> fdim(floatv<Abi> x, floatv<Abi> y);
529 template <class Abi> doublev<Abi> fdim(doublev<Abi> x, doublev<Abi> y);
530 template <class Abi> ldoublev<Abi> fdim(ldoublev<Abi> x, ldoublev<Abi> y);
531
532 template <class Abi> floatv<Abi> fmax(floatv<Abi> x, floatv<Abi> y);
533 template <class Abi> doublev<Abi> fmax(doublev<Abi> x, doublev<Abi> y);
534 template <class Abi> ldoublev<Abi> fmax(ldoublev<Abi> x, ldoublev<Abi> y);
535
536 template <class Abi> floatv<Abi> fmin(floatv<Abi> x, floatv<Abi> y);
537 template <class Abi> doublev<Abi> fmin(doublev<Abi> x, doublev<Abi> y);
538 template <class Abi> ldoublev<Abi> fmin(ldoublev<Abi> x, ldoublev<Abi> y);
539
540 template <class Abi> floatv<Abi> fma(floatv<Abi> x, floatv<Abi> y, floatv<Abi> z);
541 template <class Abi> doublev<Abi> fma(doublev<Abi> x, doublev<Abi> y, doublev<Abi> z);
542 template <class Abi> ldoublev<Abi> fma(ldoublev<Abi> x, ldoublev<Abi> y, ldoublev<Abi> z);
543
544 template <class Abi> samesize<int, floatv<Abi>> fpclassify(floatv<Abi> x);
545 template <class Abi> samesize<int, doublev<Abi>> fpclassify(doublev<Abi> x);
546 template <class Abi> samesize<int, ldoublev<Abi>> fpclassify(ldoublev<Abi> x);
547
548 template <class Abi> simd_mask<float, Abi> isfinite(floatv<Abi> x);
549 template <class Abi> simd_mask<double, Abi> isfinite(doublev<Abi> x);
550 template <class Abi> simd_mask<long double, Abi> isfinite(ldoublev<Abi> x);
551
552 template <class Abi> simd_mask<float, Abi> isinf(floatv<Abi> x);
553 template <class Abi> simd_mask<double, Abi> isinf(doublev<Abi> x);
554 template <class Abi> simd_mask<long double, Abi> isinf(ldoublev<Abi> x);
555
556 template <class Abi> simd_mask<float, Abi> isnan(floatv<Abi> x);
557 template <class Abi> simd_mask<double, Abi> isnan(doublev<Abi> x);
558 template <class Abi> simd_mask<long double, Abi> isnan(ldoublev<Abi> x);
559
560 template <class Abi> simd_mask<float, Abi> isnormal(floatv<Abi> x);
561 template <class Abi> simd_mask<double, Abi> isnormal(doublev<Abi> x);
562 template <class Abi> simd_mask<long double, Abi> isnormal(ldoublev<Abi> x);
563
564 template <class Abi> simd_mask<float, Abi> signbit(floatv<Abi> x);
565 template <class Abi> simd_mask<double, Abi> signbit(doublev<Abi> x);
566 template <class Abi> simd_mask<long double, Abi> signbit(ldoublev<Abi> x);
567
568 template <class Abi> simd_mask<float, Abi> isgreater(floatv<Abi> x, floatv<Abi> y);
569 template <class Abi> simd_mask<double, Abi> isgreater(doublev<Abi> x, doublev<Abi> y);
570 template <class Abi> simd_mask<long double, Abi> isgreater(ldoublev<Abi> x, ldoublev<Abi> y);
571
572 template <class Abi> simd_mask<float, Abi> isgreaterequal(floatv<Abi> x, floatv<Abi> y);
573 template <class Abi> simd_mask<double, Abi> isgreaterequal(doublev<Abi> x, doublev<Abi> y);
574 template <class Abi> simd_mask<long double, Abi> isgreaterequal(ldoublev<Abi> x, ldoublev<Abi> y);
575
576 template <class Abi> simd_mask<float, Abi> isless(floatv<Abi> x, floatv<Abi> y);
577 template <class Abi> simd_mask<double, Abi> isless(doublev<Abi> x, doublev<Abi> y);
578 template <class Abi> simd_mask<long double, Abi> isless(ldoublev<Abi> x, ldoublev<Abi> y);
579
580 template <class Abi> simd_mask<float, Abi> islessequal(floatv<Abi> x, floatv<Abi> y);
581 template <class Abi> simd_mask<double, Abi> islessequal(doublev<Abi> x, doublev<Abi> y);
582 template <class Abi> simd_mask<long double, Abi> islessequal(ldoublev<Abi> x, ldoublev<Abi> y);
583
584 template <class Abi> simd_mask<float, Abi> islessgreater(floatv<Abi> x, floatv<Abi> y);
585 template <class Abi> simd_mask<double, Abi> islessgreater(doublev<Abi> x, doublev<Abi> y);
586 template <class Abi> simd_mask<long double, Abi> islessgreater(ldoublev<Abi> x, ldoublev<Abi> y);
587
588 template <class Abi> simd_mask<float, Abi> isunordered(floatv<Abi> x, floatv<Abi> y);
589 template <class Abi> simd_mask<double, Abi> isunordered(doublev<Abi> x, doublev<Abi> y);
590 template <class Abi> simd_mask<long double, Abi> isunordered(ldoublev<Abi> x, ldoublev<Abi> y);
591
592 template <class V> struct simd_div_t { V quot, rem; };
593 template <class Abi> simd_div_t<scharv<Abi>> div(scharv<Abi> numer, scharv<Abi> denom);
594 template <class Abi> simd_div_t<shortv<Abi>> div(shortv<Abi> numer, shortv<Abi> denom);
595 template <class Abi> simd_div_t<intv<Abi>> div(intv<Abi> numer, intv<Abi> denom);
596 template <class Abi> simd_div_t<longv<Abi>> div(longv<Abi> numer, longv<Abi> denom);
597 template <class Abi> simd_div_t<llongv<Abi>> div(llongv<Abi> numer, llongv<Abi> denom);
598
599 // [simd.mask.class]
600 template <class T, class Abi>
601 class simd_mask {
602 public:
603   using value_type = bool;
604   using reference = see below;
605   using simd_type = simd<T, Abi>;
606   using abi_type = Abi;
607   static constexpr size_t size() noexcept;
608   simd_mask() = default;
609
610   // broadcast constructor
611   explicit simd_mask(value_type) noexcept;
612
613   // implicit type conversion constructor
614   template <class U> simd_mask(const simd_mask<U, simd_abi::fixed_size<size()>>&) noexcept;
615
616   // load constructor
617   template <class Flags> simd_mask(const value_type* mem, Flags);
618
619   // loads [simd.mask.copy]
620   template <class Flags> void copy_from(const value_type* mem, Flags);
621   template <class Flags> void copy_to(value_type* mem, Flags) const;
622
623   // scalar access [simd.mask.subscr]
624   reference operator[](size_t);
625   value_type operator[](size_t) const;
626
627   // unary operators [simd.mask.unary]
628   simd_mask operator!() const noexcept;
629
630   // simd_mask binary operators [simd.mask.binary]
631   friend simd_mask operator&&(const simd_mask&, const simd_mask&) noexcept;
632   friend simd_mask operator||(const simd_mask&, const simd_mask&) noexcept;
633   friend simd_mask operator& (const simd_mask&, const simd_mask&) noexcept;
634   friend simd_mask operator| (const simd_mask&, const simd_mask&) noexcept;
635   friend simd_mask operator^ (const simd_mask&, const simd_mask&) noexcept;
636
637   // simd_mask compound assignment [simd.mask.cassign]
638   friend simd_mask& operator&=(simd_mask&, const simd_mask&) noexcept;
639   friend simd_mask& operator|=(simd_mask&, const simd_mask&) noexcept;
640   friend simd_mask& operator^=(simd_mask&, const simd_mask&) noexcept;
641
642   // simd_mask compares [simd.mask.comparison]
643   friend simd_mask operator==(const simd_mask&, const simd_mask&) noexcept;
644   friend simd_mask operator!=(const simd_mask&, const simd_mask&) noexcept;
645 };
646
647 } // parallelism_v2
648 } // std::experimental
649
650 */
651
652 #include <experimental/__config>
653 #include <algorithm>
654 #include <array>
655 #include <cstddef>
656 #include <functional>
657
658 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
659 #pragma GCC system_header
660 #endif
661
662 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_SIMD
663
664 #if _LIBCPP_STD_VER >= 17
665
666 enum class _StorageKind {
667   _Scalar,
668   _Array,
669   _VecExt,
670 };
671
672 template <_StorageKind __kind, int _Np>
673 struct __simd_abi {};
674
675 template <class _Tp, class _Abi>
676 class __simd_storage {};
677
678 template <class _Tp, int __num_element>
679 class __simd_storage<_Tp, __simd_abi<_StorageKind::_Array, __num_element>> {
680   std::array<_Tp, __num_element> __storage_;
681
682   template <class, class>
683   friend struct simd;
684
685   template <class, class>
686   friend struct simd_mask;
687
688 public:
689   _Tp __get(size_t __index) const noexcept { return __storage_[__index]; };
690   void __set(size_t __index, _Tp __val) noexcept {
691     __storage_[__index] = __val;
692   }
693 };
694
695 template <class _Tp>
696 class __simd_storage<_Tp, __simd_abi<_StorageKind::_Scalar, 1>> {
697   _Tp __storage_;
698
699   template <class, class>
700   friend struct simd;
701
702   template <class, class>
703   friend struct simd_mask;
704
705 public:
706   _Tp __get(size_t __index) const noexcept { return (&__storage_)[__index]; };
707   void __set(size_t __index, _Tp __val) noexcept {
708     (&__storage_)[__index] = __val;
709   }
710 };
711
712 #ifndef _LIBCPP_HAS_NO_VECTOR_EXTENSION
713
714 constexpr size_t __floor_pow_of_2(size_t __val) {
715   return ((__val - 1) & __val) == 0 ? __val
716                                     : __floor_pow_of_2((__val - 1) & __val);
717 }
718
719 constexpr size_t __ceil_pow_of_2(size_t __val) {
720   return __val == 1 ? 1 : __floor_pow_of_2(__val - 1) << 1;
721 }
722
723 template <class _Tp, size_t __bytes>
724 struct __vec_ext_traits {
725 #if !defined(_LIBCPP_COMPILER_CLANG)
726   typedef _Tp type __attribute__((vector_size(__ceil_pow_of_2(__bytes))));
727 #endif
728 };
729
730 #if defined(_LIBCPP_COMPILER_CLANG)
731 #define _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, _NUM_ELEMENT)                        \
732   template <>                                                                  \
733   struct __vec_ext_traits<_TYPE, sizeof(_TYPE) * _NUM_ELEMENT> {               \
734     using type =                                                               \
735         _TYPE __attribute__((vector_size(sizeof(_TYPE) * _NUM_ELEMENT)));      \
736   }
737
738 #define _LIBCPP_SPECIALIZE_VEC_EXT_32(_TYPE)                                   \
739   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 1);                                        \
740   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 2);                                        \
741   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 3);                                        \
742   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 4);                                        \
743   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 5);                                        \
744   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 6);                                        \
745   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 7);                                        \
746   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 8);                                        \
747   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 9);                                        \
748   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 10);                                       \
749   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 11);                                       \
750   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 12);                                       \
751   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 13);                                       \
752   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 14);                                       \
753   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 15);                                       \
754   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 16);                                       \
755   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 17);                                       \
756   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 18);                                       \
757   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 19);                                       \
758   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 20);                                       \
759   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 21);                                       \
760   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 22);                                       \
761   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 23);                                       \
762   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 24);                                       \
763   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 25);                                       \
764   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 26);                                       \
765   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 27);                                       \
766   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 28);                                       \
767   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 29);                                       \
768   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 30);                                       \
769   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 31);                                       \
770   _LIBCPP_SPECIALIZE_VEC_EXT(_TYPE, 32);
771
772 _LIBCPP_SPECIALIZE_VEC_EXT_32(char);
773 _LIBCPP_SPECIALIZE_VEC_EXT_32(char16_t);
774 _LIBCPP_SPECIALIZE_VEC_EXT_32(char32_t);
775 _LIBCPP_SPECIALIZE_VEC_EXT_32(wchar_t);
776 _LIBCPP_SPECIALIZE_VEC_EXT_32(signed char);
777 _LIBCPP_SPECIALIZE_VEC_EXT_32(signed short);
778 _LIBCPP_SPECIALIZE_VEC_EXT_32(signed int);
779 _LIBCPP_SPECIALIZE_VEC_EXT_32(signed long);
780 _LIBCPP_SPECIALIZE_VEC_EXT_32(signed long long);
781 _LIBCPP_SPECIALIZE_VEC_EXT_32(unsigned char);
782 _LIBCPP_SPECIALIZE_VEC_EXT_32(unsigned short);
783 _LIBCPP_SPECIALIZE_VEC_EXT_32(unsigned int);
784 _LIBCPP_SPECIALIZE_VEC_EXT_32(unsigned long);
785 _LIBCPP_SPECIALIZE_VEC_EXT_32(unsigned long long);
786 _LIBCPP_SPECIALIZE_VEC_EXT_32(float);
787 _LIBCPP_SPECIALIZE_VEC_EXT_32(double);
788 _LIBCPP_SPECIALIZE_VEC_EXT_32(long double);
789
790 #undef _LIBCPP_SPECIALIZE_VEC_EXT_32
791 #undef _LIBCPP_SPECIALIZE_VEC_EXT
792 #endif
793
794 template <class _Tp, int __num_element>
795 class __simd_storage<_Tp, __simd_abi<_StorageKind::_VecExt, __num_element>> {
796   using _StorageType =
797       typename __vec_ext_traits<_Tp, sizeof(_Tp) * __num_element>::type;
798
799   _StorageType __storage_;
800
801   template <class, class>
802   friend struct simd;
803
804   template <class, class>
805   friend struct simd_mask;
806
807 public:
808   _Tp __get(size_t __index) const noexcept { return __storage_[__index]; };
809   void __set(size_t __index, _Tp __val) noexcept {
810     __storage_[__index] = __val;
811   }
812 };
813
814 #endif // _LIBCPP_HAS_NO_VECTOR_EXTENSION
815
816 template <class _Vp, class _Tp, class _Abi>
817 class __simd_reference {
818   static_assert(std::is_same<_Vp, _Tp>::value, "");
819
820   template <class, class>
821   friend struct simd;
822
823   template <class, class>
824   friend struct simd_mask;
825
826   __simd_storage<_Tp, _Abi>* __ptr_;
827   size_t __index_;
828
829   __simd_reference(__simd_storage<_Tp, _Abi>* __ptr, size_t __index)
830       : __ptr_(__ptr), __index_(__index) {}
831
832   __simd_reference(const __simd_reference&) = default;
833
834 public:
835   __simd_reference() = delete;
836   __simd_reference& operator=(const __simd_reference&) = delete;
837
838   operator _Vp() const { return __ptr_->__get(__index_); }
839
840   __simd_reference operator=(_Vp __value) && {
841     __ptr_->__set(__index_, __value);
842     return *this;
843   }
844
845   __simd_reference operator++() && {
846     return std::move(*this) = __ptr_->__get(__index_) + 1;
847   }
848
849   _Vp operator++(int) && {
850     auto __val = __ptr_->__get(__index_);
851     __ptr_->__set(__index_, __val + 1);
852     return __val;
853   }
854
855   __simd_reference operator--() && {
856     return std::move(*this) = __ptr_->__get(__index_) - 1;
857   }
858
859   _Vp operator--(int) && {
860     auto __val = __ptr_->__get(__index_);
861     __ptr_->__set(__index_, __val - 1);
862     return __val;
863   }
864
865   __simd_reference operator+=(_Vp __value) && {
866     return std::move(*this) = __ptr_->__get(__index_) + __value;
867   }
868
869   __simd_reference operator-=(_Vp __value) && {
870     return std::move(*this) = __ptr_->__get(__index_) - __value;
871   }
872
873   __simd_reference operator*=(_Vp __value) && {
874     return std::move(*this) = __ptr_->__get(__index_) * __value;
875   }
876
877   __simd_reference operator/=(_Vp __value) && {
878     return std::move(*this) = __ptr_->__get(__index_) / __value;
879   }
880
881   __simd_reference operator%=(_Vp __value) && {
882     return std::move(*this) = __ptr_->__get(__index_) % __value;
883   }
884
885   __simd_reference operator>>=(_Vp __value) && {
886     return std::move(*this) = __ptr_->__get(__index_) >> __value;
887   }
888
889   __simd_reference operator<<=(_Vp __value) && {
890     return std::move(*this) = __ptr_->__get(__index_) << __value;
891   }
892
893   __simd_reference operator&=(_Vp __value) && {
894     return std::move(*this) = __ptr_->__get(__index_) & __value;
895   }
896
897   __simd_reference operator|=(_Vp __value) && {
898     return std::move(*this) = __ptr_->__get(__index_) | __value;
899   }
900
901   __simd_reference operator^=(_Vp __value) && {
902     return std::move(*this) = __ptr_->__get(__index_) ^ __value;
903   }
904 };
905
906 template <class _To, class _From>
907 constexpr decltype(_To{std::declval<_From>()}, true)
908 __is_non_narrowing_convertible_impl(_From) {
909   return true;
910 }
911
912 template <class _To>
913 constexpr bool __is_non_narrowing_convertible_impl(...) {
914   return false;
915 }
916
917 template <class _From, class _To>
918 constexpr typename std::enable_if<std::is_arithmetic<_To>::value &&
919                                       std::is_arithmetic<_From>::value,
920                                   bool>::type
921 __is_non_narrowing_arithmetic_convertible() {
922   return __is_non_narrowing_convertible_impl<_To>(_From{});
923 }
924
925 template <class _From, class _To>
926 constexpr typename std::enable_if<!(std::is_arithmetic<_To>::value &&
927                                     std::is_arithmetic<_From>::value),
928                                   bool>::type
929 __is_non_narrowing_arithmetic_convertible() {
930   return false;
931 }
932
933 template <class _Tp>
934 constexpr _Tp __variadic_sum() {
935   return _Tp{};
936 }
937
938 template <class _Tp, class _Up, class... _Args>
939 constexpr _Tp __variadic_sum(_Up __first, _Args... __rest) {
940   return static_cast<_Tp>(__first) + __variadic_sum<_Tp>(__rest...);
941 }
942
943 template <class _Tp>
944 struct __nodeduce {
945   using type = _Tp;
946 };
947
948 template <class _Tp>
949 constexpr bool __vectorizable() {
950   return std::is_arithmetic<_Tp>::value && !std::is_const<_Tp>::value &&
951          !std::is_volatile<_Tp>::value && !std::is_same<_Tp, bool>::value;
952 }
953
954 _LIBCPP_END_NAMESPACE_EXPERIMENTAL_SIMD
955 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_SIMD_ABI
956
957 using scalar = __simd_abi<_StorageKind::_Scalar, 1>;
958
959 template <int _Np>
960 using fixed_size = __simd_abi<_StorageKind::_Array, _Np>;
961
962 template <class _Tp>
963 _LIBCPP_INLINE_VAR constexpr size_t max_fixed_size = 32;
964
965 template <class _Tp>
966 using compatible = fixed_size<16 / sizeof(_Tp)>;
967
968 #ifndef _LIBCPP_HAS_NO_VECTOR_EXTENSION
969 template <class _Tp>
970 using native = __simd_abi<_StorageKind::_VecExt,
971                           _LIBCPP_NATIVE_SIMD_WIDTH_IN_BYTES / sizeof(_Tp)>;
972 #else
973 template <class _Tp>
974 using native =
975     fixed_size<_Tp, _LIBCPP_NATIVE_SIMD_WIDTH_IN_BYTES / sizeof(_Tp)>;
976 #endif // _LIBCPP_HAS_NO_VECTOR_EXTENSION
977
978 _LIBCPP_END_NAMESPACE_EXPERIMENTAL_SIMD_ABI
979 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_SIMD
980
981 template <class _Tp, class _Abi = simd_abi::compatible<_Tp>>
982 class simd;
983 template <class _Tp, class _Abi = simd_abi::compatible<_Tp>>
984 class simd_mask;
985
986 struct element_aligned_tag {};
987 struct vector_aligned_tag {};
988 template <size_t>
989 struct overaligned_tag {};
990 _LIBCPP_INLINE_VAR constexpr element_aligned_tag element_aligned{};
991 _LIBCPP_INLINE_VAR constexpr vector_aligned_tag vector_aligned{};
992 template <size_t _Np>
993 _LIBCPP_INLINE_VAR constexpr overaligned_tag<_Np> overaligned{};
994
995 // traits [simd.traits]
996 template <class _Tp>
997 struct is_abi_tag : std::integral_constant<bool, false> {};
998
999 template <_StorageKind __kind, int _Np>
1000 struct is_abi_tag<__simd_abi<__kind, _Np>>
1001     : std::integral_constant<bool, true> {};
1002
1003 template <class _Tp>
1004 struct is_simd : std::integral_constant<bool, false> {};
1005
1006 template <class _Tp, class _Abi>
1007 struct is_simd<simd<_Tp, _Abi>> : std::integral_constant<bool, true> {};
1008
1009 template <class _Tp>
1010 struct is_simd_mask : std::integral_constant<bool, false> {};
1011
1012 template <class _Tp, class _Abi>
1013 struct is_simd_mask<simd_mask<_Tp, _Abi>> : std::integral_constant<bool, true> {
1014 };
1015
1016 template <class _Tp>
1017 struct is_simd_flag_type : std::integral_constant<bool, false> {};
1018
1019 template <>
1020 struct is_simd_flag_type<element_aligned_tag>
1021     : std::integral_constant<bool, true> {};
1022
1023 template <>
1024 struct is_simd_flag_type<vector_aligned_tag>
1025     : std::integral_constant<bool, true> {};
1026
1027 template <size_t _Align>
1028 struct is_simd_flag_type<overaligned_tag<_Align>>
1029     : std::integral_constant<bool, true> {};
1030
1031 template <class _Tp>
1032 _LIBCPP_INLINE_VAR constexpr bool is_abi_tag_v = is_abi_tag<_Tp>::value;
1033 template <class _Tp>
1034 _LIBCPP_INLINE_VAR constexpr bool is_simd_v = is_simd<_Tp>::value;
1035 template <class _Tp>
1036 _LIBCPP_INLINE_VAR constexpr bool is_simd_mask_v = is_simd_mask<_Tp>::value;
1037 template <class _Tp>
1038 _LIBCPP_INLINE_VAR constexpr bool is_simd_flag_type_v =
1039     is_simd_flag_type<_Tp>::value;
1040 template <class _Tp, size_t _Np>
1041 struct abi_for_size {
1042   using type = simd_abi::fixed_size<_Np>;
1043 };
1044 template <class _Tp, size_t _Np>
1045 using abi_for_size_t = typename abi_for_size<_Tp, _Np>::type;
1046
1047 template <class _Tp, class _Abi = simd_abi::compatible<_Tp>>
1048 struct simd_size;
1049
1050 template <class _Tp, _StorageKind __kind, int _Np>
1051 struct simd_size<_Tp, __simd_abi<__kind, _Np>>
1052     : std::integral_constant<size_t, _Np> {
1053   static_assert(
1054       std::is_arithmetic<_Tp>::value &&
1055           !std::is_same<typename std::remove_const<_Tp>::type, bool>::value,
1056       "Element type should be vectorizable");
1057 };
1058
1059 // TODO: implement it.
1060 template <class _Tp, class _Up = typename _Tp::value_type>
1061 struct memory_alignment;
1062
1063 template <class _Tp, class _Abi = simd_abi::compatible<_Tp>>
1064 _LIBCPP_INLINE_VAR constexpr size_t simd_size_v = simd_size<_Tp, _Abi>::value;
1065
1066 template <class _Tp, class _Up = typename _Tp::value_type>
1067 _LIBCPP_INLINE_VAR constexpr size_t memory_alignment_v =
1068     memory_alignment<_Tp, _Up>::value;
1069
1070 // class template simd [simd.class]
1071 template <class _Tp>
1072 using native_simd = simd<_Tp, simd_abi::native<_Tp>>;
1073 template <class _Tp, int _Np>
1074 using fixed_size_simd = simd<_Tp, simd_abi::fixed_size<_Np>>;
1075
1076 // class template simd_mask [simd.mask.class]
1077 template <class _Tp>
1078 using native_simd_mask = simd_mask<_Tp, simd_abi::native<_Tp>>;
1079
1080 template <class _Tp, int _Np>
1081 using fixed_size_simd_mask = simd_mask<_Tp, simd_abi::fixed_size<_Np>>;
1082
1083 // casts [simd.casts]
1084 template <class _Tp>
1085 struct __static_simd_cast_traits {
1086   template <class _Up, class _Abi>
1087   static simd<_Tp, _Abi> __apply(const simd<_Up, _Abi>& __v);
1088 };
1089
1090 template <class _Tp, class _NewAbi>
1091 struct __static_simd_cast_traits<simd<_Tp, _NewAbi>> {
1092   template <class _Up, class _Abi>
1093   static typename std::enable_if<simd<_Up, _Abi>::size() ==
1094                                      simd<_Tp, _NewAbi>::size(),
1095                                  simd<_Tp, _NewAbi>>::type
1096   __apply(const simd<_Up, _Abi>& __v);
1097 };
1098
1099 template <class _Tp>
1100 struct __simd_cast_traits {
1101   template <class _Up, class _Abi>
1102   static typename std::enable_if<
1103       __is_non_narrowing_arithmetic_convertible<_Up, _Tp>(),
1104       simd<_Tp, _Abi>>::type
1105   __apply(const simd<_Up, _Abi>& __v);
1106 };
1107
1108 template <class _Tp, class _NewAbi>
1109 struct __simd_cast_traits<simd<_Tp, _NewAbi>> {
1110   template <class _Up, class _Abi>
1111   static typename std::enable_if<
1112       __is_non_narrowing_arithmetic_convertible<_Up, _Tp>() &&
1113           simd<_Up, _Abi>::size() == simd<_Tp, _NewAbi>::size(),
1114       simd<_Tp, _NewAbi>>::type
1115   __apply(const simd<_Up, _Abi>& __v);
1116 };
1117
1118 template <class _Tp, class _Up, class _Abi>
1119 auto simd_cast(const simd<_Up, _Abi>& __v)
1120     -> decltype(__simd_cast_traits<_Tp>::__apply(__v)) {
1121   return __simd_cast_traits<_Tp>::__apply(__v);
1122 }
1123
1124 template <class _Tp, class _Up, class _Abi>
1125 auto static_simd_cast(const simd<_Up, _Abi>& __v)
1126     -> decltype(__static_simd_cast_traits<_Tp>::__apply(__v)) {
1127   return __static_simd_cast_traits<_Tp>::__apply(__v);
1128 }
1129
1130 template <class _Tp, class _Abi>
1131 fixed_size_simd<_Tp, simd_size<_Tp, _Abi>::value>
1132 to_fixed_size(const simd<_Tp, _Abi>&) noexcept;
1133
1134 template <class _Tp, class _Abi>
1135 fixed_size_simd_mask<_Tp, simd_size<_Tp, _Abi>::value>
1136 to_fixed_size(const simd_mask<_Tp, _Abi>&) noexcept;
1137
1138 template <class _Tp, size_t _Np>
1139 native_simd<_Tp> to_native(const fixed_size_simd<_Tp, _Np>&) noexcept;
1140
1141 template <class _Tp, size_t _Np>
1142 native_simd_mask<_Tp> to_native(const fixed_size_simd_mask<_Tp, _Np>&) noexcept;
1143
1144 template <class _Tp, size_t _Np>
1145 simd<_Tp> to_compatible(const fixed_size_simd<_Tp, _Np>&) noexcept;
1146
1147 template <class _Tp, size_t _Np>
1148 simd_mask<_Tp> to_compatible(const fixed_size_simd_mask<_Tp, _Np>&) noexcept;
1149
1150 template <size_t... __sizes, class _Tp, class _Abi>
1151 tuple<simd<_Tp, abi_for_size_t<_Tp, __sizes>>...> split(const simd<_Tp, _Abi>&);
1152
1153 template <size_t... __sizes, class _Tp, class _Abi>
1154 tuple<simd_mask<_Tp, abi_for_size_t<_Tp, __sizes>>...>
1155 split(const simd_mask<_Tp, _Abi>&);
1156
1157 template <class _SimdType, class _Abi>
1158 array<_SimdType, simd_size<typename _SimdType::value_type, _Abi>::value /
1159                      _SimdType::size()>
1160 split(const simd<typename _SimdType::value_type, _Abi>&);
1161
1162 template <class _SimdType, class _Abi>
1163 array<_SimdType, simd_size<typename _SimdType::value_type, _Abi>::value /
1164                      _SimdType::size()>
1165 split(const simd_mask<typename _SimdType::value_type, _Abi>&);
1166
1167 template <class _Tp, class... _Abis>
1168 simd<_Tp, abi_for_size_t<_Tp, __variadic_sum(simd_size<_Tp, _Abis>::value...)>>
1169 concat(const simd<_Tp, _Abis>&...);
1170
1171 template <class _Tp, class... _Abis>
1172 simd_mask<_Tp,
1173           abi_for_size_t<_Tp, __variadic_sum(simd_size<_Tp, _Abis>::value...)>>
1174 concat(const simd_mask<_Tp, _Abis>&...);
1175
1176 // reductions [simd.mask.reductions]
1177 template <class _Tp, class _Abi>
1178 bool all_of(const simd_mask<_Tp, _Abi>&) noexcept;
1179 template <class _Tp, class _Abi>
1180 bool any_of(const simd_mask<_Tp, _Abi>&) noexcept;
1181 template <class _Tp, class _Abi>
1182 bool none_of(const simd_mask<_Tp, _Abi>&) noexcept;
1183 template <class _Tp, class _Abi>
1184 bool some_of(const simd_mask<_Tp, _Abi>&) noexcept;
1185 template <class _Tp, class _Abi>
1186 int popcount(const simd_mask<_Tp, _Abi>&) noexcept;
1187 template <class _Tp, class _Abi>
1188 int find_first_set(const simd_mask<_Tp, _Abi>&);
1189 template <class _Tp, class _Abi>
1190 int find_last_set(const simd_mask<_Tp, _Abi>&);
1191 bool all_of(bool) noexcept;
1192 bool any_of(bool) noexcept;
1193 bool none_of(bool) noexcept;
1194 bool some_of(bool) noexcept;
1195 int popcount(bool) noexcept;
1196 int find_first_set(bool) noexcept;
1197 int find_last_set(bool) noexcept;
1198
1199 // masked assignment [simd.whereexpr]
1200 template <class _MaskType, class _Tp>
1201 class const_where_expression;
1202 template <class _MaskType, class _Tp>
1203 class where_expression;
1204
1205 // masked assignment [simd.mask.where]
1206 template <class _Tp, class _Abi>
1207 where_expression<simd_mask<_Tp, _Abi>, simd<_Tp, _Abi>>
1208 where(const typename simd<_Tp, _Abi>::mask_type&, simd<_Tp, _Abi>&) noexcept;
1209
1210 template <class _Tp, class _Abi>
1211 const_where_expression<simd_mask<_Tp, _Abi>, const simd<_Tp, _Abi>>
1212 where(const typename simd<_Tp, _Abi>::mask_type&,
1213       const simd<_Tp, _Abi>&) noexcept;
1214
1215 template <class _Tp, class _Abi>
1216 where_expression<simd_mask<_Tp, _Abi>, simd_mask<_Tp, _Abi>>
1217 where(const typename __nodeduce<simd_mask<_Tp, _Abi>>::type&,
1218       simd_mask<_Tp, _Abi>&) noexcept;
1219
1220 template <class _Tp, class _Abi>
1221 const_where_expression<simd_mask<_Tp, _Abi>, const simd_mask<_Tp, _Abi>>
1222 where(const typename __nodeduce<simd_mask<_Tp, _Abi>>::type&,
1223       const simd_mask<_Tp, _Abi>&) noexcept;
1224
1225 template <class _Tp>
1226 where_expression<bool, _Tp> where(bool, _Tp&) noexcept;
1227
1228 template <class _Tp>
1229 const_where_expression<bool, const _Tp> where(bool, const _Tp&) noexcept;
1230
1231 // reductions [simd.reductions]
1232 template <class _Tp, class _Abi, class _BinaryOp = std::plus<_Tp>>
1233 _Tp reduce(const simd<_Tp, _Abi>&, _BinaryOp = _BinaryOp());
1234
1235 template <class _MaskType, class _SimdType, class _BinaryOp>
1236 typename _SimdType::value_type
1237 reduce(const const_where_expression<_MaskType, _SimdType>&,
1238        typename _SimdType::value_type neutral_element, _BinaryOp binary_op);
1239
1240 template <class _MaskType, class _SimdType>
1241 typename _SimdType::value_type
1242 reduce(const const_where_expression<_MaskType, _SimdType>&,
1243        plus<typename _SimdType::value_type> binary_op = {});
1244
1245 template <class _MaskType, class _SimdType>
1246 typename _SimdType::value_type
1247 reduce(const const_where_expression<_MaskType, _SimdType>&,
1248        multiplies<typename _SimdType::value_type> binary_op);
1249
1250 template <class _MaskType, class _SimdType>
1251 typename _SimdType::value_type
1252 reduce(const const_where_expression<_MaskType, _SimdType>&,
1253        bit_and<typename _SimdType::value_type> binary_op);
1254
1255 template <class _MaskType, class _SimdType>
1256 typename _SimdType::value_type
1257 reduce(const const_where_expression<_MaskType, _SimdType>&,
1258        bit_or<typename _SimdType::value_type> binary_op);
1259
1260 template <class _MaskType, class _SimdType>
1261 typename _SimdType::value_type
1262 reduce(const const_where_expression<_MaskType, _SimdType>&,
1263        bit_xor<typename _SimdType::value_type> binary_op);
1264
1265 template <class _Tp, class _Abi>
1266 _Tp hmin(const simd<_Tp, _Abi>&);
1267 template <class _MaskType, class _SimdType>
1268 typename _SimdType::value_type
1269 hmin(const const_where_expression<_MaskType, _SimdType>&);
1270 template <class _Tp, class _Abi>
1271 _Tp hmax(const simd<_Tp, _Abi>&);
1272 template <class _MaskType, class _SimdType>
1273 typename _SimdType::value_type
1274 hmax(const const_where_expression<_MaskType, _SimdType>&);
1275
1276 // algorithms [simd.alg]
1277 template <class _Tp, class _Abi>
1278 simd<_Tp, _Abi> min(const simd<_Tp, _Abi>&, const simd<_Tp, _Abi>&) noexcept;
1279
1280 template <class _Tp, class _Abi>
1281 simd<_Tp, _Abi> max(const simd<_Tp, _Abi>&, const simd<_Tp, _Abi>&) noexcept;
1282
1283 template <class _Tp, class _Abi>
1284 std::pair<simd<_Tp, _Abi>, simd<_Tp, _Abi>>
1285 minmax(const simd<_Tp, _Abi>&, const simd<_Tp, _Abi>&) noexcept;
1286
1287 template <class _Tp, class _Abi>
1288 simd<_Tp, _Abi> clamp(const simd<_Tp, _Abi>&, const simd<_Tp, _Abi>&,
1289                       const simd<_Tp, _Abi>&);
1290
1291 // [simd.whereexpr]
1292 // TODO implement where expressions.
1293 template <class _MaskType, class _Tp>
1294 class const_where_expression {
1295 public:
1296   const_where_expression(const const_where_expression&) = delete;
1297   const_where_expression& operator=(const const_where_expression&) = delete;
1298   typename remove_const<_Tp>::type operator-() const&&;
1299   template <class _Up, class _Flags>
1300   void copy_to(_Up*, _Flags) const&&;
1301 };
1302
1303 template <class _MaskType, class _Tp>
1304 class where_expression : public const_where_expression<_MaskType, _Tp> {
1305 public:
1306   where_expression(const where_expression&) = delete;
1307   where_expression& operator=(const where_expression&) = delete;
1308   template <class _Up>
1309   void operator=(_Up&&);
1310   template <class _Up>
1311   void operator+=(_Up&&);
1312   template <class _Up>
1313   void operator-=(_Up&&);
1314   template <class _Up>
1315   void operator*=(_Up&&);
1316   template <class _Up>
1317   void operator/=(_Up&&);
1318   template <class _Up>
1319   void operator%=(_Up&&);
1320   template <class _Up>
1321   void operator&=(_Up&&);
1322   template <class _Up>
1323   void operator|=(_Up&&);
1324   template <class _Up>
1325   void operator^=(_Up&&);
1326   template <class _Up>
1327   void operator<<=(_Up&&);
1328   template <class _Up>
1329   void operator>>=(_Up&&);
1330   void operator++();
1331   void operator++(int);
1332   void operator--();
1333   void operator--(int);
1334   template <class _Up, class _Flags>
1335   void copy_from(const _Up*, _Flags);
1336 };
1337
1338 // [simd.class]
1339 // TODO: implement simd
1340 template <class _Tp, class _Abi>
1341 class simd {
1342 public:
1343   using value_type = _Tp;
1344   using reference = __simd_reference<_Tp, _Tp, _Abi>;
1345   using mask_type = simd_mask<_Tp, _Abi>;
1346   using abi_type = _Abi;
1347
1348   simd() = default;
1349   simd(const simd&) = default;
1350   simd& operator=(const simd&) = default;
1351
1352   static constexpr size_t size() noexcept {
1353     return simd_size<_Tp, _Abi>::value;
1354   }
1355
1356 private:
1357   __simd_storage<_Tp, _Abi> __s_;
1358
1359   template <class _Up>
1360   static constexpr bool __can_broadcast() {
1361     return (std::is_arithmetic<_Up>::value &&
1362             __is_non_narrowing_arithmetic_convertible<_Up, _Tp>()) ||
1363            (!std::is_arithmetic<_Up>::value &&
1364             std::is_convertible<_Up, _Tp>::value) ||
1365            std::is_same<typename std::remove_const<_Up>::type, int>::value ||
1366            (std::is_same<typename std::remove_const<_Up>::type,
1367                          unsigned int>::value &&
1368             std::is_unsigned<_Tp>::value);
1369   }
1370
1371   template <class _Generator, size_t... __indicies>
1372   static constexpr decltype(
1373       std::forward_as_tuple(std::declval<_Generator>()(
1374           std::integral_constant<size_t, __indicies>())...),
1375       bool())
1376   __can_generate(std::index_sequence<__indicies...>) {
1377     return !__variadic_sum<bool>(
1378         !__can_broadcast<decltype(std::declval<_Generator>()(
1379             std::integral_constant<size_t, __indicies>()))>()...);
1380   }
1381
1382   template <class _Generator>
1383   static bool __can_generate(...) {
1384     return false;
1385   }
1386
1387   template <class _Generator, size_t... __indicies>
1388   void __generator_init(_Generator&& __g, std::index_sequence<__indicies...>) {
1389     int __not_used[]{((*this)[__indicies] =
1390                           __g(std::integral_constant<size_t, __indicies>()),
1391                       0)...};
1392     (void)__not_used;
1393   }
1394
1395 public:
1396   // implicit type conversion constructor
1397   template <class _Up,
1398             class = typename std::enable_if<
1399                 std::is_same<_Abi, simd_abi::fixed_size<size()>>::value &&
1400                 __is_non_narrowing_arithmetic_convertible<_Up, _Tp>()>::type>
1401   simd(const simd<_Up, simd_abi::fixed_size<size()>>& __v) {
1402     for (size_t __i = 0; __i < size(); __i++) {
1403       (*this)[__i] = static_cast<_Tp>(__v[__i]);
1404     }
1405   }
1406
1407   // implicit broadcast constructor
1408   template <class _Up,
1409             class = typename std::enable_if<__can_broadcast<_Up>()>::type>
1410   simd(_Up&& __rv) {
1411     auto __v = static_cast<_Tp>(__rv);
1412     for (size_t __i = 0; __i < size(); __i++) {
1413       (*this)[__i] = __v;
1414     }
1415   }
1416
1417   // generator constructor
1418   template <class _Generator,
1419             int = typename std::enable_if<
1420                 __can_generate<_Generator>(std::make_index_sequence<size()>()),
1421                 int>::type()>
1422   explicit simd(_Generator&& __g) {
1423     __generator_init(std::forward<_Generator>(__g),
1424                      std::make_index_sequence<size()>());
1425   }
1426
1427   // load constructor
1428   template <
1429       class _Up, class _Flags,
1430       class = typename std::enable_if<__vectorizable<_Up>()>::type,
1431       class = typename std::enable_if<is_simd_flag_type<_Flags>::value>::type>
1432   simd(const _Up* __buffer, _Flags) {
1433     // TODO: optimize for overaligned flags
1434     for (size_t __i = 0; __i < size(); __i++) {
1435       (*this)[__i] = static_cast<_Tp>(__buffer[__i]);
1436     }
1437   }
1438
1439   // loads [simd.load]
1440   template <class _Up, class _Flags>
1441   typename std::enable_if<__vectorizable<_Up>() &&
1442                           is_simd_flag_type<_Flags>::value>::type
1443   copy_from(const _Up* __buffer, _Flags) {
1444     *this = simd(__buffer, _Flags());
1445   }
1446
1447   // stores [simd.store]
1448   template <class _Up, class _Flags>
1449   typename std::enable_if<__vectorizable<_Up>() &&
1450                           is_simd_flag_type<_Flags>::value>::type
1451   copy_to(_Up* __buffer, _Flags) const {
1452     // TODO: optimize for overaligned flags
1453     for (size_t __i = 0; __i < size(); __i++) {
1454       __buffer[__i] = static_cast<_Up>((*this)[__i]);
1455     }
1456   }
1457
1458   // scalar access [simd.subscr]
1459   reference operator[](size_t __i) { return reference(&__s_, __i); }
1460
1461   value_type operator[](size_t __i) const { return __s_.__get(__i); }
1462
1463   // unary operators [simd.unary]
1464   simd& operator++();
1465   simd operator++(int);
1466   simd& operator--();
1467   simd operator--(int);
1468   mask_type operator!() const;
1469   simd operator~() const;
1470   simd operator+() const;
1471   simd operator-() const;
1472
1473   // binary operators [simd.binary]
1474   friend simd operator+(const simd&, const simd&);
1475   friend simd operator-(const simd&, const simd&);
1476   friend simd operator*(const simd&, const simd&);
1477   friend simd operator/(const simd&, const simd&);
1478   friend simd operator%(const simd&, const simd&);
1479   friend simd operator&(const simd&, const simd&);
1480   friend simd operator|(const simd&, const simd&);
1481   friend simd operator^(const simd&, const simd&);
1482   friend simd operator<<(const simd&, const simd&);
1483   friend simd operator>>(const simd&, const simd&);
1484   friend simd operator<<(const simd&, int);
1485   friend simd operator>>(const simd&, int);
1486
1487   // compound assignment [simd.cassign]
1488   friend simd& operator+=(simd&, const simd&);
1489   friend simd& operator-=(simd&, const simd&);
1490   friend simd& operator*=(simd&, const simd&);
1491   friend simd& operator/=(simd&, const simd&);
1492   friend simd& operator%=(simd&, const simd&);
1493
1494   friend simd& operator&=(simd&, const simd&);
1495   friend simd& operator|=(simd&, const simd&);
1496   friend simd& operator^=(simd&, const simd&);
1497   friend simd& operator<<=(simd&, const simd&);
1498   friend simd& operator>>=(simd&, const simd&);
1499   friend simd& operator<<=(simd&, int);
1500   friend simd& operator>>=(simd&, int);
1501
1502   // compares [simd.comparison]
1503   friend mask_type operator==(const simd&, const simd&);
1504   friend mask_type operator!=(const simd&, const simd&);
1505   friend mask_type operator>=(const simd&, const simd&);
1506   friend mask_type operator<=(const simd&, const simd&);
1507   friend mask_type operator>(const simd&, const simd&);
1508   friend mask_type operator<(const simd&, const simd&);
1509 };
1510
1511 // [simd.mask.class]
1512 template <class _Tp, class _Abi>
1513 // TODO: implement simd_mask
1514 class simd_mask {
1515 public:
1516   using value_type = bool;
1517   // TODO: this is strawman implementation. Turn it into a proxy type.
1518   using reference = bool&;
1519   using simd_type = simd<_Tp, _Abi>;
1520   using abi_type = _Abi;
1521   static constexpr size_t size() noexcept;
1522   simd_mask() = default;
1523
1524   // broadcast constructor
1525   explicit simd_mask(value_type) noexcept;
1526
1527   // implicit type conversion constructor
1528   template <class _Up>
1529   simd_mask(const simd_mask<_Up, simd_abi::fixed_size<size()>>&) noexcept;
1530
1531   // load constructor
1532   template <class _Flags>
1533   simd_mask(const value_type*, _Flags);
1534
1535   // loads [simd.mask.copy]
1536   template <class _Flags>
1537   void copy_from(const value_type*, _Flags);
1538   template <class _Flags>
1539   void copy_to(value_type*, _Flags) const;
1540
1541   // scalar access [simd.mask.subscr]
1542   reference operator[](size_t);
1543   value_type operator[](size_t) const;
1544
1545   // unary operators [simd.mask.unary]
1546   simd_mask operator!() const noexcept;
1547
1548   // simd_mask binary operators [simd.mask.binary]
1549   friend simd_mask operator&&(const simd_mask&, const simd_mask&) noexcept;
1550   friend simd_mask operator||(const simd_mask&, const simd_mask&) noexcept;
1551   friend simd_mask operator&(const simd_mask&, const simd_mask&)noexcept;
1552   friend simd_mask operator|(const simd_mask&, const simd_mask&) noexcept;
1553   friend simd_mask operator^(const simd_mask&, const simd_mask&) noexcept;
1554
1555   // simd_mask compound assignment [simd.mask.cassign]
1556   friend simd_mask& operator&=(simd_mask&, const simd_mask&) noexcept;
1557   friend simd_mask& operator|=(simd_mask&, const simd_mask&) noexcept;
1558   friend simd_mask& operator^=(simd_mask&, const simd_mask&) noexcept;
1559
1560   // simd_mask compares [simd.mask.comparison]
1561   friend simd_mask operator==(const simd_mask&, const simd_mask&) noexcept;
1562   friend simd_mask operator!=(const simd_mask&, const simd_mask&) noexcept;
1563 };
1564
1565 #endif // _LIBCPP_STD_VER >= 17
1566
1567 _LIBCPP_END_NAMESPACE_EXPERIMENTAL_SIMD
1568
1569 #endif /* _LIBCPP_EXPERIMENTAL_SIMD */