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 
11 
13 
15  std::copy(&(other._buffer[0][0]), &(other._buffer[0][0]) + (_layers * _units), &(_buffer[0][0]));
16 
17  _straightness = other._straightness;
18 }
19 
21 
22 // Copy-and-swap idiom
24  swap(other);
25  return *this;
26 }
27 
29  std::swap_ranges(&(other._buffer[0][0]), &(other._buffer[0][0]) + (_layers * _units), &(_buffer[0][0]));
30 
31  std::swap(other._straightness, _straightness);
32 }
33 
35  std::fill(&(_buffer[0][0]), &(_buffer[0][0]) + (_layers * _units), 0);
36 
37  _straightness = 0;
38 }
39 
40 void PhiMemoryImage::set_bit(unsigned int layer, unsigned int bit) {
43  value_type mask = (1ul << (bit % UINT64_BITS));
44  _buffer[layer][unit] |= mask;
45 }
46 
47 void PhiMemoryImage::clear_bit(unsigned int layer, unsigned int bit) {
50  value_type mask = (1ul << (bit % UINT64_BITS));
51  _buffer[layer][unit] &= ~mask;
52 }
53 
54 bool PhiMemoryImage::test_bit(unsigned int layer, unsigned int bit) const {
57  value_type mask = (1ul << (bit % UINT64_BITS));
58  return _buffer[layer][unit] & mask;
59 }
60 
61 void PhiMemoryImage::set_word(unsigned int layer, unsigned int unit, value_type value) {
63  _buffer[layer][unit] = value;
64 }
65 
68  return _buffer[layer][unit];
69 }
70 
71 void PhiMemoryImage::check_input(unsigned int layer, unsigned int bit) const {
72  if (layer >= _layers) {
73  char what[128];
74  snprintf(what, sizeof(what), "layer (which is %u) >= _layers (which is %u)", layer, _layers);
75  throw std::out_of_range(what);
76  }
77 
78  unsigned int unit = bit / UINT64_BITS;
79  if (unit >= _units) {
80  char what[128];
81  snprintf(what, sizeof(what), "unit (which is %u) >= _units (which is %u)", unit, _units);
82  throw std::out_of_range(what);
83  }
84 }
85 
86 // See https://en.wikipedia.org/wiki/Circular_shift#Implementing_circular_shifts
87 // return (val << len) | ((unsigned) val >> (-len & (sizeof(INT) * CHAR_BIT - 1)));
88 void PhiMemoryImage::rotl(unsigned int n) {
89  if (n >= _units * UINT64_BITS)
90  return;
91 
93  std::copy(&(_buffer[0][0]), &(_buffer[0][0]) + (_layers * _units), &(tmp[0][0]));
94 
95  const unsigned int mask = UINT64_BITS - 1;
96  const unsigned int n1 = n % UINT64_BITS;
97  const unsigned int n2 = _units - (n / UINT64_BITS);
98  const unsigned int n3 = (n1 == 0) ? n2 + 1 : n2;
99 
100  unsigned int i = 0, j = 0, j_curr = 0, j_next = 0;
101  for (i = 0; i < _layers; ++i) {
102  for (j = 0; j < _units; ++j) {
103  // if n2 == 0:
104  // j_curr = 0, 1, 2
105  // j_next = 2, 0, 1
106  // if n2 == 1:
107  // j_curr = 2, 0, 1
108  // j_next = 1, 2, 0
109  j_curr = (n2 + j) % _units;
110  j_next = (n3 + j + _units - 1) % _units;
111  _buffer[i][j] = (tmp[i][j_curr] << n1) | (tmp[i][j_next] >> (-n1 & mask));
112  }
113  }
114 }
115 
116 void PhiMemoryImage::rotr(unsigned int n) {
117  if (n >= _units * UINT64_BITS)
118  return;
119 
121  std::copy(&(_buffer[0][0]), &(_buffer[0][0]) + (_layers * _units), &(tmp[0][0]));
122 
123  const unsigned int mask = UINT64_BITS - 1;
124  const unsigned int n1 = n % UINT64_BITS;
125  const unsigned int n2 = n / UINT64_BITS;
126  const unsigned int n3 = (n1 == 0) ? n2 + _units - 1 : n2;
127 
128  unsigned int i = 0, j = 0, j_curr = 0, j_next = 0;
129  for (i = 0; i < _layers; ++i) {
130  for (j = 0; j < _units; ++j) {
131  // if n2 == 0:
132  // j_curr = 0, 1, 2
133  // j_next = 1, 2, 0
134  // if n2 == 1:
135  // j_curr = 2, 0, 1
136  // j_next = 0, 1, 2
137  j_curr = (n2 + j) % _units;
138  j_next = (n3 + j + 1) % _units;
139  _buffer[i][j] = (tmp[i][j_curr] >> n1) | (tmp[i][j_next] << (-n1 & mask));
140  }
141  }
142 }
143 
144 unsigned int PhiMemoryImage::op_and(const PhiMemoryImage& other) const {
145  static_assert((_layers == 4 && _units == 3), "This function assumes (_layers == 4 && _units == 3)");
146 
147  // Unroll
148  bool b_st1 = (_buffer[0][0] & other._buffer[0][0]) || (_buffer[0][1] & other._buffer[0][1]) ||
149  (_buffer[0][2] & other._buffer[0][2]);
150  bool b_st2 = (_buffer[1][0] & other._buffer[1][0]) || (_buffer[1][1] & other._buffer[1][1]) ||
151  (_buffer[1][2] & other._buffer[1][2]);
152  bool b_st3 = (_buffer[2][0] & other._buffer[2][0]) || (_buffer[2][1] & other._buffer[2][1]) ||
153  (_buffer[2][2] & other._buffer[2][2]);
154  bool b_st4 = (_buffer[3][0] & other._buffer[3][0]) || (_buffer[3][1] & other._buffer[3][1]) ||
155  (_buffer[3][2] & other._buffer[3][2]);
156 
157  // bit 0: st3 or st4 hit
158  // bit 1: st2 hit
159  // bit 2: st1 hit
160  unsigned int ly = (b_st1 << 2) | (b_st2 << 1) | (b_st3 << 0) | (b_st4 << 0);
161  return ly;
162 }
163 
164 void PhiMemoryImage::print(std::ostream& out) const {
165  constexpr int N = 160;
166  out << std::bitset<N - 128>(_buffer[3][2]) << std::bitset<128 - 64>(_buffer[3][1]) << std::bitset<64>(_buffer[3][0])
167  << std::endl;
168  out << std::bitset<N - 128>(_buffer[2][2]) << std::bitset<128 - 64>(_buffer[2][1]) << std::bitset<64>(_buffer[2][0])
169  << std::endl;
170  out << std::bitset<N - 128>(_buffer[1][2]) << std::bitset<128 - 64>(_buffer[1][1]) << std::bitset<64>(_buffer[1][0])
171  << std::endl;
172  out << std::bitset<N - 128>(_buffer[0][2]) << std::bitset<128 - 64>(_buffer[0][1]) << std::bitset<64>(_buffer[0][0]);
173 }
174 
175 // _____________________________________________________________________________
176 // Output streams
177 std::ostream& operator<<(std::ostream& o, const PhiMemoryImage& patt) {
178  patt.print(o);
179  return o;
180 }
void set_word(unsigned int layer, unsigned int unit, value_type value)
bool test_bit(unsigned int layer, unsigned int bit) const
void set_bit(unsigned int layer, unsigned int bit)
static const unsigned int _units
uint64_t value_type
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
value_type get_word(unsigned int layer, unsigned int unit) const
void clear_bit(unsigned int layer, unsigned int bit)
value_type _buffer[_layers][_units]
void print(std::ostream &out) const
std::ostream & operator<<(std::ostream &o, const PhiMemoryImage &patt)
void rotr(unsigned int n)
void rotl(unsigned int n)
Definition: value.py:1
Basic3DVector unit() const
void check_input(unsigned int layer, unsigned int bit) const
#define N
Definition: blowfish.cc:9
PhiMemoryImage & operator=(PhiMemoryImage other)
unsigned int op_and(const PhiMemoryImage &other) const
#define UINT64_BITS
tmp
align.sh
Definition: createJobs.py:716
void swap(PhiMemoryImage &other)
static const unsigned int _layers
float patt[4][130000]
Definition: HijingWrapper.h:48