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