blob: 2ae8b7c7394836d4335e6810735b0849d15c21af [file] [log] [blame]
Yann Colletb1f3f4b2015-10-18 22:18:32 +01001/* ******************************************************************
Nick Terrellac58c8d2020-03-26 15:19:05 -07002 * bitstream
3 * Part of FSE library
4 * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
5 *
6 * You can contact the author at :
7 * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
8 *
9 * This source code is licensed under both the BSD-style license (found in the
10 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11 * in the COPYING file in the root directory of this source tree).
12 * You may select, at your option, one of the above-listed licenses.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010013****************************************************************** */
14#ifndef BITSTREAM_H_MODULE
15#define BITSTREAM_H_MODULE
16
17#if defined (__cplusplus)
18extern "C" {
19#endif
20
Yann Colletb1f3f4b2015-10-18 22:18:32 +010021/*
Yann Collet01e5b952016-03-19 14:14:31 +010022* This API consists of small unitary functions, which must be inlined for best performance.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010023* Since link-time-optimization is not available for all compilers,
24* these functions are defined into a .h to be included.
25*/
26
Yann Colletae7aa062016-02-03 02:46:46 +010027/*-****************************************
28* Dependencies
Yann Colletb1f3f4b2015-10-18 22:18:32 +010029******************************************/
Yann Collet977f1f32016-01-21 15:38:47 +010030#include "mem.h" /* unaligned access routines */
Nick Terrell718f00f2019-11-25 18:26:19 -080031#include "compiler.h" /* UNLIKELY() */
Yann Colletfa41bcc2018-06-13 14:59:26 -040032#include "debug.h" /* assert(), DEBUGLOG(), RAWLOG() */
Yann Collet977f1f32016-01-21 15:38:47 +010033#include "error_private.h" /* error codes and messages */
Yann Colletb1f3f4b2015-10-18 22:18:32 +010034
35
Yann Collet74bd1192016-03-26 17:50:26 +010036/*=========================================
37* Target specific
38=========================================*/
39#if defined(__BMI__) && defined(__GNUC__)
40# include <immintrin.h> /* support for bextr (experimental) */
Joseph Chen3855bc42019-07-29 15:20:37 +080041#elif defined(__ICCARM__)
42# include <intrinsics.h>
Yann Collet74bd1192016-03-26 17:50:26 +010043#endif
44
Sean Purcelld44703d2017-03-01 14:36:25 -080045#define STREAM_ACCUMULATOR_MIN_32 25
46#define STREAM_ACCUMULATOR_MIN_64 57
47#define STREAM_ACCUMULATOR_MIN ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64))
Yann Collet74bd1192016-03-26 17:50:26 +010048
Yann Collet8c910d22017-06-03 01:15:02 -070049
Yann Colletae7aa062016-02-03 02:46:46 +010050/*-******************************************
51* bitStream encoding API (write forward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +010052********************************************/
Yann Colletd1d210f2016-03-19 12:12:07 +010053/* bitStream can mix input from multiple sources.
Yann Colletb71363b2017-07-19 01:05:40 -070054 * A critical property of these streams is that they encode and decode in **reverse** direction.
55 * So the first bit sequence you add will be the last to be read, like a LIFO stack.
56 */
Yann Colletfa41bcc2018-06-13 14:59:26 -040057typedef struct {
Yann Colletb1f3f4b2015-10-18 22:18:32 +010058 size_t bitContainer;
Yann Colletf39a6732017-05-01 09:56:03 -070059 unsigned bitPos;
Yann Colletb1f3f4b2015-10-18 22:18:32 +010060 char* startPtr;
61 char* ptr;
62 char* endPtr;
63} BIT_CStream_t;
64
Yann Colletae7aa062016-02-03 02:46:46 +010065MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity);
Yann Colletb1f3f4b2015-10-18 22:18:32 +010066MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
67MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC);
68MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
69
Yann Collet01e5b952016-03-19 14:14:31 +010070/* Start with initCStream, providing the size of buffer to write into.
71* bitStream will never write outside of this buffer.
Yann Collet1032fbe2016-05-11 18:30:24 +020072* `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010073*
Yann Collet01e5b952016-03-19 14:14:31 +010074* bits are first added to a local register.
75* Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
76* Writing data into memory is an explicit operation, performed by the flushBits function.
77* Hence keep track how many bits are potentially stored into local register to avoid register overflow.
78* After a flushBits, a maximum of 7 bits might still be stored into local register.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010079*
Yann Collet01e5b952016-03-19 14:14:31 +010080* Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers.
Yann Colletb1f3f4b2015-10-18 22:18:32 +010081*
Yann Collet01e5b952016-03-19 14:14:31 +010082* Last operation is to close the bitStream.
83* The function returns the final size of CStream in bytes.
84* If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable)
Yann Colletb1f3f4b2015-10-18 22:18:32 +010085*/
86
87
Yann Colletae7aa062016-02-03 02:46:46 +010088/*-********************************************
89* bitStream decoding API (read backward)
Yann Colletb1f3f4b2015-10-18 22:18:32 +010090**********************************************/
Yann Colletfa41bcc2018-06-13 14:59:26 -040091typedef struct {
Yann Colletb1f3f4b2015-10-18 22:18:32 +010092 size_t bitContainer;
93 unsigned bitsConsumed;
94 const char* ptr;
95 const char* start;
Yann Colletf39a6732017-05-01 09:56:03 -070096 const char* limitPtr;
Yann Colletb1f3f4b2015-10-18 22:18:32 +010097} BIT_DStream_t;
98
99typedef enum { BIT_DStream_unfinished = 0,
100 BIT_DStream_endOfBuffer = 1,
101 BIT_DStream_completed = 2,
102 BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
103 /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
104
105MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
106MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
107MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
108MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
109
110
Yann Collet01e5b952016-03-19 14:14:31 +0100111/* Start by invoking BIT_initDStream().
112* A chunk of the bitStream is then stored into a local register.
113* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
114* You can then retrieve bitFields stored into the local register, **in reverse order**.
115* Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
Yann Collet1032fbe2016-05-11 18:30:24 +0200116* 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 +0100117* Otherwise, it can be less than that, so proceed accordingly.
Yann Colletb21ce152016-03-24 01:27:55 +0100118* Checking if DStream has reached its end can be performed with BIT_endOfDStream().
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100119*/
120
121
Yann Colletae7aa062016-02-03 02:46:46 +0100122/*-****************************************
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100123* unsafe API
124******************************************/
125MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
126/* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */
127
128MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
129/* unsafe version; does not check buffer overflow */
130
131MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
132/* faster, but works only if nbBits >= 1 */
133
134
135
Yann Colletae7aa062016-02-03 02:46:46 +0100136/*-**************************************************************
Yann Collet6cf45da2016-03-23 14:18:37 +0100137* Internal functions
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100138****************************************************************/
Yann Colletc173dbd2017-12-04 17:57:42 -0800139MEM_STATIC unsigned BIT_highbit32 (U32 val)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100140{
Stella Laue50ed1f2017-08-22 11:55:42 -0700141 assert(val != 0);
142 {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100143# if defined(_MSC_VER) /* Visual */
Niadb493fd402020-07-28 02:52:15 -0600144# if STATIC_BMI2 == 1
145 return _lzcnt_u32(val) ^ 31;
146# else
147 unsigned long r = 0;
148 return _BitScanReverse(&r, val) ? (unsigned)r : 0;
149# endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100150# elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
Dávid Bolvanský1f7228c2019-09-23 21:23:09 +0200151 return __builtin_clz (val) ^ 31;
Joseph Chen3855bc42019-07-29 15:20:37 +0800152# elif defined(__ICCARM__) /* IAR Intrinsic */
153 return 31 - __CLZ(val);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100154# else /* Software version */
Stella Laue50ed1f2017-08-22 11:55:42 -0700155 static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29,
156 11, 14, 16, 18, 22, 25, 3, 30,
157 8, 12, 20, 28, 15, 17, 24, 7,
158 19, 27, 23, 6, 26, 5, 4, 31 };
159 U32 v = val;
160 v |= v >> 1;
161 v |= v >> 2;
162 v |= v >> 4;
163 v |= v >> 8;
164 v |= v >> 16;
165 return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100166# endif
Stella Laue50ed1f2017-08-22 11:55:42 -0700167 }
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100168}
169
Yann Collet6cf45da2016-03-23 14:18:37 +0100170/*===== Local Constants =====*/
Nick Terrell74718d72017-09-15 17:44:09 -0700171static const unsigned BIT_mask[] = {
172 0, 1, 3, 7, 0xF, 0x1F,
173 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF,
174 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF,
175 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF,
176 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, 0xFFFFFFF, 0x1FFFFFFF,
177 0x3FFFFFFF, 0x7FFFFFFF}; /* up to 31 bits */
178#define BIT_MASK_SIZE (sizeof(BIT_mask) / sizeof(BIT_mask[0]))
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() :
Yann Colletf39a6732017-05-01 09:56:03 -0700184 * `dstCapacity` must be > sizeof(size_t)
Yann Collet01e5b952016-03-19 14:14:31 +0100185 * @return : 0 if success,
Yann Colletb71363b2017-07-19 01:05:40 -0700186 * otherwise an error code (can be tested using ERR_isError()) */
Yann Colletf39a6732017-05-01 09:56:03 -0700187MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC,
Yann Collet33c38b02017-05-01 11:12:30 -0700188 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 Colletf39a6732017-05-01 09:56:03 -0700194 bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->bitContainer);
195 if (dstCapacity <= sizeof(bitC->bitContainer)) 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() :
Nick Terrell74718d72017-09-15 17:44:09 -0700200 * can add up to 31 bits into `bitC`.
Yann Colletb71363b2017-07-19 01:05:40 -0700201 * Note : does not check for register overflow ! */
Yann Colletf39a6732017-05-01 09:56:03 -0700202MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC,
Yann Collet33c38b02017-05-01 11:12:30 -0700203 size_t value, unsigned nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100204{
Nick Terrell74718d72017-09-15 17:44:09 -0700205 MEM_STATIC_ASSERT(BIT_MASK_SIZE == 32);
206 assert(nbBits < BIT_MASK_SIZE);
207 assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);
Yann Collet6cf45da2016-03-23 14:18:37 +0100208 bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100209 bitC->bitPos += nbBits;
210}
211
Yann Colletd1d210f2016-03-19 12:12:07 +0100212/*! BIT_addBitsFast() :
Yann Colletfa41bcc2018-06-13 14:59:26 -0400213 * works only if `value` is _clean_,
214 * meaning all high bits above nbBits are 0 */
Yann Colletf39a6732017-05-01 09:56:03 -0700215MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC,
Yann Collet33c38b02017-05-01 11:12:30 -0700216 size_t value, unsigned nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100217{
Yann Collet202082f2017-04-28 16:56:39 -0700218 assert((value>>nbBits) == 0);
Nick Terrell74718d72017-09-15 17:44:09 -0700219 assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100220 bitC->bitContainer |= value << bitC->bitPos;
221 bitC->bitPos += nbBits;
222}
223
Yann Colletd1d210f2016-03-19 12:12:07 +0100224/*! BIT_flushBitsFast() :
Yann Colletf39a6732017-05-01 09:56:03 -0700225 * assumption : bitContainer has not overflowed
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100226 * unsafe version; does not check buffer overflow */
227MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC)
228{
Yann Colletd64f4352016-03-21 00:07:42 +0100229 size_t const nbBytes = bitC->bitPos >> 3;
Nick Terrell74718d72017-09-15 17:44:09 -0700230 assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8);
Bimba Shrestha43da5bf2019-09-12 14:43:50 -0700231 assert(bitC->ptr <= bitC->endPtr);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100232 MEM_writeLEST(bitC->ptr, bitC->bitContainer);
233 bitC->ptr += nbBytes;
234 bitC->bitPos &= 7;
Yann Colletf39a6732017-05-01 09:56:03 -0700235 bitC->bitContainer >>= nbBytes*8;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100236}
237
Yann Collet01e5b952016-03-19 14:14:31 +0100238/*! BIT_flushBits() :
Yann Colletf39a6732017-05-01 09:56:03 -0700239 * assumption : bitContainer has not overflowed
Yann Collet01e5b952016-03-19 14:14:31 +0100240 * safe version; check for buffer overflow, and prevents it.
Yann Collet33c38b02017-05-01 11:12:30 -0700241 * note : does not signal buffer overflow.
242 * overflow will be revealed later on using BIT_closeCStream() */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100243MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)
244{
Yann Colletd64f4352016-03-21 00:07:42 +0100245 size_t const nbBytes = bitC->bitPos >> 3;
Nick Terrell74718d72017-09-15 17:44:09 -0700246 assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8);
Bimba Shresthafe9af332019-09-12 15:35:27 -0700247 assert(bitC->ptr <= bitC->endPtr);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100248 MEM_writeLEST(bitC->ptr, bitC->bitContainer);
249 bitC->ptr += nbBytes;
250 if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr;
251 bitC->bitPos &= 7;
Yann Collet33c38b02017-05-01 11:12:30 -0700252 bitC->bitContainer >>= nbBytes*8;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100253}
254
Yann Colletd1d210f2016-03-19 12:12:07 +0100255/*! BIT_closeCStream() :
Yann Collet01e5b952016-03-19 14:14:31 +0100256 * @return : size of CStream, in bytes,
Yann Colletb71363b2017-07-19 01:05:40 -0700257 * or 0 if it could not fit into dstBuffer */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100258MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
259{
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100260 BIT_addBitsFast(bitC, 1, 1); /* endMark */
261 BIT_flushBits(bitC);
Yann Collet33c38b02017-05-01 11:12:30 -0700262 if (bitC->ptr >= bitC->endPtr) return 0; /* overflow detected */
Yann Collet01e5b952016-03-19 14:14:31 +0100263 return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100264}
265
266
Yann Colletae7aa062016-02-03 02:46:46 +0100267/*-********************************************************
Yann Colletb71363b2017-07-19 01:05:40 -0700268* bitStream decoding
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100269**********************************************************/
Yann Collet01e5b952016-03-19 14:14:31 +0100270/*! BIT_initDStream() :
Yann Colletb71363b2017-07-19 01:05:40 -0700271 * Initialize a BIT_DStream_t.
272 * `bitD` : a pointer to an already allocated BIT_DStream_t structure.
273 * `srcSize` must be the *exact* size of the bitStream, in bytes.
274 * @return : size of stream (== srcSize), or an errorCode if a problem is detected
275 */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100276MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
277{
278 if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
279
Yann Colletf39a6732017-05-01 09:56:03 -0700280 bitD->start = (const char*)srcBuffer;
281 bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer);
282
Yann Collet1032fbe2016-05-11 18:30:24 +0200283 if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */
Yann Collet1032fbe2016-05-11 18:30:24 +0200284 bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100285 bitD->bitContainer = MEM_readLEST(bitD->ptr);
Yann Colletb21ce152016-03-24 01:27:55 +0100286 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
Yann Collet5397a662016-12-13 15:21:06 +0100287 bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
Yann Collet18c8f792016-06-12 22:51:52 +0200288 if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
Yann Colletae7aa062016-02-03 02:46:46 +0100289 } else {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100290 bitD->ptr = bitD->start;
Yann Collet1ceb5a92016-05-12 13:50:13 +0200291 bitD->bitContainer = *(const BYTE*)(bitD->start);
292 switch(srcSize)
293 {
Yann Colletb71363b2017-07-19 01:05:40 -0700294 case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
295 /* fall-through */
Jos Collin05286fd2017-05-09 08:36:05 +0530296
Yann Colletb71363b2017-07-19 01:05:40 -0700297 case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
298 /* fall-through */
Yann Collet58e8d792017-06-02 18:20:48 -0700299
Yann Colletb71363b2017-07-19 01:05:40 -0700300 case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
301 /* fall-through */
Yann Collet58e8d792017-06-02 18:20:48 -0700302
Yann Colletb71363b2017-07-19 01:05:40 -0700303 case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
304 /* fall-through */
Yann Collet58e8d792017-06-02 18:20:48 -0700305
Yann Colletb71363b2017-07-19 01:05:40 -0700306 case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
307 /* fall-through */
Yann Collet58e8d792017-06-02 18:20:48 -0700308
Yann Colletb71363b2017-07-19 01:05:40 -0700309 case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
310 /* fall-through */
Yann Collet58e8d792017-06-02 18:20:48 -0700311
Yann Colletb71363b2017-07-19 01:05:40 -0700312 default: break;
Yann Collet1ceb5a92016-05-12 13:50:13 +0200313 }
Yann Colletb71363b2017-07-19 01:05:40 -0700314 { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
315 bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;
316 if (lastByte == 0) return ERROR(corruption_detected); /* endMark not present */
317 }
Yann Collet1032fbe2016-05-11 18:30:24 +0200318 bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100319 }
320
321 return srcSize;
322}
323
Yann Collet1032fbe2016-05-11 18:30:24 +0200324MEM_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
Yann Collet3c017862016-03-23 14:09:51 +0100325{
Yann Collet1032fbe2016-05-11 18:30:24 +0200326 return bitContainer >> start;
Yann Collet3c017862016-03-23 14:09:51 +0100327}
328
Yann Collet1032fbe2016-05-11 18:30:24 +0200329MEM_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
Yann Collet3c017862016-03-23 14:09:51 +0100330{
Niadb493fd402020-07-28 02:52:15 -0600331#if STATIC_BMI2
332 return _bextr_u64(bitContainer, start, nbBits);
333#else
Yann Collet6ed3b522018-10-10 18:26:44 -0700334 U32 const regMask = sizeof(bitContainer)*8 - 1;
335 /* if start > regMask, bitstream is corrupted, and result is undefined */
Nick Terrell74718d72017-09-15 17:44:09 -0700336 assert(nbBits < BIT_MASK_SIZE);
Yann Collet6ed3b522018-10-10 18:26:44 -0700337 return (bitContainer >> (start & regMask)) & BIT_mask[nbBits];
Niadb493fd402020-07-28 02:52:15 -0600338#endif
Yann Collet3c017862016-03-23 14:09:51 +0100339}
340
Yann Collet1032fbe2016-05-11 18:30:24 +0200341MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
Yann Colletafab0202016-03-23 13:57:49 +0100342{
Niadb493fd402020-07-28 02:52:15 -0600343#if STATIC_BMI2
344 return _bzhi_u64(bitContainer, nbBits);
345#else
Nick Terrell74718d72017-09-15 17:44:09 -0700346 assert(nbBits < BIT_MASK_SIZE);
Yann Collet1032fbe2016-05-11 18:30:24 +0200347 return bitContainer & BIT_mask[nbBits];
Niadb493fd402020-07-28 02:52:15 -0600348#endif
Yann Colletafab0202016-03-23 13:57:49 +0100349}
350
Yann Collet01e5b952016-03-19 14:14:31 +0100351/*! BIT_lookBits() :
352 * Provides next n bits from local register.
Yann Collet1032fbe2016-05-11 18:30:24 +0200353 * local register is not modified.
Yann Collet01e5b952016-03-19 14:14:31 +0100354 * On 32-bits, maxNbBits==24.
355 * On 64-bits, maxNbBits==56.
Yann Colletb71363b2017-07-19 01:05:40 -0700356 * @return : value extracted */
357MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100358{
Yann Collet7791f192018-10-10 16:36:11 -0700359 /* arbitrate between double-shift and shift+mask */
Yann Colletd3ec2332018-10-10 15:48:43 -0700360#if 1
Yann Collet7791f192018-10-10 16:36:11 -0700361 /* if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8,
362 * bitstream is likely corrupted, and result is undefined */
Yann Collet1032fbe2016-05-11 18:30:24 +0200363 return BIT_getMiddleBits(bitD->bitContainer, (sizeof(bitD->bitContainer)*8) - bitD->bitsConsumed - nbBits, nbBits);
Yann Collet862a8592016-03-23 18:45:23 +0100364#else
Yann Collet7791f192018-10-10 16:36:11 -0700365 /* this code path is slower on my os-x laptop */
Yann Colletf39a6732017-05-01 09:56:03 -0700366 U32 const regMask = sizeof(bitD->bitContainer)*8 - 1;
367 return ((bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> 1) >> ((regMask-nbBits) & regMask);
Yann Collet862a8592016-03-23 18:45:23 +0100368#endif
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100369}
370
Yann Collet01e5b952016-03-19 14:14:31 +0100371/*! BIT_lookBitsFast() :
Yann Collet202082f2017-04-28 16:56:39 -0700372 * unsafe version; only works if nbBits >= 1 */
Yann Colletadd08d62016-03-23 01:32:41 +0100373MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100374{
Yann Colletf39a6732017-05-01 09:56:03 -0700375 U32 const regMask = sizeof(bitD->bitContainer)*8 - 1;
Yann Collet202082f2017-04-28 16:56:39 -0700376 assert(nbBits >= 1);
Yann Colletf39a6732017-05-01 09:56:03 -0700377 return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> (((regMask+1)-nbBits) & regMask);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100378}
379
380MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
381{
382 bitD->bitsConsumed += nbBits;
383}
384
Yann Collet01e5b952016-03-19 14:14:31 +0100385/*! BIT_readBits() :
Yann Colletb21ce152016-03-24 01:27:55 +0100386 * Read (consume) next n bits from local register and update.
387 * Pay attention to not read more than nbBits contained into local register.
Yann Colletb71363b2017-07-19 01:05:40 -0700388 * @return : extracted value. */
Yann Colletededcfc2018-12-21 16:19:44 -0800389MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100390{
Yann Colletafab0202016-03-23 13:57:49 +0100391 size_t const value = BIT_lookBits(bitD, nbBits);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100392 BIT_skipBits(bitD, nbBits);
393 return value;
394}
395
Yann Collet01e5b952016-03-19 14:14:31 +0100396/*! BIT_readBitsFast() :
Yann Colletb71363b2017-07-19 01:05:40 -0700397 * unsafe version; only works only if nbBits >= 1 */
Yann Colletededcfc2018-12-21 16:19:44 -0800398MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100399{
Yann Colletafab0202016-03-23 13:57:49 +0100400 size_t const value = BIT_lookBitsFast(bitD, nbBits);
Yann Collet202082f2017-04-28 16:56:39 -0700401 assert(nbBits >= 1);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100402 BIT_skipBits(bitD, nbBits);
403 return value;
404}
405
Nick Terrell718f00f2019-11-25 18:26:19 -0800406/*! BIT_reloadDStreamFast() :
407 * Similar to BIT_reloadDStream(), but with two differences:
408 * 1. bitsConsumed <= sizeof(bitD->bitContainer)*8 must hold!
409 * 2. Returns BIT_DStream_overflow when bitD->ptr < bitD->limitPtr, at this
410 * point you must use BIT_reloadDStream() to reload.
411 */
412MEM_STATIC BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD)
413{
414 if (UNLIKELY(bitD->ptr < bitD->limitPtr))
415 return BIT_DStream_overflow;
416 assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer)*8);
417 bitD->ptr -= bitD->bitsConsumed >> 3;
418 bitD->bitsConsumed &= 7;
419 bitD->bitContainer = MEM_readLEST(bitD->ptr);
420 return BIT_DStream_unfinished;
421}
422
Yann Collet01e5b952016-03-19 14:14:31 +0100423/*! BIT_reloadDStream() :
Yann Colletb71363b2017-07-19 01:05:40 -0700424 * Refill `bitD` from buffer previously set in BIT_initDStream() .
425 * This function is safe, it guarantees it will not read beyond src buffer.
426 * @return : status of `BIT_DStream_t` internal register.
Baldur Karlsson430a2fe2018-03-13 20:02:21 +0000427 * when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100428MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
429{
Yann Colletf39a6732017-05-01 09:56:03 -0700430 if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* overflow detected, like end of stream */
Nick Terrell5152fb22017-03-29 18:51:58 -0700431 return BIT_DStream_overflow;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100432
Yann Colletf39a6732017-05-01 09:56:03 -0700433 if (bitD->ptr >= bitD->limitPtr) {
Nick Terrell718f00f2019-11-25 18:26:19 -0800434 return BIT_reloadDStreamFast(bitD);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100435 }
Yann Colletae7aa062016-02-03 02:46:46 +0100436 if (bitD->ptr == bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100437 if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
438 return BIT_DStream_completed;
439 }
Yann Colletf39a6732017-05-01 09:56:03 -0700440 /* start < ptr < limitPtr */
Yann Collet01e5b952016-03-19 14:14:31 +0100441 { U32 nbBytes = bitD->bitsConsumed >> 3;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100442 BIT_DStream_status result = BIT_DStream_unfinished;
Yann Colletae7aa062016-02-03 02:46:46 +0100443 if (bitD->ptr - nbBytes < bitD->start) {
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100444 nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
445 result = BIT_DStream_endOfBuffer;
446 }
447 bitD->ptr -= nbBytes;
448 bitD->bitsConsumed -= nbBytes*8;
Yann Colletf39a6732017-05-01 09:56:03 -0700449 bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD->bitContainer), otherwise bitD->ptr == bitD->start */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100450 return result;
451 }
452}
453
Yann Colletd1d210f2016-03-19 12:12:07 +0100454/*! BIT_endOfDStream() :
Yann Colletb71363b2017-07-19 01:05:40 -0700455 * @return : 1 if DStream has _exactly_ reached its end (all bits consumed).
456 */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100457MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
458{
459 return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
460}
461
462#if defined (__cplusplus)
463}
464#endif
465
466#endif /* BITSTREAM_H_MODULE */