Namespaces
Variants

std::simd::resize

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
Defined in header <simd>
template< /*simd-size-type*/ N, class V >
struct resize;
(since C++26)

Changes the size of a data-parallel type V to N and keeps its element type: provides the member typedef type, which is

  • basic_vec<typename V::value_type, Abi1> if V is a specialization of basic_vec, or
  • basic_mask<Bytes, Abi1> if V is basic_mask<Bytes, Abi>,

where Abi1 is an ABI tag chosen by the implementation, such that type has N elements.

There is no member type if V is not an enabled specialization of basic_vec or basic_mask, or if the implementation does not support N elements, such as when N is not positive or is larger than an implementation-defined maximum, which is at least 64.

simd-size-type is the signed integer type of size().

If the program declares an explicit or partial specialization of simd::resize, the program is ill-formed, no diagnostic required.

Member types

Name Definition
type the basic_vec or basic_mask type with the element type of V and N elements

Helper types

template< /*simd-size-type*/ N, class V >
using resize_t = resize<N, V>::type;
(since C++26)

Notes

simd::resize_t names the result types of the operations that change the number of elements: simd::chunk, simd::cat, and simd::permute return simd::resize_t types, and a function template that takes any basic_vec can use it to declare such a result.

basic_vec types of different sizes do not convert to each other. A basic_vec is changed to another size with simd::chunk, simd::cat, or simd::permute.

The ABI tag of type is up to the implementation, so simd::resize_t<N, V> is not necessarily the same type as simd::vec<typename V::value_type, N>, but each converts to the other.

In std::experimental::simd, this trait was std::experimental::resize_simd.

Feature-test macro Value Std Feature
__cpp_lib_simd 202411L (C++26) simd::resize and simd::resize_t

Example

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

namespace simd = std::simd;
using V = simd::vec<float, 8>;

// adds the two halves of a basic_vec of any even width
template<class W>
simd::resize_t<W::size() / 2, W> add_halves(const W& x)
{
    auto [lo, hi] = simd::chunk<W::size() / 2>(x);
    return lo + hi;
}

int main()
{
    V x([](int i) { return i + 1.f; });

    // the same element type, another size
    auto y = add_halves(x);
    static_assert(std::is_same_v<decltype(y), simd::resize_t<4, V>>);
    auto z = add_halves(y);
    std::println("{} -> {} -> {}", x, y, z);

    // permute<N> and cat return resize_t types
    simd::resize_t<3, V> first3 = simd::permute<3>(x, [](int i) { return i; });
    simd::resize_t<11, V> joined = simd::cat(x, first3);
    std::println("{}", joined);

    // for a basic_mask, the element size is kept
    using M = simd::resize_t<2, V::mask_type>;
    static_assert(std::is_same_v<M, simd::vec<float, 2>::mask_type>);
}

Output:

[1, 2, 3, 4, 5, 6, 7, 8] -> [6, 8, 10, 12] -> [16, 20]
[1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3]

See also

changes element type of the data-parallel type
(class template) [edit]
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]
static and dynamic permutations of basic_vec and basic_mask
(function template) [edit]