Namespaces
Variants

Data-parallel types (SIMD) (since C++26)

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 

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

data-parallel vector type, and an alias template for it that specifies the width
(class template) [edit]
data-parallel type with the element type bool, and an alias template for it that specifies the width
(class template) [edit]

Load and store operations

loads elements from a contiguous range to basic_vec
(function template) [edit]
stores elements from basic_vec to a contiguous range
(function template) [edit]
load and store flags for data-parallel types
(class template) [edit]

Casts

splits single data-parallel object to multiple ones
(function template) [edit]
(C++26)
concatenates multiple data-parallel objects into a single one
(function template) [edit]
a basic_vec holding 0, 1, 2, ..., or zero for arithmetic types
(constant) [edit]

Algorithms

(C++26)(C++26)(C++26)
element-wise min/max operations for basic_vec
(function template) [edit]
element-wise clamp operation for basic_vec
(function template) [edit]
element-wise selection using conditional operator
(function template) [edit]

Reductions

reduces all values in basic_vec over a specified binary operation to a single value
(function template) [edit]
reductions of basic_mask to bool
(function template) [edit]
reduction of basic_mask to number of true values
(function template) [edit]
reductions of basic_mask to the index of first or last true value
(function template) [edit]

Permutations

special values for static permute
(constant) [edit]
static and dynamic permutations of basic_vec and basic_mask
(function template) [edit]
compressing / expanding permutations of basic_vec and basic_mask according to a given basic_mask
(function template) [edit]
permutations of basic_vec from/to memory under optional write-mask
(function template) [edit]

Traits

obtains an appropriate alignment for simd::flag_aligned
(class template) [edit]
changes element type of the data-parallel type
(class template) [edit]
changes the width of the data-parallel type
(class template) [edit]

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:

Basic operations abs, fabs, fmod, remainder, remquo, fma, fmax, fmin, fdim, lerp
Carry-less product clmul (C++29)
Exponential functions exp, exp2, expm1, log, log10, log2, log1p
Power functions pow, sqrt, cbrt, hypot
Trigonometric functions sin, cos, tan, asin, acos, atan, atan2
Hyperbolic functions sinh, cosh, tanh, asinh, acosh, atanh
Error and gamma functions erf, erfc, tgamma, lgamma
Nearest integer operations ceil, floor, trunc, round, lround, llround, nearbyint, rint, lrint, llrint
Floating-point manipulation frexp, ldexp, modf, scalbn, scalbln, ilogb, logb, nextafter, copysign
Classification and comparison fpclassify, isfinite, isinf, isnan, isnormal, signbit, isgreater, isgreaterequal, isless, islessequal, islessgreater, isunordered
Special functions assoc_laguerre, assoc_legendre, beta, comp_ellint_1, comp_ellint_2, comp_ellint_3, cyl_bessel_i, cyl_bessel_j, cyl_bessel_k, cyl_neumann, ellint_1, ellint_2, ellint_3, expint, hermite, laguerre, legendre, riemann_zeta, sph_bessel, sph_legendre, sph_neumann
Complex numbers real, imag, abs, arg, norm, conj, proj, polar, exp, log, log10, pow, sqrt, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh

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

ABI tags: types that select the width and representation of data-parallel types
(exposition-only alias templates*)[edit]
iterator type of basic_vec and basic_mask
(exposition-only class template*)[edit]

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) [edit]
numeric arrays, array masks and array slices
(class template) [edit]