Argon 0.1.0
Loading...
Searching...
No Matches
ptr.hpp
1// Copyright (c) 2025 Katherine Whitlock
2//
3// This software is released under the MIT License.
4// https://opensource.org/licenses/MIT
5
6#pragma once
7#include <cstddef>
8#include <cstdint>
9#include <iterator>
10#include <ranges>
11#include "argon/argon_full.hpp"
12#include "arm_simd/helpers/scalar.hpp"
13#include "arm_simd/helpers/vec128.hpp"
14#include "store.hpp"
15
16#ifdef __ARM_FEATURE_MVE
17#define simd mve
18#else
19#define simd neon
20#endif
21
22// Smart pointer for argon type
23template <typename ScalarType>
24struct ArgonPtr {
25 using intrinsic_type = simd::Vec128_t<ScalarType>;
26 static constexpr size_t lanes = sizeof(intrinsic_type) / sizeof(ScalarType);
27 static constexpr size_t vectorizeable_size(size_t size) { return size & ~(lanes - 1); }
28
29 using difference_type = std::ptrdiff_t;
30 using value_type = Argon<ScalarType>;
31 using pointer = Argon<ScalarType>*;
32
33 ArgonPtr(ScalarType* ptr) : ptr_{ptr}, vec_{Argon<ScalarType>::Load(ptr_)} {}
34 ArgonPtr(const ArgonPtr&) = default;
35 ArgonPtr& operator=(const ArgonPtr&) = default;
36 ArgonPtr(ArgonPtr&&) = default;
37
38 ~ArgonPtr() {
39 if (dirty_) {
40 vec_.StoreTo(ptr_);
41 }
42 }
43
44 pointer operator->() {
45 dirty_ = true;
46 return &vec_;
47 }
48
49 value_type& operator*() {
50 dirty_ = true;
51 return vec_;
52 }
53
54 const value_type& operator*() const { return vec_; }
55 const pointer operator->() const { return &vec_; }
56
57 void store() { vec_.StoreTo(ptr_); }
58 void reload() { vec_ = value_type::Load(ptr_); }
59
60 friend bool operator==(const ArgonPtr& a, const ArgonPtr& b) { return a.ptr_ == b.ptr_; }
61 friend bool operator==(const ArgonPtr& a, const ScalarType* ptr) { return a.ptr_ == ptr; }
62 friend bool operator!=(const ArgonPtr& a, const ArgonPtr& b) { return a.ptr_ != b.ptr_; }
63 friend bool operator!=(const ArgonPtr& a, const ScalarType* ptr) { return a.ptr_ != ptr; }
64
65 private:
66 ScalarType* ptr_;
67 value_type vec_;
68 bool dirty_ = false;
69};
70
71namespace argon::vectorize {
72
73template <typename ScalarType>
74struct ptr : std::ranges::view_interface<ptr<ScalarType>> {
75 using intrinsic_type = simd::Vec128_t<ScalarType>;
76 static constexpr size_t lanes = sizeof(intrinsic_type) / sizeof(ScalarType);
77 static constexpr size_t vectorizeable_size(size_t size) { return size & ~(lanes - 1); }
78
79 public:
80 struct Iterator {
81 using difference_type = std::ptrdiff_t;
82 using value_type = ArgonPtr<ScalarType>;
83
84 Iterator(ScalarType* pointer) : ptr_{pointer} {}
85
86 ArgonPtr<ScalarType> operator*() const { return ptr_; }
87
88 Iterator& operator++() {
89 ptr_ += lanes;
90 return *this;
91 }
92
93 void operator++(int) { ++*this; }
94 friend bool operator==(const Iterator& a, const Iterator& b) { return a.ptr_ == b.ptr_; }
95 friend bool operator==(const Iterator& a, const ScalarType* pointer) { return a.ptr_ == pointer; }
96 friend bool operator!=(const Iterator& a, const Iterator& b) { return a.ptr_ != b.ptr_; }
97 friend bool operator!=(const Iterator& a, const ScalarType* pointer) { return a.ptr_ != pointer; }
98
99 private:
100 ScalarType* ptr_ = nullptr;
101 };
102 static_assert(std::input_iterator<Iterator>);
103
104 using iterator = Iterator;
105
106 iterator begin() { return start_; }
107 ScalarType* end() { return start_ + size_; }
108 size_t size() const { return size_ / lanes; }
109
110 template <std::ranges::contiguous_range R>
111 ptr(R&& r) : start_{&*std::ranges::begin(r)}, size_{vectorizeable_size(std::ranges::size(r))} {}
112
113 private:
114 ScalarType* start_;
115 size_t size_;
116};
117template <std::ranges::contiguous_range R>
119
120static_assert(std::ranges::range<ptr<int32_t>>);
121static_assert(std::ranges::view<ptr<int32_t>>);
122static_assert(std::movable<ptr<int32_t>>);
123static_assert(std::ranges::viewable_range<ptr<int32_t>>);
124
125} // namespace argon::vectorize
126#undef simd
Definition argon_full.hpp:24
static ace argon_type Load(const scalar_type *ptr)
Definition vector.hpp:788
Lane deconstruction feature.
Definition argon_full.hpp:302
Definition ptr.hpp:24
Definition ptr.hpp:80
Definition ptr.hpp:74