Argon 0.1.0
Loading...
Searching...
No Matches
load_store.hpp
1
2
3#pragma once
4#include <cstddef>
5#include <cstdint>
6#include <iterator>
7#include <ranges>
8#include <span>
9#include "argon/argon_full.hpp"
10#include "argon/helpers/argon_for.hpp"
11#include "argon/helpers/size.hpp"
12#include "arm_simd/helpers/vec128.hpp"
14
15#ifdef __ARM_FEATURE_MVE
16#define simd mve
17#else
18#define simd neon
19#endif
20
21namespace argon::vectorize {
22
25
26template <typename ScalarType>
27struct load_store : public std::ranges::view_interface<load_store<ScalarType>> {
29 using intrinsic_type = simd::Vec128_t<ScalarType>;
30
32 static constexpr size_t lanes = sizeof(intrinsic_type) / sizeof(ScalarType);
33
34 public:
36 struct Iterator {
37 using iterator_category = std::forward_iterator_tag;
39 using difference_type = std::ptrdiff_t;
40
41 Iterator() = default;
42
45 Iterator(ScalarType* ptr) : ptr{ptr} {}
46
47 const value_type& operator*() const {
48 load();
49 return vec;
50 }
51
55 load();
56 dirty_ = true;
57 return vec;
58 }
59
60 const value_type* operator->() const {
61 load();
62 return &vec;
63 }
64
68 load();
69 dirty_ = true;
70 return &vec;
71 }
72
75 Iterator& operator++() {
76 if (dirty_) {
77 vec.StoreTo(ptr); // store before increment
78 }
79 ptr += lanes;
80 loaded_ = dirty_ = false; // load lazily: the end position is never read
81 return *this;
82 }
83
85 Iterator operator++(int) {
86 Iterator tmp = *this;
87 ++(*this);
88 return tmp;
89 }
90
93 friend bool operator==(const Iterator& a, const Iterator& b) { return a.ptr == b.ptr; }
94
96 friend bool operator==(const Iterator& a, const ScalarType* ptr) { return a.ptr == ptr; }
97
100 friend bool operator!=(const Iterator& a, const Iterator& b) { return a.ptr != b.ptr; }
101
103 friend bool operator!=(const Iterator& a, const ScalarType* ptr) { return a.ptr != ptr; }
104
106 void reload() {
107 vec = value_type::Load(ptr);
108 loaded_ = true;
109 }
110
111 private:
113 void load() const {
114 if (!loaded_) {
115 vec = value_type::Load(ptr);
116 loaded_ = true;
117 }
118 }
119
120 bool dirty_ = false;
121 mutable bool loaded_ = false;
122 ScalarType* ptr = nullptr;
123 mutable value_type vec;
124 };
125 static_assert(std::input_or_output_iterator<Iterator>);
126
128 struct ConstIterator {
129 using iterator_category = std::forward_iterator_tag;
131 using difference_type = std::ptrdiff_t;
132
133 ConstIterator() = default;
134
137 ConstIterator(const ScalarType* ptr) : ptr{ptr} {}
138
141 const value_type operator*() const { return value_type::Load(ptr); }
142
145 ConstIterator& operator++() {
146 ptr += lanes;
147 return *this;
148 }
149
152 ConstIterator operator++(int) {
153 ConstIterator tmp = *this;
154 ++(*this);
155 return tmp;
156 }
157
160 friend bool operator==(const ConstIterator& a, const ConstIterator& b) { return a.ptr == b.ptr; }
161
164 friend bool operator!=(const ConstIterator& a, const ConstIterator& b) { return a.ptr != b.ptr; }
165
167 friend bool operator==(const ConstIterator& a, const ScalarType* ptr) { return a.ptr == ptr; }
168
169 private:
170 const ScalarType* ptr = nullptr;
171 };
172 static_assert(std::input_iterator<ConstIterator>);
173
176
180 load_store(ScalarType* start, ScalarType* end)
181 : start_{start}, size_{helpers::vectorizeable_size<ScalarType>(end - start) / lanes}, count_{static_cast<size_t>(end - start)} {};
182
186 load_store(ScalarType* start, const size_t size)
187 : start_{start}, size_{helpers::vectorizeable_size<ScalarType>(size) / lanes}, count_{size} {};
188
191 load_store(std::span<ScalarType> span)
192 : start_{span.data()}, size_{helpers::vectorizeable_size<ScalarType>(span.size()) / lanes}, count_{span.size()} {};
193
197 template <std::ranges::contiguous_range R>
199 : start_{&*std::ranges::begin(r)}, size_{helpers::vectorizeable_size<ScalarType>(std::ranges::size(r)) / lanes}, count_{std::ranges::size(r)} {}
200
203 iterator begin() const { return Iterator(start_); }
204
207 ScalarType* end() const { return start_ + (size_ * lanes); }
208
211 const_iterator cbegin() const { return ConstIterator(start_); }
212
215 const ScalarType* cend() const { return start_ + (size_ * lanes); }
216
220 size_t size() const { return size_; }
221
225 load_store_tail<ScalarType> with_tail() const { return {start_, count_}; }
226
227 private:
228 ScalarType* start_;
229 size_t size_;
230 size_t count_;
231};
232
235template <std::ranges::contiguous_range R>
236load_store(R&& r) -> load_store<std::ranges::range_value_t<R>>;
237
238static_assert(std::ranges::range<load_store<int32_t>>);
239static_assert(std::ranges::view<load_store<int32_t>>);
240static_assert(std::movable<load_store<int32_t>>);
241static_assert(std::ranges::viewable_range<load_store<int32_t>>);
242
243} // namespace argon::vectorize
244#undef simd
In-place read/write view of a range as vectors, including a final partial vector.
Definition tail.hpp:275
Contains helper templates and concepts for type manipulation and compile-time utilities.
Definition argon.hpp:156
typename ArgonFor< std::remove_cv_t< T > >::type ArgonFor_t
Helper alias to get the Argon type for a given vector type.
Definition argon_for.hpp:46
Lane deconstruction feature.
Definition argon_full.hpp:1547
A const iterator for the vectorized view.
Definition load_store.hpp:128
const value_type operator*() const
Dereference the const iterator to get the current value.
Definition load_store.hpp:141
friend bool operator==(const ConstIterator &a, const ScalarType *ptr)
Compare against the end of the range (cend()).
Definition load_store.hpp:167
ConstIterator(const ScalarType *ptr)
Constructs a const iterator from a pointer to the start of the range.
Definition load_store.hpp:137
ConstIterator operator++(int)
Postfix increment operator for the const iterator.
Definition load_store.hpp:152
helpers::ArgonFor_t< intrinsic_type > value_type
The value type of the iterator.
Definition load_store.hpp:130
friend bool operator==(const ConstIterator &a, const ConstIterator &b)
Equivalence operator.
Definition load_store.hpp:160
friend bool operator!=(const ConstIterator &a, const ConstIterator &b)
Non-equivalence operator.
Definition load_store.hpp:164
std::ptrdiff_t difference_type
The difference type of the iterator.
Definition load_store.hpp:131
std::forward_iterator_tag iterator_category
The iterator category.
Definition load_store.hpp:129
ConstIterator & operator++()
Increment operator for the const iterator.
Definition load_store.hpp:145
An iterator for the vectorized view.
Definition load_store.hpp:36
friend bool operator==(const Iterator &a, const ScalarType *ptr)
Comparison operators for the iterator.
Definition load_store.hpp:96
helpers::ArgonFor_t< intrinsic_type > value_type
The value type of the iterator.
Definition load_store.hpp:38
friend bool operator!=(const Iterator &a, const Iterator &b)
Comparison operators for the iterator.
Definition load_store.hpp:100
Iterator(ScalarType *ptr)
Constructs an iterator from a pointer to the start of the range.
Definition load_store.hpp:45
Iterator & operator++()
Increment the iterator to the next value.
Definition load_store.hpp:75
void reload()
Reload the iterator with the current value.
Definition load_store.hpp:106
Iterator operator++(int)
Postfix increment operator for the iterator.
Definition load_store.hpp:85
std::forward_iterator_tag iterator_category
The iterator category.
Definition load_store.hpp:37
value_type & operator*()
Dereference the iterator to get the current value.
Definition load_store.hpp:54
value_type * operator->()
Dereference the iterator to get the current value.
Definition load_store.hpp:67
friend bool operator==(const Iterator &a, const Iterator &b)
Comparison operators for the iterator.
Definition load_store.hpp:93
std::ptrdiff_t difference_type
The difference type of the iterator.
Definition load_store.hpp:39
friend bool operator!=(const Iterator &a, const ScalarType *ptr)
Comparison operators for the iterator.
Definition load_store.hpp:103
load_store(ScalarType *start, ScalarType *end)
Constructs a vectorized view from a pointer to the start of the range and the end of the range.
Definition load_store.hpp:180
load_store(std::span< ScalarType > span)
Constructs a vectorized view from a span.
Definition load_store.hpp:191
iterator begin() const
Returns an iterator to the beginning.
Definition load_store.hpp:203
ConstIterator const_iterator
The const iterator type for the vectorized view.
Definition load_store.hpp:175
size_t size() const
Get the size of the view.
Definition load_store.hpp:220
Iterator iterator
The iterator type for the vectorized view.
Definition load_store.hpp:174
simd::Vec128_t< ScalarType > intrinsic_type
The SIMD vector type used for vectorization.
Definition load_store.hpp:29
static constexpr size_t lanes
The number of lanes in the SIMD vector.
Definition load_store.hpp:32
load_store(R &&r)
Constructs a vectorized view from a range.
Definition load_store.hpp:198
const ScalarType * cend() const
Returns a const iterator to the end of the range.
Definition load_store.hpp:215
load_store_tail< ScalarType > with_tail() const
A view of the same range that also visits the final, partial vector.
Definition load_store.hpp:225
load_store(ScalarType *start, const size_t size)
Constructs a vectorized view from a pointer to the start of the range and the size of the range.
Definition load_store.hpp:186
const_iterator cbegin() const
Returns a const iterator to the beginning of the range.
Definition load_store.hpp:211
ScalarType * end() const
Returns an iterator to the end.
Definition load_store.hpp:207
A view that loads data from a contiguous range into SIMD vectors.
Definition load.hpp:37
Definition ptr.hpp:74
Vectorized views that also visit the final, partial vector of a range (see with_tail() on the views).