blob: 052ec45ff230e79a54050c926cce18e9fbc04534 [file] [log] [blame]
Nick Terrellac58c8d2020-03-26 15:19:05 -07001/* ******************************************************************
2 * Common functions of New Generation Entropy library
3 * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
4 *
5 * You can contact the author at :
6 * - FSE+HUF 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****************************************************************** */
inikep63ecd742016-05-13 11:27:56 +020014
15/* *************************************
16* Dependencies
17***************************************/
inikep63ecd742016-05-13 11:27:56 +020018#include "mem.h"
Yann Colleta91ca622016-06-05 01:33:55 +020019#include "error_private.h" /* ERR_*, ERROR */
Yann Colletd0e2cd12016-06-05 00:58:01 +020020#define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */
Yann Collet38b75dd2016-07-24 15:35:59 +020021#include "fse.h"
Yann Colleta91ca622016-06-05 01:33:55 +020022#define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */
Yann Collet38b75dd2016-07-24 15:35:59 +020023#include "huf.h"
inikep63ecd742016-05-13 11:27:56 +020024
25
Yann Collet1f2c95c2017-03-05 21:07:20 -080026/*=== Version ===*/
Yann Collet45960372017-02-15 12:00:03 -080027unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }
28
29
Yann Collet1f2c95c2017-03-05 21:07:20 -080030/*=== Error Management ===*/
inikep63ecd742016-05-13 11:27:56 +020031unsigned FSE_isError(size_t code) { return ERR_isError(code); }
inikep63ecd742016-05-13 11:27:56 +020032const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
33
inikep63ecd742016-05-13 11:27:56 +020034unsigned HUF_isError(size_t code) { return ERR_isError(code); }
inikep63ecd742016-05-13 11:27:56 +020035const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
36
37
38/*-**************************************************************
39* FSE NCount encoding-decoding
40****************************************************************/
Nick Terrell612e9472020-08-17 13:44:49 -070041FORCE_INLINE_TEMPLATE
42size_t FSE_readNCount_body(short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
43 const void* headerBuffer, size_t hbSize)
inikep63ecd742016-05-13 11:27:56 +020044{
45 const BYTE* const istart = (const BYTE*) headerBuffer;
46 const BYTE* const iend = istart + hbSize;
47 const BYTE* ip = istart;
48 int nbBits;
49 int remaining;
50 int threshold;
51 U32 bitStream;
52 int bitCount;
53 unsigned charnum = 0;
Nick Terrell6004c112020-08-14 15:28:59 -070054 unsigned const maxSV1 = *maxSVPtr + 1;
inikep63ecd742016-05-13 11:27:56 +020055 int previous0 = 0;
56
Nick Terrella97e9a62018-05-23 12:16:00 -070057 if (hbSize < 4) {
Nick Terrellf2d09242018-05-23 14:58:58 -070058 /* This function only works when hbSize >= 4 */
Yann Collet21c273d2020-07-16 20:25:15 -070059 char buffer[4] = {0};
Nick Terrellf2d09242018-05-23 14:58:58 -070060 memcpy(buffer, headerBuffer, hbSize);
61 { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
62 buffer, sizeof(buffer));
63 if (FSE_isError(countSize)) return countSize;
64 if (countSize > hbSize) return ERROR(corruption_detected);
65 return countSize;
66 } }
Nick Terrellc92dd112018-05-23 14:47:20 -070067 assert(hbSize >= 4);
Nick Terrella97e9a62018-05-23 12:16:00 -070068
Yann Colletff773bf2018-06-26 17:24:41 -070069 /* init */
70 memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
inikep63ecd742016-05-13 11:27:56 +020071 bitStream = MEM_readLE32(ip);
72 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
73 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
74 bitStream >>= 4;
75 bitCount = 4;
76 *tableLogPtr = nbBits;
77 remaining = (1<<nbBits)+1;
78 threshold = 1<<nbBits;
79 nbBits++;
80
Nick Terrell6004c112020-08-14 15:28:59 -070081 for (;;) {
inikep63ecd742016-05-13 11:27:56 +020082 if (previous0) {
Nick Terrell6004c112020-08-14 15:28:59 -070083 // TODO: Generalize to FSE_countTrailingZeros() or something
84 int repeats = __builtin_ctz(~bitStream) >> 1;
85 while (repeats >= 12) {
86 charnum += 3 * 12;
87 if (ip < iend-6) {
88 ip += 3;
inikep63ecd742016-05-13 11:27:56 +020089 bitStream = MEM_readLE32(ip) >> bitCount;
90 } else {
Nick Terrell6004c112020-08-14 15:28:59 -070091 bitStream >>= 24;
92 bitCount += 24;
93 }
94 repeats = __builtin_ctz(~bitStream) >> 1;
inikep63ecd742016-05-13 11:27:56 +020095 }
Nick Terrell6004c112020-08-14 15:28:59 -070096 charnum += 3 * repeats;
97 bitStream >>= 2 * repeats;
98 bitCount += 2 * repeats;
99
Nick Terrell6004c112020-08-14 15:28:59 -0700100 charnum += bitStream & 3;
inikep63ecd742016-05-13 11:27:56 +0200101 bitCount += 2;
Nick Terrell6004c112020-08-14 15:28:59 -0700102
103 /* This is an error, but break and return an error
104 * at the end, because returning out of a loop makes
105 * it harder for the compiler to optimize.
106 */
107 if (charnum >= maxSV1) break;
108
109 /* We don't need to set the normalized count to 0
110 * because we already memset the whole buffer to 0.
111 */
112
inikep63ecd742016-05-13 11:27:56 +0200113 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
Nick Terrella97e9a62018-05-23 12:16:00 -0700114 assert((bitCount >> 3) <= 3); /* For first condition to work */
inikep63ecd742016-05-13 11:27:56 +0200115 ip += bitCount>>3;
116 bitCount &= 7;
117 bitStream = MEM_readLE32(ip) >> bitCount;
Yann Collet38b75dd2016-07-24 15:35:59 +0200118 } else {
inikep63ecd742016-05-13 11:27:56 +0200119 bitStream >>= 2;
Nick Terrell6004c112020-08-14 15:28:59 -0700120 }
121 }
122 {
123 int const max = (2*threshold-1) - remaining;
Yann Collet45960372017-02-15 12:00:03 -0800124 int count;
inikep63ecd742016-05-13 11:27:56 +0200125
126 if ((bitStream & (threshold-1)) < (U32)max) {
Yann Collet45960372017-02-15 12:00:03 -0800127 count = bitStream & (threshold-1);
128 bitCount += nbBits-1;
inikep63ecd742016-05-13 11:27:56 +0200129 } else {
Yann Collet45960372017-02-15 12:00:03 -0800130 count = bitStream & (2*threshold-1);
inikep63ecd742016-05-13 11:27:56 +0200131 if (count >= threshold) count -= max;
Yann Collet45960372017-02-15 12:00:03 -0800132 bitCount += nbBits;
inikep63ecd742016-05-13 11:27:56 +0200133 }
134
135 count--; /* extra accuracy */
Nick Terrell6004c112020-08-14 15:28:59 -0700136 /* When it matters (small blocks), this is a
137 * predictable branch, because we don't use -1.
138 */
139 if (count >= 0) {
140 remaining -= count;
141 } else {
142 assert(count == -1);
143 remaining += count;
144 }
Yann Collet45960372017-02-15 12:00:03 -0800145 normalizedCounter[charnum++] = (short)count;
inikep63ecd742016-05-13 11:27:56 +0200146 previous0 = !count;
inikep63ecd742016-05-13 11:27:56 +0200147
Nick Terrell6004c112020-08-14 15:28:59 -0700148 assert(threshold > 1);
149 if (remaining < threshold) {
150 /* This branch can be folded into the
151 * threshold update condition because we
152 * know that threshold > 1.
153 */
154 if (remaining <= 1) break;
155 nbBits = BIT_highbit32(remaining) + 1;
156 threshold = 1 << (nbBits - 1);
157 }
158 if (charnum >= maxSV1) break;
159
160 if (LIKELY((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))) {
inikep63ecd742016-05-13 11:27:56 +0200161 ip += bitCount>>3;
162 bitCount &= 7;
163 } else {
164 bitCount -= (int)(8 * (iend - 4 - ip));
165 ip = iend - 4;
166 }
167 bitStream = MEM_readLE32(ip) >> (bitCount & 31);
Nick Terrell6004c112020-08-14 15:28:59 -0700168 } }
Yann Collet38b75dd2016-07-24 15:35:59 +0200169 if (remaining != 1) return ERROR(corruption_detected);
Nick Terrell6004c112020-08-14 15:28:59 -0700170 /* Only possible when there are too many zeros. */
171 if (charnum > maxSV1) return ERROR(maxSymbolValue_tooSmall);
Yann Colletcbc5e9d2016-07-24 18:02:04 +0200172 if (bitCount > 32) return ERROR(corruption_detected);
inikep63ecd742016-05-13 11:27:56 +0200173 *maxSVPtr = charnum-1;
174
175 ip += (bitCount+7)>>3;
inikep63ecd742016-05-13 11:27:56 +0200176 return ip-istart;
177}
Yann Colleta91ca622016-06-05 01:33:55 +0200178
Nick Terrell612e9472020-08-17 13:44:49 -0700179static size_t FSE_readNCount_body_default(
180 short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
181 const void* headerBuffer, size_t hbSize)
182{
183 return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
184}
185
186#if DYNAMIC_BMI2
187TARGET_ATTRIBUTE("bmi2") static size_t FSE_readNCount_body_bmi2(
188 short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
189 const void* headerBuffer, size_t hbSize)
190{
191 return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
192}
193#endif
194
195size_t FSE_readNCount_bmi2(
196 short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
197 const void* headerBuffer, size_t hbSize, int bmi2)
198{
199#if DYNAMIC_BMI2
200 if (bmi2) {
201 return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
202 }
203#endif
204 (void)bmi2;
205 return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
206}
207
208size_t FSE_readNCount(
209 short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
210 const void* headerBuffer, size_t hbSize)
211{
212 return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, /* bmi2 */ 0);
213}
214
215
Yann Colleta91ca622016-06-05 01:33:55 +0200216/*! HUF_readStats() :
217 Read compact Huffman tree, saved by HUF_writeCTable().
218 `huffWeight` is destination buffer.
Yann Colletb89af202016-12-01 18:24:59 -0800219 `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
Yann Colleta91ca622016-06-05 01:33:55 +0200220 @return : size read from `src` , or an error Code .
Yann Collet38b75dd2016-07-24 15:35:59 +0200221 Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
Yann Colleta91ca622016-06-05 01:33:55 +0200222*/
223size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
224 U32* nbSymbolsPtr, U32* tableLogPtr,
225 const void* src, size_t srcSize)
226{
Nick Terrellba1fd172020-08-16 22:22:33 -0700227 U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
Nick Terrell612e9472020-08-17 13:44:49 -0700228 return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp, sizeof(wksp), /* bmi2 */ 0);
Nick Terrellba1fd172020-08-16 22:22:33 -0700229}
230
Nick Terrell612e9472020-08-17 13:44:49 -0700231FORCE_INLINE_TEMPLATE size_t HUF_readStats_body(BYTE* huffWeight, size_t hwSize, U32* rankStats,
Nick Terrellba1fd172020-08-16 22:22:33 -0700232 U32* nbSymbolsPtr, U32* tableLogPtr,
233 const void* src, size_t srcSize,
Nick Terrell612e9472020-08-17 13:44:49 -0700234 void* workSpace, size_t wkspSize,
235 int bmi2)
Nick Terrellba1fd172020-08-16 22:22:33 -0700236{
Yann Colleta91ca622016-06-05 01:33:55 +0200237 U32 weightTotal;
238 const BYTE* ip = (const BYTE*) src;
Nick Terrellccfcc642016-10-17 11:28:02 -0700239 size_t iSize;
Yann Colleta91ca622016-06-05 01:33:55 +0200240 size_t oSize;
241
Nick Terrellccfcc642016-10-17 11:28:02 -0700242 if (!srcSize) return ERROR(srcSize_wrong);
243 iSize = ip[0];
Yann Collet7ed5e332016-07-24 14:26:11 +0200244 /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
Yann Colleta91ca622016-06-05 01:33:55 +0200245
Yann Collet7ed5e332016-07-24 14:26:11 +0200246 if (iSize >= 128) { /* special header */
Yann Collet38b75dd2016-07-24 15:35:59 +0200247 oSize = iSize - 127;
248 iSize = ((oSize+1)/2);
249 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
250 if (oSize >= hwSize) return ERROR(corruption_detected);
251 ip += 1;
252 { U32 n;
253 for (n=0; n<oSize; n+=2) {
254 huffWeight[n] = ip[n/2] >> 4;
255 huffWeight[n+1] = ip[n/2] & 15;
256 } } }
Yann Colleta91ca622016-06-05 01:33:55 +0200257 else { /* header compressed with FSE (normal case) */
258 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
Nick Terrell612e9472020-08-17 13:44:49 -0700259 oSize = FSE_decompress_wksp_bmi2(huffWeight, hwSize-1, ip+1, iSize, 6, workSpace, wkspSize, bmi2); /* max (hwSize-1) values decoded, as last one is implied */
Yann Colleta91ca622016-06-05 01:33:55 +0200260 if (FSE_isError(oSize)) return oSize;
261 }
262
263 /* collect weight stats */
Yann Colletb89af202016-12-01 18:24:59 -0800264 memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
Yann Colleta91ca622016-06-05 01:33:55 +0200265 weightTotal = 0;
266 { U32 n; for (n=0; n<oSize; n++) {
Yann Colletb89af202016-12-01 18:24:59 -0800267 if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200268 rankStats[huffWeight[n]]++;
269 weightTotal += (1 << huffWeight[n]) >> 1;
270 } }
Nick Terrelld7605292016-10-19 11:19:54 -0700271 if (weightTotal == 0) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200272
273 /* get last non-null symbol weight (implied, total must be 2^n) */
274 { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
Yann Colletb89af202016-12-01 18:24:59 -0800275 if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200276 *tableLogPtr = tableLog;
277 /* determine last weight */
278 { U32 const total = 1 << tableLog;
279 U32 const rest = total - weightTotal;
280 U32 const verif = 1 << BIT_highbit32(rest);
281 U32 const lastWeight = BIT_highbit32(rest) + 1;
282 if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
283 huffWeight[oSize] = (BYTE)lastWeight;
284 rankStats[lastWeight]++;
285 } }
286
287 /* check tree construction validity */
288 if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
289
290 /* results */
291 *nbSymbolsPtr = (U32)(oSize+1);
292 return iSize+1;
293}
Nick Terrell612e9472020-08-17 13:44:49 -0700294
295static size_t HUF_readStats_body_default(BYTE* huffWeight, size_t hwSize, U32* rankStats,
296 U32* nbSymbolsPtr, U32* tableLogPtr,
297 const void* src, size_t srcSize,
298 void* workSpace, size_t wkspSize)
299{
300 return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 0);
301}
302
303#if DYNAMIC_BMI2
304static TARGET_ATTRIBUTE("bmi2") size_t HUF_readStats_body_bmi2(BYTE* huffWeight, size_t hwSize, U32* rankStats,
305 U32* nbSymbolsPtr, U32* tableLogPtr,
306 const void* src, size_t srcSize,
307 void* workSpace, size_t wkspSize)
308{
309 return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 1);
310}
311#endif
312
313size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize, U32* rankStats,
314 U32* nbSymbolsPtr, U32* tableLogPtr,
315 const void* src, size_t srcSize,
316 void* workSpace, size_t wkspSize,
317 int bmi2)
318{
319#if DYNAMIC_BMI2
320 if (bmi2) {
321 return HUF_readStats_body_bmi2(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
322 }
323#endif
324 (void)bmi2;
325 return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
326}