Argon 0.1.0
Loading...
Searching...
No Matches
lane.hpp
1#pragma once
2#include <cstddef>
3#include <type_traits>
4#include "arm_simd.hpp"
5#include "arm_simd/helpers/scalar.hpp"
6#include "arm_simd/helpers/vec64.hpp"
7#include "features.h"
8#include "helpers/argon_for.hpp"
9
10#ifdef __ARM_FEATURE_MVE
11#define simd mve
12#else
13#define simd neon
14#endif
15
16#ifdef ARGON_PLATFORM_SIMDE
17#define ace [[gnu::always_inline]] inline
18#elifdef __clang__
19#define ace [[gnu::always_inline]] constexpr
20#else
21#define ace [[gnu::always_inline]] inline
22#endif
23
24namespace argon {
25template <typename T>
26class Vector;
27
34template <size_t LaneIndex, typename VectorType>
35class ConstLane {
36 using vector_type = std::remove_const_t<VectorType>; // a const vector's lanes are read-only
37 using scalar_type = simd::Scalar_t<vector_type>;
39 using argon_type = helpers::ArgonFor_t<vector_type>;
40
41 public:
43 ace ConstLane(VectorType& vec) : vec_{vec} {}
45 ace operator scalar_type() const { return Get(); }
47 ace argon_type operator=(const scalar_type b) { return vec_ = Set(b); }
50 ace argon_type Load(const scalar_type* ptr) {
51#ifdef ARGON_PLATFORM_MVE
52 return vec_ = Set(*ptr);
53#else
54 if constexpr (simd::is_quadword_v<vector_type>) {
55 return vec_ = argon_type{simd::load1_lane_quad<LaneIndex>(ptr, vec_)};
56 } else {
57 return vec_ = argon_type{simd::load1_lane<LaneIndex>(ptr, vec_)};
58 }
59#endif
60 }
61
62 ace argon_type Set(const scalar_type b) { return argon_type{simd::set_lane<LaneIndex>(b, vec_)}; }
64 ace scalar_type Get() const { return simd::get_lane<LaneIndex>(vec_); }
65
66#if __ARM_ARCH >= 8
68 ace vector_type vec() { return vec_; }
70 ace const int lane() { return LaneIndex; }
71#else
73 ace neon::Vec64_t<scalar_type> vec() {
74 if constexpr (simd::is_doubleword_v<vector_type>) {
75 return vec_;
76 } else if constexpr (simd::is_quadword_v<vector_type>) {
77 if constexpr (LaneIndex >= ArgonHalf<scalar_type>::lanes) {
78 return simd::get_high(vec_);
79 } else {
80 return simd::get_low(vec_);
81 }
82 }
83 }
84
85 ace int lane() {
86 if constexpr (simd::is_doubleword_v<vector_type>) {
87 return LaneIndex;
88 } else if constexpr (simd::is_quadword_v<vector_type>) {
89 if (LaneIndex >= ArgonHalf<scalar_type>::lanes) {
90 return (LaneIndex - ArgonHalf<scalar_type>::lanes);
91 } else {
92 return LaneIndex;
93 }
94 }
95 }
96#endif
97 private:
98 VectorType& vec_;
99};
100
106template <typename VectorType>
107class Lane {
108 using vector_type = std::remove_const_t<VectorType>; // a const vector's lanes are read-only
109 using scalar_type = simd::Scalar_t<vector_type>;
110 using type = Lane<VectorType>;
111 using argon_type = helpers::ArgonFor_t<vector_type>;
112
113 public:
115 ace Lane(VectorType& vec, const int lane) : vec_{vec}, lane_{lane} {}
117 ace argon_type operator=(const scalar_type b) { return vec_ = Set(b); }
120 ace argon_type Load(const scalar_type* ptr) {
121#ifdef ARGON_PLATFORM_MVE
122 return vec_ = Set(*ptr);
123#else
124 return vec_ = argon_type{simd::load1_lane(vec_, lane_, ptr)};
125#endif
126 }
127
128 ace argon_type Set(const scalar_type b) { return argon_type{simd::set_lane(vec_, lane_, b)}; }
130 ace scalar_type Get() const { return simd::get_lane(vec_, lane_); }
132 ace operator scalar_type() const { return Get(); }
133
134#if __ARM_ARCH >= 8
136 ace vector_type vec() { return vec_; }
138 ace const int lane() { return lane_; }
139#else
141 ace neon::Vec64_t<scalar_type> vec() {
142 if constexpr (simd::is_doubleword_v<vector_type>) {
143 return vec_;
144 } else if constexpr (simd::is_quadword_v<vector_type>) {
145 if (lane_ >= ArgonHalf<scalar_type>::lanes) {
146 return simd::get_high(vec_);
147 } else {
148 return simd::get_low(vec_);
149 }
150 }
151 }
152
153 ace int lane() {
154 if constexpr (simd::is_doubleword_v<vector_type>) {
155 return lane_;
156 } else if constexpr (simd::is_quadword_v<vector_type>) {
157 if (lane_ >= ArgonHalf<scalar_type>::lanes) {
158 return (lane_ - ArgonHalf<scalar_type>::lanes);
159 } else {
160 return lane_;
161 }
162 }
163 }
164#endif
165
166 private:
167 VectorType& vec_;
168 int lane_;
169
170 // friend class Vector<VectorType>;
171};
172} // namespace argon
173
174#undef ace
175#undef simd
Definition argon_half.hpp:11
ace int lane()
On ARM32, return the local lane index within the 64-bit half-register returned by vec().
Definition lane.hpp:85
ace argon_type Set(const scalar_type b)
Set this lane to b and return the updated vector.
Definition lane.hpp:62
ace argon_type operator=(const scalar_type b)
Write a scalar value into this lane and return the updated vector.
Definition lane.hpp:47
ace neon::Vec64_t< scalar_type > vec()
On ARM32, return the 64-bit half-register that contains this lane.
Definition lane.hpp:73
ace ConstLane(VectorType &vec)
Construct a lane accessor bound to vec.
Definition lane.hpp:43
ace scalar_type Get() const
Get the scalar value of this lane.
Definition lane.hpp:64
ace argon_type Load(const scalar_type *ptr)
Load a scalar from ptr into this lane and return the updated vector.
Definition lane.hpp:50
ace argon_type Set(const scalar_type b)
Set this lane to b and return the updated vector.
Definition lane.hpp:128
ace argon_type Load(const scalar_type *ptr)
Load a scalar from ptr into this lane and return the updated vector.
Definition lane.hpp:120
ace int lane()
Definition lane.hpp:153
ace Lane(VectorType &vec, const int lane)
Construct a lane accessor bound to vec at runtime index lane.
Definition lane.hpp:115
ace neon::Vec64_t< scalar_type > vec()
Definition lane.hpp:141
ace scalar_type Get() const
Get the scalar value of this lane.
Definition lane.hpp:130
ace argon_type operator=(const scalar_type b)
Write a scalar value into this lane and return the updated vector.
Definition lane.hpp:117
Represents a SIMD vector with various operations.
Definition vector.hpp:53
Header file for SIMD features and platform detection.
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
A 128-bit SIMD vector wrapping a scalar type, providing arithmetic, logical, and data-movement operat...
Definition argon.hpp:30