Argon 0.1.0
Loading...
Searching...
No Matches
pointer_vector.hpp
Go to the documentation of this file.
1#pragma once
2#include <array>
3#include <cstddef>
4#include <cstdint>
5#include <type_traits>
6#include "argon/argon_full.hpp"
7
10
11namespace argon {
12
20template <typename T>
21 requires(lane_scalar<std::remove_const_t<T>> && (sizeof(T) == 4 || sizeof(T) == 8))
23 using value_type = std::remove_const_t<T>;
24
25 public:
27 static constexpr size_t lanes = argon_type::lanes;
28
30 template <int offset>
31 static constexpr bool valid_offset = offset % int{sizeof(T)} == 0 && offset >= -127 * int{sizeof(T)} &&
32 offset <= 127 * int{sizeof(T)};
33
35 explicit PointerVector(const std::array<T*, lanes>& pointers) {
36#ifdef ARGON_PLATFORM_MVE
37 std::array<address_scalar, lanes> addresses{};
38 for (size_t i = 0; i < lanes; ++i) addresses[i] = static_cast<address_scalar>(reinterpret_cast<uintptr_t>(pointers[i]));
39 addresses_ = Argon<address_scalar>::Load(addresses.data()).vec();
40#else
41 pointers_ = pointers;
42#endif
43 }
44
46 std::array<T*, lanes> pointers() const {
47#ifdef ARGON_PLATFORM_MVE
48 const auto addresses = Argon<address_scalar>{addresses_}.to_array();
49 std::array<T*, lanes> out{};
50 for (size_t i = 0; i < lanes; ++i) out[i] = reinterpret_cast<T*>(static_cast<uintptr_t>(addresses[i]));
51 return out;
52#else
53 return pointers_;
54#endif
55 }
56
58 template <int offset = 0>
59 requires valid_offset<offset>
60 argon_type Load() const {
61#ifdef ARGON_PLATFORM_MVE
62 if constexpr (std::is_same_v<value_type, int32_t>) {
63 return vldrwq_gather_base_s32(addresses_, offset);
64 } else if constexpr (std::is_same_v<value_type, uint32_t>) {
65 return vldrwq_gather_base_u32(addresses_, offset);
66 } else if constexpr (std::is_same_v<value_type, float>) {
67 return vldrwq_gather_base_f32(addresses_, offset);
68 } else if constexpr (std::is_same_v<value_type, int64_t>) {
69 return vldrdq_gather_base_s64(addresses_, offset);
70 } else {
71 return vldrdq_gather_base_u64(addresses_, offset);
72 }
73#else
74 std::array<value_type, lanes> out{};
75 for (size_t i = 0; i < lanes; ++i) out[i] = *At<offset>(i);
76 return argon_type::Load(out.data());
77#endif
78 }
79
81 template <int offset = 0>
82 requires(valid_offset<offset> && !std::is_const_v<T>)
83 void Store(argon_type value) const {
84#ifdef ARGON_PLATFORM_MVE
85 if constexpr (std::is_same_v<value_type, int32_t>) {
86 vstrwq_scatter_base_s32(addresses_, offset, value.vec());
87 } else if constexpr (std::is_same_v<value_type, uint32_t>) {
88 vstrwq_scatter_base_u32(addresses_, offset, value.vec());
89 } else if constexpr (std::is_same_v<value_type, float>) {
90 vstrwq_scatter_base_f32(addresses_, offset, value.vec());
91 } else if constexpr (std::is_same_v<value_type, int64_t>) {
92 vstrdq_scatter_base_s64(addresses_, offset, value.vec());
93 } else {
94 vstrdq_scatter_base_u64(addresses_, offset, value.vec());
95 }
96#else
97 const auto values = value.to_array();
98 for (size_t i = 0; i < lanes; ++i) *At<offset>(i) = values[i];
99#endif
100 }
101
103 template <int step>
104 requires valid_offset<step>
105 void Advance() {
106#ifdef ARGON_PLATFORM_MVE
107 addresses_ = (Argon<address_scalar>{addresses_} + Argon<address_scalar>{static_cast<address_scalar>(step)}).vec();
108#else
109 for (auto& p : pointers_) p = reinterpret_cast<T*>(reinterpret_cast<std::conditional_t<std::is_const_v<T>, const char, char>*>(p) + step);
110#endif
111 }
112
117 template <int step>
118 requires valid_offset<step>
120#if defined(ARGON_PLATFORM_MVE) && defined(__clang__)
121 if constexpr (std::is_same_v<value_type, int32_t>) {
122 return vldrwq_gather_base_wb_s32(&addresses_, step);
123 } else if constexpr (std::is_same_v<value_type, uint32_t>) {
124 return vldrwq_gather_base_wb_u32(&addresses_, step);
125 } else if constexpr (std::is_same_v<value_type, float>) {
126 return vldrwq_gather_base_wb_f32(&addresses_, step);
127 } else if constexpr (std::is_same_v<value_type, int64_t>) {
128 return vldrdq_gather_base_wb_s64(&addresses_, step);
129 } else {
130 return vldrdq_gather_base_wb_u64(&addresses_, step);
131 }
132#else
133 auto value = Load<step>();
135 return value;
136#endif
137 }
138
142 template <int step>
143 requires(valid_offset<step> && !std::is_const_v<T>)
145#if defined(ARGON_PLATFORM_MVE) && defined(__clang__)
146 if constexpr (std::is_same_v<value_type, int32_t>) {
147 vstrwq_scatter_base_wb_s32(&addresses_, step, value.vec());
148 } else if constexpr (std::is_same_v<value_type, uint32_t>) {
149 vstrwq_scatter_base_wb_u32(&addresses_, step, value.vec());
150 } else if constexpr (std::is_same_v<value_type, float>) {
151 vstrwq_scatter_base_wb_f32(&addresses_, step, value.vec());
152 } else if constexpr (std::is_same_v<value_type, int64_t>) {
153 vstrdq_scatter_base_wb_s64(&addresses_, step, value.vec());
154 } else {
155 vstrdq_scatter_base_wb_u64(&addresses_, step, value.vec());
156 }
157#else
158 Store<step>(value);
160#endif
161 }
162
163 private:
164#ifdef ARGON_PLATFORM_MVE
165 // Addresses are 32-bit on M-profile; 64-bit elements gather through a uint64x2_t of addresses.
166 using address_scalar = std::conditional_t<sizeof(T) == 4, uint32_t, uint64_t>;
167 typename Argon<address_scalar>::vector_type addresses_;
168#else
169 template <int offset>
170 T* At(size_t lane) const {
171 using byte = std::conditional_t<std::is_const_v<T>, const char, char>;
172 return reinterpret_cast<T*>(reinterpret_cast<byte*>(pointers_[lane]) + offset);
173 }
174
175 std::array<T*, lanes> pointers_{};
176#endif
177};
178
179} // namespace argon
Definition argon_full.hpp:44
void AdvanceAndStore(argon_type value)
Advance every pointer by step bytes, then store value through them.
Definition pointer_vector.hpp:144
Argon< value_type > argon_type
The vector type loaded and stored.
Definition pointer_vector.hpp:26
static constexpr size_t lanes
The number of pointers.
Definition pointer_vector.hpp:27
void Store(argon_type value) const
Store lane i of value to offset bytes past pointer i.
Definition pointer_vector.hpp:83
void Advance()
Advance every pointer by step bytes.
Definition pointer_vector.hpp:105
argon_type Load() const
Load lane i from offset bytes past pointer i.
Definition pointer_vector.hpp:60
std::array< T *, lanes > pointers() const
The current pointers.
Definition pointer_vector.hpp:46
PointerVector(const std::array< T *, lanes > &pointers)
Point lane i at pointers[i].
Definition pointer_vector.hpp:35
static constexpr bool valid_offset
Whether a byte offset or step is encodable: a multiple of the element size, within range.
Definition pointer_vector.hpp:31
argon_type AdvanceAndLoad()
Advance every pointer by step bytes, then load through them: one step of a lockstep walk.
Definition pointer_vector.hpp:119
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 std::array< scalar_type, lanes > to_array() const
Convert the vector to an array of scalar values.
Definition vector.hpp:319
A scalar type a vector lane can hold: an arithmetic type, or float16_t where half-float vectors exist...
Definition lane_scalar.hpp:31
A 128-bit SIMD vector wrapping a scalar type, providing arithmetic, logical, and data-movement operat...
Definition argon.hpp:30