private static nuint ZSTD_minGain(nuint srcSize, ZSTD_strategy strat)
        {
            uint minlog = (strat >= ZSTD_strategy.ZSTD_btultra) ? (uint)(strat) - 1 : 6;

            assert((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_strategy, (int)strat)) != 0);
            return((srcSize >> (int)minlog) + 2);
        }
Beispiel #2
0
        public static nuint ZSTD_compressLiterals(ZSTD_hufCTables_t *prevHuf, ZSTD_hufCTables_t *nextHuf, ZSTD_strategy strategy, int disableLiteralCompression, void *dst, nuint dstCapacity, void *src, nuint srcSize, void *entropyWorkspace, nuint entropyWorkspaceSize, int bmi2)
        {
            nuint minGain              = ZSTD_minGain(srcSize, strategy);
            nuint lhSize               = (nuint)(3 + ((srcSize >= (uint)(1 * (1 << 10))) ? 1 : 0) + ((srcSize >= (uint)(16 * (1 << 10))) ? 1 : 0));
            byte *ostart               = (byte *)(dst);
            uint  singleStream         = ((srcSize < 256) ? 1U : 0U);
            symbolEncodingType_e hType = symbolEncodingType_e.set_compressed;
            nuint cLitSize;

            memcpy((void *)(nextHuf), (void *)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t))));
            if (disableLiteralCompression != 0)
            {
                return(ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize));
            }


            {
                nuint minLitSize = (nuint)((prevHuf->repeatMode == HUF_repeat.HUF_repeat_valid) ? 6 : 63);

                if (srcSize <= minLitSize)
                {
                    return(ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize));
                }
            }

            if (dstCapacity < lhSize + 1)
            {
                return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)));
            }


            {
                HUF_repeat repeat       = prevHuf->repeatMode;
                int        preferRepeat = strategy < ZSTD_strategy.ZSTD_lazy ? ((srcSize <= 1024) ? 1 : 0) : 0;

                if (repeat == HUF_repeat.HUF_repeat_valid && lhSize == 3)
                {
                    singleStream = 1;
                }

                cLitSize = singleStream != 0 ? HUF_compress1X_repeat((void *)(ostart + lhSize), dstCapacity - lhSize, src, srcSize, 255, 11, entropyWorkspace, entropyWorkspaceSize, (HUF_CElt_s *)(nextHuf->CTable), &repeat, preferRepeat, bmi2) : HUF_compress4X_repeat((void *)(ostart + lhSize), dstCapacity - lhSize, src, srcSize, 255, 11, entropyWorkspace, entropyWorkspaceSize, (HUF_CElt_s *)(nextHuf->CTable), &repeat, preferRepeat, bmi2);
                if (repeat != HUF_repeat.HUF_repeat_none)
                {
                    hType = symbolEncodingType_e.set_repeat;
                }
            }

            if (((uint)(((((cLitSize == 0) || (cLitSize >= srcSize - minGain))) ? 1 : 0)) | ERR_isError(cLitSize)) != 0)
            {
                memcpy((void *)(nextHuf), (void *)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t))));
                return(ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize));
            }

            if (cLitSize == 1)
            {
                memcpy((void *)(nextHuf), (void *)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t))));
                return(ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize));
            }

            if (hType == symbolEncodingType_e.set_compressed)
            {
                nextHuf->repeatMode = HUF_repeat.HUF_repeat_check;
            }

            switch (lhSize)
            {
            case 3:
            {
                uint lhc = (uint)(hType + ((singleStream == 0 ? 1 : 0) << 2)) + ((uint)(srcSize) << 4) + ((uint)(cLitSize) << 14);

                MEM_writeLE24((void *)ostart, lhc);
                break;
            }

            case 4:
            {
                uint lhc = (uint)(hType + (2 << 2)) + ((uint)(srcSize) << 4) + ((uint)(cLitSize) << 18);

                MEM_writeLE32((void *)ostart, lhc);
                break;
            }

            case 5:
            {
                uint lhc = (uint)(hType + (3 << 2)) + ((uint)(srcSize) << 4) + ((uint)(cLitSize) << 22);

                MEM_writeLE32((void *)ostart, lhc);
                ostart[4] = (byte)(cLitSize >> 10);
                break;
            }

            default:
            {
                assert(0 != 0);
            }
            break;
            }

            return(lhSize + cLitSize);
        }
        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);
        }
Beispiel #4
0
        /** 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));
        }