gem5  v19.0.0.0
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  * Authors: Giacomo Gabrielli
38  * Nathanael Premillieu
39  * Rekai Gonzalez
40  */
41 
144 #ifndef __ARCH_GENERIC_VEC_REG_HH__
145 #define __ARCH_GENERIC_VEC_REG_HH__
146 
147 #include <array>
148 #include <cassert>
149 #include <iostream>
150 #include <string>
151 #include <type_traits>
152 #include <vector>
153 
154 #include "base/cprintf.hh"
155 #include "base/logging.hh"
156 
157 constexpr unsigned MaxVecRegLenInBytes = 4096;
158 
159 template <size_t Sz>
161 
173 template <typename VecElem, size_t NumElems, bool Const>
174 class VecRegT
175 {
177  static constexpr size_t SIZE = sizeof(VecElem) * NumElems;
178  public:
180  using Container = typename std::conditional<Const,
181  const VecRegContainer<SIZE>,
182  VecRegContainer<SIZE>>::type;
183  private:
188 
189  public:
191  VecRegT(Container& cnt) : container(cnt) {};
192 
194  template<bool Condition = !Const>
196  zero() { container.zero(); }
197 
198  template<bool Condition = !Const>
200  operator=(const MyClass& that)
201  {
202  container = that.container;
203  return *this;
204  }
205 
207  const VecElem& operator[](size_t idx) const
208  {
209  return container.template raw_ptr<VecElem>()[idx];
210  }
211 
213  template<bool Condition = !Const>
215  operator[](size_t idx)
216  {
217  return container.template raw_ptr<VecElem>()[idx];
218  }
219 
223  template<typename VE2, size_t NE2, bool C2>
224  bool
226  {
227  return container == that.container;
228  }
232  template<typename VE2, size_t NE2, bool C2>
233  bool
235  {
236  return !operator==(that);
237  }
238 
240  friend std::ostream&
241  operator<<(std::ostream& os, const MyClass& vr)
242  {
243  /* 0-sized is not allowed */
244  os << "[" << std::hex << (uint32_t)vr[0];
245  for (uint32_t e = 1; e < vr.SIZE; e++)
246  os << " " << std::hex << (uint32_t)vr[e];
247  os << ']';
248  return os;
249  }
250 
251  const std::string print() const { return csprintf("%s", *this); }
257  operator Container&() { return container; }
258 };
259 
260 /* Forward declaration. */
261 template <typename VecElem, bool Const>
262 class VecLaneT;
263 
271 template <size_t Sz>
272 class VecRegContainer
273 {
274  static_assert(Sz > 0,
275  "Cannot create Vector Register Container of zero size");
276  static_assert(Sz <= MaxVecRegLenInBytes,
277  "Vector Register size limit exceeded");
278  public:
279  static constexpr size_t SIZE = Sz;
280  using Container = std::array<uint8_t,Sz>;
281  private:
284 
285  public:
287  VecRegContainer(const VecRegContainer &) = default;
288  /* This is required for de-serialisation. */
290  {
291  assert(that.size() >= SIZE);
292  std::memcpy(container.data(), &that[0], SIZE);
293  }
294 
296  void zero() { memset(container.data(), 0, SIZE); }
297 
301  MyClass& operator=(const MyClass& that)
302  {
303  if (&that == this)
304  return *this;
305  memcpy(container.data(), that.container.data(), SIZE);
306  return *this;
307  }
308 
311  {
312  std::memcpy(container.data(), that.data(), SIZE);
313  return *this;
314  }
315 
320  {
321  assert(that.size() >= SIZE);
322  std::memcpy(container.data(), that.data(), SIZE);
323  return *this;
324  }
330  void copyTo(Container& dst) const
331  {
332  std::memcpy(dst.data(), container.data(), SIZE);
333  }
334 
338  void copyTo(std::vector<uint8_t>& dst) const
339  {
340  dst.resize(SIZE);
341  std::memcpy(dst.data(), container.data(), SIZE);
342  }
348  template<size_t S2>
349  inline bool
350  operator==(const VecRegContainer<S2>& that) const
351  {
352  return SIZE == S2 &&
353  !memcmp(container.data(), that.container.data(), SIZE);
354  }
358  template<size_t S2>
359  bool
360  operator!=(const VecRegContainer<S2>& that) const
361  {
362  return !operator==(that);
363  }
364 
365  const std::string print() const { return csprintf("%s", *this); }
367  template <typename Ret>
368  const Ret* raw_ptr() const { return (const Ret*)container.data(); }
369 
370  template <typename Ret>
371  Ret* raw_ptr() { return (Ret*)container.data(); }
372 
384  template <typename VecElem, size_t NumElems = SIZE/sizeof(VecElem)>
386  {
387  static_assert(SIZE % sizeof(VecElem) == 0,
388  "VecElem does not evenly divide the register size");
389  static_assert(sizeof(VecElem) * NumElems <= SIZE,
390  "Viewing VecReg as something bigger than it is");
391  return VecRegT<VecElem, NumElems, true>(*this);
392  }
393 
394  template <typename VecElem, size_t NumElems = SIZE/sizeof(VecElem)>
396  {
397  static_assert(SIZE % sizeof(VecElem) == 0,
398  "VecElem does not evenly divide the register size");
399  static_assert(sizeof(VecElem) * NumElems <= SIZE,
400  "Viewing VecReg as something bigger than it is");
401  return VecRegT<VecElem, NumElems, false>(*this);
402  }
403 
404  template <typename VecElem, int LaneIdx>
405  VecLaneT<VecElem, false> laneView();
406  template <typename VecElem, int LaneIdx>
407  VecLaneT<VecElem, true> laneView() const;
408  template <typename VecElem>
409  VecLaneT<VecElem, false> laneView(int laneIdx);
410  template <typename VecElem>
411  VecLaneT<VecElem, true> laneView(int laneIdx) const;
417  friend std::ostream& operator<<(std::ostream& os, const MyClass& v)
418  {
419  for (auto& b: v.container) {
420  os << csprintf("%02x", b);
421  }
422  return os;
423  }
424 };
425 
431 enum class LaneSize
432 {
433  Empty = 0,
434  Byte,
435  TwoByte,
436  FourByte,
437  EightByte,
438 };
439 
456 template <LaneSize LS>
457 class LaneData
458 {
459  public:
461  using UnderlyingType =
462  typename std::conditional<LS == LaneSize::EightByte, uint64_t,
463  typename std::conditional<LS == LaneSize::FourByte, uint32_t,
464  typename std::conditional<LS == LaneSize::TwoByte, uint16_t,
465  typename std::conditional<LS == LaneSize::Byte, uint8_t,
466  void>::type
467  >::type
468  >::type
470  private:
471  static constexpr auto ByteSz = sizeof(UnderlyingType);
474 
475  public:
476  template <typename T> explicit
477  LaneData(typename std::enable_if<sizeof(T) == ByteSz, const T&>::type t)
478  : _val(t) {}
479 
480  template <typename T>
482  operator=(const T& that)
483  {
484  _val = that;
485  return *this;
486  }
487  template<typename T,
489  operator T() const {
490  return *static_cast<const T*>(&_val);
491  }
492 };
493 
495 template <LaneSize LS>
496 inline std::ostream&
497 operator<<(std::ostream& os, const LaneData<LS>& d)
498 {
499  return os << static_cast<typename LaneData<LS>::UnderlyingType>(d);
500 }
501 
509 /* General */
510 template <typename VecElem, bool Const>
511 class VecLaneT
512 {
513  public:
519 
520  /*template <size_t Sz>
521  friend class VecRegContainer;*/
522  friend class VecRegContainer<8>;
523  friend class VecRegContainer<16>;
524  friend class VecRegContainer<32>;
525  friend class VecRegContainer<64>;
526  friend class VecRegContainer<128>;
527  friend class VecRegContainer<256>;
529 
532 
533  private:
534  using Cont = typename std::conditional<Const,
535  const VecElem,
536  VecElem>::type;
537  static_assert(!std::is_const<VecElem>::value || Const,
538  "Asked for non-const lane of const type!");
539  static_assert(std::is_integral<VecElem>::value,
540  "VecElem type is not integral!");
542  Cont& container;
543 
545  VecLaneT(Cont& cont) : container(cont) { }
546 
547  public:
553  template <bool Assignable = !Const>
555  operator=(const VecElem& that) {
556  container = that;
557  return *this;
558  }
564  template <bool Assignable = !Const, typename T>
566  operator=(const T& that) {
567  static_assert(sizeof(T) >= sizeof(VecElem),
568  "Attempt to perform widening bitwise copy.");
569  static_assert(sizeof(T) <= sizeof(VecElem),
570  "Attempt to perform narrowing bitwise copy.");
571  container = static_cast<VecElem>(that);
572  return *this;
573  }
576  operator VecElem() const { return container; }
577 
581  {
583  }
584 };
585 
586 namespace std {
587  template<typename T, bool Const>
588  struct add_const<VecLaneT<T, Const>> { typedef VecLaneT<T, true> type; };
589 }
590 
592 template <size_t Sz>
593 template <typename VecElem, int LaneIdx>
596 {
597  return VecLaneT<VecElem, false>(as<VecElem>()[LaneIdx]);
598 }
599 
601 template <size_t Sz>
602 template <typename VecElem, int LaneIdx>
605 {
606  return VecLaneT<VecElem, true>(as<VecElem>()[LaneIdx]);
607 }
608 
610 template <size_t Sz>
611 template <typename VecElem>
614 {
615  return VecLaneT<VecElem, false>(as<VecElem>()[laneIdx]);
616 }
617 
619 template <size_t Sz>
620 template <typename VecElem>
623 {
624  return VecLaneT<VecElem, true>(as<VecElem>()[laneIdx]);
625 }
626 
631 
636 
641 template <size_t Sz>
642 inline bool
643 to_number(const std::string& value, VecRegContainer<Sz>& v)
644 {
645  fatal_if(value.size() > 2 * VecRegContainer<Sz>::SIZE,
646  "Vector register value overflow at unserialize");
647 
648  for (int i = 0; i < VecRegContainer<Sz>::SIZE; i++) {
649  uint8_t b = 0;
650  if (2 * i < value.size())
651  b = stoul(value.substr(i * 2, 2), nullptr, 16);
652  v.template raw_ptr<uint8_t>()[i] = b;
653  }
654  return true;
655 }
663 using DummyVecElem = uint32_t;
664 constexpr unsigned DummyNumVecElemPerVecReg = 2;
668 constexpr size_t DummyVecRegSizeBytes = DummyNumVecElemPerVecReg *
669  sizeof(DummyVecElem);
672 #endif /* __ARCH_GENERIC_VEC_REG_HH__ */
Ret * raw_ptr()
Definition: vec_reg.hh:371
UnderlyingType _val
Definition: vec_reg.hh:472
std::enable_if< Condition, VecElem & >::type operator[](size_t idx)
Index operator.
Definition: vec_reg.hh:215
void zero()
Zero the container.
Definition: vec_reg.hh:296
typename std::conditional< LS==LaneSize::EightByte, uint64_t, typename std::conditional< LS==LaneSize::FourByte, uint32_t, typename std::conditional< LS==LaneSize::TwoByte, uint16_t, typename std::conditional< LS==LaneSize::Byte, uint8_t, void >::type >::type >::type >::type UnderlyingType
Alias to the native type of the appropriate size.
Definition: vec_reg.hh:469
LaneData(typename std::enable_if< sizeof(T)==ByteSz, const T &>::type t)
Definition: vec_reg.hh:477
Bitfield< 28 > v
Bitfield< 7 > i
VecRegT< VecElem, NumElems, true > as() const
View interposers.
Definition: vec_reg.hh:385
VecRegT(Container &cnt)
Constructor.
Definition: vec_reg.hh:191
VecRegContainer(const std::vector< uint8_t > &that)
Definition: vec_reg.hh:289
constexpr unsigned DummyNumVecElemPerVecReg
Definition: vec_reg.hh:664
Vector Register Abstraction This generic class is the model in a particularization of MVC...
Definition: vec_reg.hh:160
bool operator==(const VecRegContainer< S2 > &that) const
Equality operator.
Definition: vec_reg.hh:350
const VecElem & operator[](size_t idx) const
Index operator.
Definition: vec_reg.hh:207
static constexpr size_t SIZE
Size of the register in bytes.
Definition: vec_reg.hh:177
LaneSize
We define an auxiliary abstraction for LaneData.
Definition: vec_reg.hh:431
friend std::ostream & operator<<(std::ostream &os, const MyClass &vr)
Output stream operator.
Definition: vec_reg.hh:241
const Ret * raw_ptr() const
Get pointer to bytes.
Definition: vec_reg.hh:368
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:586
std::enable_if< Condition, void >::type zero()
Zero the container.
Definition: vec_reg.hh:196
uint32_t DummyVecElem
Dummy type aliases and constants for architectures that do not implement vector registers.
Definition: vec_reg.hh:663
VecLaneT(Cont &cont)
Constructor.
Definition: vec_reg.hh:545
std::enable_if< sizeof(T)==ByteSz, MyClass & >::type operator=(const T &that)
Definition: vec_reg.hh:482
typename std::conditional< Const, const VecRegContainer< SIZE >, VecRegContainer< SIZE > >::type Container
Container type alias.
Definition: vec_reg.hh:182
friend std::ostream & operator<<(std::ostream &os, const MyClass &v)
Output operator.
Definition: vec_reg.hh:417
Bitfield< 17 > os
Definition: misc.hh:805
bool to_number(const std::string &value, VecRegContainer< Sz > &v)
Calls required for serialization/deserialization.
Definition: vec_reg.hh:643
Bitfield< 7 > b
void copyTo(Container &dst) const
Copy the contents into the input buffer.
Definition: vec_reg.hh:330
LaneSize is an abstraction of a LS byte value for the execution and thread contexts to handle values ...
Definition: vec_reg.hh:457
uint8_t type
Definition: inet.hh:333
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:162
std::enable_if< Assignable, MyClass & >::type operator=(const T &that)
Generic.
Definition: vec_reg.hh:566
::DummyVecRegContainer VecRegContainer
Definition: registers.hh:53
Bitfield< 9 > d
std::enable_if< Assignable, MyClass & >::type operator=(const VecElem &that)
Assignment operators.
Definition: vec_reg.hh:555
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:203
Container & container
Reference to container.
Definition: vec_reg.hh:187
DummyVecReg::Container DummyVecRegContainer
Definition: vec_reg.hh:667
void copyTo(std::vector< uint8_t > &dst) const
To vector<uint8_t> This is required for serialisation.
Definition: vec_reg.hh:338
bool operator!=(const VecRegT< VE2, NE2, C2 > &that) const
Inequality operator.
Definition: vec_reg.hh:234
constexpr unsigned MaxVecRegLenInBytes
Definition: vec_reg.hh:157
MyClass & operator=(const Container &that)
From appropriately sized uint8_t[].
Definition: vec_reg.hh:310
const std::string print() const
Definition: vec_reg.hh:251
std::enable_if< Condition, MyClass & >::type operator=(const MyClass &that)
Definition: vec_reg.hh:200
Container container
Definition: vec_reg.hh:282
Bitfield< 9 > e
MyClass & operator=(const std::vector< uint8_t > &that)
From vector<uint8_t>.
Definition: vec_reg.hh:319
Cont & container
Reference to data.
Definition: vec_reg.hh:538
MyClass & operator=(const MyClass &that)
Assignment operators.
Definition: vec_reg.hh:301
typename std::conditional< Const, const VecElem, VecElem >::type Cont
Definition: vec_reg.hh:536
VecLaneT< VecElem, false > laneView()
View as the Nth lane of type VecElem.
Definition: vec_reg.hh:595
Vector Register Abstraction This generic class is a view in a particularization of MVC...
Definition: vec_reg.hh:174
::DummyVecElem VecElem
Definition: registers.hh:50
VecRegT< VecElem, NumElems, false > as()
Definition: vec_reg.hh:395
Bitfield< 5 > t
bool operator==(const VecRegT< VE2, NE2, C2 > &that) const
Equality operator.
Definition: vec_reg.hh:225
bool operator!=(const VecRegContainer< S2 > &that) const
Inequality operator.
Definition: vec_reg.hh:360
Vector Lane abstraction Another view of a container.
Definition: vec_reg.hh:262
std::array< uint8_t, Sz > Container
Definition: vec_reg.hh:280
constexpr size_t DummyVecRegSizeBytes
Definition: vec_reg.hh:668
const std::string print() const
Definition: vec_reg.hh:365

Generated on Fri Feb 28 2020 16:26:57 for gem5 by doxygen 1.8.13