-
Notifications
You must be signed in to change notification settings - Fork 6
/
crcelim.cpp
358 lines (314 loc) · 10.2 KB
/
crcelim.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <stdio.h>
#include <stdint.h>
#include <vector>
#include <set>
#include <string.h>
#include <assert.h>
#include <immintrin.h>
#include <unistd.h>
#include <mutex>
#include <time.h>
#include <thread>
#include <math.h>
#include <tuple>
#define MAXLEN 80
static uint32_t bch(const uint8_t* data, size_t len, uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4) {
uint32_t checksum = 0;
while (len) {
uint32_t b = (checksum >> 25) ^ *(data++);
len--;
checksum = (checksum & 0x1FFFFFF) << 5;
checksum ^= -((b & 1) != 0) & x0;
checksum ^= -((b & 2) != 0) & x1;
checksum ^= -((b & 4) != 0) & x2;
checksum ^= -((b & 8) != 0) & x3;
checksum ^= -((b & 16) != 0) & x4;
}
return checksum;
}
static double timer(void) {
struct timespec ret;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ret);
return ret.tv_nsec * 0.000000001 + ret.tv_sec;
}
class Rander {
unsigned char data[4096];
int pos;
void Step() {
if ((pos & 0xFFF) == 0) {
for (int i = 0; i < 4096; i += sizeof(unsigned long long)) {
_rdrand64_step((unsigned long long*)(data + i));
}
pos = 0;
}
}
public:
Rander() {
memset(data, 0, sizeof(data));
pos = 0;
}
uint8_t GetByte() {
Step();
uint8_t ret = data[pos];
pos++;
return ret;
}
uint8_t GetInt(uint8_t max, int bits) {
uint8_t r;
do {
r = GetByte() & ((1 << bits) - 1);
} while (r >= max);
return r;
}
uint32_t GetBigInt(uint32_t max) {
int bits = 8 * sizeof(unsigned long) - __builtin_clzl((unsigned long)max);
uint32_t r;
do {
r = (((uint32_t)GetByte()) << 24 | ((uint32_t)GetByte()) << 16 | ((uint32_t)GetByte()) << 8 | ((uint32_t)GetByte())) & ((((uint32_t)1) << bits) - 1);
} while (r >= max);
return r;
}
};
class BCHCode {
uint32_t val[MAXLEN][31];
std::string desc;
public:
BCHCode(const std::string& desc_, uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4) : desc(desc_) {
uint8_t data[MAXLEN] = {0};
uint32_t base = bch(data, MAXLEN, x0, x1, x2, x3, x4);
for (int i = 0; i < MAXLEN; i++) {
for (int j = 1; j <= 31; j++) {
data[i] = j;
val[i][j - 1] = bch(data, MAXLEN, x0, x1, x2, x3, x4) ^ base;
}
data[i] = 0;
}
}
BCHCode(const BCHCode& code) : desc(code.desc) {
memcpy(val, code.val, sizeof(val));
}
inline uint32_t syndrome(size_t pos, int err) {
return val[pos][err - 1];
}
std::string GetDesc() const {
return desc;
}
};
struct BCHState {
size_t codepos;
uint64_t attempts;
size_t len;
};
struct StateInfo {
size_t len[4];
size_t count[4];
size_t firstiter;
double rate;
std::vector<std::string> names;
std::vector<uint64_t> iterations;
std::vector<size_t> lens;
};
struct BCHStatePtrComparator {
bool operator()(const BCHState* a, const BCHState* b) {
// Speed up the common case
if (a == b) return false;
// Codes that are candidates for higher lengths first
if (a->len > b->len) return true;
if (a->len < b->len) return false;
// Code with fewer attempts at the same length first
if (a->attempts < b->attempts) return true;
if (a->attempts > b->attempts) return false;
// Disambiguation
if (a < b) return true;
return false;
}
};
class State {
std::mutex mutex;
std::vector<BCHCode> codes;
std::vector<BCHState> states;
std::set<BCHState*, BCHStatePtrComparator> stateptrs;
double rate, weight, tim;
public:
void AddCode(BCHCode&& code, size_t len) {
codes.emplace_back(std::move(code));
states.push_back(BCHState{codes.size() - 1, 0, len});
}
State() : rate(0), weight(0), tim(0) {}
void Start() {
for (size_t i = 0; i < states.size(); i++) {
stateptrs.insert(&states[i]);
}
}
const BCHCode& code(size_t pos) { return codes[pos]; }
std::pair<size_t, std::vector<size_t>> GetCodes(size_t num, size_t iterations) {
std::vector<size_t> ret;
ret.reserve(num);
std::unique_lock<std::mutex> lock(mutex);
auto it = stateptrs.begin();
assert(it != stateptrs.end());
size_t len = (*it)->len;
ret.push_back((*it)->codepos);
{
auto oldit = it++;
auto oldptr = *oldit;
stateptrs.erase(oldit);
oldptr->attempts += iterations;
stateptrs.insert(oldptr);
}
while (it != stateptrs.end() && ret.size() < num) {
if ((*it)->len < len) {
break;
}
ret.push_back((*it)->codepos);
{
auto oldit = it++;
auto oldptr = *oldit;
stateptrs.erase(oldit);
oldptr->attempts += iterations;
stateptrs.insert(oldptr);
}
}
return std::make_pair(len, std::move(ret));
}
void Failed(size_t codepos, size_t faillen) {
std::unique_lock<std::mutex> lock(mutex);
if (faillen <= states[codepos].len) {
stateptrs.erase(&states[codepos]);
states[codepos].attempts = 0;
states[codepos].len = faillen - 1;
stateptrs.insert(&states[codepos]);
}
}
void Update(uint64_t count, double starttime) {
std::unique_lock<std::mutex> lock(mutex);
double t = timer();
double duration = t - starttime;
double coef = pow(0.99, t - tim);
rate = coef * rate + count;
weight = coef * weight + duration;
tim = t;
}
StateInfo GetInfo() {
std::unique_lock<std::mutex> lock(mutex);
StateInfo ret = {};
auto it = stateptrs.begin();
int got = -1;
size_t oldlen = 0;
size_t count = 0;
while (it != stateptrs.end()) {
if ((*it)->len != oldlen) {
if (got >= 0 && got < 4) {
ret.len[got] = oldlen;
ret.count[got] = count;
} else if (got == -1) {
ret.firstiter = (*it)->attempts;
}
got++;
count = 1;
oldlen = (*it)->len;
} else {
count++;
}
ret.names.push_back(code((*it)->codepos).GetDesc());
ret.iterations.push_back((*it)->attempts);
ret.lens.push_back((*it)->len);
it++;
}
ret.rate = rate / weight;
return ret;
}
};
void ThreadCheck(State* state, size_t num, size_t errs, size_t iterations) {
Rander rander;
do {
double start = timer();
auto x = state->GetCodes(num, iterations);
int bits = 8 * sizeof(unsigned int) - __builtin_clz((unsigned int)(x.first - 2));
std::vector<BCHCode> codes;
codes.reserve(x.second.size());
for (auto p : x.second) {
codes.emplace_back(state->code(p));
}
std::vector<uint32_t> crc;
std::vector<size_t> errpos;
errpos.resize(errs - 2);
for (size_t i = 0; i < iterations; i++) {
size_t lastpos = 0;
crc.assign(codes.size(), 0);
for (size_t e = 0; e < errs; e++) {
size_t errp;
if (e == 0) {
errp = 0;
} else if (e == 1) {
lastpos = x.first - 1;
while (rander.GetInt(2, 1)) {
lastpos--;
}
errp = lastpos;
} else {
int ok;
do {
errp = 1 + rander.GetInt(lastpos - 1, bits);
ok = 1;
for (size_t f = 2; f < e; f++) {
if (errp == errpos[f - 2]) {
ok = 0;
break;
}
}
} while (!ok);
errpos[e - 2] = errp;
}
int mis = 1 + rander.GetInt(31, 5);
for (size_t c = 0; c < codes.size(); c++) {
crc[c] ^= codes[c].syndrome(errp, mis);
}
}
for (size_t c = 0; c < codes.size(); c++) {
if (crc[c] == 0) {
state->Failed(x.second[c], lastpos + 1);
}
}
}
state->Update(iterations * codes.size(), start);
} while(true);
}
void ThreadDump(State* state) {
do {
sleep(10);
auto x = state->GetInfo();
printf("[%g Gi, %g Gi/s] {%i: %llu} {%i: %llu} {%i: %llu} {%i: %llu}\n", x.firstiter / 1073741824.0, x.rate / 1073741824.0, (int)x.len[0], (unsigned long long)x.count[0], (int)x.len[1], (unsigned long long)x.count[1], (int)x.len[2], (unsigned long long)x.count[2], (int)x.len[3], (unsigned long long)x.count[3]);
FILE* file = fopen("crcelim.dump.tmp", "w");
assert(file != NULL);
for (size_t i = 0; i < x.names.size(); i++) {
fprintf(file, "%i %s # %llu iterations\n", (int)x.lens[i], x.names[i].c_str(), (unsigned long long)x.iterations[i]);
}
fclose(file);
rename("crcelim.dump.tmp", "crcelim.dump");
} while(true);
}
int main(void) {
setbuf(stdout, NULL);
State state;
unsigned long l[5];
char c[256];
while (fgets(c, sizeof(c), stdin)) {
int len = 0;
int skip;
if (sscanf(c, "%i %n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", &len, &skip, &l[0], &l[1], &l[2], &l[3], &l[4]) == 6) {
while (isspace(c[strlen(c) - 1])) {
c[strlen(c) - 1] = 0;
}
state.AddCode(BCHCode(std::string(c + skip), l[0], l[1], l[2], l[3], l[4]), len);
}
}
printf("Running\n");
state.Start();
for (int i = 0; i < 8; i++) {
std::thread th(ThreadCheck, &state, 2 << 5, 4, 2 << 16);
th.detach();
}
ThreadDump(&state);
return 0;
}