CMS 3D CMS Logo

PhiMemoryImage.cc
Go to the documentation of this file.
3 
4 #include <stdexcept>
5 #include <iostream>
6 #include <bitset>
7 
8 #define UINT64_BITS 64
9 
10 
12  reset();
13 }
14 
16 
17 }
18 
20  std::copy(&(other._buffer[0][0]), &(other._buffer[0][0]) + (_layers*_units), &(_buffer[0][0]));
21 
23 }
24 
26  swap(other);
27 }
28 
29 // Copy-and-swap idiom
31  swap(other);
32  return *this;
33 }
34 
36  std::swap_ranges(&(other._buffer[0][0]), &(other._buffer[0][0]) + (_layers*_units), &(_buffer[0][0]));
37 
39 }
40 
42  std::fill(&(_buffer[0][0]), &(_buffer[0][0]) + (_layers*_units), 0);
43 
44  _straightness = 0;
45 }
46 
47 void PhiMemoryImage::set_bit(unsigned int layer, unsigned int bit) {
48  check_input(layer, bit);
49  value_type unit = bit / UINT64_BITS;
50  value_type mask = (1ul << (bit % UINT64_BITS));
51  _buffer[layer][unit] |= mask;
52 }
53 
54 void PhiMemoryImage::clear_bit(unsigned int layer, unsigned int bit) {
55  check_input(layer, bit);
56  value_type unit = bit / UINT64_BITS;
57  value_type mask = (1ul << (bit % UINT64_BITS));
58  _buffer[layer][unit] &= ~mask;
59 }
60 
61 bool PhiMemoryImage::test_bit(unsigned int layer, unsigned int bit) const {
62  check_input(layer, bit);
63  value_type unit = bit / UINT64_BITS;
64  value_type mask = (1ul << (bit % UINT64_BITS));
65  return _buffer[layer][unit] & mask;
66 }
67 
68 void PhiMemoryImage::set_word(unsigned int layer, unsigned int unit, value_type value) {
69  check_input(layer, unit*UINT64_BITS);
70  _buffer[layer][unit] = value;
71 }
72 
73 PhiMemoryImage::value_type PhiMemoryImage::get_word(unsigned int layer, unsigned int unit) const {
74  check_input(layer, unit*UINT64_BITS);
75  return _buffer[layer][unit];
76 }
77 
78 void PhiMemoryImage::check_input(unsigned int layer, unsigned int bit) const {
79  if (layer >= _layers) {
80  char what[128];
81  snprintf(what, sizeof(what), "layer (which is %u) >= _layers (which is %u)", layer, _layers);
82  throw std::out_of_range(what);
83  }
84 
85  unsigned int unit = bit / UINT64_BITS;
86  if (unit >= _units) {
87  char what[128];
88  snprintf(what, sizeof(what), "unit (which is %u) >= _units (which is %u)", unit, _units);
89  throw std::out_of_range(what);
90  }
91 }
92 
93 // See https://en.wikipedia.org/wiki/Circular_shift#Implementing_circular_shifts
94 // return (val << len) | ((unsigned) val >> (-len & (sizeof(INT) * CHAR_BIT - 1)));
95 void PhiMemoryImage::rotl(unsigned int n) {
96  if (n >= _units*UINT64_BITS)
97  return;
98 
100  std::copy(&(_buffer[0][0]), &(_buffer[0][0]) + (_layers*_units), &(tmp[0][0]));
101 
102  const unsigned int mask = UINT64_BITS - 1;
103  const unsigned int n1 = n % UINT64_BITS;
104  const unsigned int n2 = _units - (n / UINT64_BITS);
105  const unsigned int n3 = (n1 == 0) ? n2+1 : n2;
106 
107  unsigned int i = 0, j = 0, j_curr = 0, j_next = 0;
108  for (i = 0; i < _layers; ++i) {
109  for (j = 0; j < _units; ++j) {
110  // if n2 == 0:
111  // j_curr = 0, 1, 2
112  // j_next = 2, 0, 1
113  // if n2 == 1:
114  // j_curr = 2, 0, 1
115  // j_next = 1, 2, 0
116  j_curr = (n2+j) % _units;
117  j_next = (n3+j+_units-1) % _units;
118  _buffer[i][j] = (tmp[i][j_curr] << n1) | (tmp[i][j_next] >> (-n1 & mask));
119  }
120  }
121 }
122 
123 void PhiMemoryImage::rotr(unsigned int n) {
124  if (n >= _units*UINT64_BITS)
125  return;
126 
128  std::copy(&(_buffer[0][0]), &(_buffer[0][0]) + (_layers*_units), &(tmp[0][0]));
129 
130  const unsigned int mask = UINT64_BITS - 1;
131  const unsigned int n1 = n % UINT64_BITS;
132  const unsigned int n2 = n / UINT64_BITS;
133  const unsigned int n3 = (n1 == 0) ? n2+_units-1 : n2;
134 
135  unsigned int i = 0, j = 0, j_curr = 0, j_next = 0;
136  for (i = 0; i < _layers; ++i) {
137  for (j = 0; j < _units; ++j) {
138  // if n2 == 0:
139  // j_curr = 0, 1, 2
140  // j_next = 1, 2, 0
141  // if n2 == 1:
142  // j_curr = 2, 0, 1
143  // j_next = 0, 1, 2
144  j_curr = (n2+j)% _units;
145  j_next = (n3+j+1) % _units;
146  _buffer[i][j] = (tmp[i][j_curr] >> n1) | (tmp[i][j_next] << (-n1 & mask));
147  }
148  }
149 }
150 
151 unsigned int PhiMemoryImage::op_and(const PhiMemoryImage& other) const {
152  if (not(_layers == 4 && _units == 3))
153  { edm::LogError("L1T") << "_layers = " << _layers << ", _units = " << _units; return 0; }
154 
155  // Unroll
156  bool b_st1 = (_buffer[0][0] & other._buffer[0][0]) ||
157  (_buffer[0][1] & other._buffer[0][1]) ||
158  (_buffer[0][2] & other._buffer[0][2]);
159  bool b_st2 = (_buffer[1][0] & other._buffer[1][0]) ||
160  (_buffer[1][1] & other._buffer[1][1]) ||
161  (_buffer[1][2] & other._buffer[1][2]);
162  bool b_st3 = (_buffer[2][0] & other._buffer[2][0]) ||
163  (_buffer[2][1] & other._buffer[2][1]) ||
164  (_buffer[2][2] & other._buffer[2][2]);
165  bool b_st4 = (_buffer[3][0] & other._buffer[3][0]) ||
166  (_buffer[3][1] & other._buffer[3][1]) ||
167  (_buffer[3][2] & other._buffer[3][2]);
168 
169  // bit 0: st3 or st4 hit
170  // bit 1: st2 hit
171  // bit 2: st1 hit
172  unsigned int ly = (b_st1 << 2) | (b_st2 << 1) | (b_st3 << 0) | (b_st4 << 0);
173  return ly;
174 }
175 
176 void PhiMemoryImage::print(std::ostream& out) const {
177  constexpr int N = 160;
178  out << std::bitset<N-128>(_buffer[3][2]) << std::bitset<128-64>(_buffer[3][1]) << std::bitset<64>(_buffer[3][0]) << std::endl;
179  out << std::bitset<N-128>(_buffer[2][2]) << std::bitset<128-64>(_buffer[2][1]) << std::bitset<64>(_buffer[2][0]) << std::endl;
180  out << std::bitset<N-128>(_buffer[1][2]) << std::bitset<128-64>(_buffer[1][1]) << std::bitset<64>(_buffer[1][0]) << std::endl;
181  out << std::bitset<N-128>(_buffer[0][2]) << std::bitset<128-64>(_buffer[0][1]) << std::bitset<64>(_buffer[0][0]);
182 }
183 
184 // _____________________________________________________________________________
185 // Output streams
186 std::ostream& operator<<(std::ostream& o, const PhiMemoryImage& patt) {
187  patt.print(o);
188  return o;
189 }
unsigned int op_and(const PhiMemoryImage &other) const
void set_word(unsigned int layer, unsigned int unit, value_type value)
def copy(args, dbName)
void set_bit(unsigned int layer, unsigned int bit)
static const unsigned int _units
uint64_t value_type
#define noexcept
#define constexpr
value_type get_word(unsigned int layer, unsigned int unit) const
void check_input(unsigned int layer, unsigned int bit) const
void clear_bit(unsigned int layer, unsigned int bit)
void print(std::ostream &out) const
value_type _buffer[_layers][_units]
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
std::ostream & operator<<(std::ostream &o, const PhiMemoryImage &patt)
void rotr(unsigned int n)
void rotl(unsigned int n)
Definition: value.py:1
#define N
Definition: blowfish.cc:9
PhiMemoryImage & operator=(PhiMemoryImage other)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
#define UINT64_BITS
bool test_bit(unsigned int layer, unsigned int bit) const
void swap(PhiMemoryImage &other)
static const unsigned int _layers
float patt[4][130000]
Definition: HijingWrapper.h:38