-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcrcbits.cpp
333 lines (289 loc) · 8.24 KB
/
crcbits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <thread>
#include <mutex>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <set>
#include <immintrin.h>
namespace {
const uint32_t tbl[1][5] = {
{0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3}
};
/* BCH codes over GF(2^5)
*/
uint32_t compute_checksum4(const uint8_t* data, int len, int gen) {
uint32_t l = 0;
while (len > 6) {
uint8_t c = *(data++);
len--;
uint8_t t = (l >> 25) ^ c;
l = (l & 0x1FFFFFFULL) << 5;
l ^= -((t & 1) != 0) & tbl[gen][0];
l ^= -((t & 2) != 0) & tbl[gen][1];
l ^= -((t & 4) != 0) & tbl[gen][2];
l ^= -((t & 8) != 0) & tbl[gen][3];
l ^= -((t & 16) != 0) & tbl[gen][4];
}
uint32_t f = *(data++);
f <<= 5;
f |= *(data++);
f <<= 5;
f |= *(data++);
f <<= 5;
f |= *(data++);
f <<= 5;
f |= *(data++);
f <<= 5;
f |= *(data++);
return l ^ f;
}
/*
inline uint32_t rstable(uint16_t x) {
uint32_t t = ((x << 1) ^ x) << 1 ^ x;
uint32_t h = x >> 4 | (t >> 7) << 10 | (t >> 9) << 20;
return ((x & 0xF) << 6 | ((t & 0x7F) | (t & 0x1FF) << 8) << 13) ^ h ^ (h << 3);
}
// RS code over GF(2^10), max length 2046 (1023*2), HD 4
uint32_t compute_checksum3(const uint8_t* data, int len, int gen) {
uint32_t l = 0;
while (len > 6) {
uint16_t c = *(data++);
len--;
if (len & 1) {
c <<= 5;
c |= *(data++);
len--;
}
l = ((l & 0xFFFFF) << 10) ^ rstable((l >> 20) ^ c);
}
uint32_t f = *(data++);
f <<= 5;
f |= *(data++);
f <<= 5;
f |= *(data++);
f <<= 5;
f |= *(data++);
f <<= 5;
f |= *(data++);
f <<= 5;
f |= *(data++);
return l ^ f;
}
// different CRC-6's over the individual bits of 5-bit characters, max length 57, HD 3
uint32_t compute_checksum2(const uint8_t* data, int len, int gen) {
uint32_t ret = 0;
while (len--) {
ret = ((ret & 0x1FFFFFF) << 5) ^ (((ret >> 25) & 0x15) * 0x2000421) ^ (((ret >> 25) & 0xA) * 0x2100021) ^ *(data++);
}
return ret;
}
// A CRC-6 over the individual bits of 5-bit characters. max length 57, HD 3
uint32_t compute_checksum1(const uint8_t* data, int len, int gen) {
uint32_t ret = 0;
while (len--) {
ret = ((ret & 0x1FFFFFF) << 5) ^ ((ret >> 25) * 0x2000421) ^ *(data++);
}
return ret;
}
// A CRC-30 over all the bits. max length infinity, HD 2
uint32_t compute_checksum0(const uint8_t* data, int len, int gen) {
uint32_t ret = 0;
while (len--) {
uint8_t c = *(data++);
for (int i = 0; i < 5; i++) {
ret = ((ret & 0x1FFFFFFF) << 1) ^ ((ret >> 29) * 0x2030b9c7) ^ ((c >> i) & 1);
}
}
return ret;
}
*/
/*
struct Checksum {
const char* name;
uint32_t (*fun)(const uint8_t*, int, int);
int gen;
} static const checksums[] = {
// {"CRC30", &compute_checksum0, 0},
// {"CRC-6-sliced", &compute_checksum1, 0, 3},
// {"2xCRC-6-sliced", &compute_checksum2, 0, 3},
// {"RS_10", &compute_checksum3, 0},
};
*/
static inline uint64_t rotl(const uint64_t x, int k) {
return (x << k) | (x >> (64 - k));
}
class Rander {
uint64_t s[2];
uint32_t count;
uint64_t next;
int bits;
void Produce(void) {
const uint64_t s0 = s[0];
uint64_t s1 = s[1];
next = s0 + s1;
bits = 64;
s1 ^= s0;
s[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b
s[1] = rotl(s1, 36); // c
}
void Step() {
static_assert(sizeof(unsigned long long) == sizeof(uint64_t), "Bad ULL length");
if ((count & 0xFFFFFUL) == 0) {
s[0] = 0;
s[1] = 0;
_rdrand64_step((unsigned long long*)(s + 0));
_rdrand64_step((unsigned long long*)(s + 1));
}
++count;
Produce();
}
public:
Rander() : count(0), bits(0) {}
uint32_t GetBits(int bits_) {
if (bits_ > bits) {
Step();
}
uint32_t ret = next & ((1UL << bits_) - 1);
next >>= bits_;
bits -= bits_;
return ret;
}
uint32_t GetInt(uint32_t range, int bits_) {
do {
uint32_t r = GetBits(bits_);
if (r < range) return r;
} while(true);
}
};
#define LEN 71
#define LENBITS 7
#define MAX_BITS_PER_SYM 1
#define CHECKSUMS (sizeof(tbl)/sizeof(tbl[0]))
// #define CHECKSUMS (sizeof(checksums)/sizeof(checksums[0]))
// #define CHECKSUMS 180
#define MINERR 5
#define MAXERR 7
struct CRCOutputs {
uint32_t val[LEN][5][CHECKSUMS];
CRCOutputs() {
unsigned char data[LEN] = {0};
for (int pos = 0; pos < LEN; pos++) {
for (int v = 0; v < 5; v++) {
data[pos] = (1 << v);
for (unsigned int c = 0; c < CHECKSUMS; c++) {
// val[pos][v][c] = checksums[c].fun(data, LEN, checksums[c].gen);
val[pos][v][c] = compute_checksum4(data, LEN, c);
}
}
data[pos] = 0;
}
}
};
const CRCOutputs outputs;
struct Results {
uint64_t fails[CHECKSUMS];
uint64_t count;
Results() : fails{0}, count(0) {}
Results& operator+=(const Results& x) {
for (unsigned int i = 0; i < CHECKSUMS; i++) {
fails[i] += x.fails[i];
}
count += x.count;
return *this;
}
};
void test(int errors, uint64_t loop, Results* ret, Rander& rng, const std::set<int>& which) {
Results res = {};
for (uint64_t i = 0; i < loop; i++) {
uint32_t crc[CHECKSUMS] = {0};
char symerrs[LEN] = {0};
for (int j = 0; j < errors; j++) {
while (true) {
int sympos = rng.GetInt(LEN, LENBITS);
if (symerrs[sympos]) continue;
int biterr = rng.GetInt(5, 3);
symerrs[sympos] = 1;
// for (int c : which) {
int c = 0;
crc[c] ^= outputs.val[sympos][biterr][c];
// }
break;
}
}
for (int c : which) {
res.fails[c] += (crc[c] == 0);
}
res.count++;
}
*ret = res;
}
static Results allresults[MAXERR-MINERR+1];
std::mutex cs_allresults;
void thread_crc() {
Rander rng;
std::set<int> which;
for (unsigned int c = 0; c < CHECKSUMS; c++) {
which.insert(c);
}
do {
Results r[MAXERR-MINERR+1];
for (int e = 0; e < MAXERR-MINERR+1; e++) {
test(e + MINERR, 1 << 16, &r[e], rng, which);
}
{
std::unique_lock<std::mutex> lock(cs_allresults);
for (int e = 0; e < MAXERR-MINERR+1; e++) {
allresults[e] += r[e];
}
/* for (auto it = which.begin(); it != which.end(); ) {
bool keep = false;
for (int e = 0; e < MAXERR-MINERR+1; e++) {
if (!allresults[e].fails[*it]) {
keep = true;
break;
}
}
if (!keep) {
which.erase(it++);
} else {
++it;
}
}*/
}
} while(true);
}
void thread_dump() {
uint32_t iter = 0;
do {
sleep(10);
std::unique_lock<std::mutex> lock(cs_allresults);
printf("#counts %u: ", iter++);
for (int e = 0; e < MAXERR-MINERR+1; e++) {
printf("%llu,", (unsigned long long)allresults[e].count);
}
printf("\n");
for (unsigned int i = 0; i < CHECKSUMS; i++) {
// if (ok) {
printf("\"BCH 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\"", tbl[i][0], tbl[i][1], tbl[i][2], tbl[i][3], tbl[i][4]);
for (int e = 0; e < MAXERR-MINERR+1; e++) {
printf(",% 10g", ((double)allresults[e].fails[i]) / allresults[e].count * 1073741824.0);
}
printf("\n");
// }
}
printf("\n\n");
} while(true);
}
}
int main(int argc, char** argv) {
setbuf(stdout, NULL);
int threads = argc > 1 ? strtol(argv[1], NULL, 10) : 1;
for (int t = 0; t < threads; t++) {
std::thread th(thread_crc);
th.detach();
}
thread_dump();
return 0;
}