radv: Remove conformance warnings with ACO.
[mesa.git] / src / amd / compiler / aco_util.h
1 /*
2 * Copyright Michael Schellenberger Costa
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25 #ifndef ACO_UTIL_H
26 #define ACO_UTIL_H
27
28 #include <cassert>
29 #include <iterator>
30
31 namespace aco {
32
33 /*! \brief Definition of a span object
34 *
35 * \details A "span" is an "array view" type for holding a view of contiguous
36 * data. The "span" object does not own the data itself.
37 */
38 template <typename T>
39 class span {
40 public:
41 using value_type = T;
42 using pointer = value_type*;
43 using const_pointer = const value_type*;
44 using reference = value_type&;
45 using const_reference = const value_type&;
46 using iterator = pointer;
47 using const_iterator = const_pointer;
48 using reverse_iterator = std::reverse_iterator<iterator>;
49 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
50 using size_type = uint16_t;
51 using difference_type = ptrdiff_t;
52
53 /*! \brief Compiler generated default constructor
54 */
55 constexpr span() = default;
56
57 /*! \brief Constructor taking a pointer and the length of the span
58 * \param[in] data Pointer to the underlying data array
59 * \param[in] length The size of the span
60 */
61 constexpr span(uint16_t offset, const size_type length)
62 : offset{ offset } , length{ length } {}
63
64 /*! \brief Returns an iterator to the begin of the span
65 * \return data
66 */
67 constexpr iterator begin() noexcept {
68 return (pointer)((uintptr_t)this + offset);
69 }
70
71 /*! \brief Returns a const_iterator to the begin of the span
72 * \return data
73 */
74 constexpr const_iterator begin() const noexcept {
75 return (const_pointer)((uintptr_t)this + offset);
76 }
77
78 /*! \brief Returns an iterator to the end of the span
79 * \return data + length
80 */
81 constexpr iterator end() noexcept {
82 return std::next(begin(), length);
83 }
84
85 /*! \brief Returns a const_iterator to the end of the span
86 * \return data + length
87 */
88 constexpr const_iterator end() const noexcept {
89 return std::next(begin(), length);
90 }
91
92 /*! \brief Returns a const_iterator to the begin of the span
93 * \return data
94 */
95 constexpr const_iterator cbegin() const noexcept {
96 return begin();
97 }
98
99 /*! \brief Returns a const_iterator to the end of the span
100 * \return data + length
101 */
102 constexpr const_iterator cend() const noexcept {
103 return std::next(begin(), length);
104 }
105
106 /*! \brief Returns a reverse_iterator to the end of the span
107 * \return reverse_iterator(end())
108 */
109 constexpr reverse_iterator rbegin() noexcept {
110 return reverse_iterator(end());
111 }
112
113 /*! \brief Returns a const_reverse_iterator to the end of the span
114 * \return reverse_iterator(end())
115 */
116 constexpr const_reverse_iterator rbegin() const noexcept {
117 return const_reverse_iterator(end());
118 }
119
120 /*! \brief Returns a reverse_iterator to the begin of the span
121 * \return reverse_iterator(begin())
122 */
123 constexpr reverse_iterator rend() noexcept {
124 return reverse_iterator(begin());
125 }
126
127 /*! \brief Returns a const_reverse_iterator to the begin of the span
128 * \return reverse_iterator(begin())
129 */
130 constexpr const_reverse_iterator rend() const noexcept {
131 return const_reverse_iterator(begin());
132 }
133
134 /*! \brief Returns a const_reverse_iterator to the end of the span
135 * \return rbegin()
136 */
137 constexpr const_reverse_iterator crbegin() const noexcept {
138 return const_reverse_iterator(cend());
139 }
140
141 /*! \brief Returns a const_reverse_iterator to the begin of the span
142 * \return rend()
143 */
144 constexpr const_reverse_iterator crend() const noexcept {
145 return const_reverse_iterator(cbegin());
146 }
147
148 /*! \brief Unchecked access operator
149 * \param[in] index Index of the element we want to access
150 * \return *(std::next(data, index))
151 */
152 constexpr reference operator[](const size_type index) noexcept {
153 assert(length > index);
154 return *(std::next(begin(), index));
155 }
156
157 /*! \brief Unchecked const access operator
158 * \param[in] index Index of the element we want to access
159 * \return *(std::next(data, index))
160 */
161 constexpr const_reference operator[](const size_type index) const noexcept {
162 assert(length > index);
163 return *(std::next(begin(), index));
164 }
165
166 /*! \brief Returns a reference to the last element of the span
167 * \return *(std::next(data, length - 1))
168 */
169 constexpr reference back() noexcept {
170 assert(length > 0);
171 return *(std::next(begin(), length - 1));
172 }
173
174 /*! \brief Returns a const_reference to the last element of the span
175 * \return *(std::next(data, length - 1))
176 */
177 constexpr const_reference back() const noexcept {
178 assert(length > 0);
179 return *(std::next(begin(), length - 1));
180 }
181
182 /*! \brief Returns a reference to the first element of the span
183 * \return *begin()
184 */
185 constexpr reference front() noexcept {
186 assert(length > 0);
187 return *begin();
188 }
189
190 /*! \brief Returns a const_reference to the first element of the span
191 * \return *cbegin()
192 */
193 constexpr const_reference front() const noexcept {
194 assert(length > 0);
195 return *cbegin();
196 }
197
198 /*! \brief Returns true if the span is empty
199 * \return length == 0
200 */
201 constexpr bool empty() const noexcept {
202 return length == 0;
203 }
204
205 /*! \brief Returns the size of the span
206 * \return length == 0
207 */
208 constexpr size_type size() const noexcept {
209 return length;
210 }
211
212 /*! \brief Decreases the size of the span by 1
213 */
214 constexpr void pop_back() noexcept {
215 assert(length > 0);
216 --length;
217 }
218
219 /*! \brief Clears the span
220 */
221 constexpr void clear() noexcept {
222 offset = 0;
223 length = 0;
224 }
225
226 private:
227 uint16_t offset{ 0 }; //!> Byte offset from span to data
228 size_type length{ 0 }; //!> Size of the span
229 };
230
231 } // namespace aco
232
233 #endif // ACO_UTIL_H