CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
L1GctTwosComplement< nBits > Class Template Reference

Definition of signed integer types with overflow. More...

#include <L1GctTwosComplement.h>

Public Member Functions

 L1GctTwosComplement ()
 Construct a signed integer with initial value zero. More...
 
template<int mBits>
 L1GctTwosComplement (const L1GctTwosComplement< mBits > &rhs)
 Copy contructor to move data between representations with different numbers of bits. More...
 
 L1GctTwosComplement (int value)
 Construct a signed integer, checking for overflow. More...
 
 L1GctTwosComplement (uint32_t raw)
 Construct a signed integer from raw data, checking for overFlow. More...
 
L1GctTwosComplement operator+ (const L1GctTwosComplement &rhs) const
 add two numbers of the same size More...
 
L1GctTwosComplement operator- () const
 overload unary - (negation) operator More...
 
L1GctTwosComplementoperator= (int value)
 overload = operator More...
 
bool overFlow () const
 access overflow More...
 
uint32_t raw () const
 access raw data More...
 
void reset ()
 reset value and overflow to zero More...
 
void setOverFlow (bool oflow)
 set the overflow bit More...
 
void setRaw (uint32_t raw)
 set the raw data More...
 
void setValue (int value)
 set value from signed int More...
 
int size () const
 return number of bits More...
 
int value () const
 access value as signed int More...
 
 ~L1GctTwosComplement ()
 Destructor. More...
 

Private Member Functions

void checkOverFlow (uint32_t rawValue, uint32_t &maskValue, bool &overFlow)
 

Private Attributes

uint32_t m_data
 
int m_nBits
 
bool m_overFlow
 

Static Private Attributes

static const int MAX_NBITS = 24
 
static const int MAX_VALUE = 1 << (MAX_NBITS - 1)
 

Detailed Description

template<int nBits>
class L1GctTwosComplement< nBits >

Definition of signed integer types with overflow.

This file defines the template class L1GctTwosComplement. It is used to store energy values that are represented in a given number of bits in hardware. The type has a built-in overFlow that is set if the value to be represented is outside the allowed range for that number of bits. This type represents signed integers; unsigned integers are represented by L1GctUnsignedInt. Functions are defined to add two values, and to copy data into a different number of bits.

this header file contains method definitions because these are template classes see http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

Author
Jim Brooke & Greg Heath
Date
May 2006

Definition at line 28 of file L1GctTwosComplement.h.

Constructor & Destructor Documentation

◆ L1GctTwosComplement() [1/4]

template<int nBits>
L1GctTwosComplement< nBits >::L1GctTwosComplement ( )

Construct a signed integer with initial value zero.

Definition at line 99 of file L1GctTwosComplement.h.

99  {
100  m_nBits = nBits > 0 && nBits < MAX_NBITS ? nBits : 16;
101  this->reset();
102 }

◆ L1GctTwosComplement() [2/4]

template<int nBits>
L1GctTwosComplement< nBits >::L1GctTwosComplement ( uint32_t  raw)

Construct a signed integer from raw data, checking for overFlow.

Definition at line 106 of file L1GctTwosComplement.h.

106  {
107  m_nBits = nBits > 0 && nBits < MAX_NBITS ? nBits : 16;
108  m_overFlow = false;
109  this->setRaw(raw);
110 }

◆ L1GctTwosComplement() [3/4]

template<int nBits>
L1GctTwosComplement< nBits >::L1GctTwosComplement ( int  value)

Construct a signed integer, checking for overflow.

Definition at line 114 of file L1GctTwosComplement.h.

114  {
115  m_nBits = nBits > 0 && nBits < MAX_NBITS ? nBits : 16;
116  m_overFlow = false;
117  this->setValue(value);
118 }

◆ ~L1GctTwosComplement()

template<int nBits>
L1GctTwosComplement< nBits >::~L1GctTwosComplement ( )
inline

Destructor.

Definition at line 37 of file L1GctTwosComplement.h.

37 {}

◆ L1GctTwosComplement() [4/4]

template<int nBits>
template<int mBits>
L1GctTwosComplement< nBits >::L1GctTwosComplement ( const L1GctTwosComplement< mBits > &  rhs)

Copy contructor to move data between representations with different numbers of bits.

Definition at line 124 of file L1GctTwosComplement.h.

124  {
125  m_nBits = nBits > 0 && nBits < MAX_NBITS ? nBits : 16;
126  this->setRaw(rhs.raw());
127  this->setOverFlow(this->overFlow() || rhs.overFlow());
128 }

Member Function Documentation

◆ checkOverFlow()

template<int nBits>
void L1GctTwosComplement< nBits >::checkOverFlow ( uint32_t  rawValue,
uint32_t &  maskValue,
bool &  overFlow 
)
private

Definition at line 217 of file L1GctTwosComplement.h.

217  {
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 }

◆ operator+()

template<int nBits>
L1GctTwosComplement< nBits > L1GctTwosComplement< nBits >::operator+ ( const L1GctTwosComplement< nBits > &  rhs) const

add two numbers of the same size

Definition at line 181 of file L1GctTwosComplement.h.

181  {
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 }

◆ operator-()

template<int nBits>
L1GctTwosComplement< nBits > L1GctTwosComplement< nBits >::operator- ( ) const

overload unary - (negation) operator

Definition at line 201 of file L1GctTwosComplement.h.

201  {
203  temp.setValue(-this->value());
204  temp.setOverFlow(temp.overFlow() || this->overFlow());
205  return temp;
206 }

◆ operator=()

template<int nBits>
L1GctTwosComplement< nBits > & L1GctTwosComplement< nBits >::operator= ( int  value)

overload = operator

Definition at line 210 of file L1GctTwosComplement.h.

210  {
211  this->setValue(value);
212  return *this;
213 }

◆ overFlow()

template<int nBits>
bool L1GctTwosComplement< nBits >::overFlow ( ) const
inline

◆ raw()

template<int nBits>
uint32_t L1GctTwosComplement< nBits >::raw ( ) const
inline

◆ reset()

template<int nBits>
void L1GctTwosComplement< nBits >::reset ( void  )

reset value and overflow to zero

Definition at line 132 of file L1GctTwosComplement.h.

132  {
133  m_data = static_cast<uint32_t>(0);
134  m_overFlow = false;
135 }

Referenced by L1GctWheelEnergyFpga::resetProcessor(), L1GctWheelJetFpga::resetProcessor(), L1GctJetLeafCard::resetProcessor(), and L1GctGlobalEnergyAlgos::resetProcessor().

◆ setOverFlow()

template<int nBits>
void L1GctTwosComplement< nBits >::setOverFlow ( bool  oflow)
inline

◆ setRaw()

template<int nBits>
void L1GctTwosComplement< nBits >::setRaw ( uint32_t  raw)

set the raw data

Definition at line 139 of file L1GctTwosComplement.h.

139  {
141 }

◆ setValue()

template<int nBits>
void L1GctTwosComplement< nBits >::setValue ( int  value)

set value from signed int

Definition at line 145 of file L1GctTwosComplement.h.

145  {
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 }

Referenced by L1GctGlobalEnergyAlgos::setInputWheelEx(), L1GctGlobalEnergyAlgos::setInputWheelEy(), L1GctGlobalEnergyAlgos::setInputWheelHx(), and L1GctGlobalEnergyAlgos::setInputWheelHy().

◆ size()

template<int nBits>
int L1GctTwosComplement< nBits >::size ( void  ) const
inline

return number of bits

Definition at line 65 of file L1GctTwosComplement.h.

65 { return m_nBits; }

Referenced by ntupleDataFormat._Collection::__iter__(), and ntupleDataFormat._Collection::__len__().

◆ value()

template<int nBits>
int L1GctTwosComplement< nBits >::value ( void  ) const

access value as signed int

Definition at line 170 of file L1GctTwosComplement.h.

170  {
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 }

Referenced by average.Average::average(), L1GctMet::inputOverFlow(), and L1GctMet::metVector().

Member Data Documentation

◆ m_data

template<int nBits>
uint32_t L1GctTwosComplement< nBits >::m_data
private

◆ m_nBits

template<int nBits>
int L1GctTwosComplement< nBits >::m_nBits
private

◆ m_overFlow

template<int nBits>
bool L1GctTwosComplement< nBits >::m_overFlow
private

◆ MAX_NBITS

template<int nBits>
const int L1GctTwosComplement< nBits >::MAX_NBITS = 24
staticprivate

Definition at line 86 of file L1GctTwosComplement.h.

◆ MAX_VALUE

template<int nBits>
const int L1GctTwosComplement< nBits >::MAX_VALUE = 1 << (MAX_NBITS - 1)
staticprivate

Definition at line 87 of file L1GctTwosComplement.h.

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
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::setValue
void setValue(int value)
set value from signed int
Definition: L1GctTwosComplement.h:145
L1GctTwosComplement::m_overFlow
bool m_overFlow
Definition: L1GctTwosComplement.h:84
L1GctTwosComplement
Definition of signed integer types with overflow.
Definition: L1GctTwosComplement.h:28
value
Definition: value.py:1
L1GctTwosComplement::MAX_NBITS
static const int MAX_NBITS
Definition: L1GctTwosComplement.h:86
L1GctTwosComplement::m_data
uint32_t m_data
Definition: L1GctTwosComplement.h:81
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
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