std::simd::basic_vec, std::simd::vec
| Defined in header <simd>
|
||
template< class T, class Abi = /*native-abi*/<T> >
class basic_vec;
|
(1) | (since C++26) |
template< class T, /*simd-size-type*/ N = /*simd-size-v*/<T, /*native-abi*/<T>> >
using vec = basic_vec<T, /*deduce-abi-t*/<T, N>>;
|
(2) | (since C++26) |
std::simd::basic_vec is a data-parallel vector: a fixed number of elements of type T, its width, that are stored and processed together, typically in SIMD registers. Operators and most functions apply to each element. The ABI tag Abi determines the width and the representation. The default, native-abi<T>, is the most efficient one for T on the target, and its width depends on the compiler options that select the instruction set.std::simd::vec<T, N> is a basic_vec with exactly N elements. N defaults to the width of basic_vec<T>, so vec<T> has the same width as basic_vec<T>. The type of N is a signed integer type, the same type as the value of size().Every specialization of basic_vec is a complete type. The specialization is
- enabled, if
Tis a vectorizable type andAbiis the ABI tag ofvec<T, N>for someNfrom1to64, - otherwise disabled, if
Tis not a vectorizable type, - otherwise, it is implementation-defined whether the specialization is enabled (for example,
vec<T, N>withNgreater than64).
The vectorizable types are the standard integer types, the character types, float, double, the extended floating-point types std::float16_t, std::float32_t, and std::float64_t if they are defined, and std::complex<T> where T is one of these floating-point types. bool, long double, and std::bfloat16_t are not vectorizable.
A disabled specialization has a deleted default constructor, destructor, copy constructor, and copy assignment operator, and it has no members other than the member types value_type, mask_type, and abi_type.
An enabled specialization basic_vec<T, Abi>
- is TriviallyCopyable,
- default-initializes all elements when it is default-initialized, and value-initializes them (sets them to zero) when it is value-initialized, and
- has a
mask_typethat is an enabled std::simd::basic_mask specialization of the same width.
Template parameters
| T | - | element type; a vectorizable type |
| Abi | - | ABI tag that determines the width and the representation |
| N | - | the width: the number of elements |
Member types
| Type | Definition |
value_type
|
T
|
mask_type
|
simd::basic_mask<sizeof(T), Abi>
|
abi_type
|
Abi
|
iterator
|
simd-iterator<basic_vec>
|
const_iterator
|
simd-iterator<const basic_vec>
|
Member constant
| Name | Description |
size [static] |
the width of basic_vec: a std::integral_constant of a signed integer type, so both size and size() are constant expressions (public static member constant) |
Member functions
constructs a basic_vec object (public member function) | |
| accesses specified element, or permutes elements (public member function) | |
| returns an iterator to the beginning (public member function) | |
| returns a sentinel indicating the end (public member function) | |
| accesses the real or imaginary parts of complex elements (public member function) | |
| element-wise increment and decrement (public member function) | |
| element-wise unary operators (public member function) |
Non-member functions
| element-wise binary operators (function) | |
| element-wise compound assignment (function) | |
element-wise comparison, returning a basic_mask (function) |
(C++26) |
element-wise selection using conditional operator (function template) |
Deduction guides
Notes
A basic_vec is a value made of elements, not a container of element objects. operator[] and the iterators return copies, and a single element cannot be modified in place: v[0] = 1 does not compile. To change some elements, build a new value, for example with the generator constructor or with std::simd::select.
The comparison operators return a basic_mask, so basic_vec does not model regular, and a comparison cannot be used directly as a condition. Use std::simd::all_of and the related functions, as in if (std::simd::all_of(a == b)).
Arithmetic does not apply the integer promotions or the usual arithmetic conversions: vec<std::int8_t> + vec<std::int8_t> is a vec<std::int8_t>, and operands of different element types must be converted explicitly. A scalar operand is broadcast to all elements only if its conversion to T is value-preserving, or if it is a std::constant_wrapper whose value is representable in T: for a vec<float> x, x * 2.5 and x * 2 do not compile, while x * 2.5f and x * std::cw<2> do. A conversion between basic_vec types of the same width is implicit if it is value-preserving and does not lower the conversion rank of the element type, and explicit otherwise; types of different widths are not convertible.
Implementations are recommended to provide implicit conversions between basic_vec and the non-standard vector types of the platform, so that basic_vec objects can be passed directly to SIMD intrinsics. GCC provides them; for example, vec<int, 4> converts to and from __m128i on x86.
The width of vec<T> depends on the compiler options that select the instruction set; see ABI tags.
Most operators compile to single SIMD instructions on the elements of one register. For example, GCC with -march=x86-64-v3 (AVX2) compiles + on vec<float, 8> to vaddps, * on vec<int, 8> to vpmulld, << with a vector of shift counts to vpsllvd, and < on vec<float, 8> to vcmpltps. Operations without a matching instruction are emulated: x86 has no SIMD integer division, so / on vec<int, 8> converts the elements to double and uses vdivpd, and * on 8-bit integers is computed with 16-bit multiplications.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_simd |
202411L |
(C++26) | std::simd::basic_vec
|
__cpp_lib_simd_complex |
202502L |
(C++26) | std::complex elements, real and imag
|
Example
#include <array>
#include <print>
#include <simd>
namespace simd = std::simd;
int main()
{
using V = simd::vec<float, 8>;
V a = 1.5f; // every element is 1.5
V b([](int i) { return float(i); }); // element i is i
std::array<float, 8> data{8, 7, 6, 5, 4, 3, 2, 1};
simd::basic_vec c(data); // deduced as vec<float, 8>
V d = a * b + c; // element-wise arithmetic
std::println("d = {}", d);
auto big = d > 10.f; // comparison gives a mask
std::println("d > 10: {}", big);
std::println("clamped: {}", simd::select(big, V(10.f), d));
std::println("sum: {}, max: {}", simd::reduce(d), simd::reduce_max(d));
// the elements cannot be assigned to: build a new value instead
d = simd::select(b == 2.f, V(-1.f), d);
std::println("d[2] = {}", d[2]);
}
Output:
d = [8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5]
d > 10: [false, false, false, false, false, true, true, true]
clamped: [8, 8.5, 9, 9.5, 10, 10, 10, 10]
sum: 78, max: 11.5
d[2] = -1
See also
(C++26)(C++26) |
data-parallel type with the element type bool, and an alias template for it that specifies the width (class template) |
(C++26)(C++26) |
ABI tags: types that select the width and representation of data-parallel types (exposition-only alias templates*) |
| numeric arrays, array masks and array slices (class template) |