Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / om-file-format-sys / c / src / vint.c
vint.c417 lines · 12.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 | // vint.c - "Integer Compression" variable byte |
| 25 | #ifndef USIZE |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #pragma clang diagnostic ignored "-Wincompatible-pointer-types" |
| 30 | #pragma clang diagnostic ignored "-Wunused-variable" |
| 31 | #pragma clang diagnostic ignored "-Wmacro-redefined" |
| 32 | #pragma clang diagnostic ignored "-Wshift-op-parentheses" |
| 33 | #pragma clang diagnostic ignored "-Wunknown-pragmas" |
| 34 | #pragma clang diagnostic ignored "-Wunused-but-set-variable" |
| 35 | #pragma clang diagnostic ignored "-Wshorten-64-to-32" |
| 36 | #pragma clang diagnostic ignored "-Wunused-parameter" |
| 37 | |
| 38 | #pragma warning( disable : 4005) |
| 39 | #pragma warning( disable : 4090) |
| 40 | #pragma warning( disable : 4068) |
| 41 | |
| 42 | #define BITUTIL_IN |
| 43 | #define VINT_IN |
| 44 | #include "conf.h" |
| 45 | #include "vint.h" |
| 46 | #include "bitutil.h" |
| 47 | |
| 48 | #define UN 8 // 4 // |
| 49 | |
| 50 | #define VDELTA 0 |
| 51 | #define VBDENC vbdenc |
| 52 | #define VBDDEC vbddec |
| 53 | #define VBDGETX vbdgetx |
| 54 | #define VBDGETGEQ vbdgetgeq |
| 55 | |
| 56 | #define USIZE 32 |
| 57 | #include "vint.c" |
| 58 | #undef USIZE |
| 59 | |
| 60 | #define USIZE 64 |
| 61 | #include "vint.c" |
| 62 | #undef USIZE |
| 63 | |
| 64 | #define USIZE 16 |
| 65 | #include "vint.c" |
| 66 | #undef USIZE |
| 67 | |
| 68 | #define USIZE 8 |
| 69 | #include "vint.c" |
| 70 | #undef USIZE |
| 71 | |
| 72 | #define VDELTA 1 |
| 73 | #define VBDENC vbd1enc |
| 74 | #define VBDDEC vbd1dec |
| 75 | #define VBDGETX vbd1getx |
| 76 | #define VBDGETGEQ vbd1getgeq |
| 77 | |
| 78 | #define USIZE 32 |
| 79 | #include "vint.c" |
| 80 | #undef USIZE |
| 81 | |
| 82 | #define USIZE 64 |
| 83 | #include "vint.c" |
| 84 | #undef USIZE |
| 85 | |
| 86 | #define USIZE 16 |
| 87 | #include "vint.c" |
| 88 | #undef USIZE |
| 89 | |
| 90 | #define USIZE 8 |
| 91 | #include "vint.c" |
| 92 | #undef USIZE |
| 93 | #else |
| 94 | #define uint_t TEMPLATE3(uint, USIZE, _t) |
| 95 | |
| 96 | #if VDELTA == 0 |
| 97 | //#if USIZE < 64 |
| 98 | #define OVERFLOWD(in,n,out,vbmax) if(*in == (vbmax)) { memcpy(out, in+1, n*(USIZE/8)); return in+1+n*(USIZE/8); } |
| 99 | #define OVERFLOWE(in,n,out,op,vbmax) if(op > out + n*(USIZE/8)) { *out = (vbmax); memcpy(out+1, in, n*(USIZE/8)); op = out+1+n*(USIZE/8); } |
| 100 | //#else |
| 101 | //#define OVERFLOWD(in,n,out,vbmax) |
| 102 | //#define OVERFLOWE(in,n,out,op,vbmax) |
| 103 | //#endif |
| 104 | unsigned char *TEMPLATE2(vbdec, USIZE)(unsigned char *__restrict in, unsigned n, uint_t *__restrict out) { |
| 105 | register uint_t x, *op; |
| 106 | OVERFLOWD(in, n, out, VB_MAX+1); |
| 107 | |
| 108 | #define VBE(_i_) TEMPLATE2(_vbget, USIZE)(in, x, op[_i_] = x) |
| 109 | for(op = out; op != out+(n&~(UN-1)); op += UN) { |
| 110 | VBE(0); VBE(1); VBE(2); VBE(3); |
| 111 | #if UN > 4 |
| 112 | VBE(4); VBE(5); VBE(6); VBE(7); |
| 113 | #endif |
| 114 | PREFETCH(in+16*USIZE, 0); |
| 115 | } |
| 116 | while(op != out+n) |
| 117 | TEMPLATE2(_vbget, USIZE)(in, x, *op++ = x ); |
| 118 | return in; |
| 119 | } |
| 120 | #undef VBE |
| 121 | |
| 122 | unsigned char *TEMPLATE2(vbenc, USIZE)(uint_t *__restrict in, unsigned n, unsigned char *__restrict out) { |
| 123 | uint_t x, *ip; |
| 124 | unsigned char *op = out; |
| 125 | |
| 126 | #define VBD(_i_) x = ip[_i_]; TEMPLATE2(_vbput, USIZE)(op, x, ;); |
| 127 | for(ip = in; ip != in+(n&~(UN-1)); ip += UN) { PREFETCH(ip+USIZE*8, 0); |
| 128 | VBD(0); VBD(1); VBD(2); VBD(3); |
| 129 | #if UN > 4 |
| 130 | VBD(4); VBD(5); VBD(6); VBD(7); |
| 131 | #endif |
| 132 | } |
| 133 | while(ip != in+n) { |
| 134 | x = *ip++; |
| 135 | TEMPLATE2(_vbput, USIZE)(op, x, ;); |
| 136 | } |
| 137 | |
| 138 | OVERFLOWE(in,n,out,op,VB_MAX+1); |
| 139 | return op; |
| 140 | } |
| 141 | #undef VBD |
| 142 | |
| 143 | uint_t TEMPLATE2(vbgetx, USIZE)(unsigned char *__restrict in, unsigned idx) { |
| 144 | unsigned char *ip; |
| 145 | unsigned i; |
| 146 | uint_t x; |
| 147 | if(*in == 255) |
| 148 | return TEMPLATE2(ctou, USIZE)(in+1+idx*(USIZE/8)); |
| 149 | for(ip = in,i = 0; i <= idx; i++) |
| 150 | ip += TEMPLATE2(_vbvlen, USIZE)(*ip); |
| 151 | TEMPLATE2(_vbget, USIZE)(in, x, ;); |
| 152 | return x; |
| 153 | } |
| 154 | |
| 155 | /*unsigned TEMPLATE2(vbgeteq, USIZE)(unsigned char *__restrict in, unsigned n, uint_t key, unsigned char **__restrict _ip) { |
| 156 | unsigned i; |
| 157 | unsigned char *ip; |
| 158 | uint_t x; |
| 159 | if(*in == 255) { |
| 160 | for(ip = (*_ip==in)?in:*ip; ip < in+n; ip+USIZE/8) { |
| 161 | TEMPLATE2(_vbget, USIZE)(ip, x, ;); |
| 162 | if((x = TEMPLATE2(ctou, USIZE)(ip)) == key) break; |
| 163 | } |
| 164 | } else for(ip = *_ip,i=idx; i < n; i++) { |
| 165 | TEMPLATE2(_vbget, USIZE)(ip, x, ;); |
| 166 | if(x == key) break; |
| 167 | } |
| 168 | *_ip = ip; |
| 169 | return i; |
| 170 | }*/ |
| 171 | #define BITS 8 |
| 172 | #define BIT_SET( p, n) (p[(n)/BITS] |= (0x80>>((n)%BITS))) |
| 173 | #define BIT_CLEAR(p, n) (p[(n)/BITS] &= ~(0x80>>((n)%BITS))) |
| 174 | #define BIT_ISSET(p, n) (p[(n)/BITS] & (0x80>>((n)%BITS))) |
| 175 | unsigned char *TEMPLATE2(vbddenc, USIZE)(uint_t *__restrict in, unsigned n, unsigned char *__restrict out, uint_t start) { |
| 176 | uint_t *ip,v,pd=0,*p; |
| 177 | unsigned char *bp=out; out += (n+7)/8; |
| 178 | #define VBDDE(i) { uint_t x; start = ip[i]-start; x = start-pd; pd = start; \ |
| 179 | if(!x) BIT_CLEAR(bp,i); else { BIT_SET(bp,i); x = TEMPLATE2(zigzagenc, USIZE)(x); TEMPLATE2(_vbput, USIZE)(out, x, ;); } start = ip[i]; } |
| 180 | |
| 181 | for(ip = in; ip != in+(n&~(8-1)); ip+=8,bp++) { VBDDE(0);VBDDE(1);VBDDE(2);VBDDE(3);VBDDE(4);VBDDE(5);VBDDE(6);VBDDE(7);} |
| 182 | for(p=ip; p != in+n; p++) VBDDE(p-ip); |
| 183 | return out; |
| 184 | } |
| 185 | |
| 186 | unsigned char *TEMPLATE2(vbdddec, USIZE)(unsigned char *__restrict in, unsigned n, uint_t *__restrict out, uint_t start) { |
| 187 | uint_t *op,pd=0,*p; |
| 188 | unsigned i; |
| 189 | #define VBDDD(i) { uint_t x=0; if(BIT_ISSET(bp,i)) { TEMPLATE2(_vbget, USIZE)(in, x, ;); pd += TEMPLATE2(zigzagdec, USIZE)(x); } op[i] = (start += pd); } |
| 190 | unsigned char *bp=in; in+=(n+7)/8; |
| 191 | for(op = out; op != out+(n&~(8-1)); op+=8,bp++) { |
| 192 | if(!bp[0]) { op[0]=(start+=pd); op[1]=(start+=pd); op[2]=(start+=pd); op[3]=(start+=pd); op[4]=(start+=pd); op[5]=(start+=pd); op[6]=(start+=pd); op[7]=(start+=pd); continue; } |
| 193 | VBDDD(0); VBDDD(1); VBDDD(2); VBDDD(3); VBDDD(4); VBDDD(5); VBDDD(6); VBDDD(7); PREFETCH(in+16*USIZE, 0); |
| 194 | } |
| 195 | for(p=op; p != out+n; p++) VBDDD(p-op); |
| 196 | return in; |
| 197 | } |
| 198 | #undef VBDDE |
| 199 | #undef VBDDD |
| 200 | |
| 201 | unsigned char *TEMPLATE2(vbzenc, USIZE)(uint_t *__restrict in, unsigned n, unsigned char *__restrict out, uint_t start) { |
| 202 | uint_t *ip,v; |
| 203 | unsigned char *op = out; |
| 204 | |
| 205 | #define VBZE { v = TEMPLATE2(zigzagenc, USIZE)((TEMPLATE3(int, USIZE, _t))(*ip)-(TEMPLATE3(int, USIZE, _t))start); start=*ip++; TEMPLATE2(_vbput, USIZE)(op, v, ;); } |
| 206 | for(ip = in; ip != in+(n&~(UN-1)); ) { VBZE;VBZE;VBZE;VBZE; } |
| 207 | while(ip != in+n) VBZE; //OVERFLOWE(in,n,out,op); |
| 208 | return op; |
| 209 | } |
| 210 | #undef VBZE |
| 211 | |
| 212 | unsigned char *TEMPLATE2(vbzdec, USIZE)(unsigned char *__restrict in, unsigned n, uint_t *__restrict out, uint_t start) { |
| 213 | uint_t x,*op; |
| 214 | |
| 215 | #define VBZD { TEMPLATE2(_vbget, USIZE)(in, x, ;); *op++ = (start += TEMPLATE2(zigzagdec, USIZE)(x)); } |
| 216 | for(op = out; op != out+(n&~(UN-1)); ) { VBZD; VBZD; VBZD; VBZD; |
| 217 | #if UN > 4 |
| 218 | VBZD; VBZD; VBZD; VBZD; |
| 219 | #endif |
| 220 | PREFETCH(in+16*USIZE, 0); |
| 221 | } |
| 222 | while(op != out+n) VBZD; |
| 223 | |
| 224 | return in; |
| 225 | } |
| 226 | #undef VBZD |
| 227 | |
| 228 | uint_t TEMPLATE2(vbzgetx, USIZE)(unsigned char *__restrict in, unsigned idx, uint_t start) { |
| 229 | unsigned char *ip; |
| 230 | unsigned i; |
| 231 | uint_t x; |
| 232 | |
| 233 | for(ip = in,i = 0; i <= idx; i++) { |
| 234 | TEMPLATE2(_vbget, USIZE)(ip, x, ;); |
| 235 | start += x+VDELTA; |
| 236 | } |
| 237 | return start; |
| 238 | } |
| 239 | |
| 240 | unsigned TEMPLATE2(vbzgeteq, USIZE)(unsigned char **__restrict in, unsigned n, unsigned idx, uint_t key, uint_t start ) { |
| 241 | unsigned i; |
| 242 | unsigned char *ip; |
| 243 | uint_t x; |
| 244 | for(ip = *in,i=idx; i < n; i++) { |
| 245 | TEMPLATE2(_vbget, USIZE)(ip, x, ;); |
| 246 | if((start += x+VDELTA) == key) |
| 247 | break; |
| 248 | } |
| 249 | *in = ip; |
| 250 | return i; |
| 251 | } |
| 252 | |
| 253 | unsigned char *TEMPLATE2(vbxenc, USIZE)(uint_t *__restrict in, unsigned n, unsigned char *__restrict out, uint_t start) { |
| 254 | uint_t *ip,v; |
| 255 | unsigned char *op = out; |
| 256 | |
| 257 | #define VBXE { v = (*ip)^start; start=*ip++; TEMPLATE2(_vbput, USIZE)(op, v, ;); } |
| 258 | for(ip = in; ip != in+(n&~(UN-1)); ) { VBXE;VBXE;VBXE;VBXE; } |
| 259 | while(ip != in+n) VBXE; //OVERFLOWE(in,n,out,op); |
| 260 | return op; |
| 261 | } |
| 262 | #undef VBZE |
| 263 | |
| 264 | unsigned char *TEMPLATE2(vbxdec, USIZE)(unsigned char *__restrict in, unsigned n, uint_t *__restrict out, uint_t start) { |
| 265 | uint_t x,*op; |
| 266 | |
| 267 | #define VBXD { TEMPLATE2(_vbget, USIZE)(in, x, ;); *op++ = (start ^= x); } |
| 268 | for(op = out; op != out+(n&~(UN-1)); ) { VBXD; VBXD; VBXD; VBXD; |
| 269 | #if UN > 4 |
| 270 | VBXD; VBXD; VBXD; VBXD; |
| 271 | #endif |
| 272 | PREFETCH(in+16*USIZE, 0); |
| 273 | } |
| 274 | while(op != out+n) VBXD; |
| 275 | |
| 276 | return in; |
| 277 | } |
| 278 | #undef VBZD |
| 279 | |
| 280 | uint_t TEMPLATE2(vbxgetx, USIZE)(unsigned char *__restrict in, unsigned idx, uint_t start) { |
| 281 | unsigned char *ip; |
| 282 | unsigned i; |
| 283 | uint_t x; |
| 284 | |
| 285 | for(ip = in,i = 0; i <= idx; i++) { |
| 286 | TEMPLATE2(_vbget, USIZE)(ip, x, ;); |
| 287 | start ^= x; |
| 288 | } |
| 289 | return start; |
| 290 | } |
| 291 | |
| 292 | unsigned TEMPLATE2(vbxgeteq, USIZE)(unsigned char **__restrict in, unsigned n, unsigned idx, uint_t key, uint_t start ) { |
| 293 | unsigned i; |
| 294 | unsigned char *ip; |
| 295 | uint_t x; |
| 296 | for(ip = *in,i=idx; i < n; i++) { |
| 297 | TEMPLATE2(_vbget, USIZE)(ip, x, ;); |
| 298 | if((start ^= x) == key) |
| 299 | break; |
| 300 | } |
| 301 | *in = ip; |
| 302 | return i; |
| 303 | } |
| 304 | #endif |
| 305 | |
| 306 | unsigned char *TEMPLATE2(VBDENC, USIZE)(uint_t *__restrict in, unsigned n, unsigned char *__restrict out, uint_t start) { |
| 307 | unsigned char *op = out; |
| 308 | uint_t *ip, b = 0,v; |
| 309 | if(!n) return out; |
| 310 | #if USIZE == 64 |
| 311 | #define VB_MX 255 |
| 312 | #else |
| 313 | #define VB_MX VB_MAX |
| 314 | #endif |
| 315 | #define VBDE { v = ip[0]-start-VDELTA; start = *ip++; TEMPLATE2(_vbput, USIZE)(op, v, ;); b |= (v /*^ x*/); } |
| 316 | for(ip = in; ip != in + (n&~(UN-1)); ) { VBDE; VBDE; VBDE; VBDE; |
| 317 | #if UN > 4 |
| 318 | VBDE; VBDE; VBDE; VBDE; |
| 319 | #endif |
| 320 | } |
| 321 | while(ip != in+n) VBDE; |
| 322 | if(!b) { op = out; *op++ = VB_MX; } // if (x) { op = out; *op++ = VB_MAX-2; TEMPLATE2(_vbput, USIZE)(op, x, ;); } |
| 323 | #if USIZE < 64 |
| 324 | OVERFLOWE(in,n,out,op,VB_MAX-1); |
| 325 | #endif |
| 326 | return op; |
| 327 | } |
| 328 | #undef VBDE |
| 329 | |
| 330 | unsigned char *TEMPLATE2(VBDDEC, USIZE)(unsigned char *__restrict in, unsigned n, uint_t *__restrict out, uint_t start) { |
| 331 | uint_t x,*op; |
| 332 | if(!n) return in; |
| 333 | |
| 334 | #if USIZE < 64 |
| 335 | OVERFLOWD(in,n,out,VB_MAX-1); |
| 336 | #endif |
| 337 | |
| 338 | if(in[0] == VB_MX) { |
| 339 | in++; |
| 340 | #if (defined(__SSE2__) || defined(__ARM_NEON)) && USIZE == 32 |
| 341 | #if VDELTA == 0 |
| 342 | if(n) TEMPLATE2(BITZERO, USIZE)(out, n, start); |
| 343 | #else |
| 344 | if(n) TEMPLATE2(BITDIZERO,USIZE)(out, n, start, VDELTA); |
| 345 | #endif |
| 346 | #else |
| 347 | #if VDELTA == 0 |
| 348 | for(unsigned j = 0; j < n; j++) out[j] = start; |
| 349 | #else |
| 350 | for(unsigned j = 0; j < n; j++) out[j] = start+(uint_t)j*VDELTA; |
| 351 | #endif |
| 352 | #endif |
| 353 | return in; |
| 354 | } |
| 355 | #if 0 //USIZE < 64 |
| 356 | else if(in[0] == VB_MAX-2) { in++; |
| 357 | uint_t z; |
| 358 | TEMPLATE2(_vbget, USIZE)(in, z, ;); |
| 359 | #if VDELTA == 0 |
| 360 | for(x = 0; x < n; x++) out[x] = start+z; |
| 361 | #else |
| 362 | for(x = 0; x < n; x++) out[x] = start+x+z; |
| 363 | #endif |
| 364 | return in; |
| 365 | } |
| 366 | #endif |
| 367 | #define VBDD(i) { TEMPLATE2(_vbget, USIZE)(in, x, x+=VDELTA); op[i] = (start += x); } |
| 368 | for(op = out; op != out+(n&~(UN-1)); op+=UN) { |
| 369 | VBDD(0); VBDD(1); VBDD(2); VBDD(3); |
| 370 | #if UN > 4 |
| 371 | VBDD(4); VBDD(5); VBDD(6); VBDD(7); |
| 372 | #endif |
| 373 | PREFETCH(in+16*USIZE, 0); |
| 374 | } |
| 375 | for(;op != out+n;op++) VBDD(0); |
| 376 | return in; |
| 377 | } |
| 378 | #undef VBDD |
| 379 | |
| 380 | uint_t TEMPLATE2(VBDGETX, USIZE)(unsigned char *__restrict in, unsigned idx, uint_t start) { |
| 381 | unsigned char *ip; |
| 382 | unsigned i=0; |
| 383 | uint_t x; |
| 384 | |
| 385 | #if USIZE > 64 |
| 386 | unsigned long long u; |
| 387 | _vbget64(in, u, ;); x = u>>1; start += x+VDELTA; |
| 388 | if(u & 1) return start + VDELTA; |
| 389 | #endif |
| 390 | for(ip = in; i <= idx; i++) { |
| 391 | TEMPLATE2(_vbget, USIZE)(ip, x, ;); |
| 392 | start += x+VDELTA; |
| 393 | } |
| 394 | return start; |
| 395 | } |
| 396 | |
| 397 | unsigned TEMPLATE2(VBDGETGEQ, USIZE)(unsigned char **__restrict in, unsigned n, unsigned idx, uint_t *key, uint_t start ) { |
| 398 | unsigned i=0; |
| 399 | unsigned char *ip=*in; |
| 400 | uint_t x; |
| 401 | #if USIZE < 64 |
| 402 | if(!idx) { |
| 403 | unsigned long long u; _vbget64(ip, u, ;); x = u>>1; start += x+VDELTA; |
| 404 | if((u & 1) && start == *key) { *in = ip; return 0; } |
| 405 | i++; |
| 406 | } |
| 407 | #endif |
| 408 | for(ip = *in; i < n; i++) { |
| 409 | TEMPLATE2(_vbget, USIZE)(ip, x, ;); |
| 410 | if((start += x+VDELTA) == *key) |
| 411 | break; |
| 412 | } |
| 413 | *in = ip; |
| 414 | return i; |
| 415 | } |
| 416 | #undef uint_t |
| 417 | #endif |