1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only 2 /* 3 * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. 4 * All rights reserved. 5 * 6 * This source code is licensed under both the BSD-style license (found in the 7 * LICENSE file in the root directory of this source tree) and the GPLv2 (found 8 * in the COPYING file in the root directory of this source tree). 9 * You may select, at your option, one of the above-listed licenses. 10 */ 11 12 /*-************************************* 13 * Dependencies 14 ***************************************/ 15 #include "zstd_compress_sequences.h" 16 17 /** 18 * -log2(x / 256) lookup table for x in [0, 256). 19 * If x == 0: Return 0 20 * Else: Return floor(-log2(x / 256) * 256) 21 */ 22 static unsigned const kInverseProbabilityLog256[256] = { 23 0, 2048, 1792, 1642, 1536, 1453, 1386, 1329, 1280, 1236, 1197, 1162, 24 1130, 1100, 1073, 1047, 1024, 1001, 980, 960, 941, 923, 906, 889, 25 874, 859, 844, 830, 817, 804, 791, 779, 768, 756, 745, 734, 26 724, 714, 704, 694, 685, 676, 667, 658, 650, 642, 633, 626, 27 618, 610, 603, 595, 588, 581, 574, 567, 561, 554, 548, 542, 28 535, 529, 523, 517, 512, 506, 500, 495, 489, 484, 478, 473, 29 468, 463, 458, 453, 448, 443, 438, 434, 429, 424, 420, 415, 30 411, 407, 402, 398, 394, 390, 386, 382, 377, 373, 370, 366, 31 362, 358, 354, 350, 347, 343, 339, 336, 332, 329, 325, 322, 32 318, 315, 311, 308, 305, 302, 298, 295, 292, 289, 286, 282, 33 279, 276, 273, 270, 267, 264, 261, 258, 256, 253, 250, 247, 34 244, 241, 239, 236, 233, 230, 228, 225, 222, 220, 217, 215, 35 212, 209, 207, 204, 202, 199, 197, 194, 192, 190, 187, 185, 36 182, 180, 178, 175, 173, 171, 168, 166, 164, 162, 159, 157, 37 155, 153, 151, 149, 146, 144, 142, 140, 138, 136, 134, 132, 38 130, 128, 126, 123, 121, 119, 117, 115, 114, 112, 110, 108, 39 106, 104, 102, 100, 98, 96, 94, 93, 91, 89, 87, 85, 40 83, 82, 80, 78, 76, 74, 73, 71, 69, 67, 66, 64, 41 62, 61, 59, 57, 55, 54, 52, 50, 49, 47, 46, 44, 42 42, 41, 39, 37, 36, 34, 33, 31, 30, 28, 26, 25, 43 23, 22, 20, 19, 17, 16, 14, 13, 11, 10, 8, 7, 44 5, 4, 2, 1, 45 }; 46 47 static unsigned ZSTD_getFSEMaxSymbolValue(FSE_CTable const* ctable) { 48 void const* ptr = ctable; 49 U16 const* u16ptr = (U16 const*)ptr; 50 U32 const maxSymbolValue = MEM_read16(u16ptr + 1); 51 return maxSymbolValue; 52 } 53 54 /** 55 * Returns the cost in bytes of encoding the normalized count header. 56 * Returns an error if any of the helper functions return an error. 57 */ 58 static size_t ZSTD_NCountCost(unsigned const* count, unsigned const max, 59 size_t const nbSeq, unsigned const FSELog) 60 { 61 BYTE wksp[FSE_NCOUNTBOUND]; 62 S16 norm[MaxSeq + 1]; 63 const U32 tableLog = FSE_optimalTableLog(FSELog, nbSeq, max); 64 FORWARD_IF_ERROR(FSE_normalizeCount(norm, tableLog, count, nbSeq, max), ""); 65 return FSE_writeNCount(wksp, sizeof(wksp), norm, max, tableLog); 66 } 67 68 /** 69 * Returns the cost in bits of encoding the distribution described by count 70 * using the entropy bound. 71 */ 72 static size_t ZSTD_entropyCost(unsigned const* count, unsigned const max, size_t const total) 73 { 74 unsigned cost = 0; 75 unsigned s; 76 for (s = 0; s <= max; ++s) { 77 unsigned norm = (unsigned)((256 * count[s]) / total); 78 if (count[s] != 0 && norm == 0) 79 norm = 1; 80 assert(count[s] < total); 81 cost += count[s] * kInverseProbabilityLog256[norm]; 82 } 83 return cost >> 8; 84 } 85 86 /** 87 * Returns the cost in bits of encoding the distribution in count using ctable. 88 * Returns an error if ctable cannot represent all the symbols in count. 89 */ 90 size_t ZSTD_fseBitCost( 91 FSE_CTable const* ctable, 92 unsigned const* count, 93 unsigned const max) 94 { 95 unsigned const kAccuracyLog = 8; 96 size_t cost = 0; 97 unsigned s; 98 FSE_CState_t cstate; 99 FSE_initCState(&cstate, ctable); 100 if (ZSTD_getFSEMaxSymbolValue(ctable) < max) { 101 DEBUGLOG(5, "Repeat FSE_CTable has maxSymbolValue %u < %u", 102 ZSTD_getFSEMaxSymbolValue(ctable), max); 103 return ERROR(GENERIC); 104 } 105 for (s = 0; s <= max; ++s) { 106 unsigned const tableLog = cstate.stateLog; 107 unsigned const badCost = (tableLog + 1) << kAccuracyLog; 108 unsigned const bitCost = FSE_bitCost(cstate.symbolTT, tableLog, s, kAccuracyLog); 109 if (count[s] == 0) 110 continue; 111 if (bitCost >= badCost) { 112 DEBUGLOG(5, "Repeat FSE_CTable has Prob[%u] == 0", s); 113 return ERROR(GENERIC); 114 } 115 cost += (size_t)count[s] * bitCost; 116 } 117 return cost >> kAccuracyLog; 118 } 119 120 /** 121 * Returns the cost in bits of encoding the distribution in count using the 122 * table described by norm. The max symbol support by norm is assumed >= max. 123 * norm must be valid for every symbol with non-zero probability in count. 124 */ 125 size_t ZSTD_crossEntropyCost(short const* norm, unsigned accuracyLog, 126 unsigned const* count, unsigned const max) 127 { 128 unsigned const shift = 8 - accuracyLog; 129 size_t cost = 0; 130 unsigned s; 131 assert(accuracyLog <= 8); 132 for (s = 0; s <= max; ++s) { 133 unsigned const normAcc = (norm[s] != -1) ? (unsigned)norm[s] : 1; 134 unsigned const norm256 = normAcc << shift; 135 assert(norm256 > 0); 136 assert(norm256 < 256); 137 cost += count[s] * kInverseProbabilityLog256[norm256]; 138 } 139 return cost >> 8; 140 } 141 142 symbolEncodingType_e 143 ZSTD_selectEncodingType( 144 FSE_repeat* repeatMode, unsigned const* count, unsigned const max, 145 size_t const mostFrequent, size_t nbSeq, unsigned const FSELog, 146 FSE_CTable const* prevCTable, 147 short const* defaultNorm, U32 defaultNormLog, 148 ZSTD_defaultPolicy_e const isDefaultAllowed, 149 ZSTD_strategy const strategy) 150 { 151 ZSTD_STATIC_ASSERT(ZSTD_defaultDisallowed == 0 && ZSTD_defaultAllowed != 0); 152 if (mostFrequent == nbSeq) { 153 *repeatMode = FSE_repeat_none; 154 if (isDefaultAllowed && nbSeq <= 2) { 155 /* Prefer set_basic over set_rle when there are 2 or less symbols, 156 * since RLE uses 1 byte, but set_basic uses 5-6 bits per symbol. 157 * If basic encoding isn't possible, always choose RLE. 158 */ 159 DEBUGLOG(5, "Selected set_basic"); 160 return set_basic; 161 } 162 DEBUGLOG(5, "Selected set_rle"); 163 return set_rle; 164 } 165 if (strategy < ZSTD_lazy) { 166 if (isDefaultAllowed) { 167 size_t const staticFse_nbSeq_max = 1000; 168 size_t const mult = 10 - strategy; 169 size_t const baseLog = 3; 170 size_t const dynamicFse_nbSeq_min = (((size_t)1 << defaultNormLog) * mult) >> baseLog; /* 28-36 for offset, 56-72 for lengths */ 171 assert(defaultNormLog >= 5 && defaultNormLog <= 6); /* xx_DEFAULTNORMLOG */ 172 assert(mult <= 9 && mult >= 7); 173 if ( (*repeatMode == FSE_repeat_valid) 174 && (nbSeq < staticFse_nbSeq_max) ) { 175 DEBUGLOG(5, "Selected set_repeat"); 176 return set_repeat; 177 } 178 if ( (nbSeq < dynamicFse_nbSeq_min) 179 || (mostFrequent < (nbSeq >> (defaultNormLog-1))) ) { 180 DEBUGLOG(5, "Selected set_basic"); 181 /* The format allows default tables to be repeated, but it isn't useful. 182 * When using simple heuristics to select encoding type, we don't want 183 * to confuse these tables with dictionaries. When running more careful 184 * analysis, we don't need to waste time checking both repeating tables 185 * and default tables. 186 */ 187 *repeatMode = FSE_repeat_none; 188 return set_basic; 189 } 190 } 191 } else { 192 size_t const basicCost = isDefaultAllowed ? ZSTD_crossEntropyCost(defaultNorm, defaultNormLog, count, max) : ERROR(GENERIC); 193 size_t const repeatCost = *repeatMode != FSE_repeat_none ? ZSTD_fseBitCost(prevCTable, count, max) : ERROR(GENERIC); 194 size_t const NCountCost = ZSTD_NCountCost(count, max, nbSeq, FSELog); 195 size_t const compressedCost = (NCountCost << 3) + ZSTD_entropyCost(count, max, nbSeq); 196 197 if (isDefaultAllowed) { 198 assert(!ZSTD_isError(basicCost)); 199 assert(!(*repeatMode == FSE_repeat_valid && ZSTD_isError(repeatCost))); 200 } 201 assert(!ZSTD_isError(NCountCost)); 202 assert(compressedCost < ERROR(maxCode)); 203 DEBUGLOG(5, "Estimated bit costs: basic=%u\trepeat=%u\tcompressed=%u", 204 (unsigned)basicCost, (unsigned)repeatCost, (unsigned)compressedCost); 205 if (basicCost <= repeatCost && basicCost <= compressedCost) { 206 DEBUGLOG(5, "Selected set_basic"); 207 assert(isDefaultAllowed); 208 *repeatMode = FSE_repeat_none; 209 return set_basic; 210 } 211 if (repeatCost <= compressedCost) { 212 DEBUGLOG(5, "Selected set_repeat"); 213 assert(!ZSTD_isError(repeatCost)); 214 return set_repeat; 215 } 216 assert(compressedCost < basicCost && compressedCost < repeatCost); 217 } 218 DEBUGLOG(5, "Selected set_compressed"); 219 *repeatMode = FSE_repeat_check; 220 return set_compressed; 221 } 222 223 size_t 224 ZSTD_buildCTable(void* dst, size_t dstCapacity, 225 FSE_CTable* nextCTable, U32 FSELog, symbolEncodingType_e type, 226 unsigned* count, U32 max, 227 const BYTE* codeTable, size_t nbSeq, 228 const S16* defaultNorm, U32 defaultNormLog, U32 defaultMax, 229 const FSE_CTable* prevCTable, size_t prevCTableSize, 230 void* entropyWorkspace, size_t entropyWorkspaceSize) 231 { 232 BYTE* op = (BYTE*)dst; 233 const BYTE* const oend = op + dstCapacity; 234 DEBUGLOG(6, "ZSTD_buildCTable (dstCapacity=%u)", (unsigned)dstCapacity); 235 236 switch (type) { 237 case set_rle: 238 FORWARD_IF_ERROR(FSE_buildCTable_rle(nextCTable, (BYTE)max), ""); 239 RETURN_ERROR_IF(dstCapacity==0, dstSize_tooSmall, "not enough space"); 240 *op = codeTable[0]; 241 return 1; 242 case set_repeat: 243 memcpy(nextCTable, prevCTable, prevCTableSize); 244 return 0; 245 case set_basic: 246 FORWARD_IF_ERROR(FSE_buildCTable_wksp(nextCTable, defaultNorm, defaultMax, defaultNormLog, entropyWorkspace, entropyWorkspaceSize), ""); /* note : could be pre-calculated */ 247 return 0; 248 case set_compressed: { 249 S16 norm[MaxSeq + 1]; 250 size_t nbSeq_1 = nbSeq; 251 const U32 tableLog = FSE_optimalTableLog(FSELog, nbSeq, max); 252 if (count[codeTable[nbSeq-1]] > 1) { 253 count[codeTable[nbSeq-1]]--; 254 nbSeq_1--; 255 } 256 assert(nbSeq_1 > 1); 257 FORWARD_IF_ERROR(FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max), ""); 258 { size_t const NCountSize = FSE_writeNCount(op, oend - op, norm, max, tableLog); /* overflow protected */ 259 FORWARD_IF_ERROR(NCountSize, "FSE_writeNCount failed"); 260 FORWARD_IF_ERROR(FSE_buildCTable_wksp(nextCTable, norm, max, tableLog, entropyWorkspace, entropyWorkspaceSize), ""); 261 return NCountSize; 262 } 263 } 264 default: assert(0); RETURN_ERROR(GENERIC, "impossible to reach"); 265 } 266 } 267 268 FORCE_INLINE_TEMPLATE size_t 269 ZSTD_encodeSequences_body( 270 void* dst, size_t dstCapacity, 271 FSE_CTable const* CTable_MatchLength, BYTE const* mlCodeTable, 272 FSE_CTable const* CTable_OffsetBits, BYTE const* ofCodeTable, 273 FSE_CTable const* CTable_LitLength, BYTE const* llCodeTable, 274 seqDef const* sequences, size_t nbSeq, int longOffsets) 275 { 276 BIT_CStream_t blockStream; 277 FSE_CState_t stateMatchLength; 278 FSE_CState_t stateOffsetBits; 279 FSE_CState_t stateLitLength; 280 281 RETURN_ERROR_IF( 282 ERR_isError(BIT_initCStream(&blockStream, dst, dstCapacity)), 283 dstSize_tooSmall, "not enough space remaining"); 284 DEBUGLOG(6, "available space for bitstream : %i (dstCapacity=%u)", 285 (int)(blockStream.endPtr - blockStream.startPtr), 286 (unsigned)dstCapacity); 287 288 /* first symbols */ 289 FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq-1]); 290 FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq-1]); 291 FSE_initCState2(&stateLitLength, CTable_LitLength, llCodeTable[nbSeq-1]); 292 BIT_addBits(&blockStream, sequences[nbSeq-1].litLength, LL_bits[llCodeTable[nbSeq-1]]); 293 if (MEM_32bits()) BIT_flushBits(&blockStream); 294 BIT_addBits(&blockStream, sequences[nbSeq-1].matchLength, ML_bits[mlCodeTable[nbSeq-1]]); 295 if (MEM_32bits()) BIT_flushBits(&blockStream); 296 if (longOffsets) { 297 U32 const ofBits = ofCodeTable[nbSeq-1]; 298 unsigned const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1); 299 if (extraBits) { 300 BIT_addBits(&blockStream, sequences[nbSeq-1].offset, extraBits); 301 BIT_flushBits(&blockStream); 302 } 303 BIT_addBits(&blockStream, sequences[nbSeq-1].offset >> extraBits, 304 ofBits - extraBits); 305 } else { 306 BIT_addBits(&blockStream, sequences[nbSeq-1].offset, ofCodeTable[nbSeq-1]); 307 } 308 BIT_flushBits(&blockStream); 309 310 { size_t n; 311 for (n=nbSeq-2 ; n<nbSeq ; n--) { /* intentional underflow */ 312 BYTE const llCode = llCodeTable[n]; 313 BYTE const ofCode = ofCodeTable[n]; 314 BYTE const mlCode = mlCodeTable[n]; 315 U32 const llBits = LL_bits[llCode]; 316 U32 const ofBits = ofCode; 317 U32 const mlBits = ML_bits[mlCode]; 318 DEBUGLOG(6, "encoding: litlen:%2u - matchlen:%2u - offCode:%7u", 319 (unsigned)sequences[n].litLength, 320 (unsigned)sequences[n].matchLength + MINMATCH, 321 (unsigned)sequences[n].offset); 322 /* 32b*/ /* 64b*/ 323 /* (7)*/ /* (7)*/ 324 FSE_encodeSymbol(&blockStream, &stateOffsetBits, ofCode); /* 15 */ /* 15 */ 325 FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode); /* 24 */ /* 24 */ 326 if (MEM_32bits()) BIT_flushBits(&blockStream); /* (7)*/ 327 FSE_encodeSymbol(&blockStream, &stateLitLength, llCode); /* 16 */ /* 33 */ 328 if (MEM_32bits() || (ofBits+mlBits+llBits >= 64-7-(LLFSELog+MLFSELog+OffFSELog))) 329 BIT_flushBits(&blockStream); /* (7)*/ 330 BIT_addBits(&blockStream, sequences[n].litLength, llBits); 331 if (MEM_32bits() && ((llBits+mlBits)>24)) BIT_flushBits(&blockStream); 332 BIT_addBits(&blockStream, sequences[n].matchLength, mlBits); 333 if (MEM_32bits() || (ofBits+mlBits+llBits > 56)) BIT_flushBits(&blockStream); 334 if (longOffsets) { 335 unsigned const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1); 336 if (extraBits) { 337 BIT_addBits(&blockStream, sequences[n].offset, extraBits); 338 BIT_flushBits(&blockStream); /* (7)*/ 339 } 340 BIT_addBits(&blockStream, sequences[n].offset >> extraBits, 341 ofBits - extraBits); /* 31 */ 342 } else { 343 BIT_addBits(&blockStream, sequences[n].offset, ofBits); /* 31 */ 344 } 345 BIT_flushBits(&blockStream); /* (7)*/ 346 DEBUGLOG(7, "remaining space : %i", (int)(blockStream.endPtr - blockStream.ptr)); 347 } } 348 349 DEBUGLOG(6, "ZSTD_encodeSequences: flushing ML state with %u bits", stateMatchLength.stateLog); 350 FSE_flushCState(&blockStream, &stateMatchLength); 351 DEBUGLOG(6, "ZSTD_encodeSequences: flushing Off state with %u bits", stateOffsetBits.stateLog); 352 FSE_flushCState(&blockStream, &stateOffsetBits); 353 DEBUGLOG(6, "ZSTD_encodeSequences: flushing LL state with %u bits", stateLitLength.stateLog); 354 FSE_flushCState(&blockStream, &stateLitLength); 355 356 { size_t const streamSize = BIT_closeCStream(&blockStream); 357 RETURN_ERROR_IF(streamSize==0, dstSize_tooSmall, "not enough space"); 358 return streamSize; 359 } 360 } 361 362 static size_t 363 ZSTD_encodeSequences_default( 364 void* dst, size_t dstCapacity, 365 FSE_CTable const* CTable_MatchLength, BYTE const* mlCodeTable, 366 FSE_CTable const* CTable_OffsetBits, BYTE const* ofCodeTable, 367 FSE_CTable const* CTable_LitLength, BYTE const* llCodeTable, 368 seqDef const* sequences, size_t nbSeq, int longOffsets) 369 { 370 return ZSTD_encodeSequences_body(dst, dstCapacity, 371 CTable_MatchLength, mlCodeTable, 372 CTable_OffsetBits, ofCodeTable, 373 CTable_LitLength, llCodeTable, 374 sequences, nbSeq, longOffsets); 375 } 376 377 378 #if DYNAMIC_BMI2 379 380 static TARGET_ATTRIBUTE("bmi2") size_t 381 ZSTD_encodeSequences_bmi2( 382 void* dst, size_t dstCapacity, 383 FSE_CTable const* CTable_MatchLength, BYTE const* mlCodeTable, 384 FSE_CTable const* CTable_OffsetBits, BYTE const* ofCodeTable, 385 FSE_CTable const* CTable_LitLength, BYTE const* llCodeTable, 386 seqDef const* sequences, size_t nbSeq, int longOffsets) 387 { 388 return ZSTD_encodeSequences_body(dst, dstCapacity, 389 CTable_MatchLength, mlCodeTable, 390 CTable_OffsetBits, ofCodeTable, 391 CTable_LitLength, llCodeTable, 392 sequences, nbSeq, longOffsets); 393 } 394 395 #endif 396 397 size_t ZSTD_encodeSequences( 398 void* dst, size_t dstCapacity, 399 FSE_CTable const* CTable_MatchLength, BYTE const* mlCodeTable, 400 FSE_CTable const* CTable_OffsetBits, BYTE const* ofCodeTable, 401 FSE_CTable const* CTable_LitLength, BYTE const* llCodeTable, 402 seqDef const* sequences, size_t nbSeq, int longOffsets, int bmi2) 403 { 404 DEBUGLOG(5, "ZSTD_encodeSequences: dstCapacity = %u", (unsigned)dstCapacity); 405 #if DYNAMIC_BMI2 406 if (bmi2) { 407 return ZSTD_encodeSequences_bmi2(dst, dstCapacity, 408 CTable_MatchLength, mlCodeTable, 409 CTable_OffsetBits, ofCodeTable, 410 CTable_LitLength, llCodeTable, 411 sequences, nbSeq, longOffsets); 412 } 413 #endif 414 (void)bmi2; 415 return ZSTD_encodeSequences_default(dst, dstCapacity, 416 CTable_MatchLength, mlCodeTable, 417 CTable_OffsetBits, ofCodeTable, 418 CTable_LitLength, llCodeTable, 419 sequences, nbSeq, longOffsets); 420 } 421