FairRoot/PandaRoot
PndHammingDecoder.cxx
Go to the documentation of this file.
1 /*
2  * PndHammingEncoder.cxx
3  *
4  * Created on: 16.09.2015
5  * Author: Stockmanns
6  */
7 
9 #include <vector>
10 #include <iostream>
11 #include <cmath>
12 
14  fOrder(16), fPolynom(0x8005), fCRCXor(0x0000), fRefIn(0), fRefOut(0), fCRCInit_direct(0)
15 {
16  fCRCMask = ((((unsigned long)1<<(fOrder-1))-1)<<1)|1;
17  fCRCHighBit = (unsigned long)1<<(fOrder-1);
18 
20 }
21 
23  // TODO Auto-generated destructor stub
24 }
25 
26 UShort_t PndHammingDecoder::CheckHammingCode(ULong64_t dataword, int dataword_length)
27 {
28  // This function expects a standard hamming encoded dataword (including hamming bits at 2^i positions) and returns the hamming bits
29  // If the dataword is correct, the hamming bits are 0
30  // If an error occured, the hamming bits are !=0
31  // If a 1-bit error occured, the hamming bits give the possions of the wrong bit
32 
33  ULong64_t shift=1;
34  ULong64_t hamming_code=0;
35 
36  if(dataword_length > 63)
37  {
38  std::cout << "Error: maximum length of datastream is 63" << std::endl;
39  return 0xffff;
40  }
41 
42  // loop over all hamming bits
43  for (int i = 0; pow(2,i) < dataword_length; ++i)
44  {
45  int start=pow(2,i);
46  int stepwidth=2*start;
47  int parity_bit=0;
48  for (int j = start; j < dataword_length+1; j+=stepwidth)
49  {
50  for (int k = j; k < j+stepwidth/2 && k < dataword_length+1; ++k)
51  {
52  parity_bit ^= ((dataword & (shift <<(k-1)))>> (k-1));
53  }
54  }
55  hamming_code += (parity_bit << i);
56  }
57  return hamming_code;
58 }
59 
60 std::vector<char> PndHammingDecoder::ConvertData(std::vector<ULong64_t> topixFrame)
61 {
62  std::vector<char> topix_data;
63  for (unsigned int i = 1; i < topixFrame.size() - 1; i++) {
64  for (int j = 0; j < 8; j++) {
65  if (j == 0 or j == 1 or j == 2) {
66  topix_data.push_back(0x00);
67  } else {
68  topix_data.push_back((topixFrame[i] >> (7 - j) * 2 * 4) & 0xff);
69  }
70  }
71  }
72  return topix_data;
73 }
74 
75 ULong64_t PndHammingDecoder::CalculateCRCTableFast(std::vector<char> p, ULong64_t len) {
76 
77  // fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
78  // only usable with polynom orders of 8, 16, 24 or 32.
79 
80  ULong64_t crc = fCRCInit_direct;
81 
82  std::vector<char>::iterator it=p.begin();
83 
84  if (fRefIn)
85  {
86  crc = ReflectBitsStream(crc, fOrder);
87  }
88 
89  if (!fRefIn)
90  {
91  while (len--)
92  {
93  crc = (crc << 8) ^ fCRCTab[ ((crc >> (fOrder-8)) & 0xff) ^ (*it & 0xff)];
94  it++;
95  }
96  }
97  else
98  {
99  while (len--)
100  {
101  crc = (crc >> 8) ^ fCRCTab[ (crc & 0xff) ^ (*it & 0xff)];
102  it++;
103  }
104  }
105  if (fRefOut^fRefIn)
106  {
107  crc = ReflectBitsStream(crc, fOrder);
108  }
109  crc^= fCRCXor;
110  crc&= fCRCMask;
111 
112  return(crc);
113 }
114 
116  // make CRC lookup table used by table algorithms
117  ULong64_t bit, crc;
118  for (int i=0; i<256; i++) {
119  crc=(ULong64_t)i;
120  if (fRefIn)
121  {
122  crc=ReflectBitsStream(crc, 8);
123  }
124  crc<<= fOrder-8;
125 
126  for (int j=0; j<8; j++)
127  {
128  bit = crc & fCRCHighBit;
129  crc<<= 1;
130  if (bit)
131  {
132  crc^= fPolynom;
133  }
134  }
135  if (fRefIn)
136  {
137  crc = ReflectBitsStream(crc, fOrder);
138  }
139  crc&= fCRCMask;
140  fCRCTab[i]= crc;
141  }
142 }
143 
144 ULong64_t PndHammingDecoder::ReflectBitsStream(ULong64_t crc, int bitnum) {
145 
146  // reflects the lower 'bitnum' bits of 'crc'
147 
148  ULong64_t i, j=1, crcout=0;
149 
150  for (i=(ULong64_t)1<<(bitnum-1); i; i>>=1)
151  {
152  if (crc & i)
153  {
154  crcout|=j;
155  }
156  j<<= 1;
157  }
158  return (crcout);
159 }
Double_t p
Definition: anasim.C:58
std::vector< char > ConvertData(std::vector< ULong64_t > topixFrame)
const ULong64_t fPolynom
Int_t i
Definition: run_full.C:25
const ULong64_t fCRCXor
ULong64_t fCRCTab[256]
ULong64_t CalculateCRCTableFast(std::vector< char > p, ULong64_t len)
const UInt_t fRefOut
ULong64_t ReflectBitsStream(ULong64_t crc, int bitnum)
UShort_t CheckHammingCode(ULong64_t dataword, int dataword_length)