14#ifndef BITSTREAM_H_MODULE
15#define BITSTREAM_H_MODULE
17#if defined (__cplusplus)
39#ifndef ZSTD_NO_INTRINSICS
40# if (defined(__BMI__) || defined(__BMI2__)) && defined(__GNUC__)
41# include <immintrin.h>
42# elif defined(__ICCARM__)
43# include <intrinsics.h>
47#define STREAM_ACCUMULATOR_MIN_32 25
48#define STREAM_ACCUMULATOR_MIN_64 57
49#define STREAM_ACCUMULATOR_MIN ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64))
138static const unsigned BIT_mask[] = {
139 0, 1, 3, 7, 0xF, 0x1F,
140 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF,
141 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF,
142 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF,
143 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, 0xFFFFFFF, 0x1FFFFFFF,
144 0x3FFFFFFF, 0x7FFFFFFF};
145#define BIT_MASK_SIZE (sizeof(BIT_mask) / sizeof(BIT_mask[0]))
155 void* startPtr,
size_t dstCapacity)
168#if defined(STATIC_BMI2) && STATIC_BMI2 == 1 && !defined(ZSTD_NO_INTRINSICS)
169 return _bzhi_u64(bitContainer, nbBits);
172 return bitContainer & BIT_mask[nbBits];
180 size_t value,
unsigned nbBits)
193 size_t value,
unsigned nbBits)
195 assert((value>>nbBits) == 0);
206 size_t const nbBytes = bitC->
bitPos >> 3;
210 bitC->
ptr += nbBytes;
222 size_t const nbBytes = bitC->
bitPos >> 3;
226 bitC->
ptr += nbBytes;
255 if (srcSize < 1) {
ZSTD_memset(bitD, 0,
sizeof(*bitD));
return ERROR(srcSize_wrong); }
257 bitD->
start = (
const char*)srcBuffer;
261 bitD->
ptr = (
const char*)srcBuffer + srcSize -
sizeof(bitD->
bitContainer);
263 {
BYTE const lastByte = ((
const BYTE*)srcBuffer)[srcSize-1];
265 if (lastByte == 0)
return ERROR(GENERIC); }
291 {
BYTE const lastByte = ((
const BYTE*)srcBuffer)[srcSize-1];
293 if (lastByte == 0)
return ERROR(corruption_detected);
303 return bitContainer >> start;
308 U32 const regMask =
sizeof(bitContainer)*8 - 1;
316#if defined(__x86_64__) || defined(_M_X86)
317 return (bitContainer >> (start & regMask)) & ((((
U64)1) << nbBits) - 1);
319 return (bitContainer >> (start & regMask)) & BIT_mask[nbBits];
416 bitD->
ptr = (
const char*)&zeroFilled;
434 if (bitD->
ptr - nbBytes < bitD->start) {
438 bitD->
ptr -= nbBytes;
453#if defined (__cplusplus)
MEM_STATIC unsigned ZSTD_highbit32(U32 val)
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 size_t BIT_readBits(BIT_DStream_t *bitD, unsigned nbBits)
FORCE_INLINE_TEMPLATE size_t BIT_getMiddleBits(BitContainerType bitContainer, U32 const start, U32 const nbBits)
MEM_STATIC void BIT_flushBits(BIT_CStream_t *bitC)
FORCE_INLINE_TEMPLATE size_t BIT_getUpperBits(BitContainerType bitContainer, U32 const start)
@ BIT_DStream_endOfBuffer
FORCE_INLINE_TEMPLATE size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
FORCE_INLINE_TEMPLATE void BIT_skipBits(BIT_DStream_t *bitD, U32 nbBits)
MEM_STATIC size_t BIT_initCStream(BIT_CStream_t *bitC, void *dstBuffer, size_t dstCapacity)
FORCE_INLINE_TEMPLATE size_t BIT_lookBits(const BIT_DStream_t *bitD, U32 nbBits)
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)
MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t *bitD)
MEM_STATIC void BIT_addBitsFast(BIT_CStream_t *bitC, size_t value, unsigned nbBits)
MEM_STATIC BIT_DStream_status BIT_reloadDStream_internal(BIT_DStream_t *bitD)
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 FORCE_INLINE_TEMPLATE
#define DEBUG_STATIC_ASSERT(c)
#define assert(condition)
MEM_STATIC size_t MEM_readLEST(const void *memPtr)
MEM_STATIC void MEM_writeLEST(void *memPtr, size_t val)
BitContainerType bitContainer
#define ZSTD_memset(p, v, l)