std::simd::iota
| 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
Tis an arithmetic type,iota<T>isT(), that is, zero. - Otherwise,
iota<T>is a basic_vec whose element at indexihas the valuei. It is equal toT([](typename T::value_type i) { return i; }).
The program is ill-formed unless either
Tis an arithmetic vectorizable type, orTis an enabled specialization ofbasic_vecwith an arithmeticvalue_type, andT::size() - 1can be represented invalue_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) |
(C++23) |
|
(C++23) |
fills a range with successive increments of the starting value (algorithm function object) |
(C++20) |
a view consisting of a sequence generated by repeatedly incrementing an initial value (class template) (customization point object) |