/src/c-blosc2/internal-complibs/zstd-1.5.5/common/fse_decompress.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* ****************************************************************** |
2 | | * FSE : Finite State Entropy decoder |
3 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
4 | | * |
5 | | * You can contact the author at : |
6 | | * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy |
7 | | * - Public forum : https://groups.google.com/forum/#!forum/lz4c |
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 | | |
15 | | |
16 | | /* ************************************************************** |
17 | | * Includes |
18 | | ****************************************************************/ |
19 | | #include "debug.h" /* assert */ |
20 | | #include "bitstream.h" |
21 | | #include "compiler.h" |
22 | | #define FSE_STATIC_LINKING_ONLY |
23 | | #include "fse.h" |
24 | | #include "error_private.h" |
25 | | #define ZSTD_DEPS_NEED_MALLOC |
26 | | #include "zstd_deps.h" |
27 | | #include "bits.h" /* ZSTD_highbit32 */ |
28 | | |
29 | | |
30 | | /* ************************************************************** |
31 | | * Error Management |
32 | | ****************************************************************/ |
33 | 88.9k | #define FSE_isError ERR_isError |
34 | | #define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */ |
35 | | |
36 | | |
37 | | /* ************************************************************** |
38 | | * Templates |
39 | | ****************************************************************/ |
40 | | /* |
41 | | designed to be included |
42 | | for type-specific functions (template emulation in C) |
43 | | Objective is to write these functions only once, for improved maintenance |
44 | | */ |
45 | | |
46 | | /* safety checks */ |
47 | | #ifndef FSE_FUNCTION_EXTENSION |
48 | | # error "FSE_FUNCTION_EXTENSION must be defined" |
49 | | #endif |
50 | | #ifndef FSE_FUNCTION_TYPE |
51 | | # error "FSE_FUNCTION_TYPE must be defined" |
52 | | #endif |
53 | | |
54 | | /* Function names */ |
55 | | #define FSE_CAT(X,Y) X##Y |
56 | | #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y) |
57 | | #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y) |
58 | | |
59 | | static size_t FSE_buildDTable_internal(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize) |
60 | 88.7k | { |
61 | 88.7k | void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */ |
62 | 88.7k | FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr); |
63 | 88.7k | U16* symbolNext = (U16*)workSpace; |
64 | 88.7k | BYTE* spread = (BYTE*)(symbolNext + maxSymbolValue + 1); |
65 | | |
66 | 88.7k | U32 const maxSV1 = maxSymbolValue + 1; |
67 | 88.7k | U32 const tableSize = 1 << tableLog; |
68 | 88.7k | U32 highThreshold = tableSize-1; |
69 | | |
70 | | /* Sanity Checks */ |
71 | 88.7k | if (FSE_BUILD_DTABLE_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(maxSymbolValue_tooLarge); |
72 | 88.7k | if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge); |
73 | 88.7k | if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); |
74 | | |
75 | | /* Init, lay down lowprob symbols */ |
76 | 88.7k | { FSE_DTableHeader DTableH; |
77 | 88.7k | DTableH.tableLog = (U16)tableLog; |
78 | 88.7k | DTableH.fastMode = 1; |
79 | 88.7k | { S16 const largeLimit= (S16)(1 << (tableLog-1)); |
80 | 88.7k | U32 s; |
81 | 1.80M | for (s=0; s<maxSV1; s++) { |
82 | 1.71M | if (normalizedCounter[s]==-1) { |
83 | 306k | tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s; |
84 | 306k | symbolNext[s] = 1; |
85 | 1.41M | } else { |
86 | 1.41M | if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0; |
87 | 1.41M | symbolNext[s] = normalizedCounter[s]; |
88 | 1.41M | } } } |
89 | 88.7k | ZSTD_memcpy(dt, &DTableH, sizeof(DTableH)); |
90 | 88.7k | } |
91 | | |
92 | | /* Spread symbols */ |
93 | 88.7k | if (highThreshold == tableSize - 1) { |
94 | 30.1k | size_t const tableMask = tableSize-1; |
95 | 30.1k | size_t const step = FSE_TABLESTEP(tableSize); |
96 | | /* First lay down the symbols in order. |
97 | | * We use a uint64_t to lay down 8 bytes at a time. This reduces branch |
98 | | * misses since small blocks generally have small table logs, so nearly |
99 | | * all symbols have counts <= 8. We ensure we have 8 bytes at the end of |
100 | | * our buffer to handle the over-write. |
101 | | */ |
102 | 30.1k | { |
103 | 30.1k | U64 const add = 0x0101010101010101ull; |
104 | 30.1k | size_t pos = 0; |
105 | 30.1k | U64 sv = 0; |
106 | 30.1k | U32 s; |
107 | 161k | for (s=0; s<maxSV1; ++s, sv += add) { |
108 | 131k | int i; |
109 | 131k | int const n = normalizedCounter[s]; |
110 | 131k | MEM_write64(spread + pos, sv); |
111 | 276k | for (i = 8; i < n; i += 8) { |
112 | 144k | MEM_write64(spread + pos + i, sv); |
113 | 144k | } |
114 | 131k | pos += n; |
115 | 131k | } |
116 | 30.1k | } |
117 | | /* Now we spread those positions across the table. |
118 | | * The benefit of doing it in two stages is that we avoid the |
119 | | * variable size inner loop, which caused lots of branch misses. |
120 | | * Now we can run through all the positions without any branch misses. |
121 | | * We unroll the loop twice, since that is what empirically worked best. |
122 | | */ |
123 | 30.1k | { |
124 | 30.1k | size_t position = 0; |
125 | 30.1k | size_t s; |
126 | 30.1k | size_t const unroll = 2; |
127 | 30.1k | assert(tableSize % unroll == 0); /* FSE_MIN_TABLELOG is 5 */ |
128 | 818k | for (s = 0; s < (size_t)tableSize; s += unroll) { |
129 | 788k | size_t u; |
130 | 2.36M | for (u = 0; u < unroll; ++u) { |
131 | 1.57M | size_t const uPosition = (position + (u * step)) & tableMask; |
132 | 1.57M | tableDecode[uPosition].symbol = spread[s + u]; |
133 | 1.57M | } |
134 | 788k | position = (position + (unroll * step)) & tableMask; |
135 | 788k | } |
136 | 30.1k | assert(position == 0); |
137 | 30.1k | } |
138 | 58.6k | } else { |
139 | 58.6k | U32 const tableMask = tableSize-1; |
140 | 58.6k | U32 const step = FSE_TABLESTEP(tableSize); |
141 | 58.6k | U32 s, position = 0; |
142 | 1.64M | for (s=0; s<maxSV1; s++) { |
143 | 1.58M | int i; |
144 | 3.69M | for (i=0; i<normalizedCounter[s]; i++) { |
145 | 2.11M | tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s; |
146 | 2.11M | position = (position + step) & tableMask; |
147 | 2.42M | while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */ |
148 | 2.11M | } } |
149 | 58.6k | if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */ |
150 | 58.6k | } |
151 | | |
152 | | /* Build Decoding table */ |
153 | 88.7k | { U32 u; |
154 | 4.08M | for (u=0; u<tableSize; u++) { |
155 | 3.99M | FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol); |
156 | 3.99M | U32 const nextState = symbolNext[symbol]++; |
157 | 3.99M | tableDecode[u].nbBits = (BYTE) (tableLog - ZSTD_highbit32(nextState) ); |
158 | 3.99M | tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize); |
159 | 3.99M | } } |
160 | | |
161 | 88.7k | return 0; |
162 | 88.7k | } |
163 | | |
164 | | size_t FSE_buildDTable_wksp(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize) |
165 | 0 | { |
166 | 0 | return FSE_buildDTable_internal(dt, normalizedCounter, maxSymbolValue, tableLog, workSpace, wkspSize); |
167 | 0 | } |
168 | | |
169 | | |
170 | | #ifndef FSE_COMMONDEFS_ONLY |
171 | | |
172 | | /*-******************************************************* |
173 | | * Decompression (Byte symbols) |
174 | | *********************************************************/ |
175 | | |
176 | | FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic( |
177 | | void* dst, size_t maxDstSize, |
178 | | const void* cSrc, size_t cSrcSize, |
179 | | const FSE_DTable* dt, const unsigned fast) |
180 | 88.7k | { |
181 | 88.7k | BYTE* const ostart = (BYTE*) dst; |
182 | 88.7k | BYTE* op = ostart; |
183 | 88.7k | BYTE* const omax = op + maxDstSize; |
184 | 88.7k | BYTE* const olimit = omax-3; |
185 | | |
186 | 88.7k | BIT_DStream_t bitD; |
187 | 88.7k | FSE_DState_t state1; |
188 | 88.7k | FSE_DState_t state2; |
189 | | |
190 | | /* Init */ |
191 | 88.7k | CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize)); |
192 | | |
193 | 88.7k | FSE_initDState(&state1, &bitD, dt); |
194 | 88.7k | FSE_initDState(&state2, &bitD, dt); |
195 | | |
196 | 4.09M | #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD) |
197 | | |
198 | | /* 4 symbols per loop */ |
199 | 450k | for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) { |
200 | 361k | op[0] = FSE_GETSYMBOL(&state1); |
201 | | |
202 | 361k | if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ |
203 | 0 | BIT_reloadDStream(&bitD); |
204 | | |
205 | 361k | op[1] = FSE_GETSYMBOL(&state2); |
206 | | |
207 | 361k | if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ |
208 | 0 | { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } } |
209 | | |
210 | 361k | op[2] = FSE_GETSYMBOL(&state1); |
211 | | |
212 | 361k | if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ |
213 | 0 | BIT_reloadDStream(&bitD); |
214 | | |
215 | 361k | op[3] = FSE_GETSYMBOL(&state2); |
216 | 361k | } |
217 | | |
218 | | /* tail */ |
219 | | /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */ |
220 | 1.30M | while (1) { |
221 | 1.30M | if (op>(omax-2)) return ERROR(dstSize_tooSmall); |
222 | 1.30M | *op++ = FSE_GETSYMBOL(&state1); |
223 | 1.30M | if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) { |
224 | 46.6k | *op++ = FSE_GETSYMBOL(&state2); |
225 | 46.6k | break; |
226 | 46.6k | } |
227 | | |
228 | 1.25M | if (op>(omax-2)) return ERROR(dstSize_tooSmall); |
229 | 1.25M | *op++ = FSE_GETSYMBOL(&state2); |
230 | 1.25M | if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) { |
231 | 41.9k | *op++ = FSE_GETSYMBOL(&state1); |
232 | 41.9k | break; |
233 | 41.9k | } } |
234 | | |
235 | 88.6k | return op-ostart; |
236 | 88.7k | } |
237 | | |
238 | | typedef struct { |
239 | | short ncount[FSE_MAX_SYMBOL_VALUE + 1]; |
240 | | FSE_DTable dtable[1]; /* Dynamically sized */ |
241 | | } FSE_DecompressWksp; |
242 | | |
243 | | |
244 | | FORCE_INLINE_TEMPLATE size_t FSE_decompress_wksp_body( |
245 | | void* dst, size_t dstCapacity, |
246 | | const void* cSrc, size_t cSrcSize, |
247 | | unsigned maxLog, void* workSpace, size_t wkspSize, |
248 | | int bmi2) |
249 | 88.9k | { |
250 | 88.9k | const BYTE* const istart = (const BYTE*)cSrc; |
251 | 88.9k | const BYTE* ip = istart; |
252 | 88.9k | unsigned tableLog; |
253 | 88.9k | unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE; |
254 | 88.9k | FSE_DecompressWksp* const wksp = (FSE_DecompressWksp*)workSpace; |
255 | | |
256 | 88.9k | DEBUG_STATIC_ASSERT((FSE_MAX_SYMBOL_VALUE + 1) % 2 == 0); |
257 | 88.9k | if (wkspSize < sizeof(*wksp)) return ERROR(GENERIC); |
258 | | |
259 | | /* normal FSE decoding mode */ |
260 | 88.9k | { |
261 | 88.9k | size_t const NCountLength = FSE_readNCount_bmi2(wksp->ncount, &maxSymbolValue, &tableLog, istart, cSrcSize, bmi2); |
262 | 88.9k | if (FSE_isError(NCountLength)) return NCountLength; |
263 | 88.8k | if (tableLog > maxLog) return ERROR(tableLog_tooLarge); |
264 | 88.8k | assert(NCountLength <= cSrcSize); |
265 | 88.8k | ip += NCountLength; |
266 | 88.8k | cSrcSize -= NCountLength; |
267 | 88.8k | } |
268 | | |
269 | 88.8k | if (FSE_DECOMPRESS_WKSP_SIZE(tableLog, maxSymbolValue) > wkspSize) return ERROR(tableLog_tooLarge); |
270 | 88.7k | assert(sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog) <= wkspSize); |
271 | 88.7k | workSpace = (BYTE*)workSpace + sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog); |
272 | 88.7k | wkspSize -= sizeof(*wksp) + FSE_DTABLE_SIZE(tableLog); |
273 | | |
274 | 88.7k | CHECK_F( FSE_buildDTable_internal(wksp->dtable, wksp->ncount, maxSymbolValue, tableLog, workSpace, wkspSize) ); |
275 | | |
276 | 88.7k | { |
277 | 88.7k | const void* ptr = wksp->dtable; |
278 | 88.7k | const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr; |
279 | 88.7k | const U32 fastMode = DTableH->fastMode; |
280 | | |
281 | | /* select fast mode (static) */ |
282 | 88.7k | if (fastMode) return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, wksp->dtable, 1); |
283 | 42.0k | return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, wksp->dtable, 0); |
284 | 88.7k | } |
285 | 88.7k | } |
286 | | |
287 | | /* Avoids the FORCE_INLINE of the _body() function. */ |
288 | | static size_t FSE_decompress_wksp_body_default(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize) |
289 | 800 | { |
290 | 800 | return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 0); |
291 | 800 | } |
292 | | |
293 | | #if DYNAMIC_BMI2 |
294 | | BMI2_TARGET_ATTRIBUTE static size_t FSE_decompress_wksp_body_bmi2(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize) |
295 | 88.1k | { |
296 | 88.1k | return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 1); |
297 | 88.1k | } |
298 | | #endif |
299 | | |
300 | | size_t FSE_decompress_wksp_bmi2(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize, int bmi2) |
301 | 88.9k | { |
302 | 88.9k | #if DYNAMIC_BMI2 |
303 | 88.9k | if (bmi2) { |
304 | 88.1k | return FSE_decompress_wksp_body_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize); |
305 | 88.1k | } |
306 | 800 | #endif |
307 | 800 | (void)bmi2; |
308 | 800 | return FSE_decompress_wksp_body_default(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize); |
309 | 88.9k | } |
310 | | |
311 | | #endif /* FSE_COMMONDEFS_ONLY */ |