1 // SPDX-License-Identifier: (GPL-2.0 or BSD-2-Clause)
3 * FSE : Finite State Entropy decoder
4 * Copyright (C) 2013-2015, Yann Collet.
6 * You can contact the author at :
7 * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
10 /* **************************************************************
12 ****************************************************************/
13 #define FORCE_INLINE static __always_inline
15 /* **************************************************************
17 ****************************************************************/
18 #include "bitstream.h"
20 #include <linux/compiler.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h> /* memcpy, memset */
24 /* **************************************************************
26 ****************************************************************/
27 #define FSE_isError ERR_isError
28 #define FSE_STATIC_ASSERT(c) \
30 enum { FSE_static_assert = 1 / (int)(!!(c)) }; \
31 } /* use only *after* variable declarations */
33 /* check and forward error code */
41 /* **************************************************************
43 ****************************************************************/
45 designed to be included
46 for type-specific functions (template emulation in C)
47 Objective is to write these functions only once, for improved maintenance
51 #ifndef FSE_FUNCTION_EXTENSION
52 #error "FSE_FUNCTION_EXTENSION must be defined"
54 #ifndef FSE_FUNCTION_TYPE
55 #error "FSE_FUNCTION_TYPE must be defined"
59 #define FSE_CAT(X, Y) X##Y
60 #define FSE_FUNCTION_NAME(X, Y) FSE_CAT(X, Y)
61 #define FSE_TYPE_NAME(X, Y) FSE_CAT(X, Y)
63 /* Function templates */
65 size_t FSE_buildDTable_wksp(FSE_DTable *dt, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workspace, size_t workspaceSize)
67 void *const tdPtr = dt + 1; /* because *dt is unsigned, 32-bits aligned on 32-bits */
68 FSE_DECODE_TYPE *const tableDecode = (FSE_DECODE_TYPE *)(tdPtr);
69 U16 *symbolNext = (U16 *)workspace;
71 U32 const maxSV1 = maxSymbolValue + 1;
72 U32 const tableSize = 1 << tableLog;
73 U32 highThreshold = tableSize - 1;
76 if (workspaceSize < sizeof(U16) * (FSE_MAX_SYMBOL_VALUE + 1))
77 return ERROR(tableLog_tooLarge);
78 if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE)
79 return ERROR(maxSymbolValue_tooLarge);
80 if (tableLog > FSE_MAX_TABLELOG)
81 return ERROR(tableLog_tooLarge);
83 /* Init, lay down lowprob symbols */
85 FSE_DTableHeader DTableH;
86 DTableH.tableLog = (U16)tableLog;
89 S16 const largeLimit = (S16)(1 << (tableLog - 1));
91 for (s = 0; s < maxSV1; s++) {
92 if (normalizedCounter[s] == -1) {
93 tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
96 if (normalizedCounter[s] >= largeLimit)
98 symbolNext[s] = normalizedCounter[s];
102 memcpy(dt, &DTableH, sizeof(DTableH));
107 U32 const tableMask = tableSize - 1;
108 U32 const step = FSE_TABLESTEP(tableSize);
110 for (s = 0; s < maxSV1; s++) {
112 for (i = 0; i < normalizedCounter[s]; i++) {
113 tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
114 position = (position + step) & tableMask;
115 while (position > highThreshold)
116 position = (position + step) & tableMask; /* lowprob area */
120 return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
123 /* Build Decoding table */
126 for (u = 0; u < tableSize; u++) {
127 FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
128 U16 nextState = symbolNext[symbol]++;
129 tableDecode[u].nbBits = (BYTE)(tableLog - BIT_highbit32((U32)nextState));
130 tableDecode[u].newState = (U16)((nextState << tableDecode[u].nbBits) - tableSize);
137 /*-*******************************************************
138 * Decompression (Byte symbols)
139 *********************************************************/
140 size_t FSE_buildDTable_rle(FSE_DTable *dt, BYTE symbolValue)
143 FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
145 FSE_decode_t *const cell = (FSE_decode_t *)dPtr;
147 DTableH->tableLog = 0;
148 DTableH->fastMode = 0;
151 cell->symbol = symbolValue;
157 size_t FSE_buildDTable_raw(FSE_DTable *dt, unsigned nbBits)
160 FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
162 FSE_decode_t *const dinfo = (FSE_decode_t *)dPtr;
163 const unsigned tableSize = 1 << nbBits;
164 const unsigned tableMask = tableSize - 1;
165 const unsigned maxSV1 = tableMask + 1;
170 return ERROR(GENERIC); /* min size */
172 /* Build Decoding Table */
173 DTableH->tableLog = (U16)nbBits;
174 DTableH->fastMode = 1;
175 for (s = 0; s < maxSV1; s++) {
176 dinfo[s].newState = 0;
177 dinfo[s].symbol = (BYTE)s;
178 dinfo[s].nbBits = (BYTE)nbBits;
184 FORCE_INLINE size_t FSE_decompress_usingDTable_generic(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt,
187 BYTE *const ostart = (BYTE *)dst;
189 BYTE *const omax = op + maxDstSize;
190 BYTE *const olimit = omax - 3;
197 CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
199 FSE_initDState(&state1, &bitD, dt);
200 FSE_initDState(&state2, &bitD, dt);
202 #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
204 /* 4 symbols per loop */
205 for (; (BIT_reloadDStream(&bitD) == BIT_DStream_unfinished) & (op < olimit); op += 4) {
206 op[0] = FSE_GETSYMBOL(&state1);
208 if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
209 BIT_reloadDStream(&bitD);
211 op[1] = FSE_GETSYMBOL(&state2);
213 if (FSE_MAX_TABLELOG * 4 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
215 if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) {
221 op[2] = FSE_GETSYMBOL(&state1);
223 if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
224 BIT_reloadDStream(&bitD);
226 op[3] = FSE_GETSYMBOL(&state2);
230 /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
233 return ERROR(dstSize_tooSmall);
234 *op++ = FSE_GETSYMBOL(&state1);
235 if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
236 *op++ = FSE_GETSYMBOL(&state2);
241 return ERROR(dstSize_tooSmall);
242 *op++ = FSE_GETSYMBOL(&state2);
243 if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
244 *op++ = FSE_GETSYMBOL(&state1);
252 size_t FSE_decompress_usingDTable(void *dst, size_t originalSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt)
254 const void *ptr = dt;
255 const FSE_DTableHeader *DTableH = (const FSE_DTableHeader *)ptr;
256 const U32 fastMode = DTableH->fastMode;
258 /* select fast mode (static) */
260 return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
261 return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
264 size_t FSE_decompress_wksp(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workspace, size_t workspaceSize)
266 const BYTE *const istart = (const BYTE *)cSrc;
267 const BYTE *ip = istart;
269 unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
274 size_t spaceUsed32 = 0;
276 FSE_STATIC_ASSERT(sizeof(FSE_DTable) == sizeof(U32));
278 dt = (FSE_DTable *)((U32 *)workspace + spaceUsed32);
279 spaceUsed32 += FSE_DTABLE_SIZE_U32(maxLog);
280 counting = (short *)((U32 *)workspace + spaceUsed32);
281 spaceUsed32 += ALIGN(sizeof(short) * (FSE_MAX_SYMBOL_VALUE + 1), sizeof(U32)) >> 2;
283 if ((spaceUsed32 << 2) > workspaceSize)
284 return ERROR(tableLog_tooLarge);
285 workspace = (U32 *)workspace + spaceUsed32;
286 workspaceSize -= (spaceUsed32 << 2);
288 /* normal FSE decoding mode */
289 NCountLength = FSE_readNCount(counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
290 if (FSE_isError(NCountLength))
292 // if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size; supposed to be already checked in NCountLength, only remaining
293 // case : NCountLength==cSrcSize */
294 if (tableLog > maxLog)
295 return ERROR(tableLog_tooLarge);
297 cSrcSize -= NCountLength;
299 CHECK_F(FSE_buildDTable_wksp(dt, counting, maxSymbolValue, tableLog, workspace, workspaceSize));
301 return FSE_decompress_usingDTable(dst, dstCapacity, ip, cSrcSize, dt); /* always return, even if it is an error code */