Argon 0.1.0
Loading...
Searching...
No Matches
tail.hpp
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <array>
4#include <cstddef>
5#include <iterator>
6#include <ranges>
7#include <type_traits>
8#include "argon/argon_full.hpp"
9#include "argon/store.hpp"
11
14
15namespace argon::vectorize {
16
25template <typename ScalarType, size_t Stride = 1>
26class Partial {
27 public:
30 using vector_type = std::conditional_t<Stride == 1, argon_type, std::array<argon_type, Stride>>;
32 static constexpr size_t lanes = argon_type::lanes;
33
34 Partial() = default;
35
37 Partial(vector_type value, size_t remaining) : value_{value}, remaining_{remaining} {}
38
40 vector_type& operator*() { return value_; }
42 const vector_type& operator*() const { return value_; }
44 vector_type* operator->() { return &value_; }
46 const vector_type* operator->() const { return &value_; }
47
49 Partial& operator=(vector_type value) {
50 value_ = value;
51 return *this;
52 }
53
55 size_t count() const { return std::min(remaining_, lanes); }
57 bool full() const { return remaining_ >= lanes; }
59 predicate_type active() const { return predicate_type::FirstN(remaining_); }
60
62 size_t remaining() const { return remaining_; }
63
64 private:
65 vector_type value_{};
66 size_t remaining_ = 0; // unclamped: FirstN (vctp on MVE) saturates, and a clamp here keeps the loop from tail
67 // predicating
68};
69
70namespace detail {
72template <typename ScalarType, size_t Stride>
74 using offset_type = typename Argon<ScalarType>::offset_type;
75 return offset_type::Iota(0) * static_cast<typename offset_type::scalar_type>(Stride);
76}
77
80template <size_t Stride, typename ScalarType>
81typename Partial<ScalarType, Stride>::vector_type load_partial(const ScalarType* ptr, size_t count) {
82 using argon_type = Argon<ScalarType>;
83#ifdef ARGON_PLATFORM_MVE
84 using predicate_type = typename argon_type::argon_bool_type;
85#endif
86 if constexpr (Stride == 1) {
87#ifdef ARGON_PLATFORM_MVE
88 // Always predicate: a vctp-predicated loop is what the compiler turns into dlstp/letp.
89 return argon_type::Load(ptr, predicate_type::FirstN(count));
90#else
91 return count >= argon_type::lanes ? argon_type::Load(ptr) : load_first_n(ptr, count);
92#endif
93 } else {
94 if (count >= argon_type::lanes) {
95 return argon_type::template LoadInterleaved<Stride>(ptr);
96 }
97#ifdef ARGON_PLATFORM_MVE
98 // De-interleaving loads (vld2/vld4) can't be predicated, even on MVE: gather each channel instead.
99 const auto offsets = frame_offsets<ScalarType, Stride>();
100 const auto active = predicate_type::FirstN(count);
101 std::array<argon_type, Stride> out;
102 for (size_t channel = 0; channel < Stride; ++channel) {
103 out[channel] = argon_type::LoadGatherOffsetIndex(ptr + channel, offsets, active);
104 }
105 return out;
106#else
108#endif
109 }
110}
111
114template <size_t Stride, typename ScalarType>
115void store_partial(ScalarType* ptr, const typename Partial<ScalarType, Stride>::vector_type& value, size_t count) {
116 using argon_type = Argon<ScalarType>;
117#ifdef ARGON_PLATFORM_MVE
118 using predicate_type = typename argon_type::argon_bool_type;
119#endif
120 if constexpr (Stride == 1) {
121#ifdef ARGON_PLATFORM_MVE
122 value.StoreTo(ptr, predicate_type::FirstN(count));
123#else
124 if (count >= argon_type::lanes) {
125 value.StoreTo(ptr);
126 } else {
127 store_first_n(ptr, value, count);
128 }
129#endif
130 } else {
131 if (count >= argon_type::lanes) {
133 return;
134 }
135#ifdef ARGON_PLATFORM_MVE
136 // Interleaving stores (vst2/vst4) can't be predicated, even on MVE: scatter each channel instead.
137 const auto offsets = frame_offsets<ScalarType, Stride>();
138 const auto active = predicate_type::FirstN(count);
139 for (size_t channel = 0; channel < Stride; ++channel) {
140 value[channel].StoreScatterOffsetIndex(ptr + channel, offsets, active);
141 }
142#else
144#endif
145 }
146}
147
150template <typename PointerType, size_t Stride>
152 static constexpr size_t lanes = Argon<std::remove_const_t<PointerType>>::lanes;
153
154 PointerType* base = nullptr;
155 size_t index = 0;
156 size_t count = 0;
157
158 PointerType* ptr() const { return base + index * Stride; }
160 size_t remaining() const { return count - index; }
161 bool done() const { return index >= count; }
162};
163} // namespace detail
164
167template <typename ScalarType, size_t Stride = 1>
168class load_tail : public std::ranges::view_interface<load_tail<ScalarType, Stride>> {
169 static constexpr size_t lanes = Argon<ScalarType>::lanes;
170
171 public:
173 struct Iterator {
175 using difference_type = std::ptrdiff_t;
176
177 Iterator() = default;
178 Iterator(detail::TailCursor<const ScalarType, Stride> cursor) : cursor_{cursor} {}
179
182 const size_t n = cursor_.remaining();
183 return {detail::load_partial<Stride>(cursor_.ptr(), n), n};
184 }
185
186 Iterator& operator++() {
187 cursor_.index += lanes;
188 return *this;
189 }
190 Iterator operator++(int) {
191 Iterator tmp = *this;
192 ++*this;
193 return tmp;
194 }
195
196 friend bool operator==(const Iterator& a, const Iterator& b) { return a.cursor_.index == b.cursor_.index; }
197 friend bool operator==(const Iterator& it, std::default_sentinel_t) { return it.cursor_.done(); }
198
199 private:
200 detail::TailCursor<const ScalarType, Stride> cursor_;
201 };
202 static_assert(std::forward_iterator<Iterator>);
203
204 load_tail() = default;
206 load_tail(const ScalarType* start, size_t count) : start_{start}, count_{count} {}
207
208 Iterator begin() const { return Iterator{{start_, 0, count_}}; }
209 std::default_sentinel_t end() const { return {}; }
211 size_t size() const { return (count_ + lanes - 1) / lanes; }
212
213 private:
214 const ScalarType* start_ = nullptr;
215 size_t count_ = 0;
216};
217
221template <typename ScalarType, size_t Stride = 1>
222class store_tail : public std::ranges::view_interface<store_tail<ScalarType, Stride>> {
223 static constexpr size_t lanes = Argon<ScalarType>::lanes;
224
225 public:
227 struct Iterator {
229 using difference_type = std::ptrdiff_t;
230
231 Iterator() = default;
232 Iterator(detail::TailCursor<ScalarType, Stride> cursor) : cursor_{cursor}, current_{{}, cursor.remaining()} {}
233
234 value_type& operator*() { return current_; }
235 const value_type& operator*() const { return current_; }
236
238 Iterator& operator++() {
239 detail::store_partial<Stride>(cursor_.ptr(), *current_, current_.remaining());
240 cursor_.index += lanes;
241 // Past the end the remaining count wraps, but it is never read there; a conditional here would keep the
242 // compiler from forming a tail-predicated (dlstp/letp) loop.
243 current_ = value_type{{}, cursor_.remaining()};
244 return *this;
245 }
246 void operator++(int) { ++*this; }
247
248 friend bool operator==(const Iterator& it, std::default_sentinel_t) { return it.cursor_.done(); }
249
250 private:
251 detail::TailCursor<ScalarType, Stride> cursor_;
252 value_type current_;
253 };
254 static_assert(std::input_or_output_iterator<Iterator>);
255
256 store_tail() = default;
258 store_tail(ScalarType* start, size_t count) : start_{start}, count_{count} {}
259
260 Iterator begin() const { return Iterator{{start_, 0, count_}}; }
261 std::default_sentinel_t end() const { return {}; }
263 size_t size() const { return (count_ + lanes - 1) / lanes; }
264
265 private:
266 ScalarType* start_ = nullptr;
267 size_t count_ = 0;
268};
269
274template <typename ScalarType, size_t Stride = 1>
275class load_store_tail : public std::ranges::view_interface<load_store_tail<ScalarType, Stride>> {
276 static constexpr size_t lanes = Argon<ScalarType>::lanes;
277
278 public:
280 struct Iterator {
282 using difference_type = std::ptrdiff_t;
283
284 Iterator() = default;
285 Iterator(detail::TailCursor<ScalarType, Stride> cursor) : cursor_{cursor} {}
286
287 value_type& operator*() {
288 load();
289 dirty_ = true;
290 return current_;
291 }
292 const value_type& operator*() const {
293 load();
294 return current_;
295 }
296
298 Iterator& operator++() {
299 if (dirty_) {
300 detail::store_partial<Stride>(cursor_.ptr(), *current_, current_.remaining());
301 }
302 cursor_.index += lanes;
303 loaded_ = dirty_ = false;
304 return *this;
305 }
306 void operator++(int) { ++*this; }
307
308 friend bool operator==(const Iterator& it, std::default_sentinel_t) { return it.cursor_.done(); }
309
310 private:
311 void load() const {
312 if (!loaded_) {
313 const size_t n = cursor_.remaining();
314 current_ = value_type{detail::load_partial<Stride>(static_cast<const ScalarType*>(cursor_.ptr()), n), n};
315 loaded_ = true;
316 }
317 }
318
319 detail::TailCursor<ScalarType, Stride> cursor_;
320 mutable value_type current_;
321 mutable bool loaded_ = false;
322 bool dirty_ = false;
323 };
324 static_assert(std::input_or_output_iterator<Iterator>);
325
326 load_store_tail() = default;
328 load_store_tail(ScalarType* start, size_t count) : start_{start}, count_{count} {}
329
330 Iterator begin() const { return Iterator{{start_, 0, count_}}; }
331 std::default_sentinel_t end() const { return {}; }
333 size_t size() const { return (count_ + lanes - 1) / lanes; }
334
335 private:
336 ScalarType* start_ = nullptr;
337 size_t count_ = 0;
338};
339
340} // namespace argon::vectorize
Definition argon_full.hpp:44
helpers::ArgonFor_t< simd::make_unsigned_t< Bool_t< simd::Vec128_t< ScalarType > > > > offset_type
Definition vector.hpp:86
Predicate< simd::Vec128_t< ScalarType > > argon_bool_type
Definition vector.hpp:85
One vector (or, for interleaved views, one group of Stride vectors) of a range, and how many of its l...
Definition tail.hpp:26
std::conditional_t< Stride==1, argon_type, std::array< argon_type, Stride > > vector_type
The value: a vector, or for interleaved views an array of one vector per channel.
Definition tail.hpp:30
Argon< ScalarType > argon_type
The type of one vector.
Definition tail.hpp:28
static constexpr size_t lanes
Definition tail.hpp:32
Partial(vector_type value, size_t remaining)
A value whose first remaining lanes are in the range (lanes or more means all of them).
Definition tail.hpp:37
vector_type * operator->()
Member access on the value.
Definition tail.hpp:44
Partial & operator=(vector_type value)
Replace the value; only the lanes inside the range are stored.
Definition tail.hpp:49
size_t count() const
The number of leading lanes inside the range.
Definition tail.hpp:55
size_t remaining() const
Definition tail.hpp:62
const vector_type * operator->() const
Member access on the value.
Definition tail.hpp:46
const vector_type & operator*() const
The value. Lanes past the end of the range are zero after a load.
Definition tail.hpp:42
predicate_type active() const
The lanes inside the range, as a predicate.
Definition tail.hpp:59
vector_type & operator*()
The value. Lanes past the end of the range are zero after a load.
Definition tail.hpp:40
bool full() const
Whether every lane is inside the range.
Definition tail.hpp:57
typename argon_type::argon_bool_type predicate_type
The predicate type for active().
Definition tail.hpp:31
load_store_tail(ScalarType *start, size_t count)
View count lanes (frames, for interleaved views) starting at start.
Definition tail.hpp:328
size_t size() const
The number of vectors, counting a final partial one.
Definition tail.hpp:333
load_tail(const ScalarType *start, size_t count)
View count lanes (frames, for interleaved views) starting at start.
Definition tail.hpp:206
size_t size() const
The number of vectors, counting a final partial one.
Definition tail.hpp:211
size_t size() const
The number of vectors, counting a final partial one.
Definition tail.hpp:263
store_tail(ScalarType *start, size_t count)
View count lanes (frames, for interleaved views) starting at start.
Definition tail.hpp:258
NEON loads and stores of a loop's final, partial vector, branching on its scalar lane count.
void store_first_n_interleaved(ScalarType *ptr, std::array< Argon< ScalarType >, Stride > value, size_t count)
Store the first count frames (0 < count < lanes) of value, one vector per channel,...
Definition first_n.hpp:164
std::array< Argon< ScalarType >, Stride > load_first_n_interleaved(const ScalarType *ptr, size_t count)
Load the first count frames (0 < count < lanes) of Stride interleaved elements at ptr,...
Definition first_n.hpp:137
Argon< ScalarType > load_first_n(const ScalarType *ptr, size_t count)
Load the first count lanes at ptr (0 < count < lanes); the rest are zero and not read.
Definition first_n.hpp:72
void store_first_n(ScalarType *ptr, Argon< ScalarType > value, size_t count)
Store the first count lanes of value to ptr (0 < count < lanes); the rest are not written.
Definition first_n.hpp:84
ace Argon< T > load(const T *ptr)
Load a quad-word vector from a pointer.
Definition argon.hpp:143
ace void store_interleaved(scalar_type *ptr, simd::MultiVector_t< intrinsic_type, stride > multi_vec)
Store a multi-vector to a location in memory with interleaving.
Definition store.hpp:32
Shared position-keeping for the tail iterators.
Definition tail.hpp:151
size_t index
The current lane (frame) of the range.
Definition tail.hpp:155
size_t remaining() const
Lanes left from the current position (not clamped to the lane count; FirstN saturates).
Definition tail.hpp:160
size_t count
The number of lanes (frames) in the range.
Definition tail.hpp:156
Partial< ScalarType, Stride > value_type
The value type of the iterator.
Definition tail.hpp:281
Iterator & operator++()
Store the current vector if it was modified, then move to the next vector.
Definition tail.hpp:298
std::ptrdiff_t difference_type
The difference type of the iterator.
Definition tail.hpp:282
Iterator over the vectors of the range; dereferences to a Partial.
Definition tail.hpp:173
std::ptrdiff_t difference_type
The difference type of the iterator.
Definition tail.hpp:175
value_type operator*() const
Load the current vector; lanes past the end of the range are zero and not read.
Definition tail.hpp:181
Partial< ScalarType, Stride > value_type
The value type of the iterator.
Definition tail.hpp:174
A view that loads data from a contiguous range into SIMD vectors.
Definition load.hpp:37
Definition ptr.hpp:74
Partial< ScalarType, Stride > value_type
The value type of the iterator.
Definition tail.hpp:228
Iterator & operator++()
Store the current vector's lanes inside the range, then move to the next vector.
Definition tail.hpp:238
std::ptrdiff_t difference_type
The difference type of the iterator.
Definition tail.hpp:229
Partial< ScalarType, Stride >::vector_type load_partial(const ScalarType *ptr, size_t count)
Load the first count lanes at ptr (all lanes when count >= lanes); the rest are zero and not read.
Definition tail.hpp:81
Argon< ScalarType >::offset_type frame_offsets()
Lane offsets {0, Stride, 2*Stride, ...}: where each frame's first element lies, in elements.
Definition tail.hpp:73
void store_partial(ScalarType *ptr, const typename Partial< ScalarType, Stride >::vector_type &value, size_t count)
Store the first count lanes of value to ptr (all lanes when count >= lanes).
Definition tail.hpp:115