BitUtil: delta, zigzag, bitsnum,...

This commit is contained in:
powturbo
2015-06-16 17:14:23 +02:00
parent a2b4366f93
commit 38c4c9e65c

View File

@ -21,7 +21,7 @@
- twitter : https://twitter.com/powturbo - twitter : https://twitter.com/powturbo
- email : powturbo [_AT_] gmail [_DOT_] com - email : powturbo [_AT_] gmail [_DOT_] com
**/ **/
// bitutil.h - "Integer Compression" // bitutil.h - "Integer Compression"
#include <stdint.h> #include <stdint.h>
#define _BITFORZERO(out, n, start, inc) do {\ #define _BITFORZERO(out, n, start, inc) do {\
@ -34,66 +34,51 @@
while(i < n) out[i] = start+i*inc,++i;\ while(i < n) out[i] = start+i*inc,++i;\
} while(0) } while(0)
#define BITSIZE(__in, __n, __b, __usize) { typeof(__in[0]) *_ip;\
for(__b=0,_ip = __in; _ip != __in+(__n&~(4-1)); )\
__b |= *_ip++ | *_ip++ | *_ip++ | *_ip++;\
while(_ip != __in+__n) __b |= *_ip++;\
__b = TEMPLATE(bsr, __usize)(__b);\
}
#ifdef __SSE2__ #ifdef __SSE2__
#include <emmintrin.h> #include <emmintrin.h>
#define DELTAV(__v, __sv) _mm_sub_epi32(__v, _mm_or_si128(_mm_srli_si128(__sv, 12), _mm_slli_si128(__v, 4))) #define DELTA128_32(__v, __sv) _mm_sub_epi32(__v, _mm_or_si128(_mm_srli_si128(__sv, 12), _mm_slli_si128(__v, 4)))
#define VSCAN( __v, __sv) __v = _mm_add_epi32(__v, _mm_slli_si128(__v, 4)); __sv = _mm_add_epi32(_mm_shuffle_epi32(__sv, _MM_SHUFFLE(3, 3, 3, 3)), _mm_add_epi32(_mm_slli_si128(__v, 8), __v) ) #define SCAN128_32( __v, __sv) __v = _mm_add_epi32(__v, _mm_slli_si128(__v, 4)); __sv = _mm_add_epi32(_mm_shuffle_epi32(__sv, _MM_SHUFFLE(3, 3, 3, 3)), _mm_add_epi32(_mm_slli_si128(__v, 8), __v) )
#define VSCANI(__v, __sv, __vi) VSCAN(__v, __sv); __sv = _mm_add_epi32(__sv, __vi) #define SCANI128_32(__v, __sv, __vi) SCAN128_32(__v, __sv); __sv = _mm_add_epi32(__sv, __vi)
// SIMD Horizontal OR // SIMD Horizontal OR
#define HOR(__v,__b) __v = _mm_or_si128(__v, _mm_srli_si128(__v, 8)); __v = _mm_or_si128(__v, _mm_srli_si128(__v, 4)); __b = (unsigned)_mm_cvtsi128_si32(__v) #define HOR128_32(__v,__b) __v = _mm_or_si128(__v, _mm_srli_si128(__v, 8)); __v = _mm_or_si128(__v, _mm_srli_si128(__v, 4)); __b = (unsigned)_mm_cvtsi128_si32(__v)
#define BSRN(__in, __n, __b) { typeof(__in[0]) *_ip; __m128i v = _mm_setzero_si128();\ #define BITSIZE32(__in, __n, __b) { typeof(__in[0]) *_ip; __m128i v = _mm_setzero_si128();\
for(_ip = __in; _ip != __in+(__n&~(4-1)); _ip+=4) v = _mm_or_si128(v, _mm_loadu_si128((__m128i*)_ip));\ for(_ip = __in; _ip != __in+(__n&~(4-1)); _ip+=4) v = _mm_or_si128(v, _mm_loadu_si128((__m128i*)_ip));\
HOR(v,__b);\ HOR128_32(v,__b);\
while(_ip != __in+__n) __b |= *_ip++;\ while(_ip != __in+__n) __b |= *_ip++;\
__b = bsr32(__b);\ __b = bsr32(__b);\
} }
#define BITZERO(out, n, start) do {\ #define BITZERO32(out, n, start) do {\
__m128i sv = _mm_set1_epi32(start), *ov = (__m128i *)(out), *ove = (__m128i *)(out + n);\ __m128i sv = _mm_set1_epi32(start), *ov = (__m128i *)(out), *ove = (__m128i *)(out + n);\
do { _mm_storeu_si128(ov++, sv); } while(ov < ove); \ do { _mm_storeu_si128(ov++, sv); } while(ov < ove); \
} while(0) } while(0)
#define BITFORZERO(out, n, start, inc) do {\ #define BITFORZERO32(out, n, start, inc) do {\
__m128i sv = _mm_set1_epi32(start), *ov=(__m128i *)(out), *ove = (__m128i *)(out + n), cv = _mm_set_epi32(3*inc,2*inc,1*inc,0); \ __m128i sv = _mm_set1_epi32(start), *ov=(__m128i *)(out), *ove = (__m128i *)(out + n), cv = _mm_set_epi32(3*inc,2*inc,1*inc,0); \
sv = _mm_add_epi32(sv, cv);\ sv = _mm_add_epi32(sv, cv);\
cv = _mm_set1_epi32(4);\ cv = _mm_set1_epi32(4);\
do { _mm_storeu_si128(ov++, sv); sv = _mm_add_epi32(sv, cv); } while(ov < ove);\ do { _mm_storeu_si128(ov++, sv); sv = _mm_add_epi32(sv, cv); } while(ov < ove);\
} while(0) } while(0)
#if 0 #define BITDIZERO32(out, n, start, inc) do { __m128i sv = _mm_set1_epi32(start), cv = _mm_set_epi32(3+inc,2+inc,1+inc,inc), *ov=(__m128i *)(out), *ove = (__m128i *)(out + n);\
#define BITUNBLKV32_0(ip, i, __op, __parm) {\
_mm_storeu_si128(__op++, __parm); __parm = _mm_add_epi32(__parm, cv); \
_mm_storeu_si128(__op++, __parm); __parm = _mm_add_epi32(__parm, cv); \
_mm_storeu_si128(__op++, __parm); __parm = _mm_add_epi32(__parm, cv); \
_mm_storeu_si128(__op++, __parm); __parm = _mm_add_epi32(__parm, cv); \
_mm_storeu_si128(__op++, __parm); __parm = _mm_add_epi32(__parm, cv); \
_mm_storeu_si128(__op++, __parm); __parm = _mm_add_epi32(__parm, cv); \
_mm_storeu_si128(__op++, __parm); __parm = _mm_add_epi32(__parm, cv); \
_mm_storeu_si128(__op++, __parm); __parm = _mm_add_epi32(__parm, cv); \
}
#define BITUNPACK0(__parm,inc) __parm = _mm_add_epi32(__parm, cv); cv = _mm_set1_epi32(4*inc)
#define BITDIZERO(out, n, start, inc) do {\
__m128i sv = _mm_set1_epi32(start), cv = _mm_set_epi32(3+inc,2+inc,1+inc,inc), *ov=(__m128i *)(out), *ove = (__m128i *)(out + n); BITUNPACK0(sv,inc); do BITUNBLKV32_0( iv, 0, ov, sv) while(ov < ove);\
} while(0)
#else
#define BITDIZERO(out, n, start, inc) do { __m128i sv = _mm_set1_epi32(start), cv = _mm_set_epi32(3+inc,2+inc,1+inc,inc), *ov=(__m128i *)(out), *ove = (__m128i *)(out + n);\
sv = _mm_add_epi32(sv, cv); cv = _mm_set1_epi32(4*inc); do { _mm_storeu_si128(ov++, sv), sv = _mm_add_epi32(sv, cv); } while(ov < ove);\ sv = _mm_add_epi32(sv, cv); cv = _mm_set1_epi32(4*inc); do { _mm_storeu_si128(ov++, sv), sv = _mm_add_epi32(sv, cv); } while(ov < ove);\
} while(0) } while(0)
#endif
#else #else
#define BSRN(__in, __n, __b) { typeof(__in[0]) *_ip;\ #define BITSIZE32(__in, __n, __b) BITSIZE(__in, __n, __b, 32)
for(__b=0,_ip = __in; _ip != __in+(__n&~(4-1)); )\ #define BITFORZERO32(out, n, start, inc) _BITFORZERO(out, n, start, inc)
__b |= *_ip++ | *_ip++ | *_ip++ | *_ip++;\ #define BITZERO32(out, n, start) _BITFORZERO(out, n, start, 0)
while(_ip != __in+__n) __b |= *_ip++;\
__b = bsr32(__b);\
}
#define BITFORZERO(out, n, start, inc) _BITFORZERO(out, n, start, inc)
#endif #endif
#define ZIGZAG( __in, __n, __mode, __out) { unsigned _v; for( __out[0]=__in[0],_v = __n-1; _v > 0; --_v) { int _z = ((int)__in[_v] - (int)__in[_v-1]) - __mode; __out[_v] = (_z << 1) ^ (_z >> 31); } } #define ZIGZAG( __in, __n, __mode, __out) { unsigned _v; for( __out[0]=__in[0],_v = __n-1; _v > 0; --_v) { int _z = ((int)__in[_v] - (int)__in[_v-1]) - __mode; __out[_v] = (_z << 1) ^ (_z >> 31); } }
@ -110,6 +95,9 @@
extern "C" { extern "C" {
#endif #endif
// get maximum bit length of the elements in the integer array
unsigned bit32( unsigned *in, unsigned n);
// transform sorted integer array to delta array. inc = increment // transform sorted integer array to delta array. inc = increment
unsigned bitdelta32(unsigned *in, unsigned n, unsigned *out, unsigned start, unsigned inc); unsigned bitdelta32(unsigned *in, unsigned n, unsigned *out, unsigned start, unsigned inc);
unsigned bitdelta64(uint64_t *in, unsigned n, uint64_t *out, uint64_t start, unsigned inc); unsigned bitdelta64(uint64_t *in, unsigned n, uint64_t *out, uint64_t start, unsigned inc);