Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / third_party / om-file-format-sys / c / src / vp4d.c

vp4d.c550 lines · 17.2 KiBRaw
1/**
2 Copyright (C) powturbo 2013-2019
3 GPL v2 License
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 - homepage : https://sites.google.com/site/powturbo/
20 - github : https://github.com/powturbo
21 - twitter : https://twitter.com/powturbo
22 - email : powturbo [_AT_] gmail [_DOT_] com
23**/
24// "Integer Compression" TurboPFor - Pfor/PforDelta
25
26
27#ifdef GUARDED
28
29#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
30#pragma clang diagnostic ignored "-Wunused-variable"
31#pragma clang diagnostic ignored "-Wunknown-pragmas"
32#pragma clang diagnostic ignored "-Wmacro-redefined"
33#pragma clang diagnostic ignored "-Wconstant-conversion"
34#pragma clang diagnostic ignored "-Wshorten-64-to-32"
35
36#ifndef USIZE
37#pragma warning( disable : 4005)
38#pragma warning( disable : 4090)
39#pragma warning( disable : 4068)
40
41#define VINT_IN
42//#define BITPACK_DAC
43//#define TURBOPFOR_DAC
44#define BITUTIL_IN
45#include "conf.h"
46#include "bitutil.h"
47#include "vint.h"
48#include "bitpack.h"
49#include "vp4.h"
50
51#define PAD8(__x) ( (((__x)+8-1)/8) )
52
53#define P4DELTA(a)
54#define P4DELTA_(a)
55 #if defined(__SSSE3__) || defined(__ARM_NEON)
56extern char _shuffle_32[16][16]; // defined in bitunpack.c
57extern char _shuffle_16[256][16];
58 #endif
59
60 #ifdef AVX2_ON
61#define VSIZE 256
62#define P4DELTA(a)
63#define P4DELTA_(a)
64
65#define _P4DEC _p4dec256v
66#define P4DEC p4dec256v
67#define P4NDEC p4ndec256v
68#define P4NDECS p4dec
69#define BITUNPACK bitunpack256v
70#define BITUNPACKD bitunpack256v
71#define _BITUNPACKD _bitunpack256v
72#define USIZE 32
73#include "vp4d.c"
74
75#define P4DELTA(a) ,a
76#define P4DELTA_(a) a
77#define DELTA
78
79#define _P4DEC _p4ddec256v
80#define P4DEC p4ddec256v
81#define P4NDEC p4nddec256v
82#define P4NDECS p4ddec
83#define BITUNPACKD bitdunpack256v
84#define _BITUNPACKD _bitdunpack256v
85#define BITUNDD bitddec
86#include "vp4d.c"
87
88#define _P4DEC _p4d1dec256v
89#define P4DEC p4d1dec256v
90#define P4NDEC p4nd1dec256v
91#define P4NDECS p4d1dec
92#define BITUNPACKD bitd1unpack256v
93#define _BITUNPACKD _bitd1unpack256v
94#define BITUNDD bitd1dec
95#include "vp4d.c"
96
97#define _P4DEC _p4zdec256v
98#define P4DEC p4zdec256v
99#define P4NDEC p4nzdec256v
100#define P4NDECS p4zdec
101#define BITUNPACKD bitzunpack256v
102#define _BITUNPACKD _bitzunpack256v
103#define BITUNDD bitzdec
104#define USIZE 32
105#include "vp4d.c"
106
107#undef BITUNDD
108 #elif !defined(SSE2_ON) && !defined(AVX2_ON)
109
110#define _P4DEC _p4dec
111#define P4DEC p4dec
112#define P4NDEC p4ndec
113#define P4NDECS p4dec
114#define BITUNPACK bitunpack // unpack only
115#define BITUNPACKD bitunpack // integrated unpack
116#define _BITUNPACKD bitunpack // integrated pfor
117
118#ifdef TURBOPFOR_DAC
119 #define P4DECX // direct access no 64 bits
120#endif
121#define USIZE 8
122#include "vp4d.c"
123#define USIZE 16
124#include "vp4d.c"
125#define USIZE 32
126#include "vp4d.c"
127
128#ifdef TURBOPFOR_DAC
129 #undef P4DECX
130#endif
131#define USIZE 64
132#include "vp4d.c"
133
134#define P4DELTA(a) ,a
135#define P4DELTA_(a) a
136#define DELTA
137
138#define _P4DEC _p4ddec //delta0
139#define P4DEC p4ddec
140#define P4NDEC p4nddec
141#define P4NDECS p4ddec
142#define BITUNPACKD bitdunpack
143#define _BITUNPACKD _bitdunpack
144#define BITUNDD bitddec
145#define USIZE 8
146#include "vp4d.c"
147#define USIZE 16
148#include "vp4d.c"
149#define USIZE 32
150#include "vp4d.c"
151#define USIZE 64
152#include "vp4d.c"
153
154#define _P4DEC _p4d1dec //delta1
155#define P4DEC p4d1dec
156#define P4NDEC p4nd1dec
157#define P4NDECS p4d1dec
158#define BITUNPACKD bitd1unpack
159#define _BITUNPACKD bitd1unpack
160#define BITUNDD bitd1dec
161#define USIZE 8
162#include "vp4d.c"
163#define USIZE 16
164#include "vp4d.c"
165#define USIZE 32
166#include "vp4d.c"
167#define USIZE 64
168#include "vp4d.c"
169
170#define _P4DEC _p4zdec //zigzag0
171#define P4DEC p4zdec
172#define P4NDEC p4nzdec
173#define P4NDECS p4zdec
174#define BITUNPACKD bitzunpack
175#define _BITUNPACKD _bitzunpack
176#define BITUNDD bitzdec
177#define USIZE 8
178#include "vp4d.c"
179#define USIZE 16
180#include "vp4d.c"
181#define USIZE 32
182#include "vp4d.c"
183#define USIZE 64
184#include "vp4d.c"
185
186#undef _P4DEC
187#undef P4DEC
188#undef BITUNPACK
189#undef BITUNDD
190#undef P4DELTA
191#undef USIZE
192#undef DELTA
193
194 #elif defined(__SSSE3__) || defined(__ARM_NEON)
195#define VSIZE 128
196#define P4DELTA(a)
197#define P4DELTA_(a)
198
199#define _P4DEC _p4dec128v
200#define P4DEC p4dec128v
201#define P4NDEC p4ndec128v
202#define P4NDECS p4dec
203#define BITUNPACK bitunpack128v
204#define BITUNPACKD bitunpack128v
205#define _BITUNPACKD _bitunpack128v
206#define USIZE 16
207#include "vp4d.c"
208#define USIZE 32
209#include "vp4d.c"
210#define USIZE 64
211#include "vp4d.c"
212
213#define P4DELTA(a) ,a
214#define P4DELTA_(a) a
215#define DELTA
216
217#define _P4DEC _p4ddec128v
218#define P4DEC p4ddec128v
219#define P4NDEC p4nddec128v
220#define P4NDECS p4ddec
221#define BITUNPACKD bitdunpack128v
222#define _BITUNPACKD _bitdunpack128v
223#define BITUNDD bitddec
224#define USIZE 16
225#include "vp4d.c"
226#define USIZE 32
227#include "vp4d.c"
228
229#define _P4DEC _p4d1dec128v
230#define P4DEC p4d1dec128v
231#define P4NDEC p4nd1dec128v
232#define P4NDECS p4d1dec
233#define BITUNPACKD bitd1unpack128v
234#define _BITUNPACKD _bitd1unpack128v
235#define BITUNDD bitd1dec
236#define USIZE 16
237#include "vp4d.c"
238#define USIZE 32
239#include "vp4d.c"
240
241#define _P4DEC _p4zdec128v
242#define P4DEC p4zdec128v
243#define P4NDEC p4nzdec128v
244#define P4NDECS p4zdec
245#define BITUNPACKD bitzunpack128v
246#define _BITUNPACKD _bitzunpack128v
247#define BITUNDD bitzdec
248#define USIZE 16
249#include "vp4d.c"
250#define USIZE 32
251#include "vp4d.c"
252
253#undef BITUNDD
254#undef P4DELTA
255#undef DELTA
256
257#define VSIZE 256
258
259#define P4DELTA(a)
260#define P4DELTA_(a)
261#undef DELTA
262
263#define _P4DEC _p4dec256w
264#define P4DEC p4dec256w
265#define P4NDEC p4ndec256w
266#define P4NDECS p4dec
267#define BITUNPACK bitunpack256w
268#define BITUNPACKD bitunpack256w
269#define _BITUNPACKD _bitunpack256w
270#include "vp4d.c"
271 #endif
272#undef DELTA
273
274#else
275#define uint_t TEMPLATE3(uint, USIZE, _t)
276
277#pragma GCC push_options
278#pragma GCC optimize ("align-functions=16")
279
280ALWAYS_INLINE unsigned char *TEMPLATE2(_P4DEC, USIZE)(unsigned char *__restrict in, unsigned n, uint_t *__restrict out P4DELTA(uint_t start), unsigned b, unsigned bx ) {
281 if(!(b & 0x80)) {
282 #if USIZE == 64
283 b = (b == 63)?64:b; // 63,64 are both encoded w. same bitsize 64 (permits using only 6 bits for b)
284 #endif
285 return TEMPLATE2(BITUNPACKD, USIZE)(in, n, out P4DELTA(start), b); // bitunpack only
286 }
287 b &= 0x7f;
288 #if VSIZE >= 128 // vertical SIMD bitpacking
289 #if USIZE == 64
290 if(b+bx <= 32) // use SIMD when bitsize <= 32 (scalar horizontal bitunpack64 is used when 32 < bitsize <=64 )
291 #endif
292 { unsigned char *pb = in;
293 #if VSIZE == 128
294 #if USIZE == 64
295 uint32_t ex[P4D_MAX+64];
296 in = TEMPLATE2(bitunpack, 32)(in+16, popcnt64(ctou64(in)) + popcnt64(ctou64(in+8)), ex, bx); // unpack the fixex size part
297 #else
298 uint_t ex[P4D_MAX+64];
299 in = TEMPLATE2(bitunpack, USIZE)(in+16, popcnt64(ctou64(in)) + popcnt64(ctou64(in+8)), ex, bx);
300 #endif
301 #else
302 uint_t ex[P4D_MAX+64];
303 in = TEMPLATE2(bitunpack, USIZE)(in+32, popcnt64(ctou64(in)) + popcnt64(ctou64(in+8)) + popcnt64(ctou64(in+16)) + popcnt64(ctou64(in+24)), ex, bx);
304 #endif
305 return TEMPLATE2(_BITUNPACKD, USIZE)(in, n, out P4DELTA(start), b, ex, pb); // unpack and merge the exceptions
306 }
307 #endif
308 #if VSIZE < 128 || USIZE == 64 // bitunpack the rest number of elements < 128/256 or when 32 < bitsize <=64 is used
309 { uint_t ex[P4D_MAX+64];
310 unsigned long long bb[P4D_MAX/64];
311 unsigned num=0,i,p4dn = (n+63)/64;
312 for(i = 0; i < n/64; i++) { bb[i] = ctou64(in+i*8); num += popcnt64(bb[i]); }
313 if(n & 0x3f) { bb[i] = ctou64(in+i*8) & ((1ull<<(n&0x3f))-1); num += popcnt64(bb[i]); }
314 in = TEMPLATE2(bitunpack, USIZE)(in+PAD8(n), num, ex, bx);
315 in = TEMPLATE2(BITUNPACK, USIZE)(in, n, out, b);
316 #if 0 //defined(__AVX2__)
317 { uint_t *op,*pex = ex;
318 for(i = 0; i < p4dn; i++) {
319 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;
320 _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;
321 } //out += 64;
322 }
323 }
324 #elif (defined(__SSSE3__) || defined(__ARM_NEON)) && USIZE == 32
325 { uint_t *_op = out,*op,*pex = ex;
326 for(i = 0; i < p4dn; i++) {
327 for(op=_op; bb[i]; bb[i] >>= 4,op+=4) { const unsigned m = bb[i]&0xf;
328 _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);
329 } _op+=64;
330 }
331 }
332 #elif (defined(__SSSE3__) || defined(__ARM_NEON)) && USIZE == 16
333 { uint_t *_op = out, *op, *pex = ex;
334 for(i = 0; i < p4dn; i++) {
335 for(op = _op; bb[i]; bb[i] >>= 8,op += 8) { const unsigned char m = bb[i];
336 _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);
337 } _op += 64;
338 }
339 }
340 #else
341 { unsigned k = 0;
342 uint_t *op,*p;
343 for(op=out,i = 0; i < p4dn; i++,op += 64) { unsigned long long u = bb[i];
344 #if 1 // faster
345 while(u) op[ctz64(u)] += ex[k++]<<b, u &= u-1;
346 #else
347 p = op;
348 #define OP(_i_) p[_i_] += ex[k++]<<b;
349 while(u) {
350 unsigned x = ctz64(u); u >>= x; p+=x;
351 switch(u&0xf) {
352 case 0: break; //0000
353 case 1: OP(0); break; //0001
354 case 2: OP(1); break; //0010
355 case 3: OP(1); OP(0); break; //0011
356 case 4: OP(2); break; //0100
357 case 5: OP(2); OP(0); break; //0101
358 case 6: OP(2); OP(1); break; //0110
359 case 7: OP(2); OP(1); OP(0); break; //0111
360 case 8: OP(3); break; //1000
361 case 9: OP(3); OP(0); break; //1001
362 case 10: OP(3); OP(1); break; //1010
363 case 11: OP(3); OP(1); OP(0); break; //1011
364 case 12: OP(3); OP(2); break; //1100
365 case 13: OP(3); OP(2); OP(0); break; //1101
366 case 14: OP(3); OP(2); OP(1); break; //1110
367 case 15: OP(3); OP(2); OP(1); OP(0); break; //1111
368 }
369 u >>= 4;
370 p += 4;
371 }
372 #endif
373 }
374 }
375 #endif
376 }
377 #ifdef BITUNDD
378 TEMPLATE2(BITUNDD, USIZE)(out, n, start);
379 #endif
380 return in;
381 #endif
382}
383
384unsigned char *TEMPLATE2(P4DEC, USIZE)(unsigned char *__restrict in, unsigned n, uint_t *__restrict out P4DELTA(uint_t start) ) {
385 unsigned b, bx = 0, i;
386 if(!n) return in;
387 b = *in++;
388 if((b & 0xc0) == 0xc0) { // all items are equal
389 b &= 0x3f;
390 #if USIZE == 64
391 b = b == 63?64:b; // use 64 instead of 63 (permis using only 6 bits to store b)
392 #endif
393 uint_t u = TEMPLATE2(ctou, USIZE)(in); if(b < USIZE) u = TEMPLATE2(BZHI, USIZE)(u,b);
394 for(i=0; i < n; i++) out[i] = u;
395 #ifdef BITUNDD
396 TEMPLATE2(BITUNDD, USIZE)(out, n, start);
397 #endif
398 return in+(b+7)/8;
399 } else if(likely(!(b & 0x40))) { // PFOR
400 if(b & 0x80)
401 bx = *in++;
402 return TEMPLATE2(_P4DEC, USIZE)(in, n, out P4DELTA(start), b, bx);
403 }
404 else { // Variable byte
405#if USIZE > 8
406 uint_t ex[P4D_MAX+64],*pex=ex;
407 b &= 0x3f;
408 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*/
409 in = TEMPLATE2(BITUNPACK, USIZE)(in, n, out, b);
410 in = TEMPLATE2(vbdec, USIZE)(in, bx, ex);
411 #define ST(j) out[in[i+j]] |= ex[i+j] << b
412 //#define ST(j) out[*ip++] |= (*pex++) << b;
413 //unsigned char *ip;for(ip = in; ip != in + (bx & ~3); )
414 for(i = 0; i != (bx & ~7); i+=8)
415 { ST(0);ST(1);ST(2);ST(3); ST(4);ST(5);ST(6);ST(7); }
416 //for(;ip != in+bx; ) ST(0);
417 for(;i != bx; i++) ST(0);
418 in += bx;
419 #ifdef BITUNDD
420 TEMPLATE2(BITUNDD, USIZE)(out, n, start);
421 #endif
422 #undef ST
423#endif // USIZE > 8
424 return in;
425 }
426}
427
428#ifdef VSIZE
429 #define CSIZE VSIZE
430#else
431 #define CSIZE 128
432#endif
433
434size_t TEMPLATE2(P4NDEC, USIZE)(unsigned char *__restrict in, size_t n, uint_t *__restrict out) {
435 unsigned char *ip = in;
436 uint_t *op;
437 if(!n) return 0;
438 {
439 #ifdef DELTA
440 uint_t start;
441 TEMPLATE2(vbxget, USIZE)(ip, start);
442 *out++ = start;
443 --n;
444 #endif
445 for(op = out; op != out+(n&~(CSIZE-1)); op += CSIZE) {
446 unsigned b = *ip++, bx = 0, i; PREFETCH(ip+512,0);//ip = TEMPLATE2(P4DEC, USIZE)(ip, CSIZE, op P4DELTA(start));
447
448 if((b & 0xc0) == 0xc0) {
449 b &= 0x3f;
450 #if USIZE == 64
451 b = b == 63?64:b;
452 #endif
453 uint_t u = TEMPLATE2(ctou, USIZE)(ip); if(b < USIZE) u = TEMPLATE2(BZHI, USIZE)(u,b);
454 int i; for(i=0; i < CSIZE; i++) op[i] = u;
455 #ifdef BITUNDD
456 TEMPLATE2(BITUNDD, USIZE)(op, CSIZE, start);
457 #endif
458 ip += (b+7)/8;
459 } else if(likely(!(b & 0x40))) {
460 if(b & 0x80)
461 bx = *ip++;
462 ip = TEMPLATE2(_P4DEC, USIZE)(ip, CSIZE, op P4DELTA(start), b, bx);
463 }
464 #if USIZE > 8
465 else {
466 uint_t ex[P4D_MAX+64];
467 b &= 0x3f;
468 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
469 ip = TEMPLATE2(BITUNPACK, USIZE)(ip, CSIZE, op, b);
470 ip = TEMPLATE2(vbdec, USIZE)(ip, bx, ex);
471 for(i = 0; i != (bx & ~7); i += 8) {
472 op[ip[i ]] |= ex[i ] << b;
473 op[ip[i+1]] |= ex[i+1] << b;
474 op[ip[i+2]] |= ex[i+2] << b;
475 op[ip[i+3]] |= ex[i+3] << b;
476 op[ip[i+4]] |= ex[i+4] << b;
477 op[ip[i+5]] |= ex[i+5] << b;
478 op[ip[i+6]] |= ex[i+6] << b;
479 op[ip[i+7]] |= ex[i+7] << b;
480 }
481 for(;i != bx; i++)
482 op[ip[i]] |= ex[i] << b;
483 ip += bx;
484 #ifdef BITUNDD
485 TEMPLATE2(BITUNDD, USIZE)(op, CSIZE, start);
486 #endif
487 }
488 #endif
489 P4DELTA_(start = op[CSIZE-1]);
490 }
491 return TEMPLATE2(P4NDECS, USIZE)(ip, n&(CSIZE-1), op P4DELTA(start)) - in;
492 }
493}
494
495 #ifdef P4DECX
496unsigned char *TEMPLATE2(p4decx, USIZE)(unsigned char *in, unsigned n, uint_t *__restrict out) {
497 unsigned b,i;
498 struct p4 p4;
499
500 p4ini(&p4, &in, n, &b);
501
502 if(unlikely(p4.isx)) {
503 #define ITX(k) out[i+k] = TEMPLATE2(p4getx, USIZE)(&p4, in, i+k, b);
504 for(i = 0; i != (n&(~3)); i += 4) { ITX(0); ITX(1); ITX(2); ITX(3); }
505 for( ; i != n; i++ ) ITX(0);
506 } else {
507 #define ITY(k) out[i+k] = TEMPLATE2(bitgetx, USIZE)(in, i+k, b);
508 for(i = 0; i != (n&(~3)); i += 4) { ITY(0); ITY(1); ITY(2); ITY(3); }
509 for( ; i != n; i++) ITY(0);
510 }
511 return in + PAD8(n*b);
512}
513
514unsigned char *TEMPLATE2(p4f1decx, USIZE)(unsigned char *in, unsigned n, uint_t *__restrict out, uint_t start) {
515 unsigned b,i;
516 struct p4 p4;
517
518 p4ini(&p4, &in, n, &b);
519 if(unlikely(p4.isx)) {
520 #define ITX(k) out[i+k] = TEMPLATE2(p4getx, USIZE)(&p4, in, (i+k), b)+start+i+k+1;
521 for(i = 0; i != (n&(~3)); i+=4) { ITX(0); ITX(1); ITX(2); ITX(3); }
522 for( ; i != n; i++) ITX(0);
523 } else {
524 #define ITY(k) out[i+k] = TEMPLATE2(bitgetx, USIZE)(in, (i+k), b)+start+i+k+1;
525 for(i = 0; i != (n&(~3)); i += 4) { ITY(0); ITY(1); ITY(2); ITY(3); }
526 for( ; i != n; i++) ITY(0);
527 }
528 return in + PAD8(n*b);
529}
530
531unsigned char *TEMPLATE2(p4fdecx, USIZE)(unsigned char *in, unsigned n, uint_t *__restrict out, uint_t start) {
532 unsigned b,i;
533 struct p4 p4;
534 p4ini(&p4, &in, n, &b);
535
536 if(unlikely(p4.isx)) {
537 #define ITX(k) out[i+k] = TEMPLATE2(p4getx, USIZE)(&p4, in, i+k, b)+start;
538 for(i = 0; i != (n&(~3)); i+=4) { ITX(0); ITX(1); ITX(2); ITX(3); }
539 for( ; i != n; i++) ITX(0);
540 } else {
541 #define ITY(k) out[i+k] = TEMPLATE2(bitgetx, USIZE)(in, (i+k), b)+start;
542 for(i = 0; i != (n&(~3)); i+=4) { ITY(0); ITY(1); ITY(2); ITY(3); }
543 for( ; i != n; i++) ITY(0);
544 }
545 return in + PAD8(n*b);
546}
547 #endif
548#endif
549
550#endif