]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
enforce a minimum price of 1 bit per literal in the optimal parser
authorYann Collet <cyan@fb.com>
Fri, 7 Jan 2022 21:51:28 +0000 (13:51 -0800)
committerYann Collet <cyan@fb.com>
Fri, 7 Jan 2022 21:53:48 +0000 (13:53 -0800)
lib/compress/zstd_opt.c

index 1b1ddad428907bd4726d9f9cd1ba53df60528ce7..2c48ae55e449bf822d229db9cab3acd572a41a64 100644 (file)
@@ -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;
     }