gem5 v24.1.0.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
vec_reg.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015-2018 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
97#ifndef __ARCH_GENERIC_VEC_REG_HH__
98#define __ARCH_GENERIC_VEC_REG_HH__
99
100#include <array>
101#include <cstdint>
102#include <iomanip>
103#include <iostream>
104#include <sstream>
105#include <string>
106
107#include "base/cprintf.hh"
108#include "base/logging.hh"
109#include "base/types.hh"
111
112namespace gem5
113{
114
115constexpr unsigned MaxVecRegLenInBytes = 1ULL << 16; // 2^16 bytes
116
124template <size_t SIZE>
126{
127 private:
128 static_assert(SIZE > 0,
129 "Cannot create Vector Register Container of zero size");
130 static_assert(SIZE <= MaxVecRegLenInBytes,
131 "Vector Register size limit exceeded");
132 public:
133 static constexpr inline size_t size() { return SIZE; };
134 using Container = std::array<uint8_t, SIZE>;
135 private:
136 // 16-byte aligned to support 128bit element view
137 alignas(16) Container container;
138
139 public:
142
144 void set(uint8_t val) { memset(container.data(), val, SIZE); }
145
147 void zero() { set(0); }
148
154 {
155 if (&that != this)
156 std::memcpy(container.data(), that.container.data(), SIZE);
157 return *this;
158 }
164 template<size_t S2>
165 inline bool
167 {
168 return SIZE == S2 &&
169 !memcmp(container.data(), that.container.data(), SIZE);
170 }
174 template<size_t S2>
175 bool
177 {
178 return !operator==(that);
179 }
180
192 template <typename VecElem>
193 VecElem *
195 {
196 static_assert(SIZE % sizeof(VecElem) == 0,
197 "VecElem does not evenly divide the register size");
198 return (VecElem *)container.data();
199 }
200
201 template <typename VecElem>
202 const VecElem *
203 as() const
204 {
205 static_assert(SIZE % sizeof(VecElem) == 0,
206 "VecElem does not evenly divide the register size");
207 return (VecElem *)container.data();
208 }
209
210 friend std::ostream&
211 operator<<(std::ostream& os, const VecRegContainer<SIZE>& v)
212 {
213 // When printing for human consumption, break into 4 byte chunks.
214 ccprintf(os, "[");
215 size_t count = 0;
216 for (auto& b: v.container) {
217 if (count && (count % 4) == 0)
218 os << "_";
219 ccprintf(os, "%02x", b);
220 count++;
221 }
222 ccprintf(os, "]");
223 return os;
224 }
225
226 std::string
227 getString(const uint64_t& size) const
228 {
229 std::stringstream s;
230 size_t count = 0;
231 s << "[";
232 for (auto& b: container) {
233 if (count && (count % 4) == 0)
234 s << "_";
235 s << std::hex << std::setfill('0') << std::setw(2) << (uint16_t)b;
236 count++;
237 if (count == size)
238 break;
239 }
240 s << "]";
241 return s.str();
242 }
243
249};
250
255template <size_t Sz>
257{
258 static bool
259 parse(const std::string &str, VecRegContainer<Sz> &value)
260 {
261 fatal_if(str.size() > 2 * Sz,
262 "Vector register value overflow at unserialize");
263
264 for (int i = 0; i < Sz; i++) {
265 uint8_t b = 0;
266 if (2 * i < str.size())
267 b = stoul(str.substr(i * 2, 2), nullptr, 16);
268 value.template as<uint8_t>()[i] = b;
269 }
270 return true;
271 }
272};
273
274template <size_t Sz>
276{
277 static void
278 show(std::ostream &os, const VecRegContainer<Sz> &value)
279 {
280 for (auto& b: value.container)
281 ccprintf(os, "%02x", b);
282 }
283};
292{
294 bool operator == (const DummyVecRegContainer &d) const { return true; }
295 bool operator != (const DummyVecRegContainer &d) const { return true; }
296 template <typename VecElem>
297 VecElem *as() { return nullptr; }
298};
299template <>
301{
302 static bool
303 parse(const std::string &s, DummyVecRegContainer &value)
304 {
305 return false;
306 }
307};
308static_assert(sizeof(DummyVecRegContainer) == sizeof(RegVal));
309static inline std::ostream &
310operator<<(std::ostream &os, const DummyVecRegContainer &d)
311{
312 return os;
313}
316} // namespace gem5
317
318#endif /* __ARCH_GENERIC_VEC_REG_HH__ */
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
Vector Register Abstraction This generic class is the model in a particularization of MVC,...
Definition vec_reg.hh:126
const VecElem * as() const
Definition vec_reg.hh:203
std::string getString(const uint64_t &size) const
Definition vec_reg.hh:227
bool operator==(const VecRegContainer< S2 > &that) const
Equality operator.
Definition vec_reg.hh:166
friend std::ostream & operator<<(std::ostream &os, const VecRegContainer< SIZE > &v)
Definition vec_reg.hh:211
static constexpr size_t size()
Definition vec_reg.hh:133
VecRegContainer(const VecRegContainer &)=default
void set(uint8_t val)
Set the container.
Definition vec_reg.hh:144
VecElem * as()
View interposers.
Definition vec_reg.hh:194
VecRegContainer< SIZE > & operator=(const VecRegContainer< SIZE > &that)
Assignment operators.
Definition vec_reg.hh:153
void zero()
Zero the container.
Definition vec_reg.hh:147
bool operator!=(const VecRegContainer< S2 > &that) const
Inequality operator.
Definition vec_reg.hh:176
std::array< uint8_t, SIZE > Container
Definition vec_reg.hh:134
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
Bitfield< 28 > v
Definition misc_types.hh:54
Bitfield< 4 > s
Bitfield< 7 > b
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 12, 11 > set
Bitfield< 9 > d
Definition misc_types.hh:64
uint32_t VecElem
Definition vec.hh:63
Bitfield< 17 > os
Definition misc.hh:838
Bitfield< 63 > val
Definition misc.hh:804
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
std::ostream & operator<<(std::ostream &os, const BaseSemihosting::InPlaceArg &ipa)
uint64_t RegVal
Definition types.hh:173
constexpr unsigned MaxVecRegLenInBytes
Definition vec_reg.hh:115
void ccprintf(cp::Print &print)
Definition cprintf.hh:130
Dummy type aliases and constants for architectures that do not implement vector registers.
Definition vec_reg.hh:292
bool operator==(const DummyVecRegContainer &d) const
Definition vec_reg.hh:294
bool operator!=(const DummyVecRegContainer &d) const
Definition vec_reg.hh:295
static bool parse(const std::string &s, DummyVecRegContainer &value)
Definition vec_reg.hh:303
static bool parse(const std::string &str, VecRegContainer< Sz > &value)
Definition vec_reg.hh:259
static void show(std::ostream &os, const VecRegContainer< Sz > &value)
Definition vec_reg.hh:278

Generated on Mon Jan 13 2025 04:28:20 for gem5 by doxygen 1.9.8