-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.c
405 lines (363 loc) · 11.1 KB
/
main.c
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
int utf8_naive(const unsigned char *data, int len);
int utf8_lookup(const unsigned char *data, int len);
int utf8_boost(const unsigned char *data, int len);
int utf8_lemire(const unsigned char *data, int len);
int utf8_range(const unsigned char *data, int len);
int utf8_range2(const unsigned char *data, int len);
#ifdef __AVX2__
int utf8_lemire_avx2(const unsigned char *data, int len);
int utf8_range_avx2(const unsigned char *data, int len);
#endif
static struct ftab {
const char *name;
int (*func)(const unsigned char *data, int len);
} ftab[] = {
{
.name = "naive",
.func = utf8_naive,
},
{
.name = "lookup",
.func = utf8_lookup,
},
{
.name = "lemire",
.func = utf8_lemire,
},
{
.name = "range",
.func = utf8_range,
},
{
.name = "range2",
.func = utf8_range2,
},
#ifdef __AVX2__
{
.name = "lemire_avx2",
.func = utf8_lemire_avx2,
},
{
.name = "range_avx2",
.func = utf8_range_avx2,
},
#endif
#ifdef BOOST
{
.name = "boost",
.func = utf8_boost,
},
#endif
};
static unsigned char *load_test_buf(int len)
{
const char utf8[] = "\xF0\x90\xBF\x80";
const int utf8_len = sizeof(utf8)/sizeof(utf8[0]) - 1;
unsigned char *data = malloc(len);
unsigned char *p = data;
while (len >= utf8_len) {
memcpy(p, utf8, utf8_len);
p += utf8_len;
len -= utf8_len;
}
while (len--)
*p++ = 0x7F;
return data;
}
static unsigned char *load_test_file(int *len)
{
unsigned char *data;
int fd;
struct stat stat;
fd = open("./UTF-8-demo.txt", O_RDONLY);
if (fd == -1) {
printf("Failed to open UTF-8-demo.txt!\n");
exit(1);
}
if (fstat(fd, &stat) == -1) {
printf("Failed to get file size!\n");
exit(1);
}
*len = stat.st_size;
data = malloc(*len);
if (read(fd, data, *len) != *len) {
printf("Failed to read file!\n");
exit(1);
}
utf8_range(data, *len);
#ifdef __AVX2__
utf8_range_avx2(data, *len);
#endif
close(fd);
return data;
}
static void print_test(const unsigned char *data, int len)
{
while (len--)
printf("\\x%02X", *data++);
printf("\n");
}
struct test {
const unsigned char *data;
int len;
};
static void prepare_test_buf(unsigned char *buf, const struct test *pos,
int pos_len, int pos_idx)
{
/* Round concatenate correct tokens to 1024 bytes */
int buf_idx = 0;
while (buf_idx < 1024) {
int buf_len = 1024 - buf_idx;
if (buf_len >= pos[pos_idx].len) {
memcpy(buf+buf_idx, pos[pos_idx].data, pos[pos_idx].len);
buf_idx += pos[pos_idx].len;
} else {
memset(buf+buf_idx, 0, buf_len);
buf_idx += buf_len;
}
if (++pos_idx == pos_len)
pos_idx = 0;
}
}
/* Return 0 on success, -1 on error */
static int test_manual(const struct ftab *ftab)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-sign"
/* positive tests */
static const struct test pos[] = {
{"", 0},
{"\x00", 1},
{"\x66", 1},
{"\x7F", 1},
{"\x00\x7F", 2},
{"\x7F\x00", 2},
{"\xC2\x80", 2},
{"\xDF\xBF", 2},
{"\xE0\xA0\x80", 3},
{"\xE0\xA0\xBF", 3},
{"\xED\x9F\x80", 3},
{"\xEF\x80\xBF", 3},
{"\xF0\x90\xBF\x80", 4},
{"\xF2\x81\xBE\x99", 4},
{"\xF4\x8F\x88\xAA", 4},
};
/* negative tests */
static const struct test neg[] = {
{"\x80", 1},
{"\xBF", 1},
{"\xC0\x80", 2},
{"\xC1\x00", 2},
{"\xC2\x7F", 2},
{"\xDF\xC0", 2},
{"\xE0\x9F\x80", 3},
{"\xE0\xC2\x80", 3},
{"\xED\xA0\x80", 3},
{"\xED\x7F\x80", 3},
{"\xEF\x80\x00", 3},
{"\xF0\x8F\x80\x80", 4},
{"\xF0\xEE\x80\x80", 4},
{"\xF2\x90\x91\x7F", 4},
{"\xF4\x90\x88\xAA", 4},
{"\xF4\x00\xBF\xBF", 4},
{"\x00\x00\x00\x00\x00\xC2\x80\x00\x00\x00\xE1\x80\x80\x00\x00\xC2" \
"\xC2\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
32},
{"\x00\x00\x00\x00\x00\xC2\xC2\x80\x00\x00\xE1\x80\x80\x00\x00\x00",
16},
{"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xF1\x80",
32},
{"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xF1",
32},
{"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xF1\x80" \
"\x80", 33},
{"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xF1\x80" \
"\xC2\x80", 34},
{"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xF0" \
"\x80\x80\x80", 35},
};
#pragma GCC diagnostic push
/* Test single token */
for (int i = 0; i < sizeof(pos)/sizeof(pos[0]); ++i) {
if (ftab->func(pos[i].data, pos[i].len) != 0) {
printf("FAILED positive test: ");
print_test(pos[i].data, pos[i].len);
return -1;
}
}
for (int i = 0; i < sizeof(neg)/sizeof(neg[0]); ++i) {
if (ftab->func(neg[i].data, neg[i].len) == 0) {
printf("FAILED negitive test: ");
print_test(neg[i].data, neg[i].len);
return -1;
}
}
/* Test shifted buffer to cover 1k length */
/* buffer size must be greater than 1024 + 16 + max(test string length) */
const int max_size = 1024*2;
uint64_t buf64[max_size/8 + 2];
/* Offset 8 bytes by 1 byte */
unsigned char *buf = ((unsigned char *)buf64) + 1;
int buf_len;
for (int i = 0; i < sizeof(pos)/sizeof(pos[0]); ++i) {
/* Positive test: shift 16 bytes, validate each shift */
prepare_test_buf(buf, pos, sizeof(pos)/sizeof(pos[0]), i);
buf_len = 1024;
for (int j = 0; j < 16; ++j) {
if (ftab->func(buf, buf_len) != 0) {
printf("FAILED positive test: ");
print_test(buf, buf_len);
return -1;
}
for (int k = buf_len; k >= 1; --k)
buf[k] = buf[k-1];
buf[0] = '\x55';
++buf_len;
}
/* Negative test: trunk last non ascii */
while (buf_len >= 1 && buf[buf_len-1] <= 0x7F)
--buf_len;
if (buf_len && ftab->func(buf, buf_len-1) == 0) {
printf("FAILED negitive test: ");
print_test(buf, buf_len);
return -1;
}
}
/* Negative test */
for (int i = 0; i < sizeof(neg)/sizeof(neg[0]); ++i) {
/* Append one error token, shift 16 bytes, validate each shift */
int pos_idx = i % (sizeof(pos)/sizeof(pos[0]));
prepare_test_buf(buf, pos, sizeof(pos)/sizeof(pos[0]), pos_idx);
memcpy(buf+1024, neg[i].data, neg[i].len);
buf_len = 1024 + neg[i].len;
for (int j = 0; j < 16; ++j) {
if (ftab->func(buf, buf_len) == 0) {
printf("FAILED negative test: ");
print_test(buf, buf_len);
return -1;
}
for (int k = buf_len; k >= 1; --k)
buf[k] = buf[k-1];
buf[0] = '\x66';
++buf_len;
}
}
return 0;
}
static int test(const unsigned char *data, int len, const struct ftab *ftab)
{
int ret_standard = ftab->func(data, len);
int ret_manual = test_manual(ftab);
printf("%s\n", ftab->name);
printf("standard test: %s\n", ret_standard ? "FAIL" : "pass");
printf("manual test: %s\n", ret_manual ? "FAIL" : "pass");
return ret_standard | ret_manual;
}
static int bench(const unsigned char *data, int len, const struct ftab *ftab)
{
const int loops = 1024*1024*1024/len;
int ret = 0;
double time, size;
struct timeval tv1, tv2;
fprintf(stderr, "bench %s... ", ftab->name);
gettimeofday(&tv1, 0);
for (int i = 0; i < loops; ++i)
ret |= ftab->func(data, len);
gettimeofday(&tv2, 0);
printf("%s\n", ret?"FAIL":"pass");
time = tv2.tv_usec - tv1.tv_usec;
time = time / 1000000 + tv2.tv_sec - tv1.tv_sec;
size = ((double)len * loops) / (1024*1024);
printf("time: %.4f s\n", time);
printf("data: %.0f MB\n", size);
printf("BW: %.2f MB/s\n", size / time);
return 0;
}
static void usage(const char *bin)
{
printf("Usage:\n");
printf("%s test [alg] ==> test all or one algorithm\n", bin);
printf("%s bench [alg] ==> benchmark all or one algorithm\n", bin);
printf("%s bench size NUM ==> benchmark with specific buffer size\n", bin);
printf("alg = ");
for (int i = 0; i < sizeof(ftab)/sizeof(ftab[0]); ++i)
printf("%s ", ftab[i].name);
printf("\nNUM = buffer size in bytes, 1 ~ 67108864(64M)\n");
}
int main(int argc, char *argv[])
{
int len = 0;
unsigned char *data;
const char *alg = NULL;
int (*tb)(const unsigned char *data, int len, const struct ftab *ftab);
tb = NULL;
if (argc >= 2) {
if (strcmp(argv[1], "test") == 0)
tb = test;
else if (strcmp(argv[1], "bench") == 0)
tb = bench;
if (argc >= 3) {
alg = argv[2];
if (strcmp(alg, "size") == 0) {
if (argc < 4) {
tb = NULL;
} else {
alg = NULL;
len = atoi(argv[3]);
if (len <= 0 || len > 67108864) {
printf("Buffer size error!\n\n");
tb = NULL;
}
}
}
}
}
if (tb == NULL) {
usage(argv[0]);
return 1;
}
/* Load UTF8 test buffer */
if (len)
data = load_test_buf(len);
else
data = load_test_file(&len);
int ret = 0;
if (tb == bench)
printf("=============== Bench UTF8 (%d bytes) ===============\n", len);
for (int i = 0; i < sizeof(ftab)/sizeof(ftab[0]); ++i) {
if (alg && strcmp(alg, ftab[i].name) != 0)
continue;
ret |= tb((const unsigned char *)data, len, &ftab[i]);
printf("\n");
}
#if 0
if (tb == bench) {
printf("==================== Bench ASCII ====================\n");
/* Change test buffer to ascii */
for (int i = 0; i < len; i++)
data[i] &= 0x7F;
for (int i = 0; i < sizeof(ftab)/sizeof(ftab[0]); ++i) {
if (alg && strcmp(alg, ftab[i].name) != 0)
continue;
tb((const unsigned char *)data, len, &ftab[i]);
printf("\n");
}
}
#endif
free(data);
return ret;
}