From: Yann Collet Date: Fri, 7 Jan 2022 21:51:28 +0000 (-0800) Subject: enforce a minimum price of 1 bit per literal in the optimal parser X-Git-Tag: v1.5.4^2~273^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e1b4828e56a028efa0efdd7c30a58e6dd48c8c1;p=thirdparty%2Fzstd.git enforce a minimum price of 1 bit per literal in the optimal parser --- diff --git a/lib/compress/zstd_opt.c b/lib/compress/zstd_opt.c index 1b1ddad42..2c48ae55e 100644 --- a/lib/compress/zstd_opt.c +++ b/lib/compress/zstd_opt.c @@ -255,11 +255,13 @@ static U32 ZSTD_rawLiteralsCost(const BYTE* const literals, U32 const litLength, return (litLength*6) * BITCOST_MULTIPLIER; /* 6 bit per literal - no statistic used */ /* dynamic statistics */ - { U32 price = litLength * optPtr->litSumBasePrice; + { U32 price = 0; U32 u; for (u=0; u < litLength; u++) { + U32 litPrice = optPtr->litSumBasePrice - WEIGHT(optPtr->litFreq[literals[u]], optLevel); assert(WEIGHT(optPtr->litFreq[literals[u]], optLevel) <= optPtr->litSumBasePrice); /* literal cost should never be negative */ - price -= WEIGHT(optPtr->litFreq[literals[u]], optLevel); + if (litPrice < BITCOST_MULTIPLIER) litPrice = BITCOST_MULTIPLIER; + price += litPrice; } return price; }