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 | 8def0e5 | 2020-08-24 12:24:45 -0700 | [diff] [blame^] | 80 | /* This function only works when hbSize >= 8 */ |
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 | 8def0e5 | 2020-08-24 12:24:45 -0700 | [diff] [blame^] | 89 | assert(hbSize >= 8); |
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 | 8def0e5 | 2020-08-24 12:24:45 -0700 | [diff] [blame^] | 105 | /* Count the number of repeats. Each time the |
| 106 | * 2-bit repeat code is 0b11 there is another |
| 107 | * repeat. |
| 108 | * Avoid UB by setting the high bit to 1. |
| 109 | */ |
Nick Terrell | 8f8bd2d | 2020-08-18 16:57:35 -0700 | [diff] [blame] | 110 | int repeats = FSE_ctz(~bitStream | 0x80000000) >> 1; |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 111 | while (repeats >= 12) { |
| 112 | charnum += 3 * 12; |
Nick Terrell | 8f8bd2d | 2020-08-18 16:57:35 -0700 | [diff] [blame] | 113 | if (ip <= iend-7) { |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 114 | ip += 3; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 115 | bitStream = MEM_readLE32(ip) >> bitCount; |
| 116 | } else { |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 117 | bitStream >>= 24; |
| 118 | bitCount += 24; |
| 119 | } |
Nick Terrell | 8f8bd2d | 2020-08-18 16:57:35 -0700 | [diff] [blame] | 120 | repeats = FSE_ctz(~bitStream | 0x80000000) >> 1; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 121 | } |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 122 | charnum += 3 * repeats; |
| 123 | bitStream >>= 2 * repeats; |
| 124 | bitCount += 2 * repeats; |
| 125 | |
Nick Terrell | 8def0e5 | 2020-08-24 12:24:45 -0700 | [diff] [blame^] | 126 | /* Add the final repeat which isn't 0b11. */ |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 127 | charnum += bitStream & 3; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 128 | bitCount += 2; |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 129 | |
| 130 | /* This is an error, but break and return an error |
| 131 | * at the end, because returning out of a loop makes |
| 132 | * it harder for the compiler to optimize. |
| 133 | */ |
| 134 | if (charnum >= maxSV1) break; |
| 135 | |
| 136 | /* We don't need to set the normalized count to 0 |
| 137 | * because we already memset the whole buffer to 0. |
| 138 | */ |
| 139 | |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 140 | if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) { |
Nick Terrell | a97e9a6 | 2018-05-23 12:16:00 -0700 | [diff] [blame] | 141 | assert((bitCount >> 3) <= 3); /* For first condition to work */ |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 142 | ip += bitCount>>3; |
| 143 | bitCount &= 7; |
| 144 | bitStream = MEM_readLE32(ip) >> bitCount; |
Yann Collet | 38b75dd | 2016-07-24 15:35:59 +0200 | [diff] [blame] | 145 | } else { |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 146 | bitStream >>= 2; |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | { |
| 150 | int const max = (2*threshold-1) - remaining; |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 151 | int count; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 152 | |
| 153 | if ((bitStream & (threshold-1)) < (U32)max) { |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 154 | count = bitStream & (threshold-1); |
| 155 | bitCount += nbBits-1; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 156 | } else { |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 157 | count = bitStream & (2*threshold-1); |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 158 | if (count >= threshold) count -= max; |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 159 | bitCount += nbBits; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | count--; /* extra accuracy */ |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 163 | /* When it matters (small blocks), this is a |
| 164 | * predictable branch, because we don't use -1. |
| 165 | */ |
| 166 | if (count >= 0) { |
| 167 | remaining -= count; |
| 168 | } else { |
| 169 | assert(count == -1); |
| 170 | remaining += count; |
| 171 | } |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 172 | normalizedCounter[charnum++] = (short)count; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 173 | previous0 = !count; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 174 | |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 175 | assert(threshold > 1); |
| 176 | if (remaining < threshold) { |
| 177 | /* This branch can be folded into the |
| 178 | * threshold update condition because we |
| 179 | * know that threshold > 1. |
| 180 | */ |
| 181 | if (remaining <= 1) break; |
| 182 | nbBits = BIT_highbit32(remaining) + 1; |
| 183 | threshold = 1 << (nbBits - 1); |
| 184 | } |
| 185 | if (charnum >= maxSV1) break; |
| 186 | |
| 187 | if (LIKELY((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))) { |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 188 | ip += bitCount>>3; |
| 189 | bitCount &= 7; |
| 190 | } else { |
| 191 | bitCount -= (int)(8 * (iend - 4 - ip)); |
| 192 | ip = iend - 4; |
| 193 | } |
| 194 | bitStream = MEM_readLE32(ip) >> (bitCount & 31); |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 195 | } } |
Yann Collet | 38b75dd | 2016-07-24 15:35:59 +0200 | [diff] [blame] | 196 | if (remaining != 1) return ERROR(corruption_detected); |
Nick Terrell | 6004c11 | 2020-08-14 15:28:59 -0700 | [diff] [blame] | 197 | /* Only possible when there are too many zeros. */ |
| 198 | if (charnum > maxSV1) return ERROR(maxSymbolValue_tooSmall); |
Yann Collet | cbc5e9d | 2016-07-24 18:02:04 +0200 | [diff] [blame] | 199 | if (bitCount > 32) return ERROR(corruption_detected); |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 200 | *maxSVPtr = charnum-1; |
| 201 | |
| 202 | ip += (bitCount+7)>>3; |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 203 | return ip-istart; |
| 204 | } |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 205 | |
Nick Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 206 | static size_t FSE_readNCount_body_default( |
| 207 | short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
| 208 | const void* headerBuffer, size_t hbSize) |
| 209 | { |
| 210 | return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); |
| 211 | } |
| 212 | |
| 213 | #if DYNAMIC_BMI2 |
| 214 | TARGET_ATTRIBUTE("bmi2") static size_t FSE_readNCount_body_bmi2( |
| 215 | short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
| 216 | const void* headerBuffer, size_t hbSize) |
| 217 | { |
| 218 | return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); |
| 219 | } |
| 220 | #endif |
| 221 | |
| 222 | size_t FSE_readNCount_bmi2( |
| 223 | short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
| 224 | const void* headerBuffer, size_t hbSize, int bmi2) |
| 225 | { |
| 226 | #if DYNAMIC_BMI2 |
| 227 | if (bmi2) { |
| 228 | return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); |
| 229 | } |
| 230 | #endif |
| 231 | (void)bmi2; |
| 232 | return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); |
| 233 | } |
| 234 | |
| 235 | size_t FSE_readNCount( |
| 236 | short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
| 237 | const void* headerBuffer, size_t hbSize) |
| 238 | { |
| 239 | return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, /* bmi2 */ 0); |
| 240 | } |
| 241 | |
| 242 | |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 243 | /*! HUF_readStats() : |
| 244 | Read compact Huffman tree, saved by HUF_writeCTable(). |
| 245 | `huffWeight` is destination buffer. |
Yann Collet | b89af20 | 2016-12-01 18:24:59 -0800 | [diff] [blame] | 246 | `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] | 247 | @return : size read from `src` , or an error Code . |
Yann Collet | 38b75dd | 2016-07-24 15:35:59 +0200 | [diff] [blame] | 248 | Note : Needed by HUF_readCTable() and HUF_readDTableX?() . |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 249 | */ |
| 250 | size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats, |
| 251 | U32* nbSymbolsPtr, U32* tableLogPtr, |
| 252 | const void* src, size_t srcSize) |
| 253 | { |
Nick Terrell | ba1fd17 | 2020-08-16 22:22:33 -0700 | [diff] [blame] | 254 | U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32]; |
Nick Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 255 | 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] | 256 | } |
| 257 | |
Nick Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 258 | 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] | 259 | U32* nbSymbolsPtr, U32* tableLogPtr, |
| 260 | const void* src, size_t srcSize, |
Nick Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 261 | void* workSpace, size_t wkspSize, |
| 262 | int bmi2) |
Nick Terrell | ba1fd17 | 2020-08-16 22:22:33 -0700 | [diff] [blame] | 263 | { |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 264 | U32 weightTotal; |
| 265 | const BYTE* ip = (const BYTE*) src; |
Nick Terrell | ccfcc64 | 2016-10-17 11:28:02 -0700 | [diff] [blame] | 266 | size_t iSize; |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 267 | size_t oSize; |
| 268 | |
Nick Terrell | ccfcc64 | 2016-10-17 11:28:02 -0700 | [diff] [blame] | 269 | if (!srcSize) return ERROR(srcSize_wrong); |
| 270 | iSize = ip[0]; |
Yann Collet | 7ed5e33 | 2016-07-24 14:26:11 +0200 | [diff] [blame] | 271 | /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */ |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 272 | |
Yann Collet | 7ed5e33 | 2016-07-24 14:26:11 +0200 | [diff] [blame] | 273 | if (iSize >= 128) { /* special header */ |
Yann Collet | 38b75dd | 2016-07-24 15:35:59 +0200 | [diff] [blame] | 274 | oSize = iSize - 127; |
| 275 | iSize = ((oSize+1)/2); |
| 276 | if (iSize+1 > srcSize) return ERROR(srcSize_wrong); |
| 277 | if (oSize >= hwSize) return ERROR(corruption_detected); |
| 278 | ip += 1; |
| 279 | { U32 n; |
| 280 | for (n=0; n<oSize; n+=2) { |
| 281 | huffWeight[n] = ip[n/2] >> 4; |
| 282 | huffWeight[n+1] = ip[n/2] & 15; |
| 283 | } } } |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 284 | else { /* header compressed with FSE (normal case) */ |
| 285 | if (iSize+1 > srcSize) return ERROR(srcSize_wrong); |
Nick Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 286 | 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] | 287 | if (FSE_isError(oSize)) return oSize; |
| 288 | } |
| 289 | |
| 290 | /* collect weight stats */ |
Yann Collet | b89af20 | 2016-12-01 18:24:59 -0800 | [diff] [blame] | 291 | memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32)); |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 292 | weightTotal = 0; |
| 293 | { U32 n; for (n=0; n<oSize; n++) { |
Yann Collet | b89af20 | 2016-12-01 18:24:59 -0800 | [diff] [blame] | 294 | if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected); |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 295 | rankStats[huffWeight[n]]++; |
| 296 | weightTotal += (1 << huffWeight[n]) >> 1; |
| 297 | } } |
Nick Terrell | d760529 | 2016-10-19 11:19:54 -0700 | [diff] [blame] | 298 | if (weightTotal == 0) return ERROR(corruption_detected); |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 299 | |
| 300 | /* get last non-null symbol weight (implied, total must be 2^n) */ |
| 301 | { U32 const tableLog = BIT_highbit32(weightTotal) + 1; |
Yann Collet | b89af20 | 2016-12-01 18:24:59 -0800 | [diff] [blame] | 302 | if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected); |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 303 | *tableLogPtr = tableLog; |
| 304 | /* determine last weight */ |
| 305 | { U32 const total = 1 << tableLog; |
| 306 | U32 const rest = total - weightTotal; |
| 307 | U32 const verif = 1 << BIT_highbit32(rest); |
| 308 | U32 const lastWeight = BIT_highbit32(rest) + 1; |
| 309 | if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */ |
| 310 | huffWeight[oSize] = (BYTE)lastWeight; |
| 311 | rankStats[lastWeight]++; |
| 312 | } } |
| 313 | |
| 314 | /* check tree construction validity */ |
| 315 | if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */ |
| 316 | |
| 317 | /* results */ |
| 318 | *nbSymbolsPtr = (U32)(oSize+1); |
| 319 | return iSize+1; |
| 320 | } |
Nick Terrell | 612e947 | 2020-08-17 13:44:49 -0700 | [diff] [blame] | 321 | |
| 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 TARGET_ATTRIBUTE("bmi2") 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 | } |