private static int ZSTD_needSequenceEntropyTables(ZSTD_fseCTablesMetadata_t *fseMetadata) { if (fseMetadata->llType == symbolEncodingType_e.set_compressed || fseMetadata->llType == symbolEncodingType_e.set_rle) { return(1); } if (fseMetadata->mlType == symbolEncodingType_e.set_compressed || fseMetadata->mlType == symbolEncodingType_e.set_rle) { return(1); } if (fseMetadata->ofType == symbolEncodingType_e.set_compressed || fseMetadata->ofType == symbolEncodingType_e.set_rle) { return(1); } return(0); }
/** ZSTD_compressSubBlock_sequences() : * Compresses sequences section for a sub-block. * fseMetadata->llType, fseMetadata->ofType, and fseMetadata->mlType have * symbol compression modes for the super-block. * The first successfully compressed block will have these in its header. * We set entropyWritten=1 when we succeed in compressing the sequences. * The following sub-blocks will always have repeat mode. * @return : compressed size of sequences section of a sub-block * Or 0 if it is unable to compress * Or error code. */ private static nuint ZSTD_compressSubBlock_sequences(ZSTD_fseCTables_t *fseTables, ZSTD_fseCTablesMetadata_t *fseMetadata, seqDef_s *sequences, nuint nbSeq, byte *llCode, byte *mlCode, byte *ofCode, ZSTD_CCtx_params_s *cctxParams, void *dst, nuint dstCapacity, int bmi2, int writeEntropy, int *entropyWritten) { int longOffsets = ((cctxParams->cParams.windowLog > ((uint)(MEM_32bits ? 25 : 57))) ? 1 : 0); byte *ostart = (byte *)(dst); byte *oend = ostart + dstCapacity; byte *op = ostart; byte *seqHead; *entropyWritten = 0; if ((oend - op) < 3 + 1) { return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); } if (nbSeq < 0x7F) { *op++ = (byte)(nbSeq); } else if (nbSeq < 0x7F00) { op[0] = (byte)((nbSeq >> 8) + 0x80); op[1] = (byte)(nbSeq); op += 2; } else { op[0] = 0xFF; MEM_writeLE16((void *)(op + 1), (ushort)(nbSeq - 0x7F00)); op += 3; } if (nbSeq == 0) { return((nuint)(op - ostart)); } seqHead = op++; if (writeEntropy != 0) { uint LLtype = (uint)fseMetadata->llType; uint Offtype = (uint)fseMetadata->ofType; uint MLtype = (uint)fseMetadata->mlType; *seqHead = (byte)((LLtype << 6) + (Offtype << 4) + (MLtype << 2)); memcpy((void *)(op), (void *)(fseMetadata->fseTablesBuffer), (fseMetadata->fseTablesSize)); op += fseMetadata->fseTablesSize; } else { uint repeat = (uint)symbolEncodingType_e.set_repeat; *seqHead = (byte)((repeat << 6) + (repeat << 4) + (repeat << 2)); } { nuint bitstreamSize = ZSTD_encodeSequences((void *)op, (nuint)(oend - op), (uint *)fseTables->matchlengthCTable, mlCode, (uint *)fseTables->offcodeCTable, ofCode, (uint *)fseTables->litlengthCTable, llCode, sequences, nbSeq, longOffsets, bmi2); { nuint err_code = (bitstreamSize); if ((ERR_isError(err_code)) != 0) { return(err_code); } } op += bitstreamSize; if (writeEntropy != 0 && fseMetadata->lastCountSize != 0 && fseMetadata->lastCountSize + bitstreamSize < 4) { assert(fseMetadata->lastCountSize + bitstreamSize == 3); return(0); } } if (op - seqHead < 4) { return(0); } *entropyWritten = 1; return((nuint)(op - ostart)); }
private static nuint ZSTD_estimateSubBlockSize_sequences(byte *ofCodeTable, byte *llCodeTable, byte *mlCodeTable, nuint nbSeq, ZSTD_fseCTables_t *fseTables, ZSTD_fseCTablesMetadata_t *fseMetadata, void *workspace, nuint wkspSize, int writeEntropy) { nuint sequencesSectionHeaderSize = 3; nuint cSeqSizeEstimate = 0; cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->ofType, ofCodeTable, 31, nbSeq, (uint *)fseTables->offcodeCTable, (uint *)null, (short *)OF_defaultNorm, OF_defaultNormLog, 28, workspace, wkspSize); cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->llType, llCodeTable, 35, nbSeq, (uint *)fseTables->litlengthCTable, (uint *)LL_bits, (short *)LL_defaultNorm, LL_defaultNormLog, 35, workspace, wkspSize); cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->mlType, mlCodeTable, 52, nbSeq, (uint *)fseTables->matchlengthCTable, (uint *)ML_bits, (short *)ML_defaultNorm, ML_defaultNormLog, 52, workspace, wkspSize); if (writeEntropy != 0) { cSeqSizeEstimate += fseMetadata->fseTablesSize; } return(cSeqSizeEstimate + sequencesSectionHeaderSize); }
/** 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)); }