CMS 3D CMS Logo

FPGAWord.cc
Go to the documentation of this file.
2 
4 
5 using namespace std;
6 using namespace trklet;
7 
8 FPGAWord::FPGAWord() {}
9 
10 FPGAWord::FPGAWord(int value, int nbits, bool positive, int line, const char* file) {
11  set(value, nbits, positive, line, file);
12 }
13 
14 void FPGAWord::set(int value, int nbits, bool positive, int line, const char* file) {
15  value_ = value;
16  nbits_ = nbits;
17  positive_ = positive;
18  if (positive) {
19  if (value < 0) {
20  edm::LogProblem("Tracklet") << "FPGAWord got negative value:" << value << " (" << file << ":" << line << ")";
21  }
22  assert(value >= 0);
23  }
24  if (nbits >= 22) {
25  edm::LogPrint("Tracklet") << "FPGAWord got too many bits:" << nbits << " (" << file << ":" << line << ")";
26  }
27  assert(nbits < 22);
28  if (nbits <= 0) {
29  edm::LogPrint("Tracklet") << "FPGAWord got too few bits:" << nbits << " (" << file << ":" << line << ")";
30  }
31  assert(nbits > 0);
32  if (positive) {
33  if (value >= (1 << nbits)) {
34  if (file != nullptr) {
35  edm::LogProblem("Tracklet") << "value too large:" << value << " " << (1 << nbits) << " (" << file << ":" << line
36  << ")";
37  }
38  }
39  assert(value < (1 << nbits));
40  } else {
41  if (value >= (1 << (nbits - 1))) {
42  edm::LogProblem("Tracklet") << "value too large:" << value << " " << (1 << (nbits - 1)) - 1 << " (" << file << ":"
43  << line << ")";
44  }
45  assert(value < (1 << (nbits - 1)));
46  if (value < -(1 << (nbits - 1))) {
47  edm::LogProblem("Tracklet") << "value too negative:" << value << " " << -(1 << (nbits - 1)) << " (" << file << ":"
48  << line << ")";
49  }
50  assert(value >= -(1 << (nbits - 1)));
51  }
52 }
53 
55  const int nbit = nbits_;
56 
57  if (!(nbit > 0 && nbit < 22))
58  edm::LogVerbatim("Tracklet") << "nbit: " << nbit;
59  if (nbit == -1)
60  return "?";
61  if (nbit == 0)
62  return "~";
63 
64  int valtmp = value_;
65  string str = "";
66  for (int i = 0; i < nbit; i++) {
67  str = ((valtmp & 1) ? "1" : "0") + str;
68  valtmp >>= 1;
69  }
70 
71  return str;
72 }
73 
74 unsigned int FPGAWord::bits(unsigned int lsb, unsigned int nbit) const {
75  assert(lsb + nbit <= (unsigned int)nbits());
76  return (value_ >> lsb) & ((1 << nbit) - 1);
77 }
78 
79 bool FPGAWord::atExtreme() const {
80  if (positive_) {
81  return (value_ == 0) || (value_ == (1 << nbits_) - 1);
82  }
83  return ((value_ == (-(1 << (nbits_ - 1)))) || (value_ == ((1 << (nbits_ - 1)) - 1)));
84 }
85 
86 bool FPGAWord::operator==(const FPGAWord& other) const {
87  return (value_ == other.value_) && (nbits_ == other.nbits_) && (positive_ == other.positive_);
88 }
Log< level::Info, true > LogVerbatim
constexpr uint32_t bits
Definition: gpuClustering.h:23
assert(be >=bs)
bool operator==(const QGLikelihoodParameters &lhs, const QGLikelihoodCategory &rhs)
Test if parameters are compatible with category.
Definition: value.py:1
Log< level::Warning, true > LogPrint
#define str(s)
Log< level::Error, true > LogProblem