Namespaces
Variants

std::simd::chunk

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
Defined in header <simd>
template< class T, class Abi >
constexpr auto chunk( const basic_vec<typename T::value_type, Abi>& x ) noexcept;
(1) (since C++26)
template< class T, class Abi >
constexpr auto chunk( const basic_mask</*mask-element-size*/<T>, Abi>& x ) noexcept;
(2) (since C++26)
template< /*simd-size-type*/ N, class T, class Abi >
constexpr auto chunk( const basic_vec<T, Abi>& x ) noexcept;
(3) (since C++26)
template< /*simd-size-type*/ N, std::size_t Bytes, class Abi >
constexpr auto chunk( const basic_mask<Bytes, Abi>& x ) noexcept;
(4) (since C++26)

Splits a basic_vec or a basic_mask into consecutive chunks of equal size, and one smaller chunk with the remaining elements if the size of x is not a multiple of the size of the chunks.

1,2) Splits x into objects of type T. T is a basic_vec with the element type of x for (1), and a basic_mask with the element size Bytes of x for (2).
These overloads participate in overload resolution only if T is an enabled specialization of basic_vec for (1), or of basic_mask for (2), and the remaining elements, if any, fit in a simd::resize_t<x.size() % T::size(), T>.
3,4) Splits x into chunks of N elements. Same as chunk<simd::resize_t<N, std::remove_cvref_t<decltype(x)>>>(x).

mask-element-size<T> is the Bytes template argument of the basic_mask T, and simd-size-type is the signed integer type of size().

Parameters

x - the basic_vec or basic_mask to split

Return value

With T being the type of the chunks and n = x.size() / T::size():

  • If x.size() is a multiple of T::size(), a std::array<T, n>.
  • Otherwise, a std::tuple of n objects of type T followed by one object of type simd::resize_t<x.size() % T::size(), T>.

Element i of the chunk at position j is the element x[i + j * T::size()].

Notes

A basic_vec whose size is larger than a hardware register can be split into chunks that each fit one register, to pass them to platform-specific intrinsics or to functions written for a smaller size. simd::chunk<simd::vec<float>>(x) splits x into objects of the native size of the target, so for a simd::vec<float, 20> it returns a std::tuple<simd::vec<float>, simd::vec<float, 4>> with AVX-512 (16 elements per register), a std::tuple<simd::vec<float>, simd::vec<float>, simd::vec<float, 4>> with AVX (8 elements), and a std::array<simd::vec<float>, 5> with SSE (4 elements).

Both std::array and std::tuple work with structured bindings and std::apply, so code that uses those works whether the size of x is a multiple of the size of the chunks or not.

If T::size() is larger than x.size(), the result is a std::tuple of a single object that holds all elements of x.

simd::cat reverses simd::chunk: std::apply([](auto... c) { return simd::cat(c...); }, simd::chunk<N>(x)) returns an object of the same type as x, with the same elements.

In std::experimental::simd, objects were split by std::experimental::split and std::experimental::split_by.

GCC on x86-64:

  • returns the halves of a register with vextractf128 (the upper 128 bits of a ymm register) or vextracti64x4 (the upper 256 bits of a zmm register), and the lower half without any instruction;
  • stores a basic_vec that is larger than a register in several registers, so splitting it at the boundaries of those registers needs no instructions;
  • moves the elements of chunks that do not start at a multiple of 128 bits with shuffles, such as psrldq with SSE or vperm2f128 and vpermd with AVX2.
Feature-test macro Value Std Feature
__cpp_lib_simd 202411L (C++26) simd::chunk

Example

#include <print>
#include <simd>
#include <tuple>

namespace simd = std::simd;

int main()
{
    simd::vec<int, 10> x([](int i) { return i; });
    std::println("x = {}", x);

    // 10 is a multiple of 5: an array of two vec<int, 5>
    auto a = simd::chunk<5>(x);
    std::println("chunk<5>(x) = {}", a);
    std::println("a[0] + a[1] = {}", a[0] + a[1]);

    // otherwise, a tuple whose last object holds the remaining elements
    auto [p, q, r] = simd::chunk<4>(x);
    std::println("chunk<4>(x) = {} {} {}", p, q, r);
    std::println("the last chunk has {} elements", r.size());

    // the type of the chunks instead of their size
    auto t = simd::chunk<simd::vec<int, 3>>(x);
    std::println("chunk<vec<int, 3>>(x) = {}", t);
    // std::apply handles an array and a tuple alike
    int sum = std::apply([](auto... c) { return (simd::reduce(c) + ...); }, t);
    std::println("sum of the elements = {}", sum);

    // masks are split the same way
    for (auto& m : simd::chunk<5>(x % 3 == 0))
        std::println("{} has {} true", m, simd::reduce_count(m));
}

Output:

x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
chunk<5>(x) = [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
a[0] + a[1] = [5, 7, 9, 11, 13]
chunk<4>(x) = [0, 1, 2, 3] [4, 5, 6, 7] [8, 9]
the last chunk has 2 elements
chunk<vec<int, 3>>(x) = ([0, 1, 2], [3, 4, 5], [6, 7, 8], [9])
sum of the elements = 45
[true, false, false, true, false] has 2 true
[false, true, false, false, true] has 2 true

See also

(C++26)
concatenates multiple data-parallel objects into a single one
(function template) [edit]
changes the width of the data-parallel type
(class template) [edit]
static and dynamic permutations of basic_vec and basic_mask
(function template) [edit]
a range of views that are N-sized non-overlapping successive chunks of the elements of another view
(class template) (range adaptor object)[edit]