Where Online Learning is simpler!
The C and C++ Include Header Files
/usr/include/c++/13/experimental/bits/simd.h
$ cat -n /usr/include/c++/13/experimental/bits/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 //
. 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
33 #include
34 #ifdef _GLIBCXX_DEBUG_UB 35 #include
// for stderr 36 #endif 37 #include
38 #include
39 #include
40 #include
41 #include
42 43 #if _GLIBCXX_SIMD_X86INTRIN 44 #include
45 #elif _GLIBCXX_SIMD_HAVE_NEON 46 #pragma GCC diagnostic push 47 // narrowing conversion of '__a' from 'uint64_t' {aka 'long long unsigned int'} to 48 // 'int64x1_t' {aka 'long long int'} [-Wnarrowing] 49 #pragma GCC diagnostic ignored "-Wnarrowing" 50 #include
51 #pragma GCC diagnostic pop 52 #endif 53 54 /** @ingroup ts_simd 55 * @{ 56 */ 57 /* There are several closely related types, with the following naming 58 * convention: 59 * _Tp: vectorizable (arithmetic) type (or any type) 60 * _TV: __vector_type_t<_Tp, _Np> 61 * _TW: _SimdWrapper<_Tp, _Np> 62 * _TI: __intrinsic_type_t<_Tp, _Np> 63 * _TVT: _VectorTraits<_TV> or _VectorTraits<_TW> 64 * If one additional type is needed use _U instead of _T. 65 * Otherwise use _T\d, _TV\d, _TW\d, TI\d, _TVT\d. 66 * 67 * More naming conventions: 68 * _Ap or _Abi: An ABI tag from the simd_abi namespace 69 * _Ip: often used for integer types with sizeof(_Ip) == sizeof(_Tp), 70 * _IV, _IW as for _TV, _TW 71 * _Np: number of elements (not bytes) 72 * _Bytes: number of bytes 73 * 74 * Variable names: 75 * __k: mask object (vector- or bitmask) 76 */ 77 _GLIBCXX_SIMD_BEGIN_NAMESPACE 78 79 #if !_GLIBCXX_SIMD_X86INTRIN 80 using __m128 [[__gnu__::__vector_size__(16)]] = float; 81 using __m128d [[__gnu__::__vector_size__(16)]] = double; 82 using __m128i [[__gnu__::__vector_size__(16)]] = long long; 83 using __m256 [[__gnu__::__vector_size__(32)]] = float; 84 using __m256d [[__gnu__::__vector_size__(32)]] = double; 85 using __m256i [[__gnu__::__vector_size__(32)]] = long long; 86 using __m512 [[__gnu__::__vector_size__(64)]] = float; 87 using __m512d [[__gnu__::__vector_size__(64)]] = double; 88 using __m512i [[__gnu__::__vector_size__(64)]] = long long; 89 #endif 90 91 namespace simd_abi { 92 // simd_abi forward declarations {{{ 93 // implementation details: 94 struct _Scalar; 95 96 template
97 struct _Fixed; 98 99 // There are two major ABIs that appear on different architectures. 100 // Both have non-boolean values packed into an N Byte register 101 // -> #elements = N / sizeof(T) 102 // Masks differ: 103 // 1. Use value vector registers for masks (all 0 or all 1) 104 // 2. Use bitmasks (mask registers) with one bit per value in the corresponding 105 // value vector 106 // 107 // Both can be partially used, masking off the rest when doing horizontal 108 // operations or operations that can trap (e.g. FP_INVALID or integer division 109 // by 0). This is encoded as the number of used bytes. 110 template
111 struct _VecBuiltin; 112 113 template
114 struct _VecBltnBtmsk; 115 116 template
117 using _VecN = _VecBuiltin
; 118 119 template
120 using _Sse = _VecBuiltin<_UsedBytes>; 121 122 template
123 using _Avx = _VecBuiltin<_UsedBytes>; 124 125 template
126 using _Avx512 = _VecBltnBtmsk<_UsedBytes>; 127 128 template
129 using _Neon = _VecBuiltin<_UsedBytes>; 130 131 // implementation-defined: 132 using __sse = _Sse<>; 133 using __avx = _Avx<>; 134 using __avx512 = _Avx512<>; 135 using __neon = _Neon<>; 136 using __neon128 = _Neon<16>; 137 using __neon64 = _Neon<8>; 138 139 // standard: 140 template
141 struct deduce; 142 143 template
144 using fixed_size = _Fixed<_Np>; 145 146 using scalar = _Scalar; 147 148 // }}} 149 } // namespace simd_abi 150 // forward declarations is_simd(_mask), simd(_mask), simd_size {{{ 151 template
152 struct is_simd; 153 154 template
155 struct is_simd_mask; 156 157 template
158 class simd; 159 160 template
161 class simd_mask; 162 163 template
164 struct simd_size; 165 166 // }}} 167 // load/store flags {{{ 168 struct element_aligned_tag 169 { 170 template
171 static constexpr size_t _S_alignment = alignof(_Up); 172 173 template
174 _GLIBCXX_SIMD_INTRINSIC static constexpr _Up* 175 _S_apply(_Up* __ptr) 176 { return __ptr; } 177 }; 178 179 struct vector_aligned_tag 180 { 181 template
182 static constexpr size_t _S_alignment 183 = std::__bit_ceil(sizeof(_Up) * _Tp::size()); 184 185 template
186 _GLIBCXX_SIMD_INTRINSIC static constexpr _Up* 187 _S_apply(_Up* __ptr) 188 { return static_cast<_Up*>(__builtin_assume_aligned(__ptr, _S_alignment<_Tp, _Up>)); } 189 }; 190 191 template
struct overaligned_tag 192 { 193 template
194 static constexpr size_t _S_alignment = _Np; 195 196 template
197 _GLIBCXX_SIMD_INTRINSIC static constexpr _Up* 198 _S_apply(_Up* __ptr) 199 { return static_cast<_Up*>(__builtin_assume_aligned(__ptr, _Np)); } 200 }; 201 202 inline constexpr element_aligned_tag element_aligned = {}; 203 204 inline constexpr vector_aligned_tag vector_aligned = {}; 205 206 template
207 inline constexpr overaligned_tag<_Np> overaligned = {}; 208 209 // }}} 210 template
211 using _SizeConstant = integral_constant
; 212 // constexpr feature detection{{{ 213 constexpr inline bool __have_mmx = _GLIBCXX_SIMD_HAVE_MMX; 214 constexpr inline bool __have_sse = _GLIBCXX_SIMD_HAVE_SSE; 215 constexpr inline bool __have_sse2 = _GLIBCXX_SIMD_HAVE_SSE2; 216 constexpr inline bool __have_sse3 = _GLIBCXX_SIMD_HAVE_SSE3; 217 constexpr inline bool __have_ssse3 = _GLIBCXX_SIMD_HAVE_SSSE3; 218 constexpr inline bool __have_sse4_1 = _GLIBCXX_SIMD_HAVE_SSE4_1; 219 constexpr inline bool __have_sse4_2 = _GLIBCXX_SIMD_HAVE_SSE4_2; 220 constexpr inline bool __have_xop = _GLIBCXX_SIMD_HAVE_XOP; 221 constexpr inline bool __have_avx = _GLIBCXX_SIMD_HAVE_AVX; 222 constexpr inline bool __have_avx2 = _GLIBCXX_SIMD_HAVE_AVX2; 223 constexpr inline bool __have_bmi = _GLIBCXX_SIMD_HAVE_BMI1; 224 constexpr inline bool __have_bmi2 = _GLIBCXX_SIMD_HAVE_BMI2; 225 constexpr inline bool __have_lzcnt = _GLIBCXX_SIMD_HAVE_LZCNT; 226 constexpr inline bool __have_sse4a = _GLIBCXX_SIMD_HAVE_SSE4A; 227 constexpr inline bool __have_fma = _GLIBCXX_SIMD_HAVE_FMA; 228 constexpr inline bool __have_fma4 = _GLIBCXX_SIMD_HAVE_FMA4; 229 constexpr inline bool __have_f16c = _GLIBCXX_SIMD_HAVE_F16C; 230 constexpr inline bool __have_popcnt = _GLIBCXX_SIMD_HAVE_POPCNT; 231 constexpr inline bool __have_avx512f = _GLIBCXX_SIMD_HAVE_AVX512F; 232 constexpr inline bool __have_avx512dq = _GLIBCXX_SIMD_HAVE_AVX512DQ; 233 constexpr inline bool __have_avx512vl = _GLIBCXX_SIMD_HAVE_AVX512VL; 234 constexpr inline bool __have_avx512bw = _GLIBCXX_SIMD_HAVE_AVX512BW; 235 constexpr inline bool __have_avx512dq_vl = __have_avx512dq && __have_avx512vl; 236 constexpr inline bool __have_avx512bw_vl = __have_avx512bw && __have_avx512vl; 237 constexpr inline bool __have_avx512bitalg = _GLIBCXX_SIMD_HAVE_AVX512BITALG; 238 constexpr inline bool __have_avx512vbmi2 = _GLIBCXX_SIMD_HAVE_AVX512VBMI2; 239 constexpr inline bool __have_avx512vbmi = _GLIBCXX_SIMD_HAVE_AVX512VBMI; 240 constexpr inline bool __have_avx512ifma = _GLIBCXX_SIMD_HAVE_AVX512IFMA; 241 constexpr inline bool __have_avx512cd = _GLIBCXX_SIMD_HAVE_AVX512CD; 242 constexpr inline bool __have_avx512vnni = _GLIBCXX_SIMD_HAVE_AVX512VNNI; 243 constexpr inline bool __have_avx512vpopcntdq = _GLIBCXX_SIMD_HAVE_AVX512VPOPCNTDQ; 244 constexpr inline bool __have_avx512vp2intersect = _GLIBCXX_SIMD_HAVE_AVX512VP2INTERSECT; 245 246 constexpr inline bool __have_neon = _GLIBCXX_SIMD_HAVE_NEON; 247 constexpr inline bool __have_neon_a32 = _GLIBCXX_SIMD_HAVE_NEON_A32; 248 constexpr inline bool __have_neon_a64 = _GLIBCXX_SIMD_HAVE_NEON_A64; 249 constexpr inline bool __support_neon_float = 250 #if defined __GCC_IEC_559 251 __GCC_IEC_559 == 0; 252 #elif defined __FAST_MATH__ 253 true; 254 #else 255 false; 256 #endif 257 258 #ifdef _ARCH_PWR10 259 constexpr inline bool __have_power10vec = true; 260 #else 261 constexpr inline bool __have_power10vec = false; 262 #endif 263 #ifdef __POWER9_VECTOR__ 264 constexpr inline bool __have_power9vec = true; 265 #else 266 constexpr inline bool __have_power9vec = false; 267 #endif 268 #if defined __POWER8_VECTOR__ 269 constexpr inline bool __have_power8vec = true; 270 #else 271 constexpr inline bool __have_power8vec = __have_power9vec; 272 #endif 273 #if defined __VSX__ 274 constexpr inline bool __have_power_vsx = true; 275 #else 276 constexpr inline bool __have_power_vsx = __have_power8vec; 277 #endif 278 #if defined __ALTIVEC__ 279 constexpr inline bool __have_power_vmx = true; 280 #else 281 constexpr inline bool __have_power_vmx = __have_power_vsx; 282 #endif 283 284 // }}} 285 286 namespace __detail 287 { 288 #ifdef math_errhandling 289 // Determines _S_handle_fpexcept from math_errhandling if it is defined and expands to a constant 290 // expression. math_errhandling may expand to an extern symbol, in which case a constexpr value 291 // must be guessed. 292 template
293 constexpr bool 294 __handle_fpexcept_impl(int) 295 { return math_errhandling & MATH_ERREXCEPT; } 296 #endif 297 298 // Fallback if math_errhandling doesn't work: with fast-math assume floating-point exceptions are 299 // ignored, otherwise implement correct exception behavior. 300 constexpr bool 301 __handle_fpexcept_impl(float) 302 { 303 #if defined __FAST_MATH__ 304 return false; 305 #else 306 return true; 307 #endif 308 } 309 310 /// True if math functions must raise floating-point exceptions as specified by C17. 311 static constexpr bool _S_handle_fpexcept = __handle_fpexcept_impl(0); 312 313 constexpr std::uint_least64_t 314 __floating_point_flags() 315 { 316 std::uint_least64_t __flags = 0; 317 if constexpr (_S_handle_fpexcept) 318 __flags |= 1; 319 #ifdef __FAST_MATH__ 320 __flags |= 1 << 1; 321 #elif __FINITE_MATH_ONLY__ 322 __flags |= 2 << 1; 323 #elif __GCC_IEC_559 < 2 324 __flags |= 3 << 1; 325 #endif 326 __flags |= (__FLT_EVAL_METHOD__ + 1) << 3; 327 return __flags; 328 } 329 330 constexpr std::uint_least64_t 331 __machine_flags() 332 { 333 if constexpr (__have_mmx || __have_sse) 334 return __have_mmx 335 | (__have_sse << 1) 336 | (__have_sse2 << 2) 337 | (__have_sse3 << 3) 338 | (__have_ssse3 << 4) 339 | (__have_sse4_1 << 5) 340 | (__have_sse4_2 << 6) 341 | (__have_xop << 7) 342 | (__have_avx << 8) 343 | (__have_avx2 << 9) 344 | (__have_bmi << 10) 345 | (__have_bmi2 << 11) 346 | (__have_lzcnt << 12) 347 | (__have_sse4a << 13) 348 | (__have_fma << 14) 349 | (__have_fma4 << 15) 350 | (__have_f16c << 16) 351 | (__have_popcnt << 17) 352 | (__have_avx512f << 18) 353 | (__have_avx512dq << 19) 354 | (__have_avx512vl << 20) 355 | (__have_avx512bw << 21) 356 | (__have_avx512bitalg << 22) 357 | (__have_avx512vbmi2 << 23) 358 | (__have_avx512vbmi << 24) 359 | (__have_avx512ifma << 25) 360 | (__have_avx512cd << 26) 361 | (__have_avx512vnni << 27) 362 | (__have_avx512vpopcntdq << 28) 363 | (__have_avx512vp2intersect << 29); 364 else if constexpr (__have_neon) 365 return __have_neon 366 | (__have_neon_a32 << 1) 367 | (__have_neon_a64 << 2) 368 | (__have_neon_a64 << 2) 369 | (__support_neon_float << 3); 370 else if constexpr (__have_power_vmx) 371 return __have_power_vmx 372 | (__have_power_vsx << 1) 373 | (__have_power8vec << 2) 374 | (__have_power9vec << 3) 375 | (__have_power10vec << 4); 376 else 377 return 0; 378 } 379 380 namespace 381 { 382 struct _OdrEnforcer {}; 383 } 384 385 template
386 struct _MachineFlagsTemplate {}; 387 388 /**@internal 389 * Use this type as default template argument to all function templates that 390 * are not declared always_inline. It ensures, that a function 391 * specialization, which the compiler decides not to inline, has a unique symbol 392 * (_OdrEnforcer) or a symbol matching the machine/architecture flags 393 * (_MachineFlagsTemplate). This helps to avoid ODR violations in cases where 394 * users link TUs compiled with different flags. This is especially important 395 * for using simd in libraries. 396 */ 397 using __odr_helper 398 = conditional_t<__machine_flags() == 0, _OdrEnforcer, 399 _MachineFlagsTemplate<__machine_flags(), __floating_point_flags()>>; 400 401 struct _Minimum 402 { 403 template
404 _GLIBCXX_SIMD_INTRINSIC constexpr 405 _Tp 406 operator()(_Tp __a, _Tp __b) const 407 { 408 using std::min; 409 return min(__a, __b); 410 } 411 }; 412 413 struct _Maximum 414 { 415 template
416 _GLIBCXX_SIMD_INTRINSIC constexpr 417 _Tp 418 operator()(_Tp __a, _Tp __b) const 419 { 420 using std::max; 421 return max(__a, __b); 422 } 423 }; 424 } // namespace __detail 425 426 // unrolled/pack execution helpers 427 // __execute_n_times{{{ 428 template
429 [[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr 430 void 431 __execute_on_index_sequence(_Fp&& __f, index_sequence<_I...>) 432 { ((void)__f(_SizeConstant<_I>()), ...); } 433 434 template
435 _GLIBCXX_SIMD_INTRINSIC constexpr void 436 __execute_on_index_sequence(_Fp&&, index_sequence<>) 437 { } 438 439 template
440 _GLIBCXX_SIMD_INTRINSIC constexpr void 441 __execute_n_times(_Fp&& __f) 442 { 443 __execute_on_index_sequence(static_cast<_Fp&&>(__f), 444 make_index_sequence<_Np>{}); 445 } 446 447 // }}} 448 // __generate_from_n_evaluations{{{ 449 template
450 [[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr 451 _R 452 __execute_on_index_sequence_with_return(_Fp&& __f, index_sequence<_I...>) 453 { return _R{__f(_SizeConstant<_I>())...}; } 454 455 template
456 _GLIBCXX_SIMD_INTRINSIC constexpr _R 457 __generate_from_n_evaluations(_Fp&& __f) 458 { 459 return __execute_on_index_sequence_with_return<_R>( 460 static_cast<_Fp&&>(__f), make_index_sequence<_Np>{}); 461 } 462 463 // }}} 464 // __call_with_n_evaluations{{{ 465 template
466 [[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr 467 auto 468 __call_with_n_evaluations(index_sequence<_I...>, _F0&& __f0, _FArgs&& __fargs) 469 { return __f0(__fargs(_SizeConstant<_I>())...); } 470 471 template
472 _GLIBCXX_SIMD_INTRINSIC constexpr auto 473 __call_with_n_evaluations(_F0&& __f0, _FArgs&& __fargs) 474 { 475 return __call_with_n_evaluations(make_index_sequence<_Np>{}, 476 static_cast<_F0&&>(__f0), 477 static_cast<_FArgs&&>(__fargs)); 478 } 479 480 // }}} 481 // __call_with_subscripts{{{ 482 template
483 [[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr 484 auto 485 __call_with_subscripts(_Tp&& __x, index_sequence<_It...>, _Fp&& __fun) 486 { return __fun(__x[_First + _It]...); } 487 488 template
489 _GLIBCXX_SIMD_INTRINSIC constexpr auto 490 __call_with_subscripts(_Tp&& __x, _Fp&& __fun) 491 { 492 return __call_with_subscripts<_First>(static_cast<_Tp&&>(__x), 493 make_index_sequence<_Np>(), 494 static_cast<_Fp&&>(__fun)); 495 } 496 497 // }}} 498 499 // vvv ---- type traits ---- vvv 500 // integer type aliases{{{ 501 using _UChar = unsigned char; 502 using _SChar = signed char; 503 using _UShort = unsigned short; 504 using _UInt = unsigned int; 505 using _ULong = unsigned long; 506 using _ULLong = unsigned long long; 507 using _LLong = long long; 508 509 //}}} 510 // __first_of_pack{{{ 511 template
512 struct __first_of_pack 513 { using type = _T0; }; 514 515 template
516 using __first_of_pack_t = typename __first_of_pack<_Ts...>::type; 517 518 //}}} 519 // __value_type_or_identity_t {{{ 520 template
521 typename _Tp::value_type 522 __value_type_or_identity_impl(int); 523 524 template
525 _Tp 526 __value_type_or_identity_impl(float); 527 528 template
529 using __value_type_or_identity_t 530 = decltype(__value_type_or_identity_impl<_Tp>(int())); 531 532 // }}} 533 // __is_vectorizable {{{ 534 template
535 struct __is_vectorizable : public is_arithmetic<_Tp> {}; 536 537 template <> 538 struct __is_vectorizable
: public false_type {}; 539 540 template
541 inline constexpr bool __is_vectorizable_v = __is_vectorizable<_Tp>::value; 542 543 // Deduces to a vectorizable type 544 template
>> 545 using _Vectorizable = _Tp; 546 547 // }}} 548 // _LoadStorePtr / __is_possible_loadstore_conversion {{{ 549 template
550 struct __is_possible_loadstore_conversion 551 : conjunction<__is_vectorizable<_Ptr>, __is_vectorizable<_ValueType>> {}; 552 553 template <> 554 struct __is_possible_loadstore_conversion
: true_type {}; 555 556 // Deduces to a type allowed for load/store with the given value type. 557 template
::value>> 560 using _LoadStorePtr = _Ptr; 561 562 // }}} 563 // __is_bitmask{{{ 564 template
> 565 struct __is_bitmask : false_type {}; 566 567 template
568 inline constexpr bool __is_bitmask_v = __is_bitmask<_Tp>::value; 569 570 // the __mmaskXX case: 571 template
572 struct __is_bitmask<_Tp, 573 void_t
() = declval<_Tp>() & 1u)>> 574 : true_type {}; 575 576 // }}} 577 // __int_for_sizeof{{{ 578 #pragma GCC diagnostic push 579 #pragma GCC diagnostic ignored "-Wpedantic" 580 template
581 constexpr auto 582 __int_for_sizeof() 583 { 584 static_assert(_Bytes > 0); 585 if constexpr (_Bytes == sizeof(int)) 586 return int(); 587 else if constexpr (_Bytes == sizeof(_SChar)) 588 return _SChar(); 589 else if constexpr (_Bytes == sizeof(short)) 590 return short(); 591 else if constexpr (_Bytes == sizeof(long)) 592 return long(); 593 else if constexpr (_Bytes == sizeof(_LLong)) 594 return _LLong(); 595 #ifdef __SIZEOF_INT128__ 596 else if constexpr (_Bytes == sizeof(__int128)) 597 return __int128(); 598 #endif // __SIZEOF_INT128__ 599 else if constexpr (_Bytes % sizeof(int) == 0) 600 { 601 constexpr size_t _Np = _Bytes / sizeof(int); 602 struct _Ip 603 { 604 int _M_data[_Np]; 605 606 _GLIBCXX_SIMD_INTRINSIC constexpr _Ip 607 operator&(_Ip __rhs) const 608 { 609 return __generate_from_n_evaluations<_Np, _Ip>( 610 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { 611 return __rhs._M_data[__i] & _M_data[__i]; 612 }); 613 } 614 615 _GLIBCXX_SIMD_INTRINSIC constexpr _Ip 616 operator|(_Ip __rhs) const 617 { 618 return __generate_from_n_evaluations<_Np, _Ip>( 619 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { 620 return __rhs._M_data[__i] | _M_data[__i]; 621 }); 622 } 623 624 _GLIBCXX_SIMD_INTRINSIC constexpr _Ip 625 operator^(_Ip __rhs) const 626 { 627 return __generate_from_n_evaluations<_Np, _Ip>( 628 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { 629 return __rhs._M_data[__i] ^ _M_data[__i]; 630 }); 631 } 632 633 _GLIBCXX_SIMD_INTRINSIC constexpr _Ip 634 operator~() const 635 { 636 return __generate_from_n_evaluations<_Np, _Ip>( 637 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { return ~_M_data[__i]; }); 638 } 639 }; 640 return _Ip{}; 641 } 642 else 643 static_assert(_Bytes == 0, "this should be unreachable"); 644 } 645 #pragma GCC diagnostic pop 646 647 template
648 using __int_for_sizeof_t = decltype(__int_for_sizeof
()); 649 650 template
651 using __int_with_sizeof_t = decltype(__int_for_sizeof<_Np>()); 652 653 // }}} 654 // __is_fixed_size_abi{{{ 655 template
656 struct __is_fixed_size_abi : false_type {}; 657 658 template
659 struct __is_fixed_size_abi
> : true_type {}; 660 661 template
662 inline constexpr bool __is_fixed_size_abi_v = __is_fixed_size_abi<_Tp>::value; 663 664 // }}} 665 // __is_scalar_abi {{{ 666 template
667 constexpr bool 668 __is_scalar_abi() 669 { return is_same_v
; } 670 671 // }}} 672 // __abi_bytes_v {{{ 673 template
class _Abi, int _Bytes> 674 constexpr int 675 __abi_bytes_impl(_Abi<_Bytes>*) 676 { return _Bytes; } 677 678 template
679 constexpr int 680 __abi_bytes_impl(_Tp*) 681 { return -1; } 682 683 template
684 inline constexpr int __abi_bytes_v 685 = __abi_bytes_impl(static_cast<_Abi*>(nullptr)); 686 687 // }}} 688 // __is_builtin_bitmask_abi {{{ 689 template
690 constexpr bool 691 __is_builtin_bitmask_abi() 692 { return is_same_v
>, _Abi>; } 693 694 // }}} 695 // __is_sse_abi {{{ 696 template
697 constexpr bool 698 __is_sse_abi() 699 { 700 constexpr auto _Bytes = __abi_bytes_v<_Abi>; 701 return _Bytes <= 16 && is_same_v
, _Abi>; 702 } 703 704 // }}} 705 // __is_avx_abi {{{ 706 template
707 constexpr bool 708 __is_avx_abi() 709 { 710 constexpr auto _Bytes = __abi_bytes_v<_Abi>; 711 return _Bytes > 16 && _Bytes <= 32 712 && is_same_v
, _Abi>; 713 } 714 715 // }}} 716 // __is_avx512_abi {{{ 717 template
718 constexpr bool 719 __is_avx512_abi() 720 { 721 constexpr auto _Bytes = __abi_bytes_v<_Abi>; 722 return _Bytes <= 64 && is_same_v
, _Abi>; 723 } 724 725 // }}} 726 // __is_neon_abi {{{ 727 template
728 constexpr bool 729 __is_neon_abi() 730 { 731 constexpr auto _Bytes = __abi_bytes_v<_Abi>; 732 return _Bytes <= 16 && is_same_v
, _Abi>; 733 } 734 735 // }}} 736 // __make_dependent_t {{{ 737 template
738 struct __make_dependent 739 { using type = _Up; }; 740 741 template
742 using __make_dependent_t = typename __make_dependent<_Tp, _Up>::type; 743 744 // }}} 745 // ^^^ ---- type traits ---- ^^^ 746 747 // __invoke_ub{{{ 748 template
749 [[noreturn]] _GLIBCXX_SIMD_ALWAYS_INLINE void 750 __invoke_ub([[maybe_unused]] const char* __msg, [[maybe_unused]] const _Args&... __args) 751 { 752 #ifdef _GLIBCXX_DEBUG_UB 753 __builtin_fprintf(stderr, __msg, __args...); 754 __builtin_trap(); 755 #else 756 __builtin_unreachable(); 757 #endif 758 } 759 760 // }}} 761 // __assert_unreachable{{{ 762 template
763 struct __assert_unreachable 764 { static_assert(!is_same_v<_Tp, _Tp>, "this should be unreachable"); }; 765 766 // }}} 767 // __size_or_zero_v {{{ 768 template
::value> 769 constexpr size_t 770 __size_or_zero_dispatch(int) 771 { return _Np; } 772 773 template
774 constexpr size_t 775 __size_or_zero_dispatch(float) 776 { return 0; } 777 778 template
779 inline constexpr size_t __size_or_zero_v 780 = __size_or_zero_dispatch<_Tp, _Ap>(0); 781 782 // }}} 783 // __div_roundup {{{ 784 inline constexpr size_t 785 __div_roundup(size_t __a, size_t __b) 786 { return (__a + __b - 1) / __b; } 787 788 // }}} 789 // _ExactBool{{{ 790 class _ExactBool 791 { 792 const bool _M_data; 793 794 public: 795 _GLIBCXX_SIMD_INTRINSIC constexpr 796 _ExactBool(bool __b) : _M_data(__b) {} 797 798 _ExactBool(int) = delete; 799 800 _GLIBCXX_SIMD_INTRINSIC constexpr 801 operator bool() const 802 { return _M_data; } 803 }; 804 805 // }}} 806 // __may_alias{{{ 807 /**@internal 808 * Helper __may_alias<_Tp> that turns _Tp into the type to be used for an 809 * aliasing pointer. This adds the __may_alias attribute to _Tp (with compilers 810 * that support it). 811 */ 812 template
813 using __may_alias [[__gnu__::__may_alias__]] = _Tp; 814 815 // }}} 816 // _UnsupportedBase {{{ 817 // simd and simd_mask base for unsupported <_Tp, _Abi> 818 struct _UnsupportedBase 819 { 820 _UnsupportedBase() = delete; 821 _UnsupportedBase(const _UnsupportedBase&) = delete; 822 _UnsupportedBase& operator=(const _UnsupportedBase&) = delete; 823 ~_UnsupportedBase() = delete; 824 }; 825 826 // }}} 827 // _InvalidTraits {{{ 828 /** 829 * @internal 830 * Defines the implementation of __a given <_Tp, _Abi>. 831 * 832 * Implementations must ensure that only valid <_Tp, _Abi> instantiations are 833 * possible. Static assertions in the type definition do not suffice. It is 834 * important that SFINAE works. 835 */ 836 struct _InvalidTraits 837 { 838 using _IsValid = false_type; 839 using _SimdBase = _UnsupportedBase; 840 using _MaskBase = _UnsupportedBase; 841 842 static constexpr size_t _S_full_size = 0; 843 static constexpr bool _S_is_partial = false; 844 845 static constexpr size_t _S_simd_align = 1; 846 struct _SimdImpl; 847 struct _SimdMember {}; 848 struct _SimdCastType; 849 850 static constexpr size_t _S_mask_align = 1; 851 struct _MaskImpl; 852 struct _MaskMember {}; 853 struct _MaskCastType; 854 }; 855 856 // }}} 857 // _SimdTraits {{{ 858 template
> 859 struct _SimdTraits : _InvalidTraits {}; 860 861 // }}} 862 // __private_init, __bitset_init{{{ 863 /** 864 * @internal 865 * Tag used for private init constructor of simd and simd_mask 866 */ 867 inline constexpr struct _PrivateInit {} __private_init = {}; 868 869 inline constexpr struct _BitsetInit {} __bitset_init = {}; 870 871 // }}} 872 // __is_narrowing_conversion<_From, _To>{{{ 873 template
, 874 bool = is_arithmetic_v<_To>> 875 struct __is_narrowing_conversion; 876 877 // ignore "signed/unsigned mismatch" in the following trait. 878 // The implicit conversions will do the right thing here. 879 template
880 struct __is_narrowing_conversion<_From, _To, true, true> 881 : public __bool_constant<( 882 __digits_v<_From> > __digits_v<_To> 883 || __finite_max_v<_From> > __finite_max_v<_To> 884 || __finite_min_v<_From> < __finite_min_v<_To> 885 || (is_signed_v<_From> && is_unsigned_v<_To>))> {}; 886 887 template
888 struct __is_narrowing_conversion<_Tp, bool, true, true> 889 : public true_type {}; 890 891 template <> 892 struct __is_narrowing_conversion
893 : public false_type {}; 894 895 template
896 struct __is_narrowing_conversion<_Tp, _Tp, true, true> 897 : public false_type {}; 898 899 template
900 struct __is_narrowing_conversion<_From, _To, false, true> 901 : public negation
> {}; 902 903 // }}} 904 // __converts_to_higher_integer_rank{{{ 905 template
906 struct __converts_to_higher_integer_rank : public true_type {}; 907 908 // this may fail for char -> short if sizeof(char) == sizeof(short) 909 template
910 struct __converts_to_higher_integer_rank<_From, _To, false> 911 : public is_same
() + declval<_To>()), _To> {}; 912 913 // }}} 914 // __data(simd/simd_mask) {{{ 915 template
916 _GLIBCXX_SIMD_INTRINSIC constexpr const auto& 917 __data(const simd<_Tp, _Ap>& __x); 918 919 template
920 _GLIBCXX_SIMD_INTRINSIC constexpr auto& 921 __data(simd<_Tp, _Ap>& __x); 922 923 template
924 _GLIBCXX_SIMD_INTRINSIC constexpr const auto& 925 __data(const simd_mask<_Tp, _Ap>& __x); 926 927 template
928 _GLIBCXX_SIMD_INTRINSIC constexpr auto& 929 __data(simd_mask<_Tp, _Ap>& __x); 930 931 // }}} 932 // _SimdConverter {{{ 933 template
935 struct _SimdConverter; 936 937 template
938 struct _SimdConverter<_Tp, _Ap, _Tp, _Ap, void> 939 { 940 template
941 _GLIBCXX_SIMD_INTRINSIC const _Up& 942 operator()(const _Up& __x) 943 { return __x; } 944 }; 945 946 // }}} 947 // __to_value_type_or_member_type {{{ 948 template
949 _GLIBCXX_SIMD_INTRINSIC constexpr auto 950 __to_value_type_or_member_type(const _V& __x) -> decltype(__data(__x)) 951 { return __data(__x); } 952 953 template
954 _GLIBCXX_SIMD_INTRINSIC constexpr const typename _V::value_type& 955 __to_value_type_or_member_type(const typename _V::value_type& __x) 956 { return __x; } 957 958 // }}} 959 // __bool_storage_member_type{{{ 960 template
961 struct __bool_storage_member_type; 962 963 template
964 using __bool_storage_member_type_t = 965 typename __bool_storage_member_type<_Size>::type; 966 967 // }}} 968 // _SimdTuple {{{ 969 // why not tuple? 970 // 1. tuple gives no guarantee about the storage order, but I require 971 // storage 972 // equivalent to array<_Tp, _Np> 973 // 2. direct access to the element type (first template argument) 974 // 3. enforces equal element type, only different _Abi types are allowed 975 template
976 struct _SimdTuple; 977 978 //}}} 979 // __fixed_size_storage_t {{{ 980 template
981 struct __fixed_size_storage; 982 983 template
984 using __fixed_size_storage_t = typename __fixed_size_storage<_Tp, _Np>::type; 985 986 // }}} 987 // _SimdWrapper fwd decl{{{ 988 template
> 989 struct _SimdWrapper; 990 991 template
992 using _SimdWrapper8 = _SimdWrapper<_Tp, 8 / sizeof(_Tp)>; 993 template
994 using _SimdWrapper16 = _SimdWrapper<_Tp, 16 / sizeof(_Tp)>; 995 template
996 using _SimdWrapper32 = _SimdWrapper<_Tp, 32 / sizeof(_Tp)>; 997 template
998 using _SimdWrapper64 = _SimdWrapper<_Tp, 64 / sizeof(_Tp)>; 999 1000 // }}} 1001 // __is_simd_wrapper {{{ 1002 template
1003 struct __is_simd_wrapper : false_type {}; 1004 1005 template
1006 struct __is_simd_wrapper<_SimdWrapper<_Tp, _Np>> : true_type {}; 1007 1008 template
1009 inline constexpr bool __is_simd_wrapper_v = __is_simd_wrapper<_Tp>::value; 1010 1011 // }}} 1012 // _BitOps {{{ 1013 struct _BitOps 1014 { 1015 // _S_bit_iteration {{{ 1016 template
1017 static void 1018 _S_bit_iteration(_Tp __mask, _Fp&& __f) 1019 { 1020 static_assert(sizeof(_ULLong) >= sizeof(_Tp)); 1021 conditional_t
__k; 1022 if constexpr (is_convertible_v<_Tp, decltype(__k)>) 1023 __k = __mask; 1024 else 1025 __k = __mask.to_ullong(); 1026 while(__k) 1027 { 1028 __f(std::__countr_zero(__k)); 1029 __k &= (__k - 1); 1030 } 1031 } 1032 1033 //}}} 1034 }; 1035 1036 //}}} 1037 // __increment, __decrement {{{ 1038 template
1039 struct __increment 1040 { constexpr _Tp operator()(_Tp __a) const { return ++__a; } }; 1041 1042 template <> 1043 struct __increment
1044 { 1045 template
1046 constexpr _Tp 1047 operator()(_Tp __a) const 1048 { return ++__a; } 1049 }; 1050 1051 template
1052 struct __decrement 1053 { constexpr _Tp operator()(_Tp __a) const { return --__a; } }; 1054 1055 template <> 1056 struct __decrement
1057 { 1058 template
1059 constexpr _Tp 1060 operator()(_Tp __a) const 1061 { return --__a; } 1062 }; 1063 1064 // }}} 1065 // _ValuePreserving(OrInt) {{{ 1066 template
, _To>>::value>> 1069 using _ValuePreserving = _From; 1070 1071 template
, 1073 typename = enable_if_t
, 1075 disjunction< 1076 is_same<_DecayedFrom, _To>, is_same<_DecayedFrom, int>, 1077 conjunction
, is_unsigned<_To>>, 1078 negation<__is_narrowing_conversion<_DecayedFrom, _To>>>>::value>> 1079 using _ValuePreservingOrInt = _From; 1080 1081 // }}} 1082 // __intrinsic_type {{{ 1083 template
> 1084 struct __intrinsic_type; 1085 1086 template
1087 using __intrinsic_type_t = 1088 typename __intrinsic_type<_Tp, _Size * sizeof(_Tp)>::type; 1089 1090 template
1091 using __intrinsic_type2_t = typename __intrinsic_type<_Tp, 2>::type; 1092 template
1093 using __intrinsic_type4_t = typename __intrinsic_type<_Tp, 4>::type; 1094 template
1095 using __intrinsic_type8_t = typename __intrinsic_type<_Tp, 8>::type; 1096 template
1097 using __intrinsic_type16_t = typename __intrinsic_type<_Tp, 16>::type; 1098 template
1099 using __intrinsic_type32_t = typename __intrinsic_type<_Tp, 32>::type; 1100 template
1101 using __intrinsic_type64_t = typename __intrinsic_type<_Tp, 64>::type; 1102 1103 // }}} 1104 // _BitMask {{{ 1105 template
1106 struct _BitMask; 1107 1108 template
1109 struct __is_bitmask<_BitMask<_Np, _Sanitized>, void> : true_type {}; 1110 1111 template
1112 using _SanitizedBitMask = _BitMask<_Np, true>; 1113 1114 template
1115 struct _BitMask 1116 { 1117 static_assert(_Np > 0); 1118 1119 static constexpr size_t _NBytes = __div_roundup(_Np, __CHAR_BIT__); 1120 1121 using _Tp = conditional_t<_Np == 1, bool, 1122 make_unsigned_t<__int_with_sizeof_t
>>; 1124 1125 static constexpr int _S_array_size = __div_roundup(_NBytes, sizeof(_Tp)); 1126 1127 _Tp _M_bits[_S_array_size]; 1128 1129 static constexpr int _S_unused_bits 1130 = _Np == 1 ? 0 : _S_array_size * sizeof(_Tp) * __CHAR_BIT__ - _Np; 1131 1132 static constexpr _Tp _S_bitmask = +_Tp(~_Tp()) >> _S_unused_bits; 1133 1134 constexpr _BitMask() noexcept = default; 1135 1136 constexpr _BitMask(unsigned long long __x) noexcept 1137 : _M_bits{static_cast<_Tp>(__x)} {} 1138 1139 _BitMask(bitset<_Np> __x) noexcept : _BitMask(__x.to_ullong()) {} 1140 1141 constexpr _BitMask(const _BitMask&) noexcept = default; 1142 1143 template
> 1145 constexpr _BitMask(const _BitMask<_Np, _RhsSanitized>& __rhs) noexcept 1146 : _BitMask(__rhs._M_sanitized()) {} 1147 1148 constexpr operator _SimdWrapper
() const noexcept 1149 { 1150 static_assert(_S_array_size == 1); 1151 return _M_bits[0]; 1152 } 1153 1154 // precondition: is sanitized 1155 constexpr _Tp 1156 _M_to_bits() const noexcept 1157 { 1158 static_assert(_S_array_size == 1); 1159 return _M_bits[0]; 1160 } 1161 1162 // precondition: is sanitized 1163 constexpr unsigned long long 1164 to_ullong() const noexcept 1165 { 1166 static_assert(_S_array_size == 1); 1167 return _M_bits[0]; 1168 } 1169 1170 // precondition: is sanitized 1171 constexpr unsigned long 1172 to_ulong() const noexcept 1173 { 1174 static_assert(_S_array_size == 1); 1175 return _M_bits[0]; 1176 } 1177 1178 constexpr bitset<_Np> 1179 _M_to_bitset() const noexcept 1180 { 1181 static_assert(_S_array_size == 1); 1182 return _M_bits[0]; 1183 } 1184 1185 constexpr decltype(auto) 1186 _M_sanitized() const noexcept 1187 { 1188 if constexpr (_Sanitized) 1189 return *this; 1190 else if constexpr (_Np == 1) 1191 return _SanitizedBitMask<_Np>(_M_bits[0]); 1192 else 1193 { 1194 _SanitizedBitMask<_Np> __r = {}; 1195 for (int __i = 0; __i < _S_array_size; ++__i) 1196 __r._M_bits[__i] = _M_bits[__i]; 1197 if constexpr (_S_unused_bits > 0) 1198 __r._M_bits[_S_array_size - 1] &= _S_bitmask; 1199 return __r; 1200 } 1201 } 1202 1203 template
1204 constexpr _BitMask<_Np + _Mp, _Sanitized> 1205 _M_prepend(_BitMask<_Mp, _LSanitized> __lsb) const noexcept 1206 { 1207 constexpr size_t _RN = _Np + _Mp; 1208 using _Rp = _BitMask<_RN, _Sanitized>; 1209 if constexpr (_Rp::_S_array_size == 1) 1210 { 1211 _Rp __r{{_M_bits[0]}}; 1212 __r._M_bits[0] <<= _Mp; 1213 __r._M_bits[0] |= __lsb._M_sanitized()._M_bits[0]; 1214 return __r; 1215 } 1216 else 1217 __assert_unreachable<_Rp>(); 1218 } 1219 1220 // Return a new _BitMask with size _NewSize while dropping _DropLsb least 1221 // significant bits. If the operation implicitly produces a sanitized bitmask, 1222 // the result type will have _Sanitized set. 1223 template
1224 constexpr auto 1225 _M_extract() const noexcept 1226 { 1227 static_assert(_Np > _DropLsb); 1228 static_assert(_DropLsb + _NewSize <= sizeof(_ULLong) * __CHAR_BIT__, 1229 "not implemented for bitmasks larger than one ullong"); 1230 if constexpr (_NewSize == 1) 1231 // must sanitize because the return _Tp is bool 1232 return _SanitizedBitMask<1>(_M_bits[0] & (_Tp(1) << _DropLsb)); 1233 else 1234 return _BitMask<_NewSize, 1235 ((_NewSize + _DropLsb == sizeof(_Tp) * __CHAR_BIT__ 1236 && _NewSize + _DropLsb <= _Np) 1237 || ((_Sanitized || _Np == sizeof(_Tp) * __CHAR_BIT__) 1238 && _NewSize + _DropLsb >= _Np))>(_M_bits[0] 1239 >> _DropLsb); 1240 } 1241 1242 // True if all bits are set. Implicitly sanitizes if _Sanitized == false. 1243 constexpr bool 1244 all() const noexcept 1245 { 1246 if constexpr (_Np == 1) 1247 return _M_bits[0]; 1248 else if constexpr (!_Sanitized) 1249 return _M_sanitized().all(); 1250 else 1251 { 1252 constexpr _Tp __allbits = ~_Tp(); 1253 for (int __i = 0; __i < _S_array_size - 1; ++__i) 1254 if (_M_bits[__i] != __allbits) 1255 return false; 1256 return _M_bits[_S_array_size - 1] == _S_bitmask; 1257 } 1258 } 1259 1260 // True if at least one bit is set. Implicitly sanitizes if _Sanitized == 1261 // false. 1262 constexpr bool 1263 any() const noexcept 1264 { 1265 if constexpr (_Np == 1) 1266 return _M_bits[0]; 1267 else if constexpr (!_Sanitized) 1268 return _M_sanitized().any(); 1269 else 1270 { 1271 for (int __i = 0; __i < _S_array_size - 1; ++__i) 1272 if (_M_bits[__i] != 0) 1273 return true; 1274 return _M_bits[_S_array_size - 1] != 0; 1275 } 1276 } 1277 1278 // True if no bit is set. Implicitly sanitizes if _Sanitized == false. 1279 constexpr bool 1280 none() const noexcept 1281 { 1282 if constexpr (_Np == 1) 1283 return !_M_bits[0]; 1284 else if constexpr (!_Sanitized) 1285 return _M_sanitized().none(); 1286 else 1287 { 1288 for (int __i = 0; __i < _S_array_size - 1; ++__i) 1289 if (_M_bits[__i] != 0) 1290 return false; 1291 return _M_bits[_S_array_size - 1] == 0; 1292 } 1293 } 1294 1295 // Returns the number of set bits. Implicitly sanitizes if _Sanitized == 1296 // false. 1297 constexpr int 1298 count() const noexcept 1299 { 1300 if constexpr (_Np == 1) 1301 return _M_bits[0]; 1302 else if constexpr (!_Sanitized) 1303 return _M_sanitized().none(); 1304 else 1305 { 1306 int __result = __builtin_popcountll(_M_bits[0]); 1307 for (int __i = 1; __i < _S_array_size; ++__i) 1308 __result += __builtin_popcountll(_M_bits[__i]); 1309 return __result; 1310 } 1311 } 1312 1313 // Returns the bit at offset __i as bool. 1314 constexpr bool 1315 operator[](size_t __i) const noexcept 1316 { 1317 if constexpr (_Np == 1) 1318 return _M_bits[0]; 1319 else if constexpr (_S_array_size == 1) 1320 return (_M_bits[0] >> __i) & 1; 1321 else 1322 { 1323 const size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__); 1324 const size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__); 1325 return (_M_bits[__j] >> __shift) & 1; 1326 } 1327 } 1328 1329 template
1330 constexpr bool 1331 operator[](_SizeConstant<__i>) const noexcept 1332 { 1333 static_assert(__i < _Np); 1334 constexpr size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__); 1335 constexpr size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__); 1336 return static_cast
(_M_bits[__j] & (_Tp(1) << __shift)); 1337 } 1338 1339 // Set the bit at offset __i to __x. 1340 constexpr void 1341 set(size_t __i, bool __x) noexcept 1342 { 1343 if constexpr (_Np == 1) 1344 _M_bits[0] = __x; 1345 else if constexpr (_S_array_size == 1) 1346 { 1347 _M_bits[0] &= ~_Tp(_Tp(1) << __i); 1348 _M_bits[0] |= _Tp(_Tp(__x) << __i); 1349 } 1350 else 1351 { 1352 const size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__); 1353 const size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__); 1354 _M_bits[__j] &= ~_Tp(_Tp(1) << __shift); 1355 _M_bits[__j] |= _Tp(_Tp(__x) << __shift); 1356 } 1357 } 1358 1359 template
1360 constexpr void 1361 set(_SizeConstant<__i>, bool __x) noexcept 1362 { 1363 static_assert(__i < _Np); 1364 if constexpr (_Np == 1) 1365 _M_bits[0] = __x; 1366 else 1367 { 1368 constexpr size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__); 1369 constexpr size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__); 1370 constexpr _Tp __mask = ~_Tp(_Tp(1) << __shift); 1371 _M_bits[__j] &= __mask; 1372 _M_bits[__j] |= _Tp(_Tp(__x) << __shift); 1373 } 1374 } 1375 1376 // Inverts all bits. Sanitized input leads to sanitized output. 1377 constexpr _BitMask 1378 operator~() const noexcept 1379 { 1380 if constexpr (_Np == 1) 1381 return !_M_bits[0]; 1382 else 1383 { 1384 _BitMask __result{}; 1385 for (int __i = 0; __i < _S_array_size - 1; ++__i) 1386 __result._M_bits[__i] = ~_M_bits[__i]; 1387 if constexpr (_Sanitized) 1388 __result._M_bits[_S_array_size - 1] 1389 = _M_bits[_S_array_size - 1] ^ _S_bitmask; 1390 else 1391 __result._M_bits[_S_array_size - 1] = ~_M_bits[_S_array_size - 1]; 1392 return __result; 1393 } 1394 } 1395 1396 constexpr _BitMask& 1397 operator^=(const _BitMask& __b) & noexcept 1398 { 1399 __execute_n_times<_S_array_size>( 1400 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { _M_bits[__i] ^= __b._M_bits[__i]; }); 1401 return *this; 1402 } 1403 1404 constexpr _BitMask& 1405 operator|=(const _BitMask& __b) & noexcept 1406 { 1407 __execute_n_times<_S_array_size>( 1408 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { _M_bits[__i] |= __b._M_bits[__i]; }); 1409 return *this; 1410 } 1411 1412 constexpr _BitMask& 1413 operator&=(const _BitMask& __b) & noexcept 1414 { 1415 __execute_n_times<_S_array_size>( 1416 [&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { _M_bits[__i] &= __b._M_bits[__i]; }); 1417 return *this; 1418 } 1419 1420 friend constexpr _BitMask 1421 operator^(const _BitMask& __a, const _BitMask& __b) noexcept 1422 { 1423 _BitMask __r = __a; 1424 __r ^= __b; 1425 return __r; 1426 } 1427 1428 friend constexpr _BitMask 1429 operator|(const _BitMask& __a, const _BitMask& __b) noexcept 1430 { 1431 _BitMask __r = __a; 1432 __r |= __b; 1433 return __r; 1434 } 1435 1436 friend constexpr _BitMask 1437 operator&(const _BitMask& __a, const _BitMask& __b) noexcept 1438 { 1439 _BitMask __r = __a; 1440 __r &= __b; 1441 return __r; 1442 } 1443 1444 _GLIBCXX_SIMD_INTRINSIC 1445 constexpr bool 1446 _M_is_constprop() const 1447 { 1448 if constexpr (_S_array_size == 0) 1449 return __builtin_constant_p(_M_bits[0]); 1450 else 1451 { 1452 for (int __i = 0; __i < _S_array_size; ++__i) 1453 if (!__builtin_constant_p(_M_bits[__i])) 1454 return false; 1455 return true; 1456 } 1457 } 1458 }; 1459 1460 // }}} 1461 1462 // vvv ---- builtin vector types [[gnu::vector_size(N)]] and operations ---- vvv 1463 // __min_vector_size {{{ 1464 template
1465 static inline constexpr int __min_vector_size = 2 * sizeof(_Tp); 1466 1467 #if _GLIBCXX_SIMD_HAVE_NEON 1468 template <> 1469 inline constexpr int __min_vector_size
= 8; 1470 #else 1471 template <> 1472 inline constexpr int __min_vector_size
= 16; 1473 #endif 1474 1475 // }}} 1476 // __vector_type {{{ 1477 template
1478 struct __vector_type_n {}; 1479 1480 // substition failure for 0-element case 1481 template
1482 struct __vector_type_n<_Tp, 0, void> {}; 1483 1484 // special case 1-element to be _Tp itself 1485 template
1486 struct __vector_type_n<_Tp, 1, enable_if_t<__is_vectorizable_v<_Tp>>> 1487 { using type = _Tp; }; 1488 1489 // else, use GNU-style builtin vector types 1490 template
1491 struct __vector_type_n<_Tp, _Np, enable_if_t<__is_vectorizable_v<_Tp> && _Np >= 2>> 1492 { 1493 static constexpr size_t _S_Np2 = std::__bit_ceil(_Np * sizeof(_Tp)); 1494 1495 static constexpr size_t _S_Bytes = 1496 #ifdef __i386__ 1497 // Using [[gnu::vector_size(8)]] would wreak havoc on the FPU because 1498 // those objects are passed via MMX registers and nothing ever calls EMMS. 1499 _S_Np2 == 8 ? 16 : 1500 #endif 1501 _S_Np2 < __min_vector_size<_Tp> ? __min_vector_size<_Tp> 1502 : _S_Np2; 1503 1504 using type [[__gnu__::__vector_size__(_S_Bytes)]] = _Tp; 1505 }; 1506 1507 template
1508 struct __vector_type; 1509 1510 template
1511 struct __vector_type<_Tp, _Bytes, 0> 1512 : __vector_type_n<_Tp, _Bytes / sizeof(_Tp)> {}; 1513 1514 template
1515 using __vector_type_t = typename __vector_type_n<_Tp, _Size>::type; 1516 1517 template
1518 using __vector_type2_t = typename __vector_type<_Tp, 2>::type; 1519 template
1520 using __vector_type4_t = typename __vector_type<_Tp, 4>::type; 1521 template
1522 using __vector_type8_t = typename __vector_type<_Tp, 8>::type; 1523 template
1524 using __vector_type16_t = typename __vector_type<_Tp, 16>::type; 1525 template
1526 using __vector_type32_t = typename __vector_type<_Tp, 32>::type; 1527 template
1528 using __vector_type64_t = typename __vector_type<_Tp, 64>::type; 1529 1530 // }}} 1531 // __is_vector_type {{{ 1532 template
> 1533 struct __is_vector_type : false_type {}; 1534 1535 template
1536 struct __is_vector_type< 1537 _Tp, void_t
()[0])>, sizeof(_Tp)>::type>> 1539 : is_same<_Tp, typename __vector_type< 1540 remove_reference_t
()[0])>, 1541 sizeof(_Tp)>::type> {}; 1542 1543 template
1544 inline constexpr bool __is_vector_type_v = __is_vector_type<_Tp>::value; 1545 1546 // }}} 1547 // __is_intrinsic_type {{{ 1548 #if _GLIBCXX_SIMD_HAVE_SSE_ABI 1549 template
1550 using __is_intrinsic_type = __is_vector_type<_Tp>; 1551 #else // not SSE (x86) 1552 template
> 1553 struct __is_intrinsic_type : false_type {}; 1554 1555 template
1556 struct __is_intrinsic_type< 1557 _Tp, void_t
()[0])>, sizeof(_Tp)>::type>> 1559 : is_same<_Tp, typename __intrinsic_type< 1560 remove_reference_t
()[0])>, 1561 sizeof(_Tp)>::type> {}; 1562 #endif 1563 1564 template
1565 inline constexpr bool __is_intrinsic_type_v = __is_intrinsic_type<_Tp>::value; 1566 1567 // }}} 1568 // _VectorTraits{{{ 1569 template
> 1570 struct _VectorTraitsImpl; 1571 1572 template
1573 struct _VectorTraitsImpl<_Tp, enable_if_t<__is_vector_type_v<_Tp> 1574 || __is_intrinsic_type_v<_Tp>>> 1575 { 1576 using type = _Tp; 1577 using value_type = remove_reference_t
()[0])>; 1578 static constexpr int _S_full_size = sizeof(_Tp) / sizeof(value_type); 1579 using _Wrapper = _SimdWrapper
; 1580 template
1581 static constexpr bool _S_is 1582 = is_same_v
&& _W == _S_full_size; 1583 }; 1584 1585 template
1586 struct _VectorTraitsImpl<_SimdWrapper<_Tp, _Np>, 1587 void_t<__vector_type_t<_Tp, _Np>>> 1588 { 1589 using type = __vector_type_t<_Tp, _Np>; 1590 using value_type = _Tp; 1591 static constexpr int _S_full_size = sizeof(type) / sizeof(value_type); 1592 using _Wrapper = _SimdWrapper<_Tp, _Np>; 1593 static constexpr bool _S_is_partial = (_Np == _S_full_size); 1594 static constexpr int _S_partial_width = _Np; 1595 template
1596 static constexpr bool _S_is 1597 = is_same_v
&& _W == _S_full_size; 1598 }; 1599 1600 template
::type> 1601 using _VectorTraits = _VectorTraitsImpl<_Tp>; 1602 1603 // }}} 1604 // __as_vector{{{ 1605 template
1606 _GLIBCXX_SIMD_INTRINSIC constexpr auto 1607 __as_vector(_V __x) 1608 { 1609 if constexpr (__is_vector_type_v<_V>) 1610 return __x; 1611 else if constexpr (is_simd<_V>::value || is_simd_mask<_V>::value) 1612 { 1613 if constexpr (__is_fixed_size_abi_v
) 1614 { 1615 static_assert(is_simd<_V>::value); 1616 static_assert(_V::abi_type::template __traits< 1617 typename _V::value_type>::_SimdMember::_S_tuple_size == 1); 1618 return __as_vector(__data(__x).first); 1619 } 1620 else if constexpr (_V::size() > 1) 1621 return __data(__x)._M_data; 1622 else 1623 { 1624 static_assert(is_simd<_V>::value); 1625 using _Tp = typename _V::value_type; 1626 #ifdef __i386__ 1627 constexpr auto __bytes = sizeof(_Tp) == 8 ? 16 : sizeof(_Tp); 1628 using _RV [[__gnu__::__vector_size__(__bytes)]] = _Tp; 1629 #else 1630 using _RV [[__gnu__::__vector_size__(sizeof(_Tp))]] = _Tp; 1631 #endif 1632 return _RV{__data(__x)}; 1633 } 1634 } 1635 else if constexpr (__is_vectorizable_v<_V>) 1636 return __vector_type_t<_V, 2>{__x}; 1637 else 1638 return __x._M_data; 1639 } 1640 1641 // }}} 1642 // __as_wrapper{{{ 1643 template
1644 _GLIBCXX_SIMD_INTRINSIC constexpr auto 1645 __as_wrapper(_V __x) 1646 { 1647 if constexpr (__is_vector_type_v<_V>) 1648 return _SimdWrapper
::value_type, 1649 (_Np > 0 ? _Np : _VectorTraits<_V>::_S_full_size)>(__x); 1650 else if constexpr (is_simd<_V>::value || is_simd_mask<_V>::value) 1651 { 1652 static_assert(_V::size() == _Np); 1653 return __data(__x); 1654 } 1655 else 1656 { 1657 static_assert(_V::_S_size == _Np); 1658 return __x; 1659 } 1660 } 1661 1662 // }}} 1663 // __intrin_bitcast{{{ 1664 template
1665 _GLIBCXX_SIMD_INTRINSIC constexpr _To 1666 __intrin_bitcast(_From __v) 1667 { 1668 static_assert((__is_vector_type_v<_From> || __is_intrinsic_type_v<_From>) 1669 && (__is_vector_type_v<_To> || __is_intrinsic_type_v<_To>)); 1670 if constexpr (sizeof(_To) == sizeof(_From)) 1671 return reinterpret_cast<_To>(__v); 1672 else if constexpr (sizeof(_From) > sizeof(_To)) 1673 if constexpr (sizeof(_To) >= 16) 1674 return reinterpret_cast
&>(__v); 1675 else 1676 { 1677 _To __r; 1678 __builtin_memcpy(&__r, &__v, sizeof(_To)); 1679 return __r; 1680 } 1681 #if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__ 1682 else if constexpr (__have_avx && sizeof(_From) == 16 && sizeof(_To) == 32) 1683 return reinterpret_cast<_To>(__builtin_ia32_ps256_ps( 1684 reinterpret_cast<__vector_type_t
>(__v))); 1685 else if constexpr (__have_avx512f && sizeof(_From) == 16 1686 && sizeof(_To) == 64) 1687 return reinterpret_cast<_To>(__builtin_ia32_ps512_ps( 1688 reinterpret_cast<__vector_type_t
>(__v))); 1689 else if constexpr (__have_avx512f && sizeof(_From) == 32 1690 && sizeof(_To) == 64) 1691 return reinterpret_cast<_To>(__builtin_ia32_ps512_256ps( 1692 reinterpret_cast<__vector_type_t
>(__v))); 1693 #endif // _GLIBCXX_SIMD_X86INTRIN 1694 else if constexpr (sizeof(__v) <= 8) 1695 return reinterpret_cast<_To>( 1696 __vector_type_t<__int_for_sizeof_t<_From>, sizeof(_To) / sizeof(_From)>{ 1697 reinterpret_cast<__int_for_sizeof_t<_From>>(__v)}); 1698 else 1699 { 1700 static_assert(sizeof(_To) > sizeof(_From)); 1701 _To __r = {}; 1702 __builtin_memcpy(&__r, &__v, sizeof(_From)); 1703 return __r; 1704 } 1705 } 1706 1707 // }}} 1708 // __vector_bitcast{{{ 1709 template
, 1711 size_t _Np = _NN == 0 ? sizeof(_From) / sizeof(_To) : _NN> 1712 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_To, _Np> 1713 __vector_bitcast(_From __x) 1714 { 1715 using _R = __vector_type_t<_To, _Np>; 1716 return __intrin_bitcast<_R>(__x); 1717 } 1718 1719 template
) / sizeof(_To) : _NN> 1722 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_To, _Np> 1723 __vector_bitcast(const _SimdWrapper<_Tp, _Nx>& __x) 1724 { 1725 static_assert(_Np > 1); 1726 return __intrin_bitcast<__vector_type_t<_To, _Np>>(__x._M_data); 1727 } 1728 1729 // }}} 1730 // __convert_x86 declarations {{{ 1731 #ifdef _GLIBCXX_SIMD_WORKAROUND_PR85048 1732 template
> 1733 _To __convert_x86(_Tp); 1734 1735 template
> 1736 _To __convert_x86(_Tp, _Tp); 1737 1738 template
> 1739 _To __convert_x86(_Tp, _Tp, _Tp, _Tp); 1740 1741 template
> 1742 _To __convert_x86(_Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp); 1743 1744 template
> 1745 _To __convert_x86(_Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, 1746 _Tp, _Tp, _Tp, _Tp); 1747 #endif // _GLIBCXX_SIMD_WORKAROUND_PR85048 1748 1749 //}}} 1750 // __bit_cast {{{ 1751 template
1752 _GLIBCXX_SIMD_INTRINSIC constexpr _To 1753 __bit_cast(const _From __x) 1754 { 1755 #if __has_builtin(__builtin_bit_cast) 1756 return __builtin_bit_cast(_To, __x); 1757 #else 1758 static_assert(sizeof(_To) == sizeof(_From)); 1759 constexpr bool __to_is_vectorizable 1760 = is_arithmetic_v<_To> || is_enum_v<_To>; 1761 constexpr bool __from_is_vectorizable 1762 = is_arithmetic_v<_From> || is_enum_v<_From>; 1763 if constexpr (__is_vector_type_v<_To> && __is_vector_type_v<_From>) 1764 return reinterpret_cast<_To>(__x); 1765 else if constexpr (__is_vector_type_v<_To> && __from_is_vectorizable) 1766 { 1767 using _FV [[__gnu__::__vector_size__(sizeof(_From))]] = _From; 1768 return reinterpret_cast<_To>(_FV{__x}); 1769 } 1770 else if constexpr (__to_is_vectorizable && __from_is_vectorizable) 1771 { 1772 using _TV [[__gnu__::__vector_size__(sizeof(_To))]] = _To; 1773 using _FV [[__gnu__::__vector_size__(sizeof(_From))]] = _From; 1774 return reinterpret_cast<_TV>(_FV{__x})[0]; 1775 } 1776 else if constexpr (__to_is_vectorizable && __is_vector_type_v<_From>) 1777 { 1778 using _TV [[__gnu__::__vector_size__(sizeof(_To))]] = _To; 1779 return reinterpret_cast<_TV>(__x)[0]; 1780 } 1781 else 1782 { 1783 _To __r; 1784 __builtin_memcpy(reinterpret_cast
(&__r), 1785 reinterpret_cast
(&__x), sizeof(_To)); 1786 return __r; 1787 } 1788 #endif 1789 } 1790 1791 // }}} 1792 // __to_intrin {{{ 1793 template
, 1794 typename _R = __intrinsic_type_t
> 1795 _GLIBCXX_SIMD_INTRINSIC constexpr _R 1796 __to_intrin(_Tp __x) 1797 { 1798 static_assert(sizeof(__x) <= sizeof(_R), 1799 "__to_intrin may never drop values off the end"); 1800 if constexpr (sizeof(__x) == sizeof(_R)) 1801 return reinterpret_cast<_R>(__as_vector(__x)); 1802 else 1803 { 1804 using _Up = __int_for_sizeof_t<_Tp>; 1805 return reinterpret_cast<_R>( 1806 __vector_type_t<_Up, sizeof(_R) / sizeof(_Up)>{__bit_cast<_Up>(__x)}); 1807 } 1808 } 1809 1810 // }}} 1811 // __make_vector{{{ 1812 template
1813 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, sizeof...(_Args)> 1814 __make_vector(const _Args&... __args) 1815 { return __vector_type_t<_Tp, sizeof...(_Args)>{static_cast<_Tp>(__args)...}; } 1816 1817 // }}} 1818 // __vector_broadcast{{{ 1819 template
1820 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np> 1821 __vector_broadcast_impl(_Tp __x, index_sequence<_I...>) 1822 { return __vector_type_t<_Tp, _Np>{((void)_I, __x)...}; } 1823 1824 template
1825 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np> 1826 __vector_broadcast(_Tp __x) 1827 { return __vector_broadcast_impl<_Np, _Tp>(__x, make_index_sequence<_Np>()); } 1828 1829 // }}} 1830 // __generate_vector{{{ 1831 template
1832 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np> 1833 __generate_vector_impl(_Gp&& __gen, index_sequence<_I...>) 1834 { return __vector_type_t<_Tp, _Np>{ static_cast<_Tp>(__gen(_SizeConstant<_I>()))...}; } 1835 1836 template
, typename _Gp> 1837 _GLIBCXX_SIMD_INTRINSIC constexpr _V 1838 __generate_vector(_Gp&& __gen) 1839 { 1840 if constexpr (__is_vector_type_v<_V>) 1841 return __generate_vector_impl
( 1843 static_cast<_Gp&&>(__gen), make_index_sequence<_VVT::_S_full_size>()); 1844 else 1845 return __generate_vector_impl
( 1847 static_cast<_Gp&&>(__gen), 1848 make_index_sequence<_VVT::_S_partial_width>()); 1849 } 1850 1851 template
1852 _GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np> 1853 __generate_vector(_Gp&& __gen) 1854 { 1855 return __generate_vector_impl<_Tp, _Np>(static_cast<_Gp&&>(__gen), 1856 make_index_sequence<_Np>()); 1857 } 1858 1859 // }}} 1860 // __xor{{{ 1861 template
1862 _GLIBCXX_SIMD_INTRINSIC constexpr _TW 1863 __xor(_TW __a, _TW __b) noexcept 1864 { 1865 if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>) 1866 { 1867 using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW, 1868 _VectorTraitsImpl<_TW>>::value_type; 1869 if constexpr (is_floating_point_v<_Tp>) 1870 { 1871 using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>; 1872 return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a) 1873 ^ __vector_bitcast<_Ip>(__b)); 1874 } 1875 else if constexpr (__is_vector_type_v<_TW>) 1876 return __a ^ __b; 1877 else 1878 return __a._M_data ^ __b._M_data; 1879 } 1880 else 1881 return __a ^ __b; 1882 } 1883 1884 // }}} 1885 // __or{{{ 1886 template
1887 _GLIBCXX_SIMD_INTRINSIC constexpr _TW 1888 __or(_TW __a, _TW __b) noexcept 1889 { 1890 if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>) 1891 { 1892 using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW, 1893 _VectorTraitsImpl<_TW>>::value_type; 1894 if constexpr (is_floating_point_v<_Tp>) 1895 { 1896 using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>; 1897 return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a) 1898 | __vector_bitcast<_Ip>(__b)); 1899 } 1900 else if constexpr (__is_vector_type_v<_TW>) 1901 return __a | __b; 1902 else 1903 return __a._M_data | __b._M_data; 1904 } 1905 else 1906 return __a | __b; 1907 } 1908 1909 // }}} 1910 // __and{{{ 1911 template
1912 _GLIBCXX_SIMD_INTRINSIC constexpr _TW 1913 __and(_TW __a, _TW __b) noexcept 1914 { 1915 if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>) 1916 { 1917 using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW, 1918 _VectorTraitsImpl<_TW>>::value_type; 1919 if constexpr (is_floating_point_v<_Tp>) 1920 { 1921 using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>; 1922 return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a) 1923 & __vector_bitcast<_Ip>(__b)); 1924 } 1925 else if constexpr (__is_vector_type_v<_TW>) 1926 return __a & __b; 1927 else 1928 return __a._M_data & __b._M_data; 1929 } 1930 else 1931 return __a & __b; 1932 } 1933 1934 // }}} 1935 // __andnot{{{ 1936 #if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__ 1937 static constexpr struct 1938 { 1939 _GLIBCXX_SIMD_INTRINSIC __v4sf 1940 operator()(__v4sf __a, __v4sf __b) const noexcept 1941 { return __builtin_ia32_andnps(__a, __b); } 1942 1943 _GLIBCXX_SIMD_INTRINSIC __v2df 1944 operator()(__v2df __a, __v2df __b) const noexcept 1945 { return __builtin_ia32_andnpd(__a, __b); } 1946 1947 _GLIBCXX_SIMD_INTRINSIC __v2di 1948 operator()(__v2di __a, __v2di __b) const noexcept 1949 { return __builtin_ia32_pandn128(__a, __b); } 1950 1951 _GLIBCXX_SIMD_INTRINSIC __v8sf 1952 operator()(__v8sf __a, __v8sf __b) const noexcept 1953 { return __builtin_ia32_andnps256(__a, __b); } 1954 1955 _GLIBCXX_SIMD_INTRINSIC __v4df 1956 operator()(__v4df __a, __v4df __b) const noexcept 1957 { return __builtin_ia32_andnpd256(__a, __b); } 1958 1959 _GLIBCXX_SIMD_INTRINSIC __v4di 1960 operator()(__v4di __a, __v4di __b) const noexcept 1961 { 1962 if constexpr (__have_avx2) 1963 return __builtin_ia32_andnotsi256(__a, __b); 1964 else 1965 return reinterpret_cast<__v4di>( 1966 __builtin_ia32_andnpd256(reinterpret_cast<__v4df>(__a), 1967 reinterpret_cast<__v4df>(__b))); 1968 } 1969 1970 _GLIBCXX_SIMD_INTRINSIC __v16sf 1971 operator()(__v16sf __a, __v16sf __b) const noexcept 1972 { 1973 if constexpr (__have_avx512dq) 1974 return _mm512_andnot_ps(__a, __b); 1975 else 1976 return reinterpret_cast<__v16sf>( 1977 _mm512_andnot_si512(reinterpret_cast<__v8di>(__a), 1978 reinterpret_cast<__v8di>(__b))); 1979 } 1980 1981 _GLIBCXX_SIMD_INTRINSIC __v8df 1982 operator()(__v8df __a, __v8df __b) const noexcept 1983 { 1984 if constexpr (__have_avx512dq) 1985 return _mm512_andnot_pd(__a, __b); 1986 else 1987 return reinterpret_cast<__v8df>( 1988 _mm512_andnot_si512(reinterpret_cast<__v8di>(__a), 1989 reinterpret_cast<__v8di>(__b))); 1990 } 1991 1992 _GLIBCXX_SIMD_INTRINSIC __v8di 1993 operator()(__v8di __a, __v8di __b) const noexcept 1994 { return _mm512_andnot_si512(__a, __b); } 1995 } _S_x86_andnot; 1996 #endif // _GLIBCXX_SIMD_X86INTRIN && !__clang__ 1997 1998 template
1999 _GLIBCXX_SIMD_INTRINSIC constexpr _TW 2000 __andnot(_TW __a, _TW __b) noexcept 2001 { 2002 if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>) 2003 { 2004 using _TVT = conditional_t<__is_simd_wrapper_v<_TW>, _TW, 2005 _VectorTraitsImpl<_TW>>; 2006 using _Tp = typename _TVT::value_type; 2007 #if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__ 2008 if constexpr (sizeof(_TW) >= 16) 2009 { 2010 const auto __ai = __to_intrin(__a); 2011 const auto __bi = __to_intrin(__b); 2012 if (!__builtin_is_constant_evaluated() 2013 && !(__builtin_constant_p(__ai) && __builtin_constant_p(__bi))) 2014 { 2015 const auto __r = _S_x86_andnot(__ai, __bi); 2016 if constexpr (is_convertible_v
) 2017 return __r; 2018 else 2019 return reinterpret_cast
(__r); 2020 } 2021 } 2022 #endif // _GLIBCXX_SIMD_X86INTRIN 2023 using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>; 2024 return __vector_bitcast<_Tp>(~__vector_bitcast<_Ip>(__a) 2025 & __vector_bitcast<_Ip>(__b)); 2026 } 2027 else 2028 return ~__a & __b; 2029 } 2030 2031 // }}} 2032 // __not{{{ 2033 template
> 2034 _GLIBCXX_SIMD_INTRINSIC constexpr _Tp 2035 __not(_Tp __a) noexcept 2036 { 2037 if constexpr (is_floating_point_v
) 2038 return reinterpret_cast
( 2039 ~__vector_bitcast
(__a)); 2040 else 2041 return ~__a; 2042 } 2043 2044 // }}} 2045 // __vec_shuffle{{{ 2046 template
2047 _GLIBCXX_SIMD_INTRINSIC constexpr 2048 __vector_type_t
()[0])>, sizeof...(_Is)> 2049 __vec_shuffle(_T0 __x, _T1 __y, index_sequence<_Is...> __seq, _Fun __idx_perm) 2050 { 2051 constexpr int _N0 = sizeof(__x) / sizeof(__x[0]); 2052 constexpr int _N1 = sizeof(__y) / sizeof(__y[0]); 2053 using _Tp = remove_reference_t
()[0])>; 2054 using _RV [[maybe_unused]] = __vector_type_t<_Tp, sizeof...(_Is)>; 2055 #if __has_builtin(__builtin_shufflevector) 2056 #ifdef __clang__ 2057 // Clang requires _T0 == _T1 2058 if constexpr (sizeof(__x) > sizeof(__y) and _N1 == 1) 2059 return __vec_shuffle(__x, _T0{__y[0]}, __seq, __idx_perm); 2060 else if constexpr (sizeof(__x) > sizeof(__y)) 2061 return __vec_shuffle(__x, __intrin_bitcast<_T0>(__y), __seq, __idx_perm); 2062 else if constexpr (sizeof(__x) < sizeof(__y) and _N0 == 1) 2063 return __vec_shuffle(_T1{__x[0]}, __y, __seq, [=](int __i) { 2064 __i = __idx_perm(__i); 2065 return __i < _N0 ? __i : __i - _N0 + _N1; 2066 }); 2067 else if constexpr (sizeof(__x) < sizeof(__y)) 2068 return __vec_shuffle(__intrin_bitcast<_T1>(__x), __y, __seq, [=](int __i) { 2069 __i = __idx_perm(__i); 2070 return __i < _N0 ? __i : __i - _N0 + _N1; 2071 }); 2072 else 2073 #endif 2074 { 2075 const auto __r = __builtin_shufflevector(__x, __y, [=] { 2076 constexpr int __j = __idx_perm(_Is); 2077 static_assert(__j < _N0 + _N1); 2078 return __j; 2079 }()...); 2080 #ifdef __i386__ 2081 if constexpr (sizeof(__r) == sizeof(_RV)) 2082 return __r; 2083 else 2084 return _RV {__r[_Is]...}; 2085 #else 2086 return __r; 2087 #endif 2088 } 2089 #else 2090 return _RV { 2091 [=]() -> _Tp { 2092 constexpr int __j = __idx_perm(_Is); 2093 static_assert(__j < _N0 + _N1); 2094 if constexpr (__j < 0) 2095 return 0; 2096 else if constexpr (__j < _N0) 2097 return __x[__j]; 2098 else 2099 return __y[__j - _N0]; 2100 }()... 2101 }; 2102 #endif 2103 } 2104 2105 template
2106 _GLIBCXX_SIMD_INTRINSIC constexpr auto 2107 __vec_shuffle(_T0 __x, _Seq __seq, _Fun __idx_perm) 2108 { return __vec_shuffle(__x, _T0(), __seq, __idx_perm); } 2109 2110 // }}} 2111 // __concat{{{ 2112 template
, 2113 typename _R = __vector_type_t
> 2114 constexpr _R 2115 __concat(_Tp a_, _Tp b_) 2116 { 2117 #ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_1 2118 using _W 2119 = conditional_t
, double, 2120 conditional_t<(sizeof(_Tp) >= 2 * sizeof(long long)), 2121 long long, typename _TVT::value_type>>; 2122 constexpr int input_width = sizeof(_Tp) / sizeof(_W); 2123 const auto __a = __vector_bitcast<_W>(a_); 2124 const auto __b = __vector_bitcast<_W>(b_); 2125 using _Up = __vector_type_t<_W, sizeof(_R) / sizeof(_W)>; 2126 #else 2127 constexpr int input_width = _TVT::_S_full_size; 2128 const _Tp& __a = a_; 2129 const _Tp& __b = b_; 2130 using _Up = _R; 2131 #endif 2132 if constexpr (input_width == 2) 2133 return reinterpret_cast<_R>(_Up{__a[0], __a[1], __b[0], __b[1]}); 2134 else if constexpr (input_width == 4) 2135 return reinterpret_cast<_R>( 2136 _Up{__a[0], __a[1], __a[2], __a[3], __b[0], __b[1], __b[2], __b[3]}); 2137 else if constexpr (input_width == 8) 2138 return reinterpret_cast<_R>( 2139 _Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6], __a[7], 2140 __b[0], __b[1], __b[2], __b[3], __b[4], __b[5], __b[6], __b[7]}); 2141 else if constexpr (input_width == 16) 2142 return reinterpret_cast<_R>( 2143 _Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6], 2144 __a[7], __a[8], __a[9], __a[10], __a[11], __a[12], __a[13], 2145 __a[14], __a[15], __b[0], __b[1], __b[2], __b[3], __b[4], 2146 __b[5], __b[6], __b[7], __b[8], __b[9], __b[10], __b[11], 2147 __b[12], __b[13], __b[14], __b[15]}); 2148 else if constexpr (input_width == 32) 2149 return reinterpret_cast<_R>( 2150 _Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6], 2151 __a[7], __a[8], __a[9], __a[10], __a[11], __a[12], __a[13], 2152 __a[14], __a[15], __a[16], __a[17], __a[18], __a[19], __a[20], 2153 __a[21], __a[22], __a[23], __a[24], __a[25], __a[26], __a[27], 2154 __a[28], __a[29], __a[30], __a[31], __b[0], __b[1], __b[2], 2155 __b[3], __b[4], __b[5], __b[6], __b[7], __b[8], __b[9], 2156 __b[10], __b[11], __b[12], __b[13], __b[14], __b[15], __b[16], 2157 __b[17], __b[18], __b[19], __b[20], __b[21], __b[22], __b[23], 2158 __b[24], __b[25], __b[26], __b[27], __b[28], __b[29], __b[30], 2159 __b[31]}); 2160 } 2161 2162 // }}} 2163 // __zero_extend {{{ 2164 template
> 2165 struct _ZeroExtendProxy 2166 { 2167 using value_type = typename _TVT::value_type; 2168 static constexpr size_t _Np = _TVT::_S_full_size; 2169 const _Tp __x; 2170 2171 template
, 2172 typename 2173 = enable_if_t
>> 2174 _GLIBCXX_SIMD_INTRINSIC operator _To() const 2175 { 2176 constexpr size_t _ToN = _ToVT::_S_full_size; 2177 if constexpr (_ToN == _Np) 2178 return __x; 2179 else if constexpr (_ToN == 2 * _Np) 2180 { 2181 #ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_3 2182 if constexpr (__have_avx && _TVT::template _S_is
) 2183 return __vector_bitcast
( 2184 _mm256_insertf128_ps(__m256(), __x, 0)); 2185 else if constexpr (__have_avx && _TVT::template _S_is
) 2186 return __vector_bitcast
( 2187 _mm256_insertf128_pd(__m256d(), __x, 0)); 2188 else if constexpr (__have_avx2 && _Np * sizeof(value_type) == 16) 2189 return __vector_bitcast
( 2190 _mm256_insertf128_si256(__m256i(), __to_intrin(__x), 0)); 2191 else if constexpr (__have_avx512f && _TVT::template _S_is
) 2192 { 2193 if constexpr (__have_avx512dq) 2194 return __vector_bitcast
( 2195 _mm512_insertf32x8(__m512(), __x, 0)); 2196 else 2197 return reinterpret_cast<__m512>( 2198 _mm512_insertf64x4(__m512d(), 2199 reinterpret_cast<__m256d>(__x), 0)); 2200 } 2201 else if constexpr (__have_avx512f 2202 && _TVT::template _S_is