zstd_internal.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include <contrib/libs/zstd06/renames.h>
  2. /*
  3. zstd_internal - common functions to include
  4. Header File for include
  5. Copyright (C) 2014-2016, Yann Collet.
  6. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are
  9. met:
  10. * Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the following disclaimer
  14. in the documentation and/or other materials provided with the
  15. distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. You can contact the author at :
  28. - zstd homepage : https://www.zstd.net
  29. */
  30. #ifndef ZSTD_CCOMMON_H_MODULE
  31. #define ZSTD_CCOMMON_H_MODULE
  32. /*-*************************************
  33. * Dependencies
  34. ***************************************/
  35. #include "mem.h"
  36. #include "error_private.h"
  37. #include "zstd_static.h"
  38. /*-*************************************
  39. * Common macros
  40. ***************************************/
  41. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  42. #define MAX(a,b) ((a)>(b) ? (a) : (b))
  43. /*-*************************************
  44. * Common constants
  45. ***************************************/
  46. #define ZSTD_OPT_DEBUG 0 // 3 = compression stats; 5 = check encoded sequences; 9 = full logs
  47. #include <stdio.h>
  48. #if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9
  49. #define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__)
  50. #define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__)
  51. #define ZSTD_LOG_BLOCK(...) printf(__VA_ARGS__)
  52. #else
  53. #define ZSTD_LOG_PARSER(...)
  54. #define ZSTD_LOG_ENCODE(...)
  55. #define ZSTD_LOG_BLOCK(...)
  56. #endif
  57. #define ZSTD_OPT_NUM (1<<12)
  58. #define ZSTD_DICT_MAGIC 0xEC30A436
  59. #define ZSTD_REP_NUM 3
  60. #define ZSTD_REP_INIT ZSTD_REP_NUM
  61. #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
  62. #define KB *(1 <<10)
  63. #define MB *(1 <<20)
  64. #define GB *(1U<<30)
  65. #define BIT7 128
  66. #define BIT6 64
  67. #define BIT5 32
  68. #define BIT4 16
  69. #define BIT1 2
  70. #define BIT0 1
  71. #define ZSTD_WINDOWLOG_ABSOLUTEMIN 12
  72. static const size_t ZSTD_fcs_fieldSize[4] = { 0, 1, 2, 8 };
  73. #define ZSTD_BLOCKHEADERSIZE 3 /* because C standard does not allow a static const value to be defined using another static const value .... :( */
  74. static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
  75. typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
  76. #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
  77. #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
  78. #define HufLog 12
  79. #define IS_HUF 0
  80. #define IS_PCH 1
  81. #define IS_RAW 2
  82. #define IS_RLE 3
  83. #define LONGNBSEQ 0x7F00
  84. #define MINMATCH 3
  85. #define EQUAL_READ32 4
  86. #define REPCODE_STARTVALUE 1
  87. #define Litbits 8
  88. #define MaxLit ((1<<Litbits) - 1)
  89. #define MaxML 52
  90. #define MaxLL 35
  91. #define MaxOff 28
  92. #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
  93. #define MLFSELog 9
  94. #define LLFSELog 9
  95. #define OffFSELog 8
  96. #define FSE_ENCODING_RAW 0
  97. #define FSE_ENCODING_RLE 1
  98. #define FSE_ENCODING_STATIC 2
  99. #define FSE_ENCODING_DYNAMIC 3
  100. static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  101. 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
  102. 13,14,15,16 };
  103. static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
  104. 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
  105. -1,-1,-1,-1 };
  106. static const U32 LL_defaultNormLog = 6;
  107. static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  108. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  109. 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11,
  110. 12,13,14,15,16 };
  111. static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  112. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  113. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
  114. -1,-1,-1,-1,-1 };
  115. static const U32 ML_defaultNormLog = 6;
  116. static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  117. 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 };
  118. static const U32 OF_defaultNormLog = 5;
  119. /*-*******************************************
  120. * Shared functions to include for inlining
  121. *********************************************/
  122. static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
  123. #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
  124. /*! ZSTD_wildcopy() :
  125. * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
  126. #define WILDCOPY_OVERLENGTH 8
  127. MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length)
  128. {
  129. const BYTE* ip = (const BYTE*)src;
  130. BYTE* op = (BYTE*)dst;
  131. BYTE* const oend = op + length;
  132. do
  133. COPY8(op, ip)
  134. while (op < oend);
  135. }
  136. MEM_STATIC unsigned ZSTD_highbit(U32 val)
  137. {
  138. # if defined(_MSC_VER) /* Visual */
  139. unsigned long r=0;
  140. _BitScanReverse(&r, val);
  141. return (unsigned)r;
  142. # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
  143. return 31 - __builtin_clz(val);
  144. # else /* Software version */
  145. 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 };
  146. U32 v = val;
  147. int r;
  148. v |= v >> 1;
  149. v |= v >> 2;
  150. v |= v >> 4;
  151. v |= v >> 8;
  152. v |= v >> 16;
  153. r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
  154. return r;
  155. # endif
  156. }
  157. /*-*******************************************
  158. * Private interfaces
  159. *********************************************/
  160. typedef struct {
  161. U32 off;
  162. U32 len;
  163. } ZSTD_match_t;
  164. typedef struct {
  165. U32 price;
  166. U32 off;
  167. U32 mlen;
  168. U32 litlen;
  169. U32 rep[ZSTD_REP_INIT];
  170. } ZSTD_optimal_t;
  171. //#if ZSTD_OPT_DEBUG == 3
  172. // #include ".debug/zstd_stats.h"
  173. //#else
  174. typedef struct { U32 unused; } ZSTD_stats_t;
  175. MEM_STATIC void ZSTD_statsPrint(ZSTD_stats_t* stats, U32 searchLength) { (void)stats; (void)searchLength; }
  176. MEM_STATIC void ZSTD_statsInit(ZSTD_stats_t* stats) { (void)stats; }
  177. MEM_STATIC void ZSTD_statsResetFreqs(ZSTD_stats_t* stats) { (void)stats; }
  178. MEM_STATIC void ZSTD_statsUpdatePrices(ZSTD_stats_t* stats, size_t litLength, const BYTE* literals, size_t offset, size_t matchLength) { (void)stats; (void)litLength; (void)literals; (void)offset; (void)matchLength; }
  179. //#endif
  180. typedef struct {
  181. void* buffer;
  182. U32* offsetStart;
  183. U32* offset;
  184. BYTE* offCodeStart;
  185. BYTE* litStart;
  186. BYTE* lit;
  187. U16* litLengthStart;
  188. U16* litLength;
  189. BYTE* llCodeStart;
  190. U16* matchLengthStart;
  191. U16* matchLength;
  192. BYTE* mlCodeStart;
  193. U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
  194. U32 longLengthPos;
  195. /* opt */
  196. ZSTD_optimal_t* priceTable;
  197. ZSTD_match_t* matchTable;
  198. U32* matchLengthFreq;
  199. U32* litLengthFreq;
  200. U32* litFreq;
  201. U32* offCodeFreq;
  202. U32 matchLengthSum;
  203. U32 matchSum;
  204. U32 litLengthSum;
  205. U32 litSum;
  206. U32 offCodeSum;
  207. U32 log2matchLengthSum;
  208. U32 log2matchSum;
  209. U32 log2litLengthSum;
  210. U32 log2litSum;
  211. U32 log2offCodeSum;
  212. U32 factor;
  213. U32 cachedPrice;
  214. U32 cachedLitLength;
  215. const BYTE* cachedLiterals;
  216. ZSTD_stats_t stats;
  217. } seqStore_t;
  218. const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);
  219. void ZSTD_seqToCodes(const seqStore_t* seqStorePtr, size_t const nbSeq);
  220. #endif /* ZSTD_CCOMMON_H_MODULE */