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