libstdc++
simd.h
1// Definition of the public simd interfaces -*- C++ -*-
2
3// Copyright (C) 2020-2023 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25#ifndef _GLIBCXX_EXPERIMENTAL_SIMD_H
26#define _GLIBCXX_EXPERIMENTAL_SIMD_H
27
28#if __cplusplus >= 201703L
29
30#include "simd_detail.h"
31#include "numeric_traits.h"
32#include <bit>
33#include <bitset>
34#ifdef _GLIBCXX_DEBUG_UB
35#include <cstdio> // for stderr
36#endif
37#include <cstring>
38#include <cmath>
39#include <functional>
40#include <iosfwd>
41#include <utility>
42
43#if _GLIBCXX_SIMD_X86INTRIN
44#include <x86intrin.h>
45#elif _GLIBCXX_SIMD_HAVE_NEON
46#include <arm_neon.h>
47#endif
48
49/** @ingroup ts_simd
50 * @{
51 */
52/* There are several closely related types, with the following naming
53 * convention:
54 * _Tp: vectorizable (arithmetic) type (or any type)
55 * _TV: __vector_type_t<_Tp, _Np>
56 * _TW: _SimdWrapper<_Tp, _Np>
57 * _TI: __intrinsic_type_t<_Tp, _Np>
58 * _TVT: _VectorTraits<_TV> or _VectorTraits<_TW>
59 * If one additional type is needed use _U instead of _T.
60 * Otherwise use _T\d, _TV\d, _TW\d, TI\d, _TVT\d.
61 *
62 * More naming conventions:
63 * _Ap or _Abi: An ABI tag from the simd_abi namespace
64 * _Ip: often used for integer types with sizeof(_Ip) == sizeof(_Tp),
65 * _IV, _IW as for _TV, _TW
66 * _Np: number of elements (not bytes)
67 * _Bytes: number of bytes
68 *
69 * Variable names:
70 * __k: mask object (vector- or bitmask)
71 */
72_GLIBCXX_SIMD_BEGIN_NAMESPACE
73
74#if !_GLIBCXX_SIMD_X86INTRIN
75using __m128 [[__gnu__::__vector_size__(16)]] = float;
76using __m128d [[__gnu__::__vector_size__(16)]] = double;
77using __m128i [[__gnu__::__vector_size__(16)]] = long long;
78using __m256 [[__gnu__::__vector_size__(32)]] = float;
79using __m256d [[__gnu__::__vector_size__(32)]] = double;
80using __m256i [[__gnu__::__vector_size__(32)]] = long long;
81using __m512 [[__gnu__::__vector_size__(64)]] = float;
82using __m512d [[__gnu__::__vector_size__(64)]] = double;
83using __m512i [[__gnu__::__vector_size__(64)]] = long long;
84#endif
85
86namespace simd_abi {
87// simd_abi forward declarations {{{
88// implementation details:
89struct _Scalar;
90
91template <int _Np>
92 struct _Fixed;
93
94// There are two major ABIs that appear on different architectures.
95// Both have non-boolean values packed into an N Byte register
96// -> #elements = N / sizeof(T)
97// Masks differ:
98// 1. Use value vector registers for masks (all 0 or all 1)
99// 2. Use bitmasks (mask registers) with one bit per value in the corresponding
100// value vector
101//
102// Both can be partially used, masking off the rest when doing horizontal
103// operations or operations that can trap (e.g. FP_INVALID or integer division
104// by 0). This is encoded as the number of used bytes.
105template <int _UsedBytes>
106 struct _VecBuiltin;
107
108template <int _UsedBytes>
109 struct _VecBltnBtmsk;
110
111template <typename _Tp, int _Np>
112 using _VecN = _VecBuiltin<sizeof(_Tp) * _Np>;
113
114template <int _UsedBytes = 16>
115 using _Sse = _VecBuiltin<_UsedBytes>;
116
117template <int _UsedBytes = 32>
118 using _Avx = _VecBuiltin<_UsedBytes>;
119
120template <int _UsedBytes = 64>
121 using _Avx512 = _VecBltnBtmsk<_UsedBytes>;
122
123template <int _UsedBytes = 16>
124 using _Neon = _VecBuiltin<_UsedBytes>;
125
126// implementation-defined:
127using __sse = _Sse<>;
128using __avx = _Avx<>;
129using __avx512 = _Avx512<>;
130using __neon = _Neon<>;
131using __neon128 = _Neon<16>;
132using __neon64 = _Neon<8>;
133
134// standard:
135template <typename _Tp, size_t _Np, typename...>
136 struct deduce;
137
138template <int _Np>
139 using fixed_size = _Fixed<_Np>;
140
141using scalar = _Scalar;
142
143// }}}
144} // namespace simd_abi
145// forward declarations is_simd(_mask), simd(_mask), simd_size {{{
146template <typename _Tp>
147 struct is_simd;
148
149template <typename _Tp>
150 struct is_simd_mask;
151
152template <typename _Tp, typename _Abi>
153 class simd;
154
155template <typename _Tp, typename _Abi>
156 class simd_mask;
157
158template <typename _Tp, typename _Abi>
159 struct simd_size;
160
161// }}}
162// load/store flags {{{
163struct element_aligned_tag
164{
165 template <typename _Tp, typename _Up = typename _Tp::value_type>
166 static constexpr size_t _S_alignment = alignof(_Up);
167
168 template <typename _Tp, typename _Up>
169 _GLIBCXX_SIMD_INTRINSIC static constexpr _Up*
170 _S_apply(_Up* __ptr)
171 { return __ptr; }
172};
173
174struct vector_aligned_tag
175{
176 template <typename _Tp, typename _Up = typename _Tp::value_type>
177 static constexpr size_t _S_alignment
178 = std::__bit_ceil(sizeof(_Up) * _Tp::size());
179
180 template <typename _Tp, typename _Up>
181 _GLIBCXX_SIMD_INTRINSIC static constexpr _Up*
182 _S_apply(_Up* __ptr)
183 { return static_cast<_Up*>(__builtin_assume_aligned(__ptr, _S_alignment<_Tp, _Up>)); }
184};
185
186template <size_t _Np> struct overaligned_tag
187{
188 template <typename _Tp, typename _Up = typename _Tp::value_type>
189 static constexpr size_t _S_alignment = _Np;
190
191 template <typename _Tp, typename _Up>
192 _GLIBCXX_SIMD_INTRINSIC static constexpr _Up*
193 _S_apply(_Up* __ptr)
194 { return static_cast<_Up*>(__builtin_assume_aligned(__ptr, _Np)); }
195};
196
197inline constexpr element_aligned_tag element_aligned = {};
198
199inline constexpr vector_aligned_tag vector_aligned = {};
200
201template <size_t _Np>
202 inline constexpr overaligned_tag<_Np> overaligned = {};
203
204// }}}
205template <size_t _Xp>
206 using _SizeConstant = integral_constant<size_t, _Xp>;
207// constexpr feature detection{{{
208constexpr inline bool __have_mmx = _GLIBCXX_SIMD_HAVE_MMX;
209constexpr inline bool __have_sse = _GLIBCXX_SIMD_HAVE_SSE;
210constexpr inline bool __have_sse2 = _GLIBCXX_SIMD_HAVE_SSE2;
211constexpr inline bool __have_sse3 = _GLIBCXX_SIMD_HAVE_SSE3;
212constexpr inline bool __have_ssse3 = _GLIBCXX_SIMD_HAVE_SSSE3;
213constexpr inline bool __have_sse4_1 = _GLIBCXX_SIMD_HAVE_SSE4_1;
214constexpr inline bool __have_sse4_2 = _GLIBCXX_SIMD_HAVE_SSE4_2;
215constexpr inline bool __have_xop = _GLIBCXX_SIMD_HAVE_XOP;
216constexpr inline bool __have_avx = _GLIBCXX_SIMD_HAVE_AVX;
217constexpr inline bool __have_avx2 = _GLIBCXX_SIMD_HAVE_AVX2;
218constexpr inline bool __have_bmi = _GLIBCXX_SIMD_HAVE_BMI1;
219constexpr inline bool __have_bmi2 = _GLIBCXX_SIMD_HAVE_BMI2;
220constexpr inline bool __have_lzcnt = _GLIBCXX_SIMD_HAVE_LZCNT;
221constexpr inline bool __have_sse4a = _GLIBCXX_SIMD_HAVE_SSE4A;
222constexpr inline bool __have_fma = _GLIBCXX_SIMD_HAVE_FMA;
223constexpr inline bool __have_fma4 = _GLIBCXX_SIMD_HAVE_FMA4;
224constexpr inline bool __have_f16c = _GLIBCXX_SIMD_HAVE_F16C;
225constexpr inline bool __have_popcnt = _GLIBCXX_SIMD_HAVE_POPCNT;
226constexpr inline bool __have_avx512f = _GLIBCXX_SIMD_HAVE_AVX512F;
227constexpr inline bool __have_avx512dq = _GLIBCXX_SIMD_HAVE_AVX512DQ;
228constexpr inline bool __have_avx512vl = _GLIBCXX_SIMD_HAVE_AVX512VL;
229constexpr inline bool __have_avx512bw = _GLIBCXX_SIMD_HAVE_AVX512BW;
230constexpr inline bool __have_avx512dq_vl = __have_avx512dq && __have_avx512vl;
231constexpr inline bool __have_avx512bw_vl = __have_avx512bw && __have_avx512vl;
232constexpr inline bool __have_avx512bitalg = _GLIBCXX_SIMD_HAVE_AVX512BITALG;
233constexpr inline bool __have_avx512vbmi2 = _GLIBCXX_SIMD_HAVE_AVX512VBMI2;
234constexpr inline bool __have_avx512vbmi = _GLIBCXX_SIMD_HAVE_AVX512VBMI;
235constexpr inline bool __have_avx512ifma = _GLIBCXX_SIMD_HAVE_AVX512IFMA;
236constexpr inline bool __have_avx512cd = _GLIBCXX_SIMD_HAVE_AVX512CD;
237constexpr inline bool __have_avx512vnni = _GLIBCXX_SIMD_HAVE_AVX512VNNI;
238constexpr inline bool __have_avx512vpopcntdq = _GLIBCXX_SIMD_HAVE_AVX512VPOPCNTDQ;
239constexpr inline bool __have_avx512vp2intersect = _GLIBCXX_SIMD_HAVE_AVX512VP2INTERSECT;
240
241constexpr inline bool __have_neon = _GLIBCXX_SIMD_HAVE_NEON;
242constexpr inline bool __have_neon_a32 = _GLIBCXX_SIMD_HAVE_NEON_A32;
243constexpr inline bool __have_neon_a64 = _GLIBCXX_SIMD_HAVE_NEON_A64;
244constexpr inline bool __support_neon_float =
245#if defined __GCC_IEC_559
246 __GCC_IEC_559 == 0;
247#elif defined __FAST_MATH__
248 true;
249#else
250 false;
251#endif
252
253#ifdef _ARCH_PWR10
254constexpr inline bool __have_power10vec = true;
255#else
256constexpr inline bool __have_power10vec = false;
257#endif
258#ifdef __POWER9_VECTOR__
259constexpr inline bool __have_power9vec = true;
260#else
261constexpr inline bool __have_power9vec = false;
262#endif
263#if defined __POWER8_VECTOR__
264constexpr inline bool __have_power8vec = true;
265#else
266constexpr inline bool __have_power8vec = __have_power9vec;
267#endif
268#if defined __VSX__
269constexpr inline bool __have_power_vsx = true;
270#else
271constexpr inline bool __have_power_vsx = __have_power8vec;
272#endif
273#if defined __ALTIVEC__
274constexpr inline bool __have_power_vmx = true;
275#else
276constexpr inline bool __have_power_vmx = __have_power_vsx;
277#endif
278
279// }}}
280
281namespace __detail
282{
283#ifdef math_errhandling
284 // Determines _S_handle_fpexcept from math_errhandling if it is defined and expands to a constant
285 // expression. math_errhandling may expand to an extern symbol, in which case a constexpr value
286 // must be guessed.
287 template <int = math_errhandling>
288 constexpr bool
289 __handle_fpexcept_impl(int)
290 { return math_errhandling & MATH_ERREXCEPT; }
291#endif
292
293 // Fallback if math_errhandling doesn't work: with fast-math assume floating-point exceptions are
294 // ignored, otherwise implement correct exception behavior.
295 constexpr bool
296 __handle_fpexcept_impl(float)
297 {
298#if defined __FAST_MATH__
299 return false;
300#else
301 return true;
302#endif
303 }
304
305 /// True if math functions must raise floating-point exceptions as specified by C17.
306 static constexpr bool _S_handle_fpexcept = __handle_fpexcept_impl(0);
307
308 constexpr std::uint_least64_t
309 __floating_point_flags()
310 {
311 std::uint_least64_t __flags = 0;
312 if constexpr (_S_handle_fpexcept)
313 __flags |= 1;
314#ifdef __FAST_MATH__
315 __flags |= 1 << 1;
316#elif __FINITE_MATH_ONLY__
317 __flags |= 2 << 1;
318#elif __GCC_IEC_559 < 2
319 __flags |= 3 << 1;
320#endif
321 __flags |= (__FLT_EVAL_METHOD__ + 1) << 3;
322 return __flags;
323 }
324
325 constexpr std::uint_least64_t
326 __machine_flags()
327 {
328 if constexpr (__have_mmx || __have_sse)
329 return __have_mmx
330 | (__have_sse << 1)
331 | (__have_sse2 << 2)
332 | (__have_sse3 << 3)
333 | (__have_ssse3 << 4)
334 | (__have_sse4_1 << 5)
335 | (__have_sse4_2 << 6)
336 | (__have_xop << 7)
337 | (__have_avx << 8)
338 | (__have_avx2 << 9)
339 | (__have_bmi << 10)
340 | (__have_bmi2 << 11)
341 | (__have_lzcnt << 12)
342 | (__have_sse4a << 13)
343 | (__have_fma << 14)
344 | (__have_fma4 << 15)
345 | (__have_f16c << 16)
346 | (__have_popcnt << 17)
347 | (__have_avx512f << 18)
348 | (__have_avx512dq << 19)
349 | (__have_avx512vl << 20)
350 | (__have_avx512bw << 21)
351 | (__have_avx512bitalg << 22)
352 | (__have_avx512vbmi2 << 23)
353 | (__have_avx512vbmi << 24)
354 | (__have_avx512ifma << 25)
355 | (__have_avx512cd << 26)
356 | (__have_avx512vnni << 27)
357 | (__have_avx512vpopcntdq << 28)
358 | (__have_avx512vp2intersect << 29);
359 else if constexpr (__have_neon)
360 return __have_neon
361 | (__have_neon_a32 << 1)
362 | (__have_neon_a64 << 2)
363 | (__have_neon_a64 << 2)
364 | (__support_neon_float << 3);
365 else if constexpr (__have_power_vmx)
366 return __have_power_vmx
367 | (__have_power_vsx << 1)
368 | (__have_power8vec << 2)
369 | (__have_power9vec << 3)
370 | (__have_power10vec << 4);
371 else
372 return 0;
373 }
374
375 namespace
376 {
377 struct _OdrEnforcer {};
378 }
379
380 template <std::uint_least64_t...>
381 struct _MachineFlagsTemplate {};
382
383 /**@internal
384 * Use this type as default template argument to all function templates that
385 * are not declared always_inline. It ensures, that a function
386 * specialization, which the compiler decides not to inline, has a unique symbol
387 * (_OdrEnforcer) or a symbol matching the machine/architecture flags
388 * (_MachineFlagsTemplate). This helps to avoid ODR violations in cases where
389 * users link TUs compiled with different flags. This is especially important
390 * for using simd in libraries.
391 */
392 using __odr_helper
393 = conditional_t<__machine_flags() == 0, _OdrEnforcer,
394 _MachineFlagsTemplate<__machine_flags(), __floating_point_flags()>>;
395
396 struct _Minimum
397 {
398 template <typename _Tp>
399 _GLIBCXX_SIMD_INTRINSIC constexpr
400 _Tp
401 operator()(_Tp __a, _Tp __b) const
402 {
403 using std::min;
404 return min(__a, __b);
405 }
406 };
407
408 struct _Maximum
409 {
410 template <typename _Tp>
411 _GLIBCXX_SIMD_INTRINSIC constexpr
412 _Tp
413 operator()(_Tp __a, _Tp __b) const
414 {
415 using std::max;
416 return max(__a, __b);
417 }
418 };
419} // namespace __detail
420
421// unrolled/pack execution helpers
422// __execute_n_times{{{
423template <typename _Fp, size_t... _I>
424 [[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr
425 void
426 __execute_on_index_sequence(_Fp&& __f, index_sequence<_I...>)
427 { ((void)__f(_SizeConstant<_I>()), ...); }
428
429template <typename _Fp>
430 _GLIBCXX_SIMD_INTRINSIC constexpr void
431 __execute_on_index_sequence(_Fp&&, index_sequence<>)
432 { }
433
434template <size_t _Np, typename _Fp>
435 _GLIBCXX_SIMD_INTRINSIC constexpr void
436 __execute_n_times(_Fp&& __f)
437 {
438 __execute_on_index_sequence(static_cast<_Fp&&>(__f),
439 make_index_sequence<_Np>{});
440 }
441
442// }}}
443// __generate_from_n_evaluations{{{
444template <typename _R, typename _Fp, size_t... _I>
445 [[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr
446 _R
447 __execute_on_index_sequence_with_return(_Fp&& __f, index_sequence<_I...>)
448 { return _R{__f(_SizeConstant<_I>())...}; }
449
450template <size_t _Np, typename _R, typename _Fp>
451 _GLIBCXX_SIMD_INTRINSIC constexpr _R
452 __generate_from_n_evaluations(_Fp&& __f)
453 {
454 return __execute_on_index_sequence_with_return<_R>(
455 static_cast<_Fp&&>(__f), make_index_sequence<_Np>{});
456 }
457
458// }}}
459// __call_with_n_evaluations{{{
460template <size_t... _I, typename _F0, typename _FArgs>
461 [[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr
462 auto
463 __call_with_n_evaluations(index_sequence<_I...>, _F0&& __f0, _FArgs&& __fargs)
464 { return __f0(__fargs(_SizeConstant<_I>())...); }
465
466template <size_t _Np, typename _F0, typename _FArgs>
467 _GLIBCXX_SIMD_INTRINSIC constexpr auto
468 __call_with_n_evaluations(_F0&& __f0, _FArgs&& __fargs)
469 {
470 return __call_with_n_evaluations(make_index_sequence<_Np>{},
471 static_cast<_F0&&>(__f0),
472 static_cast<_FArgs&&>(__fargs));
473 }
474
475// }}}
476// __call_with_subscripts{{{
477template <size_t _First = 0, size_t... _It, typename _Tp, typename _Fp>
478 [[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr
479 auto
480 __call_with_subscripts(_Tp&& __x, index_sequence<_It...>, _Fp&& __fun)
481 { return __fun(__x[_First + _It]...); }
482
483template <size_t _Np, size_t _First = 0, typename _Tp, typename _Fp>
484 _GLIBCXX_SIMD_INTRINSIC constexpr auto
485 __call_with_subscripts(_Tp&& __x, _Fp&& __fun)
486 {
487 return __call_with_subscripts<_First>(static_cast<_Tp&&>(__x),
488 make_index_sequence<_Np>(),
489 static_cast<_Fp&&>(__fun));
490 }
491
492// }}}
493
494// vvv ---- type traits ---- vvv
495// integer type aliases{{{
496using _UChar = unsigned char;
497using _SChar = signed char;
498using _UShort = unsigned short;
499using _UInt = unsigned int;
500using _ULong = unsigned long;
501using _ULLong = unsigned long long;
502using _LLong = long long;
503
504//}}}
505// __first_of_pack{{{
506template <typename _T0, typename...>
507 struct __first_of_pack
508 { using type = _T0; };
509
510template <typename... _Ts>
511 using __first_of_pack_t = typename __first_of_pack<_Ts...>::type;
512
513//}}}
514// __value_type_or_identity_t {{{
515template <typename _Tp>
516 typename _Tp::value_type
517 __value_type_or_identity_impl(int);
518
519template <typename _Tp>
520 _Tp
521 __value_type_or_identity_impl(float);
522
523template <typename _Tp>
524 using __value_type_or_identity_t
525 = decltype(__value_type_or_identity_impl<_Tp>(int()));
526
527// }}}
528// __is_vectorizable {{{
529template <typename _Tp>
530 struct __is_vectorizable : public is_arithmetic<_Tp> {};
531
532template <>
533 struct __is_vectorizable<bool> : public false_type {};
534
535template <typename _Tp>
536 inline constexpr bool __is_vectorizable_v = __is_vectorizable<_Tp>::value;
537
538// Deduces to a vectorizable type
539template <typename _Tp, typename = enable_if_t<__is_vectorizable_v<_Tp>>>
540 using _Vectorizable = _Tp;
541
542// }}}
543// _LoadStorePtr / __is_possible_loadstore_conversion {{{
544template <typename _Ptr, typename _ValueType>
545 struct __is_possible_loadstore_conversion
546 : conjunction<__is_vectorizable<_Ptr>, __is_vectorizable<_ValueType>> {};
547
548template <>
549 struct __is_possible_loadstore_conversion<bool, bool> : true_type {};
550
551// Deduces to a type allowed for load/store with the given value type.
552template <typename _Ptr, typename _ValueType,
553 typename = enable_if_t<
554 __is_possible_loadstore_conversion<_Ptr, _ValueType>::value>>
555 using _LoadStorePtr = _Ptr;
556
557// }}}
558// __is_bitmask{{{
559template <typename _Tp, typename = void_t<>>
560 struct __is_bitmask : false_type {};
561
562template <typename _Tp>
563 inline constexpr bool __is_bitmask_v = __is_bitmask<_Tp>::value;
564
565// the __mmaskXX case:
566template <typename _Tp>
567 struct __is_bitmask<_Tp,
568 void_t<decltype(declval<unsigned&>() = declval<_Tp>() & 1u)>>
569 : true_type {};
570
571// }}}
572// __int_for_sizeof{{{
573#pragma GCC diagnostic push
574#pragma GCC diagnostic ignored "-Wpedantic"
575template <size_t _Bytes>
576 constexpr auto
577 __int_for_sizeof()
578 {
579 if constexpr (_Bytes == sizeof(int))
580 return int();
581 #ifdef __clang__
582 else if constexpr (_Bytes == sizeof(char))
583 return char();
584 #else
585 else if constexpr (_Bytes == sizeof(_SChar))
586 return _SChar();
587 #endif
588 else if constexpr (_Bytes == sizeof(short))
589 return short();
590 #ifndef __clang__
591 else if constexpr (_Bytes == sizeof(long))
592 return long();
593 #endif
594 else if constexpr (_Bytes == sizeof(_LLong))
595 return _LLong();
596 #ifdef __SIZEOF_INT128__
597 else if constexpr (_Bytes == sizeof(__int128))
598 return __int128();
599 #endif // __SIZEOF_INT128__
600 else if constexpr (_Bytes % sizeof(int) == 0)
601 {
602 constexpr size_t _Np = _Bytes / sizeof(int);
603 struct _Ip
604 {
605 int _M_data[_Np];
606
607 _GLIBCXX_SIMD_INTRINSIC constexpr _Ip
608 operator&(_Ip __rhs) const
609 {
610 return __generate_from_n_evaluations<_Np, _Ip>(
611 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
612 return __rhs._M_data[__i] & _M_data[__i];
613 });
614 }
615
616 _GLIBCXX_SIMD_INTRINSIC constexpr _Ip
617 operator|(_Ip __rhs) const
618 {
619 return __generate_from_n_evaluations<_Np, _Ip>(
620 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
621 return __rhs._M_data[__i] | _M_data[__i];
622 });
623 }
624
625 _GLIBCXX_SIMD_INTRINSIC constexpr _Ip
626 operator^(_Ip __rhs) const
627 {
628 return __generate_from_n_evaluations<_Np, _Ip>(
629 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
630 return __rhs._M_data[__i] ^ _M_data[__i];
631 });
632 }
633
634 _GLIBCXX_SIMD_INTRINSIC constexpr _Ip
635 operator~() const
636 {
637 return __generate_from_n_evaluations<_Np, _Ip>(
638 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { return ~_M_data[__i]; });
639 }
640 };
641 return _Ip{};
642 }
643 else
644 static_assert(_Bytes != _Bytes, "this should be unreachable");
645 }
646#pragma GCC diagnostic pop
647
648template <typename _Tp>
649 using __int_for_sizeof_t = decltype(__int_for_sizeof<sizeof(_Tp)>());
650
651template <size_t _Np>
652 using __int_with_sizeof_t = decltype(__int_for_sizeof<_Np>());
653
654// }}}
655// __is_fixed_size_abi{{{
656template <typename _Tp>
657 struct __is_fixed_size_abi : false_type {};
658
659template <int _Np>
660 struct __is_fixed_size_abi<simd_abi::fixed_size<_Np>> : true_type {};
661
662template <typename _Tp>
663 inline constexpr bool __is_fixed_size_abi_v = __is_fixed_size_abi<_Tp>::value;
664
665// }}}
666// __is_scalar_abi {{{
667template <typename _Abi>
668 constexpr bool
669 __is_scalar_abi()
670 { return is_same_v<simd_abi::scalar, _Abi>; }
671
672// }}}
673// __abi_bytes_v {{{
674template <template <int> class _Abi, int _Bytes>
675 constexpr int
676 __abi_bytes_impl(_Abi<_Bytes>*)
677 { return _Bytes; }
678
679template <typename _Tp>
680 constexpr int
681 __abi_bytes_impl(_Tp*)
682 { return -1; }
683
684template <typename _Abi>
685 inline constexpr int __abi_bytes_v
686 = __abi_bytes_impl(static_cast<_Abi*>(nullptr));
687
688// }}}
689// __is_builtin_bitmask_abi {{{
690template <typename _Abi>
691 constexpr bool
692 __is_builtin_bitmask_abi()
693 { return is_same_v<simd_abi::_VecBltnBtmsk<__abi_bytes_v<_Abi>>, _Abi>; }
694
695// }}}
696// __is_sse_abi {{{
697template <typename _Abi>
698 constexpr bool
699 __is_sse_abi()
700 {
701 constexpr auto _Bytes = __abi_bytes_v<_Abi>;
702 return _Bytes <= 16 && is_same_v<simd_abi::_VecBuiltin<_Bytes>, _Abi>;
703 }
704
705// }}}
706// __is_avx_abi {{{
707template <typename _Abi>
708 constexpr bool
709 __is_avx_abi()
710 {
711 constexpr auto _Bytes = __abi_bytes_v<_Abi>;
712 return _Bytes > 16 && _Bytes <= 32
713 && is_same_v<simd_abi::_VecBuiltin<_Bytes>, _Abi>;
714 }
715
716// }}}
717// __is_avx512_abi {{{
718template <typename _Abi>
719 constexpr bool
720 __is_avx512_abi()
721 {
722 constexpr auto _Bytes = __abi_bytes_v<_Abi>;
723 return _Bytes <= 64 && is_same_v<simd_abi::_Avx512<_Bytes>, _Abi>;
724 }
725
726// }}}
727// __is_neon_abi {{{
728template <typename _Abi>
729 constexpr bool
730 __is_neon_abi()
731 {
732 constexpr auto _Bytes = __abi_bytes_v<_Abi>;
733 return _Bytes <= 16 && is_same_v<simd_abi::_VecBuiltin<_Bytes>, _Abi>;
734 }
735
736// }}}
737// __make_dependent_t {{{
738template <typename, typename _Up>
739 struct __make_dependent
740 { using type = _Up; };
741
742template <typename _Tp, typename _Up>
743 using __make_dependent_t = typename __make_dependent<_Tp, _Up>::type;
744
745// }}}
746// ^^^ ---- type traits ---- ^^^
747
748// __invoke_ub{{{
749template <typename... _Args>
750 [[noreturn]] _GLIBCXX_SIMD_ALWAYS_INLINE void
751 __invoke_ub([[maybe_unused]] const char* __msg, [[maybe_unused]] const _Args&... __args)
752 {
753#ifdef _GLIBCXX_DEBUG_UB
754 __builtin_fprintf(stderr, __msg, __args...);
755 __builtin_trap();
756#else
757 __builtin_unreachable();
758#endif
759 }
760
761// }}}
762// __assert_unreachable{{{
763template <typename _Tp>
764 struct __assert_unreachable
765 { static_assert(!is_same_v<_Tp, _Tp>, "this should be unreachable"); };
766
767// }}}
768// __size_or_zero_v {{{
769template <typename _Tp, typename _Ap, size_t _Np = simd_size<_Tp, _Ap>::value>
770 constexpr size_t
771 __size_or_zero_dispatch(int)
772 { return _Np; }
773
774template <typename _Tp, typename _Ap>
775 constexpr size_t
776 __size_or_zero_dispatch(float)
777 { return 0; }
778
779template <typename _Tp, typename _Ap>
780 inline constexpr size_t __size_or_zero_v
781 = __size_or_zero_dispatch<_Tp, _Ap>(0);
782
783// }}}
784// __div_roundup {{{
785inline constexpr size_t
786__div_roundup(size_t __a, size_t __b)
787{ return (__a + __b - 1) / __b; }
788
789// }}}
790// _ExactBool{{{
791class _ExactBool
792{
793 const bool _M_data;
794
795public:
796 _GLIBCXX_SIMD_INTRINSIC constexpr
797 _ExactBool(bool __b) : _M_data(__b) {}
798
799 _ExactBool(int) = delete;
800
801 _GLIBCXX_SIMD_INTRINSIC constexpr
802 operator bool() const
803 { return _M_data; }
804};
805
806// }}}
807// __may_alias{{{
808/**@internal
809 * Helper __may_alias<_Tp> that turns _Tp into the type to be used for an
810 * aliasing pointer. This adds the __may_alias attribute to _Tp (with compilers
811 * that support it).
812 */
813template <typename _Tp>
814 using __may_alias [[__gnu__::__may_alias__]] = _Tp;
815
816// }}}
817// _UnsupportedBase {{{
818// simd and simd_mask base for unsupported <_Tp, _Abi>
819struct _UnsupportedBase
820{
821 _UnsupportedBase() = delete;
822 _UnsupportedBase(const _UnsupportedBase&) = delete;
823 _UnsupportedBase& operator=(const _UnsupportedBase&) = delete;
824 ~_UnsupportedBase() = delete;
825};
826
827// }}}
828// _InvalidTraits {{{
829/**
830 * @internal
831 * Defines the implementation of __a given <_Tp, _Abi>.
832 *
833 * Implementations must ensure that only valid <_Tp, _Abi> instantiations are
834 * possible. Static assertions in the type definition do not suffice. It is
835 * important that SFINAE works.
836 */
837struct _InvalidTraits
838{
839 using _IsValid = false_type;
840 using _SimdBase = _UnsupportedBase;
841 using _MaskBase = _UnsupportedBase;
842
843 static constexpr size_t _S_full_size = 0;
844 static constexpr bool _S_is_partial = false;
845
846 static constexpr size_t _S_simd_align = 1;
847 struct _SimdImpl;
848 struct _SimdMember {};
849 struct _SimdCastType;
850
851 static constexpr size_t _S_mask_align = 1;
852 struct _MaskImpl;
853 struct _MaskMember {};
854 struct _MaskCastType;
855};
856
857// }}}
858// _SimdTraits {{{
859template <typename _Tp, typename _Abi, typename = void_t<>>
860 struct _SimdTraits : _InvalidTraits {};
861
862// }}}
863// __private_init, __bitset_init{{{
864/**
865 * @internal
866 * Tag used for private init constructor of simd and simd_mask
867 */
868inline constexpr struct _PrivateInit {} __private_init = {};
869
870inline constexpr struct _BitsetInit {} __bitset_init = {};
871
872// }}}
873// __is_narrowing_conversion<_From, _To>{{{
874template <typename _From, typename _To, bool = is_arithmetic_v<_From>,
875 bool = is_arithmetic_v<_To>>
876 struct __is_narrowing_conversion;
877
878// ignore "signed/unsigned mismatch" in the following trait.
879// The implicit conversions will do the right thing here.
880template <typename _From, typename _To>
881 struct __is_narrowing_conversion<_From, _To, true, true>
882 : public __bool_constant<(
883 __digits_v<_From> > __digits_v<_To>
884 || __finite_max_v<_From> > __finite_max_v<_To>
885 || __finite_min_v<_From> < __finite_min_v<_To>
886 || (is_signed_v<_From> && is_unsigned_v<_To>))> {};
887
888template <typename _Tp>
889 struct __is_narrowing_conversion<_Tp, bool, true, true>
890 : public true_type {};
891
892template <>
893 struct __is_narrowing_conversion<bool, bool, true, true>
894 : public false_type {};
895
896template <typename _Tp>
897 struct __is_narrowing_conversion<_Tp, _Tp, true, true>
898 : public false_type {};
899
900template <typename _From, typename _To>
901 struct __is_narrowing_conversion<_From, _To, false, true>
902 : public negation<is_convertible<_From, _To>> {};
903
904// }}}
905// __converts_to_higher_integer_rank{{{
906template <typename _From, typename _To, bool = (sizeof(_From) < sizeof(_To))>
907 struct __converts_to_higher_integer_rank : public true_type {};
908
909// this may fail for char -> short if sizeof(char) == sizeof(short)
910template <typename _From, typename _To>
911 struct __converts_to_higher_integer_rank<_From, _To, false>
912 : public is_same<decltype(declval<_From>() + declval<_To>()), _To> {};
913
914// }}}
915// __data(simd/simd_mask) {{{
916template <typename _Tp, typename _Ap>
917 _GLIBCXX_SIMD_INTRINSIC constexpr const auto&
918 __data(const simd<_Tp, _Ap>& __x);
919
920template <typename _Tp, typename _Ap>
921 _GLIBCXX_SIMD_INTRINSIC constexpr auto&
922 __data(simd<_Tp, _Ap>& __x);
923
924template <typename _Tp, typename _Ap>
925 _GLIBCXX_SIMD_INTRINSIC constexpr const auto&
926 __data(const simd_mask<_Tp, _Ap>& __x);
927
928template <typename _Tp, typename _Ap>
929 _GLIBCXX_SIMD_INTRINSIC constexpr auto&
930 __data(simd_mask<_Tp, _Ap>& __x);
931
932// }}}
933// _SimdConverter {{{
934template <typename _FromT, typename _FromA, typename _ToT, typename _ToA,
935 typename = void>
936 struct _SimdConverter;
937
938template <typename _Tp, typename _Ap>
939 struct _SimdConverter<_Tp, _Ap, _Tp, _Ap, void>
940 {
941 template <typename _Up>
942 _GLIBCXX_SIMD_INTRINSIC const _Up&
943 operator()(const _Up& __x)
944 { return __x; }
945 };
946
947// }}}
948// __to_value_type_or_member_type {{{
949template <typename _V>
950 _GLIBCXX_SIMD_INTRINSIC constexpr auto
951 __to_value_type_or_member_type(const _V& __x) -> decltype(__data(__x))
952 { return __data(__x); }
953
954template <typename _V>
955 _GLIBCXX_SIMD_INTRINSIC constexpr const typename _V::value_type&
956 __to_value_type_or_member_type(const typename _V::value_type& __x)
957 { return __x; }
958
959// }}}
960// __bool_storage_member_type{{{
961template <size_t _Size>
962 struct __bool_storage_member_type;
963
964template <size_t _Size>
965 using __bool_storage_member_type_t =
966 typename __bool_storage_member_type<_Size>::type;
967
968// }}}
969// _SimdTuple {{{
970// why not tuple?
971// 1. tuple gives no guarantee about the storage order, but I require
972// storage
973// equivalent to array<_Tp, _Np>
974// 2. direct access to the element type (first template argument)
975// 3. enforces equal element type, only different _Abi types are allowed
976template <typename _Tp, typename... _Abis>
977 struct _SimdTuple;
978
979//}}}
980// __fixed_size_storage_t {{{
981template <typename _Tp, int _Np>
982 struct __fixed_size_storage;
983
984template <typename _Tp, int _Np>
985 using __fixed_size_storage_t = typename __fixed_size_storage<_Tp, _Np>::type;
986
987// }}}
988// _SimdWrapper fwd decl{{{
989template <typename _Tp, size_t _Size, typename = void_t<>>
990 struct _SimdWrapper;
991
992template <typename _Tp>
993 using _SimdWrapper8 = _SimdWrapper<_Tp, 8 / sizeof(_Tp)>;
994template <typename _Tp>
995 using _SimdWrapper16 = _SimdWrapper<_Tp, 16 / sizeof(_Tp)>;
996template <typename _Tp>
997 using _SimdWrapper32 = _SimdWrapper<_Tp, 32 / sizeof(_Tp)>;
998template <typename _Tp>
999 using _SimdWrapper64 = _SimdWrapper<_Tp, 64 / sizeof(_Tp)>;
1000
1001// }}}
1002// __is_simd_wrapper {{{
1003template <typename _Tp>
1004 struct __is_simd_wrapper : false_type {};
1005
1006template <typename _Tp, size_t _Np>
1007 struct __is_simd_wrapper<_SimdWrapper<_Tp, _Np>> : true_type {};
1008
1009template <typename _Tp>
1010 inline constexpr bool __is_simd_wrapper_v = __is_simd_wrapper<_Tp>::value;
1011
1012// }}}
1013// _BitOps {{{
1014struct _BitOps
1015{
1016 // _S_bit_iteration {{{
1017 template <typename _Tp, typename _Fp>
1018 static void
1019 _S_bit_iteration(_Tp __mask, _Fp&& __f)
1020 {
1021 static_assert(sizeof(_ULLong) >= sizeof(_Tp));
1022 conditional_t<sizeof(_Tp) <= sizeof(_UInt), _UInt, _ULLong> __k;
1023 if constexpr (is_convertible_v<_Tp, decltype(__k)>)
1024 __k = __mask;
1025 else
1026 __k = __mask.to_ullong();
1027 while(__k)
1028 {
1029 __f(std::__countr_zero(__k));
1030 __k &= (__k - 1);
1031 }
1032 }
1033
1034 //}}}
1035};
1036
1037//}}}
1038// __increment, __decrement {{{
1039template <typename _Tp = void>
1040 struct __increment
1041 { constexpr _Tp operator()(_Tp __a) const { return ++__a; } };
1042
1043template <>
1044 struct __increment<void>
1045 {
1046 template <typename _Tp>
1047 constexpr _Tp
1048 operator()(_Tp __a) const
1049 { return ++__a; }
1050 };
1051
1052template <typename _Tp = void>
1053 struct __decrement
1054 { constexpr _Tp operator()(_Tp __a) const { return --__a; } };
1055
1056template <>
1057 struct __decrement<void>
1058 {
1059 template <typename _Tp>
1060 constexpr _Tp
1061 operator()(_Tp __a) const
1062 { return --__a; }
1063 };
1064
1065// }}}
1066// _ValuePreserving(OrInt) {{{
1067template <typename _From, typename _To,
1068 typename = enable_if_t<negation<
1069 __is_narrowing_conversion<__remove_cvref_t<_From>, _To>>::value>>
1070 using _ValuePreserving = _From;
1071
1072template <typename _From, typename _To,
1073 typename _DecayedFrom = __remove_cvref_t<_From>,
1074 typename = enable_if_t<conjunction<
1075 is_convertible<_From, _To>,
1076 disjunction<
1077 is_same<_DecayedFrom, _To>, is_same<_DecayedFrom, int>,
1078 conjunction<is_same<_DecayedFrom, _UInt>, is_unsigned<_To>>,
1079 negation<__is_narrowing_conversion<_DecayedFrom, _To>>>>::value>>
1080 using _ValuePreservingOrInt = _From;
1081
1082// }}}
1083// __intrinsic_type {{{
1084template <typename _Tp, size_t _Bytes, typename = void_t<>>
1085 struct __intrinsic_type;
1086
1087template <typename _Tp, size_t _Size>
1088 using __intrinsic_type_t =
1089 typename __intrinsic_type<_Tp, _Size * sizeof(_Tp)>::type;
1090
1091template <typename _Tp>
1092 using __intrinsic_type2_t = typename __intrinsic_type<_Tp, 2>::type;
1093template <typename _Tp>
1094 using __intrinsic_type4_t = typename __intrinsic_type<_Tp, 4>::type;
1095template <typename _Tp>
1096 using __intrinsic_type8_t = typename __intrinsic_type<_Tp, 8>::type;
1097template <typename _Tp>
1098 using __intrinsic_type16_t = typename __intrinsic_type<_Tp, 16>::type;
1099template <typename _Tp>
1100 using __intrinsic_type32_t = typename __intrinsic_type<_Tp, 32>::type;
1101template <typename _Tp>
1102 using __intrinsic_type64_t = typename __intrinsic_type<_Tp, 64>::type;
1103
1104// }}}
1105// _BitMask {{{
1106template <size_t _Np, bool _Sanitized = false>
1107 struct _BitMask;
1108
1109template <size_t _Np, bool _Sanitized>
1110 struct __is_bitmask<_BitMask<_Np, _Sanitized>, void> : true_type {};
1111
1112template <size_t _Np>
1113 using _SanitizedBitMask = _BitMask<_Np, true>;
1114
1115template <size_t _Np, bool _Sanitized>
1116 struct _BitMask
1117 {
1118 static_assert(_Np > 0);
1119
1120 static constexpr size_t _NBytes = __div_roundup(_Np, __CHAR_BIT__);
1121
1122 using _Tp = conditional_t<_Np == 1, bool,
1123 make_unsigned_t<__int_with_sizeof_t<std::min(
1124 sizeof(_ULLong), std::__bit_ceil(_NBytes))>>>;
1125
1126 static constexpr int _S_array_size = __div_roundup(_NBytes, sizeof(_Tp));
1127
1128 _Tp _M_bits[_S_array_size];
1129
1130 static constexpr int _S_unused_bits
1131 = _Np == 1 ? 0 : _S_array_size * sizeof(_Tp) * __CHAR_BIT__ - _Np;
1132
1133 static constexpr _Tp _S_bitmask = +_Tp(~_Tp()) >> _S_unused_bits;
1134
1135 constexpr _BitMask() noexcept = default;
1136
1137 constexpr _BitMask(unsigned long long __x) noexcept
1138 : _M_bits{static_cast<_Tp>(__x)} {}
1139
1140 _BitMask(bitset<_Np> __x) noexcept : _BitMask(__x.to_ullong()) {}
1141
1142 constexpr _BitMask(const _BitMask&) noexcept = default;
1143
1144 template <bool _RhsSanitized, typename = enable_if_t<_RhsSanitized == false
1145 && _Sanitized == true>>
1146 constexpr _BitMask(const _BitMask<_Np, _RhsSanitized>& __rhs) noexcept
1147 : _BitMask(__rhs._M_sanitized()) {}
1148
1149 constexpr operator _SimdWrapper<bool, _Np>() const noexcept
1150 {
1151 static_assert(_S_array_size == 1);
1152 return _M_bits[0];
1153 }
1154
1155 // precondition: is sanitized
1156 constexpr _Tp
1157 _M_to_bits() const noexcept
1158 {
1159 static_assert(_S_array_size == 1);
1160 return _M_bits[0];
1161 }
1162
1163 // precondition: is sanitized
1164 constexpr unsigned long long
1165 to_ullong() const noexcept
1166 {
1167 static_assert(_S_array_size == 1);
1168 return _M_bits[0];
1169 }
1170
1171 // precondition: is sanitized
1172 constexpr unsigned long
1173 to_ulong() const noexcept
1174 {
1175 static_assert(_S_array_size == 1);
1176 return _M_bits[0];
1177 }
1178
1179 constexpr bitset<_Np>
1180 _M_to_bitset() const noexcept
1181 {
1182 static_assert(_S_array_size == 1);
1183 return _M_bits[0];
1184 }
1185
1186 constexpr decltype(auto)
1187 _M_sanitized() const noexcept
1188 {
1189 if constexpr (_Sanitized)
1190 return *this;
1191 else if constexpr (_Np == 1)
1192 return _SanitizedBitMask<_Np>(_M_bits[0]);
1193 else
1194 {
1195 _SanitizedBitMask<_Np> __r = {};
1196 for (int __i = 0; __i < _S_array_size; ++__i)
1197 __r._M_bits[__i] = _M_bits[__i];
1198 if constexpr (_S_unused_bits > 0)
1199 __r._M_bits[_S_array_size - 1] &= _S_bitmask;
1200 return __r;
1201 }
1202 }
1203
1204 template <size_t _Mp, bool _LSanitized>
1205 constexpr _BitMask<_Np + _Mp, _Sanitized>
1206 _M_prepend(_BitMask<_Mp, _LSanitized> __lsb) const noexcept
1207 {
1208 constexpr size_t _RN = _Np + _Mp;
1209 using _Rp = _BitMask<_RN, _Sanitized>;
1210 if constexpr (_Rp::_S_array_size == 1)
1211 {
1212 _Rp __r{{_M_bits[0]}};
1213 __r._M_bits[0] <<= _Mp;
1214 __r._M_bits[0] |= __lsb._M_sanitized()._M_bits[0];
1215 return __r;
1216 }
1217 else
1218 __assert_unreachable<_Rp>();
1219 }
1220
1221 // Return a new _BitMask with size _NewSize while dropping _DropLsb least
1222 // significant bits. If the operation implicitly produces a sanitized bitmask,
1223 // the result type will have _Sanitized set.
1224 template <size_t _DropLsb, size_t _NewSize = _Np - _DropLsb>
1225 constexpr auto
1226 _M_extract() const noexcept
1227 {
1228 static_assert(_Np > _DropLsb);
1229 static_assert(_DropLsb + _NewSize <= sizeof(_ULLong) * __CHAR_BIT__,
1230 "not implemented for bitmasks larger than one ullong");
1231 if constexpr (_NewSize == 1)
1232 // must sanitize because the return _Tp is bool
1233 return _SanitizedBitMask<1>(_M_bits[0] & (_Tp(1) << _DropLsb));
1234 else
1235 return _BitMask<_NewSize,
1236 ((_NewSize + _DropLsb == sizeof(_Tp) * __CHAR_BIT__
1237 && _NewSize + _DropLsb <= _Np)
1238 || ((_Sanitized || _Np == sizeof(_Tp) * __CHAR_BIT__)
1239 && _NewSize + _DropLsb >= _Np))>(_M_bits[0]
1240 >> _DropLsb);
1241 }
1242
1243 // True if all bits are set. Implicitly sanitizes if _Sanitized == false.
1244 constexpr bool
1245 all() const noexcept
1246 {
1247 if constexpr (_Np == 1)
1248 return _M_bits[0];
1249 else if constexpr (!_Sanitized)
1250 return _M_sanitized().all();
1251 else
1252 {
1253 constexpr _Tp __allbits = ~_Tp();
1254 for (int __i = 0; __i < _S_array_size - 1; ++__i)
1255 if (_M_bits[__i] != __allbits)
1256 return false;
1257 return _M_bits[_S_array_size - 1] == _S_bitmask;
1258 }
1259 }
1260
1261 // True if at least one bit is set. Implicitly sanitizes if _Sanitized ==
1262 // false.
1263 constexpr bool
1264 any() const noexcept
1265 {
1266 if constexpr (_Np == 1)
1267 return _M_bits[0];
1268 else if constexpr (!_Sanitized)
1269 return _M_sanitized().any();
1270 else
1271 {
1272 for (int __i = 0; __i < _S_array_size - 1; ++__i)
1273 if (_M_bits[__i] != 0)
1274 return true;
1275 return _M_bits[_S_array_size - 1] != 0;
1276 }
1277 }
1278
1279 // True if no bit is set. Implicitly sanitizes if _Sanitized == false.
1280 constexpr bool
1281 none() const noexcept
1282 {
1283 if constexpr (_Np == 1)
1284 return !_M_bits[0];
1285 else if constexpr (!_Sanitized)
1286 return _M_sanitized().none();
1287 else
1288 {
1289 for (int __i = 0; __i < _S_array_size - 1; ++__i)
1290 if (_M_bits[__i] != 0)
1291 return false;
1292 return _M_bits[_S_array_size - 1] == 0;
1293 }
1294 }
1295
1296 // Returns the number of set bits. Implicitly sanitizes if _Sanitized ==
1297 // false.
1298 constexpr int
1299 count() const noexcept
1300 {
1301 if constexpr (_Np == 1)
1302 return _M_bits[0];
1303 else if constexpr (!_Sanitized)
1304 return _M_sanitized().none();
1305 else
1306 {
1307 int __result = __builtin_popcountll(_M_bits[0]);
1308 for (int __i = 1; __i < _S_array_size; ++__i)
1309 __result += __builtin_popcountll(_M_bits[__i]);
1310 return __result;
1311 }
1312 }
1313
1314 // Returns the bit at offset __i as bool.
1315 constexpr bool
1316 operator[](size_t __i) const noexcept
1317 {
1318 if constexpr (_Np == 1)
1319 return _M_bits[0];
1320 else if constexpr (_S_array_size == 1)
1321 return (_M_bits[0] >> __i) & 1;
1322 else
1323 {
1324 const size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
1325 const size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
1326 return (_M_bits[__j] >> __shift) & 1;
1327 }
1328 }
1329
1330 template <size_t __i>
1331 constexpr bool
1332 operator[](_SizeConstant<__i>) const noexcept
1333 {
1334 static_assert(__i < _Np);
1335 constexpr size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
1336 constexpr size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
1337 return static_cast<bool>(_M_bits[__j] & (_Tp(1) << __shift));
1338 }
1339
1340 // Set the bit at offset __i to __x.
1341 constexpr void
1342 set(size_t __i, bool __x) noexcept
1343 {
1344 if constexpr (_Np == 1)
1345 _M_bits[0] = __x;
1346 else if constexpr (_S_array_size == 1)
1347 {
1348 _M_bits[0] &= ~_Tp(_Tp(1) << __i);
1349 _M_bits[0] |= _Tp(_Tp(__x) << __i);
1350 }
1351 else
1352 {
1353 const size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
1354 const size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
1355 _M_bits[__j] &= ~_Tp(_Tp(1) << __shift);
1356 _M_bits[__j] |= _Tp(_Tp(__x) << __shift);
1357 }
1358 }
1359
1360 template <size_t __i>
1361 constexpr void
1362 set(_SizeConstant<__i>, bool __x) noexcept
1363 {
1364 static_assert(__i < _Np);
1365 if constexpr (_Np == 1)
1366 _M_bits[0] = __x;
1367 else
1368 {
1369 constexpr size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
1370 constexpr size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
1371 constexpr _Tp __mask = ~_Tp(_Tp(1) << __shift);
1372 _M_bits[__j] &= __mask;
1373 _M_bits[__j] |= _Tp(_Tp(__x) << __shift);
1374 }
1375 }
1376
1377 // Inverts all bits. Sanitized input leads to sanitized output.
1378 constexpr _BitMask
1379 operator~() const noexcept
1380 {
1381 if constexpr (_Np == 1)
1382 return !_M_bits[0];
1383 else
1384 {
1385 _BitMask __result{};
1386 for (int __i = 0; __i < _S_array_size - 1; ++__i)
1387 __result._M_bits[__i] = ~_M_bits[__i];
1388 if constexpr (_Sanitized)
1389 __result._M_bits[_S_array_size - 1]
1390 = _M_bits[_S_array_size - 1] ^ _S_bitmask;
1391 else
1392 __result._M_bits[_S_array_size - 1] = ~_M_bits[_S_array_size - 1];
1393 return __result;
1394 }
1395 }
1396
1397 constexpr _BitMask&
1398 operator^=(const _BitMask& __b) & noexcept
1399 {
1400 __execute_n_times<_S_array_size>(
1401 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { _M_bits[__i] ^= __b._M_bits[__i]; });
1402 return *this;
1403 }
1404
1405 constexpr _BitMask&
1406 operator|=(const _BitMask& __b) & noexcept
1407 {
1408 __execute_n_times<_S_array_size>(
1409 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { _M_bits[__i] |= __b._M_bits[__i]; });
1410 return *this;
1411 }
1412
1413 constexpr _BitMask&
1414 operator&=(const _BitMask& __b) & noexcept
1415 {
1416 __execute_n_times<_S_array_size>(
1417 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { _M_bits[__i] &= __b._M_bits[__i]; });
1418 return *this;
1419 }
1420
1421 friend constexpr _BitMask
1422 operator^(const _BitMask& __a, const _BitMask& __b) noexcept
1423 {
1424 _BitMask __r = __a;
1425 __r ^= __b;
1426 return __r;
1427 }
1428
1429 friend constexpr _BitMask
1430 operator|(const _BitMask& __a, const _BitMask& __b) noexcept
1431 {
1432 _BitMask __r = __a;
1433 __r |= __b;
1434 return __r;
1435 }
1436
1437 friend constexpr _BitMask
1438 operator&(const _BitMask& __a, const _BitMask& __b) noexcept
1439 {
1440 _BitMask __r = __a;
1441 __r &= __b;
1442 return __r;
1443 }
1444
1445 _GLIBCXX_SIMD_INTRINSIC
1446 constexpr bool
1447 _M_is_constprop() const
1448 {
1449 if constexpr (_S_array_size == 0)
1450 return __builtin_constant_p(_M_bits[0]);
1451 else
1452 {
1453 for (int __i = 0; __i < _S_array_size; ++__i)
1454 if (!__builtin_constant_p(_M_bits[__i]))
1455 return false;
1456 return true;
1457 }
1458 }
1459 };
1460
1461// }}}
1462
1463// vvv ---- builtin vector types [[gnu::vector_size(N)]] and operations ---- vvv
1464// __min_vector_size {{{
1465template <typename _Tp = void>
1466 static inline constexpr int __min_vector_size = 2 * sizeof(_Tp);
1467
1468#if _GLIBCXX_SIMD_HAVE_NEON
1469template <>
1470 inline constexpr int __min_vector_size<void> = 8;
1471#else
1472template <>
1473 inline constexpr int __min_vector_size<void> = 16;
1474#endif
1475
1476// }}}
1477// __vector_type {{{
1478template <typename _Tp, size_t _Np, typename = void>
1479 struct __vector_type_n {};
1480
1481// substition failure for 0-element case
1482template <typename _Tp>
1483 struct __vector_type_n<_Tp, 0, void> {};
1484
1485// special case 1-element to be _Tp itself
1486template <typename _Tp>
1487 struct __vector_type_n<_Tp, 1, enable_if_t<__is_vectorizable_v<_Tp>>>
1488 { using type = _Tp; };
1489
1490// else, use GNU-style builtin vector types
1491template <typename _Tp, size_t _Np>
1492 struct __vector_type_n<_Tp, _Np, enable_if_t<__is_vectorizable_v<_Tp> && _Np >= 2>>
1493 {
1494 static constexpr size_t _S_Np2 = std::__bit_ceil(_Np * sizeof(_Tp));
1495
1496 static constexpr size_t _S_Bytes =
1497#ifdef __i386__
1498 // Using [[gnu::vector_size(8)]] would wreak havoc on the FPU because
1499 // those objects are passed via MMX registers and nothing ever calls EMMS.
1500 _S_Np2 == 8 ? 16 :
1501#endif
1502 _S_Np2 < __min_vector_size<_Tp> ? __min_vector_size<_Tp>
1503 : _S_Np2;
1504
1505 using type [[__gnu__::__vector_size__(_S_Bytes)]] = _Tp;
1506 };
1507
1508template <typename _Tp, size_t _Bytes, size_t = _Bytes % sizeof(_Tp)>
1509 struct __vector_type;
1510
1511template <typename _Tp, size_t _Bytes>
1512 struct __vector_type<_Tp, _Bytes, 0>
1513 : __vector_type_n<_Tp, _Bytes / sizeof(_Tp)> {};
1514
1515template <typename _Tp, size_t _Size>
1516 using __vector_type_t = typename __vector_type_n<_Tp, _Size>::type;
1517
1518template <typename _Tp>
1519 using __vector_type2_t = typename __vector_type<_Tp, 2>::type;
1520template <typename _Tp>
1521 using __vector_type4_t = typename __vector_type<_Tp, 4>::type;
1522template <typename _Tp>
1523 using __vector_type8_t = typename __vector_type<_Tp, 8>::type;
1524template <typename _Tp>
1525 using __vector_type16_t = typename __vector_type<_Tp, 16>::type;
1526template <typename _Tp>
1527 using __vector_type32_t = typename __vector_type<_Tp, 32>::type;
1528template <typename _Tp>
1529 using __vector_type64_t = typename __vector_type<_Tp, 64>::type;
1530
1531// }}}
1532// __is_vector_type {{{
1533template <typename _Tp, typename = void_t<>>
1534 struct __is_vector_type : false_type {};
1535
1536template <typename _Tp>
1537 struct __is_vector_type<
1538 _Tp, void_t<typename __vector_type<
1539 remove_reference_t<decltype(declval<_Tp>()[0])>, sizeof(_Tp)>::type>>
1540 : is_same<_Tp, typename __vector_type<
1541 remove_reference_t<decltype(declval<_Tp>()[0])>,
1542 sizeof(_Tp)>::type> {};
1543
1544template <typename _Tp>
1545 inline constexpr bool __is_vector_type_v = __is_vector_type<_Tp>::value;
1546
1547// }}}
1548// __is_intrinsic_type {{{
1549#if _GLIBCXX_SIMD_HAVE_SSE_ABI
1550template <typename _Tp>
1551 using __is_intrinsic_type = __is_vector_type<_Tp>;
1552#else // not SSE (x86)
1553template <typename _Tp, typename = void_t<>>
1554 struct __is_intrinsic_type : false_type {};
1555
1556template <typename _Tp>
1557 struct __is_intrinsic_type<
1558 _Tp, void_t<typename __intrinsic_type<
1559 remove_reference_t<decltype(declval<_Tp>()[0])>, sizeof(_Tp)>::type>>
1560 : is_same<_Tp, typename __intrinsic_type<
1561 remove_reference_t<decltype(declval<_Tp>()[0])>,
1562 sizeof(_Tp)>::type> {};
1563#endif
1564
1565template <typename _Tp>
1566 inline constexpr bool __is_intrinsic_type_v = __is_intrinsic_type<_Tp>::value;
1567
1568// }}}
1569// _VectorTraits{{{
1570template <typename _Tp, typename = void_t<>>
1571 struct _VectorTraitsImpl;
1572
1573template <typename _Tp>
1574 struct _VectorTraitsImpl<_Tp, enable_if_t<__is_vector_type_v<_Tp>
1575 || __is_intrinsic_type_v<_Tp>>>
1576 {
1577 using type = _Tp;
1578 using value_type = remove_reference_t<decltype(declval<_Tp>()[0])>;
1579 static constexpr int _S_full_size = sizeof(_Tp) / sizeof(value_type);
1580 using _Wrapper = _SimdWrapper<value_type, _S_full_size>;
1581 template <typename _Up, int _W = _S_full_size>
1582 static constexpr bool _S_is
1583 = is_same_v<value_type, _Up> && _W == _S_full_size;
1584 };
1585
1586template <typename _Tp, size_t _Np>
1587 struct _VectorTraitsImpl<_SimdWrapper<_Tp, _Np>,
1588 void_t<__vector_type_t<_Tp, _Np>>>
1589 {
1590 using type = __vector_type_t<_Tp, _Np>;
1591 using value_type = _Tp;
1592 static constexpr int _S_full_size = sizeof(type) / sizeof(value_type);
1593 using _Wrapper = _SimdWrapper<_Tp, _Np>;
1594 static constexpr bool _S_is_partial = (_Np == _S_full_size);
1595 static constexpr int _S_partial_width = _Np;
1596 template <typename _Up, int _W = _S_full_size>
1597 static constexpr bool _S_is
1598 = is_same_v<value_type, _Up>&& _W == _S_full_size;
1599 };
1600
1601template <typename _Tp, typename = typename _VectorTraitsImpl<_Tp>::type>
1602 using _VectorTraits = _VectorTraitsImpl<_Tp>;
1603
1604// }}}
1605// __as_vector{{{
1606template <typename _V>
1607 _GLIBCXX_SIMD_INTRINSIC constexpr auto
1608 __as_vector(_V __x)
1609 {
1610 if constexpr (__is_vector_type_v<_V>)
1611 return __x;
1612 else if constexpr (is_simd<_V>::value || is_simd_mask<_V>::value)
1613 return __data(__x)._M_data;
1614 else if constexpr (__is_vectorizable_v<_V>)
1615 return __vector_type_t<_V, 2>{__x};
1616 else
1617 return __x._M_data;
1618 }
1619
1620// }}}
1621// __as_wrapper{{{
1622template <size_t _Np = 0, typename _V>
1623 _GLIBCXX_SIMD_INTRINSIC constexpr auto
1624 __as_wrapper(_V __x)
1625 {
1626 if constexpr (__is_vector_type_v<_V>)
1627 return _SimdWrapper<typename _VectorTraits<_V>::value_type,
1628 (_Np > 0 ? _Np : _VectorTraits<_V>::_S_full_size)>(__x);
1629 else if constexpr (is_simd<_V>::value || is_simd_mask<_V>::value)
1630 {
1631 static_assert(_V::size() == _Np);
1632 return __data(__x);
1633 }
1634 else
1635 {
1636 static_assert(_V::_S_size == _Np);
1637 return __x;
1638 }
1639 }
1640
1641// }}}
1642// __intrin_bitcast{{{
1643template <typename _To, typename _From>
1644 _GLIBCXX_SIMD_INTRINSIC constexpr _To
1645 __intrin_bitcast(_From __v)
1646 {
1647 static_assert((__is_vector_type_v<_From> || __is_intrinsic_type_v<_From>)
1648 && (__is_vector_type_v<_To> || __is_intrinsic_type_v<_To>));
1649 if constexpr (sizeof(_To) == sizeof(_From))
1650 return reinterpret_cast<_To>(__v);
1651 else if constexpr (sizeof(_From) > sizeof(_To))
1652 if constexpr (sizeof(_To) >= 16)
1653 return reinterpret_cast<const __may_alias<_To>&>(__v);
1654 else
1655 {
1656 _To __r;
1657 __builtin_memcpy(&__r, &__v, sizeof(_To));
1658 return __r;
1659 }
1660#if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__
1661 else if constexpr (__have_avx && sizeof(_From) == 16 && sizeof(_To) == 32)
1662 return reinterpret_cast<_To>(__builtin_ia32_ps256_ps(
1663 reinterpret_cast<__vector_type_t<float, 4>>(__v)));
1664 else if constexpr (__have_avx512f && sizeof(_From) == 16
1665 && sizeof(_To) == 64)
1666 return reinterpret_cast<_To>(__builtin_ia32_ps512_ps(
1667 reinterpret_cast<__vector_type_t<float, 4>>(__v)));
1668 else if constexpr (__have_avx512f && sizeof(_From) == 32
1669 && sizeof(_To) == 64)
1670 return reinterpret_cast<_To>(__builtin_ia32_ps512_256ps(
1671 reinterpret_cast<__vector_type_t<float, 8>>(__v)));
1672#endif // _GLIBCXX_SIMD_X86INTRIN
1673 else if constexpr (sizeof(__v) <= 8)
1674 return reinterpret_cast<_To>(
1675 __vector_type_t<__int_for_sizeof_t<_From>, sizeof(_To) / sizeof(_From)>{
1676 reinterpret_cast<__int_for_sizeof_t<_From>>(__v)});
1677 else
1678 {
1679 static_assert(sizeof(_To) > sizeof(_From));
1680 _To __r = {};
1681 __builtin_memcpy(&__r, &__v, sizeof(_From));
1682 return __r;
1683 }
1684 }
1685
1686// }}}
1687// __vector_bitcast{{{
1688template <typename _To, size_t _NN = 0, typename _From,
1689 typename _FromVT = _VectorTraits<_From>,
1690 size_t _Np = _NN == 0 ? sizeof(_From) / sizeof(_To) : _NN>
1691 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_To, _Np>
1692 __vector_bitcast(_From __x)
1693 {
1694 using _R = __vector_type_t<_To, _Np>;
1695 return __intrin_bitcast<_R>(__x);
1696 }
1697
1698template <typename _To, size_t _NN = 0, typename _Tp, size_t _Nx,
1699 size_t _Np
1700 = _NN == 0 ? sizeof(_SimdWrapper<_Tp, _Nx>) / sizeof(_To) : _NN>
1701 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_To, _Np>
1702 __vector_bitcast(const _SimdWrapper<_Tp, _Nx>& __x)
1703 {
1704 static_assert(_Np > 1);
1705 return __intrin_bitcast<__vector_type_t<_To, _Np>>(__x._M_data);
1706 }
1707
1708// }}}
1709// __convert_x86 declarations {{{
1710#ifdef _GLIBCXX_SIMD_WORKAROUND_PR85048
1711template <typename _To, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
1712 _To __convert_x86(_Tp);
1713
1714template <typename _To, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
1715 _To __convert_x86(_Tp, _Tp);
1716
1717template <typename _To, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
1718 _To __convert_x86(_Tp, _Tp, _Tp, _Tp);
1719
1720template <typename _To, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
1721 _To __convert_x86(_Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp);
1722
1723template <typename _To, typename _Tp, typename _TVT = _VectorTraits<_Tp>>
1724 _To __convert_x86(_Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp,
1725 _Tp, _Tp, _Tp, _Tp);
1726#endif // _GLIBCXX_SIMD_WORKAROUND_PR85048
1727
1728//}}}
1729// __bit_cast {{{
1730template <typename _To, typename _From>
1731 _GLIBCXX_SIMD_INTRINSIC constexpr _To
1732 __bit_cast(const _From __x)
1733 {
1734#if __has_builtin(__builtin_bit_cast)
1735 return __builtin_bit_cast(_To, __x);
1736#else
1737 static_assert(sizeof(_To) == sizeof(_From));
1738 constexpr bool __to_is_vectorizable
1739 = is_arithmetic_v<_To> || is_enum_v<_To>;
1740 constexpr bool __from_is_vectorizable
1741 = is_arithmetic_v<_From> || is_enum_v<_From>;
1742 if constexpr (__is_vector_type_v<_To> && __is_vector_type_v<_From>)
1743 return reinterpret_cast<_To>(__x);
1744 else if constexpr (__is_vector_type_v<_To> && __from_is_vectorizable)
1745 {
1746 using _FV [[gnu::vector_size(sizeof(_From))]] = _From;
1747 return reinterpret_cast<_To>(_FV{__x});
1748 }
1749 else if constexpr (__to_is_vectorizable && __from_is_vectorizable)
1750 {
1751 using _TV [[gnu::vector_size(sizeof(_To))]] = _To;
1752 using _FV [[gnu::vector_size(sizeof(_From))]] = _From;
1753 return reinterpret_cast<_TV>(_FV{__x})[0];
1754 }
1755 else if constexpr (__to_is_vectorizable && __is_vector_type_v<_From>)
1756 {
1757 using _TV [[gnu::vector_size(sizeof(_To))]] = _To;
1758 return reinterpret_cast<_TV>(__x)[0];
1759 }
1760 else
1761 {
1762 _To __r;
1763 __builtin_memcpy(reinterpret_cast<char*>(&__r),
1764 reinterpret_cast<const char*>(&__x), sizeof(_To));
1765 return __r;
1766 }
1767#endif
1768 }
1769
1770// }}}
1771// __to_intrin {{{
1772template <typename _Tp, typename _TVT = _VectorTraits<_Tp>,
1773 typename _R = __intrinsic_type_t<typename _TVT::value_type, _TVT::_S_full_size>>
1774 _GLIBCXX_SIMD_INTRINSIC constexpr _R
1775 __to_intrin(_Tp __x)
1776 {
1777 static_assert(sizeof(__x) <= sizeof(_R),
1778 "__to_intrin may never drop values off the end");
1779 if constexpr (sizeof(__x) == sizeof(_R))
1780 return reinterpret_cast<_R>(__as_vector(__x));
1781 else
1782 {
1783 using _Up = __int_for_sizeof_t<_Tp>;
1784 return reinterpret_cast<_R>(
1785 __vector_type_t<_Up, sizeof(_R) / sizeof(_Up)>{__bit_cast<_Up>(__x)});
1786 }
1787 }
1788
1789// }}}
1790// __make_vector{{{
1791template <typename _Tp, typename... _Args>
1792 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, sizeof...(_Args)>
1793 __make_vector(const _Args&... __args)
1794 { return __vector_type_t<_Tp, sizeof...(_Args)>{static_cast<_Tp>(__args)...}; }
1795
1796// }}}
1797// __vector_broadcast{{{
1798template <size_t _Np, typename _Tp, size_t... _I>
1799 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np>
1800 __vector_broadcast_impl(_Tp __x, index_sequence<_I...>)
1801 { return __vector_type_t<_Tp, _Np>{((void)_I, __x)...}; }
1802
1803template <size_t _Np, typename _Tp>
1804 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np>
1805 __vector_broadcast(_Tp __x)
1806 { return __vector_broadcast_impl<_Np, _Tp>(__x, make_index_sequence<_Np>()); }
1807
1808// }}}
1809// __generate_vector{{{
1810 template <typename _Tp, size_t _Np, typename _Gp, size_t... _I>
1811 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np>
1812 __generate_vector_impl(_Gp&& __gen, index_sequence<_I...>)
1813 { return __vector_type_t<_Tp, _Np>{ static_cast<_Tp>(__gen(_SizeConstant<_I>()))...}; }
1814
1815template <typename _V, typename _VVT = _VectorTraits<_V>, typename _Gp>
1816 _GLIBCXX_SIMD_INTRINSIC constexpr _V
1817 __generate_vector(_Gp&& __gen)
1818 {
1819 if constexpr (__is_vector_type_v<_V>)
1820 return __generate_vector_impl<typename _VVT::value_type,
1821 _VVT::_S_full_size>(
1822 static_cast<_Gp&&>(__gen), make_index_sequence<_VVT::_S_full_size>());
1823 else
1824 return __generate_vector_impl<typename _VVT::value_type,
1825 _VVT::_S_partial_width>(
1826 static_cast<_Gp&&>(__gen),
1827 make_index_sequence<_VVT::_S_partial_width>());
1828 }
1829
1830template <typename _Tp, size_t _Np, typename _Gp>
1831 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np>
1832 __generate_vector(_Gp&& __gen)
1833 {
1834 return __generate_vector_impl<_Tp, _Np>(static_cast<_Gp&&>(__gen),
1835 make_index_sequence<_Np>());
1836 }
1837
1838// }}}
1839// __xor{{{
1840template <typename _TW>
1841 _GLIBCXX_SIMD_INTRINSIC constexpr _TW
1842 __xor(_TW __a, _TW __b) noexcept
1843 {
1844 if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
1845 {
1846 using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW,
1847 _VectorTraitsImpl<_TW>>::value_type;
1848 if constexpr (is_floating_point_v<_Tp>)
1849 {
1850 using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
1851 return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a)
1852 ^ __vector_bitcast<_Ip>(__b));
1853 }
1854 else if constexpr (__is_vector_type_v<_TW>)
1855 return __a ^ __b;
1856 else
1857 return __a._M_data ^ __b._M_data;
1858 }
1859 else
1860 return __a ^ __b;
1861 }
1862
1863// }}}
1864// __or{{{
1865template <typename _TW>
1866 _GLIBCXX_SIMD_INTRINSIC constexpr _TW
1867 __or(_TW __a, _TW __b) noexcept
1868 {
1869 if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
1870 {
1871 using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW,
1872 _VectorTraitsImpl<_TW>>::value_type;
1873 if constexpr (is_floating_point_v<_Tp>)
1874 {
1875 using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
1876 return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a)
1877 | __vector_bitcast<_Ip>(__b));
1878 }
1879 else if constexpr (__is_vector_type_v<_TW>)
1880 return __a | __b;
1881 else
1882 return __a._M_data | __b._M_data;
1883 }
1884 else
1885 return __a | __b;
1886 }
1887
1888// }}}
1889// __and{{{
1890template <typename _TW>
1891 _GLIBCXX_SIMD_INTRINSIC constexpr _TW
1892 __and(_TW __a, _TW __b) noexcept
1893 {
1894 if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
1895 {
1896 using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW,
1897 _VectorTraitsImpl<_TW>>::value_type;
1898 if constexpr (is_floating_point_v<_Tp>)
1899 {
1900 using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
1901 return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a)
1902 & __vector_bitcast<_Ip>(__b));
1903 }
1904 else if constexpr (__is_vector_type_v<_TW>)
1905 return __a & __b;
1906 else
1907 return __a._M_data & __b._M_data;
1908 }
1909 else
1910 return __a & __b;
1911 }
1912
1913// }}}
1914// __andnot{{{
1915#if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__
1916static constexpr struct
1917{
1918 _GLIBCXX_SIMD_INTRINSIC __v4sf
1919 operator()(__v4sf __a, __v4sf __b) const noexcept
1920 { return __builtin_ia32_andnps(__a, __b); }
1921
1922 _GLIBCXX_SIMD_INTRINSIC __v2df
1923 operator()(__v2df __a, __v2df __b) const noexcept
1924 { return __builtin_ia32_andnpd(__a, __b); }
1925
1926 _GLIBCXX_SIMD_INTRINSIC __v2di
1927 operator()(__v2di __a, __v2di __b) const noexcept
1928 { return __builtin_ia32_pandn128(__a, __b); }
1929
1930 _GLIBCXX_SIMD_INTRINSIC __v8sf
1931 operator()(__v8sf __a, __v8sf __b) const noexcept
1932 { return __builtin_ia32_andnps256(__a, __b); }
1933
1934 _GLIBCXX_SIMD_INTRINSIC __v4df
1935 operator()(__v4df __a, __v4df __b) const noexcept
1936 { return __builtin_ia32_andnpd256(__a, __b); }
1937
1938 _GLIBCXX_SIMD_INTRINSIC __v4di
1939 operator()(__v4di __a, __v4di __b) const noexcept
1940 {
1941 if constexpr (__have_avx2)
1942 return __builtin_ia32_andnotsi256(__a, __b);
1943 else
1944 return reinterpret_cast<__v4di>(
1945 __builtin_ia32_andnpd256(reinterpret_cast<__v4df>(__a),
1946 reinterpret_cast<__v4df>(__b)));
1947 }
1948
1949 _GLIBCXX_SIMD_INTRINSIC __v16sf
1950 operator()(__v16sf __a, __v16sf __b) const noexcept
1951 {
1952 if constexpr (__have_avx512dq)
1953 return _mm512_andnot_ps(__a, __b);
1954 else
1955 return reinterpret_cast<__v16sf>(
1956 _mm512_andnot_si512(reinterpret_cast<__v8di>(__a),
1957 reinterpret_cast<__v8di>(__b)));
1958 }
1959
1960 _GLIBCXX_SIMD_INTRINSIC __v8df
1961 operator()(__v8df __a, __v8df __b) const noexcept
1962 {
1963 if constexpr (__have_avx512dq)
1964 return _mm512_andnot_pd(__a, __b);
1965 else
1966 return reinterpret_cast<__v8df>(
1967 _mm512_andnot_si512(reinterpret_cast<__v8di>(__a),
1968 reinterpret_cast<__v8di>(__b)));
1969 }
1970
1971 _GLIBCXX_SIMD_INTRINSIC __v8di
1972 operator()(__v8di __a, __v8di __b) const noexcept
1973 { return _mm512_andnot_si512(__a, __b); }
1974} _S_x86_andnot;
1975#endif // _GLIBCXX_SIMD_X86INTRIN && !__clang__
1976
1977template <typename _TW>
1978 _GLIBCXX_SIMD_INTRINSIC constexpr _TW
1979 __andnot(_TW __a, _TW __b) noexcept
1980 {
1981 if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
1982 {
1983 using _TVT = conditional_t<__is_simd_wrapper_v<_TW>, _TW,
1984 _VectorTraitsImpl<_TW>>;
1985 using _Tp = typename _TVT::value_type;
1986#if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__
1987 if constexpr (sizeof(_TW) >= 16)
1988 {
1989 const auto __ai = __to_intrin(__a);
1990 const auto __bi = __to_intrin(__b);
1991 if (!__builtin_is_constant_evaluated()
1992 && !(__builtin_constant_p(__ai) && __builtin_constant_p(__bi)))
1993 {
1994 const auto __r = _S_x86_andnot(__ai, __bi);
1995 if constexpr (is_convertible_v<decltype(__r), _TW>)
1996 return __r;
1997 else
1998 return reinterpret_cast<typename _TVT::type>(__r);
1999 }
2000 }
2001#endif // _GLIBCXX_SIMD_X86INTRIN
2002 using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
2003 return __vector_bitcast<_Tp>(~__vector_bitcast<_Ip>(__a)
2004 & __vector_bitcast<_Ip>(__b));
2005 }
2006 else
2007 return ~__a & __b;
2008 }
2009
2010// }}}
2011// __not{{{
2012template <typename _Tp, typename _TVT = _VectorTraits<_Tp>>
2013 _GLIBCXX_SIMD_INTRINSIC constexpr _Tp
2014 __not(_Tp __a) noexcept
2015 {
2016 if constexpr (is_floating_point_v<typename _TVT::value_type>)
2017 return reinterpret_cast<typename _TVT::type>(
2018 ~__vector_bitcast<unsigned>(__a));
2019 else
2020 return ~__a;
2021 }
2022
2023// }}}
2024// __concat{{{
2025template <typename _Tp, typename _TVT = _VectorTraits<_Tp>,
2026 typename _R = __vector_type_t<typename _TVT::value_type, _TVT::_S_full_size * 2>>
2027 constexpr _R
2028 __concat(_Tp a_, _Tp b_)
2029 {
2030#ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_1
2031 using _W
2032 = conditional_t<is_floating_point_v<typename _TVT::value_type>, double,
2033 conditional_t<(sizeof(_Tp) >= 2 * sizeof(long long)),
2034 long long, typename _TVT::value_type>>;
2035 constexpr int input_width = sizeof(_Tp) / sizeof(_W);
2036 const auto __a = __vector_bitcast<_W>(a_);
2037 const auto __b = __vector_bitcast<_W>(b_);
2038 using _Up = __vector_type_t<_W, sizeof(_R) / sizeof(_W)>;
2039#else
2040 constexpr int input_width = _TVT::_S_full_size;
2041 const _Tp& __a = a_;
2042 const _Tp& __b = b_;
2043 using _Up = _R;
2044#endif
2045 if constexpr (input_width == 2)
2046 return reinterpret_cast<_R>(_Up{__a[0], __a[1], __b[0], __b[1]});
2047 else if constexpr (input_width == 4)
2048 return reinterpret_cast<_R>(
2049 _Up{__a[0], __a[1], __a[2], __a[3], __b[0], __b[1], __b[2], __b[3]});
2050 else if constexpr (input_width == 8)
2051 return reinterpret_cast<_R>(
2052 _Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6], __a[7],
2053 __b[0], __b[1], __b[2], __b[3], __b[4], __b[5], __b[6], __b[7]});
2054 else if constexpr (input_width == 16)
2055 return reinterpret_cast<_R>(
2056 _Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6],
2057 __a[7], __a[8], __a[9], __a[10], __a[11], __a[12], __a[13],
2058 __a[14], __a[15], __b[0], __b[1], __b[2], __b[3], __b[4],
2059 __b[5], __b[6], __b[7], __b[8], __b[9], __b[10], __b[11],
2060 __b[12], __b[13], __b[14], __b[15]});
2061 else if constexpr (input_width == 32)
2062 return reinterpret_cast<_R>(
2063 _Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6],
2064 __a[7], __a[8], __a[9], __a[10], __a[11], __a[12], __a[13],
2065 __a[14], __a[15], __a[16], __a[17], __a[18], __a[19], __a[20],
2066 __a[21], __a[22], __a[23], __a[24], __a[25], __a[26], __a[27],
2067 __a[28], __a[29], __a[30], __a[31], __b[0], __b[1], __b[2],
2068 __b[3], __b[4], __b[5], __b[6], __b[7], __b[8], __b[9],
2069 __b[10], __b[11], __b[12], __b[13], __b[14], __b[15], __b[16],
2070 __b[17], __b[18], __b[19], __b[20], __b[21], __b[22], __b[23],
2071 __b[24], __b[25], __b[26], __b[27], __b[28], __b[29], __b[30],
2072 __b[31]});
2073 }
2074
2075// }}}
2076// __zero_extend {{{
2077template <typename _Tp, typename _TVT = _VectorTraits<_Tp>>
2078 struct _ZeroExtendProxy
2079 {
2080 using value_type = typename _TVT::value_type;
2081 static constexpr size_t _Np = _TVT::_S_full_size;
2082 const _Tp __x;
2083
2084 template <typename _To, typename _ToVT = _VectorTraits<_To>,
2085 typename
2086 = enable_if_t<is_same_v<typename _ToVT::value_type, value_type>>>
2087 _GLIBCXX_SIMD_INTRINSIC operator _To() const
2088 {
2089 constexpr size_t _ToN = _ToVT::_S_full_size;
2090 if constexpr (_ToN == _Np)
2091 return __x;
2092 else if constexpr (_ToN == 2 * _Np)
2093 {
2094#ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_3
2095 if constexpr (__have_avx && _TVT::template _S_is<float, 4>)
2096 return __vector_bitcast<value_type>(
2097 _mm256_insertf128_ps(__m256(), __x, 0));
2098 else if constexpr (__have_avx && _TVT::template _S_is<double, 2>)
2099 return __vector_bitcast<value_type>(
2100 _mm256_insertf128_pd(__m256d(), __x, 0));
2101 else if constexpr (__have_avx2 && _Np * sizeof(value_type) == 16)
2102 return __vector_bitcast<value_type>(
2103 _mm256_insertf128_si256(__m256i(), __to_intrin(__x), 0));
2104 else if constexpr (__have_avx512f && _TVT::template _S_is<float, 8>)
2105 {
2106 if constexpr (__have_avx512dq)
2107 return __vector_bitcast<value_type>(
2108 _mm512_insertf32x8(__m512(), __x, 0));
2109 else
2110 return reinterpret_cast<__m512>(
2111 _mm512_insertf64x4(__m512d(),
2112 reinterpret_cast<__m256d>(__x), 0));
2113 }
2114 else if constexpr (__have_avx512f
2115 && _TVT::template _S_is<double, 4>)
2116 return __vector_bitcast<value_type>(
2117 _mm512_insertf64x4(__m512d(), __x, 0));
2118 else if constexpr (__have_avx512f && _Np * sizeof(value_type) == 32)
2119 return __vector_bitcast<value_type>(
2120 _mm512_inserti64x4(__m512i(), __to_intrin(__x), 0));
2121#endif
2122 return __concat(__x, _Tp());
2123 }
2124 else if constexpr (_ToN == 4 * _Np)
2125 {
2126#ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_3
2127 if constexpr (__have_avx512dq && _TVT::template _S_is<double, 2>)
2128 {
2129 return __vector_bitcast<value_type>(
2130 _mm512_insertf64x2(__m512d(), __x, 0));
2131 }
2132 else if constexpr (__have_avx512f
2133 && is_floating_point_v<value_type>)
2134 {
2135 return __vector_bitcast<value_type>(
2136 _mm512_insertf32x4(__m512(), reinterpret_cast<__m128>(__x),
2137 0));
2138 }
2139 else if constexpr (__have_avx512f && _Np * sizeof(value_type) == 16)
2140 {
2141 return __vector_bitcast<value_type>(
2142 _mm512_inserti32x4(__m512i(), __to_intrin(__x), 0));
2143 }
2144#endif
2145 return __concat(__concat(__x, _Tp()),
2146 __vector_type_t<value_type, _Np * 2>());
2147 }
2148 else if constexpr (_ToN == 8 * _Np)
2149 return __concat(operator __vector_type_t<value_type, _Np * 4>(),
2150 __vector_type_t<value_type, _Np * 4>());
2151 else if constexpr (_ToN == 16 * _Np)
2152 return __concat(operator __vector_type_t<value_type, _Np * 8>(),
2153 __vector_type_t<value_type, _Np * 8>());
2154 else
2155 __assert_unreachable<_Tp>();
2156 }
2157 };
2158
2159template <typename _Tp, typename _TVT = _VectorTraits<_Tp>>
2160 _GLIBCXX_SIMD_INTRINSIC _ZeroExtendProxy<_Tp, _TVT>
2161 __zero_extend(_Tp __x)
2162 { return {__x}; }
2163
2164// }}}
2165// __extract<_Np, By>{{{
2166template <int _Offset,
2167 int _SplitBy,
2168 typename _Tp,
2169 typename _TVT = _VectorTraits<_Tp>,
2170 typename _R = __vector_type_t<typename _TVT::value_type, _TVT::_S_full_size / _SplitBy>>
2171 _GLIBCXX_SIMD_INTRINSIC constexpr _R
2172 __extract(_Tp __in)
2173 {
2174 using value_type = typename _TVT::value_type;
2175#if _GLIBCXX_SIMD_X86INTRIN // {{{
2176 if constexpr (sizeof(_Tp) == 64 && _SplitBy == 4 && _Offset > 0)
2177 {
2178 if constexpr (__have_avx512dq && is_same_v<double, value_type>)
2179 return _mm512_extractf64x2_pd(__to_intrin(__in), _Offset);
2180 else if constexpr (is_floating_point_v<value_type>)
2181 return __vector_bitcast<value_type>(
2182 _mm512_extractf32x4_ps(__intrin_bitcast<__m512>(__in), _Offset));
2183 else
2184 return reinterpret_cast<_R>(
2185 _mm512_extracti32x4_epi32(__intrin_bitcast<__m512i>(__in),
2186 _Offset));
2187 }
2188 else
2189#endif // _GLIBCXX_SIMD_X86INTRIN }}}
2190 {
2191#ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_1
2192 using _W = conditional_t<
2193 is_floating_point_v<value_type>, double,
2194 conditional_t<(sizeof(_R) >= 16), long long, value_type>>;
2195 static_assert(sizeof(_R) % sizeof(_W) == 0);
2196 constexpr int __return_width = sizeof(_R) / sizeof(_W);
2197 using _Up = __vector_type_t<_W, __return_width>;
2198 const auto __x = __vector_bitcast<_W>(__in);
2199#else
2200 constexpr int __return_width = _TVT::_S_full_size / _SplitBy;
2201 using _Up = _R;
2202 const __vector_type_t<value_type, _TVT::_S_full_size>& __x
2203 = __in; // only needed for _Tp = _SimdWrapper<value_type, _Np>
2204#endif
2205 constexpr int _O = _Offset * __return_width;
2206 return __call_with_subscripts<__return_width, _O>(
2207 __x, [](auto... __entries) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
2208 return reinterpret_cast<_R>(_Up{__entries...});
2209 });
2210 }
2211 }
2212
2213// }}}
2214// __lo/__hi64[z]{{{
2215template <typename _Tp,
2216 typename _R = __vector_type8_t<typename _VectorTraits<_Tp>::value_type>>
2217 _GLIBCXX_SIMD_INTRINSIC constexpr _R
2218 __lo64(_Tp __x)
2219 {
2220 _R __r{};
2221 __builtin_memcpy(&__r, &__x, 8);
2222 return __r;
2223 }
2224
2225template <typename _Tp,
2226 typename _R = __vector_type8_t<typename _VectorTraits<_Tp>::value_type>>
2227 _GLIBCXX_SIMD_INTRINSIC constexpr _R
2228 __hi64(_Tp __x)
2229 {
2230 static_assert(sizeof(_Tp) == 16, "use __hi64z if you meant it");
2231 _R __r{};
2232 __builtin_memcpy(&__r, reinterpret_cast<const char*>(&__x) + 8, 8);
2233 return __r;
2234 }
2235
2236template <typename _Tp,
2237 typename _R = __vector_type8_t<typename _VectorTraits<_Tp>::value_type>>
2238 _GLIBCXX_SIMD_INTRINSIC constexpr _R
2239 __hi64z([[maybe_unused]] _Tp __x)
2240 {
2241 _R __r{};
2242 if constexpr (sizeof(_Tp) == 16)
2243 __builtin_memcpy(&__r, reinterpret_cast<const char*>(&__x) + 8, 8);
2244 return __r;
2245 }
2246
2247// }}}
2248// __lo/__hi128{{{
2249template <typename _Tp>
2250 _GLIBCXX_SIMD_INTRINSIC constexpr auto
2251 __lo128(_Tp __x)
2252 { return __extract<0, sizeof(_Tp) / 16>(__x); }
2253
2254template <typename _Tp>
2255 _GLIBCXX_SIMD_INTRINSIC constexpr auto
2256 __hi128(_Tp __x)
2257 {
2258 static_assert(sizeof(__x) == 32);
2259 return __extract<1, 2>(__x);
2260 }
2261
2262// }}}
2263// __lo/__hi256{{{
2264template <typename _Tp>
2265 _GLIBCXX_SIMD_INTRINSIC constexpr auto
2266 __lo256(_Tp __x)
2267 {
2268 static_assert(sizeof(__x) == 64);
2269 return __extract<0, 2>(__x);
2270 }
2271
2272template <typename _Tp>
2273 _GLIBCXX_SIMD_INTRINSIC constexpr auto
2274 __hi256(_Tp __x)
2275 {
2276 static_assert(sizeof(__x) == 64);
2277 return __extract<1, 2>(__x);
2278 }
2279
2280// }}}
2281// __auto_bitcast{{{
2282template <typename _Tp>
2283 struct _AutoCast
2284 {
2285 static_assert(__is_vector_type_v<_Tp>);
2286
2287 const _Tp __x;
2288
2289 template <typename _Up, typename _UVT = _VectorTraits<_Up>>
2290 _GLIBCXX_SIMD_INTRINSIC constexpr operator _Up() const
2291 { return __intrin_bitcast<typename _UVT::type>(__x); }
2292 };
2293
2294template <typename _Tp>
2295 _GLIBCXX_SIMD_INTRINSIC constexpr _AutoCast<_Tp>
2296 __auto_bitcast(const _Tp& __x)
2297 { return {__x}; }
2298
2299template <typename _Tp, size_t _Np>
2300 _GLIBCXX_SIMD_INTRINSIC constexpr
2301 _AutoCast<typename _SimdWrapper<_Tp, _Np>::_BuiltinType>
2302 __auto_bitcast(const _SimdWrapper<_Tp, _Np>& __x)
2303 { return {__x._M_data}; }
2304
2305// }}}
2306// ^^^ ---- builtin vector types [[gnu::vector_size(N)]] and operations ---- ^^^
2307
2308#if _GLIBCXX_SIMD_HAVE_SSE_ABI
2309// __bool_storage_member_type{{{
2310#if _GLIBCXX_SIMD_HAVE_AVX512F && _GLIBCXX_SIMD_X86INTRIN
2311template <size_t _Size>
2312 struct __bool_storage_member_type
2313 {
2314 static_assert((_Size & (_Size - 1)) != 0,
2315 "This trait may only be used for non-power-of-2 sizes. "
2316 "Power-of-2 sizes must be specialized.");
2317 using type =
2318 typename __bool_storage_member_type<std::__bit_ceil(_Size)>::type;
2319 };
2320
2321template <>
2322 struct __bool_storage_member_type<1> { using type = bool; };
2323
2324template <>
2325 struct __bool_storage_member_type<2> { using type = __mmask8; };
2326
2327template <>
2328 struct __bool_storage_member_type<4> { using type = __mmask8; };
2329
2330template <>
2331 struct __bool_storage_member_type<8> { using type = __mmask8; };
2332
2333template <>
2334 struct __bool_storage_member_type<16> { using type = __mmask16; };
2335
2336template <>
2337 struct __bool_storage_member_type<32> { using type = __mmask32; };
2338
2339template <>
2340 struct __bool_storage_member_type<64> { using type = __mmask64; };
2341#endif // _GLIBCXX_SIMD_HAVE_AVX512F
2342
2343// }}}
2344// __intrinsic_type (x86){{{
2345// the following excludes bool via __is_vectorizable
2346#if _GLIBCXX_SIMD_HAVE_SSE
2347template <typename _Tp, size_t _Bytes>
2348 struct __intrinsic_type<_Tp, _Bytes, enable_if_t<__is_vectorizable_v<_Tp> && _Bytes <= 64>>
2349 {
2350 static_assert(!is_same_v<_Tp, long double>,
2351 "no __intrinsic_type support for long double on x86");
2352
2353 static constexpr size_t _S_VBytes = _Bytes <= 16 ? 16 : _Bytes <= 32 ? 32 : 64;
2354
2355 using type [[__gnu__::__vector_size__(_S_VBytes)]]
2356 = conditional_t<is_integral_v<_Tp>, long long int, _Tp>;
2357 };
2358#endif // _GLIBCXX_SIMD_HAVE_SSE
2359
2360// }}}
2361#endif // _GLIBCXX_SIMD_HAVE_SSE_ABI
2362// __intrinsic_type (ARM){{{
2363#if _GLIBCXX_SIMD_HAVE_NEON
2364template <>
2365 struct __intrinsic_type<float, 8, void>
2366 { using type = float32x2_t; };
2367
2368template <>
2369 struct __intrinsic_type<float, 16, void>
2370 { using type = float32x4_t; };
2371
2372#if _GLIBCXX_SIMD_HAVE_NEON_A64
2373template <>
2374 struct __intrinsic_type<double, 8, void>
2375 { using type = float64x1_t; };
2376
2377template <>
2378 struct __intrinsic_type<double, 16, void>
2379 { using type = float64x2_t; };
2380#endif
2381
2382#define _GLIBCXX_SIMD_ARM_INTRIN(_Bits, _Np) \
2383template <> \
2384 struct __intrinsic_type<__int_with_sizeof_t<_Bits / 8>, \
2385 _Np * _Bits / 8, void> \
2386 { using type = int##_Bits##x##_Np##_t; }; \
2387template <> \
2388 struct __intrinsic_type<make_unsigned_t<__int_with_sizeof_t<_Bits / 8>>, \
2389 _Np * _Bits / 8, void> \
2390 { using type = uint##_Bits##x##_Np##_t; }
2391_GLIBCXX_SIMD_ARM_INTRIN(8, 8);
2392_GLIBCXX_SIMD_ARM_INTRIN(8, 16);
2393_GLIBCXX_SIMD_ARM_INTRIN(16, 4);
2394_GLIBCXX_SIMD_ARM_INTRIN(16, 8);
2395_GLIBCXX_SIMD_ARM_INTRIN(32, 2);
2396_GLIBCXX_SIMD_ARM_INTRIN(32, 4);
2397_GLIBCXX_SIMD_ARM_INTRIN(64, 1);
2398_GLIBCXX_SIMD_ARM_INTRIN(64, 2);
2399#undef _GLIBCXX_SIMD_ARM_INTRIN
2400
2401template <typename _Tp, size_t _Bytes>
2402 struct __intrinsic_type<_Tp, _Bytes, enable_if_t<__is_vectorizable_v<_Tp> && _Bytes <= 16>>
2403 {
2404 static constexpr int _SVecBytes = _Bytes <= 8 ? 8 : 16;
2405
2406 using _Ip = __int_for_sizeof_t<_Tp>;
2407
2408 using _Up = conditional_t<
2409 is_floating_point_v<_Tp>, _Tp,
2410 conditional_t<is_unsigned_v<_Tp>, make_unsigned_t<_Ip>, _Ip>>;
2411
2412 static_assert(!is_same_v<_Tp, _Up> || _SVecBytes != _Bytes,
2413 "should use explicit specialization above");
2414
2415 using type = typename __intrinsic_type<_Up, _SVecBytes>::type;
2416 };
2417#endif // _GLIBCXX_SIMD_HAVE_NEON
2418
2419// }}}
2420// __intrinsic_type (PPC){{{
2421#ifdef __ALTIVEC__
2422template <typename _Tp>
2423 struct __intrinsic_type_impl;
2424
2425#define _GLIBCXX_SIMD_PPC_INTRIN(_Tp) \
2426 template <> \
2427 struct __intrinsic_type_impl<_Tp> { using type = __vector _Tp; }
2428_GLIBCXX_SIMD_PPC_INTRIN(float);
2429#ifdef __VSX__
2430_GLIBCXX_SIMD_PPC_INTRIN(double);
2431#endif
2432_GLIBCXX_SIMD_PPC_INTRIN(signed char);
2433_GLIBCXX_SIMD_PPC_INTRIN(unsigned char);
2434_GLIBCXX_SIMD_PPC_INTRIN(signed short);
2435_GLIBCXX_SIMD_PPC_INTRIN(unsigned short);
2436_GLIBCXX_SIMD_PPC_INTRIN(signed int);
2437_GLIBCXX_SIMD_PPC_INTRIN(unsigned int);
2438#if defined __VSX__ || __SIZEOF_LONG__ == 4
2439_GLIBCXX_SIMD_PPC_INTRIN(signed long);
2440_GLIBCXX_SIMD_PPC_INTRIN(unsigned long);
2441#endif
2442#ifdef __VSX__
2443_GLIBCXX_SIMD_PPC_INTRIN(signed long long);
2444_GLIBCXX_SIMD_PPC_INTRIN(unsigned long long);
2445#endif
2446#undef _GLIBCXX_SIMD_PPC_INTRIN
2447
2448template <typename _Tp, size_t _Bytes>
2449 struct __intrinsic_type<_Tp, _Bytes, enable_if_t<__is_vectorizable_v<_Tp> && _Bytes <= 16>>
2450 {
2451 static constexpr bool _S_is_ldouble = is_same_v<_Tp, long double>;
2452
2453 // allow _Tp == long double with -mlong-double-64
2454 static_assert(!(_S_is_ldouble && sizeof(long double) > sizeof(double)),
2455 "no __intrinsic_type support for 128-bit floating point on PowerPC");
2456
2457#ifndef __VSX__
2458 static_assert(!(is_same_v<_Tp, double>
2459 || (_S_is_ldouble && sizeof(long double) == sizeof(double))),
2460 "no __intrinsic_type support for 64-bit floating point on PowerPC w/o VSX");
2461#endif
2462
2463 using type =
2464 typename __intrinsic_type_impl<
2465 conditional_t<is_floating_point_v<_Tp>,
2466 conditional_t<_S_is_ldouble, double, _Tp>,
2467 __int_for_sizeof_t<_Tp>>>::type;
2468 };
2469#endif // __ALTIVEC__
2470
2471// }}}
2472// _SimdWrapper<bool>{{{1
2473template <size_t _Width>
2474 struct _SimdWrapper<bool, _Width,
2475 void_t<typename __bool_storage_member_type<_Width>::type>>
2476 {
2477 using _BuiltinType = typename __bool_storage_member_type<_Width>::type;
2478 using value_type = bool;
2479
2480 static constexpr size_t _S_full_size = sizeof(_BuiltinType) * __CHAR_BIT__;
2481
2482 _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper<bool, _S_full_size>
2483 __as_full_vector() const
2484 { return _M_data; }
2485
2486 _GLIBCXX_SIMD_INTRINSIC constexpr
2487 _SimdWrapper() = default;
2488
2489 _GLIBCXX_SIMD_INTRINSIC constexpr
2490 _SimdWrapper(_BuiltinType __k) : _M_data(__k) {};
2491
2492 _GLIBCXX_SIMD_INTRINSIC
2493 operator const _BuiltinType&() const
2494 { return _M_data; }
2495
2496 _GLIBCXX_SIMD_INTRINSIC
2497 operator _BuiltinType&()
2498 { return _M_data; }
2499
2500 _GLIBCXX_SIMD_INTRINSIC _BuiltinType
2501 __intrin() const
2502 { return _M_data; }
2503
2504 _GLIBCXX_SIMD_INTRINSIC constexpr value_type
2505 operator[](size_t __i) const
2506 { return _M_data & (_BuiltinType(1) << __i); }
2507
2508 template <size_t __i>
2509 _GLIBCXX_SIMD_INTRINSIC constexpr value_type
2510 operator[](_SizeConstant<__i>) const
2511 { return _M_data & (_BuiltinType(1) << __i); }
2512
2513 _GLIBCXX_SIMD_INTRINSIC constexpr void
2514 _M_set(size_t __i, value_type __x)
2515 {
2516 if (__x)
2517 _M_data |= (_BuiltinType(1) << __i);
2518 else
2519 _M_data &= ~(_BuiltinType(1) << __i);
2520 }
2521
2522 _GLIBCXX_SIMD_INTRINSIC constexpr bool
2523 _M_is_constprop() const
2524 { return __builtin_constant_p(_M_data); }
2525
2526 _GLIBCXX_SIMD_INTRINSIC constexpr bool
2527 _M_is_constprop_none_of() const
2528 {
2529 if (__builtin_constant_p(_M_data))
2530 {
2531 constexpr int __nbits = sizeof(_BuiltinType) * __CHAR_BIT__;
2532 constexpr _BuiltinType __active_mask
2533 = ~_BuiltinType() >> (__nbits - _Width);
2534 return (_M_data & __active_mask) == 0;
2535 }
2536 return false;
2537 }
2538
2539 _GLIBCXX_SIMD_INTRINSIC constexpr bool
2540 _M_is_constprop_all_of() const
2541 {
2542 if (__builtin_constant_p(_M_data))
2543 {
2544 constexpr int __nbits = sizeof(_BuiltinType) * __CHAR_BIT__;
2545 constexpr _BuiltinType __active_mask
2546 = ~_BuiltinType() >> (__nbits - _Width);
2547 return (_M_data & __active_mask) == __active_mask;
2548 }
2549 return false;
2550 }
2551
2552 _BuiltinType _M_data;
2553 };
2554
2555// _SimdWrapperBase{{{1
2556template <bool _MustZeroInitPadding, typename _BuiltinType>
2557 struct _SimdWrapperBase;
2558
2559template <typename _BuiltinType>
2560 struct _SimdWrapperBase<false, _BuiltinType> // no padding or no SNaNs
2561 {
2562 _GLIBCXX_SIMD_INTRINSIC constexpr
2563 _SimdWrapperBase() = default;
2564
2565 _GLIBCXX_SIMD_INTRINSIC constexpr
2566 _SimdWrapperBase(_BuiltinType __init) : _M_data(__init) {}
2567
2568 _BuiltinType _M_data;
2569 };
2570
2571template <typename _BuiltinType>
2572 struct _SimdWrapperBase<true, _BuiltinType> // with padding that needs to
2573 // never become SNaN
2574 {
2575 _GLIBCXX_SIMD_INTRINSIC constexpr
2576 _SimdWrapperBase() : _M_data() {}
2577
2578 _GLIBCXX_SIMD_INTRINSIC constexpr
2579 _SimdWrapperBase(_BuiltinType __init) : _M_data(__init) {}
2580
2581 _BuiltinType _M_data;
2582 };
2583
2584// }}}
2585// _SimdWrapper{{{
2586template <typename _Tp, size_t _Width>
2587 struct _SimdWrapper<
2588 _Tp, _Width,
2589 void_t<__vector_type_t<_Tp, _Width>, __intrinsic_type_t<_Tp, _Width>>>
2590 : _SimdWrapperBase<__has_iec559_behavior<__signaling_NaN, _Tp>::value
2591 && sizeof(_Tp) * _Width
2592 == sizeof(__vector_type_t<_Tp, _Width>),
2593 __vector_type_t<_Tp, _Width>>
2594 {
2595 using _Base
2596 = _SimdWrapperBase<__has_iec559_behavior<__signaling_NaN, _Tp>::value
2597 && sizeof(_Tp) * _Width
2598 == sizeof(__vector_type_t<_Tp, _Width>),
2599 __vector_type_t<_Tp, _Width>>;
2600
2601 static_assert(__is_vectorizable_v<_Tp>);
2602 static_assert(_Width >= 2); // 1 doesn't make sense, use _Tp directly then
2603
2604 using _BuiltinType = __vector_type_t<_Tp, _Width>;
2605 using value_type = _Tp;
2606
2607 static inline constexpr size_t _S_full_size
2608 = sizeof(_BuiltinType) / sizeof(value_type);
2609 static inline constexpr int _S_size = _Width;
2610 static inline constexpr bool _S_is_partial = _S_full_size != _S_size;
2611
2612 using _Base::_M_data;
2613
2614 _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper<_Tp, _S_full_size>
2615 __as_full_vector() const
2616 { return _M_data; }
2617
2618 _GLIBCXX_SIMD_INTRINSIC constexpr
2619 _SimdWrapper(initializer_list<_Tp> __init)
2620 : _Base(__generate_from_n_evaluations<_Width, _BuiltinType>(
2621 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
2622 return __init.begin()[__i.value];
2623 })) {}
2624
2625 _GLIBCXX_SIMD_INTRINSIC constexpr
2626 _SimdWrapper() = default;
2627
2628 _GLIBCXX_SIMD_INTRINSIC constexpr
2629 _SimdWrapper(const _SimdWrapper&) = default;
2630
2631 _GLIBCXX_SIMD_INTRINSIC constexpr
2632 _SimdWrapper(_SimdWrapper&&) = default;
2633
2634 _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper&
2635 operator=(const _SimdWrapper&) = default;
2636
2637 _GLIBCXX_SIMD_INTRINSIC constexpr _SimdWrapper&
2638 operator=(_SimdWrapper&&) = default;
2639
2640 template <typename _V, typename = enable_if_t<disjunction_v<
2641 is_same<_V, __vector_type_t<_Tp, _Width>>,
2642 is_same<_V, __intrinsic_type_t<_Tp, _Width>>>>>
2643 _GLIBCXX_SIMD_INTRINSIC constexpr
2644 _SimdWrapper(_V __x)
2645 // __vector_bitcast can convert e.g. __m128 to __vector(2) float
2646 : _Base(__vector_bitcast<_Tp, _Width>(__x)) {}
2647
2648 template <typename... _As,
2649 typename = enable_if_t<((is_same_v<simd_abi::scalar, _As> && ...)
2650 && sizeof...(_As) <= _Width)>>
2651 _GLIBCXX_SIMD_INTRINSIC constexpr
2652 operator _SimdTuple<_Tp, _As...>() const
2653 {
2654 return __generate_from_n_evaluations<sizeof...(_As), _SimdTuple<_Tp, _As...>>(
2655 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA
2656 { return _M_data[int(__i)]; });
2657 }
2658
2659 _GLIBCXX_SIMD_INTRINSIC constexpr
2660 operator const _BuiltinType&() const
2661 { return _M_data; }
2662
2663 _GLIBCXX_SIMD_INTRINSIC constexpr
2664 operator _BuiltinType&()
2665 { return _M_data; }
2666
2667 _GLIBCXX_SIMD_INTRINSIC constexpr _Tp
2668 operator[](size_t __i) const
2669 { return _M_data[__i]; }
2670
2671 template <size_t __i>
2672 _GLIBCXX_SIMD_INTRINSIC constexpr _Tp
2673 operator[](_SizeConstant<__i>) const
2674 { return _M_data[__i]; }
2675
2676 _GLIBCXX_SIMD_INTRINSIC constexpr void
2677 _M_set(size_t __i, _Tp __x)
2678 { _M_data[__i] = __x; }
2679
2680 _GLIBCXX_SIMD_INTRINSIC
2681 constexpr bool
2682 _M_is_constprop() const
2683 { return __builtin_constant_p(_M_data); }
2684
2685 _GLIBCXX_SIMD_INTRINSIC constexpr bool
2686 _M_is_constprop_none_of() const
2687 {
2688 if (__builtin_constant_p(_M_data))
2689 {
2690 bool __r = true;
2691 if constexpr (is_floating_point_v<_Tp>)
2692 {
2693 using _Ip = __int_for_sizeof_t<_Tp>;
2694 const auto __intdata = __vector_bitcast<_Ip>(_M_data);
2695 __execute_n_times<_Width>(
2696 [&](auto __i) { __r &= __intdata[__i.value] == _Ip(); });
2697 }
2698 else
2699 __execute_n_times<_Width>(
2700 [&](auto __i) { __r &= _M_data[__i.value] == _Tp(); });
2701 if (__builtin_constant_p(__r))
2702 return __r;
2703 }
2704 return false;
2705 }
2706
2707 _GLIBCXX_SIMD_INTRINSIC constexpr bool
2708 _M_is_constprop_all_of() const
2709 {
2710 if (__builtin_constant_p(_M_data))
2711 {
2712 bool __r = true;
2713 if constexpr (is_floating_point_v<_Tp>)
2714 {
2715 using _Ip = __int_for_sizeof_t<_Tp>;
2716 const auto __intdata = __vector_bitcast<_Ip>(_M_data);
2717 __execute_n_times<_Width>(
2718 [&](auto __i) { __r &= __intdata[__i.value] == ~_Ip(); });
2719 }
2720 else
2721 __execute_n_times<_Width>(
2722 [&](auto __i) { __r &= _M_data[__i.value] == ~_Tp(); });
2723 if (__builtin_constant_p(__r))
2724 return __r;
2725 }
2726 return false;
2727 }
2728 };
2729
2730// }}}
2731
2732// __vectorized_sizeof {{{
2733template <typename _Tp>
2734 constexpr size_t
2735 __vectorized_sizeof()
2736 {
2737 if constexpr (!__is_vectorizable_v<_Tp>)
2738 return 0;
2739
2740 if constexpr (sizeof(_Tp) <= 8)
2741 {
2742 // X86:
2743 if constexpr (__have_avx512bw)
2744 return 64;
2745 if constexpr (__have_avx512f && sizeof(_Tp) >= 4)
2746 return 64;
2747 if constexpr (__have_avx2)
2748 return 32;
2749 if constexpr (__have_avx && is_floating_point_v<_Tp>)
2750 return 32;
2751 if constexpr (__have_sse2)
2752 return 16;
2753 if constexpr (__have_sse && is_same_v<_Tp, float>)
2754 return 16;
2755 /* The following is too much trouble because of mixed MMX and x87 code.
2756 * While nothing here explicitly calls MMX instructions of registers,
2757 * they are still emitted but no EMMS cleanup is done.
2758 if constexpr (__have_mmx && sizeof(_Tp) <= 4 && is_integral_v<_Tp>)
2759 return 8;
2760 */
2761
2762 // PowerPC:
2763 if constexpr (__have_power8vec
2764 || (__have_power_vmx && (sizeof(_Tp) < 8))
2765 || (__have_power_vsx && is_floating_point_v<_Tp>) )
2766 return 16;
2767
2768 // ARM:
2769 if constexpr (__have_neon_a64
2770 || (__have_neon_a32 && !is_same_v<_Tp, double>) )
2771 return 16;
2772 if constexpr (__have_neon
2773 && sizeof(_Tp) < 8
2774 // Only allow fp if the user allows non-ICE559 fp (e.g.
2775 // via -ffast-math). ARMv7 NEON fp is not conforming to
2776 // IEC559.
2777 && (__support_neon_float || !is_floating_point_v<_Tp>))
2778 return 16;
2779 }
2780
2781 return sizeof(_Tp);
2782 }
2783
2784// }}}
2785namespace simd_abi {
2786// most of simd_abi is defined in simd_detail.h
2787template <typename _Tp>
2788 inline constexpr int max_fixed_size
2789 = (__have_avx512bw && sizeof(_Tp) == 1) ? 64 : 32;
2790
2791// compatible {{{
2792#if defined __x86_64__ || defined __aarch64__
2793template <typename _Tp>
2794 using compatible = conditional_t<(sizeof(_Tp) <= 8), _VecBuiltin<16>, scalar>;
2795#elif defined __ARM_NEON
2796// FIXME: not sure, probably needs to be scalar (or dependent on the hard-float
2797// ABI?)
2798template <typename _Tp>
2799 using compatible
2800 = conditional_t<(sizeof(_Tp) < 8
2801 && (__support_neon_float || !is_floating_point_v<_Tp>)),
2802 _VecBuiltin<16>, scalar>;
2803#else
2804template <typename>
2805 using compatible = scalar;
2806#endif
2807
2808// }}}
2809// native {{{
2810template <typename _Tp>
2811 constexpr auto
2812 __determine_native_abi()
2813 {
2814 constexpr size_t __bytes = __vectorized_sizeof<_Tp>();
2815 if constexpr (__bytes == sizeof(_Tp))
2816 return static_cast<scalar*>(nullptr);
2817 else if constexpr (__have_avx512vl || (__have_avx512f && __bytes == 64))
2818 return static_cast<_VecBltnBtmsk<__bytes>*>(nullptr);
2819 else
2820 return static_cast<_VecBuiltin<__bytes>*>(nullptr);
2821 }
2822
2823template <typename _Tp, typename = enable_if_t<__is_vectorizable_v<_Tp>>>
2824 using native = remove_pointer_t<decltype(__determine_native_abi<_Tp>())>;
2825
2826// }}}
2827// __default_abi {{{
2828#if defined _GLIBCXX_SIMD_DEFAULT_ABI
2829template <typename _Tp>
2830 using __default_abi = _GLIBCXX_SIMD_DEFAULT_ABI<_Tp>;
2831#else
2832template <typename _Tp>
2833 using __default_abi = compatible<_Tp>;
2834#endif
2835
2836// }}}
2837} // namespace simd_abi
2838
2839// traits {{{1
2840template <typename _Tp>
2841 struct is_simd_flag_type
2842 : false_type
2843 {};
2844
2845template <>
2846 struct is_simd_flag_type<element_aligned_tag>
2847 : true_type
2848 {};
2849
2850template <>
2851 struct is_simd_flag_type<vector_aligned_tag>
2852 : true_type
2853 {};
2854
2855template <size_t _Np>
2856 struct is_simd_flag_type<overaligned_tag<_Np>>
2857 : __bool_constant<(_Np > 0) and __has_single_bit(_Np)>
2858 {};
2859
2860template <typename _Tp>
2861 inline constexpr bool is_simd_flag_type_v = is_simd_flag_type<_Tp>::value;
2862
2863template <typename _Tp, typename = enable_if_t<is_simd_flag_type_v<_Tp>>>
2864 using _IsSimdFlagType = _Tp;
2865
2866// is_abi_tag {{{2
2867template <typename _Tp, typename = void_t<>>
2868 struct is_abi_tag : false_type {};
2869
2870template <typename _Tp>
2871 struct is_abi_tag<_Tp, void_t<typename _Tp::_IsValidAbiTag>>
2872 : public _Tp::_IsValidAbiTag {};
2873
2874template <typename _Tp>
2875 inline constexpr bool is_abi_tag_v = is_abi_tag<_Tp>::value;
2876
2877// is_simd(_mask) {{{2
2878template <typename _Tp>
2879 struct is_simd : public false_type {};
2880
2881template <typename _Tp>
2882 inline constexpr bool is_simd_v = is_simd<_Tp>::value;
2883
2884template <typename _Tp>
2885 struct is_simd_mask : public false_type {};
2886
2887template <typename _Tp>
2888inline constexpr bool is_simd_mask_v = is_simd_mask<_Tp>::value;
2889
2890// simd_size {{{2
2891template <typename _Tp, typename _Abi, typename = void>
2892 struct __simd_size_impl {};
2893
2894template <typename _Tp, typename _Abi>
2895 struct __simd_size_impl<
2896 _Tp, _Abi,
2897 enable_if_t<conjunction_v<__is_vectorizable<_Tp>, is_abi_tag<_Abi>>>>
2898 : _SizeConstant<_Abi::template _S_size<_Tp>> {};
2899
2900template <typename _Tp, typename _Abi = simd_abi::__default_abi<_Tp>>
2901 struct simd_size : __simd_size_impl<_Tp, _Abi> {};
2902
2903template <typename _Tp, typename _Abi = simd_abi::__default_abi<_Tp>>
2904 inline constexpr size_t simd_size_v = simd_size<_Tp, _Abi>::value;
2905
2906// simd_abi::deduce {{{2
2907template <typename _Tp, size_t _Np, typename = void>
2908 struct __deduce_impl;
2909
2910namespace simd_abi {
2911/**
2912 * @tparam _Tp The requested `value_type` for the elements.
2913 * @tparam _Np The requested number of elements.
2914 * @tparam _Abis This parameter is ignored, since this implementation cannot
2915 * make any use of it. Either __a good native ABI is matched and used as `type`
2916 * alias, or the `fixed_size<_Np>` ABI is used, which internally is built from
2917 * the best matching native ABIs.
2918 */
2919template <typename _Tp, size_t _Np, typename...>
2920 struct deduce : __deduce_impl<_Tp, _Np> {};
2921
2922template <typename _Tp, size_t _Np, typename... _Abis>
2923 using deduce_t = typename deduce<_Tp, _Np, _Abis...>::type;
2924} // namespace simd_abi
2925
2926// }}}2
2927// rebind_simd {{{2
2928template <typename _Tp, typename _V, typename = void>
2929 struct rebind_simd;
2930
2931template <typename _Tp, typename _Up, typename _Abi>
2932 struct rebind_simd<_Tp, simd<_Up, _Abi>,
2933 void_t<simd_abi::deduce_t<_Tp, simd_size_v<_Up, _Abi>, _Abi>>>
2934 { using type = simd<_Tp, simd_abi::deduce_t<_Tp, simd_size_v<_Up, _Abi>, _Abi>>; };
2935
2936template <typename _Tp, typename _Up, typename _Abi>
2937 struct rebind_simd<_Tp, simd_mask<_Up, _Abi>,
2938 void_t<simd_abi::deduce_t<_Tp, simd_size_v<_Up, _Abi>, _Abi>>>
2939 { using type = simd_mask<_Tp, simd_abi::deduce_t<_Tp, simd_size_v<_Up, _Abi>, _Abi>>; };
2940
2941template <typename _Tp, typename _V>
2942 using rebind_simd_t = typename rebind_simd<_Tp, _V>::type;
2943
2944// resize_simd {{{2
2945template <int _Np, typename _V, typename = void>
2946 struct resize_simd;
2947
2948template <int _Np, typename _Tp, typename _Abi>
2949 struct resize_simd<_Np, simd<_Tp, _Abi>, void_t<simd_abi::deduce_t<_Tp, _Np, _Abi>>>
2950 { using type = simd<_Tp, simd_abi::deduce_t<_Tp, _Np, _Abi>>; };
2951
2952template <int _Np, typename _Tp, typename _Abi>
2953 struct resize_simd<_Np, simd_mask<_Tp, _Abi>, void_t<simd_abi::deduce_t<_Tp, _Np, _Abi>>>
2954 { using type = simd_mask<_Tp, simd_abi::deduce_t<_Tp, _Np, _Abi>>; };
2955
2956template <int _Np, typename _V>
2957 using resize_simd_t = typename resize_simd<_Np, _V>::type;
2958
2959// }}}2
2960// memory_alignment {{{2
2961template <typename _Tp, typename _Up = typename _Tp::value_type>
2962 struct memory_alignment
2963 : public _SizeConstant<vector_aligned_tag::_S_alignment<_Tp, _Up>> {};
2964
2965template <typename _Tp, typename _Up = typename _Tp::value_type>
2966 inline constexpr size_t memory_alignment_v = memory_alignment<_Tp, _Up>::value;
2967
2968// class template simd [simd] {{{1
2969template <typename _Tp, typename _Abi = simd_abi::__default_abi<_Tp>>
2970 class simd;
2971
2972template <typename _Tp, typename _Abi>
2973 struct is_simd<simd<_Tp, _Abi>> : public true_type {};
2974
2975template <typename _Tp>
2976 using native_simd = simd<_Tp, simd_abi::native<_Tp>>;
2977
2978template <typename _Tp, int _Np>
2979 using fixed_size_simd = simd<_Tp, simd_abi::fixed_size<_Np>>;
2980
2981template <typename _Tp, size_t _Np>
2982 using __deduced_simd = simd<_Tp, simd_abi::deduce_t<_Tp, _Np>>;
2983
2984// class template simd_mask [simd_mask] {{{1
2985template <typename _Tp, typename _Abi = simd_abi::__default_abi<_Tp>>
2986 class simd_mask;
2987
2988template <typename _Tp, typename _Abi>
2989 struct is_simd_mask<simd_mask<_Tp, _Abi>> : public true_type {};
2990
2991template <typename _Tp>
2992 using native_simd_mask = simd_mask<_Tp, simd_abi::native<_Tp>>;
2993
2994template <typename _Tp, int _Np>
2995 using fixed_size_simd_mask = simd_mask<_Tp, simd_abi::fixed_size<_Np>>;
2996
2997template <typename _Tp, size_t _Np>
2998 using __deduced_simd_mask = simd_mask<_Tp, simd_abi::deduce_t<_Tp, _Np>>;
2999
3000// casts [simd.casts] {{{1
3001// static_simd_cast {{{2
3002template <typename _Tp, typename _Up, typename _Ap, bool = is_simd_v<_Tp>, typename = void>
3003 struct __static_simd_cast_return_type;
3004
3005template <typename _Tp, typename _A0, typename _Up, typename _Ap>
3006 struct __static_simd_cast_return_type<simd_mask<_Tp, _A0>, _Up, _Ap, false, void>
3007 : __static_simd_cast_return_type<simd<_Tp, _A0>, _Up, _Ap> {};
3008
3009template <typename _Tp, typename _Up, typename _Ap>
3010 struct __static_simd_cast_return_type<
3011 _Tp, _Up, _Ap, true, enable_if_t<_Tp::size() == simd_size_v<_Up, _Ap>>>
3012 { using type = _Tp; };
3013
3014template <typename _Tp, typename _Ap>
3015 struct __static_simd_cast_return_type<_Tp, _Tp, _Ap, false,
3016#ifdef _GLIBCXX_SIMD_FIX_P2TS_ISSUE66
3017 enable_if_t<__is_vectorizable_v<_Tp>>
3018#else
3019 void
3020#endif
3021 >
3022 { using type = simd<_Tp, _Ap>; };
3023
3024template <typename _Tp, typename = void>
3025 struct __safe_make_signed { using type = _Tp;};
3026
3027template <typename _Tp>
3028 struct __safe_make_signed<_Tp, enable_if_t<is_integral_v<_Tp>>>
3029 {
3030 // the extra make_unsigned_t is because of PR85951
3031 using type = make_signed_t<make_unsigned_t<_Tp>>;
3032 };
3033
3034template <typename _Tp>
3035 using safe_make_signed_t = typename __safe_make_signed<_Tp>::type;
3036
3037template <typename _Tp, typename _Up, typename _Ap>
3038 struct __static_simd_cast_return_type<_Tp, _Up, _Ap, false,
3039#ifdef _GLIBCXX_SIMD_FIX_P2TS_ISSUE66
3040 enable_if_t<__is_vectorizable_v<_Tp>>
3041#else
3042 void
3043#endif
3044 >
3045 {
3046 using type = conditional_t<
3047 (is_integral_v<_Up> && is_integral_v<_Tp> &&
3048#ifndef _GLIBCXX_SIMD_FIX_P2TS_ISSUE65
3049 is_signed_v<_Up> != is_signed_v<_Tp> &&
3050#endif
3051 is_same_v<safe_make_signed_t<_Up>, safe_make_signed_t<_Tp>>),
3052 simd<_Tp, _Ap>, fixed_size_simd<_Tp, simd_size_v<_Up, _Ap>>>;
3053 };
3054
3055template <typename _Tp, typename _Up, typename _Ap,
3056 typename _R
3057 = typename __static_simd_cast_return_type<_Tp, _Up, _Ap>::type>
3058 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _R
3059 static_simd_cast(const simd<_Up, _Ap>& __x)
3060 {
3061 if constexpr (is_same<_R, simd<_Up, _Ap>>::value)
3062 return __x;
3063 else
3064 {
3065 _SimdConverter<_Up, _Ap, typename _R::value_type, typename _R::abi_type>
3066 __c;
3067 return _R(__private_init, __c(__data(__x)));
3068 }
3069 }
3070
3071namespace __proposed {
3072template <typename _Tp, typename _Up, typename _Ap,
3073 typename _R
3074 = typename __static_simd_cast_return_type<_Tp, _Up, _Ap>::type>
3075 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR typename _R::mask_type
3076 static_simd_cast(const simd_mask<_Up, _Ap>& __x)
3077 {
3078 using _RM = typename _R::mask_type;
3079 return {__private_init, _RM::abi_type::_MaskImpl::template _S_convert<
3080 typename _RM::simd_type::value_type>(__x)};
3081 }
3082
3083template <typename _To, typename _Up, typename _Abi>
3084 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
3085 _To
3086 simd_bit_cast(const simd<_Up, _Abi>& __x)
3087 {
3088 using _Tp = typename _To::value_type;
3089 using _ToMember = typename _SimdTraits<_Tp, typename _To::abi_type>::_SimdMember;
3090 using _From = simd<_Up, _Abi>;
3091 using _FromMember = typename _SimdTraits<_Up, _Abi>::_SimdMember;
3092 // with concepts, the following should be constraints
3093 static_assert(sizeof(_To) == sizeof(_From));
3094 static_assert(is_trivially_copyable_v<_Tp> && is_trivially_copyable_v<_Up>);
3095 static_assert(is_trivially_copyable_v<_ToMember> && is_trivially_copyable_v<_FromMember>);
3096#if __has_builtin(__builtin_bit_cast)
3097 return {__private_init, __builtin_bit_cast(_ToMember, __data(__x))};
3098#else
3099 return {__private_init, __bit_cast<_ToMember>(__data(__x))};
3100#endif
3101 }
3102
3103template <typename _To, typename _Up, typename _Abi>
3104 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
3105 _To
3106 simd_bit_cast(const simd_mask<_Up, _Abi>& __x)
3107 {
3108 using _From = simd_mask<_Up, _Abi>;
3109 static_assert(sizeof(_To) == sizeof(_From));
3110 static_assert(is_trivially_copyable_v<_From>);
3111 // _To can be simd<T, A>, specifically simd<T, fixed_size<N>> in which case _To is not trivially
3112 // copyable.
3113 if constexpr (is_simd_v<_To>)
3114 {
3115 using _Tp = typename _To::value_type;
3116 using _ToMember = typename _SimdTraits<_Tp, typename _To::abi_type>::_SimdMember;
3117 static_assert(is_trivially_copyable_v<_ToMember>);
3118#if __has_builtin(__builtin_bit_cast)
3119 return {__private_init, __builtin_bit_cast(_ToMember, __x)};
3120#else
3121 return {__private_init, __bit_cast<_ToMember>(__x)};
3122#endif
3123 }
3124 else
3125 {
3126 static_assert(is_trivially_copyable_v<_To>);
3127#if __has_builtin(__builtin_bit_cast)
3128 return __builtin_bit_cast(_To, __x);
3129#else
3130 return __bit_cast<_To>(__x);
3131#endif
3132 }
3133 }
3134} // namespace __proposed
3135
3136// simd_cast {{{2
3137template <typename _Tp, typename _Up, typename _Ap,
3138 typename _To = __value_type_or_identity_t<_Tp>>
3139 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR auto
3140 simd_cast(const simd<_ValuePreserving<_Up, _To>, _Ap>& __x)
3141 -> decltype(static_simd_cast<_Tp>(__x))
3142 { return static_simd_cast<_Tp>(__x); }
3143
3144namespace __proposed {
3145template <typename _Tp, typename _Up, typename _Ap,
3146 typename _To = __value_type_or_identity_t<_Tp>>
3147 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR auto
3148 simd_cast(const simd_mask<_ValuePreserving<_Up, _To>, _Ap>& __x)
3149 -> decltype(static_simd_cast<_Tp>(__x))
3150 { return static_simd_cast<_Tp>(__x); }
3151} // namespace __proposed
3152
3153// }}}2
3154// resizing_simd_cast {{{
3155namespace __proposed {
3156/* Proposed spec:
3157
3158template <class T, class U, class Abi>
3159T resizing_simd_cast(const simd<U, Abi>& x)
3160
3161p1 Constraints:
3162 - is_simd_v<T> is true and
3163 - T::value_type is the same type as U
3164
3165p2 Returns:
3166 A simd object with the i^th element initialized to x[i] for all i in the
3167 range of [0, min(T::size(), simd_size_v<U, Abi>)). If T::size() is larger
3168 than simd_size_v<U, Abi>, the remaining elements are value-initialized.
3169
3170template <class T, class U, class Abi>
3171T resizing_simd_cast(const simd_mask<U, Abi>& x)
3172
3173p1 Constraints: is_simd_mask_v<T> is true
3174
3175p2 Returns:
3176 A simd_mask object with the i^th element initialized to x[i] for all i in
3177the range of [0, min(T::size(), simd_size_v<U, Abi>)). If T::size() is larger
3178 than simd_size_v<U, Abi>, the remaining elements are initialized to false.
3179
3180 */
3181
3182template <typename _Tp, typename _Up, typename _Ap>
3183 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR enable_if_t<
3184 conjunction_v<is_simd<_Tp>, is_same<typename _Tp::value_type, _Up>>, _Tp>
3185 resizing_simd_cast(const simd<_Up, _Ap>& __x)
3186 {
3187 if constexpr (is_same_v<typename _Tp::abi_type, _Ap>)
3188 return __x;
3189 else if constexpr (simd_size_v<_Up, _Ap> == 1)
3190 {
3191 _Tp __r{};
3192 __r[0] = __x[0];
3193 return __r;
3194 }
3195 else if constexpr (_Tp::size() == 1)
3196 return __x[0];
3197 else if constexpr (sizeof(_Tp) == sizeof(__x)
3198 && !__is_fixed_size_abi_v<_Ap>)
3199 return {__private_init,
3200 __vector_bitcast<typename _Tp::value_type, _Tp::size()>(
3201 _Ap::_S_masked(__data(__x))._M_data)};
3202 else
3203 {
3204 _Tp __r{};
3205 __builtin_memcpy(&__data(__r), &__data(__x),
3206 sizeof(_Up)
3207 * std::min(_Tp::size(), simd_size_v<_Up, _Ap>));
3208 return __r;
3209 }
3210 }
3211
3212template <typename _Tp, typename _Up, typename _Ap>
3213 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
3214 enable_if_t<is_simd_mask_v<_Tp>, _Tp>
3215 resizing_simd_cast(const simd_mask<_Up, _Ap>& __x)
3216 {
3217 return {__private_init, _Tp::abi_type::_MaskImpl::template _S_convert<
3218 typename _Tp::simd_type::value_type>(__x)};
3219 }
3220} // namespace __proposed
3221
3222// }}}
3223// to_fixed_size {{{2
3224template <typename _Tp, int _Np>
3225 _GLIBCXX_SIMD_INTRINSIC fixed_size_simd<_Tp, _Np>
3226 to_fixed_size(const fixed_size_simd<_Tp, _Np>& __x)
3227 { return __x; }
3228
3229template <typename _Tp, int _Np>
3230 _GLIBCXX_SIMD_INTRINSIC fixed_size_simd_mask<_Tp, _Np>
3231 to_fixed_size(const fixed_size_simd_mask<_Tp, _Np>& __x)
3232 { return __x; }
3233
3234template <typename _Tp, typename _Ap>
3235 _GLIBCXX_SIMD_INTRINSIC fixed_size_simd<_Tp, simd_size_v<_Tp, _Ap>>
3236 to_fixed_size(const simd<_Tp, _Ap>& __x)
3237 {
3238 using _Rp = fixed_size_simd<_Tp, simd_size_v<_Tp, _Ap>>;
3239 return _Rp([&__x](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { return __x[__i]; });
3240 }
3241
3242template <typename _Tp, typename _Ap>
3243 _GLIBCXX_SIMD_INTRINSIC fixed_size_simd_mask<_Tp, simd_size_v<_Tp, _Ap>>
3244 to_fixed_size(const simd_mask<_Tp, _Ap>& __x)
3245 {
3246 return {__private_init,
3247 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { return __x[__i]; }};
3248 }
3249
3250// to_native {{{2
3251template <typename _Tp, int _Np>
3252 _GLIBCXX_SIMD_INTRINSIC
3253 enable_if_t<(_Np == native_simd<_Tp>::size()), native_simd<_Tp>>
3254 to_native(const fixed_size_simd<_Tp, _Np>& __x)
3255 {
3256 alignas(memory_alignment_v<native_simd<_Tp>>) _Tp __mem[_Np];
3257 __x.copy_to(__mem, vector_aligned);
3258 return {__mem, vector_aligned};
3259 }
3260
3261template <typename _Tp, size_t _Np>
3262 _GLIBCXX_SIMD_INTRINSIC
3263 enable_if_t<(_Np == native_simd_mask<_Tp>::size()), native_simd_mask<_Tp>>
3264 to_native(const fixed_size_simd_mask<_Tp, _Np>& __x)
3265 {
3266 return native_simd_mask<_Tp>(
3267 __private_init,
3268 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { return __x[__i]; });
3269 }
3270
3271// to_compatible {{{2
3272template <typename _Tp, size_t _Np>
3273 _GLIBCXX_SIMD_INTRINSIC enable_if_t<(_Np == simd<_Tp>::size()), simd<_Tp>>
3274 to_compatible(const simd<_Tp, simd_abi::fixed_size<_Np>>& __x)
3275 {
3276 alignas(memory_alignment_v<simd<_Tp>>) _Tp __mem[_Np];
3277 __x.copy_to(__mem, vector_aligned);
3278 return {__mem, vector_aligned};
3279 }
3280
3281template <typename _Tp, size_t _Np>
3282 _GLIBCXX_SIMD_INTRINSIC
3283 enable_if_t<(_Np == simd_mask<_Tp>::size()), simd_mask<_Tp>>
3284 to_compatible(const simd_mask<_Tp, simd_abi::fixed_size<_Np>>& __x)
3285 {
3286 return simd_mask<_Tp>(
3287 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { return __x[__i]; });
3288 }
3289
3290// masked assignment [simd_mask.where] {{{1
3291
3292// where_expression {{{1
3293// const_where_expression<M, T> {{{2
3294template <typename _M, typename _Tp>
3295 class const_where_expression
3296 {
3297 using _V = _Tp;
3298 static_assert(is_same_v<_V, __remove_cvref_t<_Tp>>);
3299
3300 struct _Wrapper { using value_type = _V; };
3301
3302 protected:
3303 using _Impl = typename _V::_Impl;
3304
3305 using value_type =
3306 typename conditional_t<is_arithmetic_v<_V>, _Wrapper, _V>::value_type;
3307
3308 _GLIBCXX_SIMD_INTRINSIC friend const _M&
3309 __get_mask(const const_where_expression& __x)
3310 { return __x._M_k; }
3311
3312 _GLIBCXX_SIMD_INTRINSIC friend const _Tp&
3313 __get_lvalue(const const_where_expression& __x)
3314 { return __x._M_value; }
3315
3316 const _M& _M_k;
3317 _Tp& _M_value;
3318
3319 public:
3320 const_where_expression(const const_where_expression&) = delete;
3321
3322 const_where_expression& operator=(const const_where_expression&) = delete;
3323
3324 _GLIBCXX_SIMD_INTRINSIC const_where_expression(const _M& __kk, const _Tp& dd)
3325 : _M_k(__kk), _M_value(const_cast<_Tp&>(dd)) {}
3326
3327 _GLIBCXX_SIMD_INTRINSIC _V
3328 operator-() const&&
3329 {
3330 return {__private_init,
3331 _Impl::template _S_masked_unary<negate>(__data(_M_k),
3332 __data(_M_value))};
3333 }
3334
3335 template <typename _Up, typename _Flags>
3336 [[nodiscard]] _GLIBCXX_SIMD_INTRINSIC _V
3337 copy_from(const _LoadStorePtr<_Up, value_type>* __mem, _IsSimdFlagType<_Flags>) const&&
3338 {
3339 return {__private_init,
3340 _Impl::_S_masked_load(__data(_M_value), __data(_M_k),
3341 _Flags::template _S_apply<_V>(__mem))};
3342 }
3343
3344 template <typename _Up, typename _Flags>
3345 _GLIBCXX_SIMD_INTRINSIC void
3346 copy_to(_LoadStorePtr<_Up, value_type>* __mem, _IsSimdFlagType<_Flags>) const&&
3347 {
3348 _Impl::_S_masked_store(__data(_M_value),
3349 _Flags::template _S_apply<_V>(__mem),
3350 __data(_M_k));
3351 }
3352 };
3353
3354// const_where_expression<bool, T> {{{2
3355template <typename _Tp>
3356 class const_where_expression<bool, _Tp>
3357 {
3358 using _M = bool;
3359 using _V = _Tp;
3360
3361 static_assert(is_same_v<_V, __remove_cvref_t<_Tp>>);
3362
3363 struct _Wrapper { using value_type = _V; };
3364
3365 protected:
3366 using value_type
3367 = typename conditional_t<is_arithmetic_v<_V>, _Wrapper, _V>::value_type;
3368
3369 _GLIBCXX_SIMD_INTRINSIC friend const _M&
3370 __get_mask(const const_where_expression& __x)
3371 { return __x._M_k; }
3372
3373 _GLIBCXX_SIMD_INTRINSIC friend const _Tp&
3374 __get_lvalue(const const_where_expression& __x)
3375 { return __x._M_value; }
3376
3377 const bool _M_k;
3378 _Tp& _M_value;
3379
3380 public:
3381 const_where_expression(const const_where_expression&) = delete;
3382 const_where_expression& operator=(const const_where_expression&) = delete;
3383
3384 _GLIBCXX_SIMD_INTRINSIC const_where_expression(const bool __kk, const _Tp& dd)
3385 : _M_k(__kk), _M_value(const_cast<_Tp&>(dd)) {}
3386
3387 _GLIBCXX_SIMD_INTRINSIC _V operator-() const&&
3388 { return _M_k ? -_M_value : _M_value; }
3389
3390 template <typename _Up, typename _Flags>
3391 [[nodiscard]] _GLIBCXX_SIMD_INTRINSIC _V
3392 copy_from(const _LoadStorePtr<_Up, value_type>* __mem, _IsSimdFlagType<_Flags>) const&&
3393 { return _M_k ? static_cast<_V>(__mem[0]) : _M_value; }
3394
3395 template <typename _Up, typename _Flags>
3396 _GLIBCXX_SIMD_INTRINSIC void
3397 copy_to(_LoadStorePtr<_Up, value_type>* __mem, _IsSimdFlagType<_Flags>) const&&
3398 {
3399 if (_M_k)
3400 __mem[0] = _M_value;
3401 }
3402 };
3403
3404// where_expression<M, T> {{{2
3405template <typename _M, typename _Tp>
3406 class where_expression : public const_where_expression<_M, _Tp>
3407 {
3408 using _Impl = typename const_where_expression<_M, _Tp>::_Impl;
3409
3410 static_assert(!is_const<_Tp>::value,
3411 "where_expression may only be instantiated with __a non-const "
3412 "_Tp parameter");
3413
3414 using typename const_where_expression<_M, _Tp>::value_type;
3415 using const_where_expression<_M, _Tp>::_M_k;
3416 using const_where_expression<_M, _Tp>::_M_value;
3417
3418 static_assert(
3419 is_same<typename _M::abi_type, typename _Tp::abi_type>::value, "");
3420 static_assert(_M::size() == _Tp::size(), "");
3421
3422 _GLIBCXX_SIMD_INTRINSIC friend _Tp& __get_lvalue(where_expression& __x)
3423 { return __x._M_value; }
3424
3425 public:
3426 where_expression(const where_expression&) = delete;
3427 where_expression& operator=(const where_expression&) = delete;
3428
3429 _GLIBCXX_SIMD_INTRINSIC where_expression(const _M& __kk, _Tp& dd)
3430 : const_where_expression<_M, _Tp>(__kk, dd) {}
3431
3432 template <typename _Up>
3433 _GLIBCXX_SIMD_INTRINSIC void operator=(_Up&& __x) &&
3434 {
3435 _Impl::_S_masked_assign(__data(_M_k), __data(_M_value),
3436 __to_value_type_or_member_type<_Tp>(
3437 static_cast<_Up&&>(__x)));
3438 }
3439
3440#define _GLIBCXX_SIMD_OP_(__op, __name) \
3441 template <typename _Up> \
3442 _GLIBCXX_SIMD_INTRINSIC void operator __op##=(_Up&& __x)&& \
3443 { \
3444 _Impl::template _S_masked_cassign( \
3445 __data(_M_k), __data(_M_value), \
3446 __to_value_type_or_member_type<_Tp>(static_cast<_Up&&>(__x)), \
3447 [](auto __impl, auto __lhs, auto __rhs) \
3448 constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA \
3449 { return __impl.__name(__lhs, __rhs); }); \
3450 } \
3451 static_assert(true)
3452 _GLIBCXX_SIMD_OP_(+, _S_plus);
3453 _GLIBCXX_SIMD_OP_(-, _S_minus);
3454 _GLIBCXX_SIMD_OP_(*, _S_multiplies);
3455 _GLIBCXX_SIMD_OP_(/, _S_divides);
3456 _GLIBCXX_SIMD_OP_(%, _S_modulus);
3457 _GLIBCXX_SIMD_OP_(&, _S_bit_and);
3458 _GLIBCXX_SIMD_OP_(|, _S_bit_or);
3459 _GLIBCXX_SIMD_OP_(^, _S_bit_xor);
3460 _GLIBCXX_SIMD_OP_(<<, _S_shift_left);
3461 _GLIBCXX_SIMD_OP_(>>, _S_shift_right);
3462#undef _GLIBCXX_SIMD_OP_
3463
3464 _GLIBCXX_SIMD_INTRINSIC void
3465 operator++() &&
3466 {
3467 __data(_M_value)
3468 = _Impl::template _S_masked_unary<__increment>(__data(_M_k), __data(_M_value));
3469 }
3470
3471 _GLIBCXX_SIMD_INTRINSIC void
3472 operator++(int) &&
3473 {
3474 __data(_M_value)
3475 = _Impl::template _S_masked_unary<__increment>(__data(_M_k), __data(_M_value));
3476 }
3477
3478 _GLIBCXX_SIMD_INTRINSIC void
3479 operator--() &&
3480 {
3481 __data(_M_value)
3482 = _Impl::template _S_masked_unary<__decrement>(__data(_M_k), __data(_M_value));
3483 }
3484
3485 _GLIBCXX_SIMD_INTRINSIC void
3486 operator--(int) &&
3487 {
3488 __data(_M_value)
3489 = _Impl::template _S_masked_unary<__decrement>(__data(_M_k), __data(_M_value));
3490 }
3491
3492 // intentionally hides const_where_expression::copy_from
3493 template <typename _Up, typename _Flags>
3494 _GLIBCXX_SIMD_INTRINSIC void
3495 copy_from(const _LoadStorePtr<_Up, value_type>* __mem, _IsSimdFlagType<_Flags>) &&
3496 {
3497 __data(_M_value) = _Impl::_S_masked_load(__data(_M_value), __data(_M_k),
3498 _Flags::template _S_apply<_Tp>(__mem));
3499 }
3500 };
3501
3502// where_expression<bool, T> {{{2
3503template <typename _Tp>
3504 class where_expression<bool, _Tp>
3505 : public const_where_expression<bool, _Tp>
3506 {
3507 using _M = bool;
3508 using typename const_where_expression<_M, _Tp>::value_type;
3509 using const_where_expression<_M, _Tp>::_M_k;
3510 using const_where_expression<_M, _Tp>::_M_value;
3511
3512 public:
3513 where_expression(const where_expression&) = delete;
3514 where_expression& operator=(const where_expression&) = delete;
3515
3516 _GLIBCXX_SIMD_INTRINSIC
3517 where_expression(const _M& __kk, _Tp& dd)
3518 : const_where_expression<_M, _Tp>(__kk, dd) {}
3519
3520#define _GLIBCXX_SIMD_OP_(__op) \
3521 template <typename _Up> \
3522 _GLIBCXX_SIMD_INTRINSIC void \
3523 operator __op(_Up&& __x)&& \
3524 { if (_M_k) _M_value __op static_cast<_Up&&>(__x); }
3525
3526 _GLIBCXX_SIMD_OP_(=)
3527 _GLIBCXX_SIMD_OP_(+=)
3528 _GLIBCXX_SIMD_OP_(-=)
3529 _GLIBCXX_SIMD_OP_(*=)
3530 _GLIBCXX_SIMD_OP_(/=)
3531 _GLIBCXX_SIMD_OP_(%=)
3532 _GLIBCXX_SIMD_OP_(&=)
3533 _GLIBCXX_SIMD_OP_(|=)
3534 _GLIBCXX_SIMD_OP_(^=)
3535 _GLIBCXX_SIMD_OP_(<<=)
3536 _GLIBCXX_SIMD_OP_(>>=)
3537 #undef _GLIBCXX_SIMD_OP_
3538
3539 _GLIBCXX_SIMD_INTRINSIC void
3540 operator++() &&
3541 { if (_M_k) ++_M_value; }
3542
3543 _GLIBCXX_SIMD_INTRINSIC void
3544 operator++(int) &&
3545 { if (_M_k) ++_M_value; }
3546
3547 _GLIBCXX_SIMD_INTRINSIC void
3548 operator--() &&
3549 { if (_M_k) --_M_value; }
3550
3551 _GLIBCXX_SIMD_INTRINSIC void
3552 operator--(int) &&
3553 { if (_M_k) --_M_value; }
3554
3555 // intentionally hides const_where_expression::copy_from
3556 template <typename _Up, typename _Flags>
3557 _GLIBCXX_SIMD_INTRINSIC void
3558 copy_from(const _LoadStorePtr<_Up, value_type>* __mem, _IsSimdFlagType<_Flags>) &&
3559 { if (_M_k) _M_value = __mem[0]; }
3560 };
3561
3562// where {{{1
3563template <typename _Tp, typename _Ap>
3564 _GLIBCXX_SIMD_INTRINSIC where_expression<simd_mask<_Tp, _Ap>, simd<_Tp, _Ap>>
3565 where(const typename simd<_Tp, _Ap>::mask_type& __k, simd<_Tp, _Ap>& __value)
3566 { return {__k, __value}; }
3567
3568template <typename _Tp, typename _Ap>
3569 _GLIBCXX_SIMD_INTRINSIC
3570 const_where_expression<simd_mask<_Tp, _Ap>, simd<_Tp, _Ap>>
3571 where(const typename simd<_Tp, _Ap>::mask_type& __k, const simd<_Tp, _Ap>& __value)
3572 { return {__k, __value}; }
3573
3574template <typename _Tp, typename _Ap>
3575 _GLIBCXX_SIMD_INTRINSIC
3576 where_expression<simd_mask<_Tp, _Ap>, simd_mask<_Tp, _Ap>>
3577 where(const remove_const_t<simd_mask<_Tp, _Ap>>& __k, simd_mask<_Tp, _Ap>& __value)
3578 { return {__k, __value}; }
3579
3580template <typename _Tp, typename _Ap>
3581 _GLIBCXX_SIMD_INTRINSIC
3582 const_where_expression<simd_mask<_Tp, _Ap>, simd_mask<_Tp, _Ap>>
3583 where(const remove_const_t<simd_mask<_Tp, _Ap>>& __k, const simd_mask<_Tp, _Ap>& __value)
3584 { return {__k, __value}; }
3585
3586template <typename _Tp>
3587 _GLIBCXX_SIMD_INTRINSIC where_expression<bool, _Tp>
3588 where(_ExactBool __k, _Tp& __value)
3589 { return {__k, __value}; }
3590
3591template <typename _Tp>
3592 _GLIBCXX_SIMD_INTRINSIC const_where_expression<bool, _Tp>
3593 where(_ExactBool __k, const _Tp& __value)
3594 { return {__k, __value}; }
3595
3596template <typename _Tp, typename _Ap>
3597 void where(bool __k, simd<_Tp, _Ap>& __value) = delete;
3598
3599template <typename _Tp, typename _Ap>
3600 void where(bool __k, const simd<_Tp, _Ap>& __value) = delete;
3601
3602// proposed mask iterations {{{1
3603namespace __proposed {
3604template <size_t _Np>
3605 class where_range
3606 {
3607 const bitset<_Np> __bits;
3608
3609 public:
3610 where_range(bitset<_Np> __b) : __bits(__b) {}
3611
3612 class iterator
3613 {
3614 size_t __mask;
3615 size_t __bit;
3616
3617 _GLIBCXX_SIMD_INTRINSIC void
3618 __next_bit()
3619 { __bit = __builtin_ctzl(__mask); }
3620
3621 _GLIBCXX_SIMD_INTRINSIC void
3622 __reset_lsb()
3623 {
3624 // 01100100 - 1 = 01100011
3625 __mask &= (__mask - 1);
3626 // __asm__("btr %1,%0" : "+r"(__mask) : "r"(__bit));
3627 }
3628
3629 public:
3630 iterator(decltype(__mask) __m) : __mask(__m) { __next_bit(); }
3631 iterator(const iterator&) = default;
3632 iterator(iterator&&) = default;
3633
3634 _GLIBCXX_SIMD_ALWAYS_INLINE size_t
3635 operator->() const
3636 { return __bit; }
3637
3638 _GLIBCXX_SIMD_ALWAYS_INLINE size_t
3639 operator*() const
3640 { return __bit; }
3641
3642 _GLIBCXX_SIMD_ALWAYS_INLINE iterator&
3643 operator++()
3644 {
3645 __reset_lsb();
3646 __next_bit();
3647 return *this;
3648 }
3649
3650 _GLIBCXX_SIMD_ALWAYS_INLINE iterator
3651 operator++(int)
3652 {
3653 iterator __tmp = *this;
3654 __reset_lsb();
3655 __next_bit();
3656 return __tmp;
3657 }
3658
3659 _GLIBCXX_SIMD_ALWAYS_INLINE bool
3660 operator==(const iterator& __rhs) const
3661 { return __mask == __rhs.__mask; }
3662
3663 _GLIBCXX_SIMD_ALWAYS_INLINE bool
3664 operator!=(const iterator& __rhs) const
3665 { return __mask != __rhs.__mask; }
3666 };
3667
3668 iterator
3669 begin() const
3670 { return __bits.to_ullong(); }
3671
3672 iterator
3673 end() const
3674 { return 0; }
3675 };
3676
3677template <typename _Tp, typename _Ap>
3678 where_range<simd_size_v<_Tp, _Ap>>
3679 where(const simd_mask<_Tp, _Ap>& __k)
3680 { return __k.__to_bitset(); }
3681
3682} // namespace __proposed
3683
3684// }}}1
3685// reductions [simd.reductions] {{{1
3686template <typename _Tp, typename _Abi, typename _BinaryOperation = plus<>>
3687 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp
3688 reduce(const simd<_Tp, _Abi>& __v, _BinaryOperation __binary_op = _BinaryOperation())
3689 { return _Abi::_SimdImpl::_S_reduce(__v, __binary_op); }
3690
3691template <typename _M, typename _V, typename _BinaryOperation = plus<>>
3692 _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
3693 reduce(const const_where_expression<_M, _V>& __x,
3694 typename _V::value_type __identity_element, _BinaryOperation __binary_op)
3695 {
3696 if (__builtin_expect(none_of(__get_mask(__x)), false))
3697 return __identity_element;
3698
3699 _V __tmp = __identity_element;
3700 _V::_Impl::_S_masked_assign(__data(__get_mask(__x)), __data(__tmp),
3701 __data(__get_lvalue(__x)));
3702 return reduce(__tmp, __binary_op);
3703 }
3704
3705template <typename _M, typename _V>
3706 _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
3707 reduce(const const_where_expression<_M, _V>& __x, plus<> __binary_op = {})
3708 { return reduce(__x, 0, __binary_op); }
3709
3710template <typename _M, typename _V>
3711 _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
3712 reduce(const const_where_expression<_M, _V>& __x, multiplies<> __binary_op)
3713 { return reduce(__x, 1, __binary_op); }
3714
3715template <typename _M, typename _V>
3716 _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
3717 reduce(const const_where_expression<_M, _V>& __x, bit_and<> __binary_op)
3718 { return reduce(__x, ~typename _V::value_type(), __binary_op); }
3719
3720template <typename _M, typename _V>
3721 _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
3722 reduce(const const_where_expression<_M, _V>& __x, bit_or<> __binary_op)
3723 { return reduce(__x, 0, __binary_op); }
3724
3725template <typename _M, typename _V>
3726 _GLIBCXX_SIMD_INTRINSIC typename _V::value_type
3727 reduce(const const_where_expression<_M, _V>& __x, bit_xor<> __binary_op)
3728 { return reduce(__x, 0, __binary_op); }
3729
3730template <typename _Tp, typename _Abi>
3731 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp
3732 hmin(const simd<_Tp, _Abi>& __v) noexcept
3733 { return _Abi::_SimdImpl::_S_reduce(__v, __detail::_Minimum()); }
3734
3735template <typename _Tp, typename _Abi>
3736 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp
3737 hmax(const simd<_Tp, _Abi>& __v) noexcept
3738 { return _Abi::_SimdImpl::_S_reduce(__v, __detail::_Maximum()); }
3739
3740template <typename _M, typename _V>
3741 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
3742 typename _V::value_type
3743 hmin(const const_where_expression<_M, _V>& __x) noexcept
3744 {
3745 using _Tp = typename _V::value_type;
3746 constexpr _Tp __id_elem =
3747#ifdef __FINITE_MATH_ONLY__
3748 __finite_max_v<_Tp>;
3749#else
3750 __value_or<__infinity, _Tp>(__finite_max_v<_Tp>);
3751#endif
3752 _V __tmp = __id_elem;
3753 _V::_Impl::_S_masked_assign(__data(__get_mask(__x)), __data(__tmp),
3754 __data(__get_lvalue(__x)));
3755 return _V::abi_type::_SimdImpl::_S_reduce(__tmp, __detail::_Minimum());
3756 }
3757
3758template <typename _M, typename _V>
3759 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
3760 typename _V::value_type
3761 hmax(const const_where_expression<_M, _V>& __x) noexcept
3762 {
3763 using _Tp = typename _V::value_type;
3764 constexpr _Tp __id_elem =
3765#ifdef __FINITE_MATH_ONLY__
3766 __finite_min_v<_Tp>;
3767#else
3768 [] {
3769 if constexpr (__value_exists_v<__infinity, _Tp>)
3770 return -__infinity_v<_Tp>;
3771 else
3772 return __finite_min_v<_Tp>;
3773 }();
3774#endif
3775 _V __tmp = __id_elem;
3776 _V::_Impl::_S_masked_assign(__data(__get_mask(__x)), __data(__tmp),
3777 __data(__get_lvalue(__x)));
3778 return _V::abi_type::_SimdImpl::_S_reduce(__tmp, __detail::_Maximum());
3779 }
3780
3781// }}}1
3782// algorithms [simd.alg] {{{
3783template <typename _Tp, typename _Ap>
3784 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
3785 min(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
3786 { return {__private_init, _Ap::_SimdImpl::_S_min(__data(__a), __data(__b))}; }
3787
3788template <typename _Tp, typename _Ap>
3789 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
3790 max(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
3791 { return {__private_init, _Ap::_SimdImpl::_S_max(__data(__a), __data(__b))}; }
3792
3793template <typename _Tp, typename _Ap>
3794 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
3795 pair<simd<_Tp, _Ap>, simd<_Tp, _Ap>>
3796 minmax(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
3797 {
3798 const auto pair_of_members
3799 = _Ap::_SimdImpl::_S_minmax(__data(__a), __data(__b));
3800 return {simd<_Tp, _Ap>(__private_init, pair_of_members.first),
3801 simd<_Tp, _Ap>(__private_init, pair_of_members.second)};
3802 }
3803
3804template <typename _Tp, typename _Ap>
3805 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
3806 clamp(const simd<_Tp, _Ap>& __v, const simd<_Tp, _Ap>& __lo, const simd<_Tp, _Ap>& __hi)
3807 {
3808 using _Impl = typename _Ap::_SimdImpl;
3809 return {__private_init,
3810 _Impl::_S_min(__data(__hi),
3811 _Impl::_S_max(__data(__lo), __data(__v)))};
3812 }
3813
3814// }}}
3815
3816template <size_t... _Sizes, typename _Tp, typename _Ap,
3817 typename = enable_if_t<((_Sizes + ...) == simd<_Tp, _Ap>::size())>>
3818 inline tuple<simd<_Tp, simd_abi::deduce_t<_Tp, _Sizes>>...>
3819 split(const simd<_Tp, _Ap>&);
3820
3821// __extract_part {{{
3822template <int _Index, int _Total, int _Combine = 1, typename _Tp, size_t _Np>
3823 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_CONST
3824 _SimdWrapper<_Tp, _Np / _Total * _Combine>
3825 __extract_part(const _SimdWrapper<_Tp, _Np> __x);
3826
3827template <int _Index, int _Parts, int _Combine = 1, typename _Tp, typename _A0, typename... _As>
3828 _GLIBCXX_SIMD_INTRINSIC auto
3829 __extract_part(const _SimdTuple<_Tp, _A0, _As...>& __x);
3830
3831// }}}
3832// _SizeList {{{
3833template <size_t _V0, size_t... _Values>
3834 struct _SizeList
3835 {
3836 template <size_t _I>
3837 static constexpr size_t
3838 _S_at(_SizeConstant<_I> = {})
3839 {
3840 if constexpr (_I == 0)
3841 return _V0;
3842 else
3843 return _SizeList<_Values...>::template _S_at<_I - 1>();
3844 }
3845
3846 template <size_t _I>
3847 static constexpr auto
3848 _S_before(_SizeConstant<_I> = {})
3849 {
3850 if constexpr (_I == 0)
3851 return _SizeConstant<0>();
3852 else
3853 return _SizeConstant<
3854 _V0 + _SizeList<_Values...>::template _S_before<_I - 1>()>();
3855 }
3856
3857 template <size_t _Np>
3858 static constexpr auto
3859 _S_pop_front(_SizeConstant<_Np> = {})
3860 {
3861 if constexpr (_Np == 0)
3862 return _SizeList();
3863 else
3864 return _SizeList<_Values...>::template _S_pop_front<_Np - 1>();
3865 }
3866 };
3867
3868// }}}
3869// __extract_center {{{
3870template <typename _Tp, size_t _Np>
3871 _GLIBCXX_SIMD_INTRINSIC _SimdWrapper<_Tp, _Np / 2>
3872 __extract_center(_SimdWrapper<_Tp, _Np> __x)
3873 {
3874 static_assert(_Np >= 4);
3875 static_assert(_Np % 4 == 0); // x0 - x1 - x2 - x3 -> return {x1, x2}
3876#if _GLIBCXX_SIMD_X86INTRIN // {{{
3877 if constexpr (__have_avx512f && sizeof(_Tp) * _Np == 64)
3878 {
3879 const auto __intrin = __to_intrin(__x);
3880 if constexpr (is_integral_v<_Tp>)
3881 return __vector_bitcast<_Tp>(_mm512_castsi512_si256(
3882 _mm512_shuffle_i32x4(__intrin, __intrin,
3883 1 + 2 * 0x4 + 2 * 0x10 + 3 * 0x40)));
3884 else if constexpr (sizeof(_Tp) == 4)
3885 return __vector_bitcast<_Tp>(_mm512_castps512_ps256(
3886 _mm512_shuffle_f32x4(__intrin, __intrin,
3887 1 + 2 * 0x4 + 2 * 0x10 + 3 * 0x40)));
3888 else if constexpr (sizeof(_Tp) == 8)
3889 return __vector_bitcast<_Tp>(_mm512_castpd512_pd256(
3890 _mm512_shuffle_f64x2(__intrin, __intrin,
3891 1 + 2 * 0x4 + 2 * 0x10 + 3 * 0x40)));
3892 else
3893 __assert_unreachable<_Tp>();
3894 }
3895 else if constexpr (sizeof(_Tp) * _Np == 32 && is_floating_point_v<_Tp>)
3896 return __vector_bitcast<_Tp>(
3897 _mm_shuffle_pd(__lo128(__vector_bitcast<double>(__x)),
3898 __hi128(__vector_bitcast<double>(__x)), 1));
3899 else if constexpr (sizeof(__x) == 32 && sizeof(_Tp) * _Np <= 32)
3900 return __vector_bitcast<_Tp>(
3901 _mm_alignr_epi8(__hi128(__vector_bitcast<_LLong>(__x)),
3902 __lo128(__vector_bitcast<_LLong>(__x)),
3903 sizeof(_Tp) * _Np / 4));
3904 else
3905#endif // _GLIBCXX_SIMD_X86INTRIN }}}
3906 {
3907 __vector_type_t<_Tp, _Np / 2> __r;
3908 __builtin_memcpy(&__r,
3909 reinterpret_cast<const char*>(&__x)
3910 + sizeof(_Tp) * _Np / 4,
3911 sizeof(_Tp) * _Np / 2);
3912 return __r;
3913 }
3914 }
3915
3916template <typename _Tp, typename _A0, typename... _As>
3917 _GLIBCXX_SIMD_INTRINSIC
3918 _SimdWrapper<_Tp, _SimdTuple<_Tp, _A0, _As...>::_S_size() / 2>
3919 __extract_center(const _SimdTuple<_Tp, _A0, _As...>& __x)
3920 {
3921 if constexpr (sizeof...(_As) == 0)
3922 return __extract_center(__x.first);
3923 else
3924 return __extract_part<1, 4, 2>(__x);
3925 }
3926
3927// }}}
3928// __split_wrapper {{{
3929template <size_t... _Sizes, typename _Tp, typename... _As>
3930 auto
3931 __split_wrapper(_SizeList<_Sizes...>, const _SimdTuple<_Tp, _As...>& __x)
3932 {
3933 return split<_Sizes...>(
3934 fixed_size_simd<_Tp, _SimdTuple<_Tp, _As...>::_S_size()>(__private_init,
3935 __x));
3936 }
3937
3938// }}}
3939
3940// split<simd>(simd) {{{
3941template <typename _V, typename _Ap,
3942 size_t _Parts = simd_size_v<typename _V::value_type, _Ap> / _V::size()>
3943 enable_if_t<simd_size_v<typename _V::value_type, _Ap> == _Parts * _V::size()
3944 && is_simd_v<_V>, array<_V, _Parts>>
3945 split(const simd<typename _V::value_type, _Ap>& __x)
3946 {
3947 using _Tp = typename _V::value_type;
3948 if constexpr (_Parts == 1)
3949 {
3950 return {simd_cast<_V>(__x)};
3951 }
3952 else if (__x._M_is_constprop())
3953 {
3954 return __generate_from_n_evaluations<_Parts, array<_V, _Parts>>(
3955 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
3956 return _V([&](auto __j) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA
3957 { return __x[__i * _V::size() + __j]; });
3958 });
3959 }
3960 else if constexpr (
3961 __is_fixed_size_abi_v<_Ap>
3962 && (is_same_v<typename _V::abi_type, simd_abi::scalar>
3963 || (__is_fixed_size_abi_v<typename _V::abi_type>
3964 && sizeof(_V) == sizeof(_Tp) * _V::size() // _V doesn't have padding
3965 )))
3966 {
3967 // fixed_size -> fixed_size (w/o padding) or scalar
3968#ifdef _GLIBCXX_SIMD_USE_ALIASING_LOADS
3969 const __may_alias<_Tp>* const __element_ptr
3970 = reinterpret_cast<const __may_alias<_Tp>*>(&__data(__x));
3971 return __generate_from_n_evaluations<_Parts, array<_V, _Parts>>(
3972 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA
3973 { return _V(__element_ptr + __i * _V::size(), vector_aligned); });
3974#else
3975 const auto& __xx = __data(__x);
3976 return __generate_from_n_evaluations<_Parts, array<_V, _Parts>>(
3977 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
3978 [[maybe_unused]] constexpr size_t __offset
3979 = decltype(__i)::value * _V::size();
3980 return _V([&](auto __j) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
3981 constexpr _SizeConstant<__j + __offset> __k;
3982 return __xx[__k];
3983 });
3984 });
3985#endif
3986 }
3987 else if constexpr (is_same_v<typename _V::abi_type, simd_abi::scalar>)
3988 {
3989 // normally memcpy should work here as well
3990 return __generate_from_n_evaluations<_Parts, array<_V, _Parts>>(
3991 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { return __x[__i]; });
3992 }
3993 else
3994 {
3995 return __generate_from_n_evaluations<_Parts, array<_V, _Parts>>(
3996 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
3997 if constexpr (__is_fixed_size_abi_v<typename _V::abi_type>)
3998 return _V([&](auto __j) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
3999 return __x[__i * _V::size() + __j];
4000 });
4001 else
4002 return _V(__private_init,
4003 __extract_part<decltype(__i)::value, _Parts>(__data(__x)));
4004 });
4005 }
4006 }
4007
4008// }}}
4009// split<simd_mask>(simd_mask) {{{
4010template <typename _V, typename _Ap,
4011 size_t _Parts = simd_size_v<typename _V::simd_type::value_type, _Ap> / _V::size()>
4012 enable_if_t<is_simd_mask_v<_V> && simd_size_v<typename
4013 _V::simd_type::value_type, _Ap> == _Parts * _V::size(), array<_V, _Parts>>
4014 split(const simd_mask<typename _V::simd_type::value_type, _Ap>& __x)
4015 {
4016 if constexpr (is_same_v<_Ap, typename _V::abi_type>)
4017 return {__x};
4018 else if constexpr (_Parts == 1)
4019 return {__proposed::static_simd_cast<_V>(__x)};
4020 else if constexpr (_Parts == 2 && __is_sse_abi<typename _V::abi_type>()
4021 && __is_avx_abi<_Ap>())
4022 return {_V(__private_init, __lo128(__data(__x))),
4023 _V(__private_init, __hi128(__data(__x)))};
4024 else if constexpr (_V::size() <= __CHAR_BIT__ * sizeof(_ULLong))
4025 {
4026 const bitset __bits = __x.__to_bitset();
4027 return __generate_from_n_evaluations<_Parts, array<_V, _Parts>>(
4028 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4029 constexpr size_t __offset = __i * _V::size();
4030 return _V(__bitset_init, (__bits >> __offset).to_ullong());
4031 });
4032 }
4033 else
4034 {
4035 return __generate_from_n_evaluations<_Parts, array<_V, _Parts>>(
4036 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4037 constexpr size_t __offset = __i * _V::size();
4038 return _V(__private_init,
4039 [&](auto __j) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4040 return __x[__j + __offset];
4041 });
4042 });
4043 }
4044 }
4045
4046// }}}
4047// split<_Sizes...>(simd) {{{
4048template <size_t... _Sizes, typename _Tp, typename _Ap, typename>
4049 _GLIBCXX_SIMD_ALWAYS_INLINE
4050 tuple<simd<_Tp, simd_abi::deduce_t<_Tp, _Sizes>>...>
4051 split(const simd<_Tp, _Ap>& __x)
4052 {
4053 using _SL = _SizeList<_Sizes...>;
4054 using _Tuple = tuple<__deduced_simd<_Tp, _Sizes>...>;
4055 constexpr size_t _Np = simd_size_v<_Tp, _Ap>;
4056 constexpr size_t _N0 = _SL::template _S_at<0>();
4057 using _V = __deduced_simd<_Tp, _N0>;
4058
4059 if (__x._M_is_constprop())
4060 return __generate_from_n_evaluations<sizeof...(_Sizes), _Tuple>(
4061 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4062 using _Vi = __deduced_simd<_Tp, _SL::_S_at(__i)>;
4063 constexpr size_t __offset = _SL::_S_before(__i);
4064 return _Vi([&](auto __j) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4065 return __x[__offset + __j];
4066 });
4067 });
4068 else if constexpr (_Np == _N0)
4069 {
4070 static_assert(sizeof...(_Sizes) == 1);
4071 return {simd_cast<_V>(__x)};
4072 }
4073 else if constexpr // split from fixed_size, such that __x::first.size == _N0
4074 (__is_fixed_size_abi_v<
4075 _Ap> && __fixed_size_storage_t<_Tp, _Np>::_S_first_size == _N0)
4076 {
4077 static_assert(
4078 !__is_fixed_size_abi_v<typename _V::abi_type>,
4079 "How can <_Tp, _Np> be __a single _SimdTuple entry but __a "
4080 "fixed_size_simd "
4081 "when deduced?");
4082 // extract first and recurse (__split_wrapper is needed to deduce a new
4083 // _Sizes pack)
4084 return tuple_cat(make_tuple(_V(__private_init, __data(__x).first)),
4085 __split_wrapper(_SL::template _S_pop_front<1>(),
4086 __data(__x).second));
4087 }
4088 else if constexpr ((!is_same_v<simd_abi::scalar,
4089 simd_abi::deduce_t<_Tp, _Sizes>> && ...)
4090 && (!__is_fixed_size_abi_v<
4091 simd_abi::deduce_t<_Tp, _Sizes>> && ...))
4092 {
4093 if constexpr (((_Sizes * 2 == _Np) && ...))
4094 return {{__private_init, __extract_part<0, 2>(__data(__x))},
4095 {__private_init, __extract_part<1, 2>(__data(__x))}};
4096 else if constexpr (is_same_v<_SizeList<_Sizes...>,
4097 _SizeList<_Np / 3, _Np / 3, _Np / 3>>)
4098 return {{__private_init, __extract_part<0, 3>(__data(__x))},
4099 {__private_init, __extract_part<1, 3>(__data(__x))},
4100 {__private_init, __extract_part<2, 3>(__data(__x))}};
4101 else if constexpr (is_same_v<_SizeList<_Sizes...>,
4102 _SizeList<2 * _Np / 3, _Np / 3>>)
4103 return {{__private_init, __extract_part<0, 3, 2>(__data(__x))},
4104 {__private_init, __extract_part<2, 3>(__data(__x))}};
4105 else if constexpr (is_same_v<_SizeList<_Sizes...>,
4106 _SizeList<_Np / 3, 2 * _Np / 3>>)
4107 return {{__private_init, __extract_part<0, 3>(__data(__x))},
4108 {__private_init, __extract_part<1, 3, 2>(__data(__x))}};
4109 else if constexpr (is_same_v<_SizeList<_Sizes...>,
4110 _SizeList<_Np / 2, _Np / 4, _Np / 4>>)
4111 return {{__private_init, __extract_part<0, 2>(__data(__x))},
4112 {__private_init, __extract_part<2, 4>(__data(__x))},
4113 {__private_init, __extract_part<3, 4>(__data(__x))}};
4114 else if constexpr (is_same_v<_SizeList<_Sizes...>,
4115 _SizeList<_Np / 4, _Np / 4, _Np / 2>>)
4116 return {{__private_init, __extract_part<0, 4>(__data(__x))},
4117 {__private_init, __extract_part<1, 4>(__data(__x))},
4118 {__private_init, __extract_part<1, 2>(__data(__x))}};
4119 else if constexpr (is_same_v<_SizeList<_Sizes...>,
4120 _SizeList<_Np / 4, _Np / 2, _Np / 4>>)
4121 return {{__private_init, __extract_part<0, 4>(__data(__x))},
4122 {__private_init, __extract_center(__data(__x))},
4123 {__private_init, __extract_part<3, 4>(__data(__x))}};
4124 else if constexpr (((_Sizes * 4 == _Np) && ...))
4125 return {{__private_init, __extract_part<0, 4>(__data(__x))},
4126 {__private_init, __extract_part<1, 4>(__data(__x))},
4127 {__private_init, __extract_part<2, 4>(__data(__x))},
4128 {__private_init, __extract_part<3, 4>(__data(__x))}};
4129 // else fall through
4130 }
4131#ifdef _GLIBCXX_SIMD_USE_ALIASING_LOADS
4132 const __may_alias<_Tp>* const __element_ptr
4133 = reinterpret_cast<const __may_alias<_Tp>*>(&__x);
4134 return __generate_from_n_evaluations<sizeof...(_Sizes), _Tuple>(
4135 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4136 using _Vi = __deduced_simd<_Tp, _SL::_S_at(__i)>;
4137 constexpr size_t __offset = _SL::_S_before(__i);
4138 constexpr size_t __base_align = alignof(simd<_Tp, _Ap>);
4139 constexpr size_t __a
4140 = __base_align - ((__offset * sizeof(_Tp)) % __base_align);
4141 constexpr size_t __b = ((__a - 1) & __a) ^ __a;
4142 constexpr size_t __alignment = __b == 0 ? __a : __b;
4143 return _Vi(__element_ptr + __offset, overaligned<__alignment>);
4144 });
4145#else
4146 return __generate_from_n_evaluations<sizeof...(_Sizes), _Tuple>(
4147 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4148 using _Vi = __deduced_simd<_Tp, _SL::_S_at(__i)>;
4149 const auto& __xx = __data(__x);
4150 using _Offset = decltype(_SL::_S_before(__i));
4151 return _Vi([&](auto __j) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4152 constexpr _SizeConstant<_Offset::value + __j> __k;
4153 return __xx[__k];
4154 });
4155 });
4156#endif
4157 }
4158
4159// }}}
4160
4161// __subscript_in_pack {{{
4162template <size_t _I, typename _Tp, typename _Ap, typename... _As>
4163 _GLIBCXX_SIMD_INTRINSIC constexpr _Tp
4164 __subscript_in_pack(const simd<_Tp, _Ap>& __x, const simd<_Tp, _As>&... __xs)
4165 {
4166 if constexpr (_I < simd_size_v<_Tp, _Ap>)
4167 return __x[_I];
4168 else
4169 return __subscript_in_pack<_I - simd_size_v<_Tp, _Ap>>(__xs...);
4170 }
4171
4172// }}}
4173// __store_pack_of_simd {{{
4174template <typename _Tp, typename _A0, typename... _As>
4175 _GLIBCXX_SIMD_INTRINSIC void
4176 __store_pack_of_simd(char* __mem, const simd<_Tp, _A0>& __x0, const simd<_Tp, _As>&... __xs)
4177 {
4178 constexpr size_t __n_bytes = sizeof(_Tp) * simd_size_v<_Tp, _A0>;
4179 __builtin_memcpy(__mem, &__data(__x0), __n_bytes);
4180 if constexpr (sizeof...(__xs) > 0)
4181 __store_pack_of_simd(__mem + __n_bytes, __xs...);
4182 }
4183
4184// }}}
4185// concat(simd...) {{{
4186template <typename _Tp, typename... _As, typename = __detail::__odr_helper>
4187 inline _GLIBCXX_SIMD_CONSTEXPR
4188 simd<_Tp, simd_abi::deduce_t<_Tp, (simd_size_v<_Tp, _As> + ...)>>
4189 concat(const simd<_Tp, _As>&... __xs)
4190 {
4191 using _Rp = __deduced_simd<_Tp, (simd_size_v<_Tp, _As> + ...)>;
4192 if constexpr (sizeof...(__xs) == 1)
4193 return simd_cast<_Rp>(__xs...);
4194 else if ((... && __xs._M_is_constprop()))
4195 return simd<_Tp,
4196 simd_abi::deduce_t<_Tp, (simd_size_v<_Tp, _As> + ...)>>(
4197 [&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA
4198 { return __subscript_in_pack<__i>(__xs...); });
4199 else
4200 {
4201 _Rp __r{};
4202 __store_pack_of_simd(reinterpret_cast<char*>(&__data(__r)), __xs...);
4203 return __r;
4204 }
4205 }
4206
4207// }}}
4208// concat(array<simd>) {{{
4209template <typename _Tp, typename _Abi, size_t _Np>
4210 _GLIBCXX_SIMD_ALWAYS_INLINE
4211 _GLIBCXX_SIMD_CONSTEXPR __deduced_simd<_Tp, simd_size_v<_Tp, _Abi> * _Np>
4212 concat(const array<simd<_Tp, _Abi>, _Np>& __x)
4213 {
4214 return __call_with_subscripts<_Np>(
4215 __x, [](const auto&... __xs) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4216 return concat(__xs...);
4217 });
4218 }
4219
4220// }}}
4221
4222/// @cond undocumented
4223// _SmartReference {{{
4224template <typename _Up, typename _Accessor = _Up,
4225 typename _ValueType = typename _Up::value_type>
4226 class _SmartReference
4227 {
4228 friend _Accessor;
4229 int _M_index;
4230 _Up& _M_obj;
4231
4232 _GLIBCXX_SIMD_INTRINSIC constexpr _ValueType
4233 _M_read() const noexcept
4234 {
4235 if constexpr (is_arithmetic_v<_Up>)
4236 return _M_obj;
4237 else
4238 return _M_obj[_M_index];
4239 }
4240
4241 template <typename _Tp>
4242 _GLIBCXX_SIMD_INTRINSIC constexpr void
4243 _M_write(_Tp&& __x) const
4244 { _Accessor::_S_set(_M_obj, _M_index, static_cast<_Tp&&>(__x)); }
4245
4246 public:
4247 _GLIBCXX_SIMD_INTRINSIC constexpr
4248 _SmartReference(_Up& __o, int __i) noexcept
4249 : _M_index(__i), _M_obj(__o) {}
4250
4251 using value_type = _ValueType;
4252
4253 _GLIBCXX_SIMD_INTRINSIC
4254 _SmartReference(const _SmartReference&) = delete;
4255
4256 _GLIBCXX_SIMD_INTRINSIC constexpr
4257 operator value_type() const noexcept
4258 { return _M_read(); }
4259
4260 template <typename _Tp, typename = _ValuePreservingOrInt<__remove_cvref_t<_Tp>, value_type>>
4261 _GLIBCXX_SIMD_INTRINSIC constexpr _SmartReference
4262 operator=(_Tp&& __x) &&
4263 {
4264 _M_write(static_cast<_Tp&&>(__x));
4265 return {_M_obj, _M_index};
4266 }
4267
4268#define _GLIBCXX_SIMD_OP_(__op) \
4269 template <typename _Tp, \
4270 typename _TT = decltype(declval<value_type>() __op declval<_Tp>()), \
4271 typename = _ValuePreservingOrInt<__remove_cvref_t<_Tp>, _TT>, \
4272 typename = _ValuePreservingOrInt<_TT, value_type>> \
4273 _GLIBCXX_SIMD_INTRINSIC constexpr _SmartReference \
4274 operator __op##=(_Tp&& __x) && \
4275 { \
4276 const value_type& __lhs = _M_read(); \
4277 _M_write(__lhs __op __x); \
4278 return {_M_obj, _M_index}; \
4279 }
4280 _GLIBCXX_SIMD_ALL_ARITHMETICS(_GLIBCXX_SIMD_OP_);
4281 _GLIBCXX_SIMD_ALL_SHIFTS(_GLIBCXX_SIMD_OP_);
4282 _GLIBCXX_SIMD_ALL_BINARY(_GLIBCXX_SIMD_OP_);
4283#undef _GLIBCXX_SIMD_OP_
4284
4285 template <typename _Tp = void,
4286 typename = decltype(++declval<conditional_t<true, value_type, _Tp>&>())>
4287 _GLIBCXX_SIMD_INTRINSIC constexpr _SmartReference
4288 operator++() &&
4289 {
4290 value_type __x = _M_read();
4291 _M_write(++__x);
4292 return {_M_obj, _M_index};
4293 }
4294
4295 template <typename _Tp = void,
4296 typename = decltype(declval<conditional_t<true, value_type, _Tp>&>()++)>
4297 _GLIBCXX_SIMD_INTRINSIC constexpr value_type
4298 operator++(int) &&
4299 {
4300 const value_type __r = _M_read();
4301 value_type __x = __r;
4302 _M_write(++__x);
4303 return __r;
4304 }
4305
4306 template <typename _Tp = void,
4307 typename = decltype(--declval<conditional_t<true, value_type, _Tp>&>())>
4308 _GLIBCXX_SIMD_INTRINSIC constexpr _SmartReference
4309 operator--() &&
4310 {
4311 value_type __x = _M_read();
4312 _M_write(--__x);
4313 return {_M_obj, _M_index};
4314 }
4315
4316 template <typename _Tp = void,
4317 typename = decltype(declval<conditional_t<true, value_type, _Tp>&>()--)>
4318 _GLIBCXX_SIMD_INTRINSIC constexpr value_type
4319 operator--(int) &&
4320 {
4321 const value_type __r = _M_read();
4322 value_type __x = __r;
4323 _M_write(--__x);
4324 return __r;
4325 }
4326
4327 _GLIBCXX_SIMD_INTRINSIC friend void
4328 swap(_SmartReference&& __a, _SmartReference&& __b) noexcept(
4329 conjunction<
4330 is_nothrow_constructible<value_type, _SmartReference&&>,
4331 is_nothrow_assignable<_SmartReference&&, value_type&&>>::value)
4332 {
4333 value_type __tmp = static_cast<_SmartReference&&>(__a);
4334 static_cast<_SmartReference&&>(__a) = static_cast<value_type>(__b);
4335 static_cast<_SmartReference&&>(__b) = std::move(__tmp);
4336 }
4337
4338 _GLIBCXX_SIMD_INTRINSIC friend void
4339 swap(value_type& __a, _SmartReference&& __b) noexcept(
4340 conjunction<
4341 is_nothrow_constructible<value_type, value_type&&>,
4342 is_nothrow_assignable<value_type&, value_type&&>,
4343 is_nothrow_assignable<_SmartReference&&, value_type&&>>::value)
4344 {
4345 value_type __tmp(std::move(__a));
4346 __a = static_cast<value_type>(__b);
4347 static_cast<_SmartReference&&>(__b) = std::move(__tmp);
4348 }
4349
4350 _GLIBCXX_SIMD_INTRINSIC friend void
4351 swap(_SmartReference&& __a, value_type& __b) noexcept(
4352 conjunction<
4353 is_nothrow_constructible<value_type, _SmartReference&&>,
4354 is_nothrow_assignable<value_type&, value_type&&>,
4355 is_nothrow_assignable<_SmartReference&&, value_type&&>>::value)
4356 {
4357 value_type __tmp(__a);
4358 static_cast<_SmartReference&&>(__a) = std::move(__b);
4359 __b = std::move(__tmp);
4360 }
4361 };
4362
4363// }}}
4364// __scalar_abi_wrapper {{{
4365template <int _Bytes>
4366 struct __scalar_abi_wrapper
4367 {
4368 template <typename _Tp> static constexpr size_t _S_full_size = 1;
4369 template <typename _Tp> static constexpr size_t _S_size = 1;
4370 template <typename _Tp> static constexpr size_t _S_is_partial = false;
4371
4372 template <typename _Tp, typename _Abi = simd_abi::scalar>
4373 static constexpr bool _S_is_valid_v
4374 = _Abi::template _IsValid<_Tp>::value && sizeof(_Tp) == _Bytes;
4375 };
4376
4377// }}}
4378// __decay_abi metafunction {{{
4379template <typename _Tp>
4380 struct __decay_abi { using type = _Tp; };
4381
4382template <int _Bytes>
4383 struct __decay_abi<__scalar_abi_wrapper<_Bytes>>
4384 { using type = simd_abi::scalar; };
4385
4386// }}}
4387// __find_next_valid_abi metafunction {{{1
4388// Given an ABI tag A<N>, find an N2 < N such that A<N2>::_S_is_valid_v<_Tp> ==
4389// true, N2 is a power-of-2, and A<N2>::_S_is_partial<_Tp> is false. Break
4390// recursion at 2 elements in the resulting ABI tag. In this case
4391// type::_S_is_valid_v<_Tp> may be false.
4392template <template <int> class _Abi, int _Bytes, typename _Tp>
4393 struct __find_next_valid_abi
4394 {
4395 static constexpr auto
4396 _S_choose()
4397 {
4398 constexpr int _NextBytes = std::__bit_ceil(_Bytes) / 2;
4399 using _NextAbi = _Abi<_NextBytes>;
4400 if constexpr (_NextBytes < sizeof(_Tp) * 2) // break recursion
4401 return _Abi<_Bytes>();
4402 else if constexpr (_NextAbi::template _S_is_partial<_Tp> == false
4403 && _NextAbi::template _S_is_valid_v<_Tp>)
4404 return _NextAbi();
4405 else
4406 return __find_next_valid_abi<_Abi, _NextBytes, _Tp>::_S_choose();
4407 }
4408
4409 using type = decltype(_S_choose());
4410 };
4411
4412template <int _Bytes, typename _Tp>
4413 struct __find_next_valid_abi<__scalar_abi_wrapper, _Bytes, _Tp>
4414 { using type = simd_abi::scalar; };
4415
4416// _AbiList {{{1
4417template <template <int> class...>
4418 struct _AbiList
4419 {
4420 template <typename, int> static constexpr bool _S_has_valid_abi = false;
4421 template <typename, int> using _FirstValidAbi = void;
4422 template <typename, int> using _BestAbi = void;
4423 };
4424
4425template <template <int> class _A0, template <int> class... _Rest>
4426 struct _AbiList<_A0, _Rest...>
4427 {
4428 template <typename _Tp, int _Np>
4429 static constexpr bool _S_has_valid_abi
4430 = _A0<sizeof(_Tp) * _Np>::template _S_is_valid_v<
4431 _Tp> || _AbiList<_Rest...>::template _S_has_valid_abi<_Tp, _Np>;
4432
4433 template <typename _Tp, int _Np>
4434 using _FirstValidAbi = conditional_t<
4435 _A0<sizeof(_Tp) * _Np>::template _S_is_valid_v<_Tp>,
4436 typename __decay_abi<_A0<sizeof(_Tp) * _Np>>::type,
4437 typename _AbiList<_Rest...>::template _FirstValidAbi<_Tp, _Np>>;
4438
4439 template <typename _Tp, int _Np>
4440 static constexpr auto
4441 _S_determine_best_abi()
4442 {
4443 static_assert(_Np >= 1);
4444 constexpr int _Bytes = sizeof(_Tp) * _Np;
4445 if constexpr (_Np == 1)
4446 return __make_dependent_t<_Tp, simd_abi::scalar>{};
4447 else
4448 {
4449 constexpr int __fullsize = _A0<_Bytes>::template _S_full_size<_Tp>;
4450 // _A0<_Bytes> is good if:
4451 // 1. The ABI tag is valid for _Tp
4452 // 2. The storage overhead is no more than padding to fill the next
4453 // power-of-2 number of bytes
4454 if constexpr (_A0<_Bytes>::template _S_is_valid_v<
4455 _Tp> && __fullsize / 2 < _Np)
4456 return typename __decay_abi<_A0<_Bytes>>::type{};
4457 else
4458 {
4459 using _Bp =
4460 typename __find_next_valid_abi<_A0, _Bytes, _Tp>::type;
4461 if constexpr (_Bp::template _S_is_valid_v<
4462 _Tp> && _Bp::template _S_size<_Tp> <= _Np)
4463 return _Bp{};
4464 else
4465 return
4466 typename _AbiList<_Rest...>::template _BestAbi<_Tp, _Np>{};
4467 }
4468 }
4469 }
4470
4471 template <typename _Tp, int _Np>
4472 using _BestAbi = decltype(_S_determine_best_abi<_Tp, _Np>());
4473 };
4474
4475// }}}1
4476
4477// the following lists all native ABIs, which makes them accessible to
4478// simd_abi::deduce and select_best_vector_type_t (for fixed_size). Order
4479// matters: Whatever comes first has higher priority.
4480using _AllNativeAbis = _AbiList<simd_abi::_VecBltnBtmsk, simd_abi::_VecBuiltin,
4481 __scalar_abi_wrapper>;
4482
4483// valid _SimdTraits specialization {{{1
4484template <typename _Tp, typename _Abi>
4485 struct _SimdTraits<_Tp, _Abi, void_t<typename _Abi::template _IsValid<_Tp>>>
4486 : _Abi::template __traits<_Tp> {};
4487
4488// __deduce_impl specializations {{{1
4489// try all native ABIs (including scalar) first
4490template <typename _Tp, size_t _Np>
4491 struct __deduce_impl<
4492 _Tp, _Np, enable_if_t<_AllNativeAbis::template _S_has_valid_abi<_Tp, _Np>>>
4493 { using type = _AllNativeAbis::_FirstValidAbi<_Tp, _Np>; };
4494
4495// fall back to fixed_size only if scalar and native ABIs don't match
4496template <typename _Tp, size_t _Np, typename = void>
4497 struct __deduce_fixed_size_fallback {};
4498
4499template <typename _Tp, size_t _Np>
4500 struct __deduce_fixed_size_fallback<_Tp, _Np,
4501 enable_if_t<simd_abi::fixed_size<_Np>::template _S_is_valid_v<_Tp>>>
4502 { using type = simd_abi::fixed_size<_Np>; };
4503
4504template <typename _Tp, size_t _Np, typename>
4505 struct __deduce_impl : public __deduce_fixed_size_fallback<_Tp, _Np> {};
4506
4507//}}}1
4508/// @endcond
4509
4510// simd_mask {{{
4511template <typename _Tp, typename _Abi>
4512 class simd_mask : public _SimdTraits<_Tp, _Abi>::_MaskBase
4513 {
4514 // types, tags, and friends {{{
4515 using _Traits = _SimdTraits<_Tp, _Abi>;
4516 using _MemberType = typename _Traits::_MaskMember;
4517
4518 // We map all masks with equal element sizeof to a single integer type, the
4519 // one given by __int_for_sizeof_t<_Tp>. This is the approach
4520 // [[gnu::vector_size(N)]] types take as well and it reduces the number of
4521 // template specializations in the implementation classes.
4522 using _Ip = __int_for_sizeof_t<_Tp>;
4523 static constexpr _Ip* _S_type_tag = nullptr;
4524
4525 friend typename _Traits::_MaskBase;
4526 friend class simd<_Tp, _Abi>; // to construct masks on return
4527 friend typename _Traits::_SimdImpl; // to construct masks on return and
4528 // inspect data on masked operations
4529 public:
4530 using _Impl = typename _Traits::_MaskImpl;
4531 friend _Impl;
4532
4533 // }}}
4534 // member types {{{
4535 using value_type = bool;
4536 using reference = _SmartReference<_MemberType, _Impl, value_type>;
4537 using simd_type = simd<_Tp, _Abi>;
4538 using abi_type = _Abi;
4539
4540 // }}}
4541 static constexpr size_t size() // {{{
4542 { return __size_or_zero_v<_Tp, _Abi>; }
4543
4544 // }}}
4545 // constructors & assignment {{{
4546 simd_mask() = default;
4547 simd_mask(const simd_mask&) = default;
4548 simd_mask(simd_mask&&) = default;
4549 simd_mask& operator=(const simd_mask&) = default;
4550 simd_mask& operator=(simd_mask&&) = default;
4551
4552 // }}}
4553 // access to internal representation (optional feature) {{{
4554 _GLIBCXX_SIMD_ALWAYS_INLINE explicit
4555 simd_mask(typename _Traits::_MaskCastType __init)
4556 : _M_data{__init} {}
4557 // conversions to internal type is done in _MaskBase
4558
4559 // }}}
4560 // bitset interface (extension to be proposed) {{{
4561 // TS_FEEDBACK:
4562 // Conversion of simd_mask to and from bitset makes it much easier to
4563 // interface with other facilities. I suggest adding `static
4564 // simd_mask::from_bitset` and `simd_mask::to_bitset`.
4565 _GLIBCXX_SIMD_ALWAYS_INLINE static simd_mask
4566 __from_bitset(bitset<size()> bs)
4567 { return {__bitset_init, bs}; }
4568
4569 _GLIBCXX_SIMD_ALWAYS_INLINE bitset<size()>
4570 __to_bitset() const
4571 { return _Impl::_S_to_bits(_M_data)._M_to_bitset(); }
4572
4573 // }}}
4574 // explicit broadcast constructor {{{
4575 _GLIBCXX_SIMD_ALWAYS_INLINE explicit _GLIBCXX_SIMD_CONSTEXPR
4576 simd_mask(value_type __x)
4577 : _M_data(_Impl::template _S_broadcast<_Ip>(__x)) {}
4578
4579 // }}}
4580 // implicit type conversion constructor {{{
4581 #ifdef _GLIBCXX_SIMD_ENABLE_IMPLICIT_MASK_CAST
4582 // proposed improvement
4583 template <typename _Up, typename _A2,
4584 typename = enable_if_t<simd_size_v<_Up, _A2> == size()>>
4585 _GLIBCXX_SIMD_ALWAYS_INLINE explicit(sizeof(_MemberType)
4586 != sizeof(typename _SimdTraits<_Up, _A2>::_MaskMember))
4587 simd_mask(const simd_mask<_Up, _A2>& __x)
4588 : simd_mask(__proposed::static_simd_cast<simd_mask>(__x)) {}
4589 #else
4590 // conforming to ISO/IEC 19570:2018
4591 template <typename _Up, typename = enable_if_t<conjunction<
4592 is_same<abi_type, simd_abi::fixed_size<size()>>,
4593 is_same<_Up, _Up>>::value>>
4594 _GLIBCXX_SIMD_ALWAYS_INLINE
4595 simd_mask(const simd_mask<_Up, simd_abi::fixed_size<size()>>& __x)
4596 : _M_data(_Impl::_S_from_bitmask(__data(__x), _S_type_tag)) {}
4597 #endif
4598
4599 // }}}
4600 // load constructor {{{
4601 template <typename _Flags>
4602 _GLIBCXX_SIMD_ALWAYS_INLINE
4603 simd_mask(const value_type* __mem, _IsSimdFlagType<_Flags>)
4604 : _M_data(_Impl::template _S_load<_Ip>(_Flags::template _S_apply<simd_mask>(__mem))) {}
4605
4606 template <typename _Flags>
4607 _GLIBCXX_SIMD_ALWAYS_INLINE
4608 simd_mask(const value_type* __mem, simd_mask __k, _IsSimdFlagType<_Flags>)
4609 : _M_data{}
4610 {
4611 _M_data = _Impl::_S_masked_load(_M_data, __k._M_data,
4612 _Flags::template _S_apply<simd_mask>(__mem));
4613 }
4614
4615 // }}}
4616 // loads [simd_mask.load] {{{
4617 template <typename _Flags>
4618 _GLIBCXX_SIMD_ALWAYS_INLINE void
4619 copy_from(const value_type* __mem, _IsSimdFlagType<_Flags>)
4620 { _M_data = _Impl::template _S_load<_Ip>(_Flags::template _S_apply<simd_mask>(__mem)); }
4621
4622 // }}}
4623 // stores [simd_mask.store] {{{
4624 template <typename _Flags>
4625 _GLIBCXX_SIMD_ALWAYS_INLINE void
4626 copy_to(value_type* __mem, _IsSimdFlagType<_Flags>) const
4627 { _Impl::_S_store(_M_data, _Flags::template _S_apply<simd_mask>(__mem)); }
4628
4629 // }}}
4630 // scalar access {{{
4631 _GLIBCXX_SIMD_ALWAYS_INLINE reference
4632 operator[](size_t __i)
4633 {
4634 if (__i >= size())
4635 __invoke_ub("Subscript %d is out of range [0, %d]", __i, size() - 1);
4636 return {_M_data, int(__i)};
4637 }
4638
4639 _GLIBCXX_SIMD_ALWAYS_INLINE value_type
4640 operator[](size_t __i) const
4641 {
4642 if (__i >= size())
4643 __invoke_ub("Subscript %d is out of range [0, %d]", __i, size() - 1);
4644 if constexpr (__is_scalar_abi<_Abi>())
4645 return _M_data;
4646 else
4647 return static_cast<bool>(_M_data[__i]);
4648 }
4649
4650 // }}}
4651 // negation {{{
4652 _GLIBCXX_SIMD_ALWAYS_INLINE simd_mask
4653 operator!() const
4654 { return {__private_init, _Impl::_S_bit_not(_M_data)}; }
4655
4656 // }}}
4657 // simd_mask binary operators [simd_mask.binary] {{{
4658 #ifdef _GLIBCXX_SIMD_ENABLE_IMPLICIT_MASK_CAST
4659 // simd_mask<int> && simd_mask<uint> needs disambiguation
4660 template <typename _Up, typename _A2,
4661 typename = enable_if_t<is_convertible_v<simd_mask<_Up, _A2>, simd_mask>>>
4662 _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
4663 operator&&(const simd_mask& __x, const simd_mask<_Up, _A2>& __y)
4664 {
4665 return {__private_init,
4666 _Impl::_S_logical_and(__x._M_data, simd_mask(__y)._M_data)};
4667 }
4668
4669 template <typename _Up, typename _A2,
4670 typename = enable_if_t<is_convertible_v<simd_mask<_Up, _A2>, simd_mask>>>
4671 _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
4672 operator||(const simd_mask& __x, const simd_mask<_Up, _A2>& __y)
4673 {
4674 return {__private_init,
4675 _Impl::_S_logical_or(__x._M_data, simd_mask(__y)._M_data)};
4676 }
4677 #endif // _GLIBCXX_SIMD_ENABLE_IMPLICIT_MASK_CAST
4678
4679 _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
4680 operator&&(const simd_mask& __x, const simd_mask& __y)
4681 { return {__private_init, _Impl::_S_logical_and(__x._M_data, __y._M_data)}; }
4682
4683 _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
4684 operator||(const simd_mask& __x, const simd_mask& __y)
4685 { return {__private_init, _Impl::_S_logical_or(__x._M_data, __y._M_data)}; }
4686
4687 _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
4688 operator&(const simd_mask& __x, const simd_mask& __y)
4689 { return {__private_init, _Impl::_S_bit_and(__x._M_data, __y._M_data)}; }
4690
4691 _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
4692 operator|(const simd_mask& __x, const simd_mask& __y)
4693 { return {__private_init, _Impl::_S_bit_or(__x._M_data, __y._M_data)}; }
4694
4695 _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask
4696 operator^(const simd_mask& __x, const simd_mask& __y)
4697 { return {__private_init, _Impl::_S_bit_xor(__x._M_data, __y._M_data)}; }
4698
4699 _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask&
4700 operator&=(simd_mask& __x, const simd_mask& __y)
4701 {
4702 __x._M_data = _Impl::_S_bit_and(__x._M_data, __y._M_data);
4703 return __x;
4704 }
4705
4706 _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask&
4707 operator|=(simd_mask& __x, const simd_mask& __y)
4708 {
4709 __x._M_data = _Impl::_S_bit_or(__x._M_data, __y._M_data);
4710 return __x;
4711 }
4712
4713 _GLIBCXX_SIMD_ALWAYS_INLINE friend simd_mask&
4714 operator^=(simd_mask& __x, const simd_mask& __y)
4715 {
4716 __x._M_data = _Impl::_S_bit_xor(__x._M_data, __y._M_data);
4717 return __x;
4718 }
4719
4720 // }}}
4721 // simd_mask compares [simd_mask.comparison] {{{
4722 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd_mask
4723 operator==(const simd_mask& __x, const simd_mask& __y)
4724 { return !operator!=(__x, __y); }
4725
4726 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd_mask
4727 operator!=(const simd_mask& __x, const simd_mask& __y)
4728 { return {__private_init, _Impl::_S_bit_xor(__x._M_data, __y._M_data)}; }
4729
4730 // }}}
4731 // private_init ctor {{{
4732 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
4733 simd_mask(_PrivateInit, typename _Traits::_MaskMember __init)
4734 : _M_data(__init) {}
4735
4736 // }}}
4737 // private_init generator ctor {{{
4738 template <typename _Fp, typename = decltype(bool(declval<_Fp>()(size_t())))>
4739 _GLIBCXX_SIMD_INTRINSIC constexpr
4740 simd_mask(_PrivateInit, _Fp&& __gen)
4741 : _M_data()
4742 {
4743 __execute_n_times<size()>([&](auto __i) constexpr _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4744 _Impl::_S_set(_M_data, __i, __gen(__i));
4745 });
4746 }
4747
4748 // }}}
4749 // bitset_init ctor {{{
4750 _GLIBCXX_SIMD_INTRINSIC simd_mask(_BitsetInit, bitset<size()> __init)
4751 : _M_data(_Impl::_S_from_bitmask(_SanitizedBitMask<size()>(__init), _S_type_tag))
4752 {}
4753
4754 // }}}
4755 // __cvt {{{
4756 // TS_FEEDBACK:
4757 // The conversion operator this implements should be a ctor on simd_mask.
4758 // Once you call .__cvt() on a simd_mask it converts conveniently.
4759 // A useful variation: add `explicit(sizeof(_Tp) != sizeof(_Up))`
4760 struct _CvtProxy
4761 {
4762 template <typename _Up, typename _A2,
4763 typename = enable_if_t<simd_size_v<_Up, _A2> == simd_size_v<_Tp, _Abi>>>
4764 _GLIBCXX_SIMD_ALWAYS_INLINE
4765 operator simd_mask<_Up, _A2>() &&
4766 {
4767 using namespace std::experimental::__proposed;
4768 return static_simd_cast<simd_mask<_Up, _A2>>(_M_data);
4769 }
4770
4771 const simd_mask<_Tp, _Abi>& _M_data;
4772 };
4773
4774 _GLIBCXX_SIMD_INTRINSIC _CvtProxy
4775 __cvt() const
4776 { return {*this}; }
4777
4778 // }}}
4779 // operator?: overloads (suggested extension) {{{
4780 #ifdef __GXX_CONDITIONAL_IS_OVERLOADABLE__
4781 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd_mask
4782 operator?:(const simd_mask& __k, const simd_mask& __where_true,
4783 const simd_mask& __where_false)
4784 {
4785 auto __ret = __where_false;
4786 _Impl::_S_masked_assign(__k._M_data, __ret._M_data, __where_true._M_data);
4787 return __ret;
4788 }
4789
4790 template <typename _U1, typename _U2,
4791 typename _Rp = simd<common_type_t<_U1, _U2>, _Abi>,
4792 typename = enable_if_t<conjunction_v<
4793 is_convertible<_U1, _Rp>, is_convertible<_U2, _Rp>,
4794 is_convertible<simd_mask, typename _Rp::mask_type>>>>
4795 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend _Rp
4796 operator?:(const simd_mask& __k, const _U1& __where_true,
4797 const _U2& __where_false)
4798 {
4799 _Rp __ret = __where_false;
4800 _Rp::_Impl::_S_masked_assign(
4801 __data(static_cast<typename _Rp::mask_type>(__k)), __data(__ret),
4802 __data(static_cast<_Rp>(__where_true)));
4803 return __ret;
4804 }
4805
4806 #ifdef _GLIBCXX_SIMD_ENABLE_IMPLICIT_MASK_CAST
4807 template <typename _Kp, typename _Ak, typename _Up, typename _Au,
4808 typename = enable_if_t<
4809 conjunction_v<is_convertible<simd_mask<_Kp, _Ak>, simd_mask>,
4810 is_convertible<simd_mask<_Up, _Au>, simd_mask>>>>
4811 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd_mask
4812 operator?:(const simd_mask<_Kp, _Ak>& __k, const simd_mask& __where_true,
4813 const simd_mask<_Up, _Au>& __where_false)
4814 {
4815 simd_mask __ret = __where_false;
4816 _Impl::_S_masked_assign(simd_mask(__k)._M_data, __ret._M_data,
4817 __where_true._M_data);
4818 return __ret;
4819 }
4820 #endif // _GLIBCXX_SIMD_ENABLE_IMPLICIT_MASK_CAST
4821 #endif // __GXX_CONDITIONAL_IS_OVERLOADABLE__
4822
4823 // }}}
4824 // _M_is_constprop {{{
4825 _GLIBCXX_SIMD_INTRINSIC constexpr bool
4826 _M_is_constprop() const
4827 {
4828 if constexpr (__is_scalar_abi<_Abi>())
4829 return __builtin_constant_p(_M_data);
4830 else
4831 return _M_data._M_is_constprop();
4832 }
4833
4834 // }}}
4835
4836 private:
4837 friend const auto& __data<_Tp, abi_type>(const simd_mask&);
4838 friend auto& __data<_Tp, abi_type>(simd_mask&);
4839 alignas(_Traits::_S_mask_align) _MemberType _M_data;
4840 };
4841
4842// }}}
4843
4844/// @cond undocumented
4845// __data(simd_mask) {{{
4846template <typename _Tp, typename _Ap>
4847 _GLIBCXX_SIMD_INTRINSIC constexpr const auto&
4848 __data(const simd_mask<_Tp, _Ap>& __x)
4849 { return __x._M_data; }
4850
4851template <typename _Tp, typename _Ap>
4852 _GLIBCXX_SIMD_INTRINSIC constexpr auto&
4853 __data(simd_mask<_Tp, _Ap>& __x)
4854 { return __x._M_data; }
4855
4856// }}}
4857/// @endcond
4858
4859// simd_mask reductions [simd_mask.reductions] {{{
4860template <typename _Tp, typename _Abi>
4861 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
4862 all_of(const simd_mask<_Tp, _Abi>& __k) noexcept
4863 {
4864 if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
4865 {
4866 for (size_t __i = 0; __i < simd_size_v<_Tp, _Abi>; ++__i)
4867 if (!__k[__i])
4868 return false;
4869 return true;
4870 }
4871 else
4872 return _Abi::_MaskImpl::_S_all_of(__k);
4873 }
4874
4875template <typename _Tp, typename _Abi>
4876 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
4877 any_of(const simd_mask<_Tp, _Abi>& __k) noexcept
4878 {
4879 if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
4880 {
4881 for (size_t __i = 0; __i < simd_size_v<_Tp, _Abi>; ++__i)
4882 if (__k[__i])
4883 return true;
4884 return false;
4885 }
4886 else
4887 return _Abi::_MaskImpl::_S_any_of(__k);
4888 }
4889
4890template <typename _Tp, typename _Abi>
4891 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
4892 none_of(const simd_mask<_Tp, _Abi>& __k) noexcept
4893 {
4894 if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
4895 {
4896 for (size_t __i = 0; __i < simd_size_v<_Tp, _Abi>; ++__i)
4897 if (__k[__i])
4898 return false;
4899 return true;
4900 }
4901 else
4902 return _Abi::_MaskImpl::_S_none_of(__k);
4903 }
4904
4905template <typename _Tp, typename _Abi>
4906 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
4907 some_of(const simd_mask<_Tp, _Abi>& __k) noexcept
4908 {
4909 if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
4910 {
4911 for (size_t __i = 1; __i < simd_size_v<_Tp, _Abi>; ++__i)
4912 if (__k[__i] != __k[__i - 1])
4913 return true;
4914 return false;
4915 }
4916 else
4917 return _Abi::_MaskImpl::_S_some_of(__k);
4918 }
4919
4920template <typename _Tp, typename _Abi>
4921 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
4922 popcount(const simd_mask<_Tp, _Abi>& __k) noexcept
4923 {
4924 if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
4925 {
4926 const int __r = __call_with_subscripts<simd_size_v<_Tp, _Abi>>(
4927 __k, [](auto... __elements) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4928 return ((__elements != 0) + ...);
4929 });
4930 if (__builtin_is_constant_evaluated() || __builtin_constant_p(__r))
4931 return __r;
4932 }
4933 return _Abi::_MaskImpl::_S_popcount(__k);
4934 }
4935
4936template <typename _Tp, typename _Abi>
4937 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
4938 find_first_set(const simd_mask<_Tp, _Abi>& __k)
4939 {
4940 if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
4941 {
4942 constexpr size_t _Np = simd_size_v<_Tp, _Abi>;
4943 const size_t _Idx = __call_with_n_evaluations<_Np>(
4944 [](auto... __indexes) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4945 return std::min({__indexes...});
4946 }, [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4947 return __k[__i] ? +__i : _Np;
4948 });
4949 if (_Idx >= _Np)
4950 __invoke_ub("find_first_set(empty mask) is UB");
4951 if (__builtin_constant_p(_Idx))
4952 return _Idx;
4953 }
4954 return _Abi::_MaskImpl::_S_find_first_set(__k);
4955 }
4956
4957template <typename _Tp, typename _Abi>
4958 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
4959 find_last_set(const simd_mask<_Tp, _Abi>& __k)
4960 {
4961 if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
4962 {
4963 constexpr size_t _Np = simd_size_v<_Tp, _Abi>;
4964 const int _Idx = __call_with_n_evaluations<_Np>(
4965 [](auto... __indexes) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4966 return std::max({__indexes...});
4967 }, [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
4968 return __k[__i] ? int(__i) : -1;
4969 });
4970 if (_Idx < 0)
4971 __invoke_ub("find_first_set(empty mask) is UB");
4972 if (__builtin_constant_p(_Idx))
4973 return _Idx;
4974 }
4975 return _Abi::_MaskImpl::_S_find_last_set(__k);
4976 }
4977
4978_GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
4979all_of(_ExactBool __x) noexcept
4980{ return __x; }
4981
4982_GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
4983any_of(_ExactBool __x) noexcept
4984{ return __x; }
4985
4986_GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
4987none_of(_ExactBool __x) noexcept
4988{ return !__x; }
4989
4990_GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR bool
4991some_of(_ExactBool) noexcept
4992{ return false; }
4993
4994_GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
4995popcount(_ExactBool __x) noexcept
4996{ return __x; }
4997
4998_GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
4999find_first_set(_ExactBool)
5000{ return 0; }
5001
5002_GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR int
5003find_last_set(_ExactBool)
5004{ return 0; }
5005
5006// }}}
5007
5008/// @cond undocumented
5009// _SimdIntOperators{{{1
5010template <typename _V, typename _Tp, typename _Abi, bool>
5011 class _SimdIntOperators {};
5012
5013template <typename _V, typename _Tp, typename _Abi>
5014 class _SimdIntOperators<_V, _Tp, _Abi, true>
5015 {
5016 using _Impl = typename _SimdTraits<_Tp, _Abi>::_SimdImpl;
5017
5018 _GLIBCXX_SIMD_INTRINSIC const _V& __derived() const
5019 { return *static_cast<const _V*>(this); }
5020
5021 template <typename _Up>
5022 _GLIBCXX_SIMD_INTRINSIC static _GLIBCXX_SIMD_CONSTEXPR _V
5023 _S_make_derived(_Up&& __d)
5024 { return {__private_init, static_cast<_Up&&>(__d)}; }
5025
5026 public:
5027 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5028 _V&
5029 operator%=(_V& __lhs, const _V& __x)
5030 { return __lhs = __lhs % __x; }
5031
5032 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5033 _V&
5034 operator&=(_V& __lhs, const _V& __x)
5035 { return __lhs = __lhs & __x; }
5036
5037 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5038 _V&
5039 operator|=(_V& __lhs, const _V& __x)
5040 { return __lhs = __lhs | __x; }
5041
5042 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5043 _V&
5044 operator^=(_V& __lhs, const _V& __x)
5045 { return __lhs = __lhs ^ __x; }
5046
5047 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5048 _V&
5049 operator<<=(_V& __lhs, const _V& __x)
5050 { return __lhs = __lhs << __x; }
5051
5052 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5053 _V&
5054 operator>>=(_V& __lhs, const _V& __x)
5055 { return __lhs = __lhs >> __x; }
5056
5057 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5058 _V&
5059 operator<<=(_V& __lhs, int __x)
5060 { return __lhs = __lhs << __x; }
5061
5062 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5063 _V&
5064 operator>>=(_V& __lhs, int __x)
5065 { return __lhs = __lhs >> __x; }
5066
5067 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5068 _V
5069 operator%(const _V& __x, const _V& __y)
5070 {
5071 return _SimdIntOperators::_S_make_derived(
5072 _Impl::_S_modulus(__data(__x), __data(__y)));
5073 }
5074
5075 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5076 _V
5077 operator&(const _V& __x, const _V& __y)
5078 {
5079 return _SimdIntOperators::_S_make_derived(
5080 _Impl::_S_bit_and(__data(__x), __data(__y)));
5081 }
5082
5083 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5084 _V
5085 operator|(const _V& __x, const _V& __y)
5086 {
5087 return _SimdIntOperators::_S_make_derived(
5088 _Impl::_S_bit_or(__data(__x), __data(__y)));
5089 }
5090
5091 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5092 _V
5093 operator^(const _V& __x, const _V& __y)
5094 {
5095 return _SimdIntOperators::_S_make_derived(
5096 _Impl::_S_bit_xor(__data(__x), __data(__y)));
5097 }
5098
5099 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5100 _V
5101 operator<<(const _V& __x, const _V& __y)
5102 {
5103 return _SimdIntOperators::_S_make_derived(
5104 _Impl::_S_bit_shift_left(__data(__x), __data(__y)));
5105 }
5106
5107 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5108 _V
5109 operator>>(const _V& __x, const _V& __y)
5110 {
5111 return _SimdIntOperators::_S_make_derived(
5112 _Impl::_S_bit_shift_right(__data(__x), __data(__y)));
5113 }
5114
5115 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5116 _V
5117 operator<<(const _V& __x, int __y)
5118 {
5119 if (__y < 0)
5120 __invoke_ub("The behavior is undefined if the right operand of a "
5121 "shift operation is negative. [expr.shift]\nA shift by "
5122 "%d was requested",
5123 __y);
5124 if (size_t(__y) >= sizeof(declval<_Tp>() << __y) * __CHAR_BIT__)
5125 __invoke_ub(
5126 "The behavior is undefined if the right operand of a "
5127 "shift operation is greater than or equal to the width of the "
5128 "promoted left operand. [expr.shift]\nA shift by %d was requested",
5129 __y);
5130 return _SimdIntOperators::_S_make_derived(
5131 _Impl::_S_bit_shift_left(__data(__x), __y));
5132 }
5133
5134 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend
5135 _V
5136 operator>>(const _V& __x, int __y)
5137 {
5138 if (__y < 0)
5139 __invoke_ub(
5140 "The behavior is undefined if the right operand of a shift "
5141 "operation is negative. [expr.shift]\nA shift by %d was requested",
5142 __y);
5143 if (size_t(__y) >= sizeof(declval<_Tp>() << __y) * __CHAR_BIT__)
5144 __invoke_ub(
5145 "The behavior is undefined if the right operand of a shift "
5146 "operation is greater than or equal to the width of the promoted "
5147 "left operand. [expr.shift]\nA shift by %d was requested",
5148 __y);
5149 return _SimdIntOperators::_S_make_derived(
5150 _Impl::_S_bit_shift_right(__data(__x), __y));
5151 }
5152
5153 // unary operators (for integral _Tp)
5154 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR
5155 _V
5156 operator~() const
5157 { return {__private_init, _Impl::_S_complement(__derived()._M_data)}; }
5158 };
5159
5160//}}}1
5161/// @endcond
5162
5163// simd {{{
5164template <typename _Tp, typename _Abi>
5165 class simd : public _SimdIntOperators<
5166 simd<_Tp, _Abi>, _Tp, _Abi,
5167 conjunction<is_integral<_Tp>,
5168 typename _SimdTraits<_Tp, _Abi>::_IsValid>::value>,
5169 public _SimdTraits<_Tp, _Abi>::_SimdBase
5170 {
5171 using _Traits = _SimdTraits<_Tp, _Abi>;
5172 using _MemberType = typename _Traits::_SimdMember;
5173 using _CastType = typename _Traits::_SimdCastType;
5174 static constexpr _Tp* _S_type_tag = nullptr;
5175 friend typename _Traits::_SimdBase;
5176
5177 public:
5178 using _Impl = typename _Traits::_SimdImpl;
5179 friend _Impl;
5180 friend _SimdIntOperators<simd, _Tp, _Abi, true>;
5181
5182 using value_type = _Tp;
5183 using reference = _SmartReference<_MemberType, _Impl, value_type>;
5184 using mask_type = simd_mask<_Tp, _Abi>;
5185 using abi_type = _Abi;
5186
5187 static constexpr size_t size()
5188 { return __size_or_zero_v<_Tp, _Abi>; }
5189
5190 _GLIBCXX_SIMD_CONSTEXPR simd() = default;
5191 _GLIBCXX_SIMD_CONSTEXPR simd(const simd&) = default;
5192 _GLIBCXX_SIMD_CONSTEXPR simd(simd&&) noexcept = default;
5193 _GLIBCXX_SIMD_CONSTEXPR simd& operator=(const simd&) = default;
5194 _GLIBCXX_SIMD_CONSTEXPR simd& operator=(simd&&) noexcept = default;
5195
5196 // implicit broadcast constructor
5197 template <typename _Up,
5198 typename = enable_if_t<!is_same_v<__remove_cvref_t<_Up>, bool>>>
5199 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR
5200 simd(_ValuePreservingOrInt<_Up, value_type>&& __x)
5201 : _M_data(
5202 _Impl::_S_broadcast(static_cast<value_type>(static_cast<_Up&&>(__x))))
5203 {}
5204
5205 // implicit type conversion constructor (convert from fixed_size to
5206 // fixed_size)
5207 template <typename _Up>
5208 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR
5209 simd(const simd<_Up, simd_abi::fixed_size<size()>>& __x,
5210 enable_if_t<
5211 conjunction<
5212 is_same<simd_abi::fixed_size<size()>, abi_type>,
5213 negation<__is_narrowing_conversion<_Up, value_type>>,
5214 __converts_to_higher_integer_rank<_Up, value_type>>::value,
5215 void*> = nullptr)
5216 : simd{static_cast<array<_Up, size()>>(__x).data(), vector_aligned} {}
5217
5218 // explicit type conversion constructor
5219#ifdef _GLIBCXX_SIMD_ENABLE_STATIC_CAST
5220 template <typename _Up, typename _A2,
5221 typename = decltype(static_simd_cast<simd>(
5222 declval<const simd<_Up, _A2>&>()))>
5223 _GLIBCXX_SIMD_ALWAYS_INLINE explicit _GLIBCXX_SIMD_CONSTEXPR
5224 simd(const simd<_Up, _A2>& __x)
5225 : simd(static_simd_cast<simd>(__x)) {}
5226#endif // _GLIBCXX_SIMD_ENABLE_STATIC_CAST
5227
5228 // generator constructor
5229 template <typename _Fp>
5230 _GLIBCXX_SIMD_ALWAYS_INLINE explicit _GLIBCXX_SIMD_CONSTEXPR
5231 simd(_Fp&& __gen, _ValuePreservingOrInt<decltype(declval<_Fp>()(
5232 declval<_SizeConstant<0>&>())),
5233 value_type>* = nullptr)
5234 : _M_data(_Impl::_S_generator(static_cast<_Fp&&>(__gen), _S_type_tag)) {}
5235
5236 // load constructor
5237 template <typename _Up, typename _Flags>
5238 _GLIBCXX_SIMD_ALWAYS_INLINE
5239 simd(const _Up* __mem, _IsSimdFlagType<_Flags>)
5240 : _M_data(
5241 _Impl::_S_load(_Flags::template _S_apply<simd>(__mem), _S_type_tag))
5242 {}
5243
5244 // loads [simd.load]
5245 template <typename _Up, typename _Flags>
5246 _GLIBCXX_SIMD_ALWAYS_INLINE void
5247 copy_from(const _Vectorizable<_Up>* __mem, _IsSimdFlagType<_Flags>)
5248 {
5249 _M_data = static_cast<decltype(_M_data)>(
5250 _Impl::_S_load(_Flags::template _S_apply<simd>(__mem), _S_type_tag));
5251 }
5252
5253 // stores [simd.store]
5254 template <typename _Up, typename _Flags>
5255 _GLIBCXX_SIMD_ALWAYS_INLINE void
5256 copy_to(_Vectorizable<_Up>* __mem, _IsSimdFlagType<_Flags>) const
5257 {
5258 _Impl::_S_store(_M_data, _Flags::template _S_apply<simd>(__mem),
5259 _S_type_tag);
5260 }
5261
5262 // scalar access
5263 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR reference
5264 operator[](size_t __i)
5265 { return {_M_data, int(__i)}; }
5266
5267 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR value_type
5268 operator[]([[maybe_unused]] size_t __i) const
5269 {
5270 if constexpr (__is_scalar_abi<_Abi>())
5271 {
5272 _GLIBCXX_DEBUG_ASSERT(__i == 0);
5273 return _M_data;
5274 }
5275 else
5276 return _M_data[__i];
5277 }
5278
5279 // increment and decrement:
5280 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd&
5281 operator++()
5282 {
5283 _Impl::_S_increment(_M_data);
5284 return *this;
5285 }
5286
5287 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd
5288 operator++(int)
5289 {
5290 simd __r = *this;
5291 _Impl::_S_increment(_M_data);
5292 return __r;
5293 }
5294
5295 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd&
5296 operator--()
5297 {
5298 _Impl::_S_decrement(_M_data);
5299 return *this;
5300 }
5301
5302 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd
5303 operator--(int)
5304 {
5305 simd __r = *this;
5306 _Impl::_S_decrement(_M_data);
5307 return __r;
5308 }
5309
5310 // unary operators (for any _Tp)
5311 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR mask_type
5312 operator!() const
5313 { return {__private_init, _Impl::_S_negate(_M_data)}; }
5314
5315 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd
5316 operator+() const
5317 { return *this; }
5318
5319 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR simd
5320 operator-() const
5321 { return {__private_init, _Impl::_S_unary_minus(_M_data)}; }
5322
5323 // access to internal representation (suggested extension)
5324 _GLIBCXX_SIMD_ALWAYS_INLINE explicit _GLIBCXX_SIMD_CONSTEXPR
5325 simd(_CastType __init) : _M_data(__init) {}
5326
5327 // compound assignment [simd.cassign]
5328 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd&
5329 operator+=(simd& __lhs, const simd& __x)
5330 { return __lhs = __lhs + __x; }
5331
5332 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd&
5333 operator-=(simd& __lhs, const simd& __x)
5334 { return __lhs = __lhs - __x; }
5335
5336 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd&
5337 operator*=(simd& __lhs, const simd& __x)
5338 { return __lhs = __lhs * __x; }
5339
5340 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd&
5341 operator/=(simd& __lhs, const simd& __x)
5342 { return __lhs = __lhs / __x; }
5343
5344 // binary operators [simd.binary]
5345 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd
5346 operator+(const simd& __x, const simd& __y)
5347 { return {__private_init, _Impl::_S_plus(__x._M_data, __y._M_data)}; }
5348
5349 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd
5350 operator-(const simd& __x, const simd& __y)
5351 { return {__private_init, _Impl::_S_minus(__x._M_data, __y._M_data)}; }
5352
5353 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd
5354 operator*(const simd& __x, const simd& __y)
5355 { return {__private_init, _Impl::_S_multiplies(__x._M_data, __y._M_data)}; }
5356
5357 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd
5358 operator/(const simd& __x, const simd& __y)
5359 { return {__private_init, _Impl::_S_divides(__x._M_data, __y._M_data)}; }
5360
5361 // compares [simd.comparison]
5362 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
5363 operator==(const simd& __x, const simd& __y)
5364 { return simd::_S_make_mask(_Impl::_S_equal_to(__x._M_data, __y._M_data)); }
5365
5366 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
5367 operator!=(const simd& __x, const simd& __y)
5368 {
5369 return simd::_S_make_mask(
5370 _Impl::_S_not_equal_to(__x._M_data, __y._M_data));
5371 }
5372
5373 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
5374 operator<(const simd& __x, const simd& __y)
5375 { return simd::_S_make_mask(_Impl::_S_less(__x._M_data, __y._M_data)); }
5376
5377 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
5378 operator<=(const simd& __x, const simd& __y)
5379 {
5380 return simd::_S_make_mask(_Impl::_S_less_equal(__x._M_data, __y._M_data));
5381 }
5382
5383 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
5384 operator>(const simd& __x, const simd& __y)
5385 { return simd::_S_make_mask(_Impl::_S_less(__y._M_data, __x._M_data)); }
5386
5387 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend mask_type
5388 operator>=(const simd& __x, const simd& __y)
5389 {
5390 return simd::_S_make_mask(_Impl::_S_less_equal(__y._M_data, __x._M_data));
5391 }
5392
5393 // operator?: overloads (suggested extension) {{{
5394#ifdef __GXX_CONDITIONAL_IS_OVERLOADABLE__
5395 _GLIBCXX_SIMD_ALWAYS_INLINE _GLIBCXX_SIMD_CONSTEXPR friend simd
5396 operator?:(const mask_type& __k, const simd& __where_true,
5397 const simd& __where_false)
5398 {
5399 auto __ret = __where_false;
5400 _Impl::_S_masked_assign(__data(__k), __data(__ret), __data(__where_true));
5401 return __ret;
5402 }
5403
5404#endif // __GXX_CONDITIONAL_IS_OVERLOADABLE__
5405 // }}}
5406
5407 // "private" because of the first arguments's namespace
5408 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
5409 simd(_PrivateInit, const _MemberType& __init)
5410 : _M_data(__init) {}
5411
5412 // "private" because of the first arguments's namespace
5413 _GLIBCXX_SIMD_INTRINSIC
5414 simd(_BitsetInit, bitset<size()> __init) : _M_data()
5415 { where(mask_type(__bitset_init, __init), *this) = ~*this; }
5416
5417 _GLIBCXX_SIMD_INTRINSIC constexpr bool
5418 _M_is_constprop() const
5419 {
5420 if constexpr (__is_scalar_abi<_Abi>())
5421 return __builtin_constant_p(_M_data);
5422 else
5423 return _M_data._M_is_constprop();
5424 }
5425
5426 private:
5427 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR static mask_type
5428 _S_make_mask(typename mask_type::_MemberType __k)
5429 { return {__private_init, __k}; }
5430
5431 friend const auto& __data<value_type, abi_type>(const simd&);
5432 friend auto& __data<value_type, abi_type>(simd&);
5433 alignas(_Traits::_S_simd_align) _MemberType _M_data;
5434 };
5435
5436// }}}
5437/// @cond undocumented
5438// __data {{{
5439template <typename _Tp, typename _Ap>
5440 _GLIBCXX_SIMD_INTRINSIC constexpr const auto&
5441 __data(const simd<_Tp, _Ap>& __x)
5442 { return __x._M_data; }
5443
5444template <typename _Tp, typename _Ap>
5445 _GLIBCXX_SIMD_INTRINSIC constexpr auto&
5446 __data(simd<_Tp, _Ap>& __x)
5447 { return __x._M_data; }
5448
5449// }}}
5450namespace __float_bitwise_operators { //{{{
5451template <typename _Tp, typename _Ap>
5452 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
5453 operator^(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
5454 { return {__private_init, _Ap::_SimdImpl::_S_bit_xor(__data(__a), __data(__b))}; }
5455
5456template <typename _Tp, typename _Ap>
5457 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
5458 operator|(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
5459 { return {__private_init, _Ap::_SimdImpl::_S_bit_or(__data(__a), __data(__b))}; }
5460
5461template <typename _Tp, typename _Ap>
5462 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR simd<_Tp, _Ap>
5463 operator&(const simd<_Tp, _Ap>& __a, const simd<_Tp, _Ap>& __b)
5464 { return {__private_init, _Ap::_SimdImpl::_S_bit_and(__data(__a), __data(__b))}; }
5465
5466template <typename _Tp, typename _Ap>
5467 _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
5468 enable_if_t<is_floating_point_v<_Tp>, simd<_Tp, _Ap>>
5469 operator~(const simd<_Tp, _Ap>& __a)
5470 { return {__private_init, _Ap::_SimdImpl::_S_complement(__data(__a))}; }
5471} // namespace __float_bitwise_operators }}}
5472/// @endcond
5473
5474/// @}
5475_GLIBCXX_SIMD_END_NAMESPACE
5476
5477#endif // __cplusplus >= 201703L
5478#endif // _GLIBCXX_EXPERIMENTAL_SIMD_H
5479
5480// vim: foldmethod=marker foldmarker={{{,}}}
constexpr _If_is_unsigned_integer< _Tp, int > popcount(_Tp __x) noexcept
The number of bits set in x.
Definition: bit:426
constexpr duration< __common_rep_t< _Rep1, __disable_if_is_duration< _Rep2 > >, _Period > operator%(const duration< _Rep1, _Period > &__d, const _Rep2 &__s)
Definition: chrono.h:790
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition: complex:395
constexpr complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
Definition: complex:365
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition: complex:335
constexpr complex< _Tp > operator/(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x divided by y.
Definition: complex:425
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition: type_traits:1640
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
Definition: type_traits:1983
void void_t
A metafunction that always yields void, used for detecting valid types.
Definition: type_traits:2632
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:82
typename conditional< _Cond, _Iftrue, _Iffalse >::type conditional_t
Alias template for conditional.
Definition: type_traits:2614
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
Definition: type_traits:85
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition: type_traits:2610
constexpr auto tuple_cat(_Tpls &&... __tpls) -> typename __tuple_cat_result< _Tpls... >::__type
Create a tuple containing all elements from multiple tuple-like objects.
Definition: tuple:2141
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition: type_traits:2386
constexpr tuple< typename __decay_and_strip< _Elements >::__type... > make_tuple(_Elements &&... __args)
Create a tuple containing copies of the arguments.
Definition: tuple:1987
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:104
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:257
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:233
constexpr _Tp reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
Calculate reduction of values in a range.
Definition: numeric:287
constexpr bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition: bitset:1573
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1593
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1683
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition: bitset:1563
constexpr bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition: bitset:1553