std::simd::basic_vec<T,Abi>::basic_vec
constexpr basic_vec() noexcept = default;
|
(1) | (since C++26) |
template< class U >
constexpr basic_vec( U&& value ) noexcept;
|
(2) | (since C++26) |
template< class U, class UAbi >
constexpr explicit(/* see below */)
basic_vec( const basic_vec<U, UAbi>& other ) noexcept;
|
(3) | (since C++26) |
template< class G >
constexpr explicit basic_vec( G&& gen );
|
(4) | (since C++26) |
template< class R, class... Flags >
constexpr basic_vec( R&& r, simd::flags<Flags...> f = {} );
|
(5) | (since C++26) |
template< class R, class... Flags >
constexpr basic_vec( R&& r, const mask_type& mask,
simd::flags<Flags...> f = {} );
|
(6) | (since C++26) |
constexpr basic_vec( const /*real-type*/& reals,
const /*real-type*/& imags = {} ) noexcept;
|
(7) | (since C++26) |
Constructs a basic_vec.
basic_vec v; default-initializes the elements, which leaves elements of arithmetic type without a usable value, as for a local variable T x;, and basic_vec v{}; sets all elements to zero.value converted to T. This overload participates in overload resolution only if
valuehas an arithmetic type and its conversion toTis value-preserving,valueis a std::constant_wrapper of an arithmetic value that is representable inT, such asstd::cw<2>for aToffloat, orvaluehas a type that is neither arithmetic nor aconstant_wrapper, and that is implicitly convertible toT.
ith element with static_cast<T>(other[i]). This overload participates in overload resolution only if other has the same width as *this and U is explicitly convertible to T. It is explicit if the conversion from U to T is not value-preserving, or if it lowers the integer or floating-point conversion rank.ith element with gen(std::integral_constant</*simd-size-type*/, i>()) converted to T, where /*simd-size-type*/ is the signed integer type of size(). gen is called exactly once for each element, in order of increasing i. This overload participates in overload resolution only if each result is implicitly convertible to T, with a value-preserving conversion if it has an arithmetic type.ith element with std::ranges::data(r)[i] converted to T, or, for (6), with T() if mask[i] is false. These overloads participate in overload resolution only if R models contiguous_range and sized_range, std::ranges::size(r) is a constant expression equal to size(), and the element type of R is a vectorizable type that is explicitly convertible to T. If Flags does not contain the flag of simd::flag_convert, the conversion must be value-preserving, otherwise the program is ill-formed. If Flags contains the flag of simd::flag_aligned or simd::flag_overaligned<N>, std::ranges::data(r) must point to storage aligned to simd::alignment_v<basic_vec, std::ranges::range_value_t<R>> or to N, respectively, otherwise the behavior is undefined.ith element with T(reals[i], imags[i]). /*real-type*/ is the basic_vec of the same width whose element type is T::value_type. This overload participates in overload resolution only if T is a specialization of std::complex.In the descriptions above, a conversion is value-preserving if every value of the source type can be represented in the destination type: for example, short to int and float to double are value-preserving, while int to float and long to int are not.
The copy constructor is implicitly declared and trivial.
Parameters
| value | - | the value for all elements |
| other | - | the basic_vec to convert
|
| gen | - | a callable object that returns the value of each element from its index |
| r | - | a contiguous range with exactly size() elements
|
| mask | - | the elements to read from r
|
| f | - | load flags |
| reals | - | the real parts |
| imags | - | the imaginary parts |
Notes
The index that (4) passes to gen is a constant expression, so gen can use it as a template argument, for example [&](auto i) { return std::get<i>(tuple); }. A generator that takes an int parameter also works, because std::integral_constant converts to its value.
(5,6) require a range whose size is known at compile time, such as std::array or a std::span with a static extent. To load from a range whose size is known only at run time, such as std::vector, use std::simd::unchecked_load or std::simd::partial_load.
The constructors do not resolve ambiguity: if an argument type can be used with more than one of (2), (4), and (5), for example a range that is also convertible to T and callable with an index, the call is ambiguous, and the program must convert the argument to say which one it means.
The broadcast constructor (2) typically compiles to a broadcast instruction, such as vbroadcastss on x86 with AVX, and a generator constructor with constant results to a single load of a constant from memory. The range constructor (5) compiles to an unaligned vector load (vmovups on x86), or to an aligned load (vmovaps) with simd::flag_aligned, and a conversion between element types compiles to conversion instructions, such as vcvtdq2ps from int to float, which can be combined with the load. These examples are GCC's output for x86-64 with AVX2.
Example
#include <array>
#include <complex>
#include <print>
#include <simd>
#include <utility>
namespace simd = std::simd;
int main()
{
using V = simd::vec<float, 4>;
V z{}; // (1) all elements are zero
V b = 2.5f; // (2) broadcast
V c = std::cw<2>; // (2) a constant that fits in float
simd::vec<double, 4> d = b; // (3) implicit: float to double
auto n = simd::vec<int, 4>(b); // (3) explicit: float to int
V e([](int i) { return i * 0.5f; }); // (4) generator: element i is i / 2
std::array<float, 4> arr{1, 2, 3, 4};
V f(arr); // (5) from a range of 4 floats
std::array<int, 4> ints{10, 20, 30, 40};
V g(ints, simd::flag_convert); // (5) int to float needs flag_convert
simd::vec<std::complex<float>, 4> h(f, e); // (7) real and imaginary parts
std::println("z = {}\nb = {}\nc = {}\nd = {}\nn = {}", z, b, c, d, n);
std::println("e = {}\nf = {}\ng = {}", e, f, g);
std::println("h.real() = {}, h.imag() = {}", h.real(), h.imag());
}
Output:
z = [0, 0, 0, 0]
b = [2.5, 2.5, 2.5, 2.5]
c = [2, 2, 2, 2]
d = [2.5, 2.5, 2.5, 2.5]
n = [2, 2, 2, 2]
e = [0, 0.5, 1, 1.5]
f = [1, 2, 3, 4]
g = [10, 20, 30, 40]
h.real() = [1, 2, 3, 4], h.imag() = [0, 0.5, 1, 1.5]
See also
(C++26)(C++26) |
loads elements from a contiguous range to basic_vec (function template) |
(C++29) |
a basic_vec holding 0, 1, 2, ..., or zero for arithmetic types (constant) |