Namespaces
Variants

std::simd::iota

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
Defined in header <simd>
template< class T >
constexpr T iota = /* see below */;
(since C++29)

A constant of type T holding the sequence 0, 1, 2, ...:

  • If T is an arithmetic type, iota<T> is T(), that is, zero.
  • Otherwise, iota<T> is a basic_vec whose element at index i has the value i. It is equal to T([](typename T::value_type i) { return i; }).

The program is ill-formed unless either

  • T is an arithmetic vectorizable type, or
  • T is an enabled specialization of basic_vec with an arithmetic value_type, and T::size() - 1 can be represented in value_type.

Notes

iota replaces a common use of the generator constructor of basic_vec. Other sequences can be written with arithmetic, like 5 + 3 * simd::iota<V> is 5, 8, 11, .... Unlike a list of values, iota<V> adapts to the width of V.

Because iota<T> also accepts arithmetic types, code that is generic over scalars and basic_vec can use it for both.

A width that exceeds the range of value_type makes the program ill-formed instead of producing values that wrap around: iota is intended for use as sequence of offsets or indices, where wraparound would be a bug.

iota<V> is not available when V is a basic_vec of std::complex, since its value_type is not an arithmetic type.

Some CPUs produce this sequence with a single instruction, such as INDEX on ARM SVE and vid.v on the RISC-V vector extension. On x86 SSE/AVX and ARM NEON, it is typically a constant loaded from memory.

Feature-test macro Value Std Feature
__cpp_lib_simd 202606L (C++29) std::simd::iota

Example

#include <print>
#include <simd>

namespace simd = std::simd;

int main()
{
    using V = simd::vec<int, 8>;
    constexpr V odd = 2 * simd::iota<V> + 1;
    for (int i = 0; i != odd.size(); ++i)
        std::print("{} ", odd[i]);
    std::println();

    static_assert(simd::iota<int> == 0);
}

Output:

1 3 5 7 9 11 13 15

See also

(C++11)
fills a range with successive increments of the starting value
(function template & algorithm function object)[edit]
fills a range with successive increments of the starting value
(algorithm function object)[edit]
a view consisting of a sequence generated by repeatedly incrementing an initial value
(class template) (customization point object)[edit]