Path: blob/master/Utilities/cmzstd/lib/common/zstd_internal.h
4998 views
/*1* Copyright (c) Meta Platforms, Inc. and affiliates.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910#ifndef ZSTD_CCOMMON_H_MODULE11#define ZSTD_CCOMMON_H_MODULE1213/* this module contains definitions which must be identical14* across compression, decompression and dictBuilder.15* It also contains a few functions useful to at least 2 of them16* and which benefit from being inlined */1718/*-*************************************19* Dependencies20***************************************/21#include "compiler.h"22#include "cpu.h"23#include "mem.h"24#include "debug.h" /* assert, DEBUGLOG, RAWLOG, g_debuglevel */25#include "error_private.h"26#define ZSTD_STATIC_LINKING_ONLY27#include "../zstd.h"28#define FSE_STATIC_LINKING_ONLY29#include "fse.h"30#include "huf.h"31#ifndef XXH_STATIC_LINKING_ONLY32# define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */33#endif34#include "xxhash.h" /* XXH_reset, update, digest */35#ifndef ZSTD_NO_TRACE36# include "zstd_trace.h"37#else38# define ZSTD_TRACE 039#endif4041/* ---- static assert (debug) --- */42#define ZSTD_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)43#define ZSTD_isError ERR_isError /* for inlining */44#define FSE_isError ERR_isError45#define HUF_isError ERR_isError464748/*-*************************************49* shared macros50***************************************/51#undef MIN52#undef MAX53#define MIN(a,b) ((a)<(b) ? (a) : (b))54#define MAX(a,b) ((a)>(b) ? (a) : (b))55#define BOUNDED(min,val,max) (MAX(min,MIN(val,max)))565758/*-*************************************59* Common constants60***************************************/61#define ZSTD_OPT_NUM (1<<12)6263#define ZSTD_REP_NUM 3 /* number of repcodes */64static UNUSED_ATTR const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };6566#define KB *(1 <<10)67#define MB *(1 <<20)68#define GB *(1U<<30)6970#define BIT7 12871#define BIT6 6472#define BIT5 3273#define BIT4 1674#define BIT1 275#define BIT0 17677#define ZSTD_WINDOWLOG_ABSOLUTEMIN 1078static UNUSED_ATTR const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };79static UNUSED_ATTR const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };8081#define ZSTD_FRAMEIDSIZE 4 /* magic number size */8283#define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */84static UNUSED_ATTR const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;85typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;8687#define ZSTD_FRAMECHECKSUMSIZE 48889#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */90#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */) /* for a non-null block */91#define MIN_LITERALS_FOR_4_STREAMS 69293typedef enum { set_basic, set_rle, set_compressed, set_repeat } SymbolEncodingType_e;9495#define LONGNBSEQ 0x7F009697#define MINMATCH 39899#define Litbits 8100#define LitHufLog 11101#define MaxLit ((1<<Litbits) - 1)102#define MaxML 52103#define MaxLL 35104#define DefaultMaxOff 28105#define MaxOff 31106#define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */107#define MLFSELog 9108#define LLFSELog 9109#define OffFSELog 8110#define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog)111#define MaxMLBits 16112#define MaxLLBits 16113114#define ZSTD_MAX_HUF_HEADER_SIZE 128 /* header + <= 127 byte tree description */115/* Each table cannot take more than #symbols * FSELog bits */116#define ZSTD_MAX_FSE_HEADERS_SIZE (((MaxML + 1) * MLFSELog + (MaxLL + 1) * LLFSELog + (MaxOff + 1) * OffFSELog + 7) / 8)117118static UNUSED_ATTR const U8 LL_bits[MaxLL+1] = {1190, 0, 0, 0, 0, 0, 0, 0,1200, 0, 0, 0, 0, 0, 0, 0,1211, 1, 1, 1, 2, 2, 3, 3,1224, 6, 7, 8, 9,10,11,12,12313,14,15,16124};125static UNUSED_ATTR const S16 LL_defaultNorm[MaxLL+1] = {1264, 3, 2, 2, 2, 2, 2, 2,1272, 2, 2, 2, 2, 1, 1, 1,1282, 2, 2, 2, 2, 2, 2, 2,1292, 3, 2, 1, 1, 1, 1, 1,130-1,-1,-1,-1131};132#define LL_DEFAULTNORMLOG 6 /* for static allocation */133static UNUSED_ATTR const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;134135static UNUSED_ATTR const U8 ML_bits[MaxML+1] = {1360, 0, 0, 0, 0, 0, 0, 0,1370, 0, 0, 0, 0, 0, 0, 0,1380, 0, 0, 0, 0, 0, 0, 0,1390, 0, 0, 0, 0, 0, 0, 0,1401, 1, 1, 1, 2, 2, 3, 3,1414, 4, 5, 7, 8, 9,10,11,14212,13,14,15,16143};144static UNUSED_ATTR const S16 ML_defaultNorm[MaxML+1] = {1451, 4, 3, 2, 2, 2, 2, 2,1462, 1, 1, 1, 1, 1, 1, 1,1471, 1, 1, 1, 1, 1, 1, 1,1481, 1, 1, 1, 1, 1, 1, 1,1491, 1, 1, 1, 1, 1, 1, 1,1501, 1, 1, 1, 1, 1,-1,-1,151-1,-1,-1,-1,-1152};153#define ML_DEFAULTNORMLOG 6 /* for static allocation */154static UNUSED_ATTR const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;155156static UNUSED_ATTR const S16 OF_defaultNorm[DefaultMaxOff+1] = {1571, 1, 1, 1, 1, 1, 2, 2,1582, 1, 1, 1, 1, 1, 1, 1,1591, 1, 1, 1, 1, 1, 1, 1,160-1,-1,-1,-1,-1161};162#define OF_DEFAULTNORMLOG 5 /* for static allocation */163static UNUSED_ATTR const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;164165166/*-*******************************************167* Shared functions to include for inlining168*********************************************/169static void ZSTD_copy8(void* dst, const void* src) {170#if defined(ZSTD_ARCH_ARM_NEON)171vst1_u8((uint8_t*)dst, vld1_u8((const uint8_t*)src));172#else173ZSTD_memcpy(dst, src, 8);174#endif175}176#define COPY8(d,s) do { ZSTD_copy8(d,s); d+=8; s+=8; } while (0)177178/* Need to use memmove here since the literal buffer can now be located within179the dst buffer. In circumstances where the op "catches up" to where the180literal buffer is, there can be partial overlaps in this call on the final181copy if the literal is being shifted by less than 16 bytes. */182static void ZSTD_copy16(void* dst, const void* src) {183#if defined(ZSTD_ARCH_ARM_NEON)184vst1q_u8((uint8_t*)dst, vld1q_u8((const uint8_t*)src));185#elif defined(ZSTD_ARCH_X86_SSE2)186_mm_storeu_si128((__m128i*)dst, _mm_loadu_si128((const __m128i*)src));187#elif defined(__clang__)188ZSTD_memmove(dst, src, 16);189#else190/* ZSTD_memmove is not inlined properly by gcc */191BYTE copy16_buf[16];192ZSTD_memcpy(copy16_buf, src, 16);193ZSTD_memcpy(dst, copy16_buf, 16);194#endif195}196#define COPY16(d,s) do { ZSTD_copy16(d,s); d+=16; s+=16; } while (0)197198#define WILDCOPY_OVERLENGTH 32199#define WILDCOPY_VECLEN 16200201typedef enum {202ZSTD_no_overlap,203ZSTD_overlap_src_before_dst204/* ZSTD_overlap_dst_before_src, */205} ZSTD_overlap_e;206207/*! ZSTD_wildcopy() :208* Custom version of ZSTD_memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)209* @param ovtype controls the overlap detection210* - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.211* - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart.212* The src buffer must be before the dst buffer.213*/214MEM_STATIC FORCE_INLINE_ATTR215void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e const ovtype)216{217ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;218const BYTE* ip = (const BYTE*)src;219BYTE* op = (BYTE*)dst;220BYTE* const oend = op + length;221222if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) {223/* Handle short offset copies. */224do {225COPY8(op, ip);226} while (op < oend);227} else {228assert(diff >= WILDCOPY_VECLEN || diff <= -WILDCOPY_VECLEN);229/* Separate out the first COPY16() call because the copy length is230* almost certain to be short, so the branches have different231* probabilities. Since it is almost certain to be short, only do232* one COPY16() in the first call. Then, do two calls per loop since233* at that point it is more likely to have a high trip count.234*/235ZSTD_copy16(op, ip);236if (16 >= length) return;237op += 16;238ip += 16;239do {240COPY16(op, ip);241COPY16(op, ip);242}243while (op < oend);244}245}246247MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)248{249size_t const length = MIN(dstCapacity, srcSize);250if (length > 0) {251ZSTD_memcpy(dst, src, length);252}253return length;254}255256/* define "workspace is too large" as this number of times larger than needed */257#define ZSTD_WORKSPACETOOLARGE_FACTOR 3258259/* when workspace is continuously too large260* during at least this number of times,261* context's memory usage is considered wasteful,262* because it's sized to handle a worst case scenario which rarely happens.263* In which case, resize it down to free some memory */264#define ZSTD_WORKSPACETOOLARGE_MAXDURATION 128265266/* Controls whether the input/output buffer is buffered or stable. */267typedef enum {268ZSTD_bm_buffered = 0, /* Buffer the input/output */269ZSTD_bm_stable = 1 /* ZSTD_inBuffer/ZSTD_outBuffer is stable */270} ZSTD_bufferMode_e;271272273/*-*******************************************274* Private declarations275*********************************************/276277/**278* Contains the compressed frame size and an upper-bound for the decompressed frame size.279* Note: before using `compressedSize`, check for errors using ZSTD_isError().280* similarly, before using `decompressedBound`, check for errors using:281* `decompressedBound != ZSTD_CONTENTSIZE_ERROR`282*/283typedef struct {284size_t nbBlocks;285size_t compressedSize;286unsigned long long decompressedBound;287} ZSTD_frameSizeInfo; /* decompress & legacy */288289/* ZSTD_invalidateRepCodes() :290* ensures next compression will not use repcodes from previous block.291* Note : only works with regular variant;292* do not use with extDict variant ! */293void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); /* zstdmt, adaptive_compression (shouldn't get this definition from here) */294295296typedef struct {297blockType_e blockType;298U32 lastBlock;299U32 origSize;300} blockProperties_t; /* declared here for decompress and fullbench */301302/*! ZSTD_getcBlockSize() :303* Provides the size of compressed block from block header `src` */304/* Used by: decompress, fullbench */305size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,306blockProperties_t* bpPtr);307308/*! ZSTD_decodeSeqHeaders() :309* decode sequence header from src */310/* Used by: zstd_decompress_block, fullbench */311size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,312const void* src, size_t srcSize);313314/**315* @returns true iff the CPU supports dynamic BMI2 dispatch.316*/317MEM_STATIC int ZSTD_cpuSupportsBmi2(void)318{319ZSTD_cpuid_t cpuid = ZSTD_cpuid();320return ZSTD_cpuid_bmi1(cpuid) && ZSTD_cpuid_bmi2(cpuid);321}322323#endif /* ZSTD_CCOMMON_H_MODULE */324325326