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) {
41  check_input(layer, bit);
42  value_type unit = bit / UINT64_BITS;
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) {
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 bool PhiMemoryImage::test_bit(unsigned int layer, unsigned int bit) const {
55  check_input(layer, bit);
56  value_type unit = bit / UINT64_BITS;
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 }
PhiMemoryImage::set_bit
void set_bit(unsigned int layer, unsigned int bit)
Definition: PhiMemoryImage.cc:40
mps_fire.i
i
Definition: mps_fire.py:428
patt
float patt[4][130000]
Definition: HijingWrapper.h:48
MessageLogger.h
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
PhiMemoryImage::set_word
void set_word(unsigned int layer, unsigned int unit, value_type value)
Definition: PhiMemoryImage.cc:61
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
PhiMemoryImage::_buffer
value_type _buffer[_layers][_units]
Definition: PhiMemoryImage.h:66
PhiMemoryImage::_units
static const unsigned int _units
Definition: PhiMemoryImage.h:63
edm::swap
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
PhiMemoryImage::swap
void swap(PhiMemoryImage &other)
Definition: PhiMemoryImage.cc:28
PhiMemoryImage::print
void print(std::ostream &out) const
Definition: PhiMemoryImage.cc:164
PhiMemoryImage::value_type
uint64_t value_type
Definition: PhiMemoryImage.h:11
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
EcalTangentSkim_cfg.o
o
Definition: EcalTangentSkim_cfg.py:42
PhiMemoryImage::_straightness
int _straightness
Definition: PhiMemoryImage.h:68
PhiMemoryImage::check_input
void check_input(unsigned int layer, unsigned int bit) const
Definition: PhiMemoryImage.cc:71
std::swap
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
Definition: DataFrameContainer.h:209
PhiMemoryImage::test_bit
bool test_bit(unsigned int layer, unsigned int bit) const
Definition: PhiMemoryImage.cc:54
PhiMemoryImage::reset
void reset()
Definition: PhiMemoryImage.cc:34
N
#define N
Definition: blowfish.cc:9
trackingPlots.other
other
Definition: trackingPlots.py:1460
PhiMemoryImage::~PhiMemoryImage
~PhiMemoryImage()
Definition: PhiMemoryImage.cc:12
PhiMemoryImage::clear_bit
void clear_bit(unsigned int layer, unsigned int bit)
Definition: PhiMemoryImage.cc:47
phase1PixelTopology::layer
constexpr std::array< uint8_t, layerIndexSize > layer
Definition: phase1PixelTopology.h:99
PhiMemoryImage::get_word
value_type get_word(unsigned int layer, unsigned int unit) const
Definition: PhiMemoryImage.cc:66
ntuplemaker.fill
fill
Definition: ntuplemaker.py:304
PhiMemoryImage.h
UINT64_BITS
#define UINT64_BITS
Definition: PhiMemoryImage.cc:8
value
Definition: value.py:1
PhiMemoryImage::operator=
PhiMemoryImage & operator=(PhiMemoryImage other)
Definition: PhiMemoryImage.cc:23
unit
Basic3DVector unit() const
Definition: Basic3DVectorLD.h:162
PhiMemoryImage
Definition: PhiMemoryImage.h:9
relativeConstraints.value
value
Definition: relativeConstraints.py:53
PhiMemoryImage::PhiMemoryImage
PhiMemoryImage()
Definition: PhiMemoryImage.cc:10
PhiMemoryImage::rotl
void rotl(unsigned int n)
Definition: PhiMemoryImage.cc:88
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
PhiMemoryImage::op_and
unsigned int op_and(const PhiMemoryImage &other) const
Definition: PhiMemoryImage.cc:144
PhiMemoryImage::rotr
void rotr(unsigned int n)
Definition: PhiMemoryImage.cc:116
PhiMemoryImage::_layers
static const unsigned int _layers
Definition: PhiMemoryImage.h:59
operator<<
std::ostream & operator<<(std::ostream &o, const PhiMemoryImage &patt)
Definition: PhiMemoryImage.cc:177