FairRoot/PandaRoot
PndCRCCalculator.cxx
Go to the documentation of this file.
1 
3 #include <vector>
4 #include <iostream>
5 #include <cmath>
6 
8 
10  fOrder(16), fPolynom(0x8005), fCRCXor(0x0000), fRefIn(0), fRefOut(0), fCRCInit_direct(0)
11 {
12  fCRCMask = ((((unsigned long)1<<(fOrder-1))-1)<<1)|1;
13  fCRCHighBit = (unsigned long)1<<(fOrder-1);
14 
16 }
17 
18 PndCRCCalculator::PndCRCCalculator(UInt_t order, ULong64_t polynom, ULong64_t CRCXor, UInt_t refIn, UInt_t refOut, UInt_t CRCInit_direct) :
19  fOrder(order), fPolynom(polynom), fCRCXor(CRCXor), fRefIn(refIn), fRefOut(refOut), fCRCInit_direct(CRCInit_direct)
20 {
21  fCRCMask = ((((unsigned long)1<<(fOrder-1))-1)<<1)|1;
22  fCRCHighBit = (unsigned long)1<<(fOrder-1);
23 
25 }
26 
27 
29  // TODO Auto-generated destructor stub
30 }
31 
32 //std::vector<char> PndCRCCalculator::ConvertData(std::vector<ULong64_t> topixFrame)
33 //{
34 // std::vector<char> topix_data;
35 // for (int i = 1; i < topixFrame.size() - 1; i++) {
36 // for (int j = 0; j < 8; j++) {
37 // if (j == 0 or j == 1 or j == 2) {
38 // topix_data.push_back(0x00);
39 // } else {
40 // topix_data.push_back((topixFrame[i] >> (7 - j) * 2 * 4) & 0xff);
41 // }
42 // }
43 // }
44 // return topix_data;
45 //}
46 
47 ULong64_t PndCRCCalculator::CalculateCRCTableFast(std::vector<char> p, ULong64_t len) {
48 
49  // fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
50  // only usable with polynom orders of 8, 16, 24 or 32.
51 
52  ULong64_t crc = fCRCInit_direct;
53 
54  std::vector<char>::iterator it = p.begin();
55 
56  if (fRefIn) {
57  crc = ReflectBitsStream(crc, fOrder);
58  }
59 
60  if (!fRefIn) {
61  while (len--) {
62  crc = (crc << 8) ^ fCRCTab[((crc >> (fOrder - 8)) & 0xff) ^ (*it & 0xff)];
63  it++;
64  }
65  } else {
66  while (len--) {
67  crc = (crc >> 8) ^ fCRCTab[(crc & 0xff) ^ (*it & 0xff)];
68  it++;
69  }
70  }
71  if (fRefOut ^ fRefIn) {
72  crc = ReflectBitsStream(crc, fOrder);
73  }
74  crc ^= fCRCXor;
75  crc &= fCRCMask;
76 
77  return (crc);
78 }
79 
81  // make CRC lookup table used by table algorithms
82  ULong64_t bit, crc;
83  for (int i=0; i<256; i++) {
84  crc=(ULong64_t)i;
85  if (fRefIn)
86  {
87  crc=ReflectBitsStream(crc, 8);
88  }
89  crc<<= fOrder-8;
90 
91  for (int j=0; j<8; j++)
92  {
93  bit = crc & fCRCHighBit;
94  crc<<= 1;
95  if (bit)
96  {
97  crc^= fPolynom;
98  }
99  }
100  if (fRefIn)
101  {
102  crc = ReflectBitsStream(crc, fOrder);
103  }
104  crc&= fCRCMask;
105  fCRCTab[i]= crc;
106  }
107 }
108 
110 {
111  for (int i = 0; i < 16; i++){
112  for (int j = 0; j < 16; j++){
113  std::cout << std::hex << fCRCTab[i*16 + j] << " ";
114  }
115  std::cout << std::endl;
116  }
117 }
118 
119 ULong64_t PndCRCCalculator::ReflectBitsStream(ULong64_t crc, int bitnum) {
120 
121  // reflects the lower 'bitnum' bits of 'crc'
122 
123  ULong64_t i, j=1, crcout=0;
124 
125  for (i=(ULong64_t)1<<(bitnum-1); i; i>>=1)
126  {
127  if (crc & i)
128  {
129  crcout|=j;
130  }
131  j<<= 1;
132  }
133  return (crcout);
134 }
Double_t p
Definition: anasim.C:58
Int_t i
Definition: run_full.C:25
const UInt_t fOrder
const ULong64_t fPolynom
const UInt_t fRefIn
ULong64_t ReflectBitsStream(ULong64_t crc, int bitnum)
ULong64_t fCRCTab[256]
const ULong64_t fCRCXor
ULong64_t CalculateCRCTableFast(std::vector< char > p, ULong64_t len)
const UInt_t fRefOut
ClassImp(PndAnaContFact)
ULong64_t fCRCInit_direct
PndCRCCalculator calculates the CRC checksum from a given vector&lt;char&gt;
virtual ~PndCRCCalculator()