Argon 0.1.0
Loading...
Searching...
No Matches
argon_half.hpp
1#pragma once
2#include "vector.hpp"
3
4#ifndef ARGON_PLATFORM_MVE
5#ifdef __clang__
6#define ace [[gnu::always_inline]] constexpr
7#else
8#define ace [[gnu::always_inline]] inline
9#endif
10template <typename ScalarType>
12
18template <typename ScalarType>
19 requires std::same_as<ScalarType, double> || std::same_as<ScalarType, uint64_t> || std::same_as<ScalarType, int64_t>
20class ArgonHalf<ScalarType> : public argon::Vector<neon::Vec64_t<ScalarType>> {
22
23 public:
24 using vector_type = neon::Vec64_t<ScalarType>;
25 using lane_type = const argon::Lane<vector_type>;
26
27 static_assert(neon::is_doubleword_v<vector_type>);
28
29 static constexpr size_t bytes = 8;
30 static constexpr size_t lanes = bytes / sizeof(ScalarType);
31
32 // The constructors argon::Vector declares, forwarded rather than inherited: an inherited constructor doesn't carry
33 // the base's [[gnu::always_inline]], so under -fno-inline-functions it compiles to an out-of-line call.
34
36 constexpr ArgonHalf() = default;
38 ace ArgonHalf(vector_type vector) : T{vector} {}
42 ace ArgonHalf(typename T::scalar_type scalar)
43 requires(!std::is_same_v<typename T::scalar_type, vector_type>)
44 : T{scalar} {}
45
46 ace ArgonHalf(argon::Lane<vector_type> lane) : T{lane} {}
48 template <size_t LaneIndex>
51 template <typename... ArgTypes>
52 requires(sizeof...(ArgTypes) > 1)
53 ace ArgonHalf(ArgTypes... args) : T{args...} {}
54
55 ace ArgonHalf(argon::Vector<vector_type> vec) : T{std::move(vec)} {};
56 ace ArgonHalf(const ArgonHalf&) = default;
57 ace ArgonHalf(ArgonHalf&&) = default;
58 ace ArgonHalf& operator=(const ArgonHalf&) = default;
59 ace ArgonHalf& operator=(ArgonHalf&&) = default;
60
63 ace static ArgonHalf<ScalarType> Create(uint64_t a) { return neon::create<vector_type>(a); }
64
67 template <typename NewScalarType>
69 return neon::reinterpret<neon::Vec64_t<NewScalarType>>(this->vec_);
70 }
71
74 ace ArgonHalf<ScalarType> TableLookup(ArgonHalf<ScalarType> idx) { return neon::table_lookup1(this->vec_, idx); }
75
80 return neon::table_extension1(this->vec_, b, idx);
81 }
82
85 template <size_t NumTables>
87 return TableExtension<NumTables>((vector_type*)b.data(), idx);
88 }
89
92 template <size_t NumTables>
94 static_assert(NumTables > 1 && NumTables < 5, "Table Extension can only be performed with 1, 2, 3, or 4 tables");
95
96 using multivec_type = neon::MultiVector_t<vector_type, NumTables>;
97
98 multivec_type multivector = *(multivec_type*)b;
99
100 if constexpr (NumTables == 2) {
101 return neon::table_extension2(this->vec_, multivector, idx);
102 } else if constexpr (NumTables == 3) {
103 return neon::table_extension3(this->vec_, multivector, idx);
104 } else if constexpr (NumTables == 4) {
105 return neon::table_extension4(this->vec_, multivector, idx);
106 }
107 }
108
111 template <typename U>
113#ifndef __ARM_FEATURE_MVE
114 // vcvt semantics (truncate, saturate, NaN -> 0) on every compiler: see helpers/float_fixups.hpp
115 if constexpr (std::is_same_v<ScalarType, float> && std::is_same_v<U, int32_t>) {
116 return argon::helpers::convert_to_int32(this->vec_);
117 } else if constexpr (std::is_same_v<ScalarType, float> && std::is_same_v<U, uint32_t>) {
118 return argon::helpers::convert_to_uint32(this->vec_);
119 }
120#endif
121 return neon::convert<typename neon::Vec64<U>::type>(this->vec_);
122 }
123
127 template <typename U, int fracbits>
128 requires(std::is_same_v<U, uint32_t> || std::is_same_v<U, int32_t> || std::is_same_v<U, float>)
130 if constexpr (std::is_same_v<U, float>) {
131 return neon::convert_n<fracbits>(this->vec_);
132 } else if constexpr (std::is_unsigned_v<U>) {
133 return argon::helpers::convert_n_to_uint32<fracbits>(this->vec_);
134 } else if constexpr (std::is_signed_v<U>) {
135 return argon::helpers::convert_n_to_int32<fracbits>(this->vec_);
136 }
137 }
138
140 ace Argon<ScalarType> CombineWith(ArgonHalf<ScalarType> high) const { return neon::combine(this->vec_, high); }
141
143 ace ArgonHalf<ScalarType> Reverse() const { return this->Reverse64bit(); }
144};
145
152template <argon::helpers::has_larger ScalarType>
153class ArgonHalf<ScalarType> : public argon::Vector<neon::Vec64_t<ScalarType>> {
154 using T = argon::Vector<neon::Vec64_t<ScalarType>>;
155 using next_larger = typename argon::helpers::NextLarger_t<ScalarType>;
156 using argon_next_larger = Argon<next_larger>;
157
158 public:
159 using vector_type = neon::Vec64_t<ScalarType>;
160 using lane_type = const argon::Lane<vector_type>;
161
162 static_assert(neon::is_doubleword_v<vector_type>);
163
164 static constexpr size_t bytes = 8;
165 static constexpr size_t lanes = bytes / sizeof(ScalarType);
166
167 // The constructors argon::Vector declares, forwarded rather than inherited: an inherited constructor doesn't carry
168 // the base's [[gnu::always_inline]], so under -fno-inline-functions it compiles to an out-of-line call.
169
171 constexpr ArgonHalf() = default;
173 ace ArgonHalf(vector_type vector) : T{vector} {}
175 ace ArgonHalf(typename T::scalar_type scalar) : T{scalar} {}
177 ace ArgonHalf(argon::Lane<vector_type> lane) : T{lane} {}
179 template <size_t LaneIndex>
182 template <typename... ArgTypes>
183 requires(sizeof...(ArgTypes) > 1)
184 ace ArgonHalf(ArgTypes... args) : T{args...} {}
185
188 ace static ArgonHalf<ScalarType> Create(uint64_t a) { return neon::create<vector_type>(a); }
189
192 template <typename NewScalarType>
194 return neon::reinterpret<neon::Vec64_t<NewScalarType>>(this->vec_);
195 }
196
198 ace argon_next_larger AddLong(ArgonHalf<ScalarType> b) const { return neon::add_long(this->vec_, b); }
199
201 ace argon_next_larger MultiplyLong(ArgonHalf<ScalarType> b) const { return neon::multiply_long(this->vec_, b); }
202
204 ace argon_next_larger MultiplyLong(ScalarType b) const { return neon::multiply_long(this->vec_, b); }
205
207 ace argon_next_larger MultiplyLong(lane_type b) const {
208 return neon::multiply_long_lane(this->vec_, b.vec(), b.lane());
209 }
210
212 ace argon_next_larger MultiplyAddLong(ArgonHalf<ScalarType> b) const {
213 return neon::multiply_add_long(this->vec_, b);
214 }
215
217 ace argon_next_larger MultiplyAddLong(ScalarType b) const { return neon::multiply_add_long(this->vec_, b); }
218
220 ace argon_next_larger MultiplyAddLong(lane_type b) const {
221 return neon::multiply_add_long_lane(this->vec_, b.vec(), b.lane());
222 }
223
226 requires(std::is_same_v<vector_type, int16x4_t> || std::is_same_v<vector_type, int32x2_t>)
227 {
228 return neon::multiply_double_saturate_long(this->vec_, b);
229 }
230
232 ace argon_next_larger MultiplyDoubleSaturateLong(ScalarType b) const
233 requires(std::is_same_v<vector_type, int16x4_t> || std::is_same_v<vector_type, int32x2_t>)
234 {
235 return neon::multiply_double_saturate_long(this->vec_, b);
236 }
237
239 ace argon_next_larger MultiplyDoubleSaturateLong(lane_type b) const
240 requires(std::is_same_v<vector_type, int16x4_t> || std::is_same_v<vector_type, int32x2_t>)
241 {
242 return neon::multiply_double_saturate_long_lane(this->vec_, b.vec(), b.lane());
243 }
244
247 requires(std::is_same_v<vector_type, int16x4_t> || std::is_same_v<vector_type, int32x2_t>)
248 {
249 return neon::multiply_double_add_saturate_long(this->vec_, b);
250 }
251
253 ace argon_next_larger MultiplyDoubleAddSaturateLong(ScalarType b) const
254 requires(std::is_same_v<vector_type, int16x4_t> || std::is_same_v<vector_type, int32x2_t>)
255 {
256 return neon::multiply_double_add_saturate_long(this->vec_, b);
257 }
258
260 ace argon_next_larger MultiplyDoubleAddSaturateLong(lane_type b) const
261 requires(std::is_same_v<vector_type, int16x4_t> || std::is_same_v<vector_type, int32x2_t>)
262 {
263 return neon::multiply_double_add_saturate_long_lane(this->vec_, b.vec(), b.lane());
264 }
265
267 ace argon_next_larger SubtractLong(ArgonHalf<ScalarType> b) const { return neon::subtract_long(this->vec_, b); }
268
270 ace argon_next_larger SubtractAbsoluteLong(ArgonHalf<ScalarType> b) const {
271 return neon::subtract_absolute_long(this->vec_, b);
272 }
273
277 ace ArgonHalf<next_larger> PairwiseAddLong() const { return neon::pairwise_add_long(this->vec_); }
278
283 template <typename SmallerType>
286 static_assert(std::is_same_v<SmallerType, argon::helpers::NextSmaller_t<ScalarType>>,
287 "PairwiseAddLong(b) requires b to be an ArgonHalf of the next-smaller element type");
288 return this->Add(b.PairwiseAddLong());
289 }
290
292 ace argon_next_larger Widen() const { return neon::move_long(this->vec_); }
293
296 template <size_t n>
297 ace argon_next_larger ShiftLeftLong() {
298 return neon::shift_left_long<n>(this->vec_);
299 }
300
303 ace ArgonHalf<ScalarType> TableLookup(ArgonHalf<ScalarType> idx) { return neon::table_lookup1(this->vec_, idx); }
304
309 return neon::table_extension1(this->vec_, b, idx);
310 }
311
314 template <size_t NumTables>
316 return TableExtension<NumTables>((vector_type*)b.data(), idx);
317 }
318
321 template <size_t NumTables>
323 static_assert(NumTables > 1 && NumTables < 5, "Table Extension can only be performed with 1, 2, 3, or 4 tables");
324
325 using multivec_type = neon::MultiVector_t<vector_type, NumTables>;
326
327 multivec_type multivector = *(multivec_type*)b;
328
329 if constexpr (NumTables == 2) {
330 return neon::table_extension2(this->vec_, multivector, idx);
331 } else if constexpr (NumTables == 3) {
332 return neon::table_extension3(this->vec_, multivector, idx);
333 } else if constexpr (NumTables == 4) {
334 return neon::table_extension4(this->vec_, multivector, idx);
335 }
336 }
337
340 template <typename U>
342#ifndef __ARM_FEATURE_MVE
343 // vcvt semantics (truncate, saturate, NaN -> 0) on every compiler: see helpers/float_fixups.hpp
344 if constexpr (std::is_same_v<ScalarType, float> && std::is_same_v<U, int32_t>) {
345 return argon::helpers::convert_to_int32(this->vec_);
346 } else if constexpr (std::is_same_v<ScalarType, float> && std::is_same_v<U, uint32_t>) {
347 return argon::helpers::convert_to_uint32(this->vec_);
348 }
349#endif
350 return neon::convert<typename neon::Vec64<U>::type>(this->vec_);
351 }
352
356 template <typename U, int fracbits>
357 requires(std::is_same_v<U, uint32_t> || std::is_same_v<U, int32_t> || std::is_same_v<U, float>)
359 if constexpr (std::is_same_v<U, float>) {
360 return neon::convert_n<fracbits>(this->vec_);
361 } else if constexpr (std::is_unsigned_v<U>) {
362 return argon::helpers::convert_n_to_uint32<fracbits>(this->vec_);
363 } else if constexpr (std::is_signed_v<U>) {
364 return argon::helpers::convert_n_to_int32<fracbits>(this->vec_);
365 }
366 }
367
369 ace Argon<ScalarType> CombineWith(ArgonHalf<ScalarType> high) const { return neon::combine(this->vec_, high); }
370
372 ace ArgonHalf<ScalarType> Reverse() const { return this->Reverse64bit(); }
373};
374
375template <class... ArgTypes>
376 requires(sizeof...(ArgTypes) > 1)
377ArgonHalf(ArgTypes...) -> ArgonHalf<std::tuple_element_t<0, std::tuple<ArgTypes...>>>;
378
379template <typename V>
380 requires std::is_scalar_v<V>
381ace ArgonHalf<V> operator+(const V a, const ArgonHalf<V> b) {
382 return b.Add(a);
383}
384
385template <typename V>
386 requires std::is_scalar_v<V>
387ace ArgonHalf<V> operator-(const V a, const ArgonHalf<V> b) {
388 return ArgonHalf<V>{a}.Subtract(b);
389}
390
391template <typename V>
392 requires std::is_scalar_v<V>
393ace ArgonHalf<V> operator*(const V a, const ArgonHalf<V> b) {
394 return b.Multiply(a);
395}
396
397template <typename V>
398 requires std::is_scalar_v<V>
399ace ArgonHalf<V> operator/(const V a, const ArgonHalf<V> b) {
400 return ArgonHalf<V>{a}.Divide(b);
401}
402
403namespace std {
404template <typename T>
405struct tuple_size<ArgonHalf<T>> {
406 static constexpr size_t value = ArgonHalf<T>::lanes;
407};
408template <size_t Index, typename T>
409struct tuple_element<Index, ArgonHalf<T>> {
410 static_assert(Index < ArgonHalf<T>::lanes);
412};
413} // namespace std
414#endif
415#undef ace
ace ArgonHalf< ScalarType > TableLookup(ArgonHalf< ScalarType > idx)
Look up each index lane in idx from this vector acting as a one-register table.
Definition argon_half.hpp:74
ace argon_next_larger MultiplyDoubleSaturateLong(ArgonHalf< ScalarType > b) const
Multiply, double, and saturate long: saturate(2 * this * b) widened (vector × vector).
Definition argon_half.hpp:225
ace ArgonHalf< next_larger > PairwiseAddLong() const
Pairwise add adjacent lanes and widen: produces lanes/2 wider-type results.
Definition argon_half.hpp:277
ace argon_next_larger MultiplyLong(ScalarType b) const
Multiply this by a scalar, widening each product (vector × scalar).
Definition argon_half.hpp:204
ace ArgonHalf< ScalarType > PairwiseAddLong(ArgonHalf< SmallerType > b) const
Pairwise add adjacent lanes of the next-smaller-type vector b and accumulate into this.
Definition argon_half.hpp:285
ace argon_next_larger MultiplyAddLong(ScalarType b) const
Multiply-accumulate long (vector × scalar).
Definition argon_half.hpp:217
ace ArgonHalf(ArgTypes... args)
Construct from one value per lane, each converted to the lane type.
Definition argon_half.hpp:53
ace argon_next_larger Widen() const
Zero-extend (widen) each lane to the next-larger element type.
Definition argon_half.hpp:292
ace argon_next_larger MultiplyLong(ArgonHalf< ScalarType > b) const
Multiply this and b, widening each product to the next-larger element type (vector × vector).
Definition argon_half.hpp:201
static ace ArgonHalf< ScalarType > Create(uint64_t a)
Create a new ArgonHalf from a raw 64-bit value.
Definition argon_half.hpp:63
ace ArgonHalf(typename T::scalar_type scalar)
Duplicate a scalar across every lane.
Definition argon_half.hpp:42
ace argon_next_larger MultiplyDoubleSaturateLong(lane_type b) const
Multiply, double, and saturate long (vector × lane).
Definition argon_half.hpp:239
ace argon_next_larger MultiplyDoubleAddSaturateLong(lane_type b) const
Multiply, double, add, and saturate long (vector × lane).
Definition argon_half.hpp:260
ace ArgonHalf< ScalarType > TableExtension(ArgonHalf< ScalarType > b, ArgonHalf< ScalarType > idx)
Extend a previous table-lookup result using this vector as an additional table (1 extension register)...
Definition argon_half.hpp:79
ace argon_next_larger SubtractAbsoluteLong(ArgonHalf< ScalarType > b) const
Absolute difference and widen: |this - b| widened to the next-larger element type.
Definition argon_half.hpp:270
ace argon_next_larger SubtractLong(ArgonHalf< ScalarType > b) const
Subtract b from this, widening each result to the next-larger element type.
Definition argon_half.hpp:267
ace argon_next_larger AddLong(ArgonHalf< ScalarType > b) const
Add this and b, widening each lane to the next-larger element type.
Definition argon_half.hpp:198
ace ArgonHalf< ScalarType > TableExtension(std::array< ArgonHalf< ScalarType >, NumTables > b, ArgonHalf< ScalarType > idx)
Multi-register table extension lookup (2–4 registers) supplied as std::array.
Definition argon_half.hpp:86
ace argon_next_larger MultiplyAddLong(ArgonHalf< ScalarType > b) const
Multiply this and b, widen each product, and prepare for long accumulation (vector × vector).
Definition argon_half.hpp:212
ace ArgonHalf(argon::Lane< vector_type > lane)
Duplicate the lane lane refers to across every lane.
Definition argon_half.hpp:46
ace ArgonHalf(vector_type vector)
Construct from the underlying intrinsic vector.
Definition argon_half.hpp:38
ace ArgonHalf(argon::ConstLane< LaneIndex, vector_type > lane)
Duplicate lane LaneIndex of lane's vector across every lane.
Definition argon_half.hpp:49
constexpr ArgonHalf()=default
Leave the lanes uninitialised; value-initialisation (ArgonHalf<T>{}) zeroes them.
ace argon_next_larger MultiplyDoubleAddSaturateLong(ScalarType b) const
Multiply, double, add, and saturate long (vector × scalar).
Definition argon_half.hpp:253
ace ArgonHalf(typename T::scalar_type scalar)
Duplicate a scalar across every lane.
Definition argon_half.hpp:175
ace ArgonHalf< ScalarType > TableExtension(vector_type *b, ArgonHalf< ScalarType > idx)
Multi-register table extension lookup (2–4 registers) from a raw pointer.
Definition argon_half.hpp:93
ace ArgonHalf< NewScalarType > As() const
reinterpret an ArgonHalf to a different type
Definition argon_half.hpp:68
ace argon_next_larger MultiplyDoubleSaturateLong(ScalarType b) const
Multiply, double, and saturate long (vector × scalar).
Definition argon_half.hpp:232
ace ArgonHalf< U > ConvertTo()
Convert each lane to a different element type.
Definition argon_half.hpp:112
ace Argon< ScalarType > CombineWith(ArgonHalf< ScalarType > high) const
Combine this (low) half with high to form a 128-bit Argon<ScalarType> vector.
Definition argon_half.hpp:140
ace argon_next_larger ShiftLeftLong()
Shift each lane left by n bits, widening the result to the next-larger element type.
Definition argon_half.hpp:297
ace argon_next_larger MultiplyLong(lane_type b) const
Multiply this by a lane value, widening each product (vector × lane).
Definition argon_half.hpp:207
ace ArgonHalf< U > ConvertTo()
Convert each lane to a different type using a fixed-point fractional bit count.
Definition argon_half.hpp:129
ace argon_next_larger MultiplyDoubleAddSaturateLong(ArgonHalf< ScalarType > b) const
Multiply, double, add, and saturate long: saturate(acc + 2 * this * b) widened (vector × vector).
Definition argon_half.hpp:246
ace ArgonHalf< ScalarType > Reverse() const
Reverse the order of all elements within this 64-bit vector.
Definition argon_half.hpp:143
ace argon_next_larger MultiplyAddLong(lane_type b) const
Multiply-accumulate long (vector × lane).
Definition argon_half.hpp:220
Definition argon_half.hpp:11
Definition argon_full.hpp:44
Represents a single lane of a SIMD vector with the lane index known at compile time.
Definition lane.hpp:35
Represents a single lane of a SIMD vector with a runtime-determined index.
Definition lane.hpp:107
ace int lane()
On ARM32, return the local lane index within the 64-bit half-register returned by vec().
Definition lane.hpp:153
ace neon::Vec64_t< scalar_type > vec()
On ARM32, return the 64-bit half-register that contains this lane.
Definition lane.hpp:141
Represents a SIMD vector with various operations.
Definition vector.hpp:53
simd::Scalar_t< neon::Vec64_t< ScalarType > > scalar_type
Definition vector.hpp:58
ace argon_type Add(argon_type b) const
Definition vector.hpp:387
constexpr bool has_smaller_v
Helper template to determine if a type has a smaller corresponding type.
Definition helpers.hpp:18
NextLarger< T >::type NextLarger_t
Helper alias to get the next larger type for a given type.
Definition helpers.hpp:75
Lane deconstruction feature.
Definition argon_full.hpp:1547