$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "otsdaq-core/BitManipulator/BitManipulator.h" 00002 #include <cstdlib> 00003 #include <iostream> 00004 #include <sstream> 00005 #include "otsdaq-core/Macros/CoutMacros.h" 00006 #include "otsdaq-core/MessageFacility/MessageFacility.h" 00007 00008 using namespace ots; 00009 00010 //======================================================================================================================== 00011 BitManipulator::BitManipulator() {} 00012 00013 //======================================================================================================================== 00014 BitManipulator::~BitManipulator() {} 00015 00016 //======================================================================================================================== 00017 uint64_t BitManipulator::insertBits(uint64_t& data, 00018 uint64_t value, 00019 unsigned int startBit, 00020 unsigned int numberOfBits) 00021 { 00022 // std::cout << __COUT_HDR_FL__ << "Before: " << std::hex << data << "-<-" << value 00023 // << std::dec << std::endl; 00024 for(unsigned int i = 0; i < numberOfBits; i++) 00025 data &= ~((uint64_t)1 << (startBit + i)); 00026 00027 data |= (value << startBit); 00028 // std::cout << __COUT_HDR_FL__ << "After: " << std::hex << data << "-<-" << value 00029 // << std::dec << std::endl; 00030 return data; 00031 } 00032 00033 //======================================================================================================================== 00034 uint64_t BitManipulator::insertBits(std::string& data, 00035 uint64_t value, 00036 unsigned int startBit, 00037 unsigned int numberOfBits) 00038 { 00039 uint8_t toWrite = 0; 00040 const unsigned int bitsInAByte = 8; 00041 00042 uint8_t overWritten = 0; 00043 int startByte = startBit / bitsInAByte; 00044 int finalByte = (startBit + numberOfBits - 1) / bitsInAByte; 00045 int startBitInByte = startBit % bitsInAByte; 00046 int finalBitInByte = (startBit + numberOfBits - 1) % bitsInAByte; 00047 int tmp; 00048 int firstByteLength = bitsInAByte; 00049 int lastByteLength = (startBit + numberOfBits) % bitsInAByte; 00050 00051 // 00052 // std::cout << __COUT_HDR_FL__ << " start from byte : " << startByte << ", finish in 00053 // " << finalByte << "\n" << std::endl; 00054 00055 for(int j = 0; j <= finalByte - startByte; ++j) 00056 { 00057 if(j != 0) 00058 startBitInByte = 0; 00059 if(j != finalByte - startByte) 00060 finalBitInByte = 7; 00061 else 00062 finalBitInByte = (startBit + numberOfBits - 1) % 8; 00063 00064 tmp = finalBitInByte; 00065 finalBitInByte = 7 - startBitInByte; 00066 startBitInByte = 7 - tmp; 00067 00068 // std::cout << __COUT_HDR_FL__ << "in byte : " << startByte + j << ", start bit: 00069 // " << startBitInByte << ", finish in bit " << finalBitInByte << "\n" << 00070 // std::endl; 00071 00072 overWritten = data.substr(startByte + j, 1).data()[0]; 00073 // std::cout << __COUT_HDR_FL__ << "value overwritten: " << hex << 00074 // (uint64_t)overWritten << "\n" << std::endl; 00075 // toWrite = (uint8_t)value; //FIXME you can declare value as uint8_t from 00076 // the beginning 30-0600000000000000 00077 toWrite = (uint8_t)0; 00078 for(int y = 0; y <= finalBitInByte - startBitInByte; ++y) 00079 { 00080 if(finalByte - startByte > 1) 00081 { 00082 if(j != finalByte - startByte) 00083 toWrite |= ((value >> (lastByteLength + 00084 (finalByte - startByte - 1 - j) * 8 + y)) & 00085 1) 00086 << y; 00087 else 00088 toWrite |= ((value >> (lastByteLength + y)) & 1) << y; 00089 } 00090 else if(finalByte - startByte == 1) 00091 toWrite |= ((value >> (lastByteLength * (1 - j) + y)) & 1) << y; 00092 else if(finalByte - startByte == 0) 00093 toWrite |= ((value >> y) & 1) << y; 00094 // toWrite |= ((value >> (firstByteLength + (j-1)*8 + y))&1) << (j*8 00095 // + y); 00096 } 00097 00098 // std::cout << __COUT_HDR_FL__ << "value to manipulate: " << hex << 00099 // (uint64_t)toWrite << " obtained from " << tmpVal << "\n" << std::endl; 00100 00101 for(int n = 0; n < finalBitInByte - startBitInByte + 1; n++) 00102 { 00103 overWritten &= ~((uint64_t)1 << (startBitInByte + n)); 00104 } 00105 // std::cout << __COUT_HDR_FL__ << "value in intermediate step: " << hex << 00106 // (uint64_t)overWritten << "\n" << std::endl; 00107 00108 overWritten |= (toWrite << startBitInByte); 00109 00110 // std::cout << __COUT_HDR_FL__ << "value to overwrite: " << hex << 00111 // (uint64_t)overWritten << "\n" << std::endl; 00112 data[startByte + j] = overWritten; 00113 00114 if(j == 0) 00115 firstByteLength = finalBitInByte - startBitInByte + 1; 00116 } 00117 00118 return (uint64_t)overWritten; 00119 } 00120 00121 //======================================================================================================================== 00122 uint64_t BitManipulator::reverseBits(uint64_t data, 00123 unsigned int startBit, 00124 unsigned int numberOfBits) 00125 { 00126 uint64_t reversedData = 0; 00127 for(unsigned int r = startBit; r < numberOfBits; r++) 00128 reversedData |= ((data >> r) & 1) << (numberOfBits - 1 - r); 00129 return reversedData; 00130 } 00131 00132 //======================================================================================================================== 00133 uint32_t BitManipulator::insertBits(uint32_t& data, 00134 uint32_t value, 00135 unsigned int startBit, 00136 unsigned int numberOfBits) 00137 { 00138 // std::cout << __COUT_HDR_FL__ << "Before: " << hex << data << "-<-" << value << 00139 // std::endl; 00140 value = value << startBit; 00141 for(unsigned int i = 0; i < 32; i++) 00142 { 00143 if(i >= startBit && i < startBit + numberOfBits) 00144 data &= ~((uint32_t)1 << i); 00145 else 00146 value &= ~((uint32_t)1 << i); 00147 } 00148 data += value; 00149 // std::cout << __COUT_HDR_FL__ << "After: " << hex << data << "-<-" << value << 00150 // std::endl; 00151 return data; 00152 } 00153 00154 //======================================================================================================================== 00155 uint32_t BitManipulator::insertBits(std::string& data, 00156 uint32_t value, 00157 unsigned int startBit, 00158 unsigned int numberOfBits) 00159 { 00160 uint8_t toWrite = 0; 00161 const unsigned int bitsInAByte = 8; 00162 00163 uint8_t overWritten = 0; 00164 int startByte = startBit / bitsInAByte; 00165 int finalByte = (startBit + numberOfBits - 1) / bitsInAByte; 00166 int startBitInByte = startBit % 8; 00167 int finalBitInByte = (startBit + numberOfBits - 1) % bitsInAByte; 00168 int tmp; 00169 int firstByteLength = bitsInAByte; 00170 int lastByteLength = (startBit + numberOfBits) % bitsInAByte; 00171 00172 // 00173 // std::cout << __COUT_HDR_FL__ << " start from byte : " << startByte << ", finish in 00174 // " << finalByte << "\n" << std::endl; 00175 00176 for(int j = 0; j <= finalByte - startByte; ++j) 00177 { 00178 if(j != 0) 00179 startBitInByte = 0; 00180 if(j != finalByte - startByte) 00181 finalBitInByte = 7; 00182 else 00183 finalBitInByte = (startBit + numberOfBits - 1) % 8; 00184 00185 tmp = finalBitInByte; 00186 finalBitInByte = 7 - startBitInByte; 00187 startBitInByte = 7 - tmp; 00188 00189 // std::cout << __COUT_HDR_FL__ << "in byte : " << startByte + j << ", start bit: 00190 // " << startBitInByte << ", finish in bit " << finalBitInByte << "\n" << 00191 // std::endl; 00192 00193 overWritten = data.substr(startByte + j, 1).data()[0]; 00194 // std::cout << __COUT_HDR_FL__ << "value overwritten: " << hex << 00195 // (uint64_t)overWritten << "\n" << std::endl; 00196 // toWrite = (uint8_t)value; //FIXME you can declare value as uint8_t from 00197 // the beginning 30-0600000000000000 00198 toWrite = (uint8_t)0; 00199 for(int y = 0; y <= finalBitInByte - startBitInByte; ++y) 00200 { 00201 if(finalByte - startByte > 1) 00202 { 00203 if(j != finalByte - startByte) 00204 toWrite |= ((value >> (lastByteLength + 00205 (finalByte - startByte - 1 - j) * 8 + y)) & 00206 1) 00207 << y; 00208 else 00209 toWrite |= ((value >> (lastByteLength + y)) & 1) << y; 00210 } 00211 else if(finalByte - startByte == 1) 00212 toWrite |= ((value >> (lastByteLength * (1 - j) + y)) & 1) << y; 00213 else if(finalByte - startByte == 0) 00214 toWrite |= ((value >> y) & 1) << y; 00215 // toWrite |= ((value >> (firstByteLength + (j-1)*8 + y))&1) << (j*8 00216 // + y); 00217 } 00218 00219 // std::cout << __COUT_HDR_FL__ << "value to manipulate: " << hex << 00220 // (uint64_t)toWrite << " obtained from " << tmpVal << "\n" << std::endl; 00221 00222 for(int n = 0; n < finalBitInByte - startBitInByte + 1; n++) 00223 { 00224 overWritten &= ~((uint32_t)1 << (startBitInByte + n)); 00225 } 00226 // std::cout << __COUT_HDR_FL__ << "value in intermediate step: " << hex << 00227 // (uint64_t)overWritten << "\n" << std::endl; 00228 00229 overWritten |= (toWrite << startBitInByte); 00230 00231 // std::cout << __COUT_HDR_FL__ << "value to overwrite: " << hex << 00232 // (uint64_t)overWritten << "\n" << std::endl; 00233 data[startByte + j] = overWritten; 00234 00235 if(j == 0) 00236 firstByteLength = finalBitInByte - startBitInByte + 1; 00237 } 00238 00239 return (uint32_t)overWritten; 00240 } 00241 00242 //======================================================================================================================== 00243 uint32_t BitManipulator::reverseBits(uint32_t data, 00244 unsigned int startBit, 00245 unsigned int numberOfBits) 00246 { 00247 uint32_t reversedData = 0; 00248 for(unsigned int r = startBit; r < startBit + numberOfBits; r++) 00249 reversedData |= ((data >> r) & 1) << (numberOfBits - 1 - r); 00250 return reversedData; 00251 } 00252 00253 //======================================================================================================================== 00254 uint32_t BitManipulator::readBits(uint32_t data, 00255 unsigned int startBit, 00256 unsigned int numberOfBits) 00257 { 00258 uint32_t returnData = 0; 00259 for(unsigned int r = startBit; r < startBit + numberOfBits; r++) 00260 returnData += ((data >> r) & 0x1) << (r - startBit); 00261 return returnData; 00262 }