// Definition of the public simd interfaces -*- C++ -*-
// Copyright (C) 2020-2022 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// .
#ifndef _GLIBCXX_EXPERIMENTAL_SIMD_H
#define _GLIBCXX_EXPERIMENTAL_SIMD_H
#if __cplusplus >= 201703L
#include "simd_detail.h"
#include "numeric_traits.h"
#include
#include
#ifdef _GLIBCXX_DEBUG_UB
#include // for stderr
#endif
#include
#include
#include
#include
#include
#if _GLIBCXX_SIMD_X86INTRIN
#include
#elif _GLIBCXX_SIMD_HAVE_NEON
#pragma GCC diagnostic push
// narrowing conversion of '__a' from 'uint64_t' {aka 'long long unsigned int'} to
// 'int64x1_t' {aka 'long long int'} [-Wnarrowing]
#pragma GCC diagnostic ignored "-Wnarrowing"
#include
#pragma GCC diagnostic pop
#endif
/** @ingroup ts_simd
* @{
*/
/* There are several closely related types, with the following naming
* convention:
* _Tp: vectorizable (arithmetic) type (or any type)
* _TV: __vector_type_t<_Tp, _Np>
* _TW: _SimdWrapper<_Tp, _Np>
* _TI: __intrinsic_type_t<_Tp, _Np>
* _TVT: _VectorTraits<_TV> or _VectorTraits<_TW>
* If one additional type is needed use _U instead of _T.
* Otherwise use _T\d, _TV\d, _TW\d, TI\d, _TVT\d.
*
* More naming conventions:
* _Ap or _Abi: An ABI tag from the simd_abi namespace
* _Ip: often used for integer types with sizeof(_Ip) == sizeof(_Tp),
* _IV, _IW as for _TV, _TW
* _Np: number of elements (not bytes)
* _Bytes: number of bytes
*
* Variable names:
* __k: mask object (vector- or bitmask)
*/
_GLIBCXX_SIMD_BEGIN_NAMESPACE
#if !_GLIBCXX_SIMD_X86INTRIN
using __m128 [[__gnu__::__vector_size__(16)]] = float;
using __m128d [[__gnu__::__vector_size__(16)]] = double;
using __m128i [[__gnu__::__vector_size__(16)]] = long long;
using __m256 [[__gnu__::__vector_size__(32)]] = float;
using __m256d [[__gnu__::__vector_size__(32)]] = double;
using __m256i [[__gnu__::__vector_size__(32)]] = long long;
using __m512 [[__gnu__::__vector_size__(64)]] = float;
using __m512d [[__gnu__::__vector_size__(64)]] = double;
using __m512i [[__gnu__::__vector_size__(64)]] = long long;
#endif
namespace simd_abi {
// simd_abi forward declarations {{{
// implementation details:
struct _Scalar;
template
struct _Fixed;
// There are two major ABIs that appear on different architectures.
// Both have non-boolean values packed into an N Byte register
// -> #elements = N / sizeof(T)
// Masks differ:
// 1. Use value vector registers for masks (all 0 or all 1)
// 2. Use bitmasks (mask registers) with one bit per value in the corresponding
// value vector
//
// Both can be partially used, masking off the rest when doing horizontal
// operations or operations that can trap (e.g. FP_INVALID or integer division
// by 0). This is encoded as the number of used bytes.
template
struct _VecBuiltin;
template
struct _VecBltnBtmsk;
template
using _VecN = _VecBuiltin;
template
using _Sse = _VecBuiltin<_UsedBytes>;
template
using _Avx = _VecBuiltin<_UsedBytes>;
template
using _Avx512 = _VecBltnBtmsk<_UsedBytes>;
template
using _Neon = _VecBuiltin<_UsedBytes>;
// implementation-defined:
using __sse = _Sse<>;
using __avx = _Avx<>;
using __avx512 = _Avx512<>;
using __neon = _Neon<>;
using __neon128 = _Neon<16>;
using __neon64 = _Neon<8>;
// standard:
template
struct deduce;
template
using fixed_size = _Fixed<_Np>;
using scalar = _Scalar;
// }}}
} // namespace simd_abi
// forward declarations is_simd(_mask), simd(_mask), simd_size {{{
template
struct is_simd;
template
struct is_simd_mask;
template
class simd;
template
class simd_mask;
template
struct simd_size;
// }}}
// load/store flags {{{
struct element_aligned_tag
{
template
static constexpr size_t _S_alignment = alignof(_Up);
template
_GLIBCXX_SIMD_INTRINSIC static constexpr _Up*
_S_apply(_Up* __ptr)
{ return __ptr; }
};
struct vector_aligned_tag
{
template
static constexpr size_t _S_alignment
= std::__bit_ceil(sizeof(_Up) * _Tp::size());
template
_GLIBCXX_SIMD_INTRINSIC static constexpr _Up*
_S_apply(_Up* __ptr)
{ return static_cast<_Up*>(__builtin_assume_aligned(__ptr, _S_alignment<_Tp, _Up>)); }
};
template struct overaligned_tag
{
template
static constexpr size_t _S_alignment = _Np;
template
_GLIBCXX_SIMD_INTRINSIC static constexpr _Up*
_S_apply(_Up* __ptr)
{ return static_cast<_Up*>(__builtin_assume_aligned(__ptr, _Np)); }
};
inline constexpr element_aligned_tag element_aligned = {};
inline constexpr vector_aligned_tag vector_aligned = {};
template
inline constexpr overaligned_tag<_Np> overaligned = {};
// }}}
template
using _SizeConstant = integral_constant;
// constexpr feature detection{{{
constexpr inline bool __have_mmx = _GLIBCXX_SIMD_HAVE_MMX;
constexpr inline bool __have_sse = _GLIBCXX_SIMD_HAVE_SSE;
constexpr inline bool __have_sse2 = _GLIBCXX_SIMD_HAVE_SSE2;
constexpr inline bool __have_sse3 = _GLIBCXX_SIMD_HAVE_SSE3;
constexpr inline bool __have_ssse3 = _GLIBCXX_SIMD_HAVE_SSSE3;
constexpr inline bool __have_sse4_1 = _GLIBCXX_SIMD_HAVE_SSE4_1;
constexpr inline bool __have_sse4_2 = _GLIBCXX_SIMD_HAVE_SSE4_2;
constexpr inline bool __have_xop = _GLIBCXX_SIMD_HAVE_XOP;
constexpr inline bool __have_avx = _GLIBCXX_SIMD_HAVE_AVX;
constexpr inline bool __have_avx2 = _GLIBCXX_SIMD_HAVE_AVX2;
constexpr inline bool __have_bmi = _GLIBCXX_SIMD_HAVE_BMI1;
constexpr inline bool __have_bmi2 = _GLIBCXX_SIMD_HAVE_BMI2;
constexpr inline bool __have_lzcnt = _GLIBCXX_SIMD_HAVE_LZCNT;
constexpr inline bool __have_sse4a = _GLIBCXX_SIMD_HAVE_SSE4A;
constexpr inline bool __have_fma = _GLIBCXX_SIMD_HAVE_FMA;
constexpr inline bool __have_fma4 = _GLIBCXX_SIMD_HAVE_FMA4;
constexpr inline bool __have_f16c = _GLIBCXX_SIMD_HAVE_F16C;
constexpr inline bool __have_popcnt = _GLIBCXX_SIMD_HAVE_POPCNT;
constexpr inline bool __have_avx512f = _GLIBCXX_SIMD_HAVE_AVX512F;
constexpr inline bool __have_avx512dq = _GLIBCXX_SIMD_HAVE_AVX512DQ;
constexpr inline bool __have_avx512vl = _GLIBCXX_SIMD_HAVE_AVX512VL;
constexpr inline bool __have_avx512bw = _GLIBCXX_SIMD_HAVE_AVX512BW;
constexpr inline bool __have_avx512dq_vl = __have_avx512dq && __have_avx512vl;
constexpr inline bool __have_avx512bw_vl = __have_avx512bw && __have_avx512vl;
constexpr inline bool __have_avx512bitalg = _GLIBCXX_SIMD_HAVE_AVX512BITALG;
constexpr inline bool __have_avx512vbmi2 = _GLIBCXX_SIMD_HAVE_AVX512VBMI2;
constexpr inline bool __have_avx512vbmi = _GLIBCXX_SIMD_HAVE_AVX512VBMI;
constexpr inline bool __have_avx512ifma = _GLIBCXX_SIMD_HAVE_AVX512IFMA;
constexpr inline bool __have_avx512cd = _GLIBCXX_SIMD_HAVE_AVX512CD;
constexpr inline bool __have_avx512vnni = _GLIBCXX_SIMD_HAVE_AVX512VNNI;
constexpr inline bool __have_avx512vpopcntdq = _GLIBCXX_SIMD_HAVE_AVX512VPOPCNTDQ;
constexpr inline bool __have_avx512vp2intersect = _GLIBCXX_SIMD_HAVE_AVX512VP2INTERSECT;
constexpr inline bool __have_neon = _GLIBCXX_SIMD_HAVE_NEON;
constexpr inline bool __have_neon_a32 = _GLIBCXX_SIMD_HAVE_NEON_A32;
constexpr inline bool __have_neon_a64 = _GLIBCXX_SIMD_HAVE_NEON_A64;
constexpr inline bool __support_neon_float =
#if defined __GCC_IEC_559
__GCC_IEC_559 == 0;
#elif defined __FAST_MATH__
true;
#else
false;
#endif
#ifdef _ARCH_PWR10
constexpr inline bool __have_power10vec = true;
#else
constexpr inline bool __have_power10vec = false;
#endif
#ifdef __POWER9_VECTOR__
constexpr inline bool __have_power9vec = true;
#else
constexpr inline bool __have_power9vec = false;
#endif
#if defined __POWER8_VECTOR__
constexpr inline bool __have_power8vec = true;
#else
constexpr inline bool __have_power8vec = __have_power9vec;
#endif
#if defined __VSX__
constexpr inline bool __have_power_vsx = true;
#else
constexpr inline bool __have_power_vsx = __have_power8vec;
#endif
#if defined __ALTIVEC__
constexpr inline bool __have_power_vmx = true;
#else
constexpr inline bool __have_power_vmx = __have_power_vsx;
#endif
// }}}
namespace __detail
{
#ifdef math_errhandling
// Determines _S_handle_fpexcept from math_errhandling if it is defined and expands to a constant
// expression. math_errhandling may expand to an extern symbol, in which case a constexpr value
// must be guessed.
template
constexpr bool
__handle_fpexcept_impl(int)
{ return math_errhandling & MATH_ERREXCEPT; }
#endif
// Fallback if math_errhandling doesn't work: with fast-math assume floating-point exceptions are
// ignored, otherwise implement correct exception behavior.
constexpr bool
__handle_fpexcept_impl(float)
{
#if defined __FAST_MATH__
return false;
#else
return true;
#endif
}
/// True if math functions must raise floating-point exceptions as specified by C17.
static constexpr bool _S_handle_fpexcept = __handle_fpexcept_impl(0);
constexpr std::uint_least64_t
__floating_point_flags()
{
std::uint_least64_t __flags = 0;
if constexpr (_S_handle_fpexcept)
__flags |= 1;
#ifdef __FAST_MATH__
__flags |= 1 << 1;
#elif __FINITE_MATH_ONLY__
__flags |= 2 << 1;
#elif __GCC_IEC_559 < 2
__flags |= 3 << 1;
#endif
__flags |= (__FLT_EVAL_METHOD__ + 1) << 3;
return __flags;
}
constexpr std::uint_least64_t
__machine_flags()
{
if constexpr (__have_mmx || __have_sse)
return __have_mmx
| (__have_sse << 1)
| (__have_sse2 << 2)
| (__have_sse3 << 3)
| (__have_ssse3 << 4)
| (__have_sse4_1 << 5)
| (__have_sse4_2 << 6)
| (__have_xop << 7)
| (__have_avx << 8)
| (__have_avx2 << 9)
| (__have_bmi << 10)
| (__have_bmi2 << 11)
| (__have_lzcnt << 12)
| (__have_sse4a << 13)
| (__have_fma << 14)
| (__have_fma4 << 15)
| (__have_f16c << 16)
| (__have_popcnt << 17)
| (__have_avx512f << 18)
| (__have_avx512dq << 19)
| (__have_avx512vl << 20)
| (__have_avx512bw << 21)
| (__have_avx512bitalg << 22)
| (__have_avx512vbmi2 << 23)
| (__have_avx512vbmi << 24)
| (__have_avx512ifma << 25)
| (__have_avx512cd << 26)
| (__have_avx512vnni << 27)
| (__have_avx512vpopcntdq << 28)
| (__have_avx512vp2intersect << 29);
else if constexpr (__have_neon)
return __have_neon
| (__have_neon_a32 << 1)
| (__have_neon_a64 << 2)
| (__have_neon_a64 << 2)
| (__support_neon_float << 3);
else if constexpr (__have_power_vmx)
return __have_power_vmx
| (__have_power_vsx << 1)
| (__have_power8vec << 2)
| (__have_power9vec << 3)
| (__have_power10vec << 4);
else
return 0;
}
namespace
{
struct _OdrEnforcer {};
}
template
struct _MachineFlagsTemplate {};
/**@internal
* Use this type as default template argument to all function templates that
* are not declared always_inline. It ensures, that a function
* specialization, which the compiler decides not to inline, has a unique symbol
* (_OdrEnforcer) or a symbol matching the machine/architecture flags
* (_MachineFlagsTemplate). This helps to avoid ODR violations in cases where
* users link TUs compiled with different flags. This is especially important
* for using simd in libraries.
*/
using __odr_helper
= conditional_t<__machine_flags() == 0, _OdrEnforcer,
_MachineFlagsTemplate<__machine_flags(), __floating_point_flags()>>;
struct _Minimum
{
template
_GLIBCXX_SIMD_INTRINSIC constexpr
_Tp
operator()(_Tp __a, _Tp __b) const
{
using std::min;
return min(__a, __b);
}
};
struct _Maximum
{
template
_GLIBCXX_SIMD_INTRINSIC constexpr
_Tp
operator()(_Tp __a, _Tp __b) const
{
using std::max;
return max(__a, __b);
}
};
} // namespace __detail
// unrolled/pack execution helpers
// __execute_n_times{{{
template
[[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr
void
__execute_on_index_sequence(_Fp&& __f, index_sequence<_I...>)
{ ((void)__f(_SizeConstant<_I>()), ...); }
template
_GLIBCXX_SIMD_INTRINSIC constexpr void
__execute_on_index_sequence(_Fp&&, index_sequence<>)
{ }
template
_GLIBCXX_SIMD_INTRINSIC constexpr void
__execute_n_times(_Fp&& __f)
{
__execute_on_index_sequence(static_cast<_Fp&&>(__f),
make_index_sequence<_Np>{});
}
// }}}
// __generate_from_n_evaluations{{{
template
[[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr
_R
__execute_on_index_sequence_with_return(_Fp&& __f, index_sequence<_I...>)
{ return _R{__f(_SizeConstant<_I>())...}; }
template
_GLIBCXX_SIMD_INTRINSIC constexpr _R
__generate_from_n_evaluations(_Fp&& __f)
{
return __execute_on_index_sequence_with_return<_R>(
static_cast<_Fp&&>(__f), make_index_sequence<_Np>{});
}
// }}}
// __call_with_n_evaluations{{{
template
[[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr
auto
__call_with_n_evaluations(index_sequence<_I...>, _F0&& __f0, _FArgs&& __fargs)
{ return __f0(__fargs(_SizeConstant<_I>())...); }
template
_GLIBCXX_SIMD_INTRINSIC constexpr auto
__call_with_n_evaluations(_F0&& __f0, _FArgs&& __fargs)
{
return __call_with_n_evaluations(make_index_sequence<_Np>{},
static_cast<_F0&&>(__f0),
static_cast<_FArgs&&>(__fargs));
}
// }}}
// __call_with_subscripts{{{
template
[[__gnu__::__flatten__]] _GLIBCXX_SIMD_INTRINSIC constexpr
auto
__call_with_subscripts(_Tp&& __x, index_sequence<_It...>, _Fp&& __fun)
{ return __fun(__x[_First + _It]...); }
template
_GLIBCXX_SIMD_INTRINSIC constexpr auto
__call_with_subscripts(_Tp&& __x, _Fp&& __fun)
{
return __call_with_subscripts<_First>(static_cast<_Tp&&>(__x),
make_index_sequence<_Np>(),
static_cast<_Fp&&>(__fun));
}
// }}}
// vvv ---- type traits ---- vvv
// integer type aliases{{{
using _UChar = unsigned char;
using _SChar = signed char;
using _UShort = unsigned short;
using _UInt = unsigned int;
using _ULong = unsigned long;
using _ULLong = unsigned long long;
using _LLong = long long;
//}}}
// __first_of_pack{{{
template
struct __first_of_pack
{ using type = _T0; };
template
using __first_of_pack_t = typename __first_of_pack<_Ts...>::type;
//}}}
// __value_type_or_identity_t {{{
template
typename _Tp::value_type
__value_type_or_identity_impl(int);
template
_Tp
__value_type_or_identity_impl(float);
template
using __value_type_or_identity_t
= decltype(__value_type_or_identity_impl<_Tp>(int()));
// }}}
// __is_vectorizable {{{
template
struct __is_vectorizable : public is_arithmetic<_Tp> {};
template <>
struct __is_vectorizable : public false_type {};
template
inline constexpr bool __is_vectorizable_v = __is_vectorizable<_Tp>::value;
// Deduces to a vectorizable type
template >>
using _Vectorizable = _Tp;
// }}}
// _LoadStorePtr / __is_possible_loadstore_conversion {{{
template
struct __is_possible_loadstore_conversion
: conjunction<__is_vectorizable<_Ptr>, __is_vectorizable<_ValueType>> {};
template <>
struct __is_possible_loadstore_conversion : true_type {};
// Deduces to a type allowed for load/store with the given value type.
template ::value>>
using _LoadStorePtr = _Ptr;
// }}}
// __is_bitmask{{{
template >
struct __is_bitmask : false_type {};
template
inline constexpr bool __is_bitmask_v = __is_bitmask<_Tp>::value;
// the __mmaskXX case:
template
struct __is_bitmask<_Tp,
void_t() = declval<_Tp>() & 1u)>>
: true_type {};
// }}}
// __int_for_sizeof{{{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
template
constexpr auto
__int_for_sizeof()
{
static_assert(_Bytes > 0);
if constexpr (_Bytes == sizeof(int))
return int();
#ifdef __clang__
else if constexpr (_Bytes == sizeof(char))
return char();
#else
else if constexpr (_Bytes == sizeof(_SChar))
return _SChar();
#endif
else if constexpr (_Bytes == sizeof(short))
return short();
#ifndef __clang__
else if constexpr (_Bytes == sizeof(long))
return long();
#endif
else if constexpr (_Bytes == sizeof(_LLong))
return _LLong();
#ifdef __SIZEOF_INT128__
else if constexpr (_Bytes == sizeof(__int128))
return __int128();
#endif // __SIZEOF_INT128__
else if constexpr (_Bytes % sizeof(int) == 0)
{
constexpr size_t _Np = _Bytes / sizeof(int);
struct _Ip
{
int _M_data[_Np];
_GLIBCXX_SIMD_INTRINSIC constexpr _Ip
operator&(_Ip __rhs) const
{
return __generate_from_n_evaluations<_Np, _Ip>(
[&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
return __rhs._M_data[__i] & _M_data[__i];
});
}
_GLIBCXX_SIMD_INTRINSIC constexpr _Ip
operator|(_Ip __rhs) const
{
return __generate_from_n_evaluations<_Np, _Ip>(
[&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
return __rhs._M_data[__i] | _M_data[__i];
});
}
_GLIBCXX_SIMD_INTRINSIC constexpr _Ip
operator^(_Ip __rhs) const
{
return __generate_from_n_evaluations<_Np, _Ip>(
[&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA {
return __rhs._M_data[__i] ^ _M_data[__i];
});
}
_GLIBCXX_SIMD_INTRINSIC constexpr _Ip
operator~() const
{
return __generate_from_n_evaluations<_Np, _Ip>(
[&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { return ~_M_data[__i]; });
}
};
return _Ip{};
}
else
static_assert(_Bytes == 0, "this should be unreachable");
}
#pragma GCC diagnostic pop
template
using __int_for_sizeof_t = decltype(__int_for_sizeof());
template
using __int_with_sizeof_t = decltype(__int_for_sizeof<_Np>());
// }}}
// __is_fixed_size_abi{{{
template
struct __is_fixed_size_abi : false_type {};
template
struct __is_fixed_size_abi> : true_type {};
template
inline constexpr bool __is_fixed_size_abi_v = __is_fixed_size_abi<_Tp>::value;
// }}}
// __is_scalar_abi {{{
template
constexpr bool
__is_scalar_abi()
{ return is_same_v; }
// }}}
// __abi_bytes_v {{{
template class _Abi, int _Bytes>
constexpr int
__abi_bytes_impl(_Abi<_Bytes>*)
{ return _Bytes; }
template
constexpr int
__abi_bytes_impl(_Tp*)
{ return -1; }
template
inline constexpr int __abi_bytes_v
= __abi_bytes_impl(static_cast<_Abi*>(nullptr));
// }}}
// __is_builtin_bitmask_abi {{{
template
constexpr bool
__is_builtin_bitmask_abi()
{ return is_same_v>, _Abi>; }
// }}}
// __is_sse_abi {{{
template
constexpr bool
__is_sse_abi()
{
constexpr auto _Bytes = __abi_bytes_v<_Abi>;
return _Bytes <= 16 && is_same_v, _Abi>;
}
// }}}
// __is_avx_abi {{{
template
constexpr bool
__is_avx_abi()
{
constexpr auto _Bytes = __abi_bytes_v<_Abi>;
return _Bytes > 16 && _Bytes <= 32
&& is_same_v, _Abi>;
}
// }}}
// __is_avx512_abi {{{
template
constexpr bool
__is_avx512_abi()
{
constexpr auto _Bytes = __abi_bytes_v<_Abi>;
return _Bytes <= 64 && is_same_v, _Abi>;
}
// }}}
// __is_neon_abi {{{
template
constexpr bool
__is_neon_abi()
{
constexpr auto _Bytes = __abi_bytes_v<_Abi>;
return _Bytes <= 16 && is_same_v, _Abi>;
}
// }}}
// __make_dependent_t {{{
template
struct __make_dependent
{ using type = _Up; };
template
using __make_dependent_t = typename __make_dependent<_Tp, _Up>::type;
// }}}
// ^^^ ---- type traits ---- ^^^
// __invoke_ub{{{
template
[[noreturn]] _GLIBCXX_SIMD_ALWAYS_INLINE void
__invoke_ub([[maybe_unused]] const char* __msg, [[maybe_unused]] const _Args&... __args)
{
#ifdef _GLIBCXX_DEBUG_UB
__builtin_fprintf(stderr, __msg, __args...);
__builtin_trap();
#else
__builtin_unreachable();
#endif
}
// }}}
// __assert_unreachable{{{
template
struct __assert_unreachable
{ static_assert(!is_same_v<_Tp, _Tp>, "this should be unreachable"); };
// }}}
// __size_or_zero_v {{{
template ::value>
constexpr size_t
__size_or_zero_dispatch(int)
{ return _Np; }
template
constexpr size_t
__size_or_zero_dispatch(float)
{ return 0; }
template
inline constexpr size_t __size_or_zero_v
= __size_or_zero_dispatch<_Tp, _Ap>(0);
// }}}
// __div_roundup {{{
inline constexpr size_t
__div_roundup(size_t __a, size_t __b)
{ return (__a + __b - 1) / __b; }
// }}}
// _ExactBool{{{
class _ExactBool
{
const bool _M_data;
public:
_GLIBCXX_SIMD_INTRINSIC constexpr
_ExactBool(bool __b) : _M_data(__b) {}
_ExactBool(int) = delete;
_GLIBCXX_SIMD_INTRINSIC constexpr
operator bool() const
{ return _M_data; }
};
// }}}
// __may_alias{{{
/**@internal
* Helper __may_alias<_Tp> that turns _Tp into the type to be used for an
* aliasing pointer. This adds the __may_alias attribute to _Tp (with compilers
* that support it).
*/
template
using __may_alias [[__gnu__::__may_alias__]] = _Tp;
// }}}
// _UnsupportedBase {{{
// simd and simd_mask base for unsupported <_Tp, _Abi>
struct _UnsupportedBase
{
_UnsupportedBase() = delete;
_UnsupportedBase(const _UnsupportedBase&) = delete;
_UnsupportedBase& operator=(const _UnsupportedBase&) = delete;
~_UnsupportedBase() = delete;
};
// }}}
// _InvalidTraits {{{
/**
* @internal
* Defines the implementation of __a given <_Tp, _Abi>.
*
* Implementations must ensure that only valid <_Tp, _Abi> instantiations are
* possible. Static assertions in the type definition do not suffice. It is
* important that SFINAE works.
*/
struct _InvalidTraits
{
using _IsValid = false_type;
using _SimdBase = _UnsupportedBase;
using _MaskBase = _UnsupportedBase;
static constexpr size_t _S_full_size = 0;
static constexpr bool _S_is_partial = false;
static constexpr size_t _S_simd_align = 1;
struct _SimdImpl;
struct _SimdMember {};
struct _SimdCastType;
static constexpr size_t _S_mask_align = 1;
struct _MaskImpl;
struct _MaskMember {};
struct _MaskCastType;
};
// }}}
// _SimdTraits {{{
template >
struct _SimdTraits : _InvalidTraits {};
// }}}
// __private_init, __bitset_init{{{
/**
* @internal
* Tag used for private init constructor of simd and simd_mask
*/
inline constexpr struct _PrivateInit {} __private_init = {};
inline constexpr struct _BitsetInit {} __bitset_init = {};
// }}}
// __is_narrowing_conversion<_From, _To>{{{
template ,
bool = is_arithmetic_v<_To>>
struct __is_narrowing_conversion;
// ignore "signed/unsigned mismatch" in the following trait.
// The implicit conversions will do the right thing here.
template
struct __is_narrowing_conversion<_From, _To, true, true>
: public __bool_constant<(
__digits_v<_From> > __digits_v<_To>
|| __finite_max_v<_From> > __finite_max_v<_To>
|| __finite_min_v<_From> < __finite_min_v<_To>
|| (is_signed_v<_From> && is_unsigned_v<_To>))> {};
template
struct __is_narrowing_conversion<_Tp, bool, true, true>
: public true_type {};
template <>
struct __is_narrowing_conversion
: public false_type {};
template
struct __is_narrowing_conversion<_Tp, _Tp, true, true>
: public false_type {};
template
struct __is_narrowing_conversion<_From, _To, false, true>
: public negation> {};
// }}}
// __converts_to_higher_integer_rank{{{
template
struct __converts_to_higher_integer_rank : public true_type {};
// this may fail for char -> short if sizeof(char) == sizeof(short)
template
struct __converts_to_higher_integer_rank<_From, _To, false>
: public is_same() + declval<_To>()), _To> {};
// }}}
// __data(simd/simd_mask) {{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr const auto&
__data(const simd<_Tp, _Ap>& __x);
template
_GLIBCXX_SIMD_INTRINSIC constexpr auto&
__data(simd<_Tp, _Ap>& __x);
template
_GLIBCXX_SIMD_INTRINSIC constexpr const auto&
__data(const simd_mask<_Tp, _Ap>& __x);
template
_GLIBCXX_SIMD_INTRINSIC constexpr auto&
__data(simd_mask<_Tp, _Ap>& __x);
// }}}
// _SimdConverter {{{
template
struct _SimdConverter;
template
struct _SimdConverter<_Tp, _Ap, _Tp, _Ap, void>
{
template
_GLIBCXX_SIMD_INTRINSIC const _Up&
operator()(const _Up& __x)
{ return __x; }
};
// }}}
// __to_value_type_or_member_type {{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr auto
__to_value_type_or_member_type(const _V& __x) -> decltype(__data(__x))
{ return __data(__x); }
template
_GLIBCXX_SIMD_INTRINSIC constexpr const typename _V::value_type&
__to_value_type_or_member_type(const typename _V::value_type& __x)
{ return __x; }
// }}}
// __bool_storage_member_type{{{
template
struct __bool_storage_member_type;
template
using __bool_storage_member_type_t =
typename __bool_storage_member_type<_Size>::type;
// }}}
// _SimdTuple {{{
// why not tuple?
// 1. tuple gives no guarantee about the storage order, but I require
// storage
// equivalent to array<_Tp, _Np>
// 2. direct access to the element type (first template argument)
// 3. enforces equal element type, only different _Abi types are allowed
template
struct _SimdTuple;
//}}}
// __fixed_size_storage_t {{{
template
struct __fixed_size_storage;
template
using __fixed_size_storage_t = typename __fixed_size_storage<_Tp, _Np>::type;
// }}}
// _SimdWrapper fwd decl{{{
template >
struct _SimdWrapper;
template
using _SimdWrapper8 = _SimdWrapper<_Tp, 8 / sizeof(_Tp)>;
template
using _SimdWrapper16 = _SimdWrapper<_Tp, 16 / sizeof(_Tp)>;
template
using _SimdWrapper32 = _SimdWrapper<_Tp, 32 / sizeof(_Tp)>;
template
using _SimdWrapper64 = _SimdWrapper<_Tp, 64 / sizeof(_Tp)>;
// }}}
// __is_simd_wrapper {{{
template
struct __is_simd_wrapper : false_type {};
template
struct __is_simd_wrapper<_SimdWrapper<_Tp, _Np>> : true_type {};
template
inline constexpr bool __is_simd_wrapper_v = __is_simd_wrapper<_Tp>::value;
// }}}
// _BitOps {{{
struct _BitOps
{
// _S_bit_iteration {{{
template
static void
_S_bit_iteration(_Tp __mask, _Fp&& __f)
{
static_assert(sizeof(_ULLong) >= sizeof(_Tp));
conditional_t __k;
if constexpr (is_convertible_v<_Tp, decltype(__k)>)
__k = __mask;
else
__k = __mask.to_ullong();
while(__k)
{
__f(std::__countr_zero(__k));
__k &= (__k - 1);
}
}
//}}}
};
//}}}
// __increment, __decrement {{{
template
struct __increment
{ constexpr _Tp operator()(_Tp __a) const { return ++__a; } };
template <>
struct __increment
{
template
constexpr _Tp
operator()(_Tp __a) const
{ return ++__a; }
};
template
struct __decrement
{ constexpr _Tp operator()(_Tp __a) const { return --__a; } };
template <>
struct __decrement
{
template
constexpr _Tp
operator()(_Tp __a) const
{ return --__a; }
};
// }}}
// _ValuePreserving(OrInt) {{{
template , _To>>::value>>
using _ValuePreserving = _From;
template ,
typename = enable_if_t,
disjunction<
is_same<_DecayedFrom, _To>, is_same<_DecayedFrom, int>,
conjunction, is_unsigned<_To>>,
negation<__is_narrowing_conversion<_DecayedFrom, _To>>>>::value>>
using _ValuePreservingOrInt = _From;
// }}}
// __intrinsic_type {{{
template >
struct __intrinsic_type;
template
using __intrinsic_type_t =
typename __intrinsic_type<_Tp, _Size * sizeof(_Tp)>::type;
template
using __intrinsic_type2_t = typename __intrinsic_type<_Tp, 2>::type;
template
using __intrinsic_type4_t = typename __intrinsic_type<_Tp, 4>::type;
template
using __intrinsic_type8_t = typename __intrinsic_type<_Tp, 8>::type;
template
using __intrinsic_type16_t = typename __intrinsic_type<_Tp, 16>::type;
template
using __intrinsic_type32_t = typename __intrinsic_type<_Tp, 32>::type;
template
using __intrinsic_type64_t = typename __intrinsic_type<_Tp, 64>::type;
// }}}
// _BitMask {{{
template
struct _BitMask;
template
struct __is_bitmask<_BitMask<_Np, _Sanitized>, void> : true_type {};
template
using _SanitizedBitMask = _BitMask<_Np, true>;
template
struct _BitMask
{
static_assert(_Np > 0);
static constexpr size_t _NBytes = __div_roundup(_Np, __CHAR_BIT__);
using _Tp = conditional_t<_Np == 1, bool,
make_unsigned_t<__int_with_sizeof_t>>;
static constexpr int _S_array_size = __div_roundup(_NBytes, sizeof(_Tp));
_Tp _M_bits[_S_array_size];
static constexpr int _S_unused_bits
= _Np == 1 ? 0 : _S_array_size * sizeof(_Tp) * __CHAR_BIT__ - _Np;
static constexpr _Tp _S_bitmask = +_Tp(~_Tp()) >> _S_unused_bits;
constexpr _BitMask() noexcept = default;
constexpr _BitMask(unsigned long long __x) noexcept
: _M_bits{static_cast<_Tp>(__x)} {}
_BitMask(bitset<_Np> __x) noexcept : _BitMask(__x.to_ullong()) {}
constexpr _BitMask(const _BitMask&) noexcept = default;
template >
constexpr _BitMask(const _BitMask<_Np, _RhsSanitized>& __rhs) noexcept
: _BitMask(__rhs._M_sanitized()) {}
constexpr operator _SimdWrapper() const noexcept
{
static_assert(_S_array_size == 1);
return _M_bits[0];
}
// precondition: is sanitized
constexpr _Tp
_M_to_bits() const noexcept
{
static_assert(_S_array_size == 1);
return _M_bits[0];
}
// precondition: is sanitized
constexpr unsigned long long
to_ullong() const noexcept
{
static_assert(_S_array_size == 1);
return _M_bits[0];
}
// precondition: is sanitized
constexpr unsigned long
to_ulong() const noexcept
{
static_assert(_S_array_size == 1);
return _M_bits[0];
}
constexpr bitset<_Np>
_M_to_bitset() const noexcept
{
static_assert(_S_array_size == 1);
return _M_bits[0];
}
constexpr decltype(auto)
_M_sanitized() const noexcept
{
if constexpr (_Sanitized)
return *this;
else if constexpr (_Np == 1)
return _SanitizedBitMask<_Np>(_M_bits[0]);
else
{
_SanitizedBitMask<_Np> __r = {};
for (int __i = 0; __i < _S_array_size; ++__i)
__r._M_bits[__i] = _M_bits[__i];
if constexpr (_S_unused_bits > 0)
__r._M_bits[_S_array_size - 1] &= _S_bitmask;
return __r;
}
}
template
constexpr _BitMask<_Np + _Mp, _Sanitized>
_M_prepend(_BitMask<_Mp, _LSanitized> __lsb) const noexcept
{
constexpr size_t _RN = _Np + _Mp;
using _Rp = _BitMask<_RN, _Sanitized>;
if constexpr (_Rp::_S_array_size == 1)
{
_Rp __r{{_M_bits[0]}};
__r._M_bits[0] <<= _Mp;
__r._M_bits[0] |= __lsb._M_sanitized()._M_bits[0];
return __r;
}
else
__assert_unreachable<_Rp>();
}
// Return a new _BitMask with size _NewSize while dropping _DropLsb least
// significant bits. If the operation implicitly produces a sanitized bitmask,
// the result type will have _Sanitized set.
template
constexpr auto
_M_extract() const noexcept
{
static_assert(_Np > _DropLsb);
static_assert(_DropLsb + _NewSize <= sizeof(_ULLong) * __CHAR_BIT__,
"not implemented for bitmasks larger than one ullong");
if constexpr (_NewSize == 1)
// must sanitize because the return _Tp is bool
return _SanitizedBitMask<1>(_M_bits[0] & (_Tp(1) << _DropLsb));
else
return _BitMask<_NewSize,
((_NewSize + _DropLsb == sizeof(_Tp) * __CHAR_BIT__
&& _NewSize + _DropLsb <= _Np)
|| ((_Sanitized || _Np == sizeof(_Tp) * __CHAR_BIT__)
&& _NewSize + _DropLsb >= _Np))>(_M_bits[0]
>> _DropLsb);
}
// True if all bits are set. Implicitly sanitizes if _Sanitized == false.
constexpr bool
all() const noexcept
{
if constexpr (_Np == 1)
return _M_bits[0];
else if constexpr (!_Sanitized)
return _M_sanitized().all();
else
{
constexpr _Tp __allbits = ~_Tp();
for (int __i = 0; __i < _S_array_size - 1; ++__i)
if (_M_bits[__i] != __allbits)
return false;
return _M_bits[_S_array_size - 1] == _S_bitmask;
}
}
// True if at least one bit is set. Implicitly sanitizes if _Sanitized ==
// false.
constexpr bool
any() const noexcept
{
if constexpr (_Np == 1)
return _M_bits[0];
else if constexpr (!_Sanitized)
return _M_sanitized().any();
else
{
for (int __i = 0; __i < _S_array_size - 1; ++__i)
if (_M_bits[__i] != 0)
return true;
return _M_bits[_S_array_size - 1] != 0;
}
}
// True if no bit is set. Implicitly sanitizes if _Sanitized == false.
constexpr bool
none() const noexcept
{
if constexpr (_Np == 1)
return !_M_bits[0];
else if constexpr (!_Sanitized)
return _M_sanitized().none();
else
{
for (int __i = 0; __i < _S_array_size - 1; ++__i)
if (_M_bits[__i] != 0)
return false;
return _M_bits[_S_array_size - 1] == 0;
}
}
// Returns the number of set bits. Implicitly sanitizes if _Sanitized ==
// false.
constexpr int
count() const noexcept
{
if constexpr (_Np == 1)
return _M_bits[0];
else if constexpr (!_Sanitized)
return _M_sanitized().none();
else
{
int __result = __builtin_popcountll(_M_bits[0]);
for (int __i = 1; __i < _S_array_size; ++__i)
__result += __builtin_popcountll(_M_bits[__i]);
return __result;
}
}
// Returns the bit at offset __i as bool.
constexpr bool
operator[](size_t __i) const noexcept
{
if constexpr (_Np == 1)
return _M_bits[0];
else if constexpr (_S_array_size == 1)
return (_M_bits[0] >> __i) & 1;
else
{
const size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
const size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
return (_M_bits[__j] >> __shift) & 1;
}
}
template
constexpr bool
operator[](_SizeConstant<__i>) const noexcept
{
static_assert(__i < _Np);
constexpr size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
constexpr size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
return static_cast(_M_bits[__j] & (_Tp(1) << __shift));
}
// Set the bit at offset __i to __x.
constexpr void
set(size_t __i, bool __x) noexcept
{
if constexpr (_Np == 1)
_M_bits[0] = __x;
else if constexpr (_S_array_size == 1)
{
_M_bits[0] &= ~_Tp(_Tp(1) << __i);
_M_bits[0] |= _Tp(_Tp(__x) << __i);
}
else
{
const size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
const size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
_M_bits[__j] &= ~_Tp(_Tp(1) << __shift);
_M_bits[__j] |= _Tp(_Tp(__x) << __shift);
}
}
template
constexpr void
set(_SizeConstant<__i>, bool __x) noexcept
{
static_assert(__i < _Np);
if constexpr (_Np == 1)
_M_bits[0] = __x;
else
{
constexpr size_t __j = __i / (sizeof(_Tp) * __CHAR_BIT__);
constexpr size_t __shift = __i % (sizeof(_Tp) * __CHAR_BIT__);
constexpr _Tp __mask = ~_Tp(_Tp(1) << __shift);
_M_bits[__j] &= __mask;
_M_bits[__j] |= _Tp(_Tp(__x) << __shift);
}
}
// Inverts all bits. Sanitized input leads to sanitized output.
constexpr _BitMask
operator~() const noexcept
{
if constexpr (_Np == 1)
return !_M_bits[0];
else
{
_BitMask __result{};
for (int __i = 0; __i < _S_array_size - 1; ++__i)
__result._M_bits[__i] = ~_M_bits[__i];
if constexpr (_Sanitized)
__result._M_bits[_S_array_size - 1]
= _M_bits[_S_array_size - 1] ^ _S_bitmask;
else
__result._M_bits[_S_array_size - 1] = ~_M_bits[_S_array_size - 1];
return __result;
}
}
constexpr _BitMask&
operator^=(const _BitMask& __b) & noexcept
{
__execute_n_times<_S_array_size>(
[&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { _M_bits[__i] ^= __b._M_bits[__i]; });
return *this;
}
constexpr _BitMask&
operator|=(const _BitMask& __b) & noexcept
{
__execute_n_times<_S_array_size>(
[&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { _M_bits[__i] |= __b._M_bits[__i]; });
return *this;
}
constexpr _BitMask&
operator&=(const _BitMask& __b) & noexcept
{
__execute_n_times<_S_array_size>(
[&](auto __i) _GLIBCXX_SIMD_ALWAYS_INLINE_LAMBDA { _M_bits[__i] &= __b._M_bits[__i]; });
return *this;
}
friend constexpr _BitMask
operator^(const _BitMask& __a, const _BitMask& __b) noexcept
{
_BitMask __r = __a;
__r ^= __b;
return __r;
}
friend constexpr _BitMask
operator|(const _BitMask& __a, const _BitMask& __b) noexcept
{
_BitMask __r = __a;
__r |= __b;
return __r;
}
friend constexpr _BitMask
operator&(const _BitMask& __a, const _BitMask& __b) noexcept
{
_BitMask __r = __a;
__r &= __b;
return __r;
}
_GLIBCXX_SIMD_INTRINSIC
constexpr bool
_M_is_constprop() const
{
if constexpr (_S_array_size == 0)
return __builtin_constant_p(_M_bits[0]);
else
{
for (int __i = 0; __i < _S_array_size; ++__i)
if (!__builtin_constant_p(_M_bits[__i]))
return false;
return true;
}
}
};
// }}}
// vvv ---- builtin vector types [[gnu::vector_size(N)]] and operations ---- vvv
// __min_vector_size {{{
template
static inline constexpr int __min_vector_size = 2 * sizeof(_Tp);
#if _GLIBCXX_SIMD_HAVE_NEON
template <>
inline constexpr int __min_vector_size = 8;
#else
template <>
inline constexpr int __min_vector_size = 16;
#endif
// }}}
// __vector_type {{{
template
struct __vector_type_n {};
// substition failure for 0-element case
template
struct __vector_type_n<_Tp, 0, void> {};
// special case 1-element to be _Tp itself
template
struct __vector_type_n<_Tp, 1, enable_if_t<__is_vectorizable_v<_Tp>>>
{ using type = _Tp; };
// else, use GNU-style builtin vector types
template
struct __vector_type_n<_Tp, _Np, enable_if_t<__is_vectorizable_v<_Tp> && _Np >= 2>>
{
static constexpr size_t _S_Np2 = std::__bit_ceil(_Np * sizeof(_Tp));
static constexpr size_t _S_Bytes =
#ifdef __i386__
// Using [[gnu::vector_size(8)]] would wreak havoc on the FPU because
// those objects are passed via MMX registers and nothing ever calls EMMS.
_S_Np2 == 8 ? 16 :
#endif
_S_Np2 < __min_vector_size<_Tp> ? __min_vector_size<_Tp>
: _S_Np2;
using type [[__gnu__::__vector_size__(_S_Bytes)]] = _Tp;
};
template
struct __vector_type;
template
struct __vector_type<_Tp, _Bytes, 0>
: __vector_type_n<_Tp, _Bytes / sizeof(_Tp)> {};
template
using __vector_type_t = typename __vector_type_n<_Tp, _Size>::type;
template
using __vector_type2_t = typename __vector_type<_Tp, 2>::type;
template
using __vector_type4_t = typename __vector_type<_Tp, 4>::type;
template
using __vector_type8_t = typename __vector_type<_Tp, 8>::type;
template
using __vector_type16_t = typename __vector_type<_Tp, 16>::type;
template
using __vector_type32_t = typename __vector_type<_Tp, 32>::type;
template
using __vector_type64_t = typename __vector_type<_Tp, 64>::type;
// }}}
// __is_vector_type {{{
template >
struct __is_vector_type : false_type {};
template
struct __is_vector_type<
_Tp, void_t()[0])>, sizeof(_Tp)>::type>>
: is_same<_Tp, typename __vector_type<
remove_reference_t()[0])>,
sizeof(_Tp)>::type> {};
template
inline constexpr bool __is_vector_type_v = __is_vector_type<_Tp>::value;
// }}}
// __is_intrinsic_type {{{
#if _GLIBCXX_SIMD_HAVE_SSE_ABI
template
using __is_intrinsic_type = __is_vector_type<_Tp>;
#else // not SSE (x86)
template >
struct __is_intrinsic_type : false_type {};
template
struct __is_intrinsic_type<
_Tp, void_t()[0])>, sizeof(_Tp)>::type>>
: is_same<_Tp, typename __intrinsic_type<
remove_reference_t()[0])>,
sizeof(_Tp)>::type> {};
#endif
template
inline constexpr bool __is_intrinsic_type_v = __is_intrinsic_type<_Tp>::value;
// }}}
// _VectorTraits{{{
template >
struct _VectorTraitsImpl;
template
struct _VectorTraitsImpl<_Tp, enable_if_t<__is_vector_type_v<_Tp>
|| __is_intrinsic_type_v<_Tp>>>
{
using type = _Tp;
using value_type = remove_reference_t()[0])>;
static constexpr int _S_full_size = sizeof(_Tp) / sizeof(value_type);
using _Wrapper = _SimdWrapper;
template
static constexpr bool _S_is
= is_same_v && _W == _S_full_size;
};
template
struct _VectorTraitsImpl<_SimdWrapper<_Tp, _Np>,
void_t<__vector_type_t<_Tp, _Np>>>
{
using type = __vector_type_t<_Tp, _Np>;
using value_type = _Tp;
static constexpr int _S_full_size = sizeof(type) / sizeof(value_type);
using _Wrapper = _SimdWrapper<_Tp, _Np>;
static constexpr bool _S_is_partial = (_Np == _S_full_size);
static constexpr int _S_partial_width = _Np;
template
static constexpr bool _S_is
= is_same_v&& _W == _S_full_size;
};
template ::type>
using _VectorTraits = _VectorTraitsImpl<_Tp>;
// }}}
// __as_vector{{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr auto
__as_vector(_V __x)
{
if constexpr (__is_vector_type_v<_V>)
return __x;
else if constexpr (is_simd<_V>::value || is_simd_mask<_V>::value)
return __data(__x)._M_data;
else if constexpr (__is_vectorizable_v<_V>)
return __vector_type_t<_V, 2>{__x};
else
return __x._M_data;
}
// }}}
// __as_wrapper{{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr auto
__as_wrapper(_V __x)
{
if constexpr (__is_vector_type_v<_V>)
return _SimdWrapper::value_type,
(_Np > 0 ? _Np : _VectorTraits<_V>::_S_full_size)>(__x);
else if constexpr (is_simd<_V>::value || is_simd_mask<_V>::value)
{
static_assert(_V::size() == _Np);
return __data(__x);
}
else
{
static_assert(_V::_S_size == _Np);
return __x;
}
}
// }}}
// __intrin_bitcast{{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr _To
__intrin_bitcast(_From __v)
{
static_assert((__is_vector_type_v<_From> || __is_intrinsic_type_v<_From>)
&& (__is_vector_type_v<_To> || __is_intrinsic_type_v<_To>));
if constexpr (sizeof(_To) == sizeof(_From))
return reinterpret_cast<_To>(__v);
else if constexpr (sizeof(_From) > sizeof(_To))
if constexpr (sizeof(_To) >= 16)
return reinterpret_cast&>(__v);
else
{
_To __r;
__builtin_memcpy(&__r, &__v, sizeof(_To));
return __r;
}
#if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__
else if constexpr (__have_avx && sizeof(_From) == 16 && sizeof(_To) == 32)
return reinterpret_cast<_To>(__builtin_ia32_ps256_ps(
reinterpret_cast<__vector_type_t>(__v)));
else if constexpr (__have_avx512f && sizeof(_From) == 16
&& sizeof(_To) == 64)
return reinterpret_cast<_To>(__builtin_ia32_ps512_ps(
reinterpret_cast<__vector_type_t>(__v)));
else if constexpr (__have_avx512f && sizeof(_From) == 32
&& sizeof(_To) == 64)
return reinterpret_cast<_To>(__builtin_ia32_ps512_256ps(
reinterpret_cast<__vector_type_t>(__v)));
#endif // _GLIBCXX_SIMD_X86INTRIN
else if constexpr (sizeof(__v) <= 8)
return reinterpret_cast<_To>(
__vector_type_t<__int_for_sizeof_t<_From>, sizeof(_To) / sizeof(_From)>{
reinterpret_cast<__int_for_sizeof_t<_From>>(__v)});
else
{
static_assert(sizeof(_To) > sizeof(_From));
_To __r = {};
__builtin_memcpy(&__r, &__v, sizeof(_From));
return __r;
}
}
// }}}
// __vector_bitcast{{{
template ,
size_t _Np = _NN == 0 ? sizeof(_From) / sizeof(_To) : _NN>
_GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_To, _Np>
__vector_bitcast(_From __x)
{
using _R = __vector_type_t<_To, _Np>;
return __intrin_bitcast<_R>(__x);
}
template ) / sizeof(_To) : _NN>
_GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_To, _Np>
__vector_bitcast(const _SimdWrapper<_Tp, _Nx>& __x)
{
static_assert(_Np > 1);
return __intrin_bitcast<__vector_type_t<_To, _Np>>(__x._M_data);
}
// }}}
// __convert_x86 declarations {{{
#ifdef _GLIBCXX_SIMD_WORKAROUND_PR85048
template >
_To __convert_x86(_Tp);
template >
_To __convert_x86(_Tp, _Tp);
template >
_To __convert_x86(_Tp, _Tp, _Tp, _Tp);
template >
_To __convert_x86(_Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp);
template >
_To __convert_x86(_Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp, _Tp,
_Tp, _Tp, _Tp, _Tp);
#endif // _GLIBCXX_SIMD_WORKAROUND_PR85048
//}}}
// __bit_cast {{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr _To
__bit_cast(const _From __x)
{
#if __has_builtin(__builtin_bit_cast)
return __builtin_bit_cast(_To, __x);
#else
static_assert(sizeof(_To) == sizeof(_From));
constexpr bool __to_is_vectorizable
= is_arithmetic_v<_To> || is_enum_v<_To>;
constexpr bool __from_is_vectorizable
= is_arithmetic_v<_From> || is_enum_v<_From>;
if constexpr (__is_vector_type_v<_To> && __is_vector_type_v<_From>)
return reinterpret_cast<_To>(__x);
else if constexpr (__is_vector_type_v<_To> && __from_is_vectorizable)
{
using _FV [[__gnu__::__vector_size__(sizeof(_From))]] = _From;
return reinterpret_cast<_To>(_FV{__x});
}
else if constexpr (__to_is_vectorizable && __from_is_vectorizable)
{
using _TV [[__gnu__::__vector_size__(sizeof(_To))]] = _To;
using _FV [[__gnu__::__vector_size__(sizeof(_From))]] = _From;
return reinterpret_cast<_TV>(_FV{__x})[0];
}
else if constexpr (__to_is_vectorizable && __is_vector_type_v<_From>)
{
using _TV [[__gnu__::__vector_size__(sizeof(_To))]] = _To;
return reinterpret_cast<_TV>(__x)[0];
}
else
{
_To __r;
__builtin_memcpy(reinterpret_cast(&__r),
reinterpret_cast(&__x), sizeof(_To));
return __r;
}
#endif
}
// }}}
// __to_intrin {{{
template ,
typename _R = __intrinsic_type_t>
_GLIBCXX_SIMD_INTRINSIC constexpr _R
__to_intrin(_Tp __x)
{
static_assert(sizeof(__x) <= sizeof(_R),
"__to_intrin may never drop values off the end");
if constexpr (sizeof(__x) == sizeof(_R))
return reinterpret_cast<_R>(__as_vector(__x));
else
{
using _Up = __int_for_sizeof_t<_Tp>;
return reinterpret_cast<_R>(
__vector_type_t<_Up, sizeof(_R) / sizeof(_Up)>{__bit_cast<_Up>(__x)});
}
}
// }}}
// __make_vector{{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, sizeof...(_Args)>
__make_vector(const _Args&... __args)
{ return __vector_type_t<_Tp, sizeof...(_Args)>{static_cast<_Tp>(__args)...}; }
// }}}
// __vector_broadcast{{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np>
__vector_broadcast_impl(_Tp __x, index_sequence<_I...>)
{ return __vector_type_t<_Tp, _Np>{((void)_I, __x)...}; }
template
_GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np>
__vector_broadcast(_Tp __x)
{ return __vector_broadcast_impl<_Np, _Tp>(__x, make_index_sequence<_Np>()); }
// }}}
// __generate_vector{{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np>
__generate_vector_impl(_Gp&& __gen, index_sequence<_I...>)
{ return __vector_type_t<_Tp, _Np>{ static_cast<_Tp>(__gen(_SizeConstant<_I>()))...}; }
template , typename _Gp>
_GLIBCXX_SIMD_INTRINSIC constexpr _V
__generate_vector(_Gp&& __gen)
{
if constexpr (__is_vector_type_v<_V>)
return __generate_vector_impl(
static_cast<_Gp&&>(__gen), make_index_sequence<_VVT::_S_full_size>());
else
return __generate_vector_impl(
static_cast<_Gp&&>(__gen),
make_index_sequence<_VVT::_S_partial_width>());
}
template
_GLIBCXX_SIMD_INTRINSIC constexpr __vector_type_t<_Tp, _Np>
__generate_vector(_Gp&& __gen)
{
return __generate_vector_impl<_Tp, _Np>(static_cast<_Gp&&>(__gen),
make_index_sequence<_Np>());
}
// }}}
// __xor{{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr _TW
__xor(_TW __a, _TW __b) noexcept
{
if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
{
using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW,
_VectorTraitsImpl<_TW>>::value_type;
if constexpr (is_floating_point_v<_Tp>)
{
using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a)
^ __vector_bitcast<_Ip>(__b));
}
else if constexpr (__is_vector_type_v<_TW>)
return __a ^ __b;
else
return __a._M_data ^ __b._M_data;
}
else
return __a ^ __b;
}
// }}}
// __or{{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr _TW
__or(_TW __a, _TW __b) noexcept
{
if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
{
using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW,
_VectorTraitsImpl<_TW>>::value_type;
if constexpr (is_floating_point_v<_Tp>)
{
using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a)
| __vector_bitcast<_Ip>(__b));
}
else if constexpr (__is_vector_type_v<_TW>)
return __a | __b;
else
return __a._M_data | __b._M_data;
}
else
return __a | __b;
}
// }}}
// __and{{{
template
_GLIBCXX_SIMD_INTRINSIC constexpr _TW
__and(_TW __a, _TW __b) noexcept
{
if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
{
using _Tp = typename conditional_t<__is_simd_wrapper_v<_TW>, _TW,
_VectorTraitsImpl<_TW>>::value_type;
if constexpr (is_floating_point_v<_Tp>)
{
using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
return __vector_bitcast<_Tp>(__vector_bitcast<_Ip>(__a)
& __vector_bitcast<_Ip>(__b));
}
else if constexpr (__is_vector_type_v<_TW>)
return __a & __b;
else
return __a._M_data & __b._M_data;
}
else
return __a & __b;
}
// }}}
// __andnot{{{
#if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__
static constexpr struct
{
_GLIBCXX_SIMD_INTRINSIC __v4sf
operator()(__v4sf __a, __v4sf __b) const noexcept
{ return __builtin_ia32_andnps(__a, __b); }
_GLIBCXX_SIMD_INTRINSIC __v2df
operator()(__v2df __a, __v2df __b) const noexcept
{ return __builtin_ia32_andnpd(__a, __b); }
_GLIBCXX_SIMD_INTRINSIC __v2di
operator()(__v2di __a, __v2di __b) const noexcept
{ return __builtin_ia32_pandn128(__a, __b); }
_GLIBCXX_SIMD_INTRINSIC __v8sf
operator()(__v8sf __a, __v8sf __b) const noexcept
{ return __builtin_ia32_andnps256(__a, __b); }
_GLIBCXX_SIMD_INTRINSIC __v4df
operator()(__v4df __a, __v4df __b) const noexcept
{ return __builtin_ia32_andnpd256(__a, __b); }
_GLIBCXX_SIMD_INTRINSIC __v4di
operator()(__v4di __a, __v4di __b) const noexcept
{
if constexpr (__have_avx2)
return __builtin_ia32_andnotsi256(__a, __b);
else
return reinterpret_cast<__v4di>(
__builtin_ia32_andnpd256(reinterpret_cast<__v4df>(__a),
reinterpret_cast<__v4df>(__b)));
}
_GLIBCXX_SIMD_INTRINSIC __v16sf
operator()(__v16sf __a, __v16sf __b) const noexcept
{
if constexpr (__have_avx512dq)
return _mm512_andnot_ps(__a, __b);
else
return reinterpret_cast<__v16sf>(
_mm512_andnot_si512(reinterpret_cast<__v8di>(__a),
reinterpret_cast<__v8di>(__b)));
}
_GLIBCXX_SIMD_INTRINSIC __v8df
operator()(__v8df __a, __v8df __b) const noexcept
{
if constexpr (__have_avx512dq)
return _mm512_andnot_pd(__a, __b);
else
return reinterpret_cast<__v8df>(
_mm512_andnot_si512(reinterpret_cast<__v8di>(__a),
reinterpret_cast<__v8di>(__b)));
}
_GLIBCXX_SIMD_INTRINSIC __v8di
operator()(__v8di __a, __v8di __b) const noexcept
{ return _mm512_andnot_si512(__a, __b); }
} _S_x86_andnot;
#endif // _GLIBCXX_SIMD_X86INTRIN && !__clang__
template
_GLIBCXX_SIMD_INTRINSIC constexpr _TW
__andnot(_TW __a, _TW __b) noexcept
{
if constexpr (__is_vector_type_v<_TW> || __is_simd_wrapper_v<_TW>)
{
using _TVT = conditional_t<__is_simd_wrapper_v<_TW>, _TW,
_VectorTraitsImpl<_TW>>;
using _Tp = typename _TVT::value_type;
#if _GLIBCXX_SIMD_X86INTRIN && !defined __clang__
if constexpr (sizeof(_TW) >= 16)
{
const auto __ai = __to_intrin(__a);
const auto __bi = __to_intrin(__b);
if (!__builtin_is_constant_evaluated()
&& !(__builtin_constant_p(__ai) && __builtin_constant_p(__bi)))
{
const auto __r = _S_x86_andnot(__ai, __bi);
if constexpr (is_convertible_v)
return __r;
else
return reinterpret_cast(__r);
}
}
#endif // _GLIBCXX_SIMD_X86INTRIN
using _Ip = make_unsigned_t<__int_for_sizeof_t<_Tp>>;
return __vector_bitcast<_Tp>(~__vector_bitcast<_Ip>(__a)
& __vector_bitcast<_Ip>(__b));
}
else
return ~__a & __b;
}
// }}}
// __not{{{
template >
_GLIBCXX_SIMD_INTRINSIC constexpr _Tp
__not(_Tp __a) noexcept
{
if constexpr (is_floating_point_v)
return reinterpret_cast(
~__vector_bitcast(__a));
else
return ~__a;
}
// }}}
// __concat{{{
template ,
typename _R = __vector_type_t>
constexpr _R
__concat(_Tp a_, _Tp b_)
{
#ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_1
using _W
= conditional_t, double,
conditional_t<(sizeof(_Tp) >= 2 * sizeof(long long)),
long long, typename _TVT::value_type>>;
constexpr int input_width = sizeof(_Tp) / sizeof(_W);
const auto __a = __vector_bitcast<_W>(a_);
const auto __b = __vector_bitcast<_W>(b_);
using _Up = __vector_type_t<_W, sizeof(_R) / sizeof(_W)>;
#else
constexpr int input_width = _TVT::_S_full_size;
const _Tp& __a = a_;
const _Tp& __b = b_;
using _Up = _R;
#endif
if constexpr (input_width == 2)
return reinterpret_cast<_R>(_Up{__a[0], __a[1], __b[0], __b[1]});
else if constexpr (input_width == 4)
return reinterpret_cast<_R>(
_Up{__a[0], __a[1], __a[2], __a[3], __b[0], __b[1], __b[2], __b[3]});
else if constexpr (input_width == 8)
return reinterpret_cast<_R>(
_Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6], __a[7],
__b[0], __b[1], __b[2], __b[3], __b[4], __b[5], __b[6], __b[7]});
else if constexpr (input_width == 16)
return reinterpret_cast<_R>(
_Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6],
__a[7], __a[8], __a[9], __a[10], __a[11], __a[12], __a[13],
__a[14], __a[15], __b[0], __b[1], __b[2], __b[3], __b[4],
__b[5], __b[6], __b[7], __b[8], __b[9], __b[10], __b[11],
__b[12], __b[13], __b[14], __b[15]});
else if constexpr (input_width == 32)
return reinterpret_cast<_R>(
_Up{__a[0], __a[1], __a[2], __a[3], __a[4], __a[5], __a[6],
__a[7], __a[8], __a[9], __a[10], __a[11], __a[12], __a[13],
__a[14], __a[15], __a[16], __a[17], __a[18], __a[19], __a[20],
__a[21], __a[22], __a[23], __a[24], __a[25], __a[26], __a[27],
__a[28], __a[29], __a[30], __a[31], __b[0], __b[1], __b[2],
__b[3], __b[4], __b[5], __b[6], __b[7], __b[8], __b[9],
__b[10], __b[11], __b[12], __b[13], __b[14], __b[15], __b[16],
__b[17], __b[18], __b[19], __b[20], __b[21], __b[22], __b[23],
__b[24], __b[25], __b[26], __b[27], __b[28], __b[29], __b[30],
__b[31]});
}
// }}}
// __zero_extend {{{
template >
struct _ZeroExtendProxy
{
using value_type = typename _TVT::value_type;
static constexpr size_t _Np = _TVT::_S_full_size;
const _Tp __x;
template ,
typename
= enable_if_t>>
_GLIBCXX_SIMD_INTRINSIC operator _To() const
{
constexpr size_t _ToN = _ToVT::_S_full_size;
if constexpr (_ToN == _Np)
return __x;
else if constexpr (_ToN == 2 * _Np)
{
#ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_3
if constexpr (__have_avx && _TVT::template _S_is)
return __vector_bitcast(
_mm256_insertf128_ps(__m256(), __x, 0));
else if constexpr (__have_avx && _TVT::template _S_is)
return __vector_bitcast(
_mm256_insertf128_pd(__m256d(), __x, 0));
else if constexpr (__have_avx2 && _Np * sizeof(value_type) == 16)
return __vector_bitcast(
_mm256_insertf128_si256(__m256i(), __to_intrin(__x), 0));
else if constexpr (__have_avx512f && _TVT::template _S_is)
{
if constexpr (__have_avx512dq)
return __vector_bitcast(
_mm512_insertf32x8(__m512(), __x, 0));
else
return reinterpret_cast<__m512>(
_mm512_insertf64x4(__m512d(),
reinterpret_cast<__m256d>(__x), 0));
}
else if constexpr (__have_avx512f
&& _TVT::template _S_is)
return __vector_bitcast(
_mm512_insertf64x4(__m512d(), __x, 0));
else if constexpr (__have_avx512f && _Np * sizeof(value_type) == 32)
return __vector_bitcast(
_mm512_inserti64x4(__m512i(), __to_intrin(__x), 0));
#endif
return __concat(__x, _Tp());
}
else if constexpr (_ToN == 4 * _Np)
{
#ifdef _GLIBCXX_SIMD_WORKAROUND_XXX_3
if constexpr (__have_avx512dq && _TVT::template _S_is)
{
return __vector_bitcast(
_mm512_insertf64x2(__m512d(), __x, 0));
}
else if constexpr (__have_avx512f
&& is_floating_point_v