Argon 0.1.0
Loading...
Searching...
No Matches
for_each.hpp
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <array>
4#include <cstddef>
5#include <type_traits>
6#include "argon/argon_full.hpp"
7#include "argon/store.hpp"
9
13
14namespace argon::vectorize {
15
22template <typename ScalarType, bool full>
23class Step {
24 public:
27 static constexpr std::ptrdiff_t lanes = argon_type::lanes;
28
29 [[gnu::always_inline]] Step(std::ptrdiff_t index, std::ptrdiff_t remaining) : index_{index}, remaining_{remaining} {}
30
32 [[gnu::always_inline]] std::ptrdiff_t index() const { return index_; }
33
35 [[gnu::always_inline]] std::ptrdiff_t count() const {
36 if constexpr (full) {
37 return lanes;
38 } else {
39 return remaining_ < lanes ? remaining_ : lanes;
40 }
41 }
42
45 [[gnu::always_inline]] predicate_type active() const {
46 if constexpr (full) {
47 return predicate_type::True();
48 } else {
49 return predicate_type::FirstN(static_cast<size_t>(remaining_));
50 }
51 }
52
54 [[gnu::always_inline]] argon_type Load(const ScalarType* base) const {
55 if constexpr (full) {
56 return argon_type::Load(base + index_);
57 } else {
58#ifdef ARGON_PLATFORM_MVE
59 return argon_type::Load(base + index_, active());
60#else
61 return detail::load_first_n(base + index_, static_cast<size_t>(remaining_));
62#endif
63 }
64 }
65
67 [[gnu::always_inline]] void Store(ScalarType* base, argon_type value) const {
68 if constexpr (full) {
69 value.StoreTo(base + index_);
70 } else {
71#ifdef ARGON_PLATFORM_MVE
72 value.StoreTo(base + index_, active());
73#else
74 detail::store_first_n(base + index_, value, static_cast<size_t>(remaining_));
75#endif
76 }
77 }
78
80 template <typename N>
82 [[gnu::always_inline]] argon_type LoadWiden(const N* base) const {
83 if constexpr (full) {
84 return argon_type::LoadWiden(base + index_);
85 } else {
86#ifdef ARGON_PLATFORM_MVE
87 return argon_type::LoadWiden(base + index_, active());
88#else
89 return detail::load_first_n_widen<N, ScalarType>(base + index_, static_cast<size_t>(remaining_));
90#endif
91 }
92 }
93
95 template <typename N>
97 [[gnu::always_inline]] void StoreNarrow(N* base, argon_type value) const {
98 if constexpr (full) {
99 value.StoreNarrow(base + index_);
100 } else {
101#ifdef ARGON_PLATFORM_MVE
102 value.StoreNarrow(base + index_, active());
103#else
104 detail::store_first_n_narrow<N, ScalarType>(base + index_, value, static_cast<size_t>(remaining_));
105#endif
106 }
107 }
108
109 private:
110 std::ptrdiff_t index_;
111 std::ptrdiff_t remaining_;
112};
113
131template <typename ScalarType, typename Body>
132[[gnu::always_inline]] inline void for_each(std::ptrdiff_t n, Body&& body) {
133 constexpr std::ptrdiff_t lanes = Argon<ScalarType>::lanes;
134#ifdef ARGON_PLATFORM_MVE
135 for (std::ptrdiff_t i = 0; i < n; i += lanes) {
136 body(Step<ScalarType, false>{i, n - i});
137 }
138#else
139 std::ptrdiff_t i = 0;
140 for (; i + lanes <= n; i += lanes) {
141 body(Step<ScalarType, true>{i, lanes});
142 }
143 if (i < n) {
144 body(Step<ScalarType, false>{i, n - i});
145 }
146#endif
147}
148
155template <typename ScalarType, size_t Stride, bool full>
156class InterleavedStep {
157 public:
159 using frame_type = std::array<argon_type, Stride>;
161 static constexpr std::ptrdiff_t lanes = argon_type::lanes;
162
163 [[gnu::always_inline]] InterleavedStep(std::ptrdiff_t index, std::ptrdiff_t remaining)
164 : index_{index}, remaining_{remaining} {}
165
167 [[gnu::always_inline]] std::ptrdiff_t index() const { return index_; }
168
170 [[gnu::always_inline]] std::ptrdiff_t count() const {
171 if constexpr (full) {
172 return lanes;
173 } else {
174 return remaining_ < lanes ? remaining_ : lanes;
175 }
176 }
177
179 [[gnu::always_inline]] predicate_type active() const {
180 if constexpr (full) {
181 return predicate_type::True();
182 } else {
183 return predicate_type::FirstN(static_cast<size_t>(remaining_));
184 }
185 }
186
189 [[gnu::always_inline]] frame_type Load(const ScalarType* base) const {
190 const ScalarType* ptr = base + index_ * std::ptrdiff_t{Stride};
191 if constexpr (full) {
192 return argon_type::template LoadInterleaved<Stride>(ptr);
193 } else {
194#ifdef ARGON_PLATFORM_MVE
195 const auto offsets = Offsets();
196 const auto mask = active();
197 frame_type out;
198 for (size_t channel = 0; channel < Stride; ++channel) {
199 out.data()[channel] = argon_type::LoadGatherOffsetIndex(ptr + channel, offsets, mask);
200 }
201 return out;
202#else
203 return detail::load_first_n_interleaved<Stride>(ptr, static_cast<size_t>(remaining_));
204#endif
205 }
206 }
207
210 [[gnu::always_inline]] void Store(ScalarType* base, const frame_type& channels) const {
211 ScalarType* ptr = base + index_ * std::ptrdiff_t{Stride};
212 if constexpr (full) {
213 argon::store_interleaved(ptr, channels);
214 } else {
215#ifdef ARGON_PLATFORM_MVE
216 const auto offsets = Offsets();
217 const auto mask = active();
218 frame_type vectors = channels; // the const data() isn't always-inline
219 for (size_t channel = 0; channel < Stride; ++channel) {
220 vectors.data()[channel].StoreScatterOffsetIndex(ptr + channel, offsets, mask);
221 }
222#else
223 detail::store_first_n_interleaved<Stride>(ptr, channels, static_cast<size_t>(remaining_));
224#endif
225 }
226 }
227
228 private:
230 [[gnu::always_inline]] static typename argon_type::offset_type Offsets() {
231 using offset_type = typename argon_type::offset_type;
232 return offset_type::Iota(0) * static_cast<typename offset_type::scalar_type>(Stride);
233 }
234
235 std::ptrdiff_t index_;
236 std::ptrdiff_t remaining_;
237};
238
256template <typename ScalarType, size_t Stride, typename Body>
257 requires(Stride >= 2 && Stride <= 4)
258[[gnu::always_inline]] inline void for_each_interleaved(std::ptrdiff_t frames, Body&& body) {
259 constexpr std::ptrdiff_t lanes = Argon<ScalarType>::lanes;
260#ifdef ARGON_PLATFORM_MVE
261 if constexpr (Stride == 3) {
262 for (std::ptrdiff_t i = 0; i < frames; i += lanes) {
264 }
265 return;
266 }
267#endif
268 std::ptrdiff_t i = 0;
269 for (; i + lanes <= frames; i += lanes) {
271 }
272 if (i < frames) {
274 }
275}
276
277} // namespace argon::vectorize
Definition argon_full.hpp:44
static ace Argon< S > LoadWiden(const N *ptr)
Load lanes elements of the narrower integer type N and extend each to a lane. MVE: vldrb / vldrh.
Definition argon_full.hpp:648
void StoreNarrow(N *ptr) const
Store the low bits of each lane as the narrower integer type N. MVE: vstrb / vstrh.
Definition argon_full.hpp:686
static ace argon_type Load(const scalar_type *ptr)
Definition vector.hpp:1095
ace void StoreTo(scalar_type *ptr) const
Store the vector to a pointer.
Definition vector.hpp:1356
static ace argon_type LoadGatherOffsetIndex(const scalar_type *base, offset_type offsets)
Definition vector.hpp:1152
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 step of vectorize::for_each_interleaved: the frames [index(), index() + count()),...
Definition for_each.hpp:156
typename argon_type::argon_bool_type predicate_type
The predicate type of active().
Definition for_each.hpp:160
std::array< argon_type, Stride > frame_type
One vector per channel.
Definition for_each.hpp:159
predicate_type active() const
The lanes (frames) inside the range.
Definition for_each.hpp:179
frame_type Load(const ScalarType *base) const
Load this step's frames of the interleaved array at base, one vector per channel; lanes past the end ...
Definition for_each.hpp:189
void Store(ScalarType *base, const frame_type &channels) const
Store one vector per channel, interleaved, to this step's frames of the array at base; frames past th...
Definition for_each.hpp:210
std::ptrdiff_t count() const
The number of this step's frames inside the range.
Definition for_each.hpp:170
Argon< ScalarType > argon_type
The vector type of one channel.
Definition for_each.hpp:158
std::ptrdiff_t index() const
The index of this step's first frame.
Definition for_each.hpp:167
static constexpr std::ptrdiff_t lanes
Frames per step.
Definition for_each.hpp:161
One step of vectorize::for_each: the elements [index(), index() + count()) of the range.
Definition for_each.hpp:23
void Store(ScalarType *base, argon_type value) const
Store value to this step's elements of the array at base; out-of-range lanes are not written.
Definition for_each.hpp:67
static constexpr std::ptrdiff_t lanes
Elements per step.
Definition for_each.hpp:27
std::ptrdiff_t index() const
The index of this step's first element.
Definition for_each.hpp:32
Argon< ScalarType > argon_type
The vector type of the loop.
Definition for_each.hpp:25
predicate_type active() const
The lanes inside the range.
Definition for_each.hpp:45
void StoreNarrow(N *base, argon_type value) const
Store the low bits of value to this step's elements of an array of the narrower integer type N.
Definition for_each.hpp:97
std::ptrdiff_t count() const
The number of this step's lanes inside the range.
Definition for_each.hpp:35
typename argon_type::argon_bool_type predicate_type
The predicate type of active().
Definition for_each.hpp:26
argon_type Load(const ScalarType *base) const
Load this step's elements of the array at base; out-of-range lanes are zero and not read.
Definition for_each.hpp:54
argon_type LoadWiden(const N *base) const
Load this step's elements of an array of the narrower integer type N, extending them to lanes.
Definition for_each.hpp:82
Whether lanes of integer type W can be loaded from (and stored as) the narrower integer type N,...
Definition argon_full.hpp:34
NEON loads and stores of a loop's final, partial vector, branching on its scalar lane count.
Argon< S > load_first_n_widen(const N *ptr, size_t count)
Load the first count (0 < count < lanes) elements of the narrower integer type N at ptr,...
Definition first_n.hpp:186
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_narrow(N *ptr, Argon< S > value, size_t count)
Store the low bits of the first count (0 < count < lanes) lanes of value as the narrower integer type...
Definition first_n.hpp:207
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
void for_each(std::ptrdiff_t n, Body &&body)
Call body with a Step for each vector's worth of the elements [0, n), the final partial vector includ...
Definition for_each.hpp:132
void for_each_interleaved(std::ptrdiff_t frames, Body &&body)
Call body with an InterleavedStep for each vector's worth of the frames frames of Stride interleaved ...
Definition for_each.hpp:258
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
Definition ptr.hpp:74