24#include <xvec/detail/config.hpp>
25#include <xvec/detail/utilities.hpp>
27namespace _XVEC_NAMESPACE::simd {
46template<
class _Tp,
class _Abi = simd_native_abi<_Tp>>
inline constexpr simd_size_type simd_size_v = _Abi::num_elements;
54 template <
typename>
inline constexpr bool is_vec_v =
false;
55 template <
typename _Tp,
typename _Abi>
inline constexpr bool is_vec_v<basic_vec<_Tp, _Abi>> =
true;
57 template <
typename>
inline constexpr bool is_mask_v =
false;
58 template <std::
size_t _Bytes,
typename _Abi>
inline constexpr bool is_mask_v<basic_mask<_Bytes, _Abi>> =
true;
61template<
typename _Vp>
concept vec_type = detail::is_vec_v<std::remove_cvref_t<_Vp>>;
62template<
typename _Mp>
concept mask_type = detail::is_mask_v<std::remove_cvref_t<_Mp>>;
75template<
typename _Vp,
typename _Tp>
concept vec_of =
vec_type<_Vp> && std::same_as<typename _Vp::value_type, _Tp>;
91 std::is_arithmetic_v<_Tp> ||
96template<
class _Tp,
class _Op>
98 requires(_Tp a) { _Op{}(a); } &&
101template<
class _Tp,
class _Op>
103 requires(_Tp a, _Tp b) { _Op{}(a, b); } &&
106template<
class _Tp,
class _Op>
112template<
typename _From,
typename _To>
114 using BareFrom = std::remove_cvref_t<_From>;
115 using BareTo = std::remove_cvref_t<_To>;
117 if constexpr (std::same_as<BareFrom, BareTo>)
121 using ToComponent =
typename BareTo::value_type;
127 else if constexpr (std::integral<BareFrom> && std::integral<BareTo>)
129 if constexpr (std::is_signed_v<BareFrom> == std::is_signed_v<BareTo>)
130 return (
sizeof(BareFrom) <=
sizeof(BareTo));
131 else if constexpr (std::is_unsigned_v<BareFrom> && std::is_signed_v<BareTo>)
132 return (
sizeof(BareFrom) <
sizeof(BareTo));
136 else if constexpr (std::integral<BareFrom> && is_floating_point_v<BareTo>)
137 return std::numeric_limits<BareTo>::digits >= std::numeric_limits<BareFrom>::digits;
138 else if constexpr (is_floating_point_v<BareFrom> && is_floating_point_v<BareTo>)
139 return (std::numeric_limits<BareTo>::digits >= std::numeric_limits<BareFrom>::digits &&
140 std::numeric_limits<BareTo>::max_exponent >= std::numeric_limits<BareFrom>::max_exponent &&
141 std::numeric_limits<BareTo>::lowest() <= std::numeric_limits<BareFrom>::lowest() &&
142 std::numeric_limits<BareTo>::max() >= std::numeric_limits<BareFrom>::max());
147template<
typename _From,
typename _To>
155 static_cast<_To
>(std::declval<_From>());
158template <
typename _From,
typename _To>
160 && std::same_as<std::common_type_t<_From, _To>, _From>
161 && !std::same_as<_From, _To>;
166template <
typename _From,
typename _To>
170template<
typename _Range>
concept contiguous_sized_range = std::ranges::contiguous_range<_Range> && std::ranges::sized_range<_Range>;
173template<
typename _Tp>
176 ((std::integral<_Tp> && !std::same_as<bool, _Tp>) ||
177 is_floating_point_v<_Tp> ||
178 std::same_as<std::byte, _Tp>);
182template<
typename _Tp>
188template<
typename _Tp>
196template<
typename _Tp>
198 std::is_pointer_v<_Tp> ||
199 std::is_member_pointer_v<_Tp> ||
200 std::is_union_v<_Tp> ||
201 std::is_const_v<_Tp> ||
202 std::is_volatile_v<_Tp> ||
203 std::is_empty_v<_Tp> ||
204 std::same_as<_Tp, long double> ||
209template<
typename _Tp>
211 std::is_trivially_copyable_v<_Tp> &&
212 (
sizeof(_Tp) == 1 ||
sizeof(_Tp) == 2 ||
sizeof(_Tp) == 4 ||
213 sizeof(_Tp) == 8 ||
sizeof(_Tp) == 16) &&
216template<
typename _Tp>
227template<
typename _From,
typename _To>
234 return !std::is_convertible_v<_From, _To>;
237template<
typename _From,
typename _To>
245 requires { T::value; } &&
247 requires std::convertible_to<T, std::remove_cvref_t<
decltype(T::value)>>;
248 requires std::equality_comparable_with<T, std::remove_cvref_t<
decltype(T::value)>>;
249 requires std::bool_constant<(T{} == T::value)>::value;
250 requires std::bool_constant<
251 (
static_cast<std::remove_cvref_t<decltype(T::value)
>>(T{}) == T::value)
255template<
class From,
class To>
257 std::is_convertible_v<From, To> &&
258 std::is_convertible_v<To, From> &&
260 {
static_cast<From
>(
static_cast<To
>(x)) == x } -> std::convertible_to<bool>;
263template<
class To,
class From>
265constexpr bool roundtrip_preserves_value(From x) {
return static_cast<From
>(
static_cast<To
>(x)) == x; }
267template<
class Wrapper,
class To>
268inline constexpr bool wrapped_value_is_representable_by_v = []{
269 using From = std::remove_cvref_t<
decltype(Wrapper::value)>;
271 if constexpr (std::is_integral_v<From> && std::is_integral_v<To>) {
272 return std::in_range<To>(Wrapper::value);
273 }
else if constexpr (roundtrippable<From, To>) {
274 return roundtrip_preserves_value<To>(Wrapper::value);
280template<
class Wrapper,
class To>
283template<
class From,
class To>
285 std::convertible_to<From, To> &&
296 is_arithmetic_like_v<std::remove_cvref_t<
decltype(From::value)>> &&
305template<
typename _Tp>
concept vec_container_element = std::unsigned_integral<_Tp> || std::same_as<unsigned __int128, _Tp>;
310template<
typename _From,
typename _To>
312 std::convertible_to<_From, _To> &&
313 (!is_arithmetic_like_v<_From> ||
316template<
typename _Gen,
typename _Tp, simd_
size_type _Idx>
318 requires(_Gen __gen) {
319 { __gen(std::integral_constant<simd_size_type, _Idx>{}) }
323template<
typename _Gen,
typename _Tp,
int _Size>
325 !std::constructible_from<_Tp, _Gen> &&
326 []<simd_size_type... _Idx>(std::integer_sequence<simd_size_type, _Idx... >) {
329 for (
bool check : checks)
if (!check)
return false;
331 }(std::make_integer_sequence<simd_size_type, _Size>{});
336 std::less<>, std::greater<>, std::less_equal<>, std::greater_equal<>,
337 std::equal_to<>, std::not_equal_to<>,
338 std::logical_and<>, std::logical_or<>, std::logical_not<>>;
341template <
class _Tp>
using deduced_vec_t =
decltype(std::declval<const _Tp&>() + std::declval<const _Tp&>());
342template <
class _Tp>
using deduced_mask_t =
typename deduced_vec_t<_Tp>::mask_type;
356template<
class _Tp,
class _Vp>
struct rebind {};
360template<
class _Tp, std::
size_t _Bytes,
class _Abi>
struct rebind<_Tp,
basic_mask<_Bytes, _Abi>> {
366template<simd_
size_type _Np,
class _Vp>
struct resize { };
367template<simd_
size_type _Np,
class _Tp,
class _Abi>
struct resize<_Np,
basic_vec<_Tp, _Abi>> {
370template<simd_
size_type _Np, std::
size_t _Bytes,
class _Abi>
struct resize<_Np,
basic_mask<_Bytes, _Abi>> {
393template <
class... _Flags>
struct flags {
394 template <
class... _Others>
397 return flags<_Flags..., _Others...>();
411 constexpr auto add_convert_flag(
auto f) {
return f | flags<flag_convert>{}; };
414 template<
typename _F,
typename... _Flags>
423inline constexpr flags<detail::flag_convert> flag_convert{};
424inline constexpr flags<detail::flag_aligned> flag_aligned{};
426template<std::
size_t _N>
requires (std::has_single_bit(_N))
inline constexpr flags<detail::flag_overaligned<_N>> flag_overaligned{};
434template<
typename _Tp>
437 constexpr explicit convert_to_t()
noexcept =
default;
449template<
typename _Vendor,
typename _Tp, simd_
size_type _Np>
452 static_assert(
isLlvm || _Np <= 256,
"gcc has limits on size");
465 using element_container_type = container_for_type<_Tp>;
489 constexpr auto _regSize = register_size<_Tp, _Np>;
490 static_assert(_Np <= _regSize,
"Converting to a smaller register loses data");
492 auto r = [=]<
int... _Idx>(std::integer_sequence<int, _Idx...>) {
493 return __builtin_shufflevector(v, v, (_Idx < _Np ? _Idx : -1)...);
494 }(std::make_integer_sequence<int, _regSize>());
503 constexpr auto _regSize = register_size<_Tp, _Np>;
504 static_assert(_Np <= _regSize,
"Converting from a smaller register creates undefined values");
505 if constexpr (_Np == _regSize)
506 return builtin_type(r);
510 using FullRegBuiltin =
typename vec<element_container_type, _regSize>::builtin_type;
511 auto b = FullRegBuiltin(r);
514 return builtin_type([=]<std::size_t... _Idx>(std::index_sequence<_Idx...>) {
515 return __builtin_shufflevector(b, b, _Idx...);
516 }(std::make_index_sequence<_numStorageElements>()));
524template<
typename _Vendor, std::
size_t _Bytes, simd_
size_type _Np>
527 using element_container_type = detail::container_for_num_bytes<_Bytes>;
528 using element_signed_container_type = detail::make_signed_t<element_container_type>;
536 static constexpr int internalNp = std::bit_ceil(std::max<unsigned>(8, _Np));
538#if defined (__llvm__)
541 #if defined(__INTEL_LLVM_COMPILER)
542 #if __INTEL_LLVM_COMPILER > 20220000
543 using compact_builtin_type =
unsigned _BitInt(internalNp);
545 using compact_builtin_type =
unsigned _ExtInt(internalNp);
547 #elif defined(__clang_major__)
548 #if __clang_major__ >= 14
549 using compact_builtin_type =
unsigned _BitInt(internalNp);
551 using compact_builtin_type =
unsigned _ExtInt(internalNp);
554 #error "Unknown LLVM-based compiler: choose a bit-precise integer type"
557 static constexpr auto all_bits_set = (_Np == internalNp) ? ~compact_builtin_type() : ((compact_builtin_type(1) << _Np) - 1);
560 using compact_builtin_type = __uint128_t;
561 static constexpr auto all_bits_set = (_Np == 128) ? ~compact_builtin_type() : ((compact_builtin_type(1) << _Np) - 1);
567 static constexpr builtin_type truncate_to_size(builtin_type m) {
572 static constexpr auto to_register(
const builtin_type& m)
noexcept {
584template<
typename _To,
typename _From>
585constexpr auto simd_bit_cast(
const _From& v)
noexcept {
return std::bit_cast<_To>(v); }
589template<detail::vectorizable _To, vec_type _From>
591auto simd_bit_cast(
const _From& v)
noexcept
598 static_assert((_From::size() *
sizeof(
typename _From::value_type)) %
sizeof(_To) == 0);
602 constexpr int newNumElements = _From::size() *
sizeof(
typename _From::value_type) /
sizeof(_To);
603 using new_type = vec<_To, newNumElements>;
604 if constexpr (detail::isLlvm)
607 return new_type{v.to_builtin()};
610 return new_type{__builtin_bit_cast(
typename new_type::builtin_type, v.to_builtin())};
A mask containing boolean elements.
Definition mask.hpp:121
Vector of elements sharing an element type and ABI.
Definition simd:161
Detect whether a given type is std::complex.
Definition utilities.hpp:53
Check for contiguous range with a finite range.
Definition core.hpp:170
Detect a fundamental unsigned integer type.
Definition utilities.hpp:62
Detect a simple vectorizable type.
Definition core.hpp:174
Concepts used to simplify some function signature requirements.
Definition core.hpp:305
Definition target_config.hpp:175
Check that two simdable values (mask, simds, or a mixture) have the same number of elements.
Definition core.hpp:78
Detect a vec_type with arbitrary length whose elements are of a given type.
Definition core.hpp:75
Check that the two simdable values have the same type/size of elements.
Definition core.hpp:81
constexpr flags flag_default
Standard flags.
Definition core.hpp:422
basic_mask< sizeof(_Tp), simd_fixed_size_abi< _Np > > mask
Definition core.hpp:383
basic_vec< _Tp, simd_fixed_size_abi< _Np > > vec
Definition core.hpp:379
constexpr bool disable_vectorization
Definition core.hpp:197
constexpr auto simd_bit_cast(const _From &v) noexcept
A simd-generic version of simd_bit_cast which directly invokes std::bit_cast.
Definition core.hpp:585
Detect a scoped enum. This will be provided in C++23 onwards.
Definition compat.hpp:27
constexpr bool any_same_v
Definition utilities.hpp:58
consteval auto get_register_type()
Definition target_config.hpp:157
constexpr bool value_preserving_convertible_to_v
Definition core.hpp:113
constexpr bool contains_flag(flags< _Flags... >)
Query whether the given flags contains the specified type.
Definition core.hpp:415
constexpr bool needs_explicit_conversion_v
Definition core.hpp:228
constexpr bool isLlvm
Compile time flag to indicate whether the LLVM compiler is being used.
Definition utilities.hpp:41
constexpr int maxBytesInVec
Definition target_config.hpp:148
Conversion flag, used to pass destination type to a simd_convert customisation point.
Definition core.hpp:435
decltype(detail::get_register_type< builtin_type >()) register_type
The register to use for intrinsic operations on this vec (e.g., for vec<double, 3> it would be __m256...
Definition core.hpp:474
element_storage_t< _Tp > element_type
Definition core.hpp:464
static constexpr simd_size_type _numDataElements
Definition core.hpp:455
static constexpr simd_size_type _numStorageElements
Definition core.hpp:458
static constexpr builtin_type from_register(register_type r)
A named constructor for creating a builtin type from a register value.
Definition core.hpp:502
static constexpr register_type to_register(builtin_type v)
Definition core.hpp:488
Given a simd type (vec or mask), create a new type which has the same size but a different element.
Definition core.hpp:356
Given a vec or mask type, create a new type which has the same element type but a different size.
Definition core.hpp:366
An implementation ABI type which is used to specify a simd::vec with a fixed number of elements.
Definition core.hpp:34
static constexpr simd_size_type num_elements
Definition core.hpp:35
Query the number of bytes in an element of a simd::mask.
Definition core.hpp:49