zstd_internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. #ifndef ZSTD_CCOMMON_H_MODULE
  11. #define ZSTD_CCOMMON_H_MODULE
  12. /* this module contains definitions which must be identical
  13. * across compression, decompression and dictBuilder.
  14. * It also contains a few functions useful to at least 2 of them
  15. * and which benefit from being inlined */
  16. /*-*************************************
  17. * Dependencies
  18. ***************************************/
  19. #include "compiler.h"
  20. #include "cpu.h"
  21. #include "mem.h"
  22. #include "debug.h" /* assert, DEBUGLOG, RAWLOG, g_debuglevel */
  23. #include "error_private.h"
  24. #define ZSTD_STATIC_LINKING_ONLY
  25. #include "../zstd.h"
  26. #define FSE_STATIC_LINKING_ONLY
  27. #include "fse.h"
  28. #include "huf.h"
  29. #ifndef XXH_STATIC_LINKING_ONLY
  30. # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
  31. #endif
  32. #include <contrib/libs/xxhash/xxhash.h> /* XXH_reset, update, digest */
  33. #ifndef ZSTD_NO_TRACE
  34. # include "zstd_trace.h"
  35. #else
  36. # define ZSTD_TRACE 0
  37. #endif
  38. /* ---- static assert (debug) --- */
  39. #define ZSTD_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)
  40. #define ZSTD_isError ERR_isError /* for inlining */
  41. #define FSE_isError ERR_isError
  42. #define HUF_isError ERR_isError
  43. /*-*************************************
  44. * shared macros
  45. ***************************************/
  46. #undef MIN
  47. #undef MAX
  48. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  49. #define MAX(a,b) ((a)>(b) ? (a) : (b))
  50. #define BOUNDED(min,val,max) (MAX(min,MIN(val,max)))
  51. /*-*************************************
  52. * Common constants
  53. ***************************************/
  54. #define ZSTD_OPT_NUM (1<<12)
  55. #define ZSTD_REP_NUM 3 /* number of repcodes */
  56. static UNUSED_ATTR const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
  57. #define KB *(1 <<10)
  58. #define MB *(1 <<20)
  59. #define GB *(1U<<30)
  60. #define BIT7 128
  61. #define BIT6 64
  62. #define BIT5 32
  63. #define BIT4 16
  64. #define BIT1 2
  65. #define BIT0 1
  66. #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
  67. static UNUSED_ATTR const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
  68. static UNUSED_ATTR const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
  69. #define ZSTD_FRAMEIDSIZE 4 /* magic number size */
  70. #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
  71. static UNUSED_ATTR const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
  72. typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
  73. #define ZSTD_FRAMECHECKSUMSIZE 4
  74. #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
  75. #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */) /* for a non-null block */
  76. #define MIN_LITERALS_FOR_4_STREAMS 6
  77. typedef enum { set_basic, set_rle, set_compressed, set_repeat } SymbolEncodingType_e;
  78. #define LONGNBSEQ 0x7F00
  79. #define MINMATCH 3
  80. #define Litbits 8
  81. #define LitHufLog 11
  82. #define MaxLit ((1<<Litbits) - 1)
  83. #define MaxML 52
  84. #define MaxLL 35
  85. #define DefaultMaxOff 28
  86. #define MaxOff 31
  87. #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
  88. #define MLFSELog 9
  89. #define LLFSELog 9
  90. #define OffFSELog 8
  91. #define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog)
  92. #define MaxMLBits 16
  93. #define MaxLLBits 16
  94. #define ZSTD_MAX_HUF_HEADER_SIZE 128 /* header + <= 127 byte tree description */
  95. /* Each table cannot take more than #symbols * FSELog bits */
  96. #define ZSTD_MAX_FSE_HEADERS_SIZE (((MaxML + 1) * MLFSELog + (MaxLL + 1) * LLFSELog + (MaxOff + 1) * OffFSELog + 7) / 8)
  97. static UNUSED_ATTR const U8 LL_bits[MaxLL+1] = {
  98. 0, 0, 0, 0, 0, 0, 0, 0,
  99. 0, 0, 0, 0, 0, 0, 0, 0,
  100. 1, 1, 1, 1, 2, 2, 3, 3,
  101. 4, 6, 7, 8, 9,10,11,12,
  102. 13,14,15,16
  103. };
  104. static UNUSED_ATTR const S16 LL_defaultNorm[MaxLL+1] = {
  105. 4, 3, 2, 2, 2, 2, 2, 2,
  106. 2, 2, 2, 2, 2, 1, 1, 1,
  107. 2, 2, 2, 2, 2, 2, 2, 2,
  108. 2, 3, 2, 1, 1, 1, 1, 1,
  109. -1,-1,-1,-1
  110. };
  111. #define LL_DEFAULTNORMLOG 6 /* for static allocation */
  112. static UNUSED_ATTR const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
  113. static UNUSED_ATTR const U8 ML_bits[MaxML+1] = {
  114. 0, 0, 0, 0, 0, 0, 0, 0,
  115. 0, 0, 0, 0, 0, 0, 0, 0,
  116. 0, 0, 0, 0, 0, 0, 0, 0,
  117. 0, 0, 0, 0, 0, 0, 0, 0,
  118. 1, 1, 1, 1, 2, 2, 3, 3,
  119. 4, 4, 5, 7, 8, 9,10,11,
  120. 12,13,14,15,16
  121. };
  122. static UNUSED_ATTR const S16 ML_defaultNorm[MaxML+1] = {
  123. 1, 4, 3, 2, 2, 2, 2, 2,
  124. 2, 1, 1, 1, 1, 1, 1, 1,
  125. 1, 1, 1, 1, 1, 1, 1, 1,
  126. 1, 1, 1, 1, 1, 1, 1, 1,
  127. 1, 1, 1, 1, 1, 1, 1, 1,
  128. 1, 1, 1, 1, 1, 1,-1,-1,
  129. -1,-1,-1,-1,-1
  130. };
  131. #define ML_DEFAULTNORMLOG 6 /* for static allocation */
  132. static UNUSED_ATTR const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
  133. static UNUSED_ATTR const S16 OF_defaultNorm[DefaultMaxOff+1] = {
  134. 1, 1, 1, 1, 1, 1, 2, 2,
  135. 2, 1, 1, 1, 1, 1, 1, 1,
  136. 1, 1, 1, 1, 1, 1, 1, 1,
  137. -1,-1,-1,-1,-1
  138. };
  139. #define OF_DEFAULTNORMLOG 5 /* for static allocation */
  140. static UNUSED_ATTR const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
  141. /*-*******************************************
  142. * Shared functions to include for inlining
  143. *********************************************/
  144. static void ZSTD_copy8(void* dst, const void* src) {
  145. #if defined(ZSTD_ARCH_ARM_NEON)
  146. vst1_u8((uint8_t*)dst, vld1_u8((const uint8_t*)src));
  147. #else
  148. ZSTD_memcpy(dst, src, 8);
  149. #endif
  150. }
  151. #define COPY8(d,s) do { ZSTD_copy8(d,s); d+=8; s+=8; } while (0)
  152. /* Need to use memmove here since the literal buffer can now be located within
  153. the dst buffer. In circumstances where the op "catches up" to where the
  154. literal buffer is, there can be partial overlaps in this call on the final
  155. copy if the literal is being shifted by less than 16 bytes. */
  156. static void ZSTD_copy16(void* dst, const void* src) {
  157. #if defined(ZSTD_ARCH_ARM_NEON)
  158. vst1q_u8((uint8_t*)dst, vld1q_u8((const uint8_t*)src));
  159. #elif defined(ZSTD_ARCH_X86_SSE2)
  160. _mm_storeu_si128((__m128i*)dst, _mm_loadu_si128((const __m128i*)src));
  161. #elif defined(__clang__)
  162. ZSTD_memmove(dst, src, 16);
  163. #else
  164. /* ZSTD_memmove is not inlined properly by gcc */
  165. BYTE copy16_buf[16];
  166. ZSTD_memcpy(copy16_buf, src, 16);
  167. ZSTD_memcpy(dst, copy16_buf, 16);
  168. #endif
  169. }
  170. #define COPY16(d,s) do { ZSTD_copy16(d,s); d+=16; s+=16; } while (0)
  171. #define WILDCOPY_OVERLENGTH 32
  172. #define WILDCOPY_VECLEN 16
  173. typedef enum {
  174. ZSTD_no_overlap,
  175. ZSTD_overlap_src_before_dst
  176. /* ZSTD_overlap_dst_before_src, */
  177. } ZSTD_overlap_e;
  178. /*! ZSTD_wildcopy() :
  179. * Custom version of ZSTD_memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)
  180. * @param ovtype controls the overlap detection
  181. * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.
  182. * - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart.
  183. * The src buffer must be before the dst buffer.
  184. */
  185. MEM_STATIC FORCE_INLINE_ATTR
  186. void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e const ovtype)
  187. {
  188. ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;
  189. const BYTE* ip = (const BYTE*)src;
  190. BYTE* op = (BYTE*)dst;
  191. BYTE* const oend = op + length;
  192. if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) {
  193. /* Handle short offset copies. */
  194. do {
  195. COPY8(op, ip);
  196. } while (op < oend);
  197. } else {
  198. assert(diff >= WILDCOPY_VECLEN || diff <= -WILDCOPY_VECLEN);
  199. /* Separate out the first COPY16() call because the copy length is
  200. * almost certain to be short, so the branches have different
  201. * probabilities. Since it is almost certain to be short, only do
  202. * one COPY16() in the first call. Then, do two calls per loop since
  203. * at that point it is more likely to have a high trip count.
  204. */
  205. ZSTD_copy16(op, ip);
  206. if (16 >= length) return;
  207. op += 16;
  208. ip += 16;
  209. do {
  210. COPY16(op, ip);
  211. COPY16(op, ip);
  212. }
  213. while (op < oend);
  214. }
  215. }
  216. MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  217. {
  218. size_t const length = MIN(dstCapacity, srcSize);
  219. if (length > 0) {
  220. ZSTD_memcpy(dst, src, length);
  221. }
  222. return length;
  223. }
  224. /* define "workspace is too large" as this number of times larger than needed */
  225. #define ZSTD_WORKSPACETOOLARGE_FACTOR 3
  226. /* when workspace is continuously too large
  227. * during at least this number of times,
  228. * context's memory usage is considered wasteful,
  229. * because it's sized to handle a worst case scenario which rarely happens.
  230. * In which case, resize it down to free some memory */
  231. #define ZSTD_WORKSPACETOOLARGE_MAXDURATION 128
  232. /* Controls whether the input/output buffer is buffered or stable. */
  233. typedef enum {
  234. ZSTD_bm_buffered = 0, /* Buffer the input/output */
  235. ZSTD_bm_stable = 1 /* ZSTD_inBuffer/ZSTD_outBuffer is stable */
  236. } ZSTD_bufferMode_e;
  237. /*-*******************************************
  238. * Private declarations
  239. *********************************************/
  240. /**
  241. * Contains the compressed frame size and an upper-bound for the decompressed frame size.
  242. * Note: before using `compressedSize`, check for errors using ZSTD_isError().
  243. * similarly, before using `decompressedBound`, check for errors using:
  244. * `decompressedBound != ZSTD_CONTENTSIZE_ERROR`
  245. */
  246. typedef struct {
  247. size_t nbBlocks;
  248. size_t compressedSize;
  249. unsigned long long decompressedBound;
  250. } ZSTD_frameSizeInfo; /* decompress & legacy */
  251. /* ZSTD_invalidateRepCodes() :
  252. * ensures next compression will not use repcodes from previous block.
  253. * Note : only works with regular variant;
  254. * do not use with extDict variant ! */
  255. void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); /* zstdmt, adaptive_compression (shouldn't get this definition from here) */
  256. typedef struct {
  257. blockType_e blockType;
  258. U32 lastBlock;
  259. U32 origSize;
  260. } blockProperties_t; /* declared here for decompress and fullbench */
  261. /*! ZSTD_getcBlockSize() :
  262. * Provides the size of compressed block from block header `src` */
  263. /* Used by: decompress, fullbench */
  264. size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
  265. blockProperties_t* bpPtr);
  266. /*! ZSTD_decodeSeqHeaders() :
  267. * decode sequence header from src */
  268. /* Used by: zstd_decompress_block, fullbench */
  269. size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
  270. const void* src, size_t srcSize);
  271. /**
  272. * @returns true iff the CPU supports dynamic BMI2 dispatch.
  273. */
  274. MEM_STATIC int ZSTD_cpuSupportsBmi2(void)
  275. {
  276. ZSTD_cpuid_t cpuid = ZSTD_cpuid();
  277. return ZSTD_cpuid_bmi1(cpuid) && ZSTD_cpuid_bmi2(cpuid);
  278. }
  279. #endif /* ZSTD_CCOMMON_H_MODULE */