public static symbolEncodingType_e ZSTD_selectEncodingType(FSE_repeat *repeatMode, uint *count, uint max, nuint mostFrequent, nuint nbSeq, uint FSELog, uint *prevCTable, short *defaultNorm, uint defaultNormLog, ZSTD_defaultPolicy_e isDefaultAllowed, ZSTD_strategy strategy) { if (mostFrequent == nbSeq) { *repeatMode = FSE_repeat.FSE_repeat_none; if (isDefaultAllowed != default && nbSeq <= 2) { return(symbolEncodingType_e.set_basic); } return(symbolEncodingType_e.set_rle); } if (strategy < ZSTD_strategy.ZSTD_lazy) { if (isDefaultAllowed != default) { nuint staticFse_nbSeq_max = 1000; nuint mult = (nuint)(10 - strategy); nuint baseLog = 3; nuint dynamicFse_nbSeq_min = (((nuint)(1) << (int)defaultNormLog) * mult) >> (int)baseLog; assert(defaultNormLog >= 5 && defaultNormLog <= 6); assert(mult <= 9 && mult >= 7); if ((*repeatMode == FSE_repeat.FSE_repeat_valid) && (nbSeq < staticFse_nbSeq_max)) { return(symbolEncodingType_e.set_repeat); } if ((nbSeq < dynamicFse_nbSeq_min) || (mostFrequent < (nbSeq >> (int)(defaultNormLog - 1)))) { *repeatMode = FSE_repeat.FSE_repeat_none; return(symbolEncodingType_e.set_basic); } } } else { nuint basicCost = isDefaultAllowed != default ? ZSTD_crossEntropyCost(defaultNorm, defaultNormLog, count, max) : (unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); nuint repeatCost = *repeatMode != FSE_repeat.FSE_repeat_none ? ZSTD_fseBitCost(prevCTable, count, max) : (unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); nuint NCountCost = ZSTD_NCountCost(count, max, nbSeq, FSELog); nuint compressedCost = (NCountCost << 3) + ZSTD_entropyCost(count, max, nbSeq); if (isDefaultAllowed != default) { assert((ERR_isError(basicCost)) == 0); assert(!(*repeatMode == FSE_repeat.FSE_repeat_valid && (ERR_isError(repeatCost)) != 0)); } assert((ERR_isError(NCountCost)) == 0); assert(compressedCost < (unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxCode)))); if (basicCost <= repeatCost && basicCost <= compressedCost) { assert(isDefaultAllowed != default); *repeatMode = FSE_repeat.FSE_repeat_none; return(symbolEncodingType_e.set_basic); } if (repeatCost <= compressedCost) { assert((ERR_isError(repeatCost)) == 0); return(symbolEncodingType_e.set_repeat); } assert(compressedCost < basicCost && compressedCost < repeatCost); } *repeatMode = FSE_repeat.FSE_repeat_check; return(symbolEncodingType_e.set_compressed); }
/** ZSTD_buildSuperBlockEntropy_sequences() : * Builds entropy for the super-block sequences. * Stores symbol compression modes and fse table to fseMetadata. * @return : size of fse tables or error code */ private static nuint ZSTD_buildSuperBlockEntropy_sequences(seqStore_t *seqStorePtr, ZSTD_fseCTables_t *prevEntropy, ZSTD_fseCTables_t *nextEntropy, ZSTD_CCtx_params_s *cctxParams, ZSTD_fseCTablesMetadata_t *fseMetadata, void *workspace, nuint wkspSize) { byte * wkspStart = (byte *)(workspace); byte * wkspEnd = wkspStart + wkspSize; byte * countWkspStart = wkspStart; uint * countWksp = (uint *)(workspace); nuint countWkspSize = (uint)((((35) > (52) ? (35) : (52)) + 1)) * (nuint)(4); byte * cTableWksp = countWkspStart + countWkspSize; nuint cTableWkspSize = (nuint)(wkspEnd - cTableWksp); ZSTD_strategy strategy = cctxParams->cParams.strategy; uint * CTable_LitLength = (uint *)nextEntropy->litlengthCTable; uint * CTable_OffsetBits = (uint *)nextEntropy->offcodeCTable; uint * CTable_MatchLength = (uint *)nextEntropy->matchlengthCTable; byte * ofCodeTable = seqStorePtr->ofCode; byte * llCodeTable = seqStorePtr->llCode; byte * mlCodeTable = seqStorePtr->mlCode; nuint nbSeq = (nuint)(seqStorePtr->sequences - seqStorePtr->sequencesStart); byte * ostart = (byte *)fseMetadata->fseTablesBuffer; byte * oend = ostart + (nuint)(133); byte * op = ostart; assert(cTableWkspSize >= (uint)((1 << ((((9) > (9) ? (9) : (9))) > (8) ? (((9) > (9) ? (9) : (9))) : (8)))) * (nuint)(sizeof(byte))); memset((workspace), (0), (wkspSize)); fseMetadata->lastCountSize = 0; ZSTD_seqToCodes(seqStorePtr); { uint LLtype; uint max = 35; nuint mostFrequent = HIST_countFast_wksp(countWksp, &max, (void *)llCodeTable, nbSeq, workspace, wkspSize); nextEntropy->litlength_repeatMode = prevEntropy->litlength_repeatMode; LLtype = (uint)(ZSTD_selectEncodingType(&nextEntropy->litlength_repeatMode, countWksp, max, mostFrequent, nbSeq, 9, (uint *)prevEntropy->litlengthCTable, (short *)LL_defaultNorm, LL_defaultNormLog, ZSTD_defaultPolicy_e.ZSTD_defaultAllowed, strategy)); assert(symbolEncodingType_e.set_basic < symbolEncodingType_e.set_compressed && symbolEncodingType_e.set_rle < symbolEncodingType_e.set_compressed); assert(!(LLtype < (uint)symbolEncodingType_e.set_compressed && nextEntropy->litlength_repeatMode != FSE_repeat.FSE_repeat_none)); { nuint countSize = ZSTD_buildCTable((void *)op, (nuint)(oend - op), CTable_LitLength, 9, (symbolEncodingType_e)(LLtype), countWksp, max, llCodeTable, nbSeq, (short *)LL_defaultNorm, LL_defaultNormLog, 35, (uint *)prevEntropy->litlengthCTable, (nuint)(1316), (void *)cTableWksp, cTableWkspSize); { nuint err_code = (countSize); if ((ERR_isError(err_code)) != 0) { return(err_code); } } if (LLtype == (uint)symbolEncodingType_e.set_compressed) { fseMetadata->lastCountSize = countSize; } op += countSize; fseMetadata->llType = (symbolEncodingType_e)(LLtype); } } { uint Offtype; uint max = 31; nuint mostFrequent = HIST_countFast_wksp(countWksp, &max, (void *)ofCodeTable, nbSeq, workspace, wkspSize); ZSTD_defaultPolicy_e defaultPolicy = (max <= 28) ? ZSTD_defaultPolicy_e.ZSTD_defaultAllowed : ZSTD_defaultPolicy_e.ZSTD_defaultDisallowed; nextEntropy->offcode_repeatMode = prevEntropy->offcode_repeatMode; Offtype = (uint)(ZSTD_selectEncodingType(&nextEntropy->offcode_repeatMode, countWksp, max, mostFrequent, nbSeq, 8, (uint *)prevEntropy->offcodeCTable, (short *)OF_defaultNorm, OF_defaultNormLog, defaultPolicy, strategy)); assert(!(Offtype < (uint)symbolEncodingType_e.set_compressed && nextEntropy->offcode_repeatMode != FSE_repeat.FSE_repeat_none)); { nuint countSize = ZSTD_buildCTable((void *)op, (nuint)(oend - op), CTable_OffsetBits, 8, (symbolEncodingType_e)(Offtype), countWksp, max, ofCodeTable, nbSeq, (short *)OF_defaultNorm, OF_defaultNormLog, 28, (uint *)prevEntropy->offcodeCTable, (nuint)(772), (void *)cTableWksp, cTableWkspSize); { nuint err_code = (countSize); if ((ERR_isError(err_code)) != 0) { return(err_code); } } if (Offtype == (uint)symbolEncodingType_e.set_compressed) { fseMetadata->lastCountSize = countSize; } op += countSize; fseMetadata->ofType = (symbolEncodingType_e)(Offtype); } } { uint MLtype; uint max = 52; nuint mostFrequent = HIST_countFast_wksp(countWksp, &max, (void *)mlCodeTable, nbSeq, workspace, wkspSize); nextEntropy->matchlength_repeatMode = prevEntropy->matchlength_repeatMode; MLtype = (uint)(ZSTD_selectEncodingType(&nextEntropy->matchlength_repeatMode, countWksp, max, mostFrequent, nbSeq, 9, (uint *)prevEntropy->matchlengthCTable, (short *)ML_defaultNorm, ML_defaultNormLog, ZSTD_defaultPolicy_e.ZSTD_defaultAllowed, strategy)); assert(!(MLtype < (uint)symbolEncodingType_e.set_compressed && nextEntropy->matchlength_repeatMode != FSE_repeat.FSE_repeat_none)); { nuint countSize = ZSTD_buildCTable((void *)op, (nuint)(oend - op), CTable_MatchLength, 9, (symbolEncodingType_e)(MLtype), countWksp, max, mlCodeTable, nbSeq, (short *)ML_defaultNorm, ML_defaultNormLog, 52, (uint *)prevEntropy->matchlengthCTable, (nuint)(1452), (void *)cTableWksp, cTableWkspSize); { nuint err_code = (countSize); if ((ERR_isError(err_code)) != 0) { return(err_code); } } if (MLtype == (uint)symbolEncodingType_e.set_compressed) { fseMetadata->lastCountSize = countSize; } op += countSize; fseMetadata->mlType = (symbolEncodingType_e)(MLtype); } } assert((nuint)(op - ostart) <= (nuint)(sizeof(byte) * 133)); return((nuint)(op - ostart)); }