blob: 19192dd9163ca80d105c861578102e3bfdd4ee91 [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
Yann Colletb1f3f4b2015-10-18 22:18:32 +010042/*
Yann Collet01e5b952016-03-19 14:14:31 +010043* This API consists of small unitary functions, which must be inlined for best performance.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010044* Since link-time-optimization is not available for all compilers,
45* these functions are defined into a .h to be included.
46*/
47
Yann Colletae7aa062016-02-03 02:46:46 +010048/*-****************************************
49* Dependencies
Yann Colletb1f3f4b2015-10-18 22:18:32 +010050******************************************/
Yann Collet977f1f32016-01-21 15:38:47 +010051#include "mem.h" /* unaligned access routines */
52#include "error_private.h" /* error codes and messages */
Yann Colletb1f3f4b2015-10-18 22:18:32 +010053
54
Yann Collet202082f2017-04-28 16:56:39 -070055/*-*************************************
56* Debug
57***************************************/
58#if defined(BIT_DEBUG) && (BIT_DEBUG>=1)
59# include <assert.h>
60#else
Yann Collet58e8d792017-06-02 18:20:48 -070061# ifndef assert
62# define assert(condition) ((void)0)
63# endif
Yann Collet202082f2017-04-28 16:56:39 -070064#endif
65
66
Yann Collet74bd1192016-03-26 17:50:26 +010067/*=========================================
68* Target specific
69=========================================*/
70#if defined(__BMI__) && defined(__GNUC__)
71# include <immintrin.h> /* support for bextr (experimental) */
72#endif
73
Sean Purcelld44703d2017-03-01 14:36:25 -080074#define STREAM_ACCUMULATOR_MIN_32 25
75#define STREAM_ACCUMULATOR_MIN_64 57
76#define STREAM_ACCUMULATOR_MIN ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64))
Yann Collet74bd1192016-03-26 17:50:26 +010077
Yann Collet8c910d22017-06-03 01:15:02 -070078
Yann Colletae7aa062016-02-03 02:46:46 +010079/*-******************************************
80* bitStream encoding API (write forward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +010081********************************************/
Yann Colletd1d210f2016-03-19 12:12:07 +010082/* bitStream can mix input from multiple sources.
Yann Colletb71363b2017-07-19 01:05:40 -070083 * A critical property of these streams is that they encode and decode in **reverse** direction.
84 * So the first bit sequence you add will be the last to be read, like a LIFO stack.
85 */
Yann Colletb1f3f4b2015-10-18 22:18:32 +010086typedef struct
87{
88 size_t bitContainer;
Yann Colletf39a6732017-05-01 09:56:03 -070089 unsigned bitPos;
Yann Colletb1f3f4b2015-10-18 22:18:32 +010090 char* startPtr;
91 char* ptr;
92 char* endPtr;
93} BIT_CStream_t;
94
Yann Colletae7aa062016-02-03 02:46:46 +010095MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity);
Yann Colletb1f3f4b2015-10-18 22:18:32 +010096MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
97MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC);
98MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
99
Yann Collet01e5b952016-03-19 14:14:31 +0100100/* Start with initCStream, providing the size of buffer to write into.
101* bitStream will never write outside of this buffer.
Yann Collet1032fbe2016-05-11 18:30:24 +0200102* `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code.
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100103*
Yann Collet01e5b952016-03-19 14:14:31 +0100104* bits are first added to a local register.
105* Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
106* Writing data into memory is an explicit operation, performed by the flushBits function.
107* Hence keep track how many bits are potentially stored into local register to avoid register overflow.
108* After a flushBits, a maximum of 7 bits might still be stored into local register.
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100109*
Yann Collet01e5b952016-03-19 14:14:31 +0100110* Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers.
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100111*
Yann Collet01e5b952016-03-19 14:14:31 +0100112* Last operation is to close the bitStream.
113* The function returns the final size of CStream in bytes.
114* If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100115*/
116
117
Yann Colletae7aa062016-02-03 02:46:46 +0100118/*-********************************************
119* bitStream decoding API (read backward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100120**********************************************/
121typedef struct
122{
123 size_t bitContainer;
124 unsigned bitsConsumed;
125 const char* ptr;
126 const char* start;
Yann Colletf39a6732017-05-01 09:56:03 -0700127 const char* limitPtr;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100128} BIT_DStream_t;
129
130typedef enum { BIT_DStream_unfinished = 0,
131 BIT_DStream_endOfBuffer = 1,
132 BIT_DStream_completed = 2,
133 BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
134 /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
135
136MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
137MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
138MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
139MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
140
141
Yann Collet01e5b952016-03-19 14:14:31 +0100142/* Start by invoking BIT_initDStream().
143* A chunk of the bitStream is then stored into a local register.
144* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
145* You can then retrieve bitFields stored into the local register, **in reverse order**.
146* Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
Yann Collet1032fbe2016-05-11 18:30:24 +0200147* 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 +0100148* Otherwise, it can be less than that, so proceed accordingly.
Yann Colletb21ce152016-03-24 01:27:55 +0100149* Checking if DStream has reached its end can be performed with BIT_endOfDStream().
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100150*/
151
152
Yann Colletae7aa062016-02-03 02:46:46 +0100153/*-****************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100154* unsafe API
155******************************************/
156MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
157/* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */
158
159MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
160/* unsafe version; does not check buffer overflow */
161
162MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
163/* faster, but works only if nbBits >= 1 */
164
165
166
Yann Colletae7aa062016-02-03 02:46:46 +0100167/*-**************************************************************
Yann Collet6cf45da2016-03-23 14:18:37 +0100168* Internal functions
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100169****************************************************************/
170MEM_STATIC unsigned BIT_highbit32 (register U32 val)
171{
Stella Laue50ed1f2017-08-22 11:55:42 -0700172 assert(val != 0);
173 {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100174# if defined(_MSC_VER) /* Visual */
Stella Laue50ed1f2017-08-22 11:55:42 -0700175 unsigned long r=0;
176 _BitScanReverse ( &r, val );
177 return (unsigned) r;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100178# elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
Stella Laue50ed1f2017-08-22 11:55:42 -0700179 return 31 - __builtin_clz (val);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100180# else /* Software version */
Stella Laue50ed1f2017-08-22 11:55:42 -0700181 static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29,
182 11, 14, 16, 18, 22, 25, 3, 30,
183 8, 12, 20, 28, 15, 17, 24, 7,
184 19, 27, 23, 6, 26, 5, 4, 31 };
185 U32 v = val;
186 v |= v >> 1;
187 v |= v >> 2;
188 v |= v >> 4;
189 v |= v >> 8;
190 v |= v >> 16;
191 return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100192# endif
Stella Laue50ed1f2017-08-22 11:55:42 -0700193 }
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100194}
195
Yann Collet6cf45da2016-03-23 14:18:37 +0100196/*===== Local Constants =====*/
Yann Colletf39a6732017-05-01 09:56:03 -0700197static const unsigned BIT_mask[] = { 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F,
198 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF,
199 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF,
200 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF }; /* up to 26 bits */
Yann Collet6cf45da2016-03-23 14:18:37 +0100201
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100202
Yann Colletae7aa062016-02-03 02:46:46 +0100203/*-**************************************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100204* bitStream encoding
205****************************************************************/
Yann Collet01e5b952016-03-19 14:14:31 +0100206/*! BIT_initCStream() :
Yann Colletf39a6732017-05-01 09:56:03 -0700207 * `dstCapacity` must be > sizeof(size_t)
Yann Collet01e5b952016-03-19 14:14:31 +0100208 * @return : 0 if success,
Yann Colletb71363b2017-07-19 01:05:40 -0700209 * otherwise an error code (can be tested using ERR_isError()) */
Yann Colletf39a6732017-05-01 09:56:03 -0700210MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC,
Yann Collet33c38b02017-05-01 11:12:30 -0700211 void* startPtr, size_t dstCapacity)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100212{
213 bitC->bitContainer = 0;
214 bitC->bitPos = 0;
215 bitC->startPtr = (char*)startPtr;
216 bitC->ptr = bitC->startPtr;
Yann Colletf39a6732017-05-01 09:56:03 -0700217 bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->bitContainer);
218 if (dstCapacity <= sizeof(bitC->bitContainer)) return ERROR(dstSize_tooSmall);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100219 return 0;
220}
221
Yann Collet01e5b952016-03-19 14:14:31 +0100222/*! BIT_addBits() :
Yann Colletb71363b2017-07-19 01:05:40 -0700223 * can add up to 26 bits into `bitC`.
224 * Note : does not check for register overflow ! */
Yann Colletf39a6732017-05-01 09:56:03 -0700225MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC,
Yann Collet33c38b02017-05-01 11:12:30 -0700226 size_t value, unsigned nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100227{
Yann Collet6cf45da2016-03-23 14:18:37 +0100228 bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100229 bitC->bitPos += nbBits;
230}
231
Yann Colletd1d210f2016-03-19 12:12:07 +0100232/*! BIT_addBitsFast() :
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100233 * works only if `value` is _clean_, meaning all high bits above nbBits are 0 */
Yann Colletf39a6732017-05-01 09:56:03 -0700234MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC,
Yann Collet33c38b02017-05-01 11:12:30 -0700235 size_t value, unsigned nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100236{
Yann Collet202082f2017-04-28 16:56:39 -0700237 assert((value>>nbBits) == 0);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100238 bitC->bitContainer |= value << bitC->bitPos;
239 bitC->bitPos += nbBits;
240}
241
Yann Colletd1d210f2016-03-19 12:12:07 +0100242/*! BIT_flushBitsFast() :
Yann Colletf39a6732017-05-01 09:56:03 -0700243 * assumption : bitContainer has not overflowed
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100244 * unsafe version; does not check buffer overflow */
245MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC)
246{
Yann Colletd64f4352016-03-21 00:07:42 +0100247 size_t const nbBytes = bitC->bitPos >> 3;
Yann Colletf39a6732017-05-01 09:56:03 -0700248 assert( bitC->bitPos <= (sizeof(bitC->bitContainer)*8) );
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100249 MEM_writeLEST(bitC->ptr, bitC->bitContainer);
250 bitC->ptr += nbBytes;
Yann Colletf39a6732017-05-01 09:56:03 -0700251 assert(bitC->ptr <= bitC->endPtr);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100252 bitC->bitPos &= 7;
Yann Colletf39a6732017-05-01 09:56:03 -0700253 bitC->bitContainer >>= nbBytes*8;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100254}
255
Yann Collet01e5b952016-03-19 14:14:31 +0100256/*! BIT_flushBits() :
Yann Colletf39a6732017-05-01 09:56:03 -0700257 * assumption : bitContainer has not overflowed
Yann Collet01e5b952016-03-19 14:14:31 +0100258 * safe version; check for buffer overflow, and prevents it.
Yann Collet33c38b02017-05-01 11:12:30 -0700259 * note : does not signal buffer overflow.
260 * overflow will be revealed later on using BIT_closeCStream() */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100261MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)
262{
Yann Colletd64f4352016-03-21 00:07:42 +0100263 size_t const nbBytes = bitC->bitPos >> 3;
Yann Colletf39a6732017-05-01 09:56:03 -0700264 assert( bitC->bitPos <= (sizeof(bitC->bitContainer)*8) );
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100265 MEM_writeLEST(bitC->ptr, bitC->bitContainer);
266 bitC->ptr += nbBytes;
267 if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr;
268 bitC->bitPos &= 7;
Yann Collet33c38b02017-05-01 11:12:30 -0700269 bitC->bitContainer >>= nbBytes*8;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100270}
271
Yann Colletd1d210f2016-03-19 12:12:07 +0100272/*! BIT_closeCStream() :
Yann Collet01e5b952016-03-19 14:14:31 +0100273 * @return : size of CStream, in bytes,
Yann Colletb71363b2017-07-19 01:05:40 -0700274 * or 0 if it could not fit into dstBuffer */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100275MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
276{
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100277 BIT_addBitsFast(bitC, 1, 1); /* endMark */
278 BIT_flushBits(bitC);
Yann Collet33c38b02017-05-01 11:12:30 -0700279 if (bitC->ptr >= bitC->endPtr) return 0; /* overflow detected */
Yann Collet01e5b952016-03-19 14:14:31 +0100280 return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100281}
282
283
Yann Colletae7aa062016-02-03 02:46:46 +0100284/*-********************************************************
Yann Colletb71363b2017-07-19 01:05:40 -0700285* bitStream decoding
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100286**********************************************************/
Yann Collet01e5b952016-03-19 14:14:31 +0100287/*! BIT_initDStream() :
Yann Colletb71363b2017-07-19 01:05:40 -0700288 * Initialize a BIT_DStream_t.
289 * `bitD` : a pointer to an already allocated BIT_DStream_t structure.
290 * `srcSize` must be the *exact* size of the bitStream, in bytes.
291 * @return : size of stream (== srcSize), or an errorCode if a problem is detected
292 */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100293MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
294{
295 if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
296
Yann Colletf39a6732017-05-01 09:56:03 -0700297 bitD->start = (const char*)srcBuffer;
298 bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer);
299
Yann Collet1032fbe2016-05-11 18:30:24 +0200300 if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */
Yann Collet1032fbe2016-05-11 18:30:24 +0200301 bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100302 bitD->bitContainer = MEM_readLEST(bitD->ptr);
Yann Colletb21ce152016-03-24 01:27:55 +0100303 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
Yann Collet5397a662016-12-13 15:21:06 +0100304 bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
Yann Collet18c8f792016-06-12 22:51:52 +0200305 if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
Yann Colletae7aa062016-02-03 02:46:46 +0100306 } else {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100307 bitD->ptr = bitD->start;
Yann Collet1ceb5a92016-05-12 13:50:13 +0200308 bitD->bitContainer = *(const BYTE*)(bitD->start);
309 switch(srcSize)
310 {
Yann Colletb71363b2017-07-19 01:05:40 -0700311 case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
312 /* fall-through */
Jos Collin05286fd2017-05-09 08:36:05 +0530313
Yann Colletb71363b2017-07-19 01:05:40 -0700314 case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
315 /* fall-through */
Yann Collet58e8d792017-06-02 18:20:48 -0700316
Yann Colletb71363b2017-07-19 01:05:40 -0700317 case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
318 /* fall-through */
Yann Collet58e8d792017-06-02 18:20:48 -0700319
Yann Colletb71363b2017-07-19 01:05:40 -0700320 case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
321 /* fall-through */
Yann Collet58e8d792017-06-02 18:20:48 -0700322
Yann Colletb71363b2017-07-19 01:05:40 -0700323 case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
324 /* fall-through */
Yann Collet58e8d792017-06-02 18:20:48 -0700325
Yann Colletb71363b2017-07-19 01:05:40 -0700326 case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
327 /* fall-through */
Yann Collet58e8d792017-06-02 18:20:48 -0700328
Yann Colletb71363b2017-07-19 01:05:40 -0700329 default: break;
Yann Collet1ceb5a92016-05-12 13:50:13 +0200330 }
Yann Colletb71363b2017-07-19 01:05:40 -0700331 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
332 bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;
333 if (lastByte == 0) return ERROR(corruption_detected); /* endMark not present */
334 }
Yann Collet1032fbe2016-05-11 18:30:24 +0200335 bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100336 }
337
338 return srcSize;
339}
340
Yann Collet1032fbe2016-05-11 18:30:24 +0200341MEM_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
Yann Collet3c017862016-03-23 14:09:51 +0100342{
Yann Collet1032fbe2016-05-11 18:30:24 +0200343 return bitContainer >> start;
Yann Collet3c017862016-03-23 14:09:51 +0100344}
345
Yann Collet1032fbe2016-05-11 18:30:24 +0200346MEM_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
Yann Collet3c017862016-03-23 14:09:51 +0100347{
Yann Collet5397a662016-12-13 15:21:06 +0100348#if defined(__BMI__) && defined(__GNUC__) && __GNUC__*1000+__GNUC_MINOR__ >= 4008 /* experimental */
Yann Collet6f9c0562016-05-01 10:26:30 +0200349# if defined(__x86_64__)
Yann Collet1032fbe2016-05-11 18:30:24 +0200350 if (sizeof(bitContainer)==8)
351 return _bextr_u64(bitContainer, start, nbBits);
Yann Collet6f9c0562016-05-01 10:26:30 +0200352 else
353# endif
Yann Collet1032fbe2016-05-11 18:30:24 +0200354 return _bextr_u32(bitContainer, start, nbBits);
Yann Collet862a8592016-03-23 18:45:23 +0100355#else
Yann Collet1032fbe2016-05-11 18:30:24 +0200356 return (bitContainer >> start) & BIT_mask[nbBits];
Yann Collet862a8592016-03-23 18:45:23 +0100357#endif
Yann Collet3c017862016-03-23 14:09:51 +0100358}
359
Yann Collet1032fbe2016-05-11 18:30:24 +0200360MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
Yann Colletafab0202016-03-23 13:57:49 +0100361{
Yann Collet1032fbe2016-05-11 18:30:24 +0200362 return bitContainer & BIT_mask[nbBits];
Yann Colletafab0202016-03-23 13:57:49 +0100363}
364
Yann Collet01e5b952016-03-19 14:14:31 +0100365/*! BIT_lookBits() :
366 * Provides next n bits from local register.
Yann Collet1032fbe2016-05-11 18:30:24 +0200367 * local register is not modified.
Yann Collet01e5b952016-03-19 14:14:31 +0100368 * On 32-bits, maxNbBits==24.
369 * On 64-bits, maxNbBits==56.
Yann Colletb71363b2017-07-19 01:05:40 -0700370 * @return : value extracted */
371MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100372{
Yann Collet1032fbe2016-05-11 18:30:24 +0200373#if defined(__BMI__) && defined(__GNUC__) /* experimental; fails if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8 */
374 return BIT_getMiddleBits(bitD->bitContainer, (sizeof(bitD->bitContainer)*8) - bitD->bitsConsumed - nbBits, nbBits);
Yann Collet862a8592016-03-23 18:45:23 +0100375#else
Yann Colletf39a6732017-05-01 09:56:03 -0700376 U32 const regMask = sizeof(bitD->bitContainer)*8 - 1;
377 return ((bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> 1) >> ((regMask-nbBits) & regMask);
Yann Collet862a8592016-03-23 18:45:23 +0100378#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100379}
380
Yann Collet01e5b952016-03-19 14:14:31 +0100381/*! BIT_lookBitsFast() :
Yann Collet202082f2017-04-28 16:56:39 -0700382 * unsafe version; only works if nbBits >= 1 */
Yann Colletadd08d62016-03-23 01:32:41 +0100383MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100384{
Yann Colletf39a6732017-05-01 09:56:03 -0700385 U32 const regMask = sizeof(bitD->bitContainer)*8 - 1;
Yann Collet202082f2017-04-28 16:56:39 -0700386 assert(nbBits >= 1);
Yann Colletf39a6732017-05-01 09:56:03 -0700387 return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> (((regMask+1)-nbBits) & regMask);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100388}
389
390MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
391{
392 bitD->bitsConsumed += nbBits;
393}
394
Yann Collet01e5b952016-03-19 14:14:31 +0100395/*! BIT_readBits() :
Yann Colletb21ce152016-03-24 01:27:55 +0100396 * Read (consume) next n bits from local register and update.
397 * Pay attention to not read more than nbBits contained into local register.
Yann Colletb71363b2017-07-19 01:05:40 -0700398 * @return : extracted value. */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100399MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
400{
Yann Colletafab0202016-03-23 13:57:49 +0100401 size_t const value = BIT_lookBits(bitD, nbBits);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100402 BIT_skipBits(bitD, nbBits);
403 return value;
404}
405
Yann Collet01e5b952016-03-19 14:14:31 +0100406/*! BIT_readBitsFast() :
Yann Colletb71363b2017-07-19 01:05:40 -0700407 * unsafe version; only works only if nbBits >= 1 */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100408MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
409{
Yann Colletafab0202016-03-23 13:57:49 +0100410 size_t const value = BIT_lookBitsFast(bitD, nbBits);
Yann Collet202082f2017-04-28 16:56:39 -0700411 assert(nbBits >= 1);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100412 BIT_skipBits(bitD, nbBits);
413 return value;
414}
415
Yann Collet01e5b952016-03-19 14:14:31 +0100416/*! BIT_reloadDStream() :
Yann Colletb71363b2017-07-19 01:05:40 -0700417 * Refill `bitD` from buffer previously set in BIT_initDStream() .
418 * This function is safe, it guarantees it will not read beyond src buffer.
419 * @return : status of `BIT_DStream_t` internal register.
Baldur Karlsson430a2fe2018-03-13 20:02:21 +0000420 * when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100421MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
422{
Yann Colletf39a6732017-05-01 09:56:03 -0700423 if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* overflow detected, like end of stream */
Nick Terrell5152fb22017-03-29 18:51:58 -0700424 return BIT_DStream_overflow;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100425
Yann Colletf39a6732017-05-01 09:56:03 -0700426 if (bitD->ptr >= bitD->limitPtr) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100427 bitD->ptr -= bitD->bitsConsumed >> 3;
428 bitD->bitsConsumed &= 7;
429 bitD->bitContainer = MEM_readLEST(bitD->ptr);
430 return BIT_DStream_unfinished;
431 }
Yann Colletae7aa062016-02-03 02:46:46 +0100432 if (bitD->ptr == bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100433 if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
434 return BIT_DStream_completed;
435 }
Yann Colletf39a6732017-05-01 09:56:03 -0700436 /* start < ptr < limitPtr */
Yann Collet01e5b952016-03-19 14:14:31 +0100437 { U32 nbBytes = bitD->bitsConsumed >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100438 BIT_DStream_status result = BIT_DStream_unfinished;
Yann Colletae7aa062016-02-03 02:46:46 +0100439 if (bitD->ptr - nbBytes < bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100440 nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
441 result = BIT_DStream_endOfBuffer;
442 }
443 bitD->ptr -= nbBytes;
444 bitD->bitsConsumed -= nbBytes*8;
Yann Colletf39a6732017-05-01 09:56:03 -0700445 bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD->bitContainer), otherwise bitD->ptr == bitD->start */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100446 return result;
447 }
448}
449
Yann Colletd1d210f2016-03-19 12:12:07 +0100450/*! BIT_endOfDStream() :
Yann Colletb71363b2017-07-19 01:05:40 -0700451 * @return : 1 if DStream has _exactly_ reached its end (all bits consumed).
452 */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100453MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
454{
455 return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
456}
457
458#if defined (__cplusplus)
459}
460#endif
461
462#endif /* BITSTREAM_H_MODULE */