forked from powturbo/TurboPFor-Integer-Compression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvp4d.c
executable file
·536 lines (485 loc) · 15.8 KB
/
vp4d.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
/**
Copyright (C) powturbo 2013-2018
GPL v2 License
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- homepage : https://sites.google.com/site/powturbo/
- github : https://github.com/powturbo
- twitter : https://twitter.com/powturbo
- email : powturbo [_AT_] gmail [_DOT_] com
**/
// "Integer Compression" TurboPFor - Pfor/PforDelta
#ifndef USIZE
#pragma warning( disable : 4005)
#pragma warning( disable : 4090)
#pragma warning( disable : 4068)
#include "conf.h"
#include "bitutil.h"
#include "bitpack.h"
#include "vint.h"
#include "vp4.h"
#define PAD8(__x) ( (((__x)+8-1)/8) )
#define P4DELTA(a)
#define P4DELTA_(a)
#ifdef __SSSE3__
#include <tmmintrin.h>
extern char _shuffle_32[16][16]; // defined in bitunpack.c
extern char _shuffle_16[256][16];
#endif
#if !defined(SSE2_ON) && !defined(AVX2_ON)
#define _P4DEC _p4dec
#define P4DEC p4dec
#define P4NDEC p4ndec
#define P4NDECS p4dec
#define BITUNPACK bitunpack // unpack only
#define BITUNPACKD bitunpack // integrated unpack
#define _BITUNPACKD bitunpack // integrated pfor
#define P4DECX // direct access no 64 bits
#define USIZE 8
#include "vp4d.c"
#define USIZE 16
#include "vp4d.c"
#define USIZE 32
#include "vp4d.c"
#undef P4DECX
#define USIZE 64
#include "vp4d.c"
#define P4DELTA(a) ,a
#define P4DELTA_(a) a
#define DELTA
#define _P4DEC _p4ddec //delta0
#define P4DEC p4ddec
#define P4NDEC p4nddec
#define P4NDECS p4ddec
#define BITUNPACKD bitdunpack
#define _BITUNPACKD _bitdunpack
#define BITUNDD bitddec
#define USIZE 8
#include "vp4d.c"
#define USIZE 16
#include "vp4d.c"
#define USIZE 32
#include "vp4d.c"
#define USIZE 64
#include "vp4d.c"
#define _P4DEC _p4d1dec //delta1
#define P4DEC p4d1dec
#define P4NDEC p4nd1dec
#define P4NDECS p4d1dec
#define BITUNPACKD bitd1unpack
#define _BITUNPACKD bitd1unpack
#define BITUNDD bitd1dec
#define USIZE 8
#include "vp4d.c"
#define USIZE 16
#include "vp4d.c"
#define USIZE 32
#include "vp4d.c"
#define USIZE 64
#include "vp4d.c"
#define _P4DEC _p4zdec //zigzag0
#define P4DEC p4zdec
#define P4NDEC p4nzdec
#define P4NDECS p4zdec
#define BITUNPACKD bitzunpack
#define _BITUNPACKD _bitzunpack
#define BITUNDD bitzdec
#define USIZE 8
#include "vp4d.c"
#define USIZE 16
#include "vp4d.c"
#define USIZE 32
#include "vp4d.c"
#define USIZE 64
#include "vp4d.c"
#undef _P4DEC
#undef P4DEC
#undef BITUNPACK
#undef BITUNDD
#undef P4DELTA
#endif
#undef USIZE
#undef DELTA
#if defined(__SSSE3__) && defined(SSE2_ON)
#define VSIZE 128
#define P4DELTA(a)
#define P4DELTA_(a)
#define _P4DEC _p4dec128v
#define P4DEC p4dec128v
#define P4NDEC p4ndec128v
#define P4NDECS p4dec
#define BITUNPACK bitunpack128v
#define BITUNPACKD bitunpack128v
#define _BITUNPACKD _bitunpack128v
#define USIZE 16
#include "vp4d.c"
#define USIZE 32
#include "vp4d.c"
#define USIZE 64
#include "vp4d.c"
#define P4DELTA(a) ,a
#define P4DELTA_(a) a
#define DELTA
#define _P4DEC _p4ddec128v
#define P4DEC p4ddec128v
#define P4NDEC p4nddec128v
#define P4NDECS p4ddec
#define BITUNPACKD bitdunpack128v
#define _BITUNPACKD _bitdunpack128v
#define BITUNDD bitddec
#define USIZE 16
#include "vp4d.c"
#define USIZE 32
#include "vp4d.c"
#define _P4DEC _p4d1dec128v
#define P4DEC p4d1dec128v
#define P4NDEC p4nd1dec128v
#define P4NDECS p4d1dec
#define BITUNPACKD bitd1unpack128v
#define _BITUNPACKD _bitd1unpack128v
#define BITUNDD bitd1dec
#define USIZE 16
#include "vp4d.c"
#define USIZE 32
#include "vp4d.c"
#define _P4DEC _p4zdec128v
#define P4DEC p4zdec128v
#define P4NDEC p4nzdec128v
#define P4NDECS p4zdec
#define BITUNPACKD bitzunpack128v
#define _BITUNPACKD _bitzunpack128v
#define BITUNDD bitzdec
#define USIZE 16
#include "vp4d.c"
#define USIZE 32
#include "vp4d.c"
#undef BITUNDD
#undef P4DELTA
#undef DELTA
#define VSIZE 256
#define P4DELTA(a)
#define P4DELTA_(a)
#undef DELTA
#define _P4DEC _p4dec256w
#define P4DEC p4dec256w
#define P4NDEC p4ndec256w
#define P4NDECS p4dec
#define BITUNPACK bitunpack256w
#define BITUNPACKD bitunpack256w
#define _BITUNPACKD _bitunpack256w
#include "vp4d.c"
#endif
#undef DELTA
#if defined(__AVX2__) && defined(AVX2_ON)
#define VSIZE 256
#define P4DELTA(a)
#define P4DELTA_(a)
#define _P4DEC _p4dec256v
#define P4DEC p4dec256v
#define P4NDEC p4ndec256v
#define P4NDECS p4dec
#define BITUNPACK bitunpack256v
#define BITUNPACKD bitunpack256v
#define _BITUNPACKD _bitunpack256v
#define USIZE 32
#include "vp4d.c"
#define P4DELTA(a) ,a
#define P4DELTA_(a) a
#define DELTA
#define _P4DEC _p4ddec256v
#define P4DEC p4ddec256v
#define P4NDEC p4nddec256v
#define P4NDECS p4ddec
#define BITUNPACKD bitdunpack256v
#define _BITUNPACKD _bitdunpack256v
#define BITUNDD bitddec
#include "vp4d.c"
#define _P4DEC _p4d1dec256v
#define P4DEC p4d1dec256v
#define P4NDEC p4nd1dec256v
#define P4NDECS p4d1dec
#define BITUNPACKD bitd1unpack256v
#define _BITUNPACKD _bitd1unpack256v
#define BITUNDD bitd1dec
#include "vp4d.c"
#define _P4DEC _p4zdec256v
#define P4DEC p4zdec256v
#define P4NDEC p4nzdec256v
#define P4NDECS p4zdec
#define BITUNPACKD bitzunpack256v
#define _BITUNPACKD _bitzunpack256v
#define BITUNDD bitzdec
#define USIZE 32
#include "vp4d.c"
#undef BITUNDD
#endif
#else
#define uint_t TEMPLATE3(uint, USIZE, _t)
#pragma GCC push_options
#pragma GCC optimize ("align-functions=16")
ALWAYS_INLINE unsigned char *TEMPLATE2(_P4DEC, USIZE)(unsigned char *__restrict in, unsigned n, uint_t *__restrict out P4DELTA(uint_t start), unsigned b, unsigned bx ) {
if(!(b & 0x80)) {
#if USIZE == 64
b = (b == 63)?64:b;
#endif
return TEMPLATE2(BITUNPACKD, USIZE)(in, n, out P4DELTA(start), b);
}
b &= 0x7f;
#if VSIZE >= 128
#if USIZE == 64
if(b+bx <= 32)
#endif
{ unsigned char *pb = in;
#if VSIZE == 128
#if USIZE == 64
uint32_t ex[P4D_MAX+64];
in = TEMPLATE2(bitunpack, 32)(in+16, popcnt64(ctou64(in)) + popcnt64(ctou64(in+8)), ex, bx);
#else
uint_t ex[P4D_MAX+64];
in = TEMPLATE2(bitunpack, USIZE)(in+16, popcnt64(ctou64(in)) + popcnt64(ctou64(in+8)), ex, bx);
#endif
#else
uint_t ex[P4D_MAX+64];
in = TEMPLATE2(bitunpack, USIZE)(in+32, popcnt64(ctou64(in)) + popcnt64(ctou64(in+8)) + popcnt64(ctou64(in+16)) + popcnt64(ctou64(in+24)), ex, bx);
#endif
return TEMPLATE2(_BITUNPACKD, USIZE)(in, n, out P4DELTA(start), b, ex, pb);
}
#endif
#if VSIZE < 128 || USIZE == 64
{ uint_t ex[P4D_MAX+64];
unsigned long long bb[P4D_MAX/64];
unsigned num=0,i,p4dn = (n+63)/64;
for(i = 0; i < n/64; i++) { bb[i] = ctou64(in+i*8); num += popcnt64(bb[i]); }
if(n & 0x3f) { bb[i] = ctou64(in+i*8) & ((1ull<<(n&0x3f))-1); num += popcnt64(bb[i]); }
in = TEMPLATE2(bitunpack, USIZE)(in+PAD8(n), num, ex, bx);
in = TEMPLATE2(BITUNPACK, USIZE)(in, n, out, b);
#if 0 //defined(AVX_2__)
{ uint_t *op,*pex = ex;
for(i = 0; i < p4dn; i++) {
for(op = out; bb[i]; bb[i] >>= 8,op += 8) { unsigned m = (unsigned char)bb[i], mc=popcnt32(m), s = pex[mc]; pex[mc]=0;
_mm256_storeu_si256((__m256i *)op, _mm256_add_epi32(_mm256_loadu_si256((const __m256i*)op), mm256_maskz_expand_epi32(m,_mm256_slli_epi32(_mm256_load_si256((const __m256i*)pex), b)))); pex += mc; *pex=s;
} //out += 64;
}
}
#elif defined(__SSSE3__) && USIZE == 32
{ uint_t *_op=out,*op,*pex = ex;
for(i = 0; i < p4dn; i++) {
for(op=_op; bb[i]; bb[i] >>= 4,op+=4) { const unsigned m = bb[i]&0xf;
_mm_storeu_si128((__m128i *)op, _mm_add_epi32(_mm_loadu_si128((__m128i*)op), _mm_shuffle_epi8(_mm_slli_epi32(_mm_loadu_si128((__m128i*)pex), b), _mm_load_si128((__m128i*)_shuffle_32[m]) ) )); pex += popcnt32(m);
} _op+=64;
}
}
#elif defined(__SSSE3__) && USIZE == 16
{ uint_t *_op=out,*op,*pex = ex;
for(i = 0; i < p4dn; i++) {
for(op=_op; bb[i]; bb[i] >>= 8,op+=8) { const unsigned char m = bb[i];
_mm_storeu_si128((__m128i *)op, _mm_add_epi16(_mm_loadu_si128((__m128i*)op), _mm_shuffle_epi8(_mm_slli_epi16(_mm_loadu_si128((__m128i*)pex), b), _mm_load_si128((__m128i*)_shuffle_16[m]) ) )); pex += popcnt32(m);
} _op+=64;
}
}
#else
{ unsigned k = 0;
uint_t *op,*p;
for(op=out,i = 0; i < p4dn; i++,op += 64) { unsigned long long u = bb[i];
#if 1 // faster
while(u) op[ctz64(u)] += ex[k++]<<b, u &= u-1;
#else
p = op;
#define OP(_i_) p[_i_] += ex[k++]<<b;
while(u) {
unsigned x = ctz64(u); u >>= x; p+=x;
switch(u&0xf) {
case 0: break; //0000
case 1: OP(0); break; //0001
case 2: OP(1); break; //0010
case 3: OP(1); OP(0); break; //0011
case 4: OP(2); break; //0100
case 5: OP(2); OP(0); break; //0101
case 6: OP(2); OP(1); break; //0110
case 7: OP(2); OP(1); OP(0); break; //0111
case 8: OP(3); break; //1000
case 9: OP(3); OP(0); break; //1001
case 10: OP(3); OP(1); break; //1010
case 11: OP(3); OP(1); OP(0); break; //1011
case 12: OP(3); OP(2); break; //1100
case 13: OP(3); OP(2); OP(0); break; //1101
case 14: OP(3); OP(2); OP(1); break; //1110
case 15: OP(3); OP(2); OP(1); OP(0); break; //1111
}
u >>= 4;
p += 4;
}
#endif
}
}
#endif
}
#ifdef BITUNDD
TEMPLATE2(BITUNDD, USIZE)(out, n, start);
#endif
return in;
#endif
}
unsigned char *TEMPLATE2(P4DEC, USIZE)(unsigned char *__restrict in, unsigned n, uint_t *__restrict out P4DELTA(uint_t start) ) {
unsigned b, bx, i;
if(!n) return in;
b = *in++;
if((b & 0xc0) == 0xc0) {
b &= 0x3f;
#if USIZE == 64
b = b == 63?64:b;
#endif
uint_t u = TEMPLATE2(ctou, USIZE)(in); if(b < USIZE) u = TEMPLATE2(BZHI, USIZE)(u,b);
for(i=0; i < n; i++) out[i] = u;
#ifdef BITUNDD
TEMPLATE2(BITUNDD, USIZE)(out, n, start);
#endif
return in+(b+7)/8;
} else if(likely(!(b & 0x40))) {
if(b & 0x80)
bx = *in++;
return TEMPLATE2(_P4DEC, USIZE)(in, n, out P4DELTA(start), b, bx);
}
#if USIZE > 8
else {
uint_t ex[P4D_MAX+64],*pex=ex;
b &= 0x3f;
bx = *in++; /*if(b && *in++ == 0x80) { uint_t u = TEMPLATE2(ctou, USIZE)(in); if(b < USIZE) u = TEMPLATE2(BZHI, USIZE)(u,b); int i; for(i = 0; i < n; i++) out[i] = u; in+=(b+7)/8; } else*/
in = TEMPLATE2(BITUNPACK, USIZE)(in, n, out, b);
in = TEMPLATE2(vbdec, USIZE)(in, bx, ex);
#define ST(j) out[in[i+j]] |= ex[i+j] << b;
//#define ST(j) out[*ip++] |= (*pex++) << b;
//unsigned char *ip;for(ip = in; ip != in + (bx & ~3); )
for(i = 0; i != (bx & ~7); i+=8)
{ ST(0);ST(1);ST(2);ST(3); ST(4);ST(5);ST(6);ST(7); }
//for(;ip != in+bx; ) ST(0);
for(;i != bx; i++) ST(0);
in += bx;
#ifdef BITUNDD
TEMPLATE2(BITUNDD, USIZE)(out, n, start);
#endif
return in;
}
#endif
}
#ifdef VSIZE
#define CSIZE VSIZE
#else
#define CSIZE 128
#endif
size_t TEMPLATE2(P4NDEC, USIZE)(unsigned char *__restrict in, size_t n, uint_t *__restrict out) {
unsigned char *ip = in;
uint_t *op;
if(!n) return 0;
{
#ifdef DELTA
uint_t start;
TEMPLATE2(vbxget, USIZE)(ip, start);
*out++ = start;
--n;
#endif
for(op = out; op != out+(n&~(CSIZE-1)); op += CSIZE) {
unsigned b = *ip++, bx, i; __builtin_prefetch(ip+512);//ip = TEMPLATE2(P4DEC, USIZE)(ip, CSIZE, op P4DELTA(start));
if((b & 0xc0) == 0xc0) {
b &= 0x3f;
#if USIZE == 64
b = b == 63?64:b;
#endif
uint_t u = TEMPLATE2(ctou, USIZE)(ip); if(b < USIZE) u = TEMPLATE2(BZHI, USIZE)(u,b);
int i; for(i=0; i < CSIZE; i++) op[i] = u;
#ifdef BITUNDD
TEMPLATE2(BITUNDD, USIZE)(op, CSIZE, start);
#endif
ip += (b+7)/8;
} else if(likely(!(b & 0x40))) {
if(b & 0x80)
bx = *ip++;
ip = TEMPLATE2(_P4DEC, USIZE)(ip, CSIZE, op P4DELTA(start), b, bx);
}
#if USIZE > 8
else {
uint_t ex[P4D_MAX+64];
b &= 0x3f;
bx = *ip++; //f(*ip++ == 0x80 && b) { uint_t u = TEMPLATE2(ctou, USIZE)(ip); ip+=(b+7)/8; if(b < USIZE) u = TEMPLATE2(BZHI, USIZE)(u,b); int i; for(i=0; i < CSIZE; i++) op[i] = u; } else
ip = TEMPLATE2(BITUNPACK, USIZE)(ip, CSIZE, op, b);
ip = TEMPLATE2(vbdec, USIZE)(ip, bx, ex);
for(i = 0; i != (bx & ~7); i += 8) {
op[ip[i ]] |= ex[i ] << b;
op[ip[i+1]] |= ex[i+1] << b;
op[ip[i+2]] |= ex[i+2] << b;
op[ip[i+3]] |= ex[i+3] << b;
op[ip[i+4]] |= ex[i+4] << b;
op[ip[i+5]] |= ex[i+5] << b;
op[ip[i+6]] |= ex[i+6] << b;
op[ip[i+7]] |= ex[i+7] << b;
}
for(;i != bx; i++)
op[ip[i]] |= ex[i] << b;
ip += bx;
#ifdef BITUNDD
TEMPLATE2(BITUNDD, USIZE)(op, CSIZE, start);
#endif
}
#endif
P4DELTA_(start = op[CSIZE-1]);
}
return TEMPLATE2(P4NDECS, USIZE)(ip, n&(CSIZE-1), op P4DELTA(start)) - in;
}
}
#ifdef P4DECX
unsigned char *TEMPLATE2(p4decx, USIZE)(unsigned char *in, unsigned n, uint_t *__restrict out) {
unsigned b,i;
struct p4 p4;
p4ini(&p4, &in, n, &b);
if(unlikely(p4.isx)) {
#define ITX(k) out[i+k] = TEMPLATE2(p4getx, USIZE)(&p4, in, i+k, b);
for(i = 0; i != n&~3; i += 4) { ITX(0); ITX(1); ITX(2); ITX(3); }
for( ; i != n; i++ ) ITX(0);
} else {
#define ITY(k) out[i+k] = TEMPLATE2(bitgetx, USIZE)(in, i+k, b);
for(i = 0; i != n&~3; i += 4) { ITY(0); ITY(1); ITY(2); ITY(3); }
for( ; i != n; i++) ITY(0);
}
return in + PAD8(n*b);
}
unsigned char *TEMPLATE2(p4f1decx, USIZE)(unsigned char *in, unsigned n, uint_t *__restrict out, uint_t start) {
unsigned b,i;
struct p4 p4;
p4ini(&p4, &in, n, &b);
if(unlikely(p4.isx)) {
#define ITX(k) out[i+k] = TEMPLATE2(p4getx, USIZE)(&p4, in, (i+k), b)+start+i+k+1;
for(i = 0; i != n&~3; i+=4) { ITX(0); ITX(1); ITX(2); ITX(3); }
for( ; i != n; i++) ITX(0);
} else {
#define ITY(k) out[i+k] = TEMPLATE2(bitgetx, USIZE)(in, (i+k), b)+start+i+k+1;
for(i = 0; i != n&~3; i += 4) { ITY(0); ITY(1); ITY(2); ITY(3); }
for( ; i != n; i++) ITY(0);
}
return in + PAD8(n*b);
}
unsigned char *TEMPLATE2(p4fdecx, USIZE)(unsigned char *in, unsigned n, uint_t *__restrict out, uint_t start) {
unsigned b,i;
struct p4 p4;
p4ini(&p4, &in, n, &b);
if(unlikely(p4.isx)) {
#define ITX(k) out[i+k] = TEMPLATE2(p4getx, USIZE)(&p4, in, i+k, b)+start;
for(i = 0; i != n&~3; i+=4) { ITX(0); ITX(1); ITX(2); ITX(3); }
for( ; i != n; i++) ITX(0);
} else {
#define ITY(k) out[i+k] = TEMPLATE2(bitgetx, USIZE)(in, (i+k), b)+start;
for(i = 0; i != n&~3; i+=4) { ITY(0); ITY(1); ITY(2); ITY(3); }
for( ; i != n; i++) ITY(0);
}
return in + PAD8(n*b);
}
#endif
#endif