blob: 749dc02f09a581cf38aa7ec27d5c74d1c7b886bf [file] [log] [blame]
Yann Colletb1f3f4b2015-10-18 22:18:32 +01001/* ******************************************************************
2 bitstream
Yann Colletae7aa062016-02-03 02:46:46 +01003 Part of FSE library
Yann Colletb1f3f4b2015-10-18 22:18:32 +01004 header file (to include)
Yann Colletae7aa062016-02-03 02:46:46 +01005 Copyright (C) 2013-2016, Yann Collet.
Yann Colletb1f3f4b2015-10-18 22:18:32 +01006
7 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are
11 met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above
16 copyright notice, this list of conditions and the following disclaimer
17 in the documentation and/or other materials provided with the
18 distribution.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 You can contact the author at :
33 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
Yann Colletb1f3f4b2015-10-18 22:18:32 +010034****************************************************************** */
35#ifndef BITSTREAM_H_MODULE
36#define BITSTREAM_H_MODULE
37
38#if defined (__cplusplus)
39extern "C" {
40#endif
41
42
43/*
Yann Collet01e5b952016-03-19 14:14:31 +010044* This API consists of small unitary functions, which must be inlined for best performance.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010045* Since link-time-optimization is not available for all compilers,
46* these functions are defined into a .h to be included.
47*/
48
Yann Colletae7aa062016-02-03 02:46:46 +010049/*-****************************************
50* Dependencies
Yann Colletb1f3f4b2015-10-18 22:18:32 +010051******************************************/
Yann Collet977f1f32016-01-21 15:38:47 +010052#include "mem.h" /* unaligned access routines */
53#include "error_private.h" /* error codes and messages */
Yann Colletb1f3f4b2015-10-18 22:18:32 +010054
55
Yann Colletae7aa062016-02-03 02:46:46 +010056/*-******************************************
57* bitStream encoding API (write forward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +010058********************************************/
Yann Colletd1d210f2016-03-19 12:12:07 +010059/* bitStream can mix input from multiple sources.
60* A critical property of these streams is that they encode and decode in **reverse** direction.
61* So the first bit sequence you add will be the last to be read, like a LIFO stack.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010062*/
63typedef struct
64{
65 size_t bitContainer;
66 int bitPos;
67 char* startPtr;
68 char* ptr;
69 char* endPtr;
70} BIT_CStream_t;
71
Yann Colletae7aa062016-02-03 02:46:46 +010072MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity);
Yann Colletb1f3f4b2015-10-18 22:18:32 +010073MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
74MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC);
75MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
76
Yann Collet01e5b952016-03-19 14:14:31 +010077/* Start with initCStream, providing the size of buffer to write into.
78* bitStream will never write outside of this buffer.
79* `dstCapacity` must be >= sizeof(size_t), otherwise @return will be an error code.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010080*
Yann Collet01e5b952016-03-19 14:14:31 +010081* bits are first added to a local register.
82* Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
83* Writing data into memory is an explicit operation, performed by the flushBits function.
84* Hence keep track how many bits are potentially stored into local register to avoid register overflow.
85* After a flushBits, a maximum of 7 bits might still be stored into local register.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010086*
Yann Collet01e5b952016-03-19 14:14:31 +010087* Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010088*
Yann Collet01e5b952016-03-19 14:14:31 +010089* Last operation is to close the bitStream.
90* The function returns the final size of CStream in bytes.
91* If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable)
Yann Colletb1f3f4b2015-10-18 22:18:32 +010092*/
93
94
Yann Colletae7aa062016-02-03 02:46:46 +010095/*-********************************************
96* bitStream decoding API (read backward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +010097**********************************************/
98typedef struct
99{
100 size_t bitContainer;
101 unsigned bitsConsumed;
102 const char* ptr;
103 const char* start;
104} BIT_DStream_t;
105
106typedef enum { BIT_DStream_unfinished = 0,
107 BIT_DStream_endOfBuffer = 1,
108 BIT_DStream_completed = 2,
109 BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
110 /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
111
112MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
113MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
114MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
115MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
116
117
Yann Collet01e5b952016-03-19 14:14:31 +0100118/* Start by invoking BIT_initDStream().
119* A chunk of the bitStream is then stored into a local register.
120* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
121* You can then retrieve bitFields stored into the local register, **in reverse order**.
122* Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
123* A reload guarantee a minimum of ((8*sizeof(size_t))-7) bits when its result is BIT_DStream_unfinished.
124* Otherwise, it can be less than that, so proceed accordingly.
125* Checking if DStream has reached its end can be performed with BIT_endOfDStream()
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100126*/
127
128
Yann Colletae7aa062016-02-03 02:46:46 +0100129/*-****************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100130* unsafe API
131******************************************/
132MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
133/* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */
134
135MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
136/* unsafe version; does not check buffer overflow */
137
138MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
139/* faster, but works only if nbBits >= 1 */
140
141
142
Yann Colletae7aa062016-02-03 02:46:46 +0100143/*-**************************************************************
Yann Collet6cf45da2016-03-23 14:18:37 +0100144* Internal functions
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100145****************************************************************/
146MEM_STATIC unsigned BIT_highbit32 (register U32 val)
147{
148# if defined(_MSC_VER) /* Visual */
Yann Collet4114f952015-10-30 06:40:22 +0100149 unsigned long r=0;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100150 _BitScanReverse ( &r, val );
151 return (unsigned) r;
152# elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
153 return 31 - __builtin_clz (val);
154# else /* Software version */
155 static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
156 U32 v = val;
157 unsigned r;
158 v |= v >> 1;
159 v |= v >> 2;
160 v |= v >> 4;
161 v |= v >> 8;
162 v |= v >> 16;
163 r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
164 return r;
165# endif
166}
167
Yann Collet6cf45da2016-03-23 14:18:37 +0100168/*===== Local Constants =====*/
169static const unsigned BIT_mask[] = { 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF }; /* up to 26 bits */
170
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100171
Yann Colletae7aa062016-02-03 02:46:46 +0100172/*-**************************************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100173* bitStream encoding
174****************************************************************/
Yann Collet01e5b952016-03-19 14:14:31 +0100175/*! BIT_initCStream() :
176 * `dstCapacity` must be > sizeof(void*)
177 * @return : 0 if success,
178 otherwise an error code (can be tested using ERR_isError() ) */
179MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* startPtr, size_t dstCapacity)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100180{
181 bitC->bitContainer = 0;
182 bitC->bitPos = 0;
183 bitC->startPtr = (char*)startPtr;
184 bitC->ptr = bitC->startPtr;
Yann Collet01e5b952016-03-19 14:14:31 +0100185 bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->ptr);
186 if (dstCapacity <= sizeof(bitC->ptr)) return ERROR(dstSize_tooSmall);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100187 return 0;
188}
189
Yann Collet01e5b952016-03-19 14:14:31 +0100190/*! BIT_addBits() :
191 can add up to 26 bits into `bitC`.
192 Does not check for register overflow ! */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100193MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
194{
Yann Collet6cf45da2016-03-23 14:18:37 +0100195 bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100196 bitC->bitPos += nbBits;
197}
198
Yann Colletd1d210f2016-03-19 12:12:07 +0100199/*! BIT_addBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100200 * works only if `value` is _clean_, meaning all high bits above nbBits are 0 */
201MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
202{
203 bitC->bitContainer |= value << bitC->bitPos;
204 bitC->bitPos += nbBits;
205}
206
Yann Colletd1d210f2016-03-19 12:12:07 +0100207/*! BIT_flushBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100208 * unsafe version; does not check buffer overflow */
209MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC)
210{
Yann Colletd64f4352016-03-21 00:07:42 +0100211 size_t const nbBytes = bitC->bitPos >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100212 MEM_writeLEST(bitC->ptr, bitC->bitContainer);
213 bitC->ptr += nbBytes;
214 bitC->bitPos &= 7;
Yann Collet00fd7a22015-11-28 16:03:22 +0100215 bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100216}
217
Yann Collet01e5b952016-03-19 14:14:31 +0100218/*! BIT_flushBits() :
219 * safe version; check for buffer overflow, and prevents it.
220 * note : does not signal buffer overflow. This will be revealed later on using BIT_closeCStream() */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100221MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)
222{
Yann Colletd64f4352016-03-21 00:07:42 +0100223 size_t const nbBytes = bitC->bitPos >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100224 MEM_writeLEST(bitC->ptr, bitC->bitContainer);
225 bitC->ptr += nbBytes;
226 if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr;
227 bitC->bitPos &= 7;
Yann Collet00fd7a22015-11-28 16:03:22 +0100228 bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100229}
230
Yann Colletd1d210f2016-03-19 12:12:07 +0100231/*! BIT_closeCStream() :
Yann Collet01e5b952016-03-19 14:14:31 +0100232 * @return : size of CStream, in bytes,
233 or 0 if it could not fit into dstBuffer */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100234MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
235{
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100236 BIT_addBitsFast(bitC, 1, 1); /* endMark */
237 BIT_flushBits(bitC);
238
Yann Collet01e5b952016-03-19 14:14:31 +0100239 if (bitC->ptr >= bitC->endPtr) return 0; /* doesn't fit within authorized budget : cancel */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100240
Yann Collet01e5b952016-03-19 14:14:31 +0100241 return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100242}
243
244
Yann Colletae7aa062016-02-03 02:46:46 +0100245/*-********************************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100246* bitStream decoding
247**********************************************************/
Yann Collet01e5b952016-03-19 14:14:31 +0100248/*! BIT_initDStream() :
249* Initialize a BIT_DStream_t.
250* `bitD` : a pointer to an already allocated BIT_DStream_t structure.
Yann Colletadd08d62016-03-23 01:32:41 +0100251* `srcSize` must be the *exact* size of the bitStream, in bytes.
Yann Collet01e5b952016-03-19 14:14:31 +0100252* @return : size of stream (== srcSize) or an errorCode if a problem is detected
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100253*/
254MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
255{
256 if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
257
Yann Colletae7aa062016-02-03 02:46:46 +0100258 if (srcSize >= sizeof(size_t)) { /* normal case */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100259 U32 contain32;
260 bitD->start = (const char*)srcBuffer;
261 bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t);
262 bitD->bitContainer = MEM_readLEST(bitD->ptr);
263 contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
264 if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
265 bitD->bitsConsumed = 8 - BIT_highbit32(contain32);
Yann Colletae7aa062016-02-03 02:46:46 +0100266 } else {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100267 U32 contain32;
268 bitD->start = (const char*)srcBuffer;
269 bitD->ptr = bitD->start;
270 bitD->bitContainer = *(const BYTE*)(bitD->start);
271 switch(srcSize)
272 {
273 case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);
274 case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);
275 case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);
276 case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24;
277 case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16;
278 case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8;
279 default:;
280 }
281 contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
282 if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
283 bitD->bitsConsumed = 8 - BIT_highbit32(contain32);
284 bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8;
285 }
286
287 return srcSize;
288}
289
Yann Collet3c017862016-03-23 14:09:51 +0100290MEM_STATIC size_t BIT_getUpperBits(size_t bitD, U32 const start)
291{
292 return bitD >> start;
293}
294
Yann Collet862a8592016-03-23 18:45:23 +0100295#include <immintrin.h>
Yann Collet6cf45da2016-03-23 14:18:37 +0100296MEM_STATIC size_t BIT_getMiddleBits(size_t bitD, U32 const nbBits, U32 const start)
Yann Collet3c017862016-03-23 14:09:51 +0100297{
Yann Collet862a8592016-03-23 18:45:23 +0100298#if defined(__BMI__) && defined(__GNUC__)
299 return __builtin_ia32_bextr_u64(bitD, (nbBits<<8) | start );
300#else
Yann Collet6cf45da2016-03-23 14:18:37 +0100301 return (bitD >> start) & BIT_mask[nbBits];
Yann Collet862a8592016-03-23 18:45:23 +0100302#endif
Yann Collet3c017862016-03-23 14:09:51 +0100303}
304
Yann Collet6cf45da2016-03-23 14:18:37 +0100305MEM_STATIC size_t BIT_getLowerBits(size_t bitD, U32 const nbBits)
Yann Colletafab0202016-03-23 13:57:49 +0100306{
Yann Collet6cf45da2016-03-23 14:18:37 +0100307 return bitD & BIT_mask[nbBits];
Yann Colletafab0202016-03-23 13:57:49 +0100308}
309
Yann Collet01e5b952016-03-19 14:14:31 +0100310/*! BIT_lookBits() :
311 * Provides next n bits from local register.
312 * local register is not modified (bits are still present for next read/look).
313 * On 32-bits, maxNbBits==24.
314 * On 64-bits, maxNbBits==56.
315 * @return : value extracted
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100316 */
Yann Collet862a8592016-03-23 18:45:23 +0100317 MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100318{
Yann Collet862a8592016-03-23 18:45:23 +0100319#if defined(__BMI__) && defined(__GNUC__)
320 return __builtin_ia32_bextr_u64(bitD->bitContainer, (nbBits<<8) | (64 - bitD->bitsConsumed - nbBits) );
321#else
Yann Colletd1d210f2016-03-19 12:12:07 +0100322 U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100323 return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
Yann Collet862a8592016-03-23 18:45:23 +0100324 //return (bitD->bitContainer >> (64 - bitD->bitsConsumed - nbBits)) & BIT_mask[nbBits];
325#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100326}
327
Yann Collet01e5b952016-03-19 14:14:31 +0100328/*! BIT_lookBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100329* unsafe version; only works only if nbBits >= 1 */
Yann Colletadd08d62016-03-23 01:32:41 +0100330MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100331{
Yann Colletd1d210f2016-03-19 12:12:07 +0100332 U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100333 return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
334}
335
336MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
337{
338 bitD->bitsConsumed += nbBits;
339}
340
Yann Collet01e5b952016-03-19 14:14:31 +0100341/*! BIT_readBits() :
342 * Read next n bits from local register.
343 * pay attention to not read more than nbBits contained into local register.
344 * @return : extracted value.
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100345 */
346MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
347{
Yann Colletafab0202016-03-23 13:57:49 +0100348 size_t const value = BIT_lookBits(bitD, nbBits);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100349 BIT_skipBits(bitD, nbBits);
350 return value;
351}
352
Yann Collet01e5b952016-03-19 14:14:31 +0100353/*! BIT_readBitsFast() :
354* unsafe version; only works only if nbBits >= 1 */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100355MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
356{
Yann Colletafab0202016-03-23 13:57:49 +0100357 size_t const value = BIT_lookBitsFast(bitD, nbBits);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100358 BIT_skipBits(bitD, nbBits);
359 return value;
360}
361
Yann Collet01e5b952016-03-19 14:14:31 +0100362/*! BIT_reloadDStream() :
363* Refill `BIT_DStream_t` from src buffer previously defined (see BIT_initDStream() ).
364* This function is safe, it guarantees it will not read beyond src buffer.
365* @return : status of `BIT_DStream_t` internal register.
366 if status == unfinished, internal register is filled with >= (sizeof(size_t)*8 - 7) bits */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100367MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
368{
369 if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
370 return BIT_DStream_overflow;
371
Yann Colletae7aa062016-02-03 02:46:46 +0100372 if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100373 bitD->ptr -= bitD->bitsConsumed >> 3;
374 bitD->bitsConsumed &= 7;
375 bitD->bitContainer = MEM_readLEST(bitD->ptr);
376 return BIT_DStream_unfinished;
377 }
Yann Colletae7aa062016-02-03 02:46:46 +0100378 if (bitD->ptr == bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100379 if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
380 return BIT_DStream_completed;
381 }
Yann Collet01e5b952016-03-19 14:14:31 +0100382 { U32 nbBytes = bitD->bitsConsumed >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100383 BIT_DStream_status result = BIT_DStream_unfinished;
Yann Colletae7aa062016-02-03 02:46:46 +0100384 if (bitD->ptr - nbBytes < bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100385 nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
386 result = BIT_DStream_endOfBuffer;
387 }
388 bitD->ptr -= nbBytes;
389 bitD->bitsConsumed -= nbBytes*8;
390 bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */
391 return result;
392 }
393}
394
Yann Colletd1d210f2016-03-19 12:12:07 +0100395/*! BIT_endOfDStream() :
Yann Collet01e5b952016-03-19 14:14:31 +0100396* @return Tells if DStream has exactly reached its end (all bits consumed).
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100397*/
398MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
399{
400 return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
401}
402
403#if defined (__cplusplus)
404}
405#endif
406
407#endif /* BITSTREAM_H_MODULE */