Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:30:51

0001 #include "L1Trigger/TrackFindingTracklet/interface/FPGAWord.h"
0002 
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 
0005 using namespace std;
0006 using namespace trklet;
0007 
0008 FPGAWord::FPGAWord() {}
0009 
0010 FPGAWord::FPGAWord(int value, int nbits, bool positive, int line, const char* file) {
0011   set(value, nbits, positive, line, file);
0012 }
0013 
0014 void FPGAWord::set(int value, int nbits, bool positive, int line, const char* file) {
0015   value_ = value;
0016   nbits_ = nbits;
0017   positive_ = positive;
0018   if (positive) {
0019     if (value < 0) {
0020       edm::LogProblem("Tracklet") << "FPGAWord got negative value:" << value << " (" << file << ":" << line << ")";
0021     }
0022     assert(value >= 0);
0023   }
0024   if (nbits >= 22) {
0025     edm::LogPrint("Tracklet") << "FPGAWord got too many bits:" << nbits << " (" << file << ":" << line << ")";
0026   }
0027   assert(nbits < 22);
0028   if (nbits <= 0) {
0029     edm::LogPrint("Tracklet") << "FPGAWord got too few bits:" << nbits << " (" << file << ":" << line << ")";
0030   }
0031   assert(nbits > 0);
0032   if (positive) {
0033     if (value >= (1 << nbits)) {
0034       if (file != nullptr) {
0035         edm::LogProblem("Tracklet") << "value too large:" << value << " " << (1 << nbits) << " (" << file << ":" << line
0036                                     << ")";
0037       }
0038     }
0039     assert(value < (1 << nbits));
0040   } else {
0041     if (value > (1 << (nbits - 1))) {
0042       edm::LogProblem("Tracklet") << "value too large:" << value << " " << (1 << (nbits - 1)) << " (" << file << ":"
0043                                   << line << ")";
0044     }
0045     assert(value <= (1 << (nbits - 1)));
0046     if (value < -(1 << (nbits - 1))) {
0047       edm::LogProblem("Tracklet") << "value too negative:" << value << " " << -(1 << (nbits - 1)) << " (" << file << ":"
0048                                   << line << ")";
0049     }
0050     assert(value >= -(1 << (nbits - 1)));
0051   }
0052 }
0053 
0054 std::string FPGAWord::str() const {
0055   const int nbit = nbits_;
0056 
0057   if (!(nbit > 0 && nbit < 22))
0058     edm::LogVerbatim("Tracklet") << "nbit: " << nbit;
0059   if (nbit == -1)
0060     return "?";
0061   if (nbit == 0)
0062     return "~";
0063 
0064   int valtmp = value_;
0065   string str = "";
0066   for (int i = 0; i < nbit; i++) {
0067     str = ((valtmp & 1) ? "1" : "0") + str;
0068     valtmp >>= 1;
0069   }
0070 
0071   return str;
0072 }
0073 
0074 unsigned int FPGAWord::bits(unsigned int lsb, unsigned int nbit) const {
0075   assert(lsb + nbit <= (unsigned int)nbits());
0076   return (value_ >> lsb) & ((1 << nbit) - 1);
0077 }
0078 
0079 bool FPGAWord::atExtreme() const {
0080   if (positive_) {
0081     return (value_ == 0) || (value_ == (1 << nbits_) - 1);
0082   }
0083   return ((value_ == (-(1 << (nbits_ - 1)))) || (value_ == ((1 << (nbits_ - 1)) - 1)));
0084 }
0085 
0086 bool FPGAWord::operator==(const FPGAWord& other) const {
0087   return (value_ == other.value_) && (nbits_ == other.nbits_) && (positive_ == other.positive_);
0088 }