LCOV - code coverage report
Current view: top level - pression/compressor/zstd/lib/common - zstd_internal.h (source / functions) Hit Total Coverage
Test: Pression Lines: 10 10 100.0 %
Date: 2016-12-06 05:44:58 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
       3             :  * All rights reserved.
       4             :  *
       5             :  * This source code is licensed under the BSD-style license found in the
       6             :  * LICENSE file in the root directory of this source tree. An additional grant
       7             :  * of patent rights can be found in the PATENTS file in the same directory.
       8             :  */
       9             : 
      10             : #ifndef ZSTD_CCOMMON_H_MODULE
      11             : #define ZSTD_CCOMMON_H_MODULE
      12             : 
      13             : /*-*******************************************************
      14             : *  Compiler specifics
      15             : *********************************************************/
      16             : #ifdef _MSC_VER    /* Visual Studio */
      17             : #  define FORCE_INLINE static __forceinline
      18             : #  include <intrin.h>                    /* For Visual 2005 */
      19             : #  pragma warning(disable : 4127)        /* disable: C4127: conditional expression is constant */
      20             : #  pragma warning(disable : 4324)        /* disable: C4324: padded structure */
      21             : #  pragma warning(disable : 4100)        /* disable: C4100: unreferenced formal parameter */
      22             : #else
      23             : #  if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */
      24             : #    ifdef __GNUC__
      25             : #      define FORCE_INLINE static inline __attribute__((always_inline))
      26             : #    else
      27             : #      define FORCE_INLINE static inline
      28             : #    endif
      29             : #  else
      30             : #    define FORCE_INLINE static
      31             : #  endif /* __STDC_VERSION__ */
      32             : #endif
      33             : 
      34             : 
      35             : /*-*************************************
      36             : *  Dependencies
      37             : ***************************************/
      38             : #include "mem.h"
      39             : #include "error_private.h"
      40             : #define ZSTD_STATIC_LINKING_ONLY
      41             : #include "zstd.h"
      42             : 
      43             : 
      44             : /*-*************************************
      45             : *  shared macros
      46             : ***************************************/
      47             : #define MIN(a,b) ((a)<(b) ? (a) : (b))
      48             : #define MAX(a,b) ((a)>(b) ? (a) : (b))
      49             : #define CHECK_F(f) { size_t const errcod = f; if (ERR_isError(errcod)) return errcod; }  /* check and Forward error code */
      50             : #define CHECK_E(f, e) { size_t const errcod = f; if (ERR_isError(errcod)) return ERROR(e); }  /* check and send Error code */
      51             : 
      52             : 
      53             : /*-*************************************
      54             : *  Common constants
      55             : ***************************************/
      56             : #define ZSTD_OPT_NUM    (1<<12)
      57             : #define ZSTD_DICT_MAGIC  0xEC30A437   /* v0.7+ */
      58             : 
      59             : #define ZSTD_REP_NUM      3                 /* number of repcodes */
      60             : #define ZSTD_REP_CHECK    (ZSTD_REP_NUM)    /* number of repcodes to check by the optimal parser */
      61             : #define ZSTD_REP_MOVE     (ZSTD_REP_NUM-1)
      62             : #define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM)
      63             : static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
      64             : 
      65             : #define KB *(1 <<10)
      66             : #define MB *(1 <<20)
      67             : #define GB *(1U<<30)
      68             : 
      69             : #define BIT7 128
      70             : #define BIT6  64
      71             : #define BIT5  32
      72             : #define BIT4  16
      73             : #define BIT1   2
      74             : #define BIT0   1
      75             : 
      76             : #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
      77             : static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
      78             : static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
      79             : 
      80             : #define ZSTD_BLOCKHEADERSIZE 3   /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
      81             : static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
      82             : typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
      83             : 
      84             : #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
      85             : #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */)   /* for a non-null block */
      86             : 
      87             : #define HufLog 12
      88             : typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
      89             : 
      90             : #define LONGNBSEQ 0x7F00
      91             : 
      92             : #define MINMATCH 3
      93             : #define EQUAL_READ32 4
      94             : 
      95             : #define Litbits  8
      96             : #define MaxLit ((1<<Litbits) - 1)
      97             : #define MaxML  52
      98             : #define MaxLL  35
      99             : #define MaxOff 28
     100             : #define MaxSeq MAX(MaxLL, MaxML)   /* Assumption : MaxOff < MaxLL,MaxML */
     101             : #define MLFSELog    9
     102             : #define LLFSELog    9
     103             : #define OffFSELog   8
     104             : 
     105             : static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     106             :                                       1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
     107             :                                      13,14,15,16 };
     108             : static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
     109             :                                              2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
     110             :                                             -1,-1,-1,-1 };
     111             : #define LL_DEFAULTNORMLOG 6  /* for static allocation */
     112             : static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
     113             : 
     114             : static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     115             :                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     116             :                                       1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11,
     117             :                                      12,13,14,15,16 };
     118             : static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
     119             :                                              1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     120             :                                              1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
     121             :                                             -1,-1,-1,-1,-1 };
     122             : #define ML_DEFAULTNORMLOG 6  /* for static allocation */
     123             : static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
     124             : 
     125             : static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
     126             :                                               1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 };
     127             : #define OF_DEFAULTNORMLOG 5  /* for static allocation */
     128             : static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
     129             : 
     130             : 
     131             : /*-*******************************************
     132             : *  Shared functions to include for inlining
     133             : *********************************************/
     134    54354406 : static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
     135             : #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
     136             : 
     137             : /*! ZSTD_wildcopy() :
     138             : *   custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
     139             : #define WILDCOPY_OVERLENGTH 8
     140    22838136 : MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length)
     141             : {
     142    22838136 :     const BYTE* ip = (const BYTE*)src;
     143    22838136 :     BYTE* op = (BYTE*)dst;
     144    22838136 :     BYTE* const oend = op + length;
     145             :     do
     146    32197558 :         COPY8(op, ip)
     147    32197558 :     while (op < oend);
     148    22838136 : }
     149             : 
     150             : MEM_STATIC void ZSTD_wildcopy_e(void* dst, const void* src, void* dstEnd)   /* should be faster for decoding, but strangely, not verified on all platform */
     151             : {
     152             :     const BYTE* ip = (const BYTE*)src;
     153             :     BYTE* op = (BYTE*)dst;
     154             :     BYTE* const oend = (BYTE*)dstEnd;
     155             :     do
     156             :         COPY8(op, ip)
     157             :     while (op < oend);
     158             : }
     159             : 
     160             : 
     161             : /*-*******************************************
     162             : *  Private interfaces
     163             : *********************************************/
     164             : typedef struct ZSTD_stats_s ZSTD_stats_t;
     165             : 
     166             : typedef struct {
     167             :     U32 off;
     168             :     U32 len;
     169             : } ZSTD_match_t;
     170             : 
     171             : typedef struct {
     172             :     U32 price;
     173             :     U32 off;
     174             :     U32 mlen;
     175             :     U32 litlen;
     176             :     U32 rep[ZSTD_REP_NUM];
     177             : } ZSTD_optimal_t;
     178             : 
     179             : 
     180             : typedef struct seqDef_s {
     181             :     U32 offset;
     182             :     U16 litLength;
     183             :     U16 matchLength;
     184             : } seqDef;
     185             : 
     186             : 
     187             : typedef struct {
     188             :     seqDef* sequencesStart;
     189             :     seqDef* sequences;
     190             :     BYTE* litStart;
     191             :     BYTE* lit;
     192             :     BYTE* llCode;
     193             :     BYTE* mlCode;
     194             :     BYTE* ofCode;
     195             :     U32   longLengthID;   /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
     196             :     U32   longLengthPos;
     197             :     /* opt */
     198             :     ZSTD_optimal_t* priceTable;
     199             :     ZSTD_match_t* matchTable;
     200             :     U32* matchLengthFreq;
     201             :     U32* litLengthFreq;
     202             :     U32* litFreq;
     203             :     U32* offCodeFreq;
     204             :     U32  matchLengthSum;
     205             :     U32  matchSum;
     206             :     U32  litLengthSum;
     207             :     U32  litSum;
     208             :     U32  offCodeSum;
     209             :     U32  log2matchLengthSum;
     210             :     U32  log2matchSum;
     211             :     U32  log2litLengthSum;
     212             :     U32  log2litSum;
     213             :     U32  log2offCodeSum;
     214             :     U32  factor;
     215             :     U32  cachedPrice;
     216             :     U32  cachedLitLength;
     217             :     const BYTE* cachedLiterals;
     218             : } seqStore_t;
     219             : 
     220             : const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);
     221             : void ZSTD_seqToCodes(const seqStore_t* seqStorePtr);
     222             : int ZSTD_isSkipFrame(ZSTD_DCtx* dctx);
     223             : 
     224             : /* custom memory allocation functions */
     225             : void* ZSTD_defaultAllocFunction(void* opaque, size_t size);
     226             : void ZSTD_defaultFreeFunction(void* opaque, void* address);
     227             : static const ZSTD_customMem defaultCustomMem = { ZSTD_defaultAllocFunction, ZSTD_defaultFreeFunction, NULL };
     228             : void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
     229             : void ZSTD_free(void* ptr, ZSTD_customMem customMem);
     230             : 
     231             : 
     232             : /*======  common function  ======*/
     233             : 
     234    11170878 : MEM_STATIC U32 ZSTD_highbit32(U32 val)
     235             : {
     236             : #   if defined(_MSC_VER)   /* Visual */
     237             :     unsigned long r=0;
     238             :     _BitScanReverse(&r, val);
     239             :     return (unsigned)r;
     240             : #   elif defined(__GNUC__) && (__GNUC__ >= 3)   /* GCC Intrinsic */
     241    11170878 :     return 31 - __builtin_clz(val);
     242             : #   else   /* Software version */
     243             :     static const int DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
     244             :     U32 v = val;
     245             :     int r;
     246             :     v |= v >> 1;
     247             :     v |= v >> 2;
     248             :     v |= v >> 4;
     249             :     v |= v >> 8;
     250             :     v |= v >> 16;
     251             :     r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
     252             :     return r;
     253             : #   endif
     254             : }
     255             : 
     256             : 
     257             : #endif   /* ZSTD_CCOMMON_H_MODULE */

Generated by: LCOV version 1.11