gem5 v24.0.0.0
Loading...
Searching...
No Matches
vec_pred_reg.hh
Go to the documentation of this file.
1// Copyright (c) 2017, 2021 Arm Limited
2// All rights reserved
3//
4// The license below extends only to copyright in the software and shall
5// not be construed as granting a license to any other intellectual
6// property including but not limited to intellectual property relating
7// to a hardware implementation of the functionality of the software
8// licensed hereunder. You may use the software subject to the license
9// terms below provided that you ensure that this notice is replicated
10// unmodified and in its entirety in all distributions of the software,
11// modified or unmodified, in source code or in binary form.
12//
13// Redistribution and use in source and binary forms, with or without
14// modification, are permitted provided that the following conditions are
15// met: redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer;
17// redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution;
20// neither the name of the copyright holders nor the names of its
21// contributors may be used to endorse or promote products derived from
22// this software without specific prior written permission.
23//
24// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36#ifndef __ARCH_GENERIC_VEC_PRED_REG_HH__
37#define __ARCH_GENERIC_VEC_PRED_REG_HH__
38
39#include <array>
40#include <cassert>
41#include <cstdint>
42#include <string>
43#include <type_traits>
44#include <vector>
45
46#include "base/cprintf.hh"
47#include "base/types.hh"
49
50namespace gem5
51{
52
53template <size_t NumBits, bool Packed>
55
72template <typename VecElem, size_t NumElems, bool Packed, bool Const>
74{
75 protected:
77 static constexpr size_t NUM_BITS = Packed ? NumElems :
78 sizeof(VecElem) * NumElems;
79
80 public:
82 using Container = typename std::conditional_t<
83 Const,
86
87 protected:
88 // Alias for this type
92
93 public:
95
97 template<bool Condition = !Const>
98 std::enable_if_t<Condition> reset() { container.reset(); }
99
101 template<bool Condition = !Const>
102 std::enable_if_t<Condition> set() { container.set(); }
103
104 template<bool Condition = !Const>
105 std::enable_if_t<Condition, MyClass&>
106 operator=(const MyClass& that)
107 {
108 container = that.container;
109 return *this;
110 }
111
112 const bool&
113 operator[](size_t idx) const
114 {
115 return container[idx * (Packed ? 1 : sizeof(VecElem))];
116 }
117
118 template<bool Condition = !Const>
119 std::enable_if_t<Condition, bool&>
120 operator[](size_t idx)
121 {
122 return container[idx * (Packed ? 1 : sizeof(VecElem))];
123 }
124
127 uint8_t
128 getRaw(size_t idx) const
129 {
130 return container.getBits(idx * (Packed ? 1 : sizeof(VecElem)),
131 (Packed ? 1 : sizeof(VecElem)));
132 }
133
135 template<bool Condition = !Const>
136 std::enable_if_t<Condition>
137 setRaw(size_t idx, uint8_t val)
138 {
139 container.setBits(idx * (Packed ? 1 : sizeof(VecElem)),
140 (Packed ? 1 : sizeof(VecElem)), val);
141 }
142
144 template<typename VE2, size_t NE2, bool P2, bool C2>
145 bool
147 {
148 return container == that.container;
149 }
150
152 template<typename VE2, size_t NE2, bool P2, bool C2>
153 bool
155 {
156 return !operator==(that);
157 }
158
159 friend std::ostream&
160 operator<<(std::ostream& os, const MyClass& p)
161 {
162 // Size must be greater than 0.
163 for (int i = 0; i < NUM_BITS; i++)
164 ccprintf(os, "%s%d", i ? " " : "[", (int)p.container[i]);
165 ccprintf(os, "]");
166 return os;
167 }
168
173 template <bool MC>
174 bool
176 size_t actual_num_elems) const
177 {
178 assert(actual_num_elems <= NumElems);
179 for (int i = 0; i < actual_num_elems; ++i) {
180 if (mask[i]) {
181 return (*this)[i];
182 }
183 }
184 return false;
185 }
186
191 template <bool MC>
192 bool
194 size_t actual_num_elems) const
195 {
196 assert(actual_num_elems <= NumElems);
197 for (int i = 0; i < actual_num_elems; ++i) {
198 if (mask[i] && operator[](i)) {
199 return false;
200 }
201 }
202 return true;
203 }
204
209 template <bool MC>
210 bool
212 size_t actual_num_elems) const
213 {
214 assert(actual_num_elems <= NumElems);
215 for (int i = actual_num_elems - 1; i >= 0; --i) {
216 if (mask[i]) {
217 return operator[](i);
218 }
219 }
220 return false;
221 }
222};
223
230template <size_t NumBits, bool Packed>
232{
233 static_assert(NumBits > 0,
234 "Size of a predicate register must be > 0");
235
236 public:
237 static constexpr size_t NUM_BITS = NumBits;
238 using Container = std::array<bool, NumBits>;
239
240 private:
242 // Alias for this type
244
245 public:
248
249 MyClass&
250 operator=(const MyClass& that)
251 {
252 if (&that == this)
253 return *this;
254 container = that.container;
255 return *this;
256 }
257
259 MyClass&
261 {
262 assert(that.size() == NUM_BITS);
263 std::copy(that.begin(), that.end(), container.begin());
264 return *this;
265 }
266
268 void
270 {
271 container.fill(false);
272 }
273
275 void
277 {
278 container.fill(true);
279 }
280
282 template<size_t N2, bool P2>
283 inline bool
285 {
286 return NumBits == N2 && Packed == P2 && container == that.container;
287 }
288
290 template<size_t N2, bool P2>
291 bool
293 {
294 return !operator==(that);
295 }
296
298 bool& operator[](size_t idx) { return container[idx]; }
299
302 const bool& operator[](size_t idx) const { return container[idx]; }
303
306 uint8_t
307 getBits(size_t idx, uint8_t nbits) const
308 {
309 assert(nbits > 0 && nbits <= 8 && (idx + nbits - 1) < NumBits);
310 uint8_t v = 0;
311 idx = idx + nbits - 1;
312 for (int i = 0; i < nbits; ++i, --idx) {
313 v <<= 1;
314 v |= container[idx];
315 }
316 return v;
317 }
318
321 void
322 setBits(size_t idx, uint8_t nbits, uint8_t bval)
323 {
324 assert(nbits > 0 && nbits <= 8 && (idx + nbits - 1) < NumBits);
325 for (int i = 0; i < nbits; ++i, ++idx) {
326 container[idx] = bval & 1;
327 bval >>= 1;
328 }
329 }
330
331 friend std::ostream&
332 operator<<(std::ostream& os, const MyClass& p)
333 {
334 // Size must be greater than 0.
335 for (int i = 0; i < NumBits; i++)
336 ccprintf(os, "%s%d", i ? " " : "[", (int)p.container[i]);
337 ccprintf(os, "]");
338 return os;
339 }
340
342
347 template <typename VecElem>
348 auto
349 as() const
350 {
351 static_assert(NumBits % sizeof(VecElem) == 0,
352 "Container size incompatible with view size.");
353 return VecPredRegT<VecElem,
354 Packed ? NumBits : (NumBits / sizeof(VecElem)),
355 Packed, true>(*this);
356 }
357
358 template <typename VecElem>
359 auto
361 {
362 static_assert(NumBits % sizeof(VecElem) == 0,
363 "Container size incompatible with view size.");
364 return VecPredRegT<VecElem,
365 Packed ? NumBits : (NumBits / sizeof(VecElem)),
366 Packed, false>(*this);
367 }
369};
370
371template <size_t NumBits, bool Packed>
372struct ParseParam<VecPredRegContainer<NumBits, Packed>>
373{
374 static bool
375 parse(const std::string &s, VecPredRegContainer<NumBits, Packed> &value)
376 {
377 int i = 0;
378 for (const auto& c: s)
379 value[i++] = (c == '1');
380 return true;
381 }
382};
383
384template <size_t NumBits, bool Packed>
385struct ShowParam<VecPredRegContainer<NumBits, Packed>>
386{
387 static void
388 show(std::ostream &os, const VecPredRegContainer<NumBits, Packed> &value)
389 {
390 for (auto b: value.container)
391 ccprintf(os, "%d", b);
392 }
393};
394
399{
401 bool operator == (const DummyVecPredRegContainer &d) const { return true; }
402 bool operator != (const DummyVecPredRegContainer &d) const { return true; }
403 template <typename VecElem>
404 VecElem *as() { return nullptr; }
405};
406template <>
408{
409 static bool
410 parse(const std::string &s, DummyVecPredRegContainer &value)
411 {
412 return false;
413 }
414};
415static_assert(sizeof(DummyVecPredRegContainer) == sizeof(RegVal));
416static inline std::ostream &
417operator<<(std::ostream &os, const DummyVecPredRegContainer &d)
418{
419 return os;
420}
422
423} // namespace gem5
424
425#endif // __ARCH_GENERIC_VEC_PRED_REG_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
Generic predicate register container.
void set()
Sets the predicate register to an all-true value.
MyClass & operator=(const std::vector< uint8_t > &that)
Required for de-serialization.
static constexpr size_t NUM_BITS
VecPredRegContainer(const VecPredRegContainer &)=default
const bool & operator[](size_t idx) const
Returns a const reference to a specific element of the internal container.
void setBits(size_t idx, uint8_t nbits, uint8_t bval)
Set a subset of bits starting from a specific element in the container.
friend std::ostream & operator<<(std::ostream &os, const MyClass &p)
VecPredRegContainer< NumBits, Packed > MyClass
bool & operator[](size_t idx)
Returns a reference to a specific element of the internal container.
auto as() const
Create a view of this container.
uint8_t getBits(size_t idx, uint8_t nbits) const
Returns a subset of bits starting from a specific element in the container.
bool operator!=(const VecPredRegContainer< N2, P2 > &that) const
Inequality operator, required to compare thread contexts.
MyClass & operator=(const MyClass &that)
void reset()
Resets the predicate register to an all-false register.
std::array< bool, NumBits > Container
bool operator==(const VecPredRegContainer< N2, P2 > &that) const
Equality operator, required to compare thread contexts.
Predicate register view.
bool lastActive(const VecPredRegT< VecElem, NumElems, Packed, MC > &mask, size_t actual_num_elems) const
Returns true if the last active element of the register is true.
VecPredRegT(Container &c)
bool operator==(const VecPredRegT< VE2, NE2, P2, C2 > &that) const
Equality operator, required to compare thread contexts.
std::enable_if_t< Condition, MyClass & > operator=(const MyClass &that)
typename std::conditional_t< Const, const VecPredRegContainer< NUM_BITS, Packed >, VecPredRegContainer< NUM_BITS, Packed > > Container
Container type alias.
bool firstActive(const VecPredRegT< VecElem, NumElems, Packed, MC > &mask, size_t actual_num_elems) const
Returns true if the first active element of the register is true.
std::enable_if_t< Condition > setRaw(size_t idx, uint8_t val)
Write a raw value in an element of the predicate register.
friend std::ostream & operator<<(std::ostream &os, const MyClass &p)
uint8_t getRaw(size_t idx) const
Return an element of the predicate register as it appears in the raw (untyped) internal representatio...
static constexpr size_t NUM_BITS
Size of the register in bits.
bool operator!=(const VecPredRegT< VE2, NE2, P2, C2 > &that) const
Inequality operator, required to compare thread contexts.
const bool & operator[](size_t idx) const
bool noneActive(const VecPredRegT< VecElem, NumElems, Packed, MC > &mask, size_t actual_num_elems) const
Returns true if there are no active elements in the register.
std::enable_if_t< Condition, bool & > operator[](size_t idx)
Container & container
Container corresponding to this view.
std::enable_if_t< Condition > reset()
Reset the register to an all-false value.
std::enable_if_t< Condition > set()
Reset the register to an all-true value.
STL vector class.
Definition stl.hh:37
Bitfield< 28 > v
Definition misc_types.hh:54
Bitfield< 3, 0 > mask
Definition pcstate.hh:63
Bitfield< 4 > s
Bitfield< 7 > b
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 29 > c
Definition misc_types.hh:53
VecPredReg::Container VecPredRegContainer
Definition vec.hh:71
uint32_t VecElem
Definition vec.hh:63
Bitfield< 9 > d
Definition misc_types.hh:64
Bitfield< 0 > p
Bitfield< 17 > os
Definition misc.hh:838
Bitfield< 63 > val
Definition misc.hh:804
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
uint64_t RegVal
Definition types.hh:173
static std::ostream & operator<<(std::ostream &os, const DummyMatRegContainer &d)
Definition matrix.hh:564
void ccprintf(cp::Print &print)
Definition cprintf.hh:130
Dummy type aliases and constants for architectures that do not implement vector predicate registers.
bool operator==(const DummyVecPredRegContainer &d) const
bool operator!=(const DummyVecPredRegContainer &d) const
static bool parse(const std::string &s, DummyVecPredRegContainer &value)
static bool parse(const std::string &s, VecPredRegContainer< NumBits, Packed > &value)
static void show(std::ostream &os, const VecPredRegContainer< NumBits, Packed > &value)

Generated on Tue Jun 18 2024 16:23:57 for gem5 by doxygen 1.11.0