blob: 6b3f13df0d9a0c5e58e349d018824afd645ae2ef [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 Collet74bd1192016-03-26 17:50:26 +010056/*=========================================
57* Target specific
58=========================================*/
59#if defined(__BMI__) && defined(__GNUC__)
60# include <immintrin.h> /* support for bextr (experimental) */
61#endif
62
63
Yann Colletae7aa062016-02-03 02:46:46 +010064/*-******************************************
65* bitStream encoding API (write forward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +010066********************************************/
Yann Colletd1d210f2016-03-19 12:12:07 +010067/* bitStream can mix input from multiple sources.
68* A critical property of these streams is that they encode and decode in **reverse** direction.
69* 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 +010070*/
71typedef struct
72{
73 size_t bitContainer;
74 int bitPos;
75 char* startPtr;
76 char* ptr;
77 char* endPtr;
78} BIT_CStream_t;
79
Yann Colletae7aa062016-02-03 02:46:46 +010080MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity);
Yann Colletb1f3f4b2015-10-18 22:18:32 +010081MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
82MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC);
83MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
84
Yann Collet01e5b952016-03-19 14:14:31 +010085/* Start with initCStream, providing the size of buffer to write into.
86* bitStream will never write outside of this buffer.
87* `dstCapacity` must be >= sizeof(size_t), otherwise @return will be an error code.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010088*
Yann Collet01e5b952016-03-19 14:14:31 +010089* bits are first added to a local register.
90* Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
91* Writing data into memory is an explicit operation, performed by the flushBits function.
92* Hence keep track how many bits are potentially stored into local register to avoid register overflow.
93* After a flushBits, a maximum of 7 bits might still be stored into local register.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010094*
Yann Collet01e5b952016-03-19 14:14:31 +010095* Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010096*
Yann Collet01e5b952016-03-19 14:14:31 +010097* Last operation is to close the bitStream.
98* The function returns the final size of CStream in bytes.
99* If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100100*/
101
102
Yann Colletae7aa062016-02-03 02:46:46 +0100103/*-********************************************
104* bitStream decoding API (read backward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100105**********************************************/
106typedef struct
107{
108 size_t bitContainer;
109 unsigned bitsConsumed;
110 const char* ptr;
111 const char* start;
112} BIT_DStream_t;
113
114typedef enum { BIT_DStream_unfinished = 0,
115 BIT_DStream_endOfBuffer = 1,
116 BIT_DStream_completed = 2,
117 BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
118 /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
119
120MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
121MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
122MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
123MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
124
125
Yann Collet01e5b952016-03-19 14:14:31 +0100126/* Start by invoking BIT_initDStream().
127* A chunk of the bitStream is then stored into a local register.
128* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
129* You can then retrieve bitFields stored into the local register, **in reverse order**.
130* Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
131* A reload guarantee a minimum of ((8*sizeof(size_t))-7) bits when its result is BIT_DStream_unfinished.
132* Otherwise, it can be less than that, so proceed accordingly.
Yann Colletb21ce152016-03-24 01:27:55 +0100133* Checking if DStream has reached its end can be performed with BIT_endOfDStream().
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100134*/
135
136
Yann Colletae7aa062016-02-03 02:46:46 +0100137/*-****************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100138* unsafe API
139******************************************/
140MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
141/* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */
142
143MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
144/* unsafe version; does not check buffer overflow */
145
146MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
147/* faster, but works only if nbBits >= 1 */
148
149
150
Yann Colletae7aa062016-02-03 02:46:46 +0100151/*-**************************************************************
Yann Collet6cf45da2016-03-23 14:18:37 +0100152* Internal functions
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100153****************************************************************/
154MEM_STATIC unsigned BIT_highbit32 (register U32 val)
155{
156# if defined(_MSC_VER) /* Visual */
Yann Collet4114f952015-10-30 06:40:22 +0100157 unsigned long r=0;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100158 _BitScanReverse ( &r, val );
159 return (unsigned) r;
160# elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
161 return 31 - __builtin_clz (val);
162# else /* Software version */
163 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 };
164 U32 v = val;
165 unsigned r;
166 v |= v >> 1;
167 v |= v >> 2;
168 v |= v >> 4;
169 v |= v >> 8;
170 v |= v >> 16;
171 r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
172 return r;
173# endif
174}
175
Yann Collet6cf45da2016-03-23 14:18:37 +0100176/*===== Local Constants =====*/
177static 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 */
178
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100179
Yann Colletae7aa062016-02-03 02:46:46 +0100180/*-**************************************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100181* bitStream encoding
182****************************************************************/
Yann Collet01e5b952016-03-19 14:14:31 +0100183/*! BIT_initCStream() :
184 * `dstCapacity` must be > sizeof(void*)
185 * @return : 0 if success,
186 otherwise an error code (can be tested using ERR_isError() ) */
187MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* startPtr, size_t dstCapacity)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100188{
189 bitC->bitContainer = 0;
190 bitC->bitPos = 0;
191 bitC->startPtr = (char*)startPtr;
192 bitC->ptr = bitC->startPtr;
Yann Collet01e5b952016-03-19 14:14:31 +0100193 bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->ptr);
194 if (dstCapacity <= sizeof(bitC->ptr)) return ERROR(dstSize_tooSmall);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100195 return 0;
196}
197
Yann Collet01e5b952016-03-19 14:14:31 +0100198/*! BIT_addBits() :
199 can add up to 26 bits into `bitC`.
200 Does not check for register overflow ! */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100201MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
202{
Yann Collet6cf45da2016-03-23 14:18:37 +0100203 bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100204 bitC->bitPos += nbBits;
205}
206
Yann Colletd1d210f2016-03-19 12:12:07 +0100207/*! BIT_addBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100208 * works only if `value` is _clean_, meaning all high bits above nbBits are 0 */
209MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
210{
211 bitC->bitContainer |= value << bitC->bitPos;
212 bitC->bitPos += nbBits;
213}
214
Yann Colletd1d210f2016-03-19 12:12:07 +0100215/*! BIT_flushBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100216 * unsafe version; does not check buffer overflow */
217MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC)
218{
Yann Colletd64f4352016-03-21 00:07:42 +0100219 size_t const nbBytes = bitC->bitPos >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100220 MEM_writeLEST(bitC->ptr, bitC->bitContainer);
221 bitC->ptr += nbBytes;
222 bitC->bitPos &= 7;
Yann Collet00fd7a22015-11-28 16:03:22 +0100223 bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100224}
225
Yann Collet01e5b952016-03-19 14:14:31 +0100226/*! BIT_flushBits() :
227 * safe version; check for buffer overflow, and prevents it.
228 * note : does not signal buffer overflow. This will be revealed later on using BIT_closeCStream() */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100229MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)
230{
Yann Colletd64f4352016-03-21 00:07:42 +0100231 size_t const nbBytes = bitC->bitPos >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100232 MEM_writeLEST(bitC->ptr, bitC->bitContainer);
233 bitC->ptr += nbBytes;
234 if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr;
235 bitC->bitPos &= 7;
Yann Collet00fd7a22015-11-28 16:03:22 +0100236 bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100237}
238
Yann Colletd1d210f2016-03-19 12:12:07 +0100239/*! BIT_closeCStream() :
Yann Collet01e5b952016-03-19 14:14:31 +0100240 * @return : size of CStream, in bytes,
241 or 0 if it could not fit into dstBuffer */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100242MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
243{
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100244 BIT_addBitsFast(bitC, 1, 1); /* endMark */
245 BIT_flushBits(bitC);
246
Yann Collet01e5b952016-03-19 14:14:31 +0100247 if (bitC->ptr >= bitC->endPtr) return 0; /* doesn't fit within authorized budget : cancel */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100248
Yann Collet01e5b952016-03-19 14:14:31 +0100249 return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100250}
251
252
Yann Colletae7aa062016-02-03 02:46:46 +0100253/*-********************************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100254* bitStream decoding
255**********************************************************/
Yann Collet01e5b952016-03-19 14:14:31 +0100256/*! BIT_initDStream() :
257* Initialize a BIT_DStream_t.
258* `bitD` : a pointer to an already allocated BIT_DStream_t structure.
Yann Colletadd08d62016-03-23 01:32:41 +0100259* `srcSize` must be the *exact* size of the bitStream, in bytes.
Yann Collet01e5b952016-03-19 14:14:31 +0100260* @return : size of stream (== srcSize) or an errorCode if a problem is detected
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100261*/
262MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
263{
264 if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
265
Yann Colletae7aa062016-02-03 02:46:46 +0100266 if (srcSize >= sizeof(size_t)) { /* normal case */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100267 bitD->start = (const char*)srcBuffer;
268 bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t);
269 bitD->bitContainer = MEM_readLEST(bitD->ptr);
Yann Colletb21ce152016-03-24 01:27:55 +0100270 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
271 if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */
272 bitD->bitsConsumed = 8 - BIT_highbit32(lastByte); }
Yann Colletae7aa062016-02-03 02:46:46 +0100273 } else {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100274 bitD->start = (const char*)srcBuffer;
275 bitD->ptr = bitD->start;
276 bitD->bitContainer = *(const BYTE*)(bitD->start);
277 switch(srcSize)
278 {
279 case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);
280 case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);
281 case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);
282 case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24;
283 case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16;
284 case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8;
285 default:;
286 }
Yann Colletb21ce152016-03-24 01:27:55 +0100287 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
288 if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */
289 bitD->bitsConsumed = 8 - BIT_highbit32(lastByte); }
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100290 bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8;
291 }
292
293 return srcSize;
294}
295
Yann Collet3c017862016-03-23 14:09:51 +0100296MEM_STATIC size_t BIT_getUpperBits(size_t bitD, U32 const start)
297{
298 return bitD >> start;
299}
300
Yann Collet6cf45da2016-03-23 14:18:37 +0100301MEM_STATIC size_t BIT_getMiddleBits(size_t bitD, U32 const nbBits, U32 const start)
Yann Collet3c017862016-03-23 14:09:51 +0100302{
Yann Colletb21ce152016-03-24 01:27:55 +0100303#if defined(__BMI__) && defined(__GNUC__) /* experimental */
Yann Collet6f9c0562016-05-01 10:26:30 +0200304# if defined(__x86_64__)
305 if (sizeof(bitD)==8)
306 return _bextr_u64(bitD, start, nbBits);
307 else
308# endif
309 return _bextr_u32(bitD, start, nbBits);
Yann Collet862a8592016-03-23 18:45:23 +0100310#else
Yann Collet6cf45da2016-03-23 14:18:37 +0100311 return (bitD >> start) & BIT_mask[nbBits];
Yann Collet862a8592016-03-23 18:45:23 +0100312#endif
Yann Collet3c017862016-03-23 14:09:51 +0100313}
314
Yann Collet6cf45da2016-03-23 14:18:37 +0100315MEM_STATIC size_t BIT_getLowerBits(size_t bitD, U32 const nbBits)
Yann Colletafab0202016-03-23 13:57:49 +0100316{
Yann Collet6cf45da2016-03-23 14:18:37 +0100317 return bitD & BIT_mask[nbBits];
Yann Colletafab0202016-03-23 13:57:49 +0100318}
319
Yann Collet01e5b952016-03-19 14:14:31 +0100320/*! BIT_lookBits() :
321 * Provides next n bits from local register.
322 * local register is not modified (bits are still present for next read/look).
323 * On 32-bits, maxNbBits==24.
324 * On 64-bits, maxNbBits==56.
325 * @return : value extracted
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100326 */
Yann Collet862a8592016-03-23 18:45:23 +0100327 MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100328{
Yann Colletb21ce152016-03-24 01:27:55 +0100329#if defined(__BMI__) && defined(__GNUC__) /* experimental */
Yann Collet6f9c0562016-05-01 10:26:30 +0200330# if defined(__x86_64__)
331 if (sizeof(bitD->bitContainer)==8)
332 return _bextr_u64(bitD->bitContainer, 64 - bitD->bitsConsumed - nbBits, nbBits);
333 else
334# endif
335 return _bextr_u32(bitD->bitContainer, 32 - bitD->bitsConsumed - nbBits, nbBits);
Yann Collet862a8592016-03-23 18:45:23 +0100336#else
Yann Colletd1d210f2016-03-19 12:12:07 +0100337 U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100338 return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
Yann Collet862a8592016-03-23 18:45:23 +0100339#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100340}
341
Yann Collet01e5b952016-03-19 14:14:31 +0100342/*! BIT_lookBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100343* unsafe version; only works only if nbBits >= 1 */
Yann Colletadd08d62016-03-23 01:32:41 +0100344MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100345{
Yann Colletd1d210f2016-03-19 12:12:07 +0100346 U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100347 return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
348}
349
350MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
351{
352 bitD->bitsConsumed += nbBits;
353}
354
Yann Collet01e5b952016-03-19 14:14:31 +0100355/*! BIT_readBits() :
Yann Colletb21ce152016-03-24 01:27:55 +0100356 * Read (consume) next n bits from local register and update.
357 * Pay attention to not read more than nbBits contained into local register.
Yann Collet01e5b952016-03-19 14:14:31 +0100358 * @return : extracted value.
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100359 */
360MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
361{
Yann Colletafab0202016-03-23 13:57:49 +0100362 size_t const value = BIT_lookBits(bitD, nbBits);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100363 BIT_skipBits(bitD, nbBits);
364 return value;
365}
366
Yann Collet01e5b952016-03-19 14:14:31 +0100367/*! BIT_readBitsFast() :
368* unsafe version; only works only if nbBits >= 1 */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100369MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
370{
Yann Colletafab0202016-03-23 13:57:49 +0100371 size_t const value = BIT_lookBitsFast(bitD, nbBits);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100372 BIT_skipBits(bitD, nbBits);
373 return value;
374}
375
Yann Collet01e5b952016-03-19 14:14:31 +0100376/*! BIT_reloadDStream() :
377* Refill `BIT_DStream_t` from src buffer previously defined (see BIT_initDStream() ).
378* This function is safe, it guarantees it will not read beyond src buffer.
379* @return : status of `BIT_DStream_t` internal register.
380 if status == unfinished, internal register is filled with >= (sizeof(size_t)*8 - 7) bits */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100381MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
382{
383 if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
384 return BIT_DStream_overflow;
385
Yann Colletae7aa062016-02-03 02:46:46 +0100386 if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100387 bitD->ptr -= bitD->bitsConsumed >> 3;
388 bitD->bitsConsumed &= 7;
389 bitD->bitContainer = MEM_readLEST(bitD->ptr);
390 return BIT_DStream_unfinished;
391 }
Yann Colletae7aa062016-02-03 02:46:46 +0100392 if (bitD->ptr == bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100393 if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
394 return BIT_DStream_completed;
395 }
Yann Collet01e5b952016-03-19 14:14:31 +0100396 { U32 nbBytes = bitD->bitsConsumed >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100397 BIT_DStream_status result = BIT_DStream_unfinished;
Yann Colletae7aa062016-02-03 02:46:46 +0100398 if (bitD->ptr - nbBytes < bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100399 nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
400 result = BIT_DStream_endOfBuffer;
401 }
402 bitD->ptr -= nbBytes;
403 bitD->bitsConsumed -= nbBytes*8;
404 bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */
405 return result;
406 }
407}
408
Yann Colletd1d210f2016-03-19 12:12:07 +0100409/*! BIT_endOfDStream() :
Yann Collet01e5b952016-03-19 14:14:31 +0100410* @return Tells if DStream has exactly reached its end (all bits consumed).
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100411*/
412MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
413{
414 return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
415}
416
417#if defined (__cplusplus)
418}
419#endif
420
421#endif /* BITSTREAM_H_MODULE */