Argon 0.1.0
Loading...
Searching...
No Matches
argon.hpp
1#pragma once
2#include <array>
3#include <concepts>
4#include <cstddef>
5#include <ranges>
6#include <span>
7#include <type_traits>
8#include "argon/argon_full.hpp"
9#include "argon/argon_half.hpp"
10#include "argon/helpers/argon_for.hpp"
11#include "argon/store.hpp"
12#include "argon/vector.hpp"
13#include "arm_simd/helpers/multivector.hpp"
14#include "arm_simd/helpers/scalar.hpp"
15
16#ifdef __ARM_FEATURE_MVE
17#define simd mve
18#else
19#define simd neon
20#endif
21
22#ifdef ARGON_PLATFORM_SIMDE
23#define ace [[gnu::always_inline]] inline
24#elifdef __clang__
25#define ace [[gnu::always_inline]] constexpr
26#else
27#define ace [[gnu::always_inline]] inline
28#endif
29
30namespace argon {
31
37template <typename T, typename V>
38ace auto reinterpret(Vector<V> in) {
39 if constexpr (simd::is_quadword_v<V>) {
40 return Argon<T>{simd::reinterpret<typename Argon<T>::vector_type>(in.vec())};
41 } else if constexpr (simd::is_doubleword_v<V>) {
42 return ArgonHalf<T>{simd::reinterpret<typename ArgonHalf<T>::vector_type>(in.vec())};
43 }
44}
45
51template <typename argon_type, simd::is_vector_type V>
52ace argon_type reinterpret(V in) {
53 static_assert(!std::is_same_v<typename argon_type::vector_type, V>);
54 return argon_type{simd::reinterpret<typename argon_type::vector_type>(in)};
55}
56
62template <typename ScalarType, typename ArgonType>
63ace ScalarType bit_cast(ArgonType in) {
64 return in.template As<ScalarType>();
65}
66
74template <size_t lane, size_t stride, typename argon_type>
75ace static std::array<argon_type, stride> load_to_lane_interleaved(
76 simd::MultiVector_t<typename argon_type::vector_type, stride> multi,
77 typename argon_type::scalar_type const* ptr) {
78 return argon_type::LoadToLaneInterleaved(multi, ptr);
79}
80
82
83template <size_t lane, size_t stride, typename argon_type>
84ace static std::array<argon_type, stride> load_to_lane_interleaved(std::array<argon_type, stride> multi,
85 typename argon_type::scalar_type const* ptr) {
86 return argon_type::LoadToLaneInterleaved(multi, ptr);
87}
88
96template <typename argon_type>
97ace std::array<argon_type, 2> zip(argon_type a, argon_type b) {
98 return a.ZipWith(b);
99}
100
108template <typename argon_type>
109ace std::array<argon_type, 2> unzip(argon_type a, argon_type b) {
110 return a.UnzipWith(b);
111}
112
121template <typename argon_type>
122ace std::array<argon_type, 2> transpose(argon_type a, argon_type b) {
123 return a.TransposeWith(b);
124}
125
126#ifndef ARGON_PLATFORM_MVE
132template <typename T>
134 return simd::combine(low, high);
135}
136#endif
137
142template <typename T>
143ace Argon<T> load(const T* ptr) {
144 return Argon<T>::Load(ptr);
145}
146
151template <typename T>
152ace ArgonHalf<T> load_half(const T* ptr) {
153 return ArgonHalf<T>::Load(ptr);
154}
155
156namespace helpers {
160template <typename VectorType, typename ScalarType>
161using LaneFor_t = std::conditional_t<std::is_integral_v<ScalarType> && std::is_integral_v<simd::Scalar_t<VectorType>> &&
162 sizeof(ScalarType) == sizeof(simd::Scalar_t<VectorType>) &&
163 std::is_signed_v<ScalarType> == std::is_signed_v<simd::Scalar_t<VectorType>>,
164 simd::Scalar_t<VectorType>, ScalarType>;
165} // namespace helpers
166
168template <typename VectorType, typename ScalarType>
169using VectorFor_t = std::conditional_t<sizeof(VectorType) == 16, Argon<helpers::LaneFor_t<VectorType, ScalarType>>,
171
183template <typename VectorType, typename BranchType>
184 requires(BranchType::lanes == Predicate<VectorType>::lanes)
185ace BranchType ternary(Predicate<VectorType> condition, BranchType true_value, BranchType false_value) {
186#ifndef ARGON_PLATFORM_MVE
187 if constexpr (BranchType::extension_arithmetic) {
188 return condition.native() ? true_value.vec() : false_value.vec();
189 }
190#endif
191 return condition.Select(true_value, false_value);
192}
193
196template <typename MaskType, typename BranchType>
197 requires std::is_unsigned_v<typename MaskType::scalar_type> && (MaskType::lanes == BranchType::lanes) &&
198 (sizeof(typename MaskType::vector_type) == sizeof(typename BranchType::vector_type))
199ace BranchType ternary(MaskType mask, BranchType true_value, BranchType false_value) {
200 if constexpr (BranchType::extension_arithmetic) {
201 return mask.vec() ? true_value.vec() : false_value.vec();
202 } else {
203 return mask.Select(true_value, false_value);
204 }
205}
206
208template <typename MaskType, typename ValueType>
209 requires std::is_unsigned_v<typename MaskType::scalar_type> && lane_scalar<ValueType> &&
210 (sizeof(ValueType) == sizeof(typename MaskType::scalar_type))
211ace auto ternary(MaskType mask, ValueType true_value, ValueType false_value) {
213 return ternary(mask, result_type{true_value}, result_type{false_value});
214}
215
216template <typename BranchType, typename CondType>
217 requires simd::is_vector_type<CondType> && (sizeof(CondType) == sizeof(BranchType))
218ace BranchType ternary(CondType condition, BranchType true_value, BranchType false_value) {
219 if constexpr (ARGON_USE_COMPILER_EXTENSIONS) {
220 return condition ? true_value.vec() : false_value.vec();
221 } else {
222 return helpers::ArgonFor_t<CondType>{condition}.Select(true_value, false_value);
223 }
224}
225
227template <typename VectorType, typename ValueType>
228 requires lane_scalar<ValueType> && (sizeof(ValueType) == sizeof(simd::Scalar_t<VectorType>))
229ace auto ternary(Predicate<VectorType> condition, ValueType true_value, ValueType false_value) {
230 using result_type = VectorFor_t<VectorType, ValueType>;
231 return ternary(condition, result_type{true_value}, result_type{false_value});
232}
233
239template <typename VectorType, typename ValueType>
240 requires(ValueType::lanes == Predicate<VectorType>::lanes)
242 using predicate_type = Predicate<VectorType>;
243 using scalar_type = typename ValueType::scalar_type;
244
245 public:
247 ace CondMonad(predicate_type condition, ValueType value) : condition_{condition}, value_{value} {}
248
250 ace predicate_type condition() const { return condition_; }
251
253 ace ValueType value() const { return value_; }
254
256 ace CondMonad else_if_(predicate_type new_condition, ValueType new_value) const {
257 return {condition_ | new_condition, condition_.Select(value_, new_value)};
258 }
259
261 ace CondMonad else_if_(predicate_type new_condition, scalar_type new_value) const {
262 return else_if_(new_condition, ValueType{new_value});
263 }
264
266 template <std::invocable FunctionType>
267 ace CondMonad else_if_(predicate_type new_condition, FunctionType func) const {
268 return else_if_(new_condition, ValueType{func()});
269 }
270
272 ace ValueType else_(ValueType new_value) const { return condition_.Select(value_, new_value); }
273
275 ace ValueType else_(scalar_type new_value) const { return else_(ValueType{new_value}); }
276
278 template <std::invocable FunctionType>
279 ace ValueType else_(FunctionType func) const {
280 return else_(ValueType{func()});
281 }
282
283 private:
284 predicate_type condition_;
285 ValueType value_;
286};
287
290template <typename VectorType, typename ValueType>
291 requires(ValueType::lanes == Predicate<VectorType>::lanes)
293 return {condition, value};
294}
295
297template <typename VectorType, typename ScalarType>
298 requires lane_scalar<ScalarType> && (sizeof(ScalarType) == sizeof(simd::Scalar_t<VectorType>))
299ace auto if_(Predicate<VectorType> condition, ScalarType value) {
300 using value_type = VectorFor_t<VectorType, ScalarType>;
301 return CondMonad<VectorType, value_type>{condition, value_type{value}};
302}
303
305template <typename VectorType, std::invocable FunctionType>
306ace auto if_(Predicate<VectorType> condition, FunctionType func) {
307 return if_(condition, func());
308}
309
310} // namespace argon
311
312#undef ace
313#undef simd
Definition argon_half.hpp:11
Definition argon_full.hpp:44
The result of if_(...) and each else_if_(...): a value chosen per lane by a chain of conditions.
Definition argon.hpp:241
ace CondMonad(predicate_type condition, ValueType value)
Start a chain: value where condition holds.
Definition argon.hpp:247
ace ValueType else_(scalar_type new_value) const
new_value in the lanes no condition claimed; ends the chain.
Definition argon.hpp:275
ace CondMonad else_if_(predicate_type new_condition, FunctionType func) const
new_value in the lanes where new_condition holds and no earlier condition did.
Definition argon.hpp:267
ace ValueType else_(ValueType new_value) const
new_value in the lanes no condition claimed; ends the chain.
Definition argon.hpp:272
ace predicate_type condition() const
The lanes some condition in the chain has claimed so far.
Definition argon.hpp:250
ace CondMonad else_if_(predicate_type new_condition, ValueType new_value) const
new_value in the lanes where new_condition holds and no earlier condition did.
Definition argon.hpp:256
ace CondMonad else_if_(predicate_type new_condition, scalar_type new_value) const
new_value in the lanes where new_condition holds and no earlier condition did.
Definition argon.hpp:261
ace ValueType value() const
The values chosen so far; lanes no condition has claimed are unspecified until else_.
Definition argon.hpp:253
ace ValueType else_(FunctionType func) const
new_value in the lanes no condition claimed; ends the chain.
Definition argon.hpp:279
A per-lane boolean for vectors of type VectorType, as returned by comparisons.
Definition predicate.hpp:39
ace storage_type native() const
The native predicate representation: a mask vector on NEON, an mve_pred16_t on MVE.
Definition predicate.hpp:133
ace ArgType Select(ArgType true_value, ArgType false_value) const
Choose each lane from true_value where the predicate is active, and from false_value otherwise.
Definition predicate.hpp:189
static constexpr size_t lanes
The number of lanes the predicate covers.
Definition predicate.hpp:54
Represents a SIMD vector with various operations.
Definition vector.hpp:53
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
A scalar type a vector lane can hold: an arithmetic type, or float16_t where half-float vectors exist...
Definition lane_scalar.hpp:31
Concept to check if a type is a vector type (either double-word or quad-word).
Definition concepts.hpp:109
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
std::conditional_t< std::is_integral_v< ScalarType > &&std::is_integral_v< simd::Scalar_t< VectorType > > && sizeof(ScalarType)==sizeof(simd::Scalar_t< VectorType >) && std::is_signed_v< ScalarType >==std::is_signed_v< simd::Scalar_t< VectorType > >, simd::Scalar_t< VectorType >, ScalarType > LaneFor_t
The lane type for a ScalarType value used alongside VectorType's lanes.
Definition argon.hpp:161
A 128-bit SIMD vector wrapping a scalar type, providing arithmetic, logical, and data-movement operat...
Definition argon.hpp:30
ace Argon< T > combine(ArgonHalf< T > low, ArgonHalf< T > high)
Combine two double-word vectors into a single quad-word vector.
Definition argon.hpp:133
ace Argon< T > load(const T *ptr)
Load a quad-word vector from a pointer.
Definition argon.hpp:143
ace ScalarType bit_cast(ArgonType in)
Reinterpret a vector of one type to another.
Definition argon.hpp:63
ace std::array< argon_type, 2 > zip(argon_type a, argon_type b)
Zip two vectors together into a pair of output vectors.
Definition argon.hpp:97
std::conditional_t< sizeof(VectorType)==16, Argon< helpers::LaneFor_t< VectorType, ScalarType > >, ArgonHalf< helpers::LaneFor_t< VectorType, ScalarType > > > VectorFor_t
The Argon type holding ScalarType values with the same width as VectorType (Argon or ArgonHalf).
Definition argon.hpp:169
ace std::array< argon_type, 2 > unzip(argon_type a, argon_type b)
Unzip vectors into a pair of output vectors.
Definition argon.hpp:109
ace CondMonad< VectorType, ValueType > if_(Predicate< VectorType > condition, ValueType value)
Start a per-lane if / else-if / else chain: value in the lanes where condition holds.
Definition argon.hpp:292
ace ArgonHalf< T > load_half(const T *ptr)
Load a double-word vector from a pointer.
Definition argon.hpp:152
ace std::array< argon_type, 2 > transpose(argon_type a, argon_type b)
Treats the lanes of its inputs as elements of a 2x2 matrix and transposes the matrices.
Definition argon.hpp:122
ace BranchType ternary(Predicate< VectorType > condition, BranchType true_value, BranchType false_value)
Create a new vector depending on the result of a conditional.
Definition argon.hpp:185
ace auto reinterpret(Vector< V > in)
Reinterpret a vector of one type to another.
Definition argon.hpp:38