CMS 3D CMS Logo

L1GctTwosComplement.h
Go to the documentation of this file.
1 #ifndef L1GCTETTYPES_H
2 #define L1GCTETTYPES_H
3 
4 #include <ostream>
5 #include <cstdint>
6 
27 template <int nBits>
29 public:
33  L1GctTwosComplement(uint32_t raw);
38 
40  template <int mBits>
42 
44  void reset();
45 
47  void setRaw(uint32_t raw);
48 
50  void setValue(int value);
51 
53  void setOverFlow(bool oflow) { m_overFlow = oflow; }
54 
56  uint32_t raw() const { return m_data; }
57 
59  int value() const;
60 
62  bool overFlow() const { return m_overFlow; }
63 
65  int size() const { return m_nBits; }
66 
69 
72 
75 
76 private:
77  // number of bits (for overflow checking)
78  int m_nBits;
79 
80  // the raw data
81  uint32_t m_data;
82 
83  // the overflow bit
84  bool m_overFlow;
85 
86  static const int MAX_NBITS = 24;
87  static const int MAX_VALUE = 1 << (MAX_NBITS - 1);
88 
89  // PRIVATE MEMBER FUNCTION
90  // function to check overflow
91  void checkOverFlow(uint32_t rawValue, uint32_t& maskValue, bool& overFlow);
92 };
93 
94 template <int nBits>
95 std::ostream& operator<<(std::ostream& s, const L1GctTwosComplement<nBits>& data);
96 
97 // construct with # bits and set to zero
98 template <int nBits>
100  m_nBits = nBits > 0 && nBits < MAX_NBITS ? nBits : 16;
101  this->reset();
102 }
103 
104 // construct from # bits and raw data
105 template <int nBits>
107  m_nBits = nBits > 0 && nBits < MAX_NBITS ? nBits : 16;
108  m_overFlow = false;
109  this->setRaw(raw);
110 }
111 
112 // construct from # bits and value
113 template <int nBits>
115  m_nBits = nBits > 0 && nBits < MAX_NBITS ? nBits : 16;
116  m_overFlow = false;
117  this->setValue(value);
118 }
119 
120 // copy contructor to move data between
121 // representations with different numbers of bits
122 template <int nBits>
123 template <int mBits>
125  m_nBits = nBits > 0 && nBits < MAX_NBITS ? nBits : 16;
126  this->setRaw(rhs.raw());
127  this->setOverFlow(this->overFlow() || rhs.overFlow());
128 }
129 
130 // reset value and overflow to zero
131 template <int nBits>
133  m_data = static_cast<uint32_t>(0);
134  m_overFlow = false;
135 }
136 
137 // set value from uint32_t
138 template <int nBits>
140  checkOverFlow(raw, m_data, m_overFlow);
141 }
142 
143 // set value from int
144 template <int nBits>
146  int chkValue, posValue;
147  uint32_t raw;
148 
149  // Make sure we have an integer in the range MAX_NBITS
150  chkValue = value;
151  if (chkValue < -MAX_VALUE) {
152  chkValue = -MAX_VALUE;
153  m_overFlow = true;
154  }
155  if (chkValue >= MAX_VALUE) {
156  chkValue = MAX_VALUE - 1;
157  m_overFlow = true;
158  }
159 
160  // Transform negative values to large positive values
161  posValue = chkValue < 0 ? chkValue + (1 << MAX_NBITS) : chkValue;
162  raw = static_cast<uint32_t>(posValue);
163 
164  // Use the setRaw method to check overflow for our given size nBits
165  this->setRaw(raw);
166 }
167 
168 // return value as int
169 template <int nBits>
171  int value, result;
172  int maxValueInNbits;
173  maxValueInNbits = 1 << (m_nBits - 1);
174  value = static_cast<int>(m_data);
175  result = value < maxValueInNbits ? value : value - (1 << MAX_NBITS);
176  return result;
177 }
178 
179 // add two numbers
180 template <int nBits>
182  // temporary variable for storing the result (need to set its size)
184  uint32_t sum;
185  bool ofl;
186 
187  // do the addition here
188  sum = this->raw() + rhs.raw();
189  ofl = this->overFlow() || rhs.overFlow();
190 
191  //fill the temporary argument
192  temp.setRaw(sum);
193  temp.setOverFlow(temp.overFlow() || ofl);
194 
195  // return the temporary
196  return temp;
197 }
198 
199 // overload unary - (negation) operator
200 template <int nBits>
203  temp.setValue(-this->value());
204  temp.setOverFlow(temp.overFlow() || this->overFlow());
205  return temp;
206 }
207 
208 // overload assignment by int
209 template <int nBits>
211  this->setValue(value);
212  return *this;
213 }
214 
215 // Here's the check overflow function
216 template <int nBits>
217 void L1GctTwosComplement<nBits>::checkOverFlow(uint32_t rawValue, uint32_t& maskValue, bool& overFlow) {
218  uint32_t signBit = 1 << (m_nBits - 1);
219  uint32_t signExtendBits = (static_cast<uint32_t>(MAX_VALUE) - signBit) << 1;
220  // Consider and return only MAX_NBITS least significant bits
221  uint32_t mskRawValue = rawValue & ((1 << MAX_NBITS) - 1);
222  uint32_t value;
223  bool ofl;
224 
225  if ((mskRawValue & signBit) == 0) {
226  value = mskRawValue & ~signExtendBits;
227  } else {
228  value = mskRawValue | signExtendBits;
229  }
230  ofl = value != mskRawValue;
231 
232  maskValue = value;
233  overFlow = ofl;
234 }
235 
236 // overload ostream<<
237 template <int nBits>
238 std::ostream& operator<<(std::ostream& s, const L1GctTwosComplement<nBits>& data) {
239  s << "L1GctTwosComplement<" << data.size() << "> raw : " << data.raw() << ", "
240  << "value : " << data.value();
241  if (data.overFlow()) {
242  s << " Overflow set! ";
243  }
244 
245  return s;
246 }
247 
248 #endif
reco::JetExtendedAssociation::setValue
bool setValue(Container &, const reco::JetBaseRef &, const JetExtendedData &)
associate jet with value. Returns false and associate nothing if jet is already associated
Definition: JetExtendedAssociation.cc:44
L1GctTwosComplement::m_nBits
int m_nBits
Definition: L1GctTwosComplement.h:78
L1GctTwosComplement::checkOverFlow
void checkOverFlow(uint32_t rawValue, uint32_t &maskValue, bool &overFlow)
Definition: L1GctTwosComplement.h:217
operator<<
std::ostream & operator<<(std::ostream &s, const L1GctTwosComplement< nBits > &data)
Definition: L1GctTwosComplement.h:238
L1GctTwosComplement::operator+
L1GctTwosComplement operator+(const L1GctTwosComplement &rhs) const
add two numbers of the same size
Definition: L1GctTwosComplement.h:181
L1GctTwosComplement::value
int value() const
access value as signed int
Definition: L1GctTwosComplement.h:170
L1GctTwosComplement::MAX_VALUE
static const int MAX_VALUE
Definition: L1GctTwosComplement.h:87
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
L1GctTwosComplement::operator-
L1GctTwosComplement operator-() const
overload unary - (negation) operator
Definition: L1GctTwosComplement.h:201
L1GctTwosComplement::setValue
void setValue(int value)
set value from signed int
Definition: L1GctTwosComplement.h:145
alignCSCRings.s
s
Definition: alignCSCRings.py:92
L1GctTwosComplement::size
int size() const
return number of bits
Definition: L1GctTwosComplement.h:65
L1GctTwosComplement::m_overFlow
bool m_overFlow
Definition: L1GctTwosComplement.h:84
L1GctTwosComplement
Definition of signed integer types with overflow.
Definition: L1GctTwosComplement.h:28
L1GctTwosComplement::L1GctTwosComplement
L1GctTwosComplement()
Construct a signed integer with initial value zero.
Definition: L1GctTwosComplement.h:99
value
Definition: value.py:1
L1GctTwosComplement::operator=
L1GctTwosComplement & operator=(int value)
overload = operator
Definition: L1GctTwosComplement.h:210
L1GctTwosComplement::MAX_NBITS
static const int MAX_NBITS
Definition: L1GctTwosComplement.h:86
L1GctTwosComplement::m_data
uint32_t m_data
Definition: L1GctTwosComplement.h:81
relativeConstraints.value
value
Definition: relativeConstraints.py:53
L1GctTwosComplement::overFlow
bool overFlow() const
access overflow
Definition: L1GctTwosComplement.h:62
L1GctTwosComplement::setRaw
void setRaw(uint32_t raw)
set the raw data
Definition: L1GctTwosComplement.h:139
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
reset
void reset(double vett[256])
Definition: TPedValues.cc:11
L1GctTwosComplement::raw
uint32_t raw() const
access raw data
Definition: L1GctTwosComplement.h:56
mps_fire.result
result
Definition: mps_fire.py:303
L1GctTwosComplement::setOverFlow
void setOverFlow(bool oflow)
set the overflow bit
Definition: L1GctTwosComplement.h:53
L1GctTwosComplement::reset
void reset()
reset value and overflow to zero
Definition: L1GctTwosComplement.h:132
L1GctTwosComplement::~L1GctTwosComplement
~L1GctTwosComplement()
Destructor.
Definition: L1GctTwosComplement.h:37