Argon 0.1.0
Loading...
Searching...
No Matches
load.hpp
1// Copyright 2025 Katherine Whitlock
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
18
19#pragma once
20#include <ranges>
21#include "argon/argon_full.hpp"
22#include "argon/helpers/size.hpp"
24#include "arm_simd/helpers/vec128.hpp"
25
26#ifdef __ARM_FEATURE_MVE
27#define simd mve
28#else
29#define simd neon
30#endif
31
32namespace argon::vectorize {
33
36template <typename ScalarType>
37struct load : std::ranges::view_interface<load<ScalarType>> {
38 using intrinsic_type = simd::Vec128_t<ScalarType>;
39 static constexpr size_t lanes =
40 sizeof(intrinsic_type) / sizeof(ScalarType);
41
42 public:
45 struct LoadIterator {
46 using iterator_category = std::bidirectional_iterator_tag;
47 using difference_type = std::ptrdiff_t;
49 using reference_type = value_type&;
50
51 LoadIterator() = default;
52
55 LoadIterator(const ScalarType* ptr) : ptr_{ptr} {}
56
60 Argon<ScalarType> operator*() const { return value_type::Load(ptr_); }
61
65 LoadIterator& operator+=(int n) {
66 ptr_ += n * lanes;
67 return *this;
68 }
69
73 LoadIterator& operator-=(int n) {
74 ptr_ -= n * lanes;
75 return *this;
76 }
77
80 LoadIterator operator++(int) {
81 LoadIterator tmp = *this;
82 ++(*this);
83 return tmp;
84 }
85
88 LoadIterator& operator++() {
89 ptr_ += lanes;
90 return *this;
91 }
92
95 LoadIterator operator--(int) {
96 LoadIterator tmp = *this;
97 --(*this);
98 return tmp;
99 }
100
103 LoadIterator& operator--() {
104 ptr_ -= lanes;
105 return *this;
106 }
107
111 LoadIterator operator+(int n) const {
112 LoadIterator it = *this;
113 it += n;
114 return it;
115 }
116
120 LoadIterator operator-(int n) const {
121 LoadIterator it = *this;
122 it -= n;
123 return it;
124 }
125
129 value_type operator[](int n) const { return *(*this + n); }
130
134 difference_type operator-(const LoadIterator& other) const { return (ptr_ - other.ptr_) / difference_type{lanes}; }
135
140 friend LoadIterator operator+(const int n, const LoadIterator& it) { return it + n; }
141
143 friend bool operator==(const LoadIterator& a, const LoadIterator& b) { return a.ptr_ == b.ptr_; }
144
146 friend bool operator==(const LoadIterator& a, const ScalarType* ptr) { return a.ptr_ == ptr; }
147
149 friend bool operator!=(const LoadIterator& a, const LoadIterator& b) { return a.ptr_ != b.ptr_; }
150
152 friend bool operator!=(const LoadIterator& a, const ScalarType* ptr) { return a.ptr_ != ptr; }
153
155 friend auto operator<=>(const LoadIterator& a, const LoadIterator& b) { return a.ptr_ <=> b.ptr_; }
156
157 private:
158 const ScalarType* ptr_;
159 };
160 static_assert(std::sized_sentinel_for<LoadIterator, LoadIterator>);
161 static_assert(std::bidirectional_iterator<LoadIterator>);
162 static_assert(std::input_iterator<LoadIterator>);
163
165 using sentinel = const ScalarType*;
166
169 iterator begin() { return start_; }
170
173 const ScalarType* end() { return start_ + (size_ * lanes); }
174
177 size_t size() const { return size_; }
178
182 template <std::ranges::contiguous_range R>
183 load(R&& r)
184 : start_{&*std::ranges::begin(r)},
185 size_{helpers::vectorizeable_size<ScalarType>(std::ranges::size(r)) / lanes},
186 count_{std::ranges::size(r)} {}
187
190 load(const std::span<ScalarType> span)
191 : start_{span.data()}, size_{helpers::vectorizeable_size<ScalarType>(span.size()) / lanes}, count_{span.size()} {}
192
196 load_tail<ScalarType> with_tail() const { return {start_, count_}; }
197
198 private:
199 const ScalarType* start_;
200 size_t size_;
201 size_t count_;
202};
203
204static_assert(std::ranges::range<load<int32_t>>);
205static_assert(std::ranges::view<load<int32_t>>);
206static_assert(std::movable<load<int32_t>>);
207static_assert(std::ranges::viewable_range<load<int32_t>>);
208
209template <std::ranges::contiguous_range R>
211
212template <typename ScalarType>
213load(const std::span<ScalarType>) -> load<std::remove_cv_t<ScalarType>>;
214
215} // namespace argon::vectorize
216#undef simd
Definition argon_full.hpp:44
Read-only view of a range as vectors, including a final partial vector.
Definition tail.hpp:168
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
ace Argon< T > load(const T *ptr)
Load a quad-word vector from a pointer.
Definition argon.hpp:143
Lane deconstruction feature.
Definition argon_full.hpp:1547
The Iterator type for the range.
Definition load.hpp:45
LoadIterator & operator+=(int n)
Increments the iterator by a number of steps.
Definition load.hpp:65
friend bool operator!=(const LoadIterator &a, const LoadIterator &b)
Compares LoadIterators for inequality.
Definition load.hpp:149
LoadIterator operator+(int n) const
Adds an integer to the iterator and returns a new iterator.
Definition load.hpp:111
Argon< ScalarType > operator*() const
Dereferences the iterator to get the SIMD vector.
Definition load.hpp:60
LoadIterator operator++(int)
Postfix increment operator.
Definition load.hpp:80
friend bool operator==(const LoadIterator &a, const ScalarType *ptr)
Compares LoadIterators for equality.
Definition load.hpp:146
std::bidirectional_iterator_tag iterator_category
The iterator category.
Definition load.hpp:46
LoadIterator & operator-=(int n)
Decrements the iterator by a number of steps.
Definition load.hpp:73
LoadIterator & operator++()
Prefix increment operator.
Definition load.hpp:88
std::ptrdiff_t difference_type
The difference type.
Definition load.hpp:47
friend LoadIterator operator+(const int n, const LoadIterator &it)
Adds an integer to the iterator and returns a new iterator.
Definition load.hpp:140
friend bool operator==(const LoadIterator &a, const LoadIterator &b)
Compares LoadIterators for equality.
Definition load.hpp:143
LoadIterator & operator--()
Prefix decrement operator.
Definition load.hpp:103
friend bool operator!=(const LoadIterator &a, const ScalarType *ptr)
Compares LoadIterators for inequality.
Definition load.hpp:152
LoadIterator operator--(int)
Postfix decrement operator.
Definition load.hpp:95
LoadIterator operator-(int n) const
Subtracts an integer from the iterator and returns a new iterator.
Definition load.hpp:120
difference_type operator-(const LoadIterator &other) const
Calculates the difference between two iterators.
Definition load.hpp:134
helpers::ArgonFor_t< intrinsic_type > value_type
The value type of the iterator.
Definition load.hpp:48
LoadIterator(const ScalarType *ptr)
Constructs a LoadIterator from a pointer to the data.
Definition load.hpp:55
value_type operator[](int n) const
Accesses the element at the given index.
Definition load.hpp:129
friend auto operator<=>(const LoadIterator &a, const LoadIterator &b)
Compares LoadIterators.
Definition load.hpp:155
const ScalarType * end()
Returns an iterator to the end of the range.
Definition load.hpp:173
load_tail< ScalarType > with_tail() const
A view of the same range that also visits the final, partial vector.
Definition load.hpp:196
size_t size() const
Returns the size of the range.
Definition load.hpp:177
load(R &&r)
Construct a load from a range.
Definition load.hpp:183
static constexpr size_t lanes
The number of lanes in the SIMD vector.
Definition load.hpp:39
const ScalarType * sentinel
The sentinel type for the range.
Definition load.hpp:165
iterator begin()
Returns an iterator to the beginning of the range.
Definition load.hpp:169
load(const std::span< ScalarType > span)
Construct a load from a span.
Definition load.hpp:190
LoadIterator iterator
The iterator type for the range.
Definition load.hpp:164
simd::Vec128_t< ScalarType > intrinsic_type
The SIMD vector type.
Definition load.hpp:38
Definition ptr.hpp:74
Vectorized views that also visit the final, partial vector of a range (see with_tail() on the views).