source: trunk/Mars/mcore/checksum.h

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