comparison contrib/python-zstandard/zstd/common/huf.h @ 40121:73fef626dae3

zstandard: vendor python-zstandard 0.10.1 This was just released. The upstream source distribution from PyPI was extracted. Unwanted files were removed. The clang-format ignore list was updated to reflect the new source of files. setup.py was updated to pass a new argument to python-zstandard's function for returning an Extension instance. Upstream had to change to use relative paths because Python 3.7's packaging doesn't seem to like absolute paths when defining sources, includes, etc. The default relative path calculation is relative to setup_zstd.py which is different from the directory of Mercurial's setup.py. The project contains a vendored copy of zstandard 1.3.6. The old version was 1.3.4. The API should be backwards compatible and nothing in core should need adjusted. However, there is a new "chunker" API that we may find useful in places where we want to emit compressed chunks of a fixed size. There are a pair of bug fixes in 0.10.0 with regards to compressobj() and decompressobj() when block flushing is used. I actually found these bugs when introducing these APIs in Mercurial! But existing Mercurial code is not affected because we don't perform block flushing. # no-check-commit because 3rd party code has different style guidelines Differential Revision: https://phab.mercurial-scm.org/D4911
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 08 Oct 2018 16:27:40 -0700
parents b1fb341d8a61
children 675775c33ab6
comparison
equal deleted inserted replaced
40120:89742f1fa6cb 40121:73fef626dae3
1 /* ****************************************************************** 1 /* ******************************************************************
2 Huffman coder, part of New Generation Entropy library 2 huff0 huffman codec,
3 header file 3 part of Finite State Entropy library
4 Copyright (C) 2013-2016, Yann Collet. 4 Copyright (C) 2013-present, Yann Collet.
5 5
6 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 6 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7 7
8 Redistribution and use in source and binary forms, with or without 8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are 9 modification, are permitted provided that the following conditions are
161 HUF_CElt* name = (HUF_CElt*)(name##hv) /* no final ; */ 161 HUF_CElt* name = (HUF_CElt*)(name##hv) /* no final ; */
162 162
163 /* static allocation of HUF's DTable */ 163 /* static allocation of HUF's DTable */
164 typedef U32 HUF_DTable; 164 typedef U32 HUF_DTable;
165 #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<(maxTableLog))) 165 #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<(maxTableLog)))
166 #define HUF_CREATE_STATIC_DTABLEX1(DTable, maxTableLog) \
167 HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) }
166 #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \ 168 #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
167 HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) }
168 #define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \
169 HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) } 169 HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) }
170 170
171 171
172 /* **************************************** 172 /* ****************************************
173 * Advanced decompression functions 173 * Advanced decompression functions
174 ******************************************/ 174 ******************************************/
175 size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */ 175 size_t HUF_decompress4X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
176 size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */ 176 size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
177 177
178 size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< decodes RLE and uncompressed */ 178 size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< decodes RLE and uncompressed */
179 size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< considers RLE and uncompressed as errors */ 179 size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< considers RLE and uncompressed as errors */
180 size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< considers RLE and uncompressed as errors */ 180 size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< considers RLE and uncompressed as errors */
181 size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */ 181 size_t HUF_decompress4X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
182 size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */ 182 size_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */
183 size_t HUF_decompress4X4_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */ 183 size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
184 size_t HUF_decompress4X4_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */ 184 size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */
185 185
186 186
187 /* **************************************** 187 /* ****************************************
188 * HUF detailed API 188 * HUF detailed API
189 * ****************************************/ 189 * ****************************************/
206 size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable); 206 size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
207 207
208 typedef enum { 208 typedef enum {
209 HUF_repeat_none, /**< Cannot use the previous table */ 209 HUF_repeat_none, /**< Cannot use the previous table */
210 HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1, 4}X_repeat */ 210 HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1, 4}X_repeat */
211 HUF_repeat_valid /**< Can use the previous table and it is asumed to be valid */ 211 HUF_repeat_valid /**< Can use the previous table and it is assumed to be valid */
212 } HUF_repeat; 212 } HUF_repeat;
213 /** HUF_compress4X_repeat() : 213 /** HUF_compress4X_repeat() :
214 * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none. 214 * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
215 * If it uses hufTable it does not modify hufTable or repeat. 215 * If it uses hufTable it does not modify hufTable or repeat.
216 * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used. 216 * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
225 * Same as HUF_buildCTable(), but using externally allocated scratch buffer. 225 * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
226 * `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= HUF_CTABLE_WORKSPACE_SIZE. 226 * `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= HUF_CTABLE_WORKSPACE_SIZE.
227 */ 227 */
228 #define HUF_CTABLE_WORKSPACE_SIZE_U32 (2*HUF_SYMBOLVALUE_MAX +1 +1) 228 #define HUF_CTABLE_WORKSPACE_SIZE_U32 (2*HUF_SYMBOLVALUE_MAX +1 +1)
229 #define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned)) 229 #define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned))
230 size_t HUF_buildCTable_wksp (HUF_CElt* tree, const U32* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize); 230 size_t HUF_buildCTable_wksp (HUF_CElt* tree,
231 const U32* count, U32 maxSymbolValue, U32 maxNbBits,
232 void* workSpace, size_t wkspSize);
231 233
232 /*! HUF_readStats() : 234 /*! HUF_readStats() :
233 * Read compact Huffman tree, saved by HUF_writeCTable(). 235 * Read compact Huffman tree, saved by HUF_writeCTable().
234 * `huffWeight` is destination buffer. 236 * `huffWeight` is destination buffer.
235 * @return : size read from `src` , or an error Code . 237 * @return : size read from `src` , or an error Code .
240 242
241 /** HUF_readCTable() : 243 /** HUF_readCTable() :
242 * Loading a CTable saved with HUF_writeCTable() */ 244 * Loading a CTable saved with HUF_writeCTable() */
243 size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize); 245 size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize);
244 246
247 /** HUF_getNbBits() :
248 * Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX
249 * Note 1 : is not inlined, as HUF_CElt definition is private
250 * Note 2 : const void* used, so that it can provide a statically allocated table as argument (which uses type U32) */
251 U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue);
245 252
246 /* 253 /*
247 * HUF_decompress() does the following: 254 * HUF_decompress() does the following:
248 * 1. select the decompression algorithm (X2, X4) based on pre-computed heuristics 255 * 1. select the decompression algorithm (X1, X2) based on pre-computed heuristics
249 * 2. build Huffman table from save, using HUF_readDTableX?() 256 * 2. build Huffman table from save, using HUF_readDTableX?()
250 * 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable() 257 * 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable()
251 */ 258 */
252 259
253 /** HUF_selectDecoder() : 260 /** HUF_selectDecoder() :
254 * Tells which decoder is likely to decode faster, 261 * Tells which decoder is likely to decode faster,
255 * based on a set of pre-computed metrics. 262 * based on a set of pre-computed metrics.
256 * @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 . 263 * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 .
257 * Assumption : 0 < dstSize <= 128 KB */ 264 * Assumption : 0 < dstSize <= 128 KB */
258 U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize); 265 U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);
259 266
260 /** 267 /**
261 * The minimum workspace size for the `workSpace` used in 268 * The minimum workspace size for the `workSpace` used in
262 * HUF_readDTableX2_wksp() and HUF_readDTableX4_wksp(). 269 * HUF_readDTableX1_wksp() and HUF_readDTableX2_wksp().
263 * 270 *
264 * The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when 271 * The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when
265 * HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15. 272 * HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15.
266 * Buffer overflow errors may potentially occur if code modifications result in 273 * Buffer overflow errors may potentially occur if code modifications result in
267 * a required workspace size greater than that specified in the following 274 * a required workspace size greater than that specified in the following
268 * macro. 275 * macro.
269 */ 276 */
270 #define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10) 277 #define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10)
271 #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32)) 278 #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
272 279
280 size_t HUF_readDTableX1 (HUF_DTable* DTable, const void* src, size_t srcSize);
281 size_t HUF_readDTableX1_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
273 size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize); 282 size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize);
274 size_t HUF_readDTableX2_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize); 283 size_t HUF_readDTableX2_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
275 size_t HUF_readDTableX4 (HUF_DTable* DTable, const void* src, size_t srcSize);
276 size_t HUF_readDTableX4_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
277 284
278 size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); 285 size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
286 size_t HUF_decompress4X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
279 size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); 287 size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
280 size_t HUF_decompress4X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
281 288
282 289
283 /* ====================== */ 290 /* ====================== */
284 /* single stream variants */ 291 /* single stream variants */
285 /* ====================== */ 292 /* ====================== */
296 const void* src, size_t srcSize, 303 const void* src, size_t srcSize,
297 unsigned maxSymbolValue, unsigned tableLog, 304 unsigned maxSymbolValue, unsigned tableLog,
298 void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */ 305 void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
299 HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2); 306 HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
300 307
301 size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */ 308 size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
302 size_t HUF_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */ 309 size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */
303 310
304 size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); 311 size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
305 size_t HUF_decompress1X_DCtx_wksp (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); 312 size_t HUF_decompress1X_DCtx_wksp (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);
306 size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */ 313 size_t HUF_decompress1X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
307 size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */ 314 size_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */
308 size_t HUF_decompress1X4_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */ 315 size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
309 size_t HUF_decompress1X4_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */ 316 size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */
310 317
311 size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); /**< automatic selection of sing or double symbol decoder, based on DTable */ 318 size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); /**< automatic selection of sing or double symbol decoder, based on DTable */
319 size_t HUF_decompress1X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
312 size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); 320 size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
313 size_t HUF_decompress1X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
314 321
315 /* BMI2 variants. 322 /* BMI2 variants.
316 * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0. 323 * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.
317 */ 324 */
318 size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2); 325 size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
319 size_t HUF_decompress1X2_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2); 326 size_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);
320 size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2); 327 size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
321 size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2); 328 size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);
322 329
323 #endif /* HUF_STATIC_LINKING_ONLY */ 330 #endif /* HUF_STATIC_LINKING_ONLY */
324 331