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