Namespaces
Variants

std::simd::permute, std::simd::zero_element, std::simd::uninit_element

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
Defined in header <simd>
template< /*simd-size-type*/ N = /* see below */, /*simd-vec-type*/ V, class IdxMap >
constexpr resize_t<N, V> permute( const V& v, IdxMap&& idxmap );
(1) (since C++26)
template< /*simd-size-type*/ N = /* see below */, /*simd-mask-type*/ V, class IdxMap >
constexpr resize_t<N, V> permute( const V& v, IdxMap&& idxmap );
(2) (since C++26)
template< /*simd-vec-type*/ V, /*simd-integral*/ I >
constexpr resize_t<I::size(), V> permute( const V& v, const I& indices );
(3) (since C++26)
template< /*simd-mask-type*/ V, /*simd-integral*/ I >
constexpr resize_t<I::size(), V> permute( const V& v, const I& indices );
(4) (since C++26)
inline constexpr /*simd-size-type*/ zero_element = /* implementation-defined */;
(5) (since C++26)
inline constexpr /*simd-size-type*/ uninit_element = /* implementation-defined */;
(6) (since C++26)

Returns a basic_vec or a basic_mask that holds elements of v in the order given by a list of indices: the ith element of the result is v[j], where j is the ith index. An index can appear more than once or not at all. The result has the element type of v and as many elements as there are indices.

1,2) The indices are computed at compile time by idxmap. The result has N elements, V::size() if N is not given. The index for the element i of the result is idxmap(i, V::size()) if that call is well-formed, and idxmap(i) otherwise, where i is of type simd-size-type. If the index is zero_element, the element is zero (typename V::value_type(), which is false for a basic_mask). If it is uninit_element, the value of the element is unspecified.
The program is ill-formed if an index is not a constant expression, or is not zero_element, uninit_element, or in the range [0, V::size()).
These overloads participate in overload resolution only if idxmap can be called with one or with two arguments of type simd-size-type and returns an integral type.
3,4) The ith element of the result is v[indices[i]]. If an element of indices is not in the range [0, V::size()), the behavior is undefined.
5,6) Special indices that idxmap of (1,2) can return.

simd-size-type is the signed integer type of size(). simd-vec-type and simd-mask-type are satisfied by the enabled specializations of basic_vec and basic_mask, and simd-integral by those of basic_vec with an integral element type.

Parameters

v - the basic_vec or basic_mask to take the elements from
idxmap - a function object that returns the index of an element of v for each position in the result, and optionally takes the size of v as the second argument
indices - a basic_vec of indices of elements of v

Return value

1,2) A resize_t<N, V> that holds the permuted elements.
3,4) A resize_t<I::size(), V> that holds the permuted elements.

Notes

The indices of (1,2) are constants, so the compiler can choose the best instruction for the permutation. The second argument of idxmap lets one function object work for objects of any size: [](int i, int n) { return n - 1 - i; } reverses the elements. That argument is the size of v, not of the result. There are no negative indices that count from the end.

uninit_element is for elements whose values are never used, such as elements that are overwritten later: it lets the compiler pick whatever the cheapest instructions leave there.

To take elements from two objects, permute their concatenation: simd::permute<N>(simd::cat(a, b), idxmap).

The subscript operators of basic_vec and basic_mask that take a basic_vec of indices are the same as (3,4).

In std::experimental::simd, elements were rearranged with std::experimental::split and std::experimental::concat.

GCC on x86-64 implements (1):

  • for a permutation within each 128-bit lane, such as swapping neighbors or repeating the even elements, with one instruction that takes the pattern as an immediate: shufps with SSE, movsldup with SSE3, or vpermilps with AVX;
  • for a permutation across lanes, with vpermd with AVX2, or with vextractf128 if the result is the upper half;
  • for zero_element, with an added vblendps from a zeroed register with AVX2, or with vpermi2ps, which takes the elements from two registers, one of them zeroed, with AVX-512.

A permutation with indices known only at run time, as in (3), is one instruction for 32-bit elements with AVX (vpermilps within 128-bit lanes), AVX2 (vpermps), and the RISC-V vector extension (vrgather.vv).

Feature-test macro Value Std Feature
__cpp_lib_simd_permutations 202506L (C++26) simd::permute, simd::zero_element, and simd::uninit_element

Example

#include <print>
#include <simd>

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

int main()
{
    V v([](int i) { return (i + 1) * 10.f; });
    std::println("v:              {}", v);

    // the second argument is v.size(): this works for any width
    auto reverse = [](int i, int n) { return n - 1 - i; };
    std::println("reversed:       {}", simd::permute(v, reverse));

    // indices can repeat
    std::println("even twice:     {}", simd::permute(v, [](int i) {
        return i & ~1;
    }));

    // the result can have another size: every third element
    std::println("stride 3:       {}", simd::permute<3>(v, [](int i) {
        return i * 3;
    }));

    // shifted by one element, with a zero in front
    std::println("shifted:        {}", simd::permute(v, [](int i) {
        return i == 0 ? simd::zero_element : i - 1;
    }));

    // elements of two objects: permute their concatenation
    V w(1.f);
    std::println("interleaved:    {}", simd::permute<8>(simd::cat(v, w),
        [](int i) { return i % 2 * 8 + i / 2; }));

    // sums of neighbors; the odd elements of pairs are never used
    V pairs = v + simd::permute(v, [](int i) {
        return i % 2 ? simd::uninit_element : i + 1;
    });
    std::println("neighbor sums:  {}", simd::permute<4>(pairs, [](int i) {
        return i * 2;
    }));
}

Output:

v:              [10, 20, 30, 40, 50, 60, 70, 80]
reversed:       [80, 70, 60, 50, 40, 30, 20, 10]
even twice:     [10, 10, 30, 30, 50, 50, 70, 70]
stride 3:       [10, 40, 70]
shifted:        [0, 10, 20, 30, 40, 50, 60, 70]
interleaved:    [10, 1, 20, 1, 30, 1, 40, 1]
neighbor sums:  [30, 70, 110, 150]

See also

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