1*e0c1b49fSNick Terrell /* ****************************************************************** 2*e0c1b49fSNick Terrell * bitstream 3*e0c1b49fSNick Terrell * Part of FSE library 4*e0c1b49fSNick Terrell * Copyright (c) Yann Collet, Facebook, Inc. 5*e0c1b49fSNick Terrell * 6*e0c1b49fSNick Terrell * You can contact the author at : 7*e0c1b49fSNick Terrell * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy 8*e0c1b49fSNick Terrell * 9*e0c1b49fSNick Terrell * This source code is licensed under both the BSD-style license (found in the 10*e0c1b49fSNick Terrell * LICENSE file in the root directory of this source tree) and the GPLv2 (found 11*e0c1b49fSNick Terrell * in the COPYING file in the root directory of this source tree). 12*e0c1b49fSNick Terrell * You may select, at your option, one of the above-listed licenses. 13*e0c1b49fSNick Terrell ****************************************************************** */ 14*e0c1b49fSNick Terrell #ifndef BITSTREAM_H_MODULE 15*e0c1b49fSNick Terrell #define BITSTREAM_H_MODULE 16*e0c1b49fSNick Terrell 17*e0c1b49fSNick Terrell /* 18*e0c1b49fSNick Terrell * This API consists of small unitary functions, which must be inlined for best performance. 19*e0c1b49fSNick Terrell * Since link-time-optimization is not available for all compilers, 20*e0c1b49fSNick Terrell * these functions are defined into a .h to be included. 21*e0c1b49fSNick Terrell */ 22*e0c1b49fSNick Terrell 23*e0c1b49fSNick Terrell /*-**************************************** 24*e0c1b49fSNick Terrell * Dependencies 25*e0c1b49fSNick Terrell ******************************************/ 26*e0c1b49fSNick Terrell #include "mem.h" /* unaligned access routines */ 27*e0c1b49fSNick Terrell #include "compiler.h" /* UNLIKELY() */ 28*e0c1b49fSNick Terrell #include "debug.h" /* assert(), DEBUGLOG(), RAWLOG() */ 29*e0c1b49fSNick Terrell #include "error_private.h" /* error codes and messages */ 30*e0c1b49fSNick Terrell 31*e0c1b49fSNick Terrell 32*e0c1b49fSNick Terrell /*========================================= 33*e0c1b49fSNick Terrell * Target specific 34*e0c1b49fSNick Terrell =========================================*/ 35*e0c1b49fSNick Terrell 36*e0c1b49fSNick Terrell #define STREAM_ACCUMULATOR_MIN_32 25 37*e0c1b49fSNick Terrell #define STREAM_ACCUMULATOR_MIN_64 57 38*e0c1b49fSNick Terrell #define STREAM_ACCUMULATOR_MIN ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64)) 39*e0c1b49fSNick Terrell 40*e0c1b49fSNick Terrell 41*e0c1b49fSNick Terrell /*-****************************************** 42*e0c1b49fSNick Terrell * bitStream encoding API (write forward) 43*e0c1b49fSNick Terrell ********************************************/ 44*e0c1b49fSNick Terrell /* bitStream can mix input from multiple sources. 45*e0c1b49fSNick Terrell * A critical property of these streams is that they encode and decode in **reverse** direction. 46*e0c1b49fSNick Terrell * So the first bit sequence you add will be the last to be read, like a LIFO stack. 47*e0c1b49fSNick Terrell */ 48*e0c1b49fSNick Terrell typedef struct { 49*e0c1b49fSNick Terrell size_t bitContainer; 50*e0c1b49fSNick Terrell unsigned bitPos; 51*e0c1b49fSNick Terrell char* startPtr; 52*e0c1b49fSNick Terrell char* ptr; 53*e0c1b49fSNick Terrell char* endPtr; 54*e0c1b49fSNick Terrell } BIT_CStream_t; 55*e0c1b49fSNick Terrell 56*e0c1b49fSNick Terrell MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity); 57*e0c1b49fSNick Terrell MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits); 58*e0c1b49fSNick Terrell MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC); 59*e0c1b49fSNick Terrell MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC); 60*e0c1b49fSNick Terrell 61*e0c1b49fSNick Terrell /* Start with initCStream, providing the size of buffer to write into. 62*e0c1b49fSNick Terrell * bitStream will never write outside of this buffer. 63*e0c1b49fSNick Terrell * `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code. 64*e0c1b49fSNick Terrell * 65*e0c1b49fSNick Terrell * bits are first added to a local register. 66*e0c1b49fSNick Terrell * Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems. 67*e0c1b49fSNick Terrell * Writing data into memory is an explicit operation, performed by the flushBits function. 68*e0c1b49fSNick Terrell * Hence keep track how many bits are potentially stored into local register to avoid register overflow. 69*e0c1b49fSNick Terrell * After a flushBits, a maximum of 7 bits might still be stored into local register. 70*e0c1b49fSNick Terrell * 71*e0c1b49fSNick Terrell * Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers. 72*e0c1b49fSNick Terrell * 73*e0c1b49fSNick Terrell * Last operation is to close the bitStream. 74*e0c1b49fSNick Terrell * The function returns the final size of CStream in bytes. 75*e0c1b49fSNick Terrell * If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable) 76*e0c1b49fSNick Terrell */ 77*e0c1b49fSNick Terrell 78*e0c1b49fSNick Terrell 79*e0c1b49fSNick Terrell /*-******************************************** 80*e0c1b49fSNick Terrell * bitStream decoding API (read backward) 81*e0c1b49fSNick Terrell **********************************************/ 82*e0c1b49fSNick Terrell typedef struct { 83*e0c1b49fSNick Terrell size_t bitContainer; 84*e0c1b49fSNick Terrell unsigned bitsConsumed; 85*e0c1b49fSNick Terrell const char* ptr; 86*e0c1b49fSNick Terrell const char* start; 87*e0c1b49fSNick Terrell const char* limitPtr; 88*e0c1b49fSNick Terrell } BIT_DStream_t; 89*e0c1b49fSNick Terrell 90*e0c1b49fSNick Terrell typedef enum { BIT_DStream_unfinished = 0, 91*e0c1b49fSNick Terrell BIT_DStream_endOfBuffer = 1, 92*e0c1b49fSNick Terrell BIT_DStream_completed = 2, 93*e0c1b49fSNick Terrell BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */ 94*e0c1b49fSNick Terrell /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */ 95*e0c1b49fSNick Terrell 96*e0c1b49fSNick Terrell MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize); 97*e0c1b49fSNick Terrell MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits); 98*e0c1b49fSNick Terrell MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD); 99*e0c1b49fSNick Terrell MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD); 100*e0c1b49fSNick Terrell 101*e0c1b49fSNick Terrell 102*e0c1b49fSNick Terrell /* Start by invoking BIT_initDStream(). 103*e0c1b49fSNick Terrell * A chunk of the bitStream is then stored into a local register. 104*e0c1b49fSNick Terrell * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t). 105*e0c1b49fSNick Terrell * You can then retrieve bitFields stored into the local register, **in reverse order**. 106*e0c1b49fSNick Terrell * Local register is explicitly reloaded from memory by the BIT_reloadDStream() method. 107*e0c1b49fSNick Terrell * A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished. 108*e0c1b49fSNick Terrell * Otherwise, it can be less than that, so proceed accordingly. 109*e0c1b49fSNick Terrell * Checking if DStream has reached its end can be performed with BIT_endOfDStream(). 110*e0c1b49fSNick Terrell */ 111*e0c1b49fSNick Terrell 112*e0c1b49fSNick Terrell 113*e0c1b49fSNick Terrell /*-**************************************** 114*e0c1b49fSNick Terrell * unsafe API 115*e0c1b49fSNick Terrell ******************************************/ 116*e0c1b49fSNick Terrell MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits); 117*e0c1b49fSNick Terrell /* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */ 118*e0c1b49fSNick Terrell 119*e0c1b49fSNick Terrell MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC); 120*e0c1b49fSNick Terrell /* unsafe version; does not check buffer overflow */ 121*e0c1b49fSNick Terrell 122*e0c1b49fSNick Terrell MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits); 123*e0c1b49fSNick Terrell /* faster, but works only if nbBits >= 1 */ 124*e0c1b49fSNick Terrell 125*e0c1b49fSNick Terrell 126*e0c1b49fSNick Terrell 127*e0c1b49fSNick Terrell /*-************************************************************** 128*e0c1b49fSNick Terrell * Internal functions 129*e0c1b49fSNick Terrell ****************************************************************/ 130*e0c1b49fSNick Terrell MEM_STATIC unsigned BIT_highbit32 (U32 val) 131*e0c1b49fSNick Terrell { 132*e0c1b49fSNick Terrell assert(val != 0); 133*e0c1b49fSNick Terrell { 134*e0c1b49fSNick Terrell # if (__GNUC__ >= 3) /* Use GCC Intrinsic */ 135*e0c1b49fSNick Terrell return __builtin_clz (val) ^ 31; 136*e0c1b49fSNick Terrell # else /* Software version */ 137*e0c1b49fSNick Terrell static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 138*e0c1b49fSNick Terrell 11, 14, 16, 18, 22, 25, 3, 30, 139*e0c1b49fSNick Terrell 8, 12, 20, 28, 15, 17, 24, 7, 140*e0c1b49fSNick Terrell 19, 27, 23, 6, 26, 5, 4, 31 }; 141*e0c1b49fSNick Terrell U32 v = val; 142*e0c1b49fSNick Terrell v |= v >> 1; 143*e0c1b49fSNick Terrell v |= v >> 2; 144*e0c1b49fSNick Terrell v |= v >> 4; 145*e0c1b49fSNick Terrell v |= v >> 8; 146*e0c1b49fSNick Terrell v |= v >> 16; 147*e0c1b49fSNick Terrell return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27]; 148*e0c1b49fSNick Terrell # endif 149*e0c1b49fSNick Terrell } 150*e0c1b49fSNick Terrell } 151*e0c1b49fSNick Terrell 152*e0c1b49fSNick Terrell /*===== Local Constants =====*/ 153*e0c1b49fSNick Terrell static const unsigned BIT_mask[] = { 154*e0c1b49fSNick Terrell 0, 1, 3, 7, 0xF, 0x1F, 155*e0c1b49fSNick Terrell 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 156*e0c1b49fSNick Terrell 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 157*e0c1b49fSNick Terrell 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 158*e0c1b49fSNick Terrell 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, 0xFFFFFFF, 0x1FFFFFFF, 159*e0c1b49fSNick Terrell 0x3FFFFFFF, 0x7FFFFFFF}; /* up to 31 bits */ 160*e0c1b49fSNick Terrell #define BIT_MASK_SIZE (sizeof(BIT_mask) / sizeof(BIT_mask[0])) 161*e0c1b49fSNick Terrell 162*e0c1b49fSNick Terrell /*-************************************************************** 163*e0c1b49fSNick Terrell * bitStream encoding 164*e0c1b49fSNick Terrell ****************************************************************/ 165*e0c1b49fSNick Terrell /*! BIT_initCStream() : 166*e0c1b49fSNick Terrell * `dstCapacity` must be > sizeof(size_t) 167*e0c1b49fSNick Terrell * @return : 0 if success, 168*e0c1b49fSNick Terrell * otherwise an error code (can be tested using ERR_isError()) */ 169*e0c1b49fSNick Terrell MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, 170*e0c1b49fSNick Terrell void* startPtr, size_t dstCapacity) 171*e0c1b49fSNick Terrell { 172*e0c1b49fSNick Terrell bitC->bitContainer = 0; 173*e0c1b49fSNick Terrell bitC->bitPos = 0; 174*e0c1b49fSNick Terrell bitC->startPtr = (char*)startPtr; 175*e0c1b49fSNick Terrell bitC->ptr = bitC->startPtr; 176*e0c1b49fSNick Terrell bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->bitContainer); 177*e0c1b49fSNick Terrell if (dstCapacity <= sizeof(bitC->bitContainer)) return ERROR(dstSize_tooSmall); 178*e0c1b49fSNick Terrell return 0; 179*e0c1b49fSNick Terrell } 180*e0c1b49fSNick Terrell 181*e0c1b49fSNick Terrell /*! BIT_addBits() : 182*e0c1b49fSNick Terrell * can add up to 31 bits into `bitC`. 183*e0c1b49fSNick Terrell * Note : does not check for register overflow ! */ 184*e0c1b49fSNick Terrell MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, 185*e0c1b49fSNick Terrell size_t value, unsigned nbBits) 186*e0c1b49fSNick Terrell { 187*e0c1b49fSNick Terrell DEBUG_STATIC_ASSERT(BIT_MASK_SIZE == 32); 188*e0c1b49fSNick Terrell assert(nbBits < BIT_MASK_SIZE); 189*e0c1b49fSNick Terrell assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8); 190*e0c1b49fSNick Terrell bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos; 191*e0c1b49fSNick Terrell bitC->bitPos += nbBits; 192*e0c1b49fSNick Terrell } 193*e0c1b49fSNick Terrell 194*e0c1b49fSNick Terrell /*! BIT_addBitsFast() : 195*e0c1b49fSNick Terrell * works only if `value` is _clean_, 196*e0c1b49fSNick Terrell * meaning all high bits above nbBits are 0 */ 197*e0c1b49fSNick Terrell MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, 198*e0c1b49fSNick Terrell size_t value, unsigned nbBits) 199*e0c1b49fSNick Terrell { 200*e0c1b49fSNick Terrell assert((value>>nbBits) == 0); 201*e0c1b49fSNick Terrell assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8); 202*e0c1b49fSNick Terrell bitC->bitContainer |= value << bitC->bitPos; 203*e0c1b49fSNick Terrell bitC->bitPos += nbBits; 204*e0c1b49fSNick Terrell } 205*e0c1b49fSNick Terrell 206*e0c1b49fSNick Terrell /*! BIT_flushBitsFast() : 207*e0c1b49fSNick Terrell * assumption : bitContainer has not overflowed 208*e0c1b49fSNick Terrell * unsafe version; does not check buffer overflow */ 209*e0c1b49fSNick Terrell MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC) 210*e0c1b49fSNick Terrell { 211*e0c1b49fSNick Terrell size_t const nbBytes = bitC->bitPos >> 3; 212*e0c1b49fSNick Terrell assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8); 213*e0c1b49fSNick Terrell assert(bitC->ptr <= bitC->endPtr); 214*e0c1b49fSNick Terrell MEM_writeLEST(bitC->ptr, bitC->bitContainer); 215*e0c1b49fSNick Terrell bitC->ptr += nbBytes; 216*e0c1b49fSNick Terrell bitC->bitPos &= 7; 217*e0c1b49fSNick Terrell bitC->bitContainer >>= nbBytes*8; 218*e0c1b49fSNick Terrell } 219*e0c1b49fSNick Terrell 220*e0c1b49fSNick Terrell /*! BIT_flushBits() : 221*e0c1b49fSNick Terrell * assumption : bitContainer has not overflowed 222*e0c1b49fSNick Terrell * safe version; check for buffer overflow, and prevents it. 223*e0c1b49fSNick Terrell * note : does not signal buffer overflow. 224*e0c1b49fSNick Terrell * overflow will be revealed later on using BIT_closeCStream() */ 225*e0c1b49fSNick Terrell MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC) 226*e0c1b49fSNick Terrell { 227*e0c1b49fSNick Terrell size_t const nbBytes = bitC->bitPos >> 3; 228*e0c1b49fSNick Terrell assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8); 229*e0c1b49fSNick Terrell assert(bitC->ptr <= bitC->endPtr); 230*e0c1b49fSNick Terrell MEM_writeLEST(bitC->ptr, bitC->bitContainer); 231*e0c1b49fSNick Terrell bitC->ptr += nbBytes; 232*e0c1b49fSNick Terrell if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr; 233*e0c1b49fSNick Terrell bitC->bitPos &= 7; 234*e0c1b49fSNick Terrell bitC->bitContainer >>= nbBytes*8; 235*e0c1b49fSNick Terrell } 236*e0c1b49fSNick Terrell 237*e0c1b49fSNick Terrell /*! BIT_closeCStream() : 238*e0c1b49fSNick Terrell * @return : size of CStream, in bytes, 239*e0c1b49fSNick Terrell * or 0 if it could not fit into dstBuffer */ 240*e0c1b49fSNick Terrell MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC) 241*e0c1b49fSNick Terrell { 242*e0c1b49fSNick Terrell BIT_addBitsFast(bitC, 1, 1); /* endMark */ 243*e0c1b49fSNick Terrell BIT_flushBits(bitC); 244*e0c1b49fSNick Terrell if (bitC->ptr >= bitC->endPtr) return 0; /* overflow detected */ 245*e0c1b49fSNick Terrell return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0); 246*e0c1b49fSNick Terrell } 247*e0c1b49fSNick Terrell 248*e0c1b49fSNick Terrell 249*e0c1b49fSNick Terrell /*-******************************************************** 250*e0c1b49fSNick Terrell * bitStream decoding 251*e0c1b49fSNick Terrell **********************************************************/ 252*e0c1b49fSNick Terrell /*! BIT_initDStream() : 253*e0c1b49fSNick Terrell * Initialize a BIT_DStream_t. 254*e0c1b49fSNick Terrell * `bitD` : a pointer to an already allocated BIT_DStream_t structure. 255*e0c1b49fSNick Terrell * `srcSize` must be the *exact* size of the bitStream, in bytes. 256*e0c1b49fSNick Terrell * @return : size of stream (== srcSize), or an errorCode if a problem is detected 257*e0c1b49fSNick Terrell */ 258*e0c1b49fSNick Terrell MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize) 259*e0c1b49fSNick Terrell { 260*e0c1b49fSNick Terrell if (srcSize < 1) { ZSTD_memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); } 261*e0c1b49fSNick Terrell 262*e0c1b49fSNick Terrell bitD->start = (const char*)srcBuffer; 263*e0c1b49fSNick Terrell bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer); 264*e0c1b49fSNick Terrell 265*e0c1b49fSNick Terrell if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */ 266*e0c1b49fSNick Terrell bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer); 267*e0c1b49fSNick Terrell bitD->bitContainer = MEM_readLEST(bitD->ptr); 268*e0c1b49fSNick Terrell { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1]; 269*e0c1b49fSNick Terrell bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */ 270*e0c1b49fSNick Terrell if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ } 271*e0c1b49fSNick Terrell } else { 272*e0c1b49fSNick Terrell bitD->ptr = bitD->start; 273*e0c1b49fSNick Terrell bitD->bitContainer = *(const BYTE*)(bitD->start); 274*e0c1b49fSNick Terrell switch(srcSize) 275*e0c1b49fSNick Terrell { 276*e0c1b49fSNick Terrell case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16); 277*e0c1b49fSNick Terrell ZSTD_FALLTHROUGH; 278*e0c1b49fSNick Terrell 279*e0c1b49fSNick Terrell case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24); 280*e0c1b49fSNick Terrell ZSTD_FALLTHROUGH; 281*e0c1b49fSNick Terrell 282*e0c1b49fSNick Terrell case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32); 283*e0c1b49fSNick Terrell ZSTD_FALLTHROUGH; 284*e0c1b49fSNick Terrell 285*e0c1b49fSNick Terrell case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24; 286*e0c1b49fSNick Terrell ZSTD_FALLTHROUGH; 287*e0c1b49fSNick Terrell 288*e0c1b49fSNick Terrell case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16; 289*e0c1b49fSNick Terrell ZSTD_FALLTHROUGH; 290*e0c1b49fSNick Terrell 291*e0c1b49fSNick Terrell case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8; 292*e0c1b49fSNick Terrell ZSTD_FALLTHROUGH; 293*e0c1b49fSNick Terrell 294*e0c1b49fSNick Terrell default: break; 295*e0c1b49fSNick Terrell } 296*e0c1b49fSNick Terrell { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1]; 297*e0c1b49fSNick Terrell bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; 298*e0c1b49fSNick Terrell if (lastByte == 0) return ERROR(corruption_detected); /* endMark not present */ 299*e0c1b49fSNick Terrell } 300*e0c1b49fSNick Terrell bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8; 301*e0c1b49fSNick Terrell } 302*e0c1b49fSNick Terrell 303*e0c1b49fSNick Terrell return srcSize; 304*e0c1b49fSNick Terrell } 305*e0c1b49fSNick Terrell 306*e0c1b49fSNick Terrell MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getUpperBits(size_t bitContainer, U32 const start) 307*e0c1b49fSNick Terrell { 308*e0c1b49fSNick Terrell return bitContainer >> start; 309*e0c1b49fSNick Terrell } 310*e0c1b49fSNick Terrell 311*e0c1b49fSNick Terrell MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits) 312*e0c1b49fSNick Terrell { 313*e0c1b49fSNick Terrell U32 const regMask = sizeof(bitContainer)*8 - 1; 314*e0c1b49fSNick Terrell /* if start > regMask, bitstream is corrupted, and result is undefined */ 315*e0c1b49fSNick Terrell assert(nbBits < BIT_MASK_SIZE); 316*e0c1b49fSNick Terrell return (bitContainer >> (start & regMask)) & BIT_mask[nbBits]; 317*e0c1b49fSNick Terrell } 318*e0c1b49fSNick Terrell 319*e0c1b49fSNick Terrell MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits) 320*e0c1b49fSNick Terrell { 321*e0c1b49fSNick Terrell assert(nbBits < BIT_MASK_SIZE); 322*e0c1b49fSNick Terrell return bitContainer & BIT_mask[nbBits]; 323*e0c1b49fSNick Terrell } 324*e0c1b49fSNick Terrell 325*e0c1b49fSNick Terrell /*! BIT_lookBits() : 326*e0c1b49fSNick Terrell * Provides next n bits from local register. 327*e0c1b49fSNick Terrell * local register is not modified. 328*e0c1b49fSNick Terrell * On 32-bits, maxNbBits==24. 329*e0c1b49fSNick Terrell * On 64-bits, maxNbBits==56. 330*e0c1b49fSNick Terrell * @return : value extracted */ 331*e0c1b49fSNick Terrell MEM_STATIC FORCE_INLINE_ATTR size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits) 332*e0c1b49fSNick Terrell { 333*e0c1b49fSNick Terrell /* arbitrate between double-shift and shift+mask */ 334*e0c1b49fSNick Terrell #if 1 335*e0c1b49fSNick Terrell /* if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8, 336*e0c1b49fSNick Terrell * bitstream is likely corrupted, and result is undefined */ 337*e0c1b49fSNick Terrell return BIT_getMiddleBits(bitD->bitContainer, (sizeof(bitD->bitContainer)*8) - bitD->bitsConsumed - nbBits, nbBits); 338*e0c1b49fSNick Terrell #else 339*e0c1b49fSNick Terrell /* this code path is slower on my os-x laptop */ 340*e0c1b49fSNick Terrell U32 const regMask = sizeof(bitD->bitContainer)*8 - 1; 341*e0c1b49fSNick Terrell return ((bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> 1) >> ((regMask-nbBits) & regMask); 342*e0c1b49fSNick Terrell #endif 343*e0c1b49fSNick Terrell } 344*e0c1b49fSNick Terrell 345*e0c1b49fSNick Terrell /*! BIT_lookBitsFast() : 346*e0c1b49fSNick Terrell * unsafe version; only works if nbBits >= 1 */ 347*e0c1b49fSNick Terrell MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits) 348*e0c1b49fSNick Terrell { 349*e0c1b49fSNick Terrell U32 const regMask = sizeof(bitD->bitContainer)*8 - 1; 350*e0c1b49fSNick Terrell assert(nbBits >= 1); 351*e0c1b49fSNick Terrell return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> (((regMask+1)-nbBits) & regMask); 352*e0c1b49fSNick Terrell } 353*e0c1b49fSNick Terrell 354*e0c1b49fSNick Terrell MEM_STATIC FORCE_INLINE_ATTR void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits) 355*e0c1b49fSNick Terrell { 356*e0c1b49fSNick Terrell bitD->bitsConsumed += nbBits; 357*e0c1b49fSNick Terrell } 358*e0c1b49fSNick Terrell 359*e0c1b49fSNick Terrell /*! BIT_readBits() : 360*e0c1b49fSNick Terrell * Read (consume) next n bits from local register and update. 361*e0c1b49fSNick Terrell * Pay attention to not read more than nbBits contained into local register. 362*e0c1b49fSNick Terrell * @return : extracted value. */ 363*e0c1b49fSNick Terrell MEM_STATIC FORCE_INLINE_ATTR size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits) 364*e0c1b49fSNick Terrell { 365*e0c1b49fSNick Terrell size_t const value = BIT_lookBits(bitD, nbBits); 366*e0c1b49fSNick Terrell BIT_skipBits(bitD, nbBits); 367*e0c1b49fSNick Terrell return value; 368*e0c1b49fSNick Terrell } 369*e0c1b49fSNick Terrell 370*e0c1b49fSNick Terrell /*! BIT_readBitsFast() : 371*e0c1b49fSNick Terrell * unsafe version; only works only if nbBits >= 1 */ 372*e0c1b49fSNick Terrell MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits) 373*e0c1b49fSNick Terrell { 374*e0c1b49fSNick Terrell size_t const value = BIT_lookBitsFast(bitD, nbBits); 375*e0c1b49fSNick Terrell assert(nbBits >= 1); 376*e0c1b49fSNick Terrell BIT_skipBits(bitD, nbBits); 377*e0c1b49fSNick Terrell return value; 378*e0c1b49fSNick Terrell } 379*e0c1b49fSNick Terrell 380*e0c1b49fSNick Terrell /*! BIT_reloadDStreamFast() : 381*e0c1b49fSNick Terrell * Similar to BIT_reloadDStream(), but with two differences: 382*e0c1b49fSNick Terrell * 1. bitsConsumed <= sizeof(bitD->bitContainer)*8 must hold! 383*e0c1b49fSNick Terrell * 2. Returns BIT_DStream_overflow when bitD->ptr < bitD->limitPtr, at this 384*e0c1b49fSNick Terrell * point you must use BIT_reloadDStream() to reload. 385*e0c1b49fSNick Terrell */ 386*e0c1b49fSNick Terrell MEM_STATIC BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD) 387*e0c1b49fSNick Terrell { 388*e0c1b49fSNick Terrell if (UNLIKELY(bitD->ptr < bitD->limitPtr)) 389*e0c1b49fSNick Terrell return BIT_DStream_overflow; 390*e0c1b49fSNick Terrell assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer)*8); 391*e0c1b49fSNick Terrell bitD->ptr -= bitD->bitsConsumed >> 3; 392*e0c1b49fSNick Terrell bitD->bitsConsumed &= 7; 393*e0c1b49fSNick Terrell bitD->bitContainer = MEM_readLEST(bitD->ptr); 394*e0c1b49fSNick Terrell return BIT_DStream_unfinished; 395*e0c1b49fSNick Terrell } 396*e0c1b49fSNick Terrell 397*e0c1b49fSNick Terrell /*! BIT_reloadDStream() : 398*e0c1b49fSNick Terrell * Refill `bitD` from buffer previously set in BIT_initDStream() . 399*e0c1b49fSNick Terrell * This function is safe, it guarantees it will not read beyond src buffer. 400*e0c1b49fSNick Terrell * @return : status of `BIT_DStream_t` internal register. 401*e0c1b49fSNick Terrell * when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */ 402*e0c1b49fSNick Terrell MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD) 403*e0c1b49fSNick Terrell { 404*e0c1b49fSNick Terrell if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* overflow detected, like end of stream */ 405*e0c1b49fSNick Terrell return BIT_DStream_overflow; 406*e0c1b49fSNick Terrell 407*e0c1b49fSNick Terrell if (bitD->ptr >= bitD->limitPtr) { 408*e0c1b49fSNick Terrell return BIT_reloadDStreamFast(bitD); 409*e0c1b49fSNick Terrell } 410*e0c1b49fSNick Terrell if (bitD->ptr == bitD->start) { 411*e0c1b49fSNick Terrell if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer; 412*e0c1b49fSNick Terrell return BIT_DStream_completed; 413*e0c1b49fSNick Terrell } 414*e0c1b49fSNick Terrell /* start < ptr < limitPtr */ 415*e0c1b49fSNick Terrell { U32 nbBytes = bitD->bitsConsumed >> 3; 416*e0c1b49fSNick Terrell BIT_DStream_status result = BIT_DStream_unfinished; 417*e0c1b49fSNick Terrell if (bitD->ptr - nbBytes < bitD->start) { 418*e0c1b49fSNick Terrell nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ 419*e0c1b49fSNick Terrell result = BIT_DStream_endOfBuffer; 420*e0c1b49fSNick Terrell } 421*e0c1b49fSNick Terrell bitD->ptr -= nbBytes; 422*e0c1b49fSNick Terrell bitD->bitsConsumed -= nbBytes*8; 423*e0c1b49fSNick Terrell bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD->bitContainer), otherwise bitD->ptr == bitD->start */ 424*e0c1b49fSNick Terrell return result; 425*e0c1b49fSNick Terrell } 426*e0c1b49fSNick Terrell } 427*e0c1b49fSNick Terrell 428*e0c1b49fSNick Terrell /*! BIT_endOfDStream() : 429*e0c1b49fSNick Terrell * @return : 1 if DStream has _exactly_ reached its end (all bits consumed). 430*e0c1b49fSNick Terrell */ 431*e0c1b49fSNick Terrell MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream) 432*e0c1b49fSNick Terrell { 433*e0c1b49fSNick Terrell return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8)); 434*e0c1b49fSNick Terrell } 435*e0c1b49fSNick Terrell 436*e0c1b49fSNick Terrell 437*e0c1b49fSNick Terrell #endif /* BITSTREAM_H_MODULE */ 438