Nick Terrell | ac58c8d | 2020-03-26 15:19:05 -0700 | [diff] [blame] | 1 | /* ****************************************************************** |
| 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 | ****************************************************************** */ |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 14 | |
| 15 | /* ************************************* |
| 16 | * Dependencies |
| 17 | ***************************************/ |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 18 | #include "mem.h" |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 19 | #include "error_private.h" /* ERR_*, ERROR */ |
Yann Collet | d0e2cd1 | 2016-06-05 00:58:01 +0200 | [diff] [blame] | 20 | #define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */ |
Yann Collet | 38b75dd | 2016-07-24 15:35:59 +0200 | [diff] [blame] | 21 | #include "fse.h" |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 22 | #define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */ |
Yann Collet | 38b75dd | 2016-07-24 15:35:59 +0200 | [diff] [blame] | 23 | #include "huf.h" |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 24 | |
| 25 | |
Yann Collet | 1f2c95c | 2017-03-05 21:07:20 -0800 | [diff] [blame] | 26 | /*=== Version ===*/ |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 27 | unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; } |
| 28 | |
| 29 | |
Yann Collet | 1f2c95c | 2017-03-05 21:07:20 -0800 | [diff] [blame] | 30 | /*=== Error Management ===*/ |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 31 | unsigned FSE_isError(size_t code) { return ERR_isError(code); } |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 32 | const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); } |
| 33 | |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 34 | unsigned HUF_isError(size_t code) { return ERR_isError(code); } |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 35 | const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); } |
| 36 | |
| 37 | |
| 38 | /*-************************************************************** |
| 39 | * FSE NCount encoding-decoding |
| 40 | ****************************************************************/ |
Nick Terrell | 8f8bd2d | 2020-08-18 16:57:35 -0700 | [diff] [blame^] | 41 | static 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 Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 63 | FORCE_INLINE_TEMPLATE |
| 64 | size_t FSE_readNCount_body(short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
| 65 | const void* headerBuffer, size_t hbSize) |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 66 | { |
| 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 Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 76 | unsigned const maxSV1 = *maxSVPtr + 1; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 77 | int previous0 = 0; |
| 78 | |
Nick Terrell | 8f8bd2d | 2020-08-18 16:57:35 -0700 | [diff] [blame^] | 79 | if (hbSize < 8) { |
Nick Terrell | f2d0924 | 2018-05-23 14:58:58 -0700 | [diff] [blame] | 80 | /* This function only works when hbSize >= 4 */ |
Nick Terrell | 8f8bd2d | 2020-08-18 16:57:35 -0700 | [diff] [blame^] | 81 | char buffer[8] = {0}; |
Nick Terrell | f2d0924 | 2018-05-23 14:58:58 -0700 | [diff] [blame] | 82 | 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 Terrell | c92dd11 | 2018-05-23 14:47:20 -0700 | [diff] [blame] | 89 | assert(hbSize >= 4); |
Nick Terrell | a97e9a6 | 2018-05-23 12:16:00 -0700 | [diff] [blame] | 90 | |
Yann Collet | ff773bf | 2018-06-26 17:24:41 -0700 | [diff] [blame] | 91 | /* init */ |
| 92 | memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */ |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 93 | 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 Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 103 | for (;;) { |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 104 | if (previous0) { |
Nick Terrell | 8f8bd2d | 2020-08-18 16:57:35 -0700 | [diff] [blame^] | 105 | int repeats = FSE_ctz(~bitStream | 0x80000000) >> 1; |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 106 | while (repeats >= 12) { |
| 107 | charnum += 3 * 12; |
Nick Terrell | 8f8bd2d | 2020-08-18 16:57:35 -0700 | [diff] [blame^] | 108 | if (ip <= iend-7) { |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 109 | ip += 3; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 110 | bitStream = MEM_readLE32(ip) >> bitCount; |
| 111 | } else { |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 112 | bitStream >>= 24; |
| 113 | bitCount += 24; |
| 114 | } |
Nick Terrell | 8f8bd2d | 2020-08-18 16:57:35 -0700 | [diff] [blame^] | 115 | repeats = FSE_ctz(~bitStream | 0x80000000) >> 1; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 116 | } |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 117 | charnum += 3 * repeats; |
| 118 | bitStream >>= 2 * repeats; |
| 119 | bitCount += 2 * repeats; |
| 120 | |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 121 | charnum += bitStream & 3; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 122 | bitCount += 2; |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 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 | |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 134 | if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) { |
Nick Terrell | a97e9a6 | 2018-05-23 12:16:00 -0700 | [diff] [blame] | 135 | assert((bitCount >> 3) <= 3); /* For first condition to work */ |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 136 | ip += bitCount>>3; |
| 137 | bitCount &= 7; |
| 138 | bitStream = MEM_readLE32(ip) >> bitCount; |
Yann Collet | 38b75dd | 2016-07-24 15:35:59 +0200 | [diff] [blame] | 139 | } else { |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 140 | bitStream >>= 2; |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | { |
| 144 | int const max = (2*threshold-1) - remaining; |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 145 | int count; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 146 | |
| 147 | if ((bitStream & (threshold-1)) < (U32)max) { |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 148 | count = bitStream & (threshold-1); |
| 149 | bitCount += nbBits-1; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 150 | } else { |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 151 | count = bitStream & (2*threshold-1); |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 152 | if (count >= threshold) count -= max; |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 153 | bitCount += nbBits; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | count--; /* extra accuracy */ |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 157 | /* 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 Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 166 | normalizedCounter[charnum++] = (short)count; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 167 | previous0 = !count; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 168 | |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 169 | 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))) { |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 182 | 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 Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 189 | } } |
Yann Collet | 38b75dd | 2016-07-24 15:35:59 +0200 | [diff] [blame] | 190 | if (remaining != 1) return ERROR(corruption_detected); |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 191 | /* Only possible when there are too many zeros. */ |
| 192 | if (charnum > maxSV1) return ERROR(maxSymbolValue_tooSmall); |
Yann Collet | cbc5e9d | 2016-07-24 18:02:04 +0200 | [diff] [blame] | 193 | if (bitCount > 32) return ERROR(corruption_detected); |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 194 | *maxSVPtr = charnum-1; |
| 195 | |
| 196 | ip += (bitCount+7)>>3; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 197 | return ip-istart; |
| 198 | } |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 199 | |
Nick Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 200 | static 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 |
| 208 | TARGET_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 | |
| 216 | size_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 | |
| 229 | size_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 Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 237 | /*! HUF_readStats() : |
| 238 | Read compact Huffman tree, saved by HUF_writeCTable(). |
| 239 | `huffWeight` is destination buffer. |
Yann Collet | b89af20 | 2016-12-01 18:24:59 -0800 | [diff] [blame] | 240 | `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32. |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 241 | @return : size read from `src` , or an error Code . |
Yann Collet | 38b75dd | 2016-07-24 15:35:59 +0200 | [diff] [blame] | 242 | Note : Needed by HUF_readCTable() and HUF_readDTableX?() . |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 243 | */ |
| 244 | size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats, |
| 245 | U32* nbSymbolsPtr, U32* tableLogPtr, |
| 246 | const void* src, size_t srcSize) |
| 247 | { |
Nick Terrell | ba1fd17 | 2020-08-16 22:22:33 -0700 | [diff] [blame] | 248 | U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32]; |
Nick Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 249 | return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp, sizeof(wksp), /* bmi2 */ 0); |
Nick Terrell | ba1fd17 | 2020-08-16 22:22:33 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Nick Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 252 | FORCE_INLINE_TEMPLATE size_t HUF_readStats_body(BYTE* huffWeight, size_t hwSize, U32* rankStats, |
Nick Terrell | ba1fd17 | 2020-08-16 22:22:33 -0700 | [diff] [blame] | 253 | U32* nbSymbolsPtr, U32* tableLogPtr, |
| 254 | const void* src, size_t srcSize, |
Nick Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 255 | void* workSpace, size_t wkspSize, |
| 256 | int bmi2) |
Nick Terrell | ba1fd17 | 2020-08-16 22:22:33 -0700 | [diff] [blame] | 257 | { |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 258 | U32 weightTotal; |
| 259 | const BYTE* ip = (const BYTE*) src; |
Nick Terrell | ccfcc64 | 2016-10-17 11:28:02 -0700 | [diff] [blame] | 260 | size_t iSize; |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 261 | size_t oSize; |
| 262 | |
Nick Terrell | ccfcc64 | 2016-10-17 11:28:02 -0700 | [diff] [blame] | 263 | if (!srcSize) return ERROR(srcSize_wrong); |
| 264 | iSize = ip[0]; |
Yann Collet | 7ed5e33 | 2016-07-24 14:26:11 +0200 | [diff] [blame] | 265 | /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */ |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 266 | |
Yann Collet | 7ed5e33 | 2016-07-24 14:26:11 +0200 | [diff] [blame] | 267 | if (iSize >= 128) { /* special header */ |
Yann Collet | 38b75dd | 2016-07-24 15:35:59 +0200 | [diff] [blame] | 268 | 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 Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 278 | else { /* header compressed with FSE (normal case) */ |
| 279 | if (iSize+1 > srcSize) return ERROR(srcSize_wrong); |
Nick Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 280 | 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 Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 281 | if (FSE_isError(oSize)) return oSize; |
| 282 | } |
| 283 | |
| 284 | /* collect weight stats */ |
Yann Collet | b89af20 | 2016-12-01 18:24:59 -0800 | [diff] [blame] | 285 | memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32)); |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 286 | weightTotal = 0; |
| 287 | { U32 n; for (n=0; n<oSize; n++) { |
Yann Collet | b89af20 | 2016-12-01 18:24:59 -0800 | [diff] [blame] | 288 | if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected); |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 289 | rankStats[huffWeight[n]]++; |
| 290 | weightTotal += (1 << huffWeight[n]) >> 1; |
| 291 | } } |
Nick Terrell | d760529 | 2016-10-19 11:19:54 -0700 | [diff] [blame] | 292 | if (weightTotal == 0) return ERROR(corruption_detected); |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 293 | |
| 294 | /* get last non-null symbol weight (implied, total must be 2^n) */ |
| 295 | { U32 const tableLog = BIT_highbit32(weightTotal) + 1; |
Yann Collet | b89af20 | 2016-12-01 18:24:59 -0800 | [diff] [blame] | 296 | if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected); |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 297 | *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 Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 315 | |
| 316 | static 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 |
| 325 | static 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 | |
| 334 | size_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 | } |