otsdaq  v2_05_02_indev
BitManipulator.cc
1 #include "otsdaq/BitManipulator/BitManipulator.h"
2 #include <cstdlib>
3 #include <iostream>
4 #include <sstream>
5 #include "otsdaq/Macros/CoutMacros.h"
6 #include "otsdaq/MessageFacility/MessageFacility.h"
7 
8 using namespace ots;
9 
10 //==============================================================================
11 BitManipulator::BitManipulator() {}
12 
13 //==============================================================================
14 BitManipulator::~BitManipulator() {}
15 
16 //==============================================================================
17 uint64_t BitManipulator::insertBits(uint64_t& data, uint64_t value, unsigned int startBit, unsigned int numberOfBits)
18 {
19  // std::cout << __COUT_HDR_FL__ << "Before: " << std::hex << data << "-<-" << value
20  // << std::dec << std::endl;
21  for(unsigned int i = 0; i < numberOfBits; i++)
22  data &= ~((uint64_t)1 << (startBit + i));
23 
24  data |= (value << startBit);
25  // std::cout << __COUT_HDR_FL__ << "After: " << std::hex << data << "-<-" << value
26  // << std::dec << std::endl;
27  return data;
28 }
29 
30 //==============================================================================
31 uint64_t BitManipulator::insertBits(std::string& data, uint64_t value, unsigned int startBit, unsigned int numberOfBits)
32 {
33  uint8_t toWrite = 0;
34  const unsigned int bitsInAByte = 8;
35 
36  uint8_t overWritten = 0;
37  int startByte = startBit / bitsInAByte;
38  int finalByte = (startBit + numberOfBits - 1) / bitsInAByte;
39  int startBitInByte = startBit % bitsInAByte;
40  int finalBitInByte = (startBit + numberOfBits - 1) % bitsInAByte;
41  int tmp;
42  //int firstByteLength = bitsInAByte;
43  int lastByteLength = (startBit + numberOfBits) % bitsInAByte;
44 
45  //
46  // std::cout << __COUT_HDR_FL__ << " start from byte : " << startByte << ", finish in
47  // " << finalByte << "\n" << std::endl;
48 
49  for(int j = 0; j <= finalByte - startByte; ++j)
50  {
51  if(j != 0)
52  startBitInByte = 0;
53  if(j != finalByte - startByte)
54  finalBitInByte = 7;
55  else
56  finalBitInByte = (startBit + numberOfBits - 1) % 8;
57 
58  tmp = finalBitInByte;
59  finalBitInByte = 7 - startBitInByte;
60  startBitInByte = 7 - tmp;
61 
62  // std::cout << __COUT_HDR_FL__ << "in byte : " << startByte + j << ", start bit:
63  // " << startBitInByte << ", finish in bit " << finalBitInByte << "\n" <<
64  // std::endl;
65 
66  overWritten = data.substr(startByte + j, 1).data()[0];
67  // std::cout << __COUT_HDR_FL__ << "value overwritten: " << hex <<
68  // (uint64_t)overWritten << "\n" << std::endl;
69  // toWrite = (uint8_t)value; //FIXME you can declare value as uint8_t from
70  // the beginning 30-0600000000000000
71  toWrite = (uint8_t)0;
72  for(int y = 0; y <= finalBitInByte - startBitInByte; ++y)
73  {
74  if(finalByte - startByte > 1)
75  {
76  if(j != finalByte - startByte)
77  toWrite |= ((value >> (lastByteLength + (finalByte - startByte - 1 - j) * 8 + y)) & 1) << y;
78  else
79  toWrite |= ((value >> (lastByteLength + y)) & 1) << y;
80  }
81  else if(finalByte - startByte == 1)
82  toWrite |= ((value >> (lastByteLength * (1 - j) + y)) & 1) << y;
83  else if(finalByte - startByte == 0)
84  toWrite |= ((value >> y) & 1) << y;
85  // toWrite |= ((value >> (firstByteLength + (j-1)*8 + y))&1) << (j*8
86  // + y);
87  }
88 
89  // std::cout << __COUT_HDR_FL__ << "value to manipulate: " << hex <<
90  // (uint64_t)toWrite << " obtained from " << tmpVal << "\n" << std::endl;
91 
92  for(int n = 0; n < finalBitInByte - startBitInByte + 1; n++)
93  {
94  overWritten &= ~((uint64_t)1 << (startBitInByte + n));
95  }
96  // std::cout << __COUT_HDR_FL__ << "value in intermediate step: " << hex <<
97  // (uint64_t)overWritten << "\n" << std::endl;
98 
99  overWritten |= (toWrite << startBitInByte);
100 
101  // std::cout << __COUT_HDR_FL__ << "value to overwrite: " << hex <<
102  // (uint64_t)overWritten << "\n" << std::endl;
103  data[startByte + j] = overWritten;
104 
105  // if(j == 0)
106  // firstByteLength = finalBitInByte - startBitInByte + 1;
107  }
108 
109  return (uint64_t)overWritten;
110 }
111 
112 //==============================================================================
113 uint64_t BitManipulator::reverseBits(uint64_t data, unsigned int startBit, unsigned int numberOfBits)
114 {
115  uint64_t reversedData = 0;
116  for(unsigned int r = startBit; r < numberOfBits; r++)
117  reversedData |= ((data >> r) & 1) << (numberOfBits - 1 - r);
118  return reversedData;
119 }
120 
121 //==============================================================================
122 uint32_t BitManipulator::insertBits(uint32_t& data, uint32_t value, unsigned int startBit, unsigned int numberOfBits)
123 {
124  // std::cout << __COUT_HDR_FL__ << "Before: " << hex << data << "-<-" << value <<
125  // std::endl;
126  value = value << startBit;
127  for(unsigned int i = 0; i < 32; i++)
128  {
129  if(i >= startBit && i < startBit + numberOfBits)
130  data &= ~((uint32_t)1 << i);
131  else
132  value &= ~((uint32_t)1 << i);
133  }
134  data += value;
135  // std::cout << __COUT_HDR_FL__ << "After: " << hex << data << "-<-" << value <<
136  // std::endl;
137  return data;
138 }
139 
140 //==============================================================================
141 uint32_t BitManipulator::insertBits(std::string& data, uint32_t value, unsigned int startBit, unsigned int numberOfBits)
142 {
143  uint8_t toWrite = 0;
144  const unsigned int bitsInAByte = 8;
145 
146  uint8_t overWritten = 0;
147  int startByte = startBit / bitsInAByte;
148  int finalByte = (startBit + numberOfBits - 1) / bitsInAByte;
149  int startBitInByte = startBit % 8;
150  int finalBitInByte = (startBit + numberOfBits - 1) % bitsInAByte;
151  int tmp;
152  //int firstByteLength = bitsInAByte;
153  int lastByteLength = (startBit + numberOfBits) % bitsInAByte;
154 
155  //
156  // std::cout << __COUT_HDR_FL__ << " start from byte : " << startByte << ", finish in
157  // " << finalByte << "\n" << std::endl;
158 
159  for(int j = 0; j <= finalByte - startByte; ++j)
160  {
161  if(j != 0)
162  startBitInByte = 0;
163  if(j != finalByte - startByte)
164  finalBitInByte = 7;
165  else
166  finalBitInByte = (startBit + numberOfBits - 1) % 8;
167 
168  tmp = finalBitInByte;
169  finalBitInByte = 7 - startBitInByte;
170  startBitInByte = 7 - tmp;
171 
172  // std::cout << __COUT_HDR_FL__ << "in byte : " << startByte + j << ", start bit:
173  // " << startBitInByte << ", finish in bit " << finalBitInByte << "\n" <<
174  // std::endl;
175 
176  overWritten = data.substr(startByte + j, 1).data()[0];
177  // std::cout << __COUT_HDR_FL__ << "value overwritten: " << hex <<
178  // (uint64_t)overWritten << "\n" << std::endl;
179  // toWrite = (uint8_t)value; //FIXME you can declare value as uint8_t from
180  // the beginning 30-0600000000000000
181  toWrite = (uint8_t)0;
182  for(int y = 0; y <= finalBitInByte - startBitInByte; ++y)
183  {
184  if(finalByte - startByte > 1)
185  {
186  if(j != finalByte - startByte)
187  toWrite |= ((value >> (lastByteLength + (finalByte - startByte - 1 - j) * 8 + y)) & 1) << y;
188  else
189  toWrite |= ((value >> (lastByteLength + y)) & 1) << y;
190  }
191  else if(finalByte - startByte == 1)
192  toWrite |= ((value >> (lastByteLength * (1 - j) + y)) & 1) << y;
193  else if(finalByte - startByte == 0)
194  toWrite |= ((value >> y) & 1) << y;
195  // toWrite |= ((value >> (firstByteLength + (j-1)*8 + y))&1) << (j*8
196  // + y);
197  }
198 
199  // std::cout << __COUT_HDR_FL__ << "value to manipulate: " << hex <<
200  // (uint64_t)toWrite << " obtained from " << tmpVal << "\n" << std::endl;
201 
202  for(int n = 0; n < finalBitInByte - startBitInByte + 1; n++)
203  {
204  overWritten &= ~((uint32_t)1 << (startBitInByte + n));
205  }
206  // std::cout << __COUT_HDR_FL__ << "value in intermediate step: " << hex <<
207  // (uint64_t)overWritten << "\n" << std::endl;
208 
209  overWritten |= (toWrite << startBitInByte);
210 
211  // std::cout << __COUT_HDR_FL__ << "value to overwrite: " << hex <<
212  // (uint64_t)overWritten << "\n" << std::endl;
213  data[startByte + j] = overWritten;
214 
215  //if(j == 0)
216  // firstByteLength = finalBitInByte - startBitInByte + 1;
217  }
218 
219  return (uint32_t)overWritten;
220 }
221 
222 //==============================================================================
223 uint32_t BitManipulator::reverseBits(uint32_t data, unsigned int startBit, unsigned int numberOfBits)
224 {
225  uint32_t reversedData = 0;
226  for(unsigned int r = startBit; r < startBit + numberOfBits; r++)
227  reversedData |= ((data >> r) & 1) << (numberOfBits - 1 - r);
228  return reversedData;
229 }
230 
231 //==============================================================================
232 uint32_t BitManipulator::readBits(uint32_t data, unsigned int startBit, unsigned int numberOfBits)
233 {
234  uint32_t returnData = 0;
235  for(unsigned int r = startBit; r < startBit + numberOfBits; r++)
236  returnData += ((data >> r) & 0x1) << (r - startBit);
237  return returnData;
238 }