Namespaces
Variants

std::simd::cat

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
Defined in header <simd>
template< class T, class... Abis >
constexpr resize_t<(basic_vec<T, Abis>::size() + ...), basic_vec<T, Abis...[0]>>
    cat( const basic_vec<T, Abis>&... xs ) noexcept;
(1) (since C++26)
template< std::size_t Bytes, class... Abis >
constexpr resize_t<(basic_mask<Bytes, Abis>::size() + ...), basic_mask<Bytes, Abis...[0]>>
    cat( const basic_mask<Bytes, Abis>&... xs ) noexcept;
(2) (since C++26)

Concatenates the elements of one or more data-parallel objects, in the order of the arguments, into a single object whose size is the sum of their sizes. Element i of the argument at position j becomes element i plus the sum of the sizes of the first j arguments of the result.

1) Concatenates basic_vecs that have the same element type T.
2) Concatenates basic_masks that have the same element size Bytes.

The arguments can differ in size and in ABI tag. The result has the type of the first argument, resized with simd::resize_t.

Parameters

xs - the objects to concatenate

Return value

A basic_vec (1) or basic_mask (2) that holds the elements of all xs.

Notes

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. std::apply also concatenates the elements of any other std::array or std::tuple of basic_vec objects.

A basic_vec with a different element type has to be converted first, as in simd::cat(a, simd::vec<float, 4>(b)) for a simd::vec<float, 8> a and a simd::vec<int, 4> b. Masks of basic_vec types whose elements have the same size, such as simd::mask<int> and simd::mask<float>, can be concatenated without conversion.

In std::experimental::simd, objects were concatenated by std::experimental::concat.

GCC on x86-64:

  • joins two halves of a register with movlhps (two 64-bit halves of an xmm register), vinsertf128 (two xmm registers into a ymm register), or vshuff32x4 (two ymm registers into a zmm register);
  • stores a basic_vec that is larger than a register in several registers, so concatenating objects that each fill those registers needs no instructions;
  • moves the elements of objects that do not end at a multiple of 128 bits with shuffles, such as shufps with SSE or vpermd and vpblendd with AVX2.
Feature-test macro Value Std Feature
__cpp_lib_simd 202411L (C++26) simd::cat

Example

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

namespace simd = std::simd;

int main()
{
    simd::vec<int, 4> a([](int i) { return i; });
    simd::vec<int, 3> b(7);
    std::println("cat(a, b, a) = {}", simd::cat(a, b, a));

    // with simd::chunk: swap the halves of a vec
    simd::vec<int, 8> x([](int i) { return i * 10; });
    auto [lo, hi] = simd::chunk<4>(x);
    std::println("cat(hi, lo) = {}", simd::cat(hi, lo));

    // reassemble all chunks: the same type as x
    auto y = std::apply([](auto... c) { return simd::cat(c...); },
                        simd::chunk<3>(x));
    static_assert(std::is_same_v<decltype(y), decltype(x)>);
    std::println("cat of chunk<3>(x) = {}", y);

    // masks with the same element size, such as those of int and float
    simd::vec<float, 4> f([](int i) { return i - 1.5f; });
    auto m = simd::cat(a > 1, f > 0.f);
    std::println("cat(a > 1, f > 0.f) = {}", m);
}

Output:

cat(a, b, a) = [0, 1, 2, 3, 7, 7, 7, 0, 1, 2, 3]
cat(hi, lo) = [40, 50, 60, 70, 0, 10, 20, 30]
cat of chunk<3>(x) = [0, 10, 20, 30, 40, 50, 60, 70]
cat(a > 1, f > 0.f) = [false, false, true, true, false, false, true, true]

See also

splits single data-parallel object to multiple ones
(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]
(C++11)
creates a tuple by concatenating any number of tuples
(function template) [edit]