Data-parallel types (SIMD) (since C++26)
Data-parallel types let a program compute on several values at once with the SIMD registers and instructions of the target processor, without processor-specific intrinsics.
An object of type std::simd::vec<float> holds as many float values as fit in a SIMD register of the target (for example 4 with SSE, 8 with AVX, 16 with AVX-512), and an expression such as a * xv + yv on such objects computes all of them together:
// y = a * x + y, where x.size() is a multiple of the width
void axpy(float a, std::span<const float> x, std::span<float> y)
{
using V = std::simd::vec<float>;
for (std::size_t i = 0; i < x.size(); i += V::size())
{
V xv = std::simd::unchecked_load<V>(x.subspan(i));
V yv = std::simd::unchecked_load<V>(y.subspan(i));
std::simd::unchecked_store(a * xv + yv, y.subspan(i));
}
}
The same source adapts to the width of each target it is compiled for, and it vectorizes whether or not the compiler's optimizer would have vectorized the equivalent scalar loop. Algorithms can also be written once as templates that work with both data-parallel types and plain arithmetic types.
Defined in header
<simd> | |
Defined in namespace
std::simd |
Main classes
(C++26)(C++26) |
data-parallel vector type, and an alias template for it that specifies the width (class template) |
(C++26)(C++26) |
data-parallel type with the element type bool, and an alias template for it that specifies the width (class template) |
Load and store operations
(C++26)(C++26) |
loads elements from a contiguous range to basic_vec (function template) |
(C++26)(C++26) |
stores elements from basic_vec to a contiguous range (function template) |
(C++26) |
load and store flags for data-parallel types (class template) |
Casts
(C++26) |
splits single data-parallel object to multiple ones (function template) |
(C++26) |
concatenates multiple data-parallel objects into a single one (function template) |
(C++29) |
a basic_vec holding 0, 1, 2, ..., or zero for arithmetic types (constant) |
Algorithms
(C++26)(C++26)(C++26) |
element-wise min/max operations for basic_vec (function template) |
(C++26) |
element-wise clamp operation for basic_vec (function template) |
(C++26) |
element-wise selection using conditional operator (function template) |
Reductions
(C++26)(C++26)(C++26) |
reduces all values in basic_vec over a specified binary operation to a single value (function template) |
(C++26)(C++26)(C++26) |
reductions of basic_mask to bool (function template) |
(C++26) |
reduction of basic_mask to number of true values (function template) |
(C++26)(C++26) |
reductions of basic_mask to the index of first or last true value (function template) |
Permutations
(C++26)(C++26) |
special values for static permute (constant) |
(C++26) |
static and dynamic permutations of basic_vec and basic_mask (function template) |
(C++26)(C++26) |
compressing / expanding permutations of basic_vec and basic_mask according to a given basic_mask (function template) |
(C++26)(C++26)(C++26)(C++26) |
permutations of basic_vec from/to memory under optional write-mask (function template) |
Traits
(C++26) |
obtains an appropriate alignment for simd::flag_aligned (class template) |
(C++26) |
changes element type of the data-parallel type (class template) |
(C++26) |
changes the width of the data-parallel type (class template) |
Math functions
The functions from <cmath> are overloaded for basic_vec of floating-point types, and the functions from <complex> for basic_vec of std::complex. abs is also overloaded for basic_vec of signed integer types, and std::clmul for basic_vec of unsigned integer types. The functions apply to each element:
Bit manipulation functions
All bit manipulation functions in <bit> except for std::bit_cast are overloaded for basic_vec of integer types, and apply to each element:
| Integral powers of 2 | has_single_bit, bit_ceil, bit_floor, bit_width |
|---|---|
| Shifting and rotating | shl (C++29), shr (C++29), rotl, rotr |
| Counting | countl_zero, countl_one, countr_zero, countr_one, popcount |
| Byte and bit permutations | byteswap, bit_reverse (C++29), bit_repeat (C++29), bit_compress (C++29), bit_expand (C++29) |
Helper items
(C++26)(C++26) |
ABI tags: types that select the width and representation of data-parallel types (exposition-only alias templates*) |
(C++26) |
iterator type of basic_vec and basic_mask(exposition-only class template*) |
Notes
All operations on basic_vec and basic_mask are constexpr, so they can be used in constant expressions, except the math functions nearbyint, rint, lrint, llrint, and the special math functions.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_simd |
202411L |
(C++26) | Data-parallel types and operations |
202502L |
(C++26) | Bit manipulation functions for basic_vec
| |
202506L |
(C++26) | More constructors and accessors: basic_mask from and to std::bitset and unsigned integers
| |
202511L |
(C++26) | Deduction guide from basic_mask to basic_vec
| |
202603L |
(C++26) | Reductions also accept scalars, for SIMD-generic code | |
202606L |
(C++29) | simd::iota
| |
__cpp_lib_simd_bitops |
202606L |
(C++29) | Overloads of std::shl and std::shr for basic_vec
|
202607L |
(C++29) | Overloads of std::bit_reverse, std::bit_repeat, std::bit_compress, and std::bit_expand for basic_vec
| |
__cpp_lib_simd_clmul |
202611L |
(C++29) | Carry-less product: overloads of std::clmul for basic_vec
|
__cpp_lib_simd_complex |
202502L |
(C++26) | Interleaved complex values |
__cpp_lib_simd_permutations |
202506L |
(C++26) | SIMD permutations |
Example
#include <numeric>
#include <print>
#include <simd>
#include <span>
#include <vector>
namespace simd = std::simd;
// y = a * x + y, where x.size() is a multiple of the width
void axpy(float a, std::span<const float> x, std::span<float> y)
{
using V = simd::vec<float>;
for (std::size_t i = 0; i < x.size(); i += V::size())
{
V xv = simd::unchecked_load<V>(x.subspan(i));
V yv = simd::unchecked_load<V>(y.subspan(i));
simd::unchecked_store(a * xv + yv, y.subspan(i));
}
}
template<class A>
constexpr simd::basic_vec<int, A> my_abs(simd::basic_vec<int, A> x)
{
return simd::select(x < 0, -x, x);
}
int main()
{
std::vector<float> x(16), y(16, 0.5f);
std::iota(x.begin(), x.end(), 0.f);
axpy(2, x, y);
std::println("axpy: {}", y);
constexpr simd::vec<int> a = 1;
std::println("a: {}", a);
constexpr simd::vec<int> b([](int i) { return i - 2; });
std::println("b: {}", b);
constexpr auto c = a + b;
std::println("c: {}", c);
constexpr auto d = my_abs(c);
std::println("d: {}", d);
constexpr auto e = d * d;
std::println("e: {}", e);
constexpr auto inner_product = simd::reduce(e);
std::println("inner product: {}", inner_product);
constexpr simd::vec<double, 16> v([](int i) { return i; });
std::println("v: {}", v);
std::println("reduce_max(v * v - 10. * v): {}", simd::reduce_max(v * v - 10. * v));
}
Possible output:
axpy: [0.5, 2.5, 4.5, 6.5, 8.5, 10.5, 12.5, 14.5, 16.5, 18.5, 20.5, 22.5, 24.5, 26.5, 28.5, 30.5]
a: [1, 1, 1, 1]
b: [-2, -1, 0, 1]
c: [-1, 0, 1, 2]
d: [1, 0, 1, 2]
e: [1, 0, 1, 4]
inner product: 6
v: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
reduce_max(v * v - 10. * v): 75
See also
(parallelism TS v2) |
data-parallel vector type (class template) |
| numeric arrays, array masks and array slices (class template) |