CMS 3D CMS Logo

TTBV.h
Go to the documentation of this file.
1 #ifndef DataFormats_L1TrackTrigger_TTBV_h
2 #define DataFormats_L1TrackTrigger_TTBV_h
3 
4 #include <bitset>
5 #include <array>
6 #include <string>
7 #include <algorithm>
8 #include <cmath>
9 #include <utility>
10 #include <vector>
11 #include <iostream>
12 
20 class TTBV {
21 public:
22  static constexpr int S_ = 64; // Frame width of emp infrastructure f/w, max number of bits a TTBV can handle
23 
24 private:
25  bool twos_; // Two's complement (true) or binary (false)
26  int size_; // number or bits
27  std::bitset<S_> bs_; // underlying storage
28 
29 public:
30  // constructor: default
31  TTBV() : twos_(false), size_(0), bs_() {}
32 
33  // constructor: double precision (IEEE 754); from most to least significant bit: 1 bit sign + 11 bit binary exponent + 52 bit binary mantisse
34  TTBV(const double d) : twos_(false), size_(S_) {
35  int index(0);
36  const char* c = reinterpret_cast<const char*>(&d);
37  for (int iByte = 0; iByte < (int)sizeof(d); iByte++) {
38  const std::bitset<std::numeric_limits<unsigned char>::digits> byte(*(c + iByte));
39  for (int bit = 0; bit < std::numeric_limits<unsigned char>::digits; bit++)
40  bs_[index++] = byte[bit];
41  }
42  }
43 
44  // constructor: unsigned int value
45  TTBV(unsigned long long int value, int size) : twos_(false), size_(size), bs_(value) {}
46 
47  // constructor: int value
48  TTBV(int value, int size, bool twos = false)
49  : twos_(twos), size_(size), bs_((!twos || value >= 0) ? value : value + iMax()) {}
50 
51  // constructor: double value + precision, biased (floor) representation
52  TTBV(double value, double base, int size, bool twos = false) : TTBV((int)std::floor(value / base), size, twos) {}
53 
54  // constructor: string
55  TTBV(const std::string& str, bool twos = false) : twos_(twos), size_(str.size()), bs_(str) {}
56 
57  // constructor: bitset
58  TTBV(const std::bitset<S_>& bs, bool twos = false) : twos_(twos), size_(S_), bs_(bs) {}
59 
60  // constructor: slice reinterpret sign
61  TTBV(const TTBV& ttBV, int begin, int end = 0, bool twos = false) : twos_(twos), size_(begin - end), bs_(ttBV.bs_) {
62  bs_ <<= S_ - begin;
63  bs_ >>= S_ - begin + end;
64  }
65 
66  // Two's complement (true) or binary (false)
67  bool twos() const { return twos_; }
68  // number or bits
69  int size() const { return size_; }
70  // underlying storage
71  const std::bitset<S_>& bs() const { return bs_; }
72 
73  // access: single bit
74  bool operator[](int pos) const { return bs_[pos]; }
76 
77  // access: most significant bit copy
78  bool msb() const { return bs_[size_ - 1]; }
79 
80  // access: most significant bit reference
82 
83  // access: members of underlying bitset
84 
85  bool all() const { return bs_.all(); }
86  bool any() const { return bs_.any(); }
87  bool none() const { return bs_.none(); }
88  int count() const { return bs_.count(); }
89 
90  // operator: comparisons equal
91  bool operator==(const TTBV& rhs) const { return bs_ == rhs.bs_; }
92 
93  // operator: comparisons not equal
94  bool operator!=(const TTBV& rhs) const { return bs_ != rhs.bs_; }
95 
96  // operator: boolean and
97  TTBV& operator&=(const TTBV& rhs) {
98  const int m(std::max(size_, rhs.size()));
99  this->resize(m);
100  TTBV bv(rhs);
101  bv.resize(m);
102  bs_ &= bv.bs_;
103  return *this;
104  }
105 
106  // operator: boolean or
107  TTBV& operator|=(const TTBV& rhs) {
108  const int m(std::max(size_, rhs.size()));
109  this->resize(m);
110  TTBV bv(rhs);
111  bv.resize(m);
112  bs_ |= bv.bs_;
113  return *this;
114  }
115 
116  // operator: boolean xor
117  TTBV& operator^=(const TTBV& rhs) {
118  const int m(std::max(size_, rhs.size()));
119  this->resize(m);
120  TTBV bv(rhs);
121  bv.resize(m);
122  bs_ ^= bv.bs_;
123  return *this;
124  }
125 
126  // operator: not
127  TTBV operator~() const {
128  TTBV bv(*this);
129  return bv.flip();
130  }
131 
132  // reference operator: bit remove right
134  bs_ >>= pos;
135  size_ -= pos;
136  return *this;
137  }
138 
139  // reference operator: bit remove left
141  bs_ <<= S_ - size_ + pos;
142  bs_ >>= S_ - size_ + pos;
143  size_ -= pos;
144  return *this;
145  }
146 
147  // operator: bit remove left copy
148  TTBV operator<<(int pos) const {
149  TTBV bv(*this);
150  return bv <<= pos;
151  }
152 
153  // operator: bit remove right copy
154  TTBV operator>>(int pos) const {
155  TTBV bv(*this);
156  return bv >>= pos;
157  }
158 
159  // reference operator: concatenation
160  TTBV& operator+=(const TTBV& rhs) {
161  bs_ <<= rhs.size();
162  bs_ |= rhs.bs_;
163  size_ += rhs.size();
164  return *this;
165  }
166 
167  // operator: concatenation copy
168  TTBV operator+(const TTBV& rhs) const {
169  TTBV lhs(*this);
170  return lhs += rhs;
171  }
172 
173  // operator: value increment, overflow protected
175  bs_ = std::bitset<S_>(bs_.to_ullong() + 1);
176  this->resize(size_);
177  return *this;
178  }
179 
180  // manipulation: all bits set to 0
181  TTBV& reset() {
182  bs_.reset();
183  return *this;
184  }
185 
186  // manipulation: all bits set to 1
187  TTBV& set() {
188  for (int n = 0; n < size_; n++)
189  bs_.set(n);
190  return *this;
191  }
192 
193  // manipulation: all bits flip 1 to 0 and vice versa
194  TTBV& flip() {
195  for (int n = 0; n < size_; n++)
196  bs_.flip(n);
197  return *this;
198  }
199 
200  // manipulation: single bit set to 0
201  TTBV& reset(int pos) {
202  bs_.reset(pos);
203  return *this;
204  }
205 
206  // manipulation: single bit set to 1
207  TTBV& set(int pos) {
208  bs_.set(pos);
209  return *this;
210  }
211 
212  // manipulation: multiple bit set to 1
213  TTBV& set(std::vector<int> vpos) {
214  for (int pos : vpos)
215  bs_.set(pos);
216  return *this;
217  }
218 
219  // manipulation: single bit flip 1 to 0 and vice versa
220  TTBV& flip(int pos) {
221  bs_.flip(pos);
222  return *this;
223  }
224 
225  // manipulation: absolute value of biased twos' complement. Converts twos' complenet into binary.
226  TTBV& abs() {
227  if (twos_) {
228  twos_ = false;
229  if (this->msb())
230  this->flip();
231  size_--;
232  }
233  return *this;
234  }
235 
236  // manipulation: resize
237  TTBV& resize(int size) {
238  bool msb = this->msb();
239  if (size > size_) {
240  if (twos_)
241  for (int n = size_; n < size; n++)
242  bs_.set(n, msb);
243  size_ = size;
244  } else if (size < size_ && size > 0) {
245  this->operator<<=(size - size_);
246  if (twos_)
247  this->msb() = msb;
248  }
249  return *this;
250  }
251 
252  // conversion: to string
253  std::string str() const { return bs_.to_string().substr(S_ - size_, S_); }
254 
255  // conversion: range based to string
256  std::string str(int start, int end = 0) const { return this->str().substr(size_ - start, size_ - end); }
257 
258  // conversion: to int
259  int val() const { return (twos_ && this->msb()) ? (int)bs_.to_ullong() - iMax() : bs_.to_ullong(); }
260 
261  // conversion: to int, reinterpret sign
262  int val(bool twos) const { return (twos && this->msb()) ? (int)bs_.to_ullong() - iMax() : bs_.to_ullong(); }
263 
264  // conversion: range based to int, reinterpret sign
265  int val(int start, int end = 0, bool twos = false) const { return TTBV(*this, start, end).val(twos); }
266 
267  // conversion: to double for given precision assuming biased (floor) representation
268  double val(double base) const { return (this->val() + .5) * base; }
269 
270  // conversion: range based to double for given precision assuming biased (floor) representation, reinterpret sign
271  double val(double base, int start, int end = 0, bool twos = false) const {
272  return (this->val(start, end, twos) + .5) * base;
273  }
274 
275  // maniplulation and conversion: extracts range based to double reinterpret sign and removes these bits
276  double extract(double base, int size, bool twos = false) {
277  double val = this->val(base, size, 0, twos);
278  this->operator>>=(size);
279  return val;
280  }
281 
282  // maniplulation and conversion: extracts range based to int reinterpret sign and removes these bits
283  int extract(int size, bool twos = false) {
284  double val = this->val(size, 0, twos);
285  this->operator>>=(size);
286  return val;
287  }
288 
289  // manipulation: extracts slice and removes these bits
290  TTBV slice(int size, bool twos = false) {
291  TTBV ttBV(*this, size, 0, twos);
292  this->operator>>=(size);
293  return ttBV;
294  }
295 
296  // range based count of '1's or '0's
297  int count(int begin, int end, bool b = true) const {
298  int c(0);
299  for (int i = begin; i < end; i++)
300  if (bs_[i] == b)
301  c++;
302  return c;
303  }
304 
305  // position of least significant '1' or '0'
306  int plEncode(bool b = true) const {
307  for (int e = 0; e < size_; e++)
308  if (bs_[e] == b)
309  return e;
310  return size_;
311  }
312 
313  // position of most significant '1' or '0'
314  int pmEncode(bool b = true) const {
315  for (int e = size_ - 1; e > -1; e--)
316  if (bs_[e] == b)
317  return e;
318  return size_;
319  }
320 
321  // position for n'th '1' or '0' counted from least to most significant bit
322  int encode(int n, bool b = true) const {
323  int sum(0);
324  for (int e = 0; e < size_; e++) {
325  if (bs_[e] == b) {
326  sum++;
327  if (sum == n)
328  return e;
329  }
330  }
331  return size_;
332  }
333 
334  std::vector<int> ids(bool b = true, bool singed = false) const {
335  std::vector<int> v;
336  v.reserve(bs_.count());
337  for (int i = 0; i < size_; i++)
338  if (bs_[i] == b)
339  v.push_back(singed ? i + size_ / 2 : i);
340  return v;
341  }
342 
343  friend std::ostream& operator<<(std::ostream& os, const TTBV& ttBV) { return os << ttBV.str(); }
344 
345 private:
346  // look up table initializer for powers of 2
347  constexpr std::array<unsigned long long int, S_> powersOfTwo() const {
348  std::array<unsigned long long int, S_> lut = {};
349  for (int i = 0; i < S_; i++)
350  lut[i] = std::pow(2, i);
351  return lut;
352  }
353 
354  // returns 2 ** size_
355  unsigned long long int iMax() const {
356  static const std::array<unsigned long long int, S_> lut = powersOfTwo();
357  return lut[size_];
358  }
359 };
360 
361 #endif
Definition: start.py:1
TTBV & operator++()
Definition: TTBV.h:174
TTBV(const double d)
Definition: TTBV.h:34
double extract(double base, int size, bool twos=false)
Definition: TTBV.h:276
double val(double base, int start, int end=0, bool twos=false) const
Definition: TTBV.h:271
unsigned long long int iMax() const
Definition: TTBV.h:355
TTBV(const std::bitset< S_ > &bs, bool twos=false)
Definition: TTBV.h:58
bool none() const
Definition: TTBV.h:87
int plEncode(bool b=true) const
Definition: TTBV.h:306
TTBV(unsigned long long int value, int size)
Definition: TTBV.h:45
TTBV operator+(const TTBV &rhs) const
Definition: TTBV.h:168
Bit vector used by Track Trigger emulators. Mainly used to convert integers into arbitrary (within ma...
Definition: TTBV.h:20
TTBV(const std::string &str, bool twos=false)
Definition: TTBV.h:55
TTBV & flip(int pos)
Definition: TTBV.h:220
int extract(int size, bool twos=false)
Definition: TTBV.h:283
bool msb() const
Definition: TTBV.h:78
const std::bitset< S_ > & bs() const
Definition: TTBV.h:71
std::string str(int start, int end=0) const
Definition: TTBV.h:256
constexpr std::array< unsigned long long int, S_ > powersOfTwo() const
Definition: TTBV.h:347
TTBV & operator+=(const TTBV &rhs)
Definition: TTBV.h:160
int val(int start, int end=0, bool twos=false) const
Definition: TTBV.h:265
TTBV & resize(int size)
Definition: TTBV.h:237
TTBV & flip()
Definition: TTBV.h:194
TTBV & operator &=(const TTBV &rhs)
Definition: TTBV.h:97
TTBV & operator|=(const TTBV &rhs)
Definition: TTBV.h:107
bool operator==(const TTBV &rhs) const
Definition: TTBV.h:91
TTBV & reset()
Definition: TTBV.h:181
bool twos() const
Definition: TTBV.h:67
int val(bool twos) const
Definition: TTBV.h:262
bool twos_
Definition: TTBV.h:25
TTBV & operator>>=(int pos)
Definition: TTBV.h:133
TTBV()
Definition: TTBV.h:31
double val(double base) const
Definition: TTBV.h:268
TTBV(double value, double base, int size, bool twos=false)
Definition: TTBV.h:52
TTBV & abs()
Definition: TTBV.h:226
bool any() const
Definition: TTBV.h:86
TTBV(int value, int size, bool twos=false)
Definition: TTBV.h:48
std::string str() const
Definition: TTBV.h:253
int size() const
Definition: TTBV.h:69
TTBV operator>>(int pos) const
Definition: TTBV.h:154
std::vector< int > ids(bool b=true, bool singed=false) const
Definition: TTBV.h:334
bool all() const
Definition: TTBV.h:85
friend std::ostream & operator<<(std::ostream &os, const TTBV &ttBV)
Definition: TTBV.h:343
TTBV operator<<(int pos) const
Definition: TTBV.h:148
int count() const
Definition: TTBV.h:88
TTBV(const TTBV &ttBV, int begin, int end=0, bool twos=false)
Definition: TTBV.h:61
Definition: value.py:1
int count(int begin, int end, bool b=true) const
Definition: TTBV.h:297
bool operator[](int pos) const
Definition: TTBV.h:74
d
Definition: ztail.py:151
TTBV operator~() const
Definition: TTBV.h:127
std::bitset< S_ >::reference operator[](int pos)
Definition: TTBV.h:75
TTBV & reset(int pos)
Definition: TTBV.h:201
int encode(int n, bool b=true) const
Definition: TTBV.h:322
std::bitset< S_ >::reference msb()
Definition: TTBV.h:81
TTBV & operator^=(const TTBV &rhs)
Definition: TTBV.h:117
bool operator!=(const TTBV &rhs) const
Definition: TTBV.h:94
int val() const
Definition: TTBV.h:259
double b
Definition: hdecay.h:120
std::bitset< S_ > bs_
Definition: TTBV.h:27
int size_
Definition: TTBV.h:26
TTBV slice(int size, bool twos=false)
Definition: TTBV.h:290
int pmEncode(bool b=true) const
Definition: TTBV.h:314
TTBV & operator<<=(int pos)
Definition: TTBV.h:140
static constexpr int S_
Definition: TTBV.h:22
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29