14#ifndef BITSTREAM_H_MODULE
15#define BITSTREAM_H_MODULE
17#if defined (__cplusplus)
39#if defined(__BMI__) && defined(__GNUC__)
40# include <immintrin.h>
41#elif defined(__ICCARM__)
42# include <intrinsics.h>
45#define STREAM_ACCUMULATOR_MIN_32 25
46#define STREAM_ACCUMULATOR_MIN_64 57
47#define STREAM_ACCUMULATOR_MIN ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64))
143# if defined(_MSC_VER)
146# elif defined(__GNUC__) && (__GNUC__ >= 3)
147 return __builtin_clz (
val) ^ 31;
148# elif defined(__ICCARM__)
149 return 31 - __CLZ(
val);
151 static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29,
152 11, 14, 16, 18, 22, 25, 3, 30,
153 8, 12, 20, 28, 15, 17, 24, 7,
154 19, 27, 23, 6, 26, 5, 4, 31 };
161 return DeBruijnClz[ (
U32) (
v * 0x07C4ACDDU) >> 27];
168 0, 1, 3, 7, 0xF, 0x1F,
169 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF,
170 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF,
171 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF,
172 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, 0xFFFFFFF, 0x1FFFFFFF,
173 0x3FFFFFFF, 0x7FFFFFFF};
174#define BIT_MASK_SIZE (sizeof(BIT_mask) / sizeof(BIT_mask[0]))
184 void* startPtr,
size_t dstCapacity)
199 size_t value,
unsigned nbBits)
212 size_t value,
unsigned nbBits)
225 size_t const nbBytes = bitC->
bitPos >> 3;
229 bitC->
ptr += nbBytes;
241 size_t const nbBytes = bitC->
bitPos >> 3;
245 bitC->
ptr += nbBytes;
274 if (srcSize < 1) {
memset(bitD, 0,
sizeof(*bitD));
return ERROR(srcSize_wrong); }
276 bitD->
start = (
const char*)srcBuffer;
280 bitD->
ptr = (
const char*)srcBuffer + srcSize -
sizeof(bitD->
bitContainer);
282 {
BYTE const lastByte = ((
const BYTE*)srcBuffer)[srcSize-1];
284 if (lastByte == 0)
return ERROR(GENERIC); }
310 {
BYTE const lastByte = ((
const BYTE*)srcBuffer)[srcSize-1];
312 if (lastByte == 0)
return ERROR(corruption_detected);
322 return bitContainer >>
start;
327 U32 const regMask =
sizeof(bitContainer)*8 - 1;
330 return (bitContainer >> (
start & regMask)) &
BIT_mask[nbBits];
336 return bitContainer &
BIT_mask[nbBits];
431 if (bitD->
ptr - nbBytes < bitD->
start) {
435 bitD->
ptr -= nbBytes;
450#if defined (__cplusplus)
MEM_STATIC size_t BIT_initDStream(BIT_DStream_t *bitD, const void *srcBuffer, size_t srcSize)
MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t *bitC)
MEM_STATIC unsigned BIT_highbit32(U32 val)
MEM_STATIC void BIT_skipBits(BIT_DStream_t *bitD, U32 nbBits)
MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t *bitD)
MEM_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
MEM_STATIC size_t BIT_readBits(BIT_DStream_t *bitD, unsigned nbBits)
MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
MEM_STATIC void BIT_flushBits(BIT_CStream_t *bitC)
MEM_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
@ BIT_DStream_endOfBuffer
MEM_STATIC size_t BIT_initCStream(BIT_CStream_t *bitC, void *dstBuffer, size_t dstCapacity)
MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t *bitD, unsigned nbBits)
MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t *bitD)
MEM_STATIC void BIT_addBits(BIT_CStream_t *bitC, size_t value, unsigned nbBits)
static const unsigned BIT_mask[]
MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t *bitD, U32 nbBits)
MEM_STATIC void BIT_addBitsFast(BIT_CStream_t *bitC, size_t value, unsigned nbBits)
MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t *bitD, U32 nbBits)
MEM_STATIC BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t *bitD)
MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t *bitC)
#define MEM_STATIC_ASSERT(c)
MEM_STATIC size_t MEM_readLEST(const void *memPtr)
MEM_STATIC void MEM_writeLEST(void *memPtr, size_t val)
GLdouble GLdouble GLdouble r
unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask)
static unsigned(__cdecl *hash_bstr)(bstr_t s)