zstd_internal.h 11 KB

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