Namespaces
Variants

SIMD iterator (since C++26)

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
Defined in header <simd>
template< class V >
class /*simd-iterator*/;
(since C++26)
(exposition only*)

The member types iterator and const_iterator of std::simd::basic_vec and std::simd::basic_mask are specified in terms of the exposition-only class template simd-iterator: V::iterator is /*simd-iterator*/<V>, and V::const_iterator is /*simd-iterator*/<const V>. The member functions begin() and cbegin() return them, and end() and cend() return std::default_sentinel, so these types are read-only random access ranges.

An iterator holds a pointer to the basic_vec or basic_mask object and the index of an element. Dereferencing it returns a copy of the element (a prvalue of type V::value_type), because the elements of a data-parallel object are not separate objects in memory. Consequently, the iterator models random_access_iterator but not contiguous_iterator or output_iterator, and algorithms that modify the elements, such as std::ranges::sort, cannot be used.

Member types

Type Definition
value_type V::value_type
difference_type a signed integer type, the same type as the value of V::size()
iterator_concept std::random_access_iterator_tag
iterator_category std::input_iterator_tag

Operations

simd-iterator provides all the operations of a random access iterator: *i, i[n], ++i, i++, --i, i--, i += n, i -= n, i + n, n + i, i - n, i - j, i == j, and i <=> j. In addition, i == std::default_sentinel, i - std::default_sentinel, and std::default_sentinel - i compare and measure against the end. A V::iterator converts to V::const_iterator, and a value-initialized iterator does not refer to any object.

The behavior is undefined if an operation moves an iterator outside the elements and the end of its object, or if i - j or i <=> j is used with iterators into different objects.

Notes

The iterator refers to the object it was obtained from, so it observes changes to that object, such as v += 1, and it dangles once that object is destroyed: auto it = (a + b).begin(); leaves it dangling, while a range-based for loop over a + b is fine. basic_vec and basic_mask are not borrowed ranges.

Because begin() and end() return different types, they are not common ranges, and algorithms and constructors that take an iterator pair of one type, such as std::copy(v.begin(), v.end(), out), do not compile. Use the range algorithms, such as std::ranges::copy(v, out), or adapt the range with std::views::common.

Iterating over a basic_vec reads one element at a time. For example, GCC on x86-64 stores the whole register to the stack and loads the elements from there one by one, while a single element at a constant index is extracted directly (with pextrd if SSE4.1 is available). To copy all elements to memory at once, use std::simd::unchecked_store or std::simd::partial_store, which use vector store instructions.

Example

#include <algorithm>
#include <print>
#include <ranges>
#include <simd>
#include <vector>

int main()
{
    namespace simd = std::simd;
    simd::vec<int, 4> v([](int i) { return i * i; });

    for (int x : v)
        std::print("{} ", x);
    std::println();

    auto it = v.begin();
    v += 100; // the iterator reads the current value
    std::println("*it = {}, it[3] = {}, size = {}", *it, it[3], std::default_sentinel - it);

    std::println("max: {}", std::ranges::max(v));
    std::println("reversed: {}", v | std::views::reverse);

    auto c = std::views::common(v); // for algorithms that need begin and end of the same type
    std::vector<int> out(c.begin(), c.end());
    std::println("vector: {}", out);

    std::vector<simd::vec<float, 4>> data{simd::vec<float, 4>(1.5f), simd::vec<float, 4>(2.5f)};
    std::println("joined: {}", data | std::views::join);
}

Output:

0 1 4 9 
*it = 100, it[3] = 109, size = 4
max: 109
reversed: [109, 104, 101, 100]
vector: [100, 101, 104, 109]
joined: [1.5, 1.5, 1.5, 1.5, 2.5, 2.5, 2.5, 2.5]

See also

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]
default sentinel for use with iterators that know the bound of their range
(class) [edit]