blob: 2cc2b4dce09a7b8b478d59d9579b093f72610251 [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 Terrell8f8bd2d2020-08-18 16:57:35 -070041static U32 FSE_ctz(U32 val)
42{
43 assert(val != 0);
44 {
45# if defined(_MSC_VER) /* Visual */
46 unsigned long r=0;
47 return _BitScanForward(&r, val) ? (unsigned)r : 0;
48# elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
49 return __builtin_ctz(val);
50# elif defined(__ICCARM__) /* IAR Intrinsic */
51 return __CTZ(val);
52# else /* Software version */
53 U32 count = 0;
54 while ((val & 1) == 0) {
55 val >>= 1;
56 ++count;
57 }
58 return count;
59# endif
60 }
61}
62
Nick Terrell612e9472020-08-17 13:44:49 -070063FORCE_INLINE_TEMPLATE
64size_t FSE_readNCount_body(short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
65 const void* headerBuffer, size_t hbSize)
inikep63ecd742016-05-13 11:27:56 +020066{
67 const BYTE* const istart = (const BYTE*) headerBuffer;
68 const BYTE* const iend = istart + hbSize;
69 const BYTE* ip = istart;
70 int nbBits;
71 int remaining;
72 int threshold;
73 U32 bitStream;
74 int bitCount;
75 unsigned charnum = 0;
Nick Terrell6004c112020-08-14 15:28:59 -070076 unsigned const maxSV1 = *maxSVPtr + 1;
inikep63ecd742016-05-13 11:27:56 +020077 int previous0 = 0;
78
Nick Terrell8f8bd2d2020-08-18 16:57:35 -070079 if (hbSize < 8) {
Nick Terrellf2d09242018-05-23 14:58:58 -070080 /* This function only works when hbSize >= 4 */
Nick Terrell8f8bd2d2020-08-18 16:57:35 -070081 char buffer[8] = {0};
Nick Terrellf2d09242018-05-23 14:58:58 -070082 memcpy(buffer, headerBuffer, hbSize);
83 { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
84 buffer, sizeof(buffer));
85 if (FSE_isError(countSize)) return countSize;
86 if (countSize > hbSize) return ERROR(corruption_detected);
87 return countSize;
88 } }
Nick Terrellc92dd112018-05-23 14:47:20 -070089 assert(hbSize >= 4);
Nick Terrella97e9a62018-05-23 12:16:00 -070090
Yann Colletff773bf2018-06-26 17:24:41 -070091 /* init */
92 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 +020093 bitStream = MEM_readLE32(ip);
94 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
95 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
96 bitStream >>= 4;
97 bitCount = 4;
98 *tableLogPtr = nbBits;
99 remaining = (1<<nbBits)+1;
100 threshold = 1<<nbBits;
101 nbBits++;
102
Nick Terrell6004c112020-08-14 15:28:59 -0700103 for (;;) {
inikep63ecd742016-05-13 11:27:56 +0200104 if (previous0) {
Nick Terrell8f8bd2d2020-08-18 16:57:35 -0700105 int repeats = FSE_ctz(~bitStream | 0x80000000) >> 1;
Nick Terrell6004c112020-08-14 15:28:59 -0700106 while (repeats >= 12) {
107 charnum += 3 * 12;
Nick Terrell8f8bd2d2020-08-18 16:57:35 -0700108 if (ip <= iend-7) {
Nick Terrell6004c112020-08-14 15:28:59 -0700109 ip += 3;
inikep63ecd742016-05-13 11:27:56 +0200110 bitStream = MEM_readLE32(ip) >> bitCount;
111 } else {
Nick Terrell6004c112020-08-14 15:28:59 -0700112 bitStream >>= 24;
113 bitCount += 24;
114 }
Nick Terrell8f8bd2d2020-08-18 16:57:35 -0700115 repeats = FSE_ctz(~bitStream | 0x80000000) >> 1;
inikep63ecd742016-05-13 11:27:56 +0200116 }
Nick Terrell6004c112020-08-14 15:28:59 -0700117 charnum += 3 * repeats;
118 bitStream >>= 2 * repeats;
119 bitCount += 2 * repeats;
120
Nick Terrell6004c112020-08-14 15:28:59 -0700121 charnum += bitStream & 3;
inikep63ecd742016-05-13 11:27:56 +0200122 bitCount += 2;
Nick Terrell6004c112020-08-14 15:28:59 -0700123
124 /* This is an error, but break and return an error
125 * at the end, because returning out of a loop makes
126 * it harder for the compiler to optimize.
127 */
128 if (charnum >= maxSV1) break;
129
130 /* We don't need to set the normalized count to 0
131 * because we already memset the whole buffer to 0.
132 */
133
inikep63ecd742016-05-13 11:27:56 +0200134 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
Nick Terrella97e9a62018-05-23 12:16:00 -0700135 assert((bitCount >> 3) <= 3); /* For first condition to work */
inikep63ecd742016-05-13 11:27:56 +0200136 ip += bitCount>>3;
137 bitCount &= 7;
138 bitStream = MEM_readLE32(ip) >> bitCount;
Yann Collet38b75dd2016-07-24 15:35:59 +0200139 } else {
inikep63ecd742016-05-13 11:27:56 +0200140 bitStream >>= 2;
Nick Terrell6004c112020-08-14 15:28:59 -0700141 }
142 }
143 {
144 int const max = (2*threshold-1) - remaining;
Yann Collet45960372017-02-15 12:00:03 -0800145 int count;
inikep63ecd742016-05-13 11:27:56 +0200146
147 if ((bitStream & (threshold-1)) < (U32)max) {
Yann Collet45960372017-02-15 12:00:03 -0800148 count = bitStream & (threshold-1);
149 bitCount += nbBits-1;
inikep63ecd742016-05-13 11:27:56 +0200150 } else {
Yann Collet45960372017-02-15 12:00:03 -0800151 count = bitStream & (2*threshold-1);
inikep63ecd742016-05-13 11:27:56 +0200152 if (count >= threshold) count -= max;
Yann Collet45960372017-02-15 12:00:03 -0800153 bitCount += nbBits;
inikep63ecd742016-05-13 11:27:56 +0200154 }
155
156 count--; /* extra accuracy */
Nick Terrell6004c112020-08-14 15:28:59 -0700157 /* When it matters (small blocks), this is a
158 * predictable branch, because we don't use -1.
159 */
160 if (count >= 0) {
161 remaining -= count;
162 } else {
163 assert(count == -1);
164 remaining += count;
165 }
Yann Collet45960372017-02-15 12:00:03 -0800166 normalizedCounter[charnum++] = (short)count;
inikep63ecd742016-05-13 11:27:56 +0200167 previous0 = !count;
inikep63ecd742016-05-13 11:27:56 +0200168
Nick Terrell6004c112020-08-14 15:28:59 -0700169 assert(threshold > 1);
170 if (remaining < threshold) {
171 /* This branch can be folded into the
172 * threshold update condition because we
173 * know that threshold > 1.
174 */
175 if (remaining <= 1) break;
176 nbBits = BIT_highbit32(remaining) + 1;
177 threshold = 1 << (nbBits - 1);
178 }
179 if (charnum >= maxSV1) break;
180
181 if (LIKELY((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))) {
inikep63ecd742016-05-13 11:27:56 +0200182 ip += bitCount>>3;
183 bitCount &= 7;
184 } else {
185 bitCount -= (int)(8 * (iend - 4 - ip));
186 ip = iend - 4;
187 }
188 bitStream = MEM_readLE32(ip) >> (bitCount & 31);
Nick Terrell6004c112020-08-14 15:28:59 -0700189 } }
Yann Collet38b75dd2016-07-24 15:35:59 +0200190 if (remaining != 1) return ERROR(corruption_detected);
Nick Terrell6004c112020-08-14 15:28:59 -0700191 /* Only possible when there are too many zeros. */
192 if (charnum > maxSV1) return ERROR(maxSymbolValue_tooSmall);
Yann Colletcbc5e9d2016-07-24 18:02:04 +0200193 if (bitCount > 32) return ERROR(corruption_detected);
inikep63ecd742016-05-13 11:27:56 +0200194 *maxSVPtr = charnum-1;
195
196 ip += (bitCount+7)>>3;
inikep63ecd742016-05-13 11:27:56 +0200197 return ip-istart;
198}
Yann Colleta91ca622016-06-05 01:33:55 +0200199
Nick Terrell612e9472020-08-17 13:44:49 -0700200static size_t FSE_readNCount_body_default(
201 short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
202 const void* headerBuffer, size_t hbSize)
203{
204 return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
205}
206
207#if DYNAMIC_BMI2
208TARGET_ATTRIBUTE("bmi2") static size_t FSE_readNCount_body_bmi2(
209 short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
210 const void* headerBuffer, size_t hbSize)
211{
212 return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
213}
214#endif
215
216size_t FSE_readNCount_bmi2(
217 short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
218 const void* headerBuffer, size_t hbSize, int bmi2)
219{
220#if DYNAMIC_BMI2
221 if (bmi2) {
222 return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
223 }
224#endif
225 (void)bmi2;
226 return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
227}
228
229size_t FSE_readNCount(
230 short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
231 const void* headerBuffer, size_t hbSize)
232{
233 return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, /* bmi2 */ 0);
234}
235
236
Yann Colleta91ca622016-06-05 01:33:55 +0200237/*! HUF_readStats() :
238 Read compact Huffman tree, saved by HUF_writeCTable().
239 `huffWeight` is destination buffer.
Yann Colletb89af202016-12-01 18:24:59 -0800240 `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
Yann Colleta91ca622016-06-05 01:33:55 +0200241 @return : size read from `src` , or an error Code .
Yann Collet38b75dd2016-07-24 15:35:59 +0200242 Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
Yann Colleta91ca622016-06-05 01:33:55 +0200243*/
244size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
245 U32* nbSymbolsPtr, U32* tableLogPtr,
246 const void* src, size_t srcSize)
247{
Nick Terrellba1fd172020-08-16 22:22:33 -0700248 U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
Nick Terrell612e9472020-08-17 13:44:49 -0700249 return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp, sizeof(wksp), /* bmi2 */ 0);
Nick Terrellba1fd172020-08-16 22:22:33 -0700250}
251
Nick Terrell612e9472020-08-17 13:44:49 -0700252FORCE_INLINE_TEMPLATE size_t HUF_readStats_body(BYTE* huffWeight, size_t hwSize, U32* rankStats,
Nick Terrellba1fd172020-08-16 22:22:33 -0700253 U32* nbSymbolsPtr, U32* tableLogPtr,
254 const void* src, size_t srcSize,
Nick Terrell612e9472020-08-17 13:44:49 -0700255 void* workSpace, size_t wkspSize,
256 int bmi2)
Nick Terrellba1fd172020-08-16 22:22:33 -0700257{
Yann Colleta91ca622016-06-05 01:33:55 +0200258 U32 weightTotal;
259 const BYTE* ip = (const BYTE*) src;
Nick Terrellccfcc642016-10-17 11:28:02 -0700260 size_t iSize;
Yann Colleta91ca622016-06-05 01:33:55 +0200261 size_t oSize;
262
Nick Terrellccfcc642016-10-17 11:28:02 -0700263 if (!srcSize) return ERROR(srcSize_wrong);
264 iSize = ip[0];
Yann Collet7ed5e332016-07-24 14:26:11 +0200265 /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
Yann Colleta91ca622016-06-05 01:33:55 +0200266
Yann Collet7ed5e332016-07-24 14:26:11 +0200267 if (iSize >= 128) { /* special header */
Yann Collet38b75dd2016-07-24 15:35:59 +0200268 oSize = iSize - 127;
269 iSize = ((oSize+1)/2);
270 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
271 if (oSize >= hwSize) return ERROR(corruption_detected);
272 ip += 1;
273 { U32 n;
274 for (n=0; n<oSize; n+=2) {
275 huffWeight[n] = ip[n/2] >> 4;
276 huffWeight[n+1] = ip[n/2] & 15;
277 } } }
Yann Colleta91ca622016-06-05 01:33:55 +0200278 else { /* header compressed with FSE (normal case) */
279 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
Nick Terrell612e9472020-08-17 13:44:49 -0700280 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 +0200281 if (FSE_isError(oSize)) return oSize;
282 }
283
284 /* collect weight stats */
Yann Colletb89af202016-12-01 18:24:59 -0800285 memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
Yann Colleta91ca622016-06-05 01:33:55 +0200286 weightTotal = 0;
287 { U32 n; for (n=0; n<oSize; n++) {
Yann Colletb89af202016-12-01 18:24:59 -0800288 if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200289 rankStats[huffWeight[n]]++;
290 weightTotal += (1 << huffWeight[n]) >> 1;
291 } }
Nick Terrelld7605292016-10-19 11:19:54 -0700292 if (weightTotal == 0) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200293
294 /* get last non-null symbol weight (implied, total must be 2^n) */
295 { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
Yann Colletb89af202016-12-01 18:24:59 -0800296 if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200297 *tableLogPtr = tableLog;
298 /* determine last weight */
299 { U32 const total = 1 << tableLog;
300 U32 const rest = total - weightTotal;
301 U32 const verif = 1 << BIT_highbit32(rest);
302 U32 const lastWeight = BIT_highbit32(rest) + 1;
303 if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
304 huffWeight[oSize] = (BYTE)lastWeight;
305 rankStats[lastWeight]++;
306 } }
307
308 /* check tree construction validity */
309 if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
310
311 /* results */
312 *nbSymbolsPtr = (U32)(oSize+1);
313 return iSize+1;
314}
Nick Terrell612e9472020-08-17 13:44:49 -0700315
316static size_t HUF_readStats_body_default(BYTE* huffWeight, size_t hwSize, U32* rankStats,
317 U32* nbSymbolsPtr, U32* tableLogPtr,
318 const void* src, size_t srcSize,
319 void* workSpace, size_t wkspSize)
320{
321 return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 0);
322}
323
324#if DYNAMIC_BMI2
325static TARGET_ATTRIBUTE("bmi2") size_t HUF_readStats_body_bmi2(BYTE* huffWeight, size_t hwSize, U32* rankStats,
326 U32* nbSymbolsPtr, U32* tableLogPtr,
327 const void* src, size_t srcSize,
328 void* workSpace, size_t wkspSize)
329{
330 return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 1);
331}
332#endif
333
334size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize, U32* rankStats,
335 U32* nbSymbolsPtr, U32* tableLogPtr,
336 const void* src, size_t srcSize,
337 void* workSpace, size_t wkspSize,
338 int bmi2)
339{
340#if DYNAMIC_BMI2
341 if (bmi2) {
342 return HUF_readStats_body_bmi2(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
343 }
344#endif
345 (void)bmi2;
346 return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
347}