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