source: branches/removing_cpp11_features/mcore/checksum.h@ 19850

Last change on this file since 19850 was 18049, checked in by tbretz, 10 years ago
File size: 5.7 KB
Line 
1#ifndef MARS_checksum
2#define MARS_checksum
3
4#ifndef __CINT__
5#include <arpa/inet.h>
6#endif
7
8#include <stdint.h>
9
10class Checksum
11{
12public:
13 uint64_t buffer;
14
15 void reset() { buffer = 0; }
16 Checksum() : buffer(0) { }
17 Checksum(const Checksum &sum) : buffer(sum.buffer) { }
18 Checksum(uint64_t v) : buffer(((v>>16)&0xffff) | ((v&0xffff)<<32)) { }
19
20 uint32_t val() const { return (((buffer&0xffff)<<16) | ((buffer>>32)&0xffff)); }
21
22 bool valid() const { return buffer==0xffff0000ffff; }
23
24 void HandleCarryBits()
25 {
26 while (1)
27 {
28 const uint64_t carry = ((buffer>>48)&0xffff) | ((buffer&0xffff0000)<<16);
29 if (!carry)
30 break;
31
32 buffer = (buffer&0xffff0000ffff) + carry;
33 }
34 }
35
36 Checksum &operator+=(const Checksum &sum)
37 {
38 buffer += sum.buffer;
39 HandleCarryBits();
40 return *this;
41 }
42
43 Checksum operator+(Checksum sum) const
44 {
45 return (sum += *this);
46 }
47
48
49 bool add(const char *buf, size_t len, bool big_endian = true)
50 {
51 // Avoid overflows in carry bits
52 if (len>262140) // 2^18-4
53 {
54 add(buf, 262140);
55 return add(buf+262140, len-262140);
56 }
57
58 if (len%4>0)
59 {
60 std::ostringstream sout;
61 sout << "Length " << len << " not dividable by 4";
62
63#ifdef __EXCEPTIONS
64 throw std::runtime_error(sout.str());
65#else
66 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
67 return false;
68#endif
69 }
70
71 const uint16_t *sbuf = reinterpret_cast<const uint16_t *>(buf);
72
73 uint32_t *hilo = reinterpret_cast<uint32_t*>(&buffer);
74
75
76 const uint16_t *end = sbuf + len/2;
77
78 if (big_endian)
79 addLoopSwapping(sbuf, end, hilo);
80 else
81 addLoop(sbuf, end, hilo);
82 /*const uint16_t *end = sbuf + len/2;
83 while (1)
84 {
85 if (sbuf==end)
86 break;
87
88 hilo[0] += ntohs(*sbuf++);
89
90 if (sbuf==end)
91 break;
92
93 hilo[1] += ntohs(*sbuf++);
94 }*/
95
96 HandleCarryBits();
97
98 return true;
99 }
100
101 void addLoopSwapping(const uint16_t *sbuf, const uint16_t *end, uint32_t* hilo)
102 {
103 /*
104 for (size_t i = 0; i < len/2; i++)
105 {
106 //swap the bytes of the 32 bits value. but...
107 //the hi and lo values are stored in fits-like order. do not swap them
108 hilo[i%2] += ntohs(sbuf[i]); //(sbuf[i]&0xff00)>>8 | (sbuf[i]&0x00ff)<<8;
109 }*/
110
111 // This is about as twice as fast as the loop above
112 // ntohs is CPU optimized, i%2 doesn't need to be computed
113 while (1)
114 {
115 if (sbuf==end)
116 break;
117
118 hilo[0] += ntohs(*sbuf++);
119
120 if (sbuf==end)
121 break;
122
123 hilo[1] += ntohs(*sbuf++);
124 }
125 }
126
127 void addLoop(const uint16_t *sbuf, const uint16_t *end, uint32_t* hilo)
128 {
129 while (1)
130 {
131 if (sbuf==end)
132 break;
133
134 hilo[0] += ntohs(*sbuf++);
135
136 if (sbuf==end)
137 break;
138
139 hilo[1] += ntohs(*sbuf++);
140 }
141 }
142
143 bool add(const std::vector<char> &v, bool big_endian = true)
144 {
145 return add(v.data(), v.size(), big_endian);
146 }
147
148 std::string str(bool complm=true) const
149 {
150 std::string rc(16,0);
151
152 const uint8_t exclude[13] =
153 {
154 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
155 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60
156 };
157
158 const uint32_t value = complm ? ~val() : val(); // complement each bit of the value
159
160 for (int ii = 0; ii < 4; ii++)
161 {
162 uint8_t byte = (value >> (24 - (8 * ii)));
163
164 const uint8_t quotient = byte / 4 + '0';
165 const uint8_t remainder = byte % 4;
166
167 uint32_t ch[4] = { uint32_t(quotient+remainder), quotient, quotient, quotient };
168
169 // avoid ASCII punctuation
170 while (1)
171 {
172 bool check = false;
173 for (int kk = 0; kk < 13; kk++)
174 {
175 for (int jj = 0; jj < 4; jj += 2)
176 {
177 if (ch[jj] != exclude[kk] && ch[jj+1] != exclude[kk])
178 continue;
179
180 ch[jj]++;
181 ch[jj+1]--;
182 check++;
183 }
184 }
185
186 if (!check)
187 break;
188 }
189
190 for (int jj = 0; jj < 4; jj++) // assign the bytes
191 rc[4*jj+ii] = ch[jj];
192 }
193
194 const char lastChar = rc[15];
195 for (int i=15;i>0;i--)
196 rc[i] = rc[i-1];
197 rc[0] = lastChar;
198 return rc;
199/*
200 uint8_t *p = reinterpret_cast<uint8_t*>(&value);
201 //swap the bytes of the value
202 uint8_t temp;
203 temp = p[0];
204 p[0] = p[3];
205 p[3] = temp;
206 temp = p[1];
207 p[1] = p[2];
208 p[2] = temp;
209
210 for (int i=0; i<4; i++)
211 {
212 rc[i+ 0] = '0' + p[i]/4 + p[i]%4;
213 rc[i+ 4] = '0' + p[i]/4;
214 rc[i+ 8] = '0' + p[i]/4;
215 rc[i+12] = '0' + p[i]/4;
216 }
217
218 while(1)
219 {
220 bool ok = true;
221 for (int i=0; i<16-4; i++)
222 {
223 for (int j=0; j<13; j++)
224 if (rc[i]==exclude[j] || rc[(i+4)%16]==exclude[j])
225 {
226 rc[i]++;
227 rc[(i+4)%16]--;
228 ok = false;
229 }
230 }
231
232 if (ok)
233 break;
234 }
235
236
237 return rc;
238 */
239 }
240};
241
242#endif
Note: See TracBrowser for help on using the repository browser.