blob: d3873002ebd1a3944f8d47437bca8c0aebcd1b01 [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
Sean Purcelld44703d2017-03-01 14:36:25 -080063#define STREAM_ACCUMULATOR_MIN_32 25
64#define STREAM_ACCUMULATOR_MIN_64 57
65#define STREAM_ACCUMULATOR_MIN ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64))
Yann Collet74bd1192016-03-26 17:50:26 +010066
Yann Colletae7aa062016-02-03 02:46:46 +010067/*-******************************************
68* bitStream encoding API (write forward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +010069********************************************/
Yann Colletd1d210f2016-03-19 12:12:07 +010070/* bitStream can mix input from multiple sources.
71* A critical property of these streams is that they encode and decode in **reverse** direction.
72* 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 +010073*/
74typedef struct
75{
76 size_t bitContainer;
77 int bitPos;
78 char* startPtr;
79 char* ptr;
80 char* endPtr;
81} BIT_CStream_t;
82
Yann Colletae7aa062016-02-03 02:46:46 +010083MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity);
Yann Colletb1f3f4b2015-10-18 22:18:32 +010084MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
85MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC);
86MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
87
Yann Collet01e5b952016-03-19 14:14:31 +010088/* Start with initCStream, providing the size of buffer to write into.
89* bitStream will never write outside of this buffer.
Yann Collet1032fbe2016-05-11 18:30:24 +020090* `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010091*
Yann Collet01e5b952016-03-19 14:14:31 +010092* bits are first added to a local register.
93* Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
94* Writing data into memory is an explicit operation, performed by the flushBits function.
95* Hence keep track how many bits are potentially stored into local register to avoid register overflow.
96* After a flushBits, a maximum of 7 bits might still be stored into local register.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010097*
Yann Collet01e5b952016-03-19 14:14:31 +010098* Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010099*
Yann Collet01e5b952016-03-19 14:14:31 +0100100* Last operation is to close the bitStream.
101* The function returns the final size of CStream in bytes.
102* If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100103*/
104
105
Yann Colletae7aa062016-02-03 02:46:46 +0100106/*-********************************************
107* bitStream decoding API (read backward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100108**********************************************/
109typedef struct
110{
111 size_t bitContainer;
112 unsigned bitsConsumed;
113 const char* ptr;
114 const char* start;
115} BIT_DStream_t;
116
117typedef enum { BIT_DStream_unfinished = 0,
118 BIT_DStream_endOfBuffer = 1,
119 BIT_DStream_completed = 2,
120 BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
121 /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
122
123MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
124MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
125MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
126MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
127
128
Yann Collet01e5b952016-03-19 14:14:31 +0100129/* Start by invoking BIT_initDStream().
130* A chunk of the bitStream is then stored into a local register.
131* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
132* You can then retrieve bitFields stored into the local register, **in reverse order**.
133* Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
Yann Collet1032fbe2016-05-11 18:30:24 +0200134* A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished.
Yann Collet01e5b952016-03-19 14:14:31 +0100135* Otherwise, it can be less than that, so proceed accordingly.
Yann Colletb21ce152016-03-24 01:27:55 +0100136* Checking if DStream has reached its end can be performed with BIT_endOfDStream().
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100137*/
138
139
Yann Colletae7aa062016-02-03 02:46:46 +0100140/*-****************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100141* unsafe API
142******************************************/
143MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
144/* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */
145
146MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
147/* unsafe version; does not check buffer overflow */
148
149MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
150/* faster, but works only if nbBits >= 1 */
151
152
153
Yann Colletae7aa062016-02-03 02:46:46 +0100154/*-**************************************************************
Yann Collet6cf45da2016-03-23 14:18:37 +0100155* Internal functions
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100156****************************************************************/
157MEM_STATIC unsigned BIT_highbit32 (register U32 val)
158{
159# if defined(_MSC_VER) /* Visual */
Yann Collet4114f952015-10-30 06:40:22 +0100160 unsigned long r=0;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100161 _BitScanReverse ( &r, val );
162 return (unsigned) r;
163# elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
164 return 31 - __builtin_clz (val);
165# else /* Software version */
166 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 };
167 U32 v = val;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100168 v |= v >> 1;
169 v |= v >> 2;
170 v |= v >> 4;
171 v |= v >> 8;
172 v |= v >> 16;
Yann Colletf22a0d62016-05-20 14:36:36 +0200173 return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100174# endif
175}
176
Yann Collet6cf45da2016-03-23 14:18:37 +0100177/*===== Local Constants =====*/
178static 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 */
179
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100180
Yann Colletae7aa062016-02-03 02:46:46 +0100181/*-**************************************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100182* bitStream encoding
183****************************************************************/
Yann Collet01e5b952016-03-19 14:14:31 +0100184/*! BIT_initCStream() :
185 * `dstCapacity` must be > sizeof(void*)
186 * @return : 0 if success,
187 otherwise an error code (can be tested using ERR_isError() ) */
188MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* startPtr, size_t dstCapacity)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100189{
190 bitC->bitContainer = 0;
191 bitC->bitPos = 0;
192 bitC->startPtr = (char*)startPtr;
193 bitC->ptr = bitC->startPtr;
Yann Collet01e5b952016-03-19 14:14:31 +0100194 bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->ptr);
195 if (dstCapacity <= sizeof(bitC->ptr)) return ERROR(dstSize_tooSmall);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100196 return 0;
197}
198
Yann Collet01e5b952016-03-19 14:14:31 +0100199/*! BIT_addBits() :
200 can add up to 26 bits into `bitC`.
201 Does not check for register overflow ! */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100202MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
203{
Yann Collet6cf45da2016-03-23 14:18:37 +0100204 bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100205 bitC->bitPos += nbBits;
206}
207
Yann Colletd1d210f2016-03-19 12:12:07 +0100208/*! BIT_addBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100209 * works only if `value` is _clean_, meaning all high bits above nbBits are 0 */
210MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
211{
212 bitC->bitContainer |= value << bitC->bitPos;
213 bitC->bitPos += nbBits;
214}
215
Yann Colletd1d210f2016-03-19 12:12:07 +0100216/*! BIT_flushBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100217 * unsafe version; does not check buffer overflow */
218MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC)
219{
Yann Colletd64f4352016-03-21 00:07:42 +0100220 size_t const nbBytes = bitC->bitPos >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100221 MEM_writeLEST(bitC->ptr, bitC->bitContainer);
222 bitC->ptr += nbBytes;
223 bitC->bitPos &= 7;
Yann Collet00fd7a22015-11-28 16:03:22 +0100224 bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100225}
226
Yann Collet01e5b952016-03-19 14:14:31 +0100227/*! BIT_flushBits() :
228 * safe version; check for buffer overflow, and prevents it.
229 * note : does not signal buffer overflow. This will be revealed later on using BIT_closeCStream() */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100230MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)
231{
Yann Colletd64f4352016-03-21 00:07:42 +0100232 size_t const nbBytes = bitC->bitPos >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100233 MEM_writeLEST(bitC->ptr, bitC->bitContainer);
234 bitC->ptr += nbBytes;
235 if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr;
236 bitC->bitPos &= 7;
Yann Collet00fd7a22015-11-28 16:03:22 +0100237 bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100238}
239
Yann Colletd1d210f2016-03-19 12:12:07 +0100240/*! BIT_closeCStream() :
Yann Collet01e5b952016-03-19 14:14:31 +0100241 * @return : size of CStream, in bytes,
242 or 0 if it could not fit into dstBuffer */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100243MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
244{
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100245 BIT_addBitsFast(bitC, 1, 1); /* endMark */
246 BIT_flushBits(bitC);
247
Yann Collet01e5b952016-03-19 14:14:31 +0100248 if (bitC->ptr >= bitC->endPtr) return 0; /* doesn't fit within authorized budget : cancel */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100249
Yann Collet01e5b952016-03-19 14:14:31 +0100250 return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100251}
252
253
Yann Colletae7aa062016-02-03 02:46:46 +0100254/*-********************************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100255* bitStream decoding
256**********************************************************/
Yann Collet01e5b952016-03-19 14:14:31 +0100257/*! BIT_initDStream() :
258* Initialize a BIT_DStream_t.
259* `bitD` : a pointer to an already allocated BIT_DStream_t structure.
Yann Colletadd08d62016-03-23 01:32:41 +0100260* `srcSize` must be the *exact* size of the bitStream, in bytes.
Yann Collet01e5b952016-03-19 14:14:31 +0100261* @return : size of stream (== srcSize) or an errorCode if a problem is detected
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100262*/
263MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
264{
265 if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
266
Yann Collet1032fbe2016-05-11 18:30:24 +0200267 if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100268 bitD->start = (const char*)srcBuffer;
Yann Collet1032fbe2016-05-11 18:30:24 +0200269 bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100270 bitD->bitContainer = MEM_readLEST(bitD->ptr);
Yann Colletb21ce152016-03-24 01:27:55 +0100271 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
Yann Collet5397a662016-12-13 15:21:06 +0100272 bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
Yann Collet18c8f792016-06-12 22:51:52 +0200273 if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
Yann Colletae7aa062016-02-03 02:46:46 +0100274 } else {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100275 bitD->start = (const char*)srcBuffer;
276 bitD->ptr = bitD->start;
Yann Collet1ceb5a92016-05-12 13:50:13 +0200277 bitD->bitContainer = *(const BYTE*)(bitD->start);
278 switch(srcSize)
279 {
280 case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
281 case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
282 case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
283 case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
284 case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
285 case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
286 default:;
287 }
Yann Colletb21ce152016-03-24 01:27:55 +0100288 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
Yann Collet18c8f792016-06-12 22:51:52 +0200289 bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;
290 if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
Yann Collet1032fbe2016-05-11 18:30:24 +0200291 bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100292 }
293
294 return srcSize;
295}
296
Yann Collet1032fbe2016-05-11 18:30:24 +0200297MEM_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
Yann Collet3c017862016-03-23 14:09:51 +0100298{
Yann Collet1032fbe2016-05-11 18:30:24 +0200299 return bitContainer >> start;
Yann Collet3c017862016-03-23 14:09:51 +0100300}
301
Yann Collet1032fbe2016-05-11 18:30:24 +0200302MEM_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
Yann Collet3c017862016-03-23 14:09:51 +0100303{
Yann Collet5397a662016-12-13 15:21:06 +0100304#if defined(__BMI__) && defined(__GNUC__) && __GNUC__*1000+__GNUC_MINOR__ >= 4008 /* experimental */
Yann Collet6f9c0562016-05-01 10:26:30 +0200305# if defined(__x86_64__)
Yann Collet1032fbe2016-05-11 18:30:24 +0200306 if (sizeof(bitContainer)==8)
307 return _bextr_u64(bitContainer, start, nbBits);
Yann Collet6f9c0562016-05-01 10:26:30 +0200308 else
309# endif
Yann Collet1032fbe2016-05-11 18:30:24 +0200310 return _bextr_u32(bitContainer, start, nbBits);
Yann Collet862a8592016-03-23 18:45:23 +0100311#else
Yann Collet1032fbe2016-05-11 18:30:24 +0200312 return (bitContainer >> start) & BIT_mask[nbBits];
Yann Collet862a8592016-03-23 18:45:23 +0100313#endif
Yann Collet3c017862016-03-23 14:09:51 +0100314}
315
Yann Collet1032fbe2016-05-11 18:30:24 +0200316MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
Yann Colletafab0202016-03-23 13:57:49 +0100317{
Yann Collet1032fbe2016-05-11 18:30:24 +0200318 return bitContainer & BIT_mask[nbBits];
Yann Colletafab0202016-03-23 13:57:49 +0100319}
320
Yann Collet01e5b952016-03-19 14:14:31 +0100321/*! BIT_lookBits() :
322 * Provides next n bits from local register.
Yann Collet1032fbe2016-05-11 18:30:24 +0200323 * local register is not modified.
Yann Collet01e5b952016-03-19 14:14:31 +0100324 * On 32-bits, maxNbBits==24.
325 * On 64-bits, maxNbBits==56.
326 * @return : value extracted
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100327 */
Yann Collet862a8592016-03-23 18:45:23 +0100328 MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100329{
Yann Collet1032fbe2016-05-11 18:30:24 +0200330#if defined(__BMI__) && defined(__GNUC__) /* experimental; fails if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8 */
331 return BIT_getMiddleBits(bitD->bitContainer, (sizeof(bitD->bitContainer)*8) - bitD->bitsConsumed - nbBits, nbBits);
Yann Collet862a8592016-03-23 18:45:23 +0100332#else
Yann Colletd1d210f2016-03-19 12:12:07 +0100333 U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100334 return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
Yann Collet862a8592016-03-23 18:45:23 +0100335#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100336}
337
Yann Collet01e5b952016-03-19 14:14:31 +0100338/*! BIT_lookBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100339* unsafe version; only works only if nbBits >= 1 */
Yann Colletadd08d62016-03-23 01:32:41 +0100340MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100341{
Yann Colletd1d210f2016-03-19 12:12:07 +0100342 U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100343 return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
344}
345
346MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
347{
348 bitD->bitsConsumed += nbBits;
349}
350
Yann Collet01e5b952016-03-19 14:14:31 +0100351/*! BIT_readBits() :
Yann Colletb21ce152016-03-24 01:27:55 +0100352 * Read (consume) next n bits from local register and update.
353 * Pay attention to not read more than nbBits contained into local register.
Yann Collet01e5b952016-03-19 14:14:31 +0100354 * @return : extracted value.
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100355 */
356MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
357{
Yann Colletafab0202016-03-23 13:57:49 +0100358 size_t const value = BIT_lookBits(bitD, nbBits);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100359 BIT_skipBits(bitD, nbBits);
360 return value;
361}
362
Yann Collet01e5b952016-03-19 14:14:31 +0100363/*! BIT_readBitsFast() :
364* unsafe version; only works only if nbBits >= 1 */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100365MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
366{
Yann Colletafab0202016-03-23 13:57:49 +0100367 size_t const value = BIT_lookBitsFast(bitD, nbBits);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100368 BIT_skipBits(bitD, nbBits);
369 return value;
370}
371
Yann Collet01e5b952016-03-19 14:14:31 +0100372/*! BIT_reloadDStream() :
Yann Collet5397a662016-12-13 15:21:06 +0100373* Refill `bitD` from buffer previously set in BIT_initDStream() .
Yann Collet01e5b952016-03-19 14:14:31 +0100374* This function is safe, it guarantees it will not read beyond src buffer.
375* @return : status of `BIT_DStream_t` internal register.
Yann Collet5397a662016-12-13 15:21:06 +0100376 if status == BIT_DStream_unfinished, internal register is filled with >= (sizeof(bitD->bitContainer)*8 - 7) bits */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100377MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
378{
Yann Colletf22a0d62016-05-20 14:36:36 +0200379 if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should not happen => corruption detected */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100380 return BIT_DStream_overflow;
381
Yann Colletae7aa062016-02-03 02:46:46 +0100382 if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100383 bitD->ptr -= bitD->bitsConsumed >> 3;
384 bitD->bitsConsumed &= 7;
385 bitD->bitContainer = MEM_readLEST(bitD->ptr);
386 return BIT_DStream_unfinished;
387 }
Yann Colletae7aa062016-02-03 02:46:46 +0100388 if (bitD->ptr == bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100389 if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
390 return BIT_DStream_completed;
391 }
Yann Collet01e5b952016-03-19 14:14:31 +0100392 { U32 nbBytes = bitD->bitsConsumed >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100393 BIT_DStream_status result = BIT_DStream_unfinished;
Yann Colletae7aa062016-02-03 02:46:46 +0100394 if (bitD->ptr - nbBytes < bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100395 nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
396 result = BIT_DStream_endOfBuffer;
397 }
398 bitD->ptr -= nbBytes;
399 bitD->bitsConsumed -= nbBytes*8;
400 bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */
401 return result;
402 }
403}
404
Yann Colletd1d210f2016-03-19 12:12:07 +0100405/*! BIT_endOfDStream() :
Yann Collet01e5b952016-03-19 14:14:31 +0100406* @return Tells if DStream has exactly reached its end (all bits consumed).
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100407*/
408MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
409{
410 return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
411}
412
413#if defined (__cplusplus)
414}
415#endif
416
417#endif /* BITSTREAM_H_MODULE */