Brandon Maier | 4b9b25d | 2023-01-12 10:27:45 -0600 | [diff] [blame] | 1 | /* ****************************************************************** |
| 2 | * Common functions of New Generation Entropy library |
| 3 | * Copyright (c) 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 | ****************************************************************** */ |
| 14 | |
| 15 | /* ************************************* |
| 16 | * Dependencies |
| 17 | ***************************************/ |
| 18 | #include "mem.h" |
| 19 | #include "error_private.h" /* ERR_*, ERROR */ |
| 20 | #define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */ |
| 21 | #include "fse.h" |
| 22 | #define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */ |
| 23 | #include "huf.h" |
| 24 | |
Brandon Maier | 4b9b25d | 2023-01-12 10:27:45 -0600 | [diff] [blame] | 25 | /*=== Version ===*/ |
| 26 | unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; } |
| 27 | |
Brandon Maier | 4b9b25d | 2023-01-12 10:27:45 -0600 | [diff] [blame] | 28 | /*=== Error Management ===*/ |
| 29 | unsigned FSE_isError(size_t code) { return ERR_isError(code); } |
| 30 | const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); } |
| 31 | |
| 32 | unsigned HUF_isError(size_t code) { return ERR_isError(code); } |
| 33 | const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); } |
| 34 | |
Brandon Maier | 4b9b25d | 2023-01-12 10:27:45 -0600 | [diff] [blame] | 35 | /*-************************************************************** |
| 36 | * FSE NCount encoding-decoding |
| 37 | ****************************************************************/ |
| 38 | static U32 FSE_ctz(U32 val) |
| 39 | { |
| 40 | assert(val != 0); |
| 41 | { |
| 42 | # if (__GNUC__ >= 3) /* GCC Intrinsic */ |
| 43 | return __builtin_ctz(val); |
| 44 | # else /* Software version */ |
| 45 | U32 count = 0; |
| 46 | while ((val & 1) == 0) { |
| 47 | val >>= 1; |
| 48 | ++count; |
| 49 | } |
| 50 | return count; |
| 51 | # endif |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | FORCE_INLINE_TEMPLATE |
| 56 | size_t FSE_readNCount_body(short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
| 57 | const void* headerBuffer, size_t hbSize) |
| 58 | { |
| 59 | const BYTE* const istart = (const BYTE*) headerBuffer; |
| 60 | const BYTE* const iend = istart + hbSize; |
| 61 | const BYTE* ip = istart; |
| 62 | int nbBits; |
| 63 | int remaining; |
| 64 | int threshold; |
| 65 | U32 bitStream; |
| 66 | int bitCount; |
| 67 | unsigned charnum = 0; |
| 68 | unsigned const maxSV1 = *maxSVPtr + 1; |
| 69 | int previous0 = 0; |
| 70 | |
| 71 | if (hbSize < 8) { |
| 72 | /* This function only works when hbSize >= 8 */ |
| 73 | char buffer[8] = {0}; |
| 74 | ZSTD_memcpy(buffer, headerBuffer, hbSize); |
| 75 | { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr, |
| 76 | buffer, sizeof(buffer)); |
| 77 | if (FSE_isError(countSize)) return countSize; |
| 78 | if (countSize > hbSize) return ERROR(corruption_detected); |
| 79 | return countSize; |
| 80 | } } |
| 81 | assert(hbSize >= 8); |
| 82 | |
| 83 | /* init */ |
| 84 | ZSTD_memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */ |
| 85 | bitStream = MEM_readLE32(ip); |
| 86 | nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */ |
| 87 | if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge); |
| 88 | bitStream >>= 4; |
| 89 | bitCount = 4; |
| 90 | *tableLogPtr = nbBits; |
| 91 | remaining = (1<<nbBits)+1; |
| 92 | threshold = 1<<nbBits; |
| 93 | nbBits++; |
| 94 | |
| 95 | for (;;) { |
| 96 | if (previous0) { |
| 97 | /* Count the number of repeats. Each time the |
| 98 | * 2-bit repeat code is 0b11 there is another |
| 99 | * repeat. |
| 100 | * Avoid UB by setting the high bit to 1. |
| 101 | */ |
| 102 | int repeats = FSE_ctz(~bitStream | 0x80000000) >> 1; |
| 103 | while (repeats >= 12) { |
| 104 | charnum += 3 * 12; |
| 105 | if (LIKELY(ip <= iend-7)) { |
| 106 | ip += 3; |
| 107 | } else { |
| 108 | bitCount -= (int)(8 * (iend - 7 - ip)); |
| 109 | bitCount &= 31; |
| 110 | ip = iend - 4; |
| 111 | } |
| 112 | bitStream = MEM_readLE32(ip) >> bitCount; |
| 113 | repeats = FSE_ctz(~bitStream | 0x80000000) >> 1; |
| 114 | } |
| 115 | charnum += 3 * repeats; |
| 116 | bitStream >>= 2 * repeats; |
| 117 | bitCount += 2 * repeats; |
| 118 | |
| 119 | /* Add the final repeat which isn't 0b11. */ |
| 120 | assert((bitStream & 3) < 3); |
| 121 | charnum += bitStream & 3; |
| 122 | bitCount += 2; |
| 123 | |
| 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 | |
| 134 | if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) { |
| 135 | assert((bitCount >> 3) <= 3); /* For first condition to work */ |
| 136 | ip += bitCount>>3; |
| 137 | bitCount &= 7; |
| 138 | } else { |
| 139 | bitCount -= (int)(8 * (iend - 4 - ip)); |
| 140 | bitCount &= 31; |
| 141 | ip = iend - 4; |
| 142 | } |
| 143 | bitStream = MEM_readLE32(ip) >> bitCount; |
| 144 | } |
| 145 | { |
| 146 | int const max = (2*threshold-1) - remaining; |
| 147 | int count; |
| 148 | |
| 149 | if ((bitStream & (threshold-1)) < (U32)max) { |
| 150 | count = bitStream & (threshold-1); |
| 151 | bitCount += nbBits-1; |
| 152 | } else { |
| 153 | count = bitStream & (2*threshold-1); |
| 154 | if (count >= threshold) count -= max; |
| 155 | bitCount += nbBits; |
| 156 | } |
| 157 | |
| 158 | count--; /* extra accuracy */ |
| 159 | /* When it matters (small blocks), this is a |
| 160 | * predictable branch, because we don't use -1. |
| 161 | */ |
| 162 | if (count >= 0) { |
| 163 | remaining -= count; |
| 164 | } else { |
| 165 | assert(count == -1); |
| 166 | remaining += count; |
| 167 | } |
| 168 | normalizedCounter[charnum++] = (short)count; |
| 169 | previous0 = !count; |
| 170 | |
| 171 | assert(threshold > 1); |
| 172 | if (remaining < threshold) { |
| 173 | /* This branch can be folded into the |
| 174 | * threshold update condition because we |
| 175 | * know that threshold > 1. |
| 176 | */ |
| 177 | if (remaining <= 1) break; |
| 178 | nbBits = BIT_highbit32(remaining) + 1; |
| 179 | threshold = 1 << (nbBits - 1); |
| 180 | } |
| 181 | if (charnum >= maxSV1) break; |
| 182 | |
| 183 | if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) { |
| 184 | ip += bitCount>>3; |
| 185 | bitCount &= 7; |
| 186 | } else { |
| 187 | bitCount -= (int)(8 * (iend - 4 - ip)); |
| 188 | bitCount &= 31; |
| 189 | ip = iend - 4; |
| 190 | } |
| 191 | bitStream = MEM_readLE32(ip) >> bitCount; |
| 192 | } } |
| 193 | if (remaining != 1) return ERROR(corruption_detected); |
| 194 | /* Only possible when there are too many zeros. */ |
| 195 | if (charnum > maxSV1) return ERROR(maxSymbolValue_tooSmall); |
| 196 | if (bitCount > 32) return ERROR(corruption_detected); |
| 197 | *maxSVPtr = charnum-1; |
| 198 | |
| 199 | ip += (bitCount+7)>>3; |
| 200 | return ip-istart; |
| 201 | } |
| 202 | |
| 203 | /* Avoids the FORCE_INLINE of the _body() function. */ |
| 204 | static size_t FSE_readNCount_body_default( |
| 205 | short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
| 206 | const void* headerBuffer, size_t hbSize) |
| 207 | { |
| 208 | return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); |
| 209 | } |
| 210 | |
| 211 | #if DYNAMIC_BMI2 |
| 212 | BMI2_TARGET_ATTRIBUTE static size_t FSE_readNCount_body_bmi2( |
| 213 | short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
| 214 | const void* headerBuffer, size_t hbSize) |
| 215 | { |
| 216 | return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); |
| 217 | } |
| 218 | #endif |
| 219 | |
| 220 | size_t FSE_readNCount_bmi2( |
| 221 | short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
| 222 | const void* headerBuffer, size_t hbSize, int bmi2) |
| 223 | { |
| 224 | #if DYNAMIC_BMI2 |
| 225 | if (bmi2) { |
| 226 | return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); |
| 227 | } |
| 228 | #endif |
| 229 | (void)bmi2; |
| 230 | return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); |
| 231 | } |
| 232 | |
| 233 | size_t FSE_readNCount( |
| 234 | short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
| 235 | const void* headerBuffer, size_t hbSize) |
| 236 | { |
| 237 | return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, /* bmi2 */ 0); |
| 238 | } |
| 239 | |
Brandon Maier | 4b9b25d | 2023-01-12 10:27:45 -0600 | [diff] [blame] | 240 | /*! HUF_readStats() : |
| 241 | Read compact Huffman tree, saved by HUF_writeCTable(). |
| 242 | `huffWeight` is destination buffer. |
| 243 | `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32. |
| 244 | @return : size read from `src` , or an error Code . |
| 245 | Note : Needed by HUF_readCTable() and HUF_readDTableX?() . |
| 246 | */ |
| 247 | size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats, |
| 248 | U32* nbSymbolsPtr, U32* tableLogPtr, |
| 249 | const void* src, size_t srcSize) |
| 250 | { |
| 251 | U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32]; |
| 252 | return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp, sizeof(wksp), /* bmi2 */ 0); |
| 253 | } |
| 254 | |
| 255 | FORCE_INLINE_TEMPLATE size_t |
| 256 | HUF_readStats_body(BYTE* huffWeight, size_t hwSize, U32* rankStats, |
| 257 | U32* nbSymbolsPtr, U32* tableLogPtr, |
| 258 | const void* src, size_t srcSize, |
| 259 | void* workSpace, size_t wkspSize, |
| 260 | int bmi2) |
| 261 | { |
| 262 | U32 weightTotal; |
| 263 | const BYTE* ip = (const BYTE*) src; |
| 264 | size_t iSize; |
| 265 | size_t oSize; |
| 266 | |
| 267 | if (!srcSize) return ERROR(srcSize_wrong); |
| 268 | iSize = ip[0]; |
| 269 | /* ZSTD_memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */ |
| 270 | |
| 271 | if (iSize >= 128) { /* special header */ |
| 272 | oSize = iSize - 127; |
| 273 | iSize = ((oSize+1)/2); |
| 274 | if (iSize+1 > srcSize) return ERROR(srcSize_wrong); |
| 275 | if (oSize >= hwSize) return ERROR(corruption_detected); |
| 276 | ip += 1; |
| 277 | { U32 n; |
| 278 | for (n=0; n<oSize; n+=2) { |
| 279 | huffWeight[n] = ip[n/2] >> 4; |
| 280 | huffWeight[n+1] = ip[n/2] & 15; |
| 281 | } } } |
| 282 | else { /* header compressed with FSE (normal case) */ |
| 283 | if (iSize+1 > srcSize) return ERROR(srcSize_wrong); |
| 284 | /* max (hwSize-1) values decoded, as last one is implied */ |
| 285 | oSize = FSE_decompress_wksp_bmi2(huffWeight, hwSize-1, ip+1, iSize, 6, workSpace, wkspSize, bmi2); |
| 286 | if (FSE_isError(oSize)) return oSize; |
| 287 | } |
| 288 | |
| 289 | /* collect weight stats */ |
| 290 | ZSTD_memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32)); |
| 291 | weightTotal = 0; |
| 292 | { U32 n; for (n=0; n<oSize; n++) { |
| 293 | if (huffWeight[n] > HUF_TABLELOG_MAX) return ERROR(corruption_detected); |
| 294 | rankStats[huffWeight[n]]++; |
| 295 | weightTotal += (1 << huffWeight[n]) >> 1; |
| 296 | } } |
| 297 | if (weightTotal == 0) return ERROR(corruption_detected); |
| 298 | |
| 299 | /* get last non-null symbol weight (implied, total must be 2^n) */ |
| 300 | { U32 const tableLog = BIT_highbit32(weightTotal) + 1; |
| 301 | if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected); |
| 302 | *tableLogPtr = tableLog; |
| 303 | /* determine last weight */ |
| 304 | { U32 const total = 1 << tableLog; |
| 305 | U32 const rest = total - weightTotal; |
| 306 | U32 const verif = 1 << BIT_highbit32(rest); |
| 307 | U32 const lastWeight = BIT_highbit32(rest) + 1; |
| 308 | if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */ |
| 309 | huffWeight[oSize] = (BYTE)lastWeight; |
| 310 | rankStats[lastWeight]++; |
| 311 | } } |
| 312 | |
| 313 | /* check tree construction validity */ |
| 314 | if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */ |
| 315 | |
| 316 | /* results */ |
| 317 | *nbSymbolsPtr = (U32)(oSize+1); |
| 318 | return iSize+1; |
| 319 | } |
| 320 | |
| 321 | /* Avoids the FORCE_INLINE of the _body() function. */ |
| 322 | static size_t HUF_readStats_body_default(BYTE* huffWeight, size_t hwSize, U32* rankStats, |
| 323 | U32* nbSymbolsPtr, U32* tableLogPtr, |
| 324 | const void* src, size_t srcSize, |
| 325 | void* workSpace, size_t wkspSize) |
| 326 | { |
| 327 | return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 0); |
| 328 | } |
| 329 | |
| 330 | #if DYNAMIC_BMI2 |
| 331 | static BMI2_TARGET_ATTRIBUTE size_t HUF_readStats_body_bmi2(BYTE* huffWeight, size_t hwSize, U32* rankStats, |
| 332 | U32* nbSymbolsPtr, U32* tableLogPtr, |
| 333 | const void* src, size_t srcSize, |
| 334 | void* workSpace, size_t wkspSize) |
| 335 | { |
| 336 | return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 1); |
| 337 | } |
| 338 | #endif |
| 339 | |
| 340 | size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize, U32* rankStats, |
| 341 | U32* nbSymbolsPtr, U32* tableLogPtr, |
| 342 | const void* src, size_t srcSize, |
| 343 | void* workSpace, size_t wkspSize, |
| 344 | int bmi2) |
| 345 | { |
| 346 | #if DYNAMIC_BMI2 |
| 347 | if (bmi2) { |
| 348 | return HUF_readStats_body_bmi2(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize); |
| 349 | } |
| 350 | #endif |
| 351 | (void)bmi2; |
| 352 | return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize); |
| 353 | } |