contrib/python-zstandard/zstd/compress/huf_compress.c
changeset 40122 73fef626dae3
parent 37495 b1fb341d8a61
child 42070 675775c33ab6
equal deleted inserted replaced
40121:89742f1fa6cb 40122:73fef626dae3
    43 /* **************************************************************
    43 /* **************************************************************
    44 *  Includes
    44 *  Includes
    45 ****************************************************************/
    45 ****************************************************************/
    46 #include <string.h>     /* memcpy, memset */
    46 #include <string.h>     /* memcpy, memset */
    47 #include <stdio.h>      /* printf (debug) */
    47 #include <stdio.h>      /* printf (debug) */
       
    48 #include "compiler.h"
    48 #include "bitstream.h"
    49 #include "bitstream.h"
    49 #include "compiler.h"
    50 #include "hist.h"
    50 #define FSE_STATIC_LINKING_ONLY   /* FSE_optimalTableLog_internal */
    51 #define FSE_STATIC_LINKING_ONLY   /* FSE_optimalTableLog_internal */
    51 #include "fse.h"        /* header compression */
    52 #include "fse.h"        /* header compression */
    52 #define HUF_STATIC_LINKING_ONLY
    53 #define HUF_STATIC_LINKING_ONLY
    53 #include "huf.h"
    54 #include "huf.h"
    54 #include "error_private.h"
    55 #include "error_private.h"
    56 
    57 
    57 /* **************************************************************
    58 /* **************************************************************
    58 *  Error Management
    59 *  Error Management
    59 ****************************************************************/
    60 ****************************************************************/
    60 #define HUF_isError ERR_isError
    61 #define HUF_isError ERR_isError
    61 #define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; }   /* use only *after* variable declarations */
    62 #define HUF_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)   /* use only *after* variable declarations */
    62 #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
    63 #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
    63 #define CHECK_F(f)   { CHECK_V_F(_var_err__, f); }
    64 #define CHECK_F(f)   { CHECK_V_F(_var_err__, f); }
    64 
    65 
    65 
    66 
    66 /* **************************************************************
    67 /* **************************************************************
    79  * Same as FSE_compress(), but dedicated to huff0's weights compression.
    80  * Same as FSE_compress(), but dedicated to huff0's weights compression.
    80  * The use case needs much less stack memory.
    81  * The use case needs much less stack memory.
    81  * Note : all elements within weightTable are supposed to be <= HUF_TABLELOG_MAX.
    82  * Note : all elements within weightTable are supposed to be <= HUF_TABLELOG_MAX.
    82  */
    83  */
    83 #define MAX_FSE_TABLELOG_FOR_HUFF_HEADER 6
    84 #define MAX_FSE_TABLELOG_FOR_HUFF_HEADER 6
    84 size_t HUF_compressWeights (void* dst, size_t dstSize, const void* weightTable, size_t wtSize)
    85 static size_t HUF_compressWeights (void* dst, size_t dstSize, const void* weightTable, size_t wtSize)
    85 {
    86 {
    86     BYTE* const ostart = (BYTE*) dst;
    87     BYTE* const ostart = (BYTE*) dst;
    87     BYTE* op = ostart;
    88     BYTE* op = ostart;
    88     BYTE* const oend = ostart + dstSize;
    89     BYTE* const oend = ostart + dstSize;
    89 
    90 
    98 
    99 
    99     /* init conditions */
   100     /* init conditions */
   100     if (wtSize <= 1) return 0;  /* Not compressible */
   101     if (wtSize <= 1) return 0;  /* Not compressible */
   101 
   102 
   102     /* Scan input and build symbol stats */
   103     /* Scan input and build symbol stats */
   103     {   CHECK_V_F(maxCount, FSE_count_simple(count, &maxSymbolValue, weightTable, wtSize) );
   104     {   unsigned const maxCount = HIST_count_simple(count, &maxSymbolValue, weightTable, wtSize);   /* never fails */
   104         if (maxCount == wtSize) return 1;   /* only a single symbol in src : rle */
   105         if (maxCount == wtSize) return 1;   /* only a single symbol in src : rle */
   105         if (maxCount == 1) return 0;         /* each symbol present maximum once => not compressible */
   106         if (maxCount == 1) return 0;        /* each symbol present maximum once => not compressible */
   106     }
   107     }
   107 
   108 
   108     tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue);
   109     tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue);
   109     CHECK_F( FSE_normalizeCount(norm, tableLog, count, wtSize, maxSymbolValue) );
   110     CHECK_F( FSE_normalizeCount(norm, tableLog, count, wtSize, maxSymbolValue) );
   110 
   111 
   212         { U32 n; for (n=0; n<nbSymbols; n++) CTable[n].val = valPerRank[CTable[n].nbBits]++; }
   213         { U32 n; for (n=0; n<nbSymbols; n++) CTable[n].val = valPerRank[CTable[n].nbBits]++; }
   213     }
   214     }
   214 
   215 
   215     *maxSymbolValuePtr = nbSymbols - 1;
   216     *maxSymbolValuePtr = nbSymbols - 1;
   216     return readSize;
   217     return readSize;
       
   218 }
       
   219 
       
   220 U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue)
       
   221 {
       
   222     const HUF_CElt* table = (const HUF_CElt*)symbolTable;
       
   223     assert(symbolValue <= HUF_SYMBOLVALUE_MAX);
       
   224     return table[symbolValue].nbBits;
   217 }
   225 }
   218 
   226 
   219 
   227 
   220 typedef struct nodeElt_s {
   228 typedef struct nodeElt_s {
   221     U32 count;
   229     U32 count;
   658                                            src, srcSize,
   666                                            src, srcSize,
   659                                            singleStream, oldHufTable, bmi2);
   667                                            singleStream, oldHufTable, bmi2);
   660     }
   668     }
   661 
   669 
   662     /* Scan input and build symbol stats */
   670     /* Scan input and build symbol stats */
   663     {   CHECK_V_F(largest, FSE_count_wksp (table->count, &maxSymbolValue, (const BYTE*)src, srcSize, table->count) );
   671     {   CHECK_V_F(largest, HIST_count_wksp (table->count, &maxSymbolValue, (const BYTE*)src, srcSize, table->count) );
   664         if (largest == srcSize) { *ostart = ((const BYTE*)src)[0]; return 1; }   /* single symbol, rle */
   672         if (largest == srcSize) { *ostart = ((const BYTE*)src)[0]; return 1; }   /* single symbol, rle */
   665         if (largest <= (srcSize >> 7)+1) return 0;   /* heuristic : probably not compressible enough */
   673         if (largest <= (srcSize >> 7)+4) return 0;   /* heuristic : probably not compressible enough */
   666     }
   674     }
   667 
   675 
   668     /* Check validity of previous table */
   676     /* Check validity of previous table */
   669     if ( repeat
   677     if ( repeat
   670       && *repeat == HUF_repeat_check
   678       && *repeat == HUF_repeat_check