gem5  v22.1.0.0
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 
50 namespace gem5
51 {
52 
53 template <size_t NumBits, bool Packed>
55 
72 template <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 
230 template <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
276  set()
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
360  as()
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 
371 template <size_t NumBits, bool Packed>
372 struct 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 
384 template <size_t NumBits, bool Packed>
385 struct 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 };
406 template <>
408 {
409  static bool
410  parse(const std::string &s, DummyVecPredRegContainer &value)
411  {
412  return false;
413  }
414 };
415 static_assert(sizeof(DummyVecPredRegContainer) == sizeof(RegVal));
416 static inline std::ostream &
417 operator<<(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.
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.
MyClass & operator=(const std::vector< uint8_t > &that)
Required for de-serialization.
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
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[](size_t idx)
Returns a reference to a specific element of the internal 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.
Definition: vec_pred_reg.hh:74
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)
Definition: vec_pred_reg.hh:94
std::enable_if_t< Condition, bool & > operator[](size_t idx)
bool operator==(const VecPredRegT< VE2, NE2, P2, C2 > &that) const
Equality operator, required to compare thread contexts.
std::enable_if_t< Condition > set()
Reset the register to an all-true value.
std::enable_if_t< Condition > reset()
Reset the register to an all-false value.
Definition: vec_pred_reg.hh:98
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, MyClass & > operator=(const MyClass &that)
friend std::ostream & operator<<(std::ostream &os, const MyClass &p)
std::enable_if_t< Condition > setRaw(size_t idx, uint8_t val)
Write a raw value in an element of the predicate register.
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.
Definition: vec_pred_reg.hh:77
bool operator!=(const VecPredRegT< VE2, NE2, P2, C2 > &that) const
Inequality operator, required to compare thread contexts.
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.
const bool & operator[](size_t idx) const
Container & container
Container corresponding to this view.
Definition: vec_pred_reg.hh:91
typename std::conditional_t< Const, const VecPredRegContainer< NUM_BITS, Packed >, VecPredRegContainer< NUM_BITS, Packed > > Container
Container type alias.
Definition: vec_pred_reg.hh:85
constexpr uint64_t mask(unsigned nbits)
Generate a 64-bit mask of 'nbits' 1s, right justified.
Definition: bitfield.hh:63
VecPredReg::Container VecPredRegContainer
Definition: vec.hh:71
Bitfield< 7 > b
Definition: misc_types.hh:388
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 9 > d
Definition: misc_types.hh:64
uint32_t VecElem
Definition: vec.hh:63
Bitfield< 1 > s
Definition: pagetable.hh:64
Bitfield< 0 > v
Definition: pagetable.hh:65
Bitfield< 2 > c
Definition: pagetable.hh:63
Bitfield< 54 > p
Definition: pagetable.hh:70
Bitfield< 17 > os
Definition: misc.hh:810
Bitfield< 63 > val
Definition: misc.hh:776
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
std::ostream & operator<<(std::ostream &os, const ArmSemihosting::InPlaceArg &ipa)
uint64_t RegVal
Definition: types.hh:173
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 Wed Dec 21 2022 10:22:27 for gem5 by doxygen 1.9.1