Namespaces
Variants

std::simd::rebind

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
Defined in header <simd>
template< class T, class V >
struct rebind;
(since C++26)

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

  • basic_vec<T, Abi1> if V is a specialization of basic_vec, or
  • basic_mask<sizeof(T), Abi1>, the mask type of that basic_vec, if V is a specialization of basic_mask,

where Abi1 is an ABI tag chosen by the implementation, such that type has V::size() elements.

There is no member type if V is not an enabled specialization of basic_vec or basic_mask, if T is not a vectorizable type, or if the implementation does not support a basic_vec of V::size() elements of type T.

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

Member types

Name Definition
type the basic_vec or basic_mask type with element type T and the size of V

Helper types

template< class T, class V >
using rebind_t = rebind<T, V>::type;
(since C++26)

Notes

A function template that takes any basic_vec can use simd::rebind_t for a basic_vec with the same number of elements of another type, to compute in a wider type or to hold integer results. The math functions ilogb, lround, lrint, and others return a simd::rebind_t of an integer type, and frexp and remquo take a pointer to one.

The ABI tag of type is up to the implementation, so simd::rebind_t<T, V> is not necessarily the same type as simd::vec<T, V::size()>, but each converts to the other.

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

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

Example

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

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

// the sum of squares of a basic_vec of any width, computed in double
template<class W>
double sum_squares(const W& x)
{
    simd::rebind_t<double, W> d = x; // float to double is value-preserving
    return simd::reduce(d * d);
}

int main()
{
    V x([](int i) { return 0.5f * i + 0.25f; });
    std::println("x = {}", x);
    std::println("sum of squares = {}", sum_squares(x));

    // the same size, another element type
    simd::rebind_t<int, V> n(x * 4.f);
    std::println("n = {}", n);

    // for a basic_mask, the new element size is sizeof(T)
    using M = simd::rebind_t<double, V::mask_type>;
    static_assert(std::is_same_v<M, simd::vec<double, 4>::mask_type>);
    M m(x > 1.f);
    std::println("m = {}", m);
}

Output:

x = [0.25, 0.75, 1.25, 1.75]
sum of squares = 5.25
n = [1, 3, 5, 7]
m = [false, false, true, true]

See also

changes the width of the data-parallel type
(class template) [edit]
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]