CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
L1GctTwosComplement.h
Go to the documentation of this file.
1 #ifndef L1GCTETTYPES_H
2 #define L1GCTETTYPES_H
3 
4 #include <boost/cstdint.hpp>
5 #include <ostream>
6 
28 template <int nBits>
30  public:
34  L1GctTwosComplement(uint32_t raw);
39 
41  template <int mBits>
43 
45  void reset();
46 
48  void setRaw(uint32_t raw);
49 
51  void setValue(int value);
52 
54  void setOverFlow(bool oflow) { m_overFlow = oflow; }
55 
57  uint32_t raw() const { return m_data; }
58 
60  int value() const;
61 
63  bool overFlow() const { return m_overFlow; }
64 
66  int size() const { return m_nBits; }
67 
70 
73 
75  L1GctTwosComplement& operator= (int value);
76 
77  private:
78 
79  // number of bits (for overflow checking)
80  int m_nBits;
81 
82  // the raw data
83  uint32_t m_data;
84 
85  // the overflow bit
86  bool m_overFlow;
87 
88  static const int MAX_NBITS = 24;
89  static const int MAX_VALUE = 1<<(MAX_NBITS-1);
90 
91  // PRIVATE MEMBER FUNCTION
92  // function to check overflow
93  void checkOverFlow(uint32_t rawValue, uint32_t &maskValue, bool &overFlow);
94 };
95 
96 template <int nBits>
97 std::ostream& operator<<(std::ostream& s, const L1GctTwosComplement<nBits>& data);
98 
99 // construct with # bits and set to zero
100 template <int nBits>
102  m_nBits = nBits>0 && nBits<MAX_NBITS ? nBits : 16 ;
103  this->reset();
104 }
105 
106 // construct from # bits and raw data
107 template <int nBits>
109  m_nBits = nBits>0 && nBits<MAX_NBITS ? nBits : 16 ;
110  m_overFlow = false;
111  this->setRaw(raw);
112 }
113 
114 // construct from # bits and value
115 template <int nBits>
117  m_nBits = nBits>0 && nBits<MAX_NBITS ? nBits : 16 ;
118  m_overFlow = false;
119  this->setValue(value);
120 }
121 
122 // copy contructor to move data between
123 // representations with different numbers of bits
124 template <int nBits>
125 template <int mBits>
127  m_nBits = nBits>0 && nBits<MAX_NBITS ? nBits : 16 ;
128  this->setRaw( rhs.raw() );
129  this->setOverFlow( this->overFlow() || rhs.overFlow() );
130 }
131 
132 // reset value and overflow to zero
133 template <int nBits>
135  m_data = static_cast<uint32_t>(0);
136  m_overFlow = false;
137 }
138 
139 // set value from uint32_t
140 template <int nBits>
142  checkOverFlow(raw, m_data, m_overFlow);
143 }
144 
145 // set value from int
146 template <int nBits>
148  int chkValue, posValue;
149  uint32_t raw;
150 
151  // Make sure we have an integer in the range MAX_NBITS
152  chkValue = value;
153  if (chkValue<-MAX_VALUE) { chkValue = -MAX_VALUE; m_overFlow = true; }
154  if (chkValue>=MAX_VALUE) { chkValue = MAX_VALUE-1; m_overFlow = true; }
155 
156  // Transform negative values to large positive values
157  posValue = chkValue<0 ? chkValue + (1<<MAX_NBITS) : chkValue ;
158  raw = static_cast<uint32_t>(posValue);
159 
160  // Use the setRaw method to check overflow for our given size nBits
161  this->setRaw(raw);
162 }
163 
164 // return value as int
165 template <int nBits>
167  int value, result;
168  int maxValueInNbits;
169  maxValueInNbits = 1<<(m_nBits-1);
170  value = static_cast<int>(m_data);
171  result = value < maxValueInNbits ? value : value - (1<<MAX_NBITS) ;
172  return result;
173 }
174 
175 // add two numbers
176 template <int nBits>
179 
180  // temporary variable for storing the result (need to set its size)
182  uint32_t sum;
183  bool ofl;
184 
185  // do the addition here
186  sum = this->raw() + rhs.raw();
187  ofl = this->overFlow() || rhs.overFlow();
188 
189  //fill the temporary argument
190  temp.setRaw(sum);
191  temp.setOverFlow(temp.overFlow() || ofl);
192 
193  // return the temporary
194  return temp;
195 
196 }
197 
198 // overload unary - (negation) operator
199 template <int nBits>
201 
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 
212  this->setValue(value);
213  return *this;
214 
215 }
216 
217 // Here's the check overflow function
218 template <int nBits>
219 void L1GctTwosComplement<nBits>::checkOverFlow(uint32_t rawValue, uint32_t &maskValue, bool &overFlow) {
220  uint32_t signBit = 1<<(m_nBits-1);
221  uint32_t signExtendBits = (static_cast<uint32_t>(MAX_VALUE)-signBit)<<1;
222  // Consider and return only MAX_NBITS least significant bits
223  uint32_t mskRawValue = rawValue & ((1<<MAX_NBITS)-1);
224  uint32_t value;
225  bool ofl;
226 
227  if ((mskRawValue&signBit)==0) {
228  value = mskRawValue & ~signExtendBits;
229  } else {
230  value = mskRawValue | signExtendBits;
231  }
232  ofl = value != mskRawValue;
233 
234  maskValue = value;
235  overFlow = ofl;
236 
237 }
238 
239 // overload ostream<<
240 template <int nBits>
241 std::ostream& operator<<(std::ostream& s, const L1GctTwosComplement<nBits>& data) {
242 
243  s << "L1GctTwosComplement<" << data.size() << "> raw : " << data.raw() << ", " << "value : " << data.value();
244  if (data.overFlow()) { s << " Overflow set! "; }
245 
246  return s;
247 
248 }
249 
250 
251 #endif
bool overFlow() const
access overflow
void setOverFlow(bool oflow)
set the overflow bit
void setRaw(uint32_t raw)
set the raw data
uint32_t raw() const
access raw data
~L1GctTwosComplement()
Destructor.
bool setValue(Container &, const reco::JetBaseRef &, const JetExtendedData &)
associate jet with value. Returns false and associate nothing if jet is already associated ...
Definition of signed integer types with overflow.
tuple result
Definition: query.py:137
static const int MAX_VALUE
void setValue(int value)
set value from signed int
void checkOverFlow(uint32_t rawValue, uint32_t &maskValue, bool &overFlow)
L1GctTwosComplement()
Construct a signed integer with initial value zero.
int value() const
access value as signed int
L1GctTwosComplement operator-() const
overload unary - (negation) operator
int size() const
return number of bits
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void reset()
reset value and overflow to zero
L1GctTwosComplement operator+(const L1GctTwosComplement &rhs) const
add two numbers of the same size
void reset(double vett[256])
Definition: TPedValues.cc:11
static const int MAX_NBITS
L1GctTwosComplement & operator=(int value)
overload = operator