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

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

vp4c.c437 lines · 14.1 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// "TurboPFor: Integer Compression" Turbo PFor/PforDelta
25
26#ifdef GUARDED
27
28#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
29#pragma clang diagnostic ignored "-Wunused-variable"
30#pragma clang diagnostic ignored "-Wunknown-pragmas"
31#pragma clang diagnostic ignored "-Wmacro-redefined"
32#pragma clang diagnostic ignored "-Wconstant-conversion"
33#pragma clang diagnostic ignored "-Wshorten-64-to-32"
34#pragma clang diagnostic ignored "-Wsign-compare"
35
36
37#ifndef USIZE //--------------------------------- Functions ----------------------------------------------------------------------
38#pragma warning( disable : 4005)
39#pragma warning( disable : 4090)
40#pragma warning( disable : 4068)
41
42#define VINT_IN
43#define BITUTIL_IN
44#include "conf.h"
45#include "bitpack.h"
46#include "vint.h"
47#include "bitutil.h"
48#include "vp4.h"
49
50#undef P4DELTA
51#define PAD8(_x_) ( (((_x_)+8-1)/8) )
52
53#define HYBRID 1 // Hybrid TurboPFor : 0=fixed bit packing, 1=fixed BP+Variable byte
54
55 #if !defined(SSE2_ON) && !defined(AVX2_ON)
56#define _P4BITS _p4bits
57#define P4BITS _p4bits
58#define _P4ENC _p4enc
59#define P4ENC p4enc
60#define P4NENC p4nenc
61#define BITPACK bitpack
62#define BITDELTA bitdienc
63#define USIZE 8
64#include "vp4c.c"
65#define USIZE 16
66#include "vp4c.c"
67#define USIZE 32
68#include "vp4c.c"
69#define USIZE 64
70#include "vp4c.c"
71
72#define P4DELTA 0 // p4d functions
73#define P4DENC p4denc
74#define P4NENC p4ndenc
75#define P4NENCS p4denc
76#define USIZE 8
77#include "vp4c.c"
78#define USIZE 16
79#include "vp4c.c"
80#define USIZE 32
81#include "vp4c.c"
82#define USIZE 64
83#include "vp4c.c"
84
85#define P4DELTA 1 // p4d1 functions
86#define P4DENC p4d1enc
87#define P4NENC p4nd1enc
88#define P4NENCS p4d1enc
89#define USIZE 8
90#include "vp4c.c"
91#define USIZE 16
92#include "vp4c.c"
93#define USIZE 32
94#include "vp4c.c"
95#define USIZE 64
96#include "vp4c.c"
97
98#define BITDELTA bitzenc // // p4z functions
99#define P4DENC p4zenc
100#define P4NENC p4nzenc
101#define P4NENCS p4zenc
102#define USIZE 8
103#include "vp4c.c"
104#define USIZE 16
105#include "vp4c.c"
106#define USIZE 32
107#include "vp4c.c"
108#define USIZE 64
109#include "vp4c.c"
110
111#undef P4DELTA
112#define BITDELTA bitdienc
113
114/*#define HYBRID 0 // Direct access
115#define P4BITS _p4bitsx
116#define _P4BITS _p4bitsx
117#define _P4ENC _p4encx
118#define P4ENC p4encx
119#define P4NENC p4nencx
120#define USIZE 8
121#include "vp4c.c"
122#define USIZE 16
123#include "vp4c.c"
124#define USIZE 32
125#include "vp4c.c"
126#define USIZE 64
127#include "vp4c.c"*/
128
129
130#undef _P4ENC
131#undef P4ENC
132#undef BITPACK
133
134#define P4NDENC(in, n, out, _csize_, _usize_, _p4c_) { if(!n) return 0;\
135 unsigned char *op = out; \
136 start = *in++;\
137 TEMPLATE2(vbxput, _usize_)(op, start);\
138 for(n--,ip = in; ip != in + (n&~(_csize_-1)); ) { PREFETCH(ip+512,0);\
139 op = TEMPLATE2(_p4c_, _usize_)(ip, _csize_, op, start); ip += _csize_; start = ip[-1];\
140 } if(n&=(_csize_-1)) { op = TEMPLATE2(_p4c_, _usize_)(ip, n, op, start); }\
141 return op - out;\
142}
143
144#define P4NDDEC(in, n, out, _csize_, _usize_, _p4d_) { if(!n) return 0;\
145 unsigned char *ip = in;\
146 TEMPLATE2(vbxget, _usize_)(ip, start);\
147 for(*out++ = start,--n,op = out; op != out+(n&~(_csize_-1)); ) { PREFETCH(ip+512,0);\
148 ip = TEMPLATE2(_p4d_, _usize_)(ip, _csize_, op, start); op += _csize_; start = op[-1];\
149 } if(n&=(_csize_-1)) { ip = TEMPLATE2(_p4d_, _usize_)(ip, n, op, start); }\
150 return ip - in;\
151}
152
153unsigned char *p4senc16(uint16_t *in, unsigned n, unsigned char *out, uint16_t x) { uint16_t pa[128+32],eq, mdelta = bitdi16(in, n, 0, x); vbput16(out, mdelta); bitdienc16(in, n, pa, x, mdelta); return p4enc16(pa, n, out);}
154unsigned char *p4senc32(uint32_t *in, unsigned n, unsigned char *out, uint32_t x) { uint32_t pa[128+32],eq, mdelta = bitdi32(in, n, 0, x); vbput32(out, mdelta); bitdienc32(in, n, pa, x, mdelta); return p4enc32(pa, n, out);}
155unsigned char *p4senc64(uint64_t *in, unsigned n, unsigned char *out, uint64_t x) { uint64_t pa[128+64],eq, mdelta = bitdi64(in, n, 0, x); vbput64(out, mdelta); bitdienc64(in, n, pa, x, mdelta); return p4enc64(pa, n, out);}
156
157unsigned char *p4sdec16(unsigned char *in, unsigned n, uint16_t *out, uint16_t x) { uint16_t mdelta; vbget16(in, mdelta); in = p4dec16(in, n, out); bitdidec16(out, n, x, mdelta); return in; }
158unsigned char *p4sdec32(unsigned char *in, unsigned n, uint32_t *out, uint32_t x) { uint32_t mdelta; vbget32(in, mdelta); in = p4dec32(in, n, out); bitdidec32(out, n, x, mdelta); return in; }
159unsigned char *p4sdec64(unsigned char *in, unsigned n, uint64_t *out, uint64_t x) { uint64_t mdelta; vbget64(in, mdelta); in = p4dec64(in, n, out); bitdidec64(out, n, x, mdelta); return in; }
160
161size_t p4nsenc16(uint16_t *in, size_t n, unsigned char *out) { uint16_t *ip,start; P4NDENC(in, n, out, 128, 16, p4senc); }
162size_t p4nsenc32(uint32_t *in, size_t n, unsigned char *out) { uint32_t *ip,start; P4NDENC(in, n, out, 128, 32, p4senc); }
163size_t p4nsenc64(uint64_t *in, size_t n, unsigned char *out) { uint64_t *ip,start; P4NDENC(in, n, out, 128, 64, p4senc); }
164
165size_t p4nsdec16(unsigned char *in, size_t n, uint16_t *out) { uint16_t *op,start; P4NDDEC(in, n, out, 128, 16, p4sdec); }
166size_t p4nsdec32(unsigned char *in, size_t n, uint32_t *out) { uint32_t *op,start; P4NDDEC(in, n, out, 128, 32, p4sdec); }
167size_t p4nsdec64(unsigned char *in, size_t n, uint64_t *out) { uint64_t *op,start; P4NDDEC(in, n, out, 128, 64, p4sdec); }
168#undef _P4BITS
169 #elif defined(AVX2_ON)
170#define BITDELTA bitdienc
171#define HYBRID 1
172#define P4BITS _p4bits
173#define VSIZE 256
174
175#define _P4ENC _p4enc256v
176#define P4ENC p4enc256v
177#define P4NENC p4nenc256v
178#define P4NENCS p4enc
179#define BITPACK bitpack256v
180#define USIZE 32
181#include "vp4c.c"
182
183#define P4DELTA 0
184#define P4DENC p4denc256v
185#define P4NENC p4ndenc256v
186#define P4NENCS p4denc
187#include "vp4c.c"
188
189#define P4DELTA 1
190#define P4DENC p4d1enc256v
191#define P4NENC p4nd1enc256v
192#define P4NENCS p4d1enc
193#include "vp4c.c"
194#undef P4DELTA
195
196#define P4DELTA 0
197#define BITDELTA bitzenc
198#define P4DENC p4zenc256v
199#define P4NENC p4nzenc256v
200#define P4NENCS p4zenc
201#include "vp4c.c"
202
203#undef _P4ENC
204#undef P4ENC
205#undef BITPACK
206 #elif defined(__SSE2__) || defined(__ARM_NEON) //--------------------------------------------------
207#define BITDELTA bitdienc
208#define HYBRID 1
209#define P4BITS _p4bits
210#define USIZE 32
211
212#define VSIZE 128
213#define _P4ENC _p4enc128v
214#define P4ENC p4enc128v
215#define P4NENCS p4enc
216#define P4NENC p4nenc128v
217#define BITPACK bitpack128v
218#define USIZE 16
219#include "vp4c.c"
220#define USIZE 32
221#include "vp4c.c"
222#define USIZE 64
223#include "vp4c.c"
224
225#define P4DELTA 0
226#define P4DENC p4denc128v
227#define P4NENC p4ndenc128v
228#define P4NENCS p4denc
229#define USIZE 16
230#include "vp4c.c"
231#define USIZE 32
232#include "vp4c.c"
233
234#define P4DELTA 1
235#define P4DENC p4d1enc128v
236#define P4NENC p4nd1enc128v
237#define P4NENCS p4d1enc
238#define USIZE 16
239#include "vp4c.c"
240#define USIZE 32
241#include "vp4c.c"
242
243#define P4DELTA 0
244#define BITDELTA bitzenc
245#define P4DENC p4zenc128v
246#define P4NENC p4nzenc128v
247#define P4NENCS p4zenc
248#define USIZE 16
249#include "vp4c.c"
250#define USIZE 32
251#include "vp4c.c"
252
253/*#define BITDELTA bitdienc
254#define VSIZE 256
255#define _P4ENC _p4enc256w
256#define P4ENC p4enc256w
257#define P4NENCS p4encw
258#define P4NENC p4nenc256w
259#define BITPACK bitpack256w
260#include "vp4c.c"*/
261 #endif
262
263#undef P4DELTA
264#undef _P4ENC
265#undef P4ENC
266#undef BITPACK
267
268#else //------------------------------------------ Templates ---------------------------------------------------------------
269#pragma clang diagnostic push
270#pragma clang diagnostic ignored "-Wparentheses"
271
272#pragma GCC push_options
273#pragma GCC optimize ("align-functions=16")
274
275#define uint_t TEMPLATE3(uint, USIZE, _t)
276
277 #ifdef VSIZE
278#define CSIZE VSIZE
279 #else
280#define CSIZE 128
281 #endif
282
283 #ifndef P4DELTA
284
285 #ifdef _P4BITS
286unsigned TEMPLATE2(_P4BITS, USIZE)(uint_t *__restrict in, unsigned n, unsigned *pbx) {
287 #if HYBRID > 0 && USIZE >= 16
288 // OM EDIT: added `+16` on both sides to prevent a invalid stack access
289 unsigned _vb[USIZE*2+64+16] = {0}, *vb=&_vb[USIZE+16];
290 #endif
291 unsigned cnt[USIZE+8] = {0}, x, bx, bmp8=(n+7)/8;
292 uint_t *ip, u=0, a = in[0];
293 int b,i,ml,l,fx=0,vv,eq=0;
294
295 #define CNTE(i) { ++cnt[TEMPLATE2(bsr, USIZE)(ip[i])], u |= ip[i]; eq += (ip[i] == a); }
296 for(ip = in; ip != in+(n&~3); ip+=4) { CNTE(0); CNTE(1); CNTE(2); CNTE(3); }
297 for(;ip != in+n;ip++) CNTE(0);
298
299 b = TEMPLATE2(bsr, USIZE)(u);
300 #if HYBRID > 0
301 if(eq == n && a) { *pbx = USIZE+2;
302 #if USIZE == 64
303 if(b == USIZE-1) b = USIZE;
304 #endif
305 return b;
306 }
307 #endif
308 bx = b;
309 ml = PAD8(n*b)+1; x = cnt[b];
310
311 #if HYBRID > 0 && USIZE >= 16
312 #define VBB(_x_,_b_) vb[_b_-7]+=_x_; vb[_b_-15]+=_x_*2; vb[_b_-19]+=_x_*3; vb[_b_-25]+=_x_*4;
313 vv = x; VBB(x,b);
314 #else
315 ml -= 2+bmp8;
316 #endif
317 for(i = b-1; i >= 0; --i) {
318 int fi,v;
319 #if HYBRID > 0 && USIZE >= 16
320 v = PAD8(n*i) + 2 + x + vv;
321 l = PAD8(n*i) + 2+bmp8 + PAD8(x*(bx-i));
322 x += cnt[i];
323 vv += cnt[i]+vb[i];
324 VBB(cnt[i],i);
325 fi = l < ml; ml = fi?l:ml; b = fi?i:b; fx=fi?0:fx;
326 fi = v < ml; ml = fi?v:ml; b = fi?i:b; fx=fi?1:fx;
327 #else
328 l = PAD8(n*i) + PAD8(x*(bx-i));
329 x += cnt[i];
330 fi = l < ml;
331 ml = fi?l:ml; b = fi?i:b;
332 #endif
333 } //fx = 0;
334 #if HYBRID > 0 && USIZE >= 16
335 *pbx = fx?(USIZE+1):(bx - b);
336 #if USIZE == 64
337 if(b == USIZE-1) { b = USIZE; *pbx = 0; }
338 #endif
339 #else
340 *pbx = bx - b;
341 #endif
342 return b;
343}
344 #endif
345
346
347unsigned char *TEMPLATE2(_P4ENC, USIZE)(uint_t *__restrict in, unsigned n, unsigned char *__restrict out, unsigned b, unsigned bx) {
348 uint_t msk = (1ull << b)-1, _in[P4D_MAX+32]={0}, inx[P4D_MAX+32]={0},a,ax;
349 unsigned long long xmap[P4D_MAX/64] = {0};
350 unsigned miss[P4D_MAX]={0},i, xn, c; //, eq=0,eqx=0;
351 unsigned char *_out = out;
352
353 if(!bx)
354 return TEMPLATE2(BITPACK, USIZE)(in, n, out, b);
355 #if HYBRID > 0
356 if(bx == USIZE+2) {
357 TEMPLATE2(ctou, USIZE)(out) = in[0];
358 return out+((b+7)/8);
359 }
360 #endif
361 #define MISS { miss[xn] = i; xn += in[i] > msk; _in[i] = in[i] & msk; i++; } //eq+= (_in[i] == a); } a = in[0] & msk;
362 for(xn = i = 0; i != n&~3; ) { MISS; MISS; MISS; MISS; }
363 while(i != n) MISS;
364 //ax = inx[miss[0]] >> b;
365 for(i = 0; i != xn; ++i) {
366 c = miss[i];
367 xmap[c>>6] |= (1ull << (c&0x3f));
368 inx[i] = in[c] >> b; // eqx += inx[i] == a;
369 }
370
371 #if HYBRID > 0 && USIZE >= 16
372 if(bx <= USIZE) {
373 #endif
374 for(i = 0; i < (n+63)/64; i++) ctou64(out+i*8) = xmap[i]; out += PAD8(n); //if(eqx == xn && bx) { out[-1] |=0x80; TEMPLATE2(ctou, USIZE)(out)=ax; out += (bx+7)/8; } else
375 out = TEMPLATE2(bitpack, USIZE)(inx, xn, out, bx); //if(eq == n && b) { out[-1]|= 0x80; TEMPLATE2(ctou, USIZE)(out)=a; out += (b+7)/8; } else
376 out = TEMPLATE2(BITPACK, USIZE)(_in, n, out, b);
377 #if HYBRID > 0 && USIZE >= 16
378 }
379 else {
380 *out++ = xn; //if(b && eq == n) { *out++ = 0x80; TEMPLATE2(ctou, USIZE)(out) = _in[0]; out += (b+7)/8; } else { *out++ = 0;
381 out = TEMPLATE2(BITPACK, USIZE)(_in, n, out, b);
382
383 out = TEMPLATE2(vbenc, USIZE)(inx, xn, out);
384 for(i = 0; i != xn; ++i) *out++ = miss[i];
385 }
386 #endif
387 return out;
388}
389
390unsigned char *TEMPLATE2(P4ENC, USIZE)(uint_t *__restrict in, unsigned n, unsigned char *__restrict out) { unsigned bx, b;
391 if(!n) return out;
392 b = TEMPLATE2(P4BITS, USIZE)(in, n, &bx);
393 TEMPLATE2(P4HVE, USIZE)(out,b,bx);
394 out = TEMPLATE2(_P4ENC, USIZE)(in, n, out, b, bx);
395 return out;
396}
397
398size_t TEMPLATE2(P4NENC, USIZE)(uint_t *__restrict in, size_t n, unsigned char *__restrict out) { if(!n) return 0;
399 unsigned char *op = out;
400 uint_t *ip;
401
402 for(ip = in; ip != in+(n&~(CSIZE-1)); ip += CSIZE) { unsigned bx, b; PREFETCH(ip+512,0);
403 b = TEMPLATE2(P4BITS, USIZE)(ip, CSIZE, &bx);
404 TEMPLATE2(P4HVE, USIZE)(op,b,bx);
405 op = TEMPLATE2(_P4ENC, USIZE)(ip, CSIZE, op, b, bx);
406 }
407 return TEMPLATE2(p4enc, USIZE)(ip, n&(CSIZE-1), op) - out;
408}
409 #else
410unsigned char *TEMPLATE2(P4DENC, USIZE)(uint_t *__restrict in, unsigned n, unsigned char *__restrict out, uint_t start) {
411 uint_t _in[P4D_MAX+8]={0};
412 if(!n) return out;
413 TEMPLATE2(BITDELTA, USIZE)(in, n, _in, start, P4DELTA);
414 return TEMPLATE2(P4ENC, USIZE)(_in, n, out);
415}
416
417size_t TEMPLATE2(P4NENC, USIZE)(uint_t *__restrict in, size_t n, unsigned char *__restrict out) {
418 unsigned char *op = out;
419 uint_t *ip, start = *in++;
420 if(!n)
421 return 0;
422
423 TEMPLATE2(vbxput, USIZE)(op, start);
424 for(ip = in, --n; ip != in+(n&~(CSIZE-1)); ip += CSIZE) { uint_t _in[P4D_MAX+8];unsigned bx, b; PREFETCH(ip+512,0);
425 TEMPLATE2(BITDELTA, USIZE)(ip, CSIZE, _in, start, P4DELTA);
426 b = TEMPLATE2(_p4bits, USIZE)(_in, CSIZE, &bx);
427 TEMPLATE2(P4HVE, USIZE)(op,b,bx);
428 op = TEMPLATE2(_P4ENC, USIZE)(_in, CSIZE, op, b, bx); // op = TEMPLATE2(P4ENC, USIZE)(_in, CSIZE, op);
429 start = ip[CSIZE-1];
430 }
431 return TEMPLATE2(P4NENCS, USIZE)(ip, n&(CSIZE-1), op, start) - out;
432}
433 #endif
434#pragma clang diagnostic pop
435 #endif
436
437#endif