Argon 0.1.0
Loading...
Searching...
No Matches
first_n.hpp
Go to the documentation of this file.
1#pragma once
2#include <array>
3#include <cstddef>
4#include <type_traits>
5#include "argon/argon_full.hpp"
6#include "argon/argon_half.hpp"
7#include "argon/features.h"
8#include "argon/store.hpp"
9
25
26#ifndef ARGON_PLATFORM_MVE
27namespace argon::vectorize::detail {
28
34template <typename ScalarType>
35[[gnu::always_inline]] inline ScalarType* opaque_address(ScalarType* ptr) {
36#if defined(__arm__) && !defined(__aarch64__) && defined(__GNUC__)
37 asm("" : "+r"(ptr));
38#endif
39 return ptr;
40}
41
45template <size_t Lane, typename ScalarType>
47 const ScalarType* ptr,
48 size_t first,
49 size_t count) {
50 if constexpr (Lane + 1 < ArgonHalf<ScalarType>::lanes) {
51 if (first + Lane < count) {
52 return load_lanes<Lane + 1>(v.template LoadToLane<Lane>(ptr + Lane), ptr, first, count);
53 }
54 }
55 return v;
56}
57
60template <size_t Lane, typename ScalarType>
61[[gnu::always_inline]] inline void store_lanes(ArgonHalf<ScalarType> v, ScalarType* ptr, size_t first, size_t count) {
62 if constexpr (Lane + 1 < ArgonHalf<ScalarType>::lanes) {
63 if (first + Lane < count) {
64 v.template StoreLaneTo<Lane>(opaque_address(ptr + Lane));
65 store_lanes<Lane + 1>(v, ptr, first, count);
66 }
67 }
68}
69
71template <typename ScalarType>
72[[gnu::always_inline]] inline Argon<ScalarType> load_first_n(const ScalarType* ptr, size_t count) {
73 using half_type = ArgonHalf<ScalarType>;
74 constexpr size_t half = half_type::lanes;
75 const half_type zero{ScalarType{0}};
76 if (count >= half) {
77 return {half_type::Load(ptr), load_lanes<0>(zero, ptr + half, half, count)};
78 }
79 return {load_lanes<0>(zero, ptr, 0, count), zero};
80}
81
83template <typename ScalarType>
84[[gnu::always_inline]] inline void store_first_n(ScalarType* ptr, Argon<ScalarType> value, size_t count) {
85 constexpr size_t half = ArgonHalf<ScalarType>::lanes;
86 if (count >= half) {
87 value.GetLow().StoreTo(ptr);
88 store_lanes<0>(value.GetHigh(), ptr + half, half, count);
89 } else {
90 store_lanes<0>(value.GetLow(), ptr, 0, count);
91 }
92}
93
95template <size_t Stride, typename ScalarType>
96using half_multi_type = neon::MultiVector_t<typename ArgonHalf<ScalarType>::vector_type, Stride>;
97
100template <size_t Lane, size_t Stride, typename ScalarType>
102 const ScalarType* ptr,
103 size_t first,
104 size_t count) {
105 using half_type = ArgonHalf<ScalarType>;
106 if constexpr (Lane + 1 < half_type::lanes) {
107 if (first + Lane < count) {
108 auto loaded = half_type::template LoadToLaneInterleaved<Lane, Stride>(multi, ptr + Lane * Stride);
109 for (size_t channel = 0; channel < Stride; ++channel)
110 multi.val[channel] = loaded.data()[channel].vec();
111 return load_frames<Lane + 1, Stride>(multi, ptr, first, count);
112 }
113 }
114 return multi;
115}
116
119template <size_t Lane, size_t Stride, typename ScalarType>
120[[gnu::always_inline]] inline void store_frames(half_multi_type<Stride, ScalarType> multi,
121 ScalarType* ptr,
122 size_t first,
123 size_t count) {
124 using half_type = ArgonHalf<ScalarType>;
125 if constexpr (Lane + 1 < half_type::lanes) {
126 if (first + Lane < count) {
128 multi);
129 store_frames<Lane + 1, Stride>(multi, ptr, first, count);
130 }
131 }
132}
133
136template <size_t Stride, typename ScalarType>
137[[gnu::always_inline]] inline std::array<Argon<ScalarType>, Stride> load_first_n_interleaved(const ScalarType* ptr,
138 size_t count) {
139 using half_type = ArgonHalf<ScalarType>;
140 constexpr size_t half = half_type::lanes;
142 for (size_t channel = 0; channel < Stride; ++channel)
143 zero.val[channel] = half_type{ScalarType{0}}.vec();
144 half_multi_type<Stride, ScalarType> low = zero;
145 half_multi_type<Stride, ScalarType> high = zero;
146 if (count >= half) {
147 auto whole = half_type::template LoadInterleaved<Stride>(ptr);
148 for (size_t channel = 0; channel < Stride; ++channel)
149 low.val[channel] = whole.data()[channel].vec();
150 high = load_frames<0, Stride>(zero, ptr + half * Stride, half, count);
151 } else {
152 low = load_frames<0, Stride>(zero, ptr, 0, count);
153 }
154 std::array<Argon<ScalarType>, Stride> out;
155 for (size_t channel = 0; channel < Stride; ++channel) {
156 out.data()[channel] = Argon<ScalarType>{half_type{low.val[channel]}, half_type{high.val[channel]}};
157 }
158 return out;
159}
160
163template <size_t Stride, typename ScalarType>
164[[gnu::always_inline]] inline void store_first_n_interleaved(ScalarType* ptr,
165 std::array<Argon<ScalarType>, Stride> value,
166 size_t count) {
167 using half_type = ArgonHalf<ScalarType>;
168 constexpr size_t half = half_type::lanes;
171 for (size_t channel = 0; channel < Stride; ++channel) {
172 low.val[channel] = value.data()[channel].GetLow().vec();
173 high.val[channel] = value.data()[channel].GetHigh().vec();
174 }
175 if (count >= half) {
177 store_frames<0, Stride>(high, ptr + half * Stride, half, count);
178 } else {
179 store_frames<0, Stride>(low, ptr, 0, count);
180 }
181}
182
185template <typename N, typename S>
186[[gnu::always_inline]] inline Argon<S> load_first_n_widen(const N* ptr, size_t count) {
187 constexpr size_t lanes = Argon<S>::lanes;
188 if constexpr (sizeof(S) == 2 * sizeof(N)) {
189 // the narrow lanes fill a doubleword: load them there one by one, then widen with vmovl as a whole load would
190 const auto narrow = load_lanes<0>(ArgonHalf<N>{N{0}}, ptr, 0, count);
191 const auto wide = neon::move_long(narrow.vec());
192 return Argon<std::conditional_t<std::is_signed_v<N>, std::make_signed_t<S>, std::make_unsigned_t<S>>>{wide}
193 .template As<S>();
194 } else {
195 std::array<S, lanes> out{};
196 for (size_t i = 0; i < lanes; ++i) {
197 if (i < count)
198 out.data()[i] = static_cast<S>(ptr[i]);
199 }
200 return Argon<S>::Load(out.data());
201 }
202}
203
206template <typename N, typename S>
207[[gnu::always_inline]] inline void store_first_n_narrow(N* ptr, Argon<S> value, size_t count) {
208 constexpr size_t lanes = Argon<S>::lanes;
209 if constexpr (sizeof(S) == 2 * sizeof(N)) {
210 // narrow with vmovn as a whole store would, then store the doubleword's lanes one by one
211 using SameSign = std::conditional_t<std::is_signed_v<N>, std::make_signed_t<S>, std::make_unsigned_t<S>>;
212 store_lanes<0>(ArgonHalf<N>{neon::move_narrow(value.template As<SameSign>().vec())}, ptr, 0, count);
213 } else {
214 auto values = value.to_array();
215 for (size_t i = 0; i < lanes; ++i) {
216 if (i < count)
217 ptr[i] = static_cast<N>(values.data()[i]);
218 }
219 }
220}
221
222} // namespace argon::vectorize::detail
223#endif
Definition argon_half.hpp:11
Definition argon_full.hpp:44
ace ArgonHalf< ScalarType > GetLow() const
Return the lower 64 bits as an ArgonHalf.
Definition argon_full.hpp:268
ace ArgonHalf< ScalarType > GetHigh() const
Return the upper 64 bits as an ArgonHalf.
Definition argon_full.hpp:266
Represents a single lane of a SIMD vector with a runtime-determined index.
Definition lane.hpp:107
constexpr VectorType vec() const
Get the underlying SIMD vector.
Definition vector.hpp:312
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
ace std::array< scalar_type, lanes > to_array() const
Convert the vector to an array of scalar values.
Definition vector.hpp:319
Header file for SIMD features and platform detection.
ArgonHalf< ScalarType > load_lanes(ArgonHalf< ScalarType > v, const ScalarType *ptr, size_t first, size_t count)
Load lanes [Lane, half - 1) of the doubleword v from ptr + lane, while first + lane < count.
Definition first_n.hpp:46
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
half_multi_type< Stride, ScalarType > load_frames(half_multi_type< Stride, ScalarType > multi, const ScalarType *ptr, size_t first, size_t count)
Load frames [Lane, half - 1) of multi from ptr + lane * Stride, one vld2-vld4 to a lane each,...
Definition first_n.hpp:101
void store_frames(half_multi_type< Stride, ScalarType > multi, ScalarType *ptr, size_t first, size_t count)
Store frames [Lane, half - 1) of multi to ptr + lane * Stride, one vst2-vst4 from a lane each,...
Definition first_n.hpp:120
neon::MultiVector_t< typename ArgonHalf< ScalarType >::vector_type, Stride > half_multi_type
The channels of interleaved data as doublewords, the operand of the structured loads and stores.
Definition first_n.hpp:96
void store_lanes(ArgonHalf< ScalarType > v, ScalarType *ptr, size_t first, size_t count)
Store lanes [Lane, half - 1) of the doubleword v to ptr + lane, while first + lane < count (see load_...
Definition first_n.hpp:61
ScalarType * opaque_address(ScalarType *ptr)
ptr, as an address the compiler can't relate to its neighbours.
Definition first_n.hpp:35
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
ace void store_lane_interleaved(scalar_type *ptr, std::array< argon_type, stride > multi_vec)
Store a lane of an array of vectors to a location in memory with interleaving.
Definition store.hpp:203
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