OpenASIP  2.0
SimValue.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2014 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file SimValue.hh
26  *
27  * Declaration of SimValue class.
28  *
29  * @author Pekka Jääskeläinen 2004,2010,2014 (pjaaskel-no.spam-cs.tut.fi)
30  * @author Mikko Jarvela 2013, 2014 (mikko.jarvela-no.spam-.tut.fi)
31  * @note This file is used in compiled simulation. Keep dependencies *clean*
32  * @note rating: red
33  */
34 
35 #ifndef TTA_SIM_VALUE_HH
36 #define TTA_SIM_VALUE_HH
37 
38 #include "BaseType.hh"
39 #include "HalfFloatWord.hh"
40 #include <string.h>
41 
42 #define SIMD_WORD_WIDTH 4096
43 #define SIMVALUE_MAX_BYTE_SIZE (SIMD_WORD_WIDTH / BYTE_BITWIDTH)
44 
45 class TCEString;
46 
47 //////////////////////////////////////////////////////////////////////////////
48 // SimValue
49 //////////////////////////////////////////////////////////////////////////////
50 
51 /**
52  * Class that represents values in simulation.
53  *
54  * This class represents any data type that can be manipulated by operations
55  * of the target architecture template, and provides the interface to access
56  * the data in predefined types.
57  *
58  * Values are always (regardless of the endianness of the machine) stored in
59  * little-endian convention in the rawData_ byte array. This is to model
60  * closer the internal registers and buses where the convention is to:
61  *
62  * - store the smallest vector elements to the smallest bit position,
63  * in the SimValue case, the smallest bytes in the storage array
64  * - store the elements in little-endian format
65  *
66  * This is to avoid the need to implement two variations of function unit
67  * implementations, for both endianness modes.
68  *
69  * This also means that big endian machines need endianness-aware
70  * load/stores as they need to swap the elements to the little-endian
71  * "internal format". However, as we lean towards using little-endian
72  * with vector machines (for example due to buggy LLVM BE/vector code gen),
73  * it means we usually use endianness-unaware "chunk" memory operations
74  * that can be uses both for scalar and vector data.
75  *
76  * When a user wants to interpret SimValue as any primitive value
77  * (FloatWord, UIntWord, etc.), depending on the user's machine endianness
78  * the interpreted bytes are swapped correctly to be either in big-endian
79  * or little-endian convention. For instance, if the user has a big-endian
80  * machine and calls the uIntWordValue() function for a SimValue, which has
81  * the above value, it gets swapped so the OSAL operations can treat the
82  * result as a host integer to model the computation with.
83  *
84  * The same swapping convention also occurs when a primitive value is
85  * assigned to SimValue. If the value to be assigned is in big-endian
86  * and its bytes are 0xabcd0000, the last four bytes in the SimValue are
87  * as 0x0000cdab.
88  *
89  * SimValue users don't need to worry about the possible byte swapping
90  * since it is automatic and is done only if the user's machine is a
91  * little-endian machine. However, users shouldn't access the public
92  * rawData_ member directly unless they know exactly what they are doing,
93  * and always use the accessors for getting/setting lane data.
94  */
95 
96 class SimValue {
97 public:
98  // We should use a separate type for 64b ints when they are implemented.
100  SimValue();
101  explicit SimValue(int width);
102  explicit SimValue(SLongWord value, int width);
103  SimValue(const SimValue& source);
105 
106  int width() const;
107  void setBitWidth(int width);
108 
109  SimValue& operator=(const SIntWord& source);
110  SimValue& operator=(const UIntWord& source);
111  SimValue& operator=(const SLongWord& source);
112  SimValue& operator=(const ULongWord& source);
113  SimValue& operator=(const HalfFloatWord& source);
114  SimValue& operator=(const FloatWord& source);
115  SimValue& operator=(const DoubleWord& source);
116  SimValue& operator=(const SimValue& source);
117  void deepCopy(const SimValue& source);
118 
119  const SimValue operator+(const SIntWord& rightHand);
120  const SimValue operator+(const UIntWord& rightHand);
121  const SimValue operator+(const SLongWord& rightHand);
122  const SimValue operator+(const ULongWord& rightHand);
123  const SimValue operator+(const HalfFloatWord& rightHand);
124  const SimValue operator+(const FloatWord& rightHand);
125  const SimValue operator+(const DoubleWord& rightHand);
126 
127  const SimValue operator-(const SIntWord& rightHand);
128  const SimValue operator-(const UIntWord& rightHand);
129  const SimValue operator-(const SLongWord& rightHand);
130  const SimValue operator-(const ULongWord& rightHand);
131  const SimValue operator-(const HalfFloatWord& rightHand);
132  const SimValue operator-(const FloatWord& rightHand);
133  const SimValue operator-(const DoubleWord& rightHand);
134 
135  const SimValue operator/(const SIntWord& rightHand);
136  const SimValue operator/(const UIntWord& rightHand);
137  const SimValue operator/(const SLongWord& rightHand);
138  const SimValue operator/(const ULongWord& rightHand);
139  const SimValue operator/(const HalfFloatWord& rightHand);
140  const SimValue operator/(const FloatWord& rightHand);
141  const SimValue operator/(const DoubleWord& rightHand);
142 
143  const SimValue operator*(const SIntWord& rightHand);
144  const SimValue operator*(const UIntWord& rightHand);
145  const SimValue operator*(const SLongWord& rightHand);
146  const SimValue operator*(const ULongWord& rightHand);
147  const SimValue operator*(const HalfFloatWord& rightHand);
148  const SimValue operator*(const FloatWord& rightHand);
149  const SimValue operator*(const DoubleWord& rightHand);
150 
151  int operator==(const SimValue& rightHand) const;
152  int operator==(const SIntWord& rightHand) const;
153  int operator==(const UIntWord& rightHand) const;
154  int operator==(const SLongWord& rightHand) const;
155  int operator==(const ULongWord& rightHand) const;
156  int operator==(const HalfFloatWord& rightHand) const;
157  int operator==(const FloatWord& rightHand) const;
158  int operator==(const DoubleWord& rightHand) const;
159 
160  int intValue() const;
161  unsigned int unsignedValue() const;
162 
163  SIntWord sIntWordValue() const;
164  UIntWord uIntWordValue() const;
165  SLongWord sLongWordValue() const;
166  ULongWord uLongWordValue() const;
167  DoubleWord doubleWordValue() const;
168  FloatWord floatWordValue() const;
170 
171  TCEString binaryValue() const;
172  TCEString hexValue(bool noHexIdentifier = false) const;
173 
174  Word wordElement(size_t elementIndex) const;
175  SIntWord sIntWordElement(size_t elementIndex) const;
176  UIntWord uIntWordElement(size_t elementIndex) const;
177  HalfWord halfWordElement(size_t elementIndex) const;
178  HalfFloatWord halfFloatElement(size_t elementIndex) const;
179  FloatWord floatElement(size_t elementIndex) const;
180  DoubleFloatWord doubleFloatElement(size_t elementIndex) const;
181  Byte byteElement(size_t elementIndex) const;
182  UIntWord bitElement(size_t elementIndex) const;
183  Word element(size_t elementIndex, size_t elementWidth) const;
184 
185  void setWordElement(size_t elementIndex, Word data);
186  void setHalfWordElement(size_t elementIndex, HalfWord data);
187  void setByteElement(size_t elementIndex, Byte data);
188  void setBitElement(size_t elementIndex, UIntWord data);
189  void setElement(size_t elementIndex, size_t elementWidth, Word data);
190  void setHalfFloatElement(size_t elementIndex, HalfFloatWord data);
191  void setFloatElement(size_t elementIndex, FloatWord data);
192  void setDoubleFloatElement(size_t elementIndex, DoubleFloatWord data);
193 
195  void clearToZero(int bitWidth);
196  void clearToZero();
197  void signExtendTo(int bitWidth);
198  void zeroExtendTo(int bitWidth);
199  TCEString dump() const;
200 
201  /// Array that contains SimValue's underlaying bytes in little endian.
203 
204  /// The bitwidth of the value.
206 
207 private:
208 
209  template <typename T>
210  T vectorElement(size_t elementIndex) const;
211  template <typename T>
212  void setVectorElement(size_t elementIndex, T data);
213 
214  /// @todo This currently works, but there could be more optimal 8-byte,
215  /// 4-byte and 2-byte swapper functions for 2/4/8 byte swaps. The more
216  /// optimal swappers would load all bytes to 8, 4 or 2-byte values and
217  /// shift invidivual bytes to their correct places, which would reduce
218  /// memory accesses. Or use some intrincs for optimal execution and
219  /// better code density.
220  void swapByteOrder(const Byte* from, size_t byteCount, Byte* to) const;
221 
222  /// Mask for masking extra bits when returning unsigned value.
224 
225 };
226 
227 //////////////////////////////////////////////////////////////////////////////
228 // NullSimValue
229 //////////////////////////////////////////////////////////////////////////////
230 
231 /**
232  * Singleton class that is used to represent a null SimValue.
233  *
234  * All methods cause program abort with an error log message.
235  *
236  */
238 public:
239  static SimValue& instance();
240 
241 private:
242  NullSimValue();
243 
245 
246 };
247 
248 #define SIMULATOR_MAX_INTWORD_BITWIDTH 32
249 #define SIMULATOR_MAX_LONGWORD_BITWIDTH 64
250 
251 #endif
SimValue::sIntWordElement
SIntWord sIntWordElement(size_t elementIndex) const
Definition: SimValue.cc:1227
SimValue::setElement
void setElement(size_t elementIndex, size_t elementWidth, Word data)
Definition: SimValue.cc:1422
SimValue::intValue
int intValue() const
Definition: SimValue.cc:895
UIntWord
Word UIntWord
Definition: BaseType.hh:144
SimValue::setBitWidth
void setBitWidth(int width)
Definition: SimValue.cc:113
BaseType.hh
SimValue::element
Word element(size_t elementIndex, size_t elementWidth) const
Definition: SimValue.cc:1311
SimValue::doubleWordValue
DoubleWord doubleWordValue() const
Definition: SimValue.cc:1052
SimValue::sLongWordValue
SLongWord sLongWordValue() const
Definition: SimValue.cc:997
SimValue::DoubleFloatWord
DoubleWord DoubleFloatWord
Definition: SimValue.hh:99
SimValue::SimValue
SimValue()
Definition: SimValue.cc:47
SimValue::setFloatElement
void setFloatElement(size_t elementIndex, FloatWord data)
Definition: SimValue.cc:1376
SimValue::bitElement
UIntWord bitElement(size_t elementIndex) const
Definition: SimValue.cc:1288
SimValue::operator/
const SimValue operator/(const SIntWord &rightHand)
Definition: SimValue.cc:568
SimValue::setWordElement
void setWordElement(size_t elementIndex, Word data)
Definition: SimValue.cc:1354
NullSimValue::instance
static SimValue & instance()
Definition: SimValue.cc:1642
SimValue::setVectorElement
void setVectorElement(size_t elementIndex, T data)
Definition: SimValue.cc:1339
SimValue::floatElement
FloatWord floatElement(size_t elementIndex) const
Definition: SimValue.cc:1261
SimValue::operator=
SimValue & operator=(const SIntWord &source)
Definition: SimValue.cc:138
HalfFloatWord
Definition: HalfFloatWord.hh:41
Byte
unsigned char Byte
Definition: BaseType.hh:116
SimValue::uIntWordValue
UIntWord uIntWordValue() const
Definition: SimValue.cc:972
SimValue::wordElement
Word wordElement(size_t elementIndex) const
Definition: SimValue.cc:1219
SimValue::vectorElement
T vectorElement(size_t elementIndex) const
Definition: SimValue.cc:1187
SimValue
Definition: SimValue.hh:96
SimValue::zeroExtendTo
void zeroExtendTo(int bitWidth)
Definition: SimValue.cc:1567
SIMVALUE_MAX_BYTE_SIZE
#define SIMVALUE_MAX_BYTE_SIZE
Definition: SimValue.hh:43
SimValue::~SimValue
~SimValue()
Definition: SimValue.hh:104
SimValue::dump
TCEString dump() const
Definition: SimValue.cc:1593
SimValue::rawData_
Byte rawData_[SIMVALUE_MAX_BYTE_SIZE]
Array that contains SimValue's underlaying bytes in little endian.
Definition: SimValue.hh:202
NullSimValue::instance_
static SimValue instance_
Definition: SimValue.hh:244
SimValue::mask_
ULongWord mask_
Mask for masking extra bits when returning unsigned value.
Definition: SimValue.hh:223
SimValue::swapByteOrder
void swapByteOrder(const Byte *from, size_t byteCount, Byte *to) const
Definition: SimValue.cc:1621
FloatWord
float FloatWord
Definition: BaseType.hh:160
SimValue::floatWordValue
FloatWord floatWordValue() const
Definition: SimValue.cc:1075
SimValue::setHalfFloatElement
void setHalfFloatElement(size_t elementIndex, HalfFloatWord data)
Definition: SimValue.cc:1371
SimValue::setBitElement
void setBitElement(size_t elementIndex, UIntWord data)
Definition: SimValue.cc:1397
SimValue::uLongWordValue
ULongWord uLongWordValue() const
Definition: SimValue.cc:1027
SIntWord
SignedWord SIntWord
Definition: BaseType.hh:149
SimValue::clearToZero
void clearToZero()
Definition: SimValue.cc:1516
SimValue::setDoubleFloatElement
void setDoubleFloatElement(size_t elementIndex, DoubleFloatWord data)
Definition: SimValue.cc:1381
SimValue::uIntWordElement
UIntWord uIntWordElement(size_t elementIndex) const
Definition: SimValue.cc:1240
DoubleWord
double DoubleWord
Definition: BaseType.hh:166
SimValue::byteElement
Byte byteElement(size_t elementIndex) const
Definition: SimValue.cc:1274
SimValue::operator*
const SimValue operator*(const SIntWord &rightHand)
Definition: SimValue.cc:680
SimValue::operator==
int operator==(const SimValue &rightHand) const
Definition: SimValue.cc:780
SimValue::setHalfWordElement
void setHalfWordElement(size_t elementIndex, HalfWord data)
Definition: SimValue.cc:1366
SimValue::doubleFloatElement
DoubleFloatWord doubleFloatElement(size_t elementIndex) const
Definition: SimValue.cc:1266
SimValue::hexValue
TCEString hexValue(bool noHexIdentifier=false) const
Definition: SimValue.cc:1150
SimValue::operator+
const SimValue operator+(const SIntWord &rightHand)
Definition: SimValue.cc:327
SimValue::unsignedValue
unsigned int unsignedValue() const
Definition: SimValue.cc:919
SimValue::sIntWordValue
SIntWord sIntWordValue() const
Definition: SimValue.cc:944
SimValue::signExtendTo
void signExtendTo(int bitWidth)
Definition: SimValue.cc:1531
SimValue::width
int width() const
Definition: SimValue.cc:103
SimValue::halfWordElement
HalfWord halfWordElement(size_t elementIndex) const
Definition: SimValue.cc:1249
SimValue::binaryValue
TCEString binaryValue() const
Definition: SimValue.cc:1121
TCEString
Definition: TCEString.hh:53
ULongWord
unsigned long ULongWord
Definition: BaseType.hh:51
NullSimValue
Definition: SimValue.hh:237
SimValue::setValue
void setValue(TCEString hexValue)
Definition: SimValue.cc:1460
SimValue::setByteElement
void setByteElement(size_t elementIndex, Byte data)
Definition: SimValue.cc:1386
SimValue::bitWidth_
int bitWidth_
The bitwidth of the value.
Definition: SimValue.hh:205
SimValue::operator-
const SimValue operator-(const SIntWord &rightHand)
Definition: SimValue.cc:456
SLongWord
long SLongWord
Definition: BaseType.hh:52
HalfFloatWord.hh
SimValue::halfFloatWordValue
HalfFloatWord halfFloatWordValue() const
Definition: SimValue.cc:1098
NullSimValue::NullSimValue
NullSimValue()
SimValue::halfFloatElement
HalfFloatWord halfFloatElement(size_t elementIndex) const
Definition: SimValue.cc:1254
SimValue::deepCopy
void deepCopy(const SimValue &source)
Definition: SimValue.cc:307