From 845ad8cd5f91e1918b6b1f58ae9239bc80804c24 Mon Sep 17 00:00:00 2001 From: powturbo Date: Thu, 28 May 2015 16:52:50 +0200 Subject: [PATCH] . --- vint.h | 76 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/vint.h b/vint.h index 6e7f4ce..3048a94 100644 --- a/vint.h +++ b/vint.h @@ -1,7 +1,7 @@ /** - Copyright (C) powturbo 2013-2014 + Copyright (C) powturbo 2013-2015 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 @@ -16,17 +16,15 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - email : powturbo [AT] gmail.com - - github : https://github.com/powturbo - homepage : https://sites.google.com/site/powturbo/ + - github : https://github.com/powturbo - twitter : https://twitter.com/powturbo - - vint.h - "Integer Compression" variable byte + - email : powturbo [_AT_] gmail [_DOT_] com **/ +// vint.h - "Integer Compression" variable byte include header #ifndef VINT_H #define VINT_H -#include "conf.h" #ifdef __cplusplus extern "C" { @@ -36,50 +34,64 @@ extern "C" { #define vbvlen(__x) vtab[(__x)&0xf] #define vbputa(__op, __x, __act) {\ - if(likely(__x < (1<< 7))) { *__op++ = __x << 1; __act;}\ - else if(likely(__x < (1<<14))) { *(unsigned short *)__op = __x << 2 | 0x01; __op += 2; __act;}\ + if(likely(__x < (1<< 7))) { *__op++ = __x << 1; __act;}\ + else if(likely(__x < (1<<14))) { *(unsigned short *)__op = __x << 2 | 0x01; __op += 2; __act;}\ else if(likely(__x < (1<<21))) { *(unsigned short *)__op = __x << 3 | 0x03; __op += 2; *__op++ = __x >> 13; __act;}\ - else if(likely(__x < (1<<28))) { *(unsigned *)__op = __x << 4 | 0x07; __op += 4; __act;}\ - else { *(unsigned *)__op = __x << 4 | 0x0f; __op += 4; *__op++ = __x >> 28; __act;}\ + else if(likely(__x < (1<<28))) { *(unsigned *)__op = __x << 4 | 0x07; __op += 4; __act;}\ + else { *(unsigned *)__op = __x << 4 | 0x0f; __op += 4; *__op++ = __x >> 28; __act;}\ } - -#define vbgeta(__ip, __x, __act) do { __x = *__ip;\ - if(!(__x & (1<<0))) { __x >>= 1; __ip++; __act;}\ - else if(!(__x & (1<<1))) { __x = (*(unsigned short *)__ip) >> 2; __ip += 2; __act;}\ - else if(!(__x & (1<<2))) { __x = (*(unsigned short *)__ip) >> 3 | *(__ip+2) << 13; __ip += 3; __act;}\ - else if(!(__x & (1<<3))) { __x = (*(unsigned *)__ip) >> 4; __ip += 4; __act;}\ - else { __x = (*(unsigned *)__ip) >> 4 | *(__ip+4) << 28; __ip += 5; __act;}\ + +//#define __AVX2__VINT + + #if defined(__AVX2__) && defined(__AVX2__VINT) +#include + +extern unsigned long long mtab[]; + +#define vbgeta(__ip, __x, __act) do { unsigned _vdx=(*__ip)&0xf; __x = _pext_u64(*(unsigned long long *)__ip, mtab[_vdx]); __ip+=vtab[_vdx]; __act; } while(0) + #else +#define vbgeta(__ip, __x, __act) do {\ + if(!((__x = *__ip) & (1<<0))) { __ip++; __x >>= 1; __act;}\ + else if(!((__x = *(unsigned short *)__ip) & (1<<1))) { __ip += 2; __x >>= 2; __act;}\ + else if(!( __x & (1<<2))) { __ip += 3; __x = __x >> 3 | (unsigned)__ip[-1] << 13; __act;}\ + else if(!((__x = *(unsigned *)__ip) & (1<<3))) { __ip += 4; __x >>= 4; __act;}\ + else { __ip += 5; __x = __x >> 4 | (unsigned)__ip[-1] << 28; __act;}\ } while(0) + + #endif //------------------------------------------------------------------------------------------------------------------------ - //0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 -static unsigned char vtab[]= { 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5 }; +extern unsigned char vtab[]; // Length of uncompress value. Input __x is the compressed buffer start // Length in bytes of compressed "__x" when using variable byte #define vblen(__x) ({ unsigned _x = __x; _x > 0x7f?(_x > 0x3fff?(_x > 0x1fffff?(_x > 0x0fffffff?5:4):3):2):1; }) -// compress single value -#define vbput(__op, __x) { unsigned _x__ = __x; vbputa(__op, _x__, ;); } -// decompress single value -#define vbget(__ip) ({ unsigned _x_; vbgeta(__ip, _x_, ;); _x_; }) +// encode/decode single value +#define vbput(__op, __x) { unsigned _x_ = __x; vbputa(__op, _x_, ;); } +#define vbget(__ip) ({ unsigned _x_; vbgeta(__ip, _x_, ;); _x_; }) -// compress array with n unsigned (32 bits in[n]) values to the buffer out. Return value = end of compressed buffer out -static inline unsigned char *vbenc (unsigned *__restrict in, int n, unsigned char *__restrict out) { unsigned *in_ = in +n; while(in < in_) vbput(out, *in++); return out;} +// encode array with n unsigned (32 bits in[n]) values to the buffer out. Return value = end of compressed buffer out +unsigned char *vbenc (unsigned *__restrict in, unsigned n, unsigned char *__restrict out); // decompress buffer into an array of n unsigned values. Return value = end of decompressed buffer in -static inline unsigned char *vbdec (unsigned char *__restrict in, int n, unsigned *__restrict out) { unsigned *out_ = out+n,x; while(out < out_) vbgeta(in, x, *out++ = x); return in;} +unsigned char *vbdec (unsigned char *__restrict in, unsigned n, unsigned *__restrict out); +//---------------------------------- increasing integer lists ------------------------------------------------------------- +unsigned char *vbdenc (unsigned *__restrict in, unsigned n, unsigned char *__restrict out, unsigned start); +unsigned char *vbddec (unsigned char *__restrict in, unsigned n, unsigned *__restrict out, unsigned start); -//--------------------------------------- variable byte : 15 bits ------------------------------------------------------------------- +//---------------------------------- strictly increasing (never remaining constant or decreasing) integer lists------------- +unsigned char *vbd1enc (unsigned *__restrict in, unsigned n, unsigned char *__restrict out, unsigned start); +unsigned char *vbd1dec (unsigned char *__restrict in, unsigned n, unsigned *__restrict out, unsigned start); + +//---------------------------------- variable byte : 15 bits (like vbenc/vbdec but for 16 bits integer lists) -------- #define vbput16(__op, __x) do { unsigned _x = __x; if(likely(_x < 0x80)) *__op++ = _x; else { *__op++ = (_x) >> 8 | 0x80; *__op++ = _x; } } while(0) #define vbgeta16(__ip,__x, __act) do { if((__x = *__ip++) > 0x7f) __x = (__x & 0x7f) << 8 | *__ip++; __act; } while(0) #define vblen16(__x) ((__x) > 0x7f?2:1) #define vbget16(__ip) ({ unsigned _x; vbgeta16(__ip, _x, ;); _x; }) -// like vbenc32 but for 16 bits values -static inline unsigned char *vbenc16(unsigned short *__restrict in, int n, unsigned char *__restrict out) { unsigned short *in_ = in +n; while(in < in_) vbput16(out, *in++); return out;} -// like vbdec32 but for 16 bits values -static inline unsigned char *vbdec16(unsigned char *__restrict in, int n, unsigned short *__restrict out) { unsigned short *out_ = out+n,x; while(out < out_) vgeta16(in, x, *out++ = x); return in; } +unsigned char *vbenc16(unsigned short *__restrict in, unsigned n, unsigned char *__restrict out); +unsigned char *vbdec16(unsigned char *__restrict in, unsigned n, unsigned short *__restrict out); #ifdef __cplusplus }