forked from powturbo/Turbo-Histogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turbohist.c
585 lines (509 loc) · 19.5 KB
/
turbohist.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// compile w. cc -O3 -march=native turbohist.c -o turbohist
// tested w. Linux: gcc & clang, Windows: mingw-w64
// homepage: http://sites.google.com/site/powturbo/
//#define RET { unsigned a=0, i; for(i = 0; i < 256; i++) a+=i*c0[i]; return a;} // CheckSum
#define RET return c0[0] // benchmark
#define CHKSIZE (1<<30) // (56*1024) //
#if 0 //CHKSIZE <= (1<<16)
typedef unsigned short bin_t;
#define PAD8 0
#else
typedef unsigned bin_t;
#define PAD8 8 // +8 to avoid repeated call into the same cache line pool (cache associativity). by Nathan Kurz : https://github.com/powturbo/turbohist/issues/2
#endif
int hist_1_8(unsigned char *in, unsigned inlen) {
bin_t c0[256] = {0};
unsigned char *ip;
for(ip=in; ip != in+inlen;)
c0[*ip++]++;
RET;
}
int hist_4_8(unsigned char *in, unsigned inlen) {
bin_t c0[256]={0},c1[256]={0},c2[256]={0},c3[256]={0};
unsigned char *ip;
for(ip = in; ip != in + (inlen&~(4-1)); )
c0[*ip++]++, c1[*ip++]++, c2[*ip++]++, c3[*ip++]++;
while(ip != in+inlen)
c0[*ip++]++;
int i;for(i = 0; i != 256; i++) c0[i] += c1[i]+c2[i]+c3[i];
RET;
}
int hist_8_8(unsigned char *in, unsigned inlen) {
bin_t c0[256+PAD8]={0},c1[256+PAD8]={0},c2[256+PAD8]={0},c3[256+PAD8]={0},c4[256+PAD8]={0},c5[256+PAD8]={0},c6[256+PAD8]={0},c7[256+PAD8]={0};
unsigned char *ip;
for(ip=in; ip != in + (inlen&~(8-1)); )
c0[*ip++]++, c1[*ip++]++, c2[*ip++]++, c3[*ip++]++, c4[*ip++]++, c5[*ip++]++, c6[*ip++]++, c7[*ip++]++;
while(ip != in+inlen)
c0[*ip++]++;
int i;for(i = 0; i < 256; i++) c0[i] += c1[i]+c2[i]+c3[i]+c4[i]+c5[i]+c6[i]+c7[i];
RET;
}
int hist_4_32(unsigned char *in, unsigned inlen) { // Optimized for x86
bin_t c0[256]={0},c1[256]={0},c2[256]={0},c3[256]={0};
unsigned char *ip;
unsigned cp = *(unsigned *)in;
for(ip = in; ip != in+(inlen&~(16-1));) {
unsigned c = cp, d = *(unsigned *)(ip+=4); cp = *(unsigned *)(ip+=4);
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[(unsigned char)(c>>8)]++; c>>=16;
c3[(unsigned char)(d>>8)]++; d>>=16;
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[ c>>8 ]++;
c3[ d>>8 ]++;
c = cp; d = *(unsigned *)(ip+=4); cp = *(unsigned *)(ip+=4);
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[(unsigned char)(c>>8)]++; c>>=16;
c3[(unsigned char)(d>>8)]++; d>>=16;
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[ c>>8 ]++;
c3[ d>>8 ]++;
}
while(ip != in+inlen) c0[*ip++]++;
int i;
for(i = 0; i < 256; i++)
c0[i] += c1[i]+c2[i]+c3[i];
RET;
}
int hist_8_32(unsigned char *in, unsigned inlen) { // Optimized for x86
unsigned char *ip;
unsigned c, d, cp = *(unsigned *)in;
bin_t c0[256+PAD8]={0},c1[256+PAD8]={0},c2[256+PAD8]={0},c3[256+PAD8]={0},c4[256+PAD8]={0},c5[256+PAD8]={0},c6[256+PAD8]={0},c7[256+PAD8]={0};
for(ip = in; ip != in+(inlen&~(16-1));) {
c = cp, d = *(unsigned *)(ip+=4); cp = *(unsigned *)(ip+=4);
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[(unsigned char)(c>>8)]++; c>>=16;
c3[(unsigned char)(d>>8)]++; d>>=16;
c4[(unsigned char) c ]++;
c5[(unsigned char) d ]++;
c6[ c>>8 ]++;
c7[ d>>8 ]++;
c = cp; d = *(unsigned *)(ip+=4); cp = *(unsigned *)(ip+=4);
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[(unsigned char)(c>>8)]++; c>>=16;
c3[(unsigned char)(d>>8)]++; d>>=16;
c4[(unsigned char) c ]++;
c5[(unsigned char) d ]++;
c6[ c>>8 ]++;
c7[ d>>8 ]++;
}
while(ip != in+inlen) c0[*ip++]++;
int i;
for(i = 0; i < 256; i++)
c0[i] += c1[i]+c2[i]+c3[i]+c4[i]+c5[i]+c6[i]+c7[i];
RET;
}
int hist_4_64(unsigned char *in, unsigned inlen) { // Optimized for x86
bin_t c0[256+PAD8]={0},c1[256+PAD8]={0},c2[256+PAD8]={0},c3[256+PAD8]={0};
unsigned char *ip;
unsigned long long cp = *(unsigned long long*)in;
for(ip = in; ip != in+(inlen&~(16-1));) {
unsigned c = cp, d = cp>>32; cp = *(unsigned long long*)(ip+=8);
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[(unsigned char)(c>>8)]++; c>>=16;
c3[(unsigned char)(d>>8)]++; d>>=16;
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[ c>>8 ]++;
c3[ d>>8 ]++;
c = cp, d = cp>>32; cp = *(unsigned long long*)(ip+=8);
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[(unsigned char)(c>>8)]++; c>>=16;
c3[(unsigned char)(d>>8)]++; d>>=16;
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[ c>>8 ]++;
c3[ d>>8 ]++;
}
while(ip < in+inlen) c0[*ip++]++;
int i;
for(i = 0; i < 256; i++)
c0[i] += c1[i]+c2[i]+c3[i];
RET;
}
int hist_8_64(unsigned char *in, unsigned inlen) { // Optimized for x86
bin_t c0[256+PAD8]={0},c1[256+PAD8]={0},c2[256+PAD8]={0},c3[256+PAD8]={0},c4[256+PAD8]={0},c5[256+PAD8]={0},c6[256+PAD8]={0},c7[256+PAD8]={0};
unsigned char *ip;
unsigned long long cp = *(unsigned long long*)in;
for(ip = in; ip != in+(inlen&~(16-1));) {
unsigned c = cp, d = cp>>32; cp = *(unsigned long long*)(ip+=8);
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[(unsigned char)(c>>8)]++; c>>=16;
c3[(unsigned char)(d>>8)]++; d>>=16;
c4[(unsigned char) c ]++;
c5[(unsigned char) d ]++;
c6[ c>>8 ]++;
c7[ d>>8 ]++;
c = cp, d = cp>>32; cp = *(unsigned long long*)(ip+=8);
c0[(unsigned char) c ]++;
c1[(unsigned char) d ]++;
c2[(unsigned char)(c>>8)]++; c>>=16;
c3[(unsigned char)(d>>8)]++; d>>=16;
c4[(unsigned char) c ]++;
c5[(unsigned char) d ]++;
c6[ c>>8 ]++;
c7[ d>>8 ]++;
}
while(ip < in+inlen) c0[*ip++]++;
int i;
for(i = 0; i < 256; i++)
c0[i] += c1[i]+c2[i]+c3[i]+c4[i]+c5[i]+c6[i]+c7[i];
RET;
}
#ifdef __SSE4_1__
#include <smmintrin.h>
int hist_4_128(unsigned char *in, unsigned inlen) {
bin_t c0[256+PAD8]={0},c1[256+PAD8]={0},c2[256+PAD8]={0},c3[256+PAD8]={0};
unsigned char *ip;
__m128i cpv = _mm_loadu_si128((__m128i*)in);
for(ip = in; ip != in+(inlen&~(32-1)); ) {
__m128i cv=cpv, dv = _mm_loadu_si128((__m128i*)(ip+=16)); cpv = _mm_loadu_si128((__m128i*)(ip+=16));
c0[_mm_extract_epi8(cv, 0)]++;
c1[_mm_extract_epi8(dv, 0)]++;
c2[_mm_extract_epi8(cv, 1)]++;
c3[_mm_extract_epi8(dv, 1)]++;
c0[_mm_extract_epi8(cv, 2)]++;
c1[_mm_extract_epi8(dv, 2)]++;
c2[_mm_extract_epi8(cv, 3)]++;
c3[_mm_extract_epi8(dv, 3)]++;
c0[_mm_extract_epi8(cv, 4)]++;
c1[_mm_extract_epi8(dv, 4)]++;
c2[_mm_extract_epi8(cv, 5)]++;
c3[_mm_extract_epi8(dv, 5)]++;
c0[_mm_extract_epi8(cv, 6)]++;
c1[_mm_extract_epi8(dv, 6)]++;
c2[_mm_extract_epi8(cv, 7)]++;
c3[_mm_extract_epi8(dv, 7)]++;
c0[_mm_extract_epi8(cv, 8)]++;
c1[_mm_extract_epi8(dv, 8)]++;
c2[_mm_extract_epi8(cv, 9)]++;
c3[_mm_extract_epi8(dv, 9)]++;
c0[_mm_extract_epi8(cv, 10)]++;
c1[_mm_extract_epi8(dv, 10)]++;
c2[_mm_extract_epi8(cv, 11)]++;
c3[_mm_extract_epi8(dv, 11)]++;
c0[_mm_extract_epi8(cv, 12)]++;
c1[_mm_extract_epi8(dv, 12)]++;
c2[_mm_extract_epi8(cv, 13)]++;
c3[_mm_extract_epi8(dv, 13)]++;
c0[_mm_extract_epi8(cv, 14)]++;
c1[_mm_extract_epi8(dv, 14)]++;
c2[_mm_extract_epi8(cv, 15)]++;
c3[_mm_extract_epi8(dv, 15)]++;
}
while(ip < in+inlen) c0[*ip++]++;
int i;
for(i = 0; i < 256; i++)
c0[i] += c1[i]+c2[i]+c3[i];
RET;
}
int hist_8_128(unsigned char *in, unsigned inlen) {
bin_t c0[256+PAD8]={0},c1[256+PAD8]={0},c2[256+PAD8]={0},c3[256+PAD8]={0},c4[256+PAD8]={0},c5[256+PAD8]={0},c6[256+PAD8]={0},c7[256+PAD8]={0};
unsigned char *ip;
__m128i cpv = _mm_loadu_si128((__m128i*)in);
for(ip = in; ip != in+(inlen&~(32-1)); ) {
__m128i cv=cpv, dv = _mm_loadu_si128((__m128i*)(ip+=16)); cpv = _mm_loadu_si128((__m128i*)(ip+=16));
c0[_mm_extract_epi8(cv, 0)]++;
c1[_mm_extract_epi8(dv, 0)]++;
c2[_mm_extract_epi8(cv, 1)]++;
c3[_mm_extract_epi8(dv, 1)]++;
c4[_mm_extract_epi8(cv, 2)]++;
c5[_mm_extract_epi8(dv, 2)]++;
c6[_mm_extract_epi8(cv, 3)]++;
c7[_mm_extract_epi8(dv, 3)]++;
c0[_mm_extract_epi8(cv, 4)]++;
c1[_mm_extract_epi8(dv, 4)]++;
c2[_mm_extract_epi8(cv, 5)]++;
c3[_mm_extract_epi8(dv, 5)]++;
c4[_mm_extract_epi8(cv, 6)]++;
c5[_mm_extract_epi8(dv, 6)]++;
c6[_mm_extract_epi8(cv, 7)]++;
c7[_mm_extract_epi8(dv, 7)]++;
c0[_mm_extract_epi8(cv, 8)]++;
c1[_mm_extract_epi8(dv, 8)]++;
c2[_mm_extract_epi8(cv, 9)]++;
c3[_mm_extract_epi8(dv, 9)]++;
c4[_mm_extract_epi8(cv, 10)]++;
c5[_mm_extract_epi8(dv, 10)]++;
c6[_mm_extract_epi8(cv, 11)]++;
c7[_mm_extract_epi8(dv, 11)]++;
c0[_mm_extract_epi8(cv, 12)]++;
c1[_mm_extract_epi8(dv, 12)]++;
c2[_mm_extract_epi8(cv, 13)]++;
c3[_mm_extract_epi8(dv, 13)]++;
c4[_mm_extract_epi8(cv, 14)]++;
c5[_mm_extract_epi8(dv, 14)]++;
c6[_mm_extract_epi8(cv, 15)]++;
c7[_mm_extract_epi8(dv, 15)]++;
}
while(ip < in+inlen) c0[*ip++]++;
int i;
for(i = 0; i < 256; i++)
c0[i] += c1[i]+c2[i]+c3[i]+c4[i]+c5[i]+c6[i]+c7[i];
RET;
}
#endif
#ifdef __AVX2__
#include <immintrin.h>
int hist_4_256(unsigned char *in, unsigned inlen) {
bin_t c0[256+PAD8]={0},c1[256+PAD8]={0},c2[256+PAD8]={0},c3[256+PAD8]={0};
unsigned char *ip;
__m256i cpv = _mm256_loadu_si256((__m256i*)in);
for(ip = in; ip != in+(inlen&~(32-1)); ) {
__m256i cv=cpv; cpv = _mm256_loadu_si256((__m256i*)(ip+=32));
c0[_mm256_extract_epi8(cv, 0)]++;
c1[_mm256_extract_epi8(cv, 1)]++;
c2[_mm256_extract_epi8(cv, 2)]++;
c3[_mm256_extract_epi8(cv, 3)]++;
c0[_mm256_extract_epi8(cv, 4)]++;
c1[_mm256_extract_epi8(cv, 5)]++;
c2[_mm256_extract_epi8(cv, 6)]++;
c3[_mm256_extract_epi8(cv, 7)]++;
c0[_mm256_extract_epi8(cv, 8)]++;
c1[_mm256_extract_epi8(cv, 9)]++;
c2[_mm256_extract_epi8(cv, 10)]++;
c3[_mm256_extract_epi8(cv, 11)]++;
c0[_mm256_extract_epi8(cv, 12)]++;
c1[_mm256_extract_epi8(cv, 13)]++;
c2[_mm256_extract_epi8(cv, 14)]++;
c3[_mm256_extract_epi8(cv, 15)]++;
c0[_mm256_extract_epi8(cv, 16)]++;
c1[_mm256_extract_epi8(cv, 17)]++;
c2[_mm256_extract_epi8(cv, 18)]++;
c3[_mm256_extract_epi8(cv, 19)]++;
c0[_mm256_extract_epi8(cv, 20)]++;
c1[_mm256_extract_epi8(cv, 21)]++;
c2[_mm256_extract_epi8(cv, 22)]++;
c3[_mm256_extract_epi8(cv, 23)]++;
c0[_mm256_extract_epi8(cv, 24)]++;
c1[_mm256_extract_epi8(cv, 25)]++;
c2[_mm256_extract_epi8(cv, 26)]++;
c3[_mm256_extract_epi8(cv, 27)]++;
c0[_mm256_extract_epi8(cv, 28)]++;
c1[_mm256_extract_epi8(cv, 29)]++;
c2[_mm256_extract_epi8(cv, 30)]++;
c3[_mm256_extract_epi8(cv, 31)]++;
}
while(ip < in+inlen) c0[*ip++]++;
int i;
for(i = 0; i < 256; i++)
c0[i] += c1[i]+c2[i]+c3[i];
RET;
}
int hist_8_256(unsigned char *in, unsigned inlen) {
bin_t c0[256+PAD8]={0},c1[256+PAD8]={0},c2[256+PAD8]={0},c3[256+PAD8]={0},c4[256+PAD8]={0},c5[256+PAD8]={0},c6[256+PAD8]={0},c7[256+PAD8]={0};
unsigned char *ip;
__m256i cpv = _mm256_loadu_si256((__m256i*)in);
for(ip = in; ip != in+(inlen&~(32-1)); ) {
__m256i cv=cpv; cpv = _mm256_loadu_si256((__m256i*)(ip+=32));
c0[_mm256_extract_epi8(cv, 0)]++;
c1[_mm256_extract_epi8(cv, 1)]++;
c2[_mm256_extract_epi8(cv, 2)]++;
c3[_mm256_extract_epi8(cv, 3)]++;
c4[_mm256_extract_epi8(cv, 4)]++;
c5[_mm256_extract_epi8(cv, 5)]++;
c6[_mm256_extract_epi8(cv, 6)]++;
c7[_mm256_extract_epi8(cv, 7)]++;
c0[_mm256_extract_epi8(cv, 8)]++;
c1[_mm256_extract_epi8(cv, 9)]++;
c2[_mm256_extract_epi8(cv, 10)]++;
c3[_mm256_extract_epi8(cv, 11)]++;
c4[_mm256_extract_epi8(cv, 12)]++;
c5[_mm256_extract_epi8(cv, 13)]++;
c6[_mm256_extract_epi8(cv, 14)]++;
c7[_mm256_extract_epi8(cv, 15)]++;
c0[_mm256_extract_epi8(cv, 16)]++;
c1[_mm256_extract_epi8(cv, 17)]++;
c2[_mm256_extract_epi8(cv, 18)]++;
c3[_mm256_extract_epi8(cv, 19)]++;
c4[_mm256_extract_epi8(cv, 20)]++;
c5[_mm256_extract_epi8(cv, 21)]++;
c6[_mm256_extract_epi8(cv, 22)]++;
c7[_mm256_extract_epi8(cv, 23)]++;
c0[_mm256_extract_epi8(cv, 24)]++;
c1[_mm256_extract_epi8(cv, 25)]++;
c2[_mm256_extract_epi8(cv, 26)]++;
c3[_mm256_extract_epi8(cv, 27)]++;
c4[_mm256_extract_epi8(cv, 28)]++;
c5[_mm256_extract_epi8(cv, 29)]++;
c6[_mm256_extract_epi8(cv, 30)]++;
c7[_mm256_extract_epi8(cv, 31)]++;
}
while(ip < in+inlen) c0[*ip++]++;
int i;
for(i = 0; i < 256; i++)
c0[i] += c1[i]+c2[i]+c3[i]+c4[i]+c5[i]+c6[i]+c7[i];
RET;
}
#endif
#define NKURTZ
#include <string.h>
#ifdef NKURTZ
// Not portable "count2x64" from https://github.com/nkurz/countbench
#define COUNT_SIZE (256 + 8)
#define ASM_SHIFT_RIGHT(reg, bitsToShift) \
__asm volatile ("shr %1, %0": \
"+r" (reg): /* read and written */ \
"i" (bitsToShift) /* constant */ \
)
#define ASM_INC_TABLES(src0, src1, byte0, byte1, offset, size, base, scale) \
__asm volatile ("movzbl %b2, %k0\n" /* byte0 = src0 & 0xFF */ \
"movzbl %b3, %k1\n" /* byte1 = src1 & 0xFF */ \
"incl (%c4+0)*%c5(%6, %0, %c7)\n" /* count[i+0][byte0]++ */ \
"incl (%c4+1)*%c5(%6, %1, %c7)\n" /* count[i+1][byte1]++ */ \
"movzbl %h2, %k0\n" /* byte0 = (src0 & 0xFF00) >> 8 */ \
"movzbl %h3, %k1\n" /* byte1 = (src1 & 0xFF00) >> 8 */ \
"incl (%c4+2)*%c5(%6, %0, %c7)\n" /* count[i+2][byte0]++ */ \
"incl (%c4+3)*%c5(%6, %1, %c7)\n": /* count[i+3][byte1]++ */ \
"=&R" (byte0), /* write only (R == non REX) */ \
"=&R" (byte1): /* write only (R == non REX) */ \
"Q" (src0), /* read only (Q == must have rH) */ \
"Q" (src1), /* read only (Q == must have rH) */ \
"i" (offset), /* constant array offset */ \
"i" (size), /* constant array size */ \
"r" (base), /* read only array address */ \
"i" (scale): /* constant [1,2,4,8] */ \
"memory" /* clobbered (forces compiler to compute sum ) */ \
)
int count2x64(unsigned char *src, size_t srcSize)
{
unsigned long long remainder = srcSize;
if (srcSize < 32) goto handle_remainder;
unsigned count[16][COUNT_SIZE];
memset(count, 0, sizeof(count));
remainder = srcSize % 16;
srcSize -= remainder;
const unsigned char *endSrc = src + srcSize;
unsigned long long next0 = *(unsigned long long *)(src + 0);
unsigned long long next1 = *(unsigned long long *)(src + 8);
//IACA_START;
while (src != endSrc)
{
unsigned long long byte0, byte1;
unsigned long long data0 = next0;
unsigned long long data1 = next1;
src += 16;
next0 = *(unsigned long long *)(src + 0);
next1 = *(unsigned long long *)(src + 8);
ASM_INC_TABLES(data0, data1, byte0, byte1, 0, COUNT_SIZE * 4, count, 4);
ASM_SHIFT_RIGHT(data0, 16);
ASM_SHIFT_RIGHT(data1, 16);
ASM_INC_TABLES(data0, data1, byte0, byte1, 4, COUNT_SIZE * 4, count, 4);
ASM_SHIFT_RIGHT(data0, 16);
ASM_SHIFT_RIGHT(data1, 16);
ASM_INC_TABLES(data0, data1, byte0, byte1, 8, COUNT_SIZE * 4, count, 4);
ASM_SHIFT_RIGHT(data0, 16);
ASM_SHIFT_RIGHT(data1, 16);
ASM_INC_TABLES(data0, data1, byte0, byte1, 12, COUNT_SIZE * 4, count, 4);
}
//IACA_END;
size_t i;
handle_remainder:
for (i = 0; i < remainder; i++) {
unsigned long long byte = src[i];
count[0][byte]++;
}
for (i = 0; i < 256; i++) { int idx;
for (idx=1; idx < 16; idx++) {
count[0][i] += count[idx][i];
}
}
return count[0][0];
}
#endif
//----------------------------------------------------------------------------------------------------------------------------
typedef unsigned long long tm_t;
static tm_t tmtime(void);
#ifdef _MSC_VER
#include <intrin.h>
#else
#include <x86intrin.h>
#endif
#ifdef _WIN32
#include <windows.h>
#define TM_T 1
static tm_t tmtime(void) {
LARGE_INTEGER tm;
QueryPerformanceCounter(&tm);
return (tm_t)(tm.QuadPart/tps.QuadPart);
}
LARGE_INTEGER tps;
static tm_t tminit() { QueryPerformanceFrequency(&tps); tm_t t0=tmtime(),ts; while((ts = tmtime())==t0); return ts; }
#else
#include <sys/time.h>
#include <sys/resource.h>
#define TM_T 1000000.0
static tm_t tmtime(void) {
struct timeval tm;
gettimeofday(&tm, NULL);
return (tm_t)tm.tv_sec*1000000ull + tm.tv_usec;
}
static tm_t tminit() { tm_t t0=tmtime(),ts; while((ts = tmtime())==t0) {}; return ts; }
#endif
//----------------------------------------------------------------------------------------------------------------------------
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MBS 1000000.0 //MiBS 1048576.0
#define TMPRINT(__x) { printf("%7.2f MB/s\t%3.1f clocks/symbol\tc=%d it=%d\t%s\n", (double)(tc>=0.000001?(((double)n*it/MBS)/(((double)tc/1)/TM_T)):0.0), (double)cc/((double)n*it), r, it, __x); }
#define TMBEG for(t0=tminit(),c0=__rdtsc(),it=0;it<reps;) { r = 0;
#define TMEND it++; if((tc = tmtime() - t0) > tx) break; } cc=__rdtsc()-c0
#if CHKSIZE <= (1<<16)
#define process(__fct, __in, __inlen) { unsigned char *ip = __in, *in_=__in+__inlen; int iplen;\
for(; ip < in_; ip += iplen) { iplen = in_ - ip; if(iplen > CHKSIZE) iplen = CHKSIZE;\
r += __fct(ip, iplen); \
}\
}
#else
#define process(__fct, __in, __inlen) r = __fct(__in, __inlen)
#endif
#define GB (1<<30)
int main(int argc, char *argv[]) { int i; unsigned char *in;int n,reps=GB; tm_t tx=1*1000000;
{ char *inname = argv[1];
FILE *fi = fopen(inname, "rb");
if(!fi ) perror(inname), exit(1);
printf("'%s'\n", inname);
fseek(fi, 0, SEEK_END);
long long flen = ftell(fi);
fseek(fi, 0, SEEK_SET);
if(flen > GB) flen = GB;
n = flen;
if(!(in = (unsigned char*)malloc(n))) { fprintf(stderr, "malloc error\n"); exit(-1); }
n = fread(in, 1, n, fi);
fclose(fi);
if(n <= 0) exit(0);
}
{
int r,k,it;
tm_t t0,tc,c0,cc;
process(hist_4_32, in, n);
TMBEG process(hist_8_32, in,n); TMEND; TMPRINT("hist_8_32");
TMBEG process(hist_4_32, in,n); TMEND; TMPRINT("hist_4_32");
#ifdef NKURTZ
TMBEG process(count2x64, in,n); TMEND; TMPRINT("count2x64 nkurz");
#endif
#ifdef __SSE4_1__
TMBEG process(hist_8_128,in,n); TMEND; TMPRINT("hist_8_128");
TMBEG process(hist_4_128,in,n); TMEND; TMPRINT("hist_4_128");
#endif
#ifdef __AVX2__
TMBEG process(hist_8_256,in,n); TMEND; TMPRINT("hist_8_256");
TMBEG process(hist_4_256,in,n); TMEND; TMPRINT("hist_4_256");
#endif
TMBEG process(hist_8_64, in,n); TMEND; TMPRINT("hist_8_64");
TMBEG process(hist_4_64, in,n); TMEND; TMPRINT("hist_4_64");
TMBEG process(hist_8_8, in,n); TMEND; TMPRINT("hist_8_8");
TMBEG process(hist_4_8, in,n); TMEND; TMPRINT("hist_4_8");
TMBEG process(hist_1_8, in,n); TMEND; TMPRINT("hist_1_8");
}
free(in);
}