Argon 0.1.0
Loading...
Searching...
No Matches
load_interleaved.hpp
1#pragma once
2#include <ranges>
3#include "argon/argon_full.hpp"
5#include "arm_simd/helpers/vec128.hpp"
6
7#ifdef __ARM_FEATURE_MVE
8#define simd mve
9#else
10#define simd neon
11#endif
12
13namespace argon::vectorize {
14
15template <typename ScalarType, size_t Stride = 2>
16struct load_interleaved : std::ranges::view_interface<load_interleaved<ScalarType, Stride>> {
17 using intrinsic_type = simd::Vec128_t<ScalarType>;
18 static constexpr size_t lanes = sizeof(intrinsic_type) / sizeof(ScalarType);
19 // Round down to a whole number of iterations; each iteration spans lanes * Stride elements.
20 static constexpr size_t vectorizeable_size(size_t size) { return size - (size % (lanes * Stride)); }
21
22 static_assert(Stride > 1 && Stride < 5, "Interleaving Loads can only be performed with a stride of 2, 3, or 4");
23
24 public:
25 struct LoadInterleavedIterator {
26 using iterator_category = std::bidirectional_iterator_tag;
27 using difference_type = std::ptrdiff_t;
28 using reference_type = LoadInterleavedIterator&;
29 using value_type = std::array<Argon<ScalarType>, Stride>;
30
31 LoadInterleavedIterator() = default;
32 LoadInterleavedIterator(const ScalarType* ptr) : ptr_{ptr} {}
33
34 std::array<Argon<ScalarType>, Stride> operator*() const {
35 return Argon<ScalarType>::template LoadInterleaved<Stride>(ptr_);
36 }
37
38 LoadInterleavedIterator& operator+=(int n) {
39 ptr_ += n * lanes * Stride;
40 return *this;
41 }
42
43 LoadInterleavedIterator& operator-=(int n) {
44 ptr_ -= n * lanes * Stride;
45 return *this;
46 }
47
48 LoadInterleavedIterator operator++(int) {
49 LoadInterleavedIterator tmp = *this;
50 ++(*this);
51 return tmp;
52 }
53
54 LoadInterleavedIterator& operator++() {
55 ptr_ += lanes * Stride;
56 return *this;
57 }
58
59 LoadInterleavedIterator operator--(int) {
60 LoadInterleavedIterator tmp = *this;
61 --(*this);
62 return tmp;
63 }
64
65 LoadInterleavedIterator& operator--() {
66 ptr_ -= lanes * Stride;
67 return *this;
68 }
69
70 LoadInterleavedIterator operator+(int n) const {
71 LoadInterleavedIterator it = *this;
72 it += n;
73 return it;
74 }
75
76 LoadInterleavedIterator operator-(int n) const {
77 LoadInterleavedIterator it = *this;
78 it -= n;
79 return it;
80 }
81
82 value_type operator[](int n) const { return *(*this + n); }
83
84 difference_type operator-(const LoadInterleavedIterator& other) const {
85 return (ptr_ - other.ptr_) / difference_type{lanes * Stride};
86 }
87
88 friend bool operator==(const LoadInterleavedIterator& a, const LoadInterleavedIterator& b) {
89 return a.ptr_ == b.ptr_;
90 }
91 friend bool operator==(const LoadInterleavedIterator& a, const ScalarType* ptr) { return a.ptr_ == ptr; }
92 friend bool operator!=(const LoadInterleavedIterator& a, const LoadInterleavedIterator& b) {
93 return a.ptr_ != b.ptr_;
94 }
95 friend bool operator!=(const LoadInterleavedIterator& a, const ScalarType* ptr) { return a.ptr_ != ptr; }
96 friend auto operator<=>(const LoadInterleavedIterator& a, const LoadInterleavedIterator& b) {
97 return a.ptr_ <=> b.ptr_;
98 }
99
100 friend LoadInterleavedIterator operator+(const int n, const LoadInterleavedIterator& it) { return it + n; }
101
102 private:
103 const ScalarType* ptr_;
104 };
105 static_assert(std::sized_sentinel_for<LoadInterleavedIterator, LoadInterleavedIterator>);
106 static_assert(std::bidirectional_iterator<LoadInterleavedIterator>);
107 static_assert(std::input_iterator<LoadInterleavedIterator>);
108
109 using iterator = LoadInterleavedIterator;
110 using sentinel = const ScalarType*;
111
112 iterator begin() { return start_; }
113 const ScalarType* end() { return start_ + size_; }
114 size_t size() const { return size_ / (lanes * Stride); }
115
116 template <std::ranges::contiguous_range R>
117 load_interleaved(R&& r)
118 : start_{&*std::ranges::begin(r)}, size_{vectorizeable_size(std::ranges::size(r))}, count_{std::ranges::size(r)} {}
119
124 load_tail<ScalarType, Stride> with_tail() const { return {start_, count_ / Stride}; }
125
126 private:
127 const ScalarType* start_;
128 size_t size_;
129 size_t count_;
130};
131
132static_assert(std::ranges::range<load_interleaved<int32_t, 2>>);
133static_assert(std::ranges::view<load_interleaved<int32_t, 2>>);
134
135template <std::ranges::contiguous_range R, size_t stride = 2>
136load_interleaved(R&& r) -> load_interleaved<std::ranges::range_value_t<R>, stride>;
137
138} // namespace argon::vectorize
139#undef simd
Definition argon_full.hpp:44
Read-only view of a range as vectors, including a final partial vector.
Definition tail.hpp:168
Lane deconstruction feature.
Definition argon_full.hpp:1547
load_tail< ScalarType, Stride > with_tail() const
A view of the same range that also visits the final, partial group of vectors.
Definition load_interleaved.hpp:124
Definition ptr.hpp:74
Vectorized views that also visit the final, partial vector of a range (see with_tail() on the views).