blob: c7a7644656308a7fc47a04618f585e94dc5ca3cb [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 Collet202082f2017-04-28 16:56:39 -07005 Copyright (C) 2013-2017, 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 Collet202082f2017-04-28 16:56:39 -070056/*-*************************************
57* Debug
58***************************************/
59#if defined(BIT_DEBUG) && (BIT_DEBUG>=1)
60# include <assert.h>
61#else
62# define assert(condition) ((void)0)
63#endif
64
65
Yann Collet74bd1192016-03-26 17:50:26 +010066/*=========================================
67* Target specific
68=========================================*/
69#if defined(__BMI__) && defined(__GNUC__)
70# include <immintrin.h> /* support for bextr (experimental) */
71#endif
72
Sean Purcelld44703d2017-03-01 14:36:25 -080073#define STREAM_ACCUMULATOR_MIN_32 25
74#define STREAM_ACCUMULATOR_MIN_64 57
75#define STREAM_ACCUMULATOR_MIN ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64))
Yann Collet74bd1192016-03-26 17:50:26 +010076
Yann Colletae7aa062016-02-03 02:46:46 +010077/*-******************************************
78* bitStream encoding API (write forward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +010079********************************************/
Yann Colletd1d210f2016-03-19 12:12:07 +010080/* bitStream can mix input from multiple sources.
81* A critical property of these streams is that they encode and decode in **reverse** direction.
82* 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 +010083*/
84typedef struct
85{
86 size_t bitContainer;
87 int bitPos;
88 char* startPtr;
89 char* ptr;
90 char* endPtr;
91} BIT_CStream_t;
92
Yann Colletae7aa062016-02-03 02:46:46 +010093MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity);
Yann Colletb1f3f4b2015-10-18 22:18:32 +010094MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
95MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC);
96MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
97
Yann Collet01e5b952016-03-19 14:14:31 +010098/* Start with initCStream, providing the size of buffer to write into.
99* bitStream will never write outside of this buffer.
Yann Collet1032fbe2016-05-11 18:30:24 +0200100* `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code.
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100101*
Yann Collet01e5b952016-03-19 14:14:31 +0100102* bits are first added to a local register.
103* Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
104* Writing data into memory is an explicit operation, performed by the flushBits function.
105* Hence keep track how many bits are potentially stored into local register to avoid register overflow.
106* After a flushBits, a maximum of 7 bits might still be stored into local register.
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100107*
Yann Collet01e5b952016-03-19 14:14:31 +0100108* Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers.
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100109*
Yann Collet01e5b952016-03-19 14:14:31 +0100110* Last operation is to close the bitStream.
111* The function returns the final size of CStream in bytes.
112* If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100113*/
114
115
Yann Colletae7aa062016-02-03 02:46:46 +0100116/*-********************************************
117* bitStream decoding API (read backward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100118**********************************************/
119typedef struct
120{
121 size_t bitContainer;
122 unsigned bitsConsumed;
123 const char* ptr;
124 const char* start;
125} BIT_DStream_t;
126
127typedef enum { BIT_DStream_unfinished = 0,
128 BIT_DStream_endOfBuffer = 1,
129 BIT_DStream_completed = 2,
130 BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
131 /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
132
133MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
134MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
135MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
136MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
137
138
Yann Collet01e5b952016-03-19 14:14:31 +0100139/* Start by invoking BIT_initDStream().
140* A chunk of the bitStream is then stored into a local register.
141* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
142* You can then retrieve bitFields stored into the local register, **in reverse order**.
143* Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
Yann Collet1032fbe2016-05-11 18:30:24 +0200144* 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 +0100145* Otherwise, it can be less than that, so proceed accordingly.
Yann Colletb21ce152016-03-24 01:27:55 +0100146* Checking if DStream has reached its end can be performed with BIT_endOfDStream().
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100147*/
148
149
Yann Colletae7aa062016-02-03 02:46:46 +0100150/*-****************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100151* unsafe API
152******************************************/
153MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
154/* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */
155
156MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
157/* unsafe version; does not check buffer overflow */
158
159MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
160/* faster, but works only if nbBits >= 1 */
161
162
163
Yann Colletae7aa062016-02-03 02:46:46 +0100164/*-**************************************************************
Yann Collet6cf45da2016-03-23 14:18:37 +0100165* Internal functions
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100166****************************************************************/
167MEM_STATIC unsigned BIT_highbit32 (register U32 val)
168{
169# if defined(_MSC_VER) /* Visual */
Yann Collet4114f952015-10-30 06:40:22 +0100170 unsigned long r=0;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100171 _BitScanReverse ( &r, val );
172 return (unsigned) r;
173# elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
174 return 31 - __builtin_clz (val);
175# else /* Software version */
176 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 };
177 U32 v = val;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100178 v |= v >> 1;
179 v |= v >> 2;
180 v |= v >> 4;
181 v |= v >> 8;
182 v |= v >> 16;
Yann Colletf22a0d62016-05-20 14:36:36 +0200183 return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100184# endif
185}
186
Yann Collet6cf45da2016-03-23 14:18:37 +0100187/*===== Local Constants =====*/
188static 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 */
189
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100190
Yann Colletae7aa062016-02-03 02:46:46 +0100191/*-**************************************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100192* bitStream encoding
193****************************************************************/
Yann Collet01e5b952016-03-19 14:14:31 +0100194/*! BIT_initCStream() :
195 * `dstCapacity` must be > sizeof(void*)
196 * @return : 0 if success,
197 otherwise an error code (can be tested using ERR_isError() ) */
198MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* startPtr, size_t dstCapacity)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100199{
200 bitC->bitContainer = 0;
201 bitC->bitPos = 0;
202 bitC->startPtr = (char*)startPtr;
203 bitC->ptr = bitC->startPtr;
Yann Collet01e5b952016-03-19 14:14:31 +0100204 bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->ptr);
205 if (dstCapacity <= sizeof(bitC->ptr)) return ERROR(dstSize_tooSmall);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100206 return 0;
207}
208
Yann Collet01e5b952016-03-19 14:14:31 +0100209/*! BIT_addBits() :
210 can add up to 26 bits into `bitC`.
211 Does not check for register overflow ! */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100212MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
213{
Yann Collet6cf45da2016-03-23 14:18:37 +0100214 bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100215 bitC->bitPos += nbBits;
216}
217
Yann Colletd1d210f2016-03-19 12:12:07 +0100218/*! BIT_addBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100219 * works only if `value` is _clean_, meaning all high bits above nbBits are 0 */
220MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
221{
Yann Collet202082f2017-04-28 16:56:39 -0700222 assert((value>>nbBits) == 0);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100223 bitC->bitContainer |= value << bitC->bitPos;
224 bitC->bitPos += nbBits;
225}
226
Yann Colletd1d210f2016-03-19 12:12:07 +0100227/*! BIT_flushBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100228 * unsafe version; does not check buffer overflow */
229MEM_STATIC void BIT_flushBitsFast(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 bitC->bitPos &= 7;
Yann Collet00fd7a22015-11-28 16:03:22 +0100235 bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100236}
237
Yann Collet01e5b952016-03-19 14:14:31 +0100238/*! BIT_flushBits() :
239 * safe version; check for buffer overflow, and prevents it.
240 * note : does not signal buffer overflow. This will be revealed later on using BIT_closeCStream() */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100241MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)
242{
Yann Colletd64f4352016-03-21 00:07:42 +0100243 size_t const nbBytes = bitC->bitPos >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100244 MEM_writeLEST(bitC->ptr, bitC->bitContainer);
245 bitC->ptr += nbBytes;
246 if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr;
247 bitC->bitPos &= 7;
Yann Collet00fd7a22015-11-28 16:03:22 +0100248 bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100249}
250
Yann Colletd1d210f2016-03-19 12:12:07 +0100251/*! BIT_closeCStream() :
Yann Collet01e5b952016-03-19 14:14:31 +0100252 * @return : size of CStream, in bytes,
253 or 0 if it could not fit into dstBuffer */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100254MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
255{
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100256 BIT_addBitsFast(bitC, 1, 1); /* endMark */
257 BIT_flushBits(bitC);
258
Yann Collet01e5b952016-03-19 14:14:31 +0100259 if (bitC->ptr >= bitC->endPtr) return 0; /* doesn't fit within authorized budget : cancel */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100260
Yann Collet01e5b952016-03-19 14:14:31 +0100261 return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100262}
263
264
Yann Colletae7aa062016-02-03 02:46:46 +0100265/*-********************************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100266* bitStream decoding
267**********************************************************/
Yann Collet01e5b952016-03-19 14:14:31 +0100268/*! BIT_initDStream() :
269* Initialize a BIT_DStream_t.
270* `bitD` : a pointer to an already allocated BIT_DStream_t structure.
Yann Colletadd08d62016-03-23 01:32:41 +0100271* `srcSize` must be the *exact* size of the bitStream, in bytes.
Yann Collet01e5b952016-03-19 14:14:31 +0100272* @return : size of stream (== srcSize) or an errorCode if a problem is detected
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100273*/
274MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
275{
276 if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
277
Yann Collet1032fbe2016-05-11 18:30:24 +0200278 if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100279 bitD->start = (const char*)srcBuffer;
Yann Collet1032fbe2016-05-11 18:30:24 +0200280 bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100281 bitD->bitContainer = MEM_readLEST(bitD->ptr);
Yann Colletb21ce152016-03-24 01:27:55 +0100282 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
Yann Collet5397a662016-12-13 15:21:06 +0100283 bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
Yann Collet18c8f792016-06-12 22:51:52 +0200284 if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
Yann Colletae7aa062016-02-03 02:46:46 +0100285 } else {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100286 bitD->start = (const char*)srcBuffer;
287 bitD->ptr = bitD->start;
Yann Collet1ceb5a92016-05-12 13:50:13 +0200288 bitD->bitContainer = *(const BYTE*)(bitD->start);
289 switch(srcSize)
290 {
291 case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
292 case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
293 case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
294 case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
295 case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
296 case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
297 default:;
298 }
Yann Colletb21ce152016-03-24 01:27:55 +0100299 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
Yann Collet18c8f792016-06-12 22:51:52 +0200300 bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;
301 if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
Yann Collet1032fbe2016-05-11 18:30:24 +0200302 bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100303 }
304
305 return srcSize;
306}
307
Yann Collet1032fbe2016-05-11 18:30:24 +0200308MEM_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
Yann Collet3c017862016-03-23 14:09:51 +0100309{
Yann Collet1032fbe2016-05-11 18:30:24 +0200310 return bitContainer >> start;
Yann Collet3c017862016-03-23 14:09:51 +0100311}
312
Yann Collet1032fbe2016-05-11 18:30:24 +0200313MEM_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
Yann Collet3c017862016-03-23 14:09:51 +0100314{
Yann Collet5397a662016-12-13 15:21:06 +0100315#if defined(__BMI__) && defined(__GNUC__) && __GNUC__*1000+__GNUC_MINOR__ >= 4008 /* experimental */
Yann Collet6f9c0562016-05-01 10:26:30 +0200316# if defined(__x86_64__)
Yann Collet1032fbe2016-05-11 18:30:24 +0200317 if (sizeof(bitContainer)==8)
318 return _bextr_u64(bitContainer, start, nbBits);
Yann Collet6f9c0562016-05-01 10:26:30 +0200319 else
320# endif
Yann Collet1032fbe2016-05-11 18:30:24 +0200321 return _bextr_u32(bitContainer, start, nbBits);
Yann Collet862a8592016-03-23 18:45:23 +0100322#else
Yann Collet1032fbe2016-05-11 18:30:24 +0200323 return (bitContainer >> start) & BIT_mask[nbBits];
Yann Collet862a8592016-03-23 18:45:23 +0100324#endif
Yann Collet3c017862016-03-23 14:09:51 +0100325}
326
Yann Collet1032fbe2016-05-11 18:30:24 +0200327MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
Yann Colletafab0202016-03-23 13:57:49 +0100328{
Yann Collet1032fbe2016-05-11 18:30:24 +0200329 return bitContainer & BIT_mask[nbBits];
Yann Colletafab0202016-03-23 13:57:49 +0100330}
331
Yann Collet01e5b952016-03-19 14:14:31 +0100332/*! BIT_lookBits() :
333 * Provides next n bits from local register.
Yann Collet1032fbe2016-05-11 18:30:24 +0200334 * local register is not modified.
Yann Collet01e5b952016-03-19 14:14:31 +0100335 * On 32-bits, maxNbBits==24.
336 * On 64-bits, maxNbBits==56.
337 * @return : value extracted
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100338 */
Yann Collet862a8592016-03-23 18:45:23 +0100339 MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100340{
Yann Collet1032fbe2016-05-11 18:30:24 +0200341#if defined(__BMI__) && defined(__GNUC__) /* experimental; fails if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8 */
342 return BIT_getMiddleBits(bitD->bitContainer, (sizeof(bitD->bitContainer)*8) - bitD->bitsConsumed - nbBits, nbBits);
Yann Collet862a8592016-03-23 18:45:23 +0100343#else
Yann Colletd1d210f2016-03-19 12:12:07 +0100344 U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100345 return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
Yann Collet862a8592016-03-23 18:45:23 +0100346#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100347}
348
Yann Collet01e5b952016-03-19 14:14:31 +0100349/*! BIT_lookBitsFast() :
Yann Collet202082f2017-04-28 16:56:39 -0700350 * unsafe version; only works if nbBits >= 1 */
Yann Colletadd08d62016-03-23 01:32:41 +0100351MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100352{
Yann Colletd1d210f2016-03-19 12:12:07 +0100353 U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
Yann Collet202082f2017-04-28 16:56:39 -0700354 assert(nbBits >= 1);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100355 return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
356}
357
358MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
359{
360 bitD->bitsConsumed += nbBits;
361}
362
Yann Collet01e5b952016-03-19 14:14:31 +0100363/*! BIT_readBits() :
Yann Colletb21ce152016-03-24 01:27:55 +0100364 * Read (consume) next n bits from local register and update.
365 * Pay attention to not read more than nbBits contained into local register.
Yann Collet01e5b952016-03-19 14:14:31 +0100366 * @return : extracted value.
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100367 */
368MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
369{
Yann Colletafab0202016-03-23 13:57:49 +0100370 size_t const value = BIT_lookBits(bitD, nbBits);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100371 BIT_skipBits(bitD, nbBits);
372 return value;
373}
374
Yann Collet01e5b952016-03-19 14:14:31 +0100375/*! BIT_readBitsFast() :
376* unsafe version; only works only if nbBits >= 1 */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100377MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
378{
Yann Colletafab0202016-03-23 13:57:49 +0100379 size_t const value = BIT_lookBitsFast(bitD, nbBits);
Yann Collet202082f2017-04-28 16:56:39 -0700380 assert(nbBits >= 1);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100381 BIT_skipBits(bitD, nbBits);
382 return value;
383}
384
Yann Collet01e5b952016-03-19 14:14:31 +0100385/*! BIT_reloadDStream() :
Yann Collet5397a662016-12-13 15:21:06 +0100386* Refill `bitD` from buffer previously set in BIT_initDStream() .
Yann Collet01e5b952016-03-19 14:14:31 +0100387* This function is safe, it guarantees it will not read beyond src buffer.
388* @return : status of `BIT_DStream_t` internal register.
Yann Collet5397a662016-12-13 15:21:06 +0100389 if status == BIT_DStream_unfinished, internal register is filled with >= (sizeof(bitD->bitContainer)*8 - 7) bits */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100390MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
391{
Nick Terrell5152fb22017-03-29 18:51:58 -0700392 if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should not happen => corruption detected */
393 return BIT_DStream_overflow;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100394
Yann Colletae7aa062016-02-03 02:46:46 +0100395 if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100396 bitD->ptr -= bitD->bitsConsumed >> 3;
397 bitD->bitsConsumed &= 7;
398 bitD->bitContainer = MEM_readLEST(bitD->ptr);
399 return BIT_DStream_unfinished;
400 }
Yann Colletae7aa062016-02-03 02:46:46 +0100401 if (bitD->ptr == bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100402 if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
403 return BIT_DStream_completed;
404 }
Yann Collet01e5b952016-03-19 14:14:31 +0100405 { U32 nbBytes = bitD->bitsConsumed >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100406 BIT_DStream_status result = BIT_DStream_unfinished;
Yann Colletae7aa062016-02-03 02:46:46 +0100407 if (bitD->ptr - nbBytes < bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100408 nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
409 result = BIT_DStream_endOfBuffer;
410 }
411 bitD->ptr -= nbBytes;
412 bitD->bitsConsumed -= nbBytes*8;
413 bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */
414 return result;
415 }
416}
417
Yann Colletd1d210f2016-03-19 12:12:07 +0100418/*! BIT_endOfDStream() :
Yann Collet01e5b952016-03-19 14:14:31 +0100419* @return Tells if DStream has exactly reached its end (all bits consumed).
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100420*/
421MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
422{
423 return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
424}
425
426#if defined (__cplusplus)
427}
428#endif
429
430#endif /* BITSTREAM_H_MODULE */