contrib/python-zstandard/zstd/common/entropy_common.c
changeset 37495 b1fb341d8a61
parent 30822 b54a2984cdd4
child 40121 73fef626dae3
equal deleted inserted replaced
37494:1ce7a55b09d1 37495:b1fb341d8a61
    41 #include "fse.h"
    41 #include "fse.h"
    42 #define HUF_STATIC_LINKING_ONLY  /* HUF_TABLELOG_ABSOLUTEMAX */
    42 #define HUF_STATIC_LINKING_ONLY  /* HUF_TABLELOG_ABSOLUTEMAX */
    43 #include "huf.h"
    43 #include "huf.h"
    44 
    44 
    45 
    45 
    46 /*-****************************************
    46 /*===   Version   ===*/
    47 *  FSE Error Management
    47 unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }
    48 ******************************************/
    48 
       
    49 
       
    50 /*===   Error Management   ===*/
    49 unsigned FSE_isError(size_t code) { return ERR_isError(code); }
    51 unsigned FSE_isError(size_t code) { return ERR_isError(code); }
    50 
       
    51 const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
    52 const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
    52 
    53 
    53 
       
    54 /* **************************************************************
       
    55 *  HUF Error Management
       
    56 ****************************************************************/
       
    57 unsigned HUF_isError(size_t code) { return ERR_isError(code); }
    54 unsigned HUF_isError(size_t code) { return ERR_isError(code); }
    58 
       
    59 const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
    55 const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
    60 
    56 
    61 
    57 
    62 /*-**************************************************************
    58 /*-**************************************************************
    63 *  FSE NCount encoding-decoding
    59 *  FSE NCount encoding-decoding
    64 ****************************************************************/
    60 ****************************************************************/
    65 static short FSE_abs(short a) { return (short)(a<0 ? -a : a); }
       
    66 
       
    67 size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
    61 size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
    68                  const void* headerBuffer, size_t hbSize)
    62                  const void* headerBuffer, size_t hbSize)
    69 {
    63 {
    70     const BYTE* const istart = (const BYTE*) headerBuffer;
    64     const BYTE* const istart = (const BYTE*) headerBuffer;
    71     const BYTE* const iend = istart + hbSize;
    65     const BYTE* const iend = istart + hbSize;
   115                 bitCount &= 7;
   109                 bitCount &= 7;
   116                 bitStream = MEM_readLE32(ip) >> bitCount;
   110                 bitStream = MEM_readLE32(ip) >> bitCount;
   117             } else {
   111             } else {
   118                 bitStream >>= 2;
   112                 bitStream >>= 2;
   119         }   }
   113         }   }
   120         {   short const max = (short)((2*threshold-1)-remaining);
   114         {   int const max = (2*threshold-1) - remaining;
   121             short count;
   115             int count;
   122 
   116 
   123             if ((bitStream & (threshold-1)) < (U32)max) {
   117             if ((bitStream & (threshold-1)) < (U32)max) {
   124                 count = (short)(bitStream & (threshold-1));
   118                 count = bitStream & (threshold-1);
   125                 bitCount   += nbBits-1;
   119                 bitCount += nbBits-1;
   126             } else {
   120             } else {
   127                 count = (short)(bitStream & (2*threshold-1));
   121                 count = bitStream & (2*threshold-1);
   128                 if (count >= threshold) count -= max;
   122                 if (count >= threshold) count -= max;
   129                 bitCount   += nbBits;
   123                 bitCount += nbBits;
   130             }
   124             }
   131 
   125 
   132             count--;   /* extra accuracy */
   126             count--;   /* extra accuracy */
   133             remaining -= FSE_abs(count);
   127             remaining -= count < 0 ? -count : count;   /* -1 means +1 */
   134             normalizedCounter[charnum++] = count;
   128             normalizedCounter[charnum++] = (short)count;
   135             previous0 = !count;
   129             previous0 = !count;
   136             while (remaining < threshold) {
   130             while (remaining < threshold) {
   137                 nbBits--;
   131                 nbBits--;
   138                 threshold >>= 1;
   132                 threshold >>= 1;
   139             }
   133             }