extzstd 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/contrib/zstd/CHANGELOG +188 -1
  4. data/contrib/zstd/CONTRIBUTING.md +157 -74
  5. data/contrib/zstd/LICENSE +4 -4
  6. data/contrib/zstd/Makefile +81 -58
  7. data/contrib/zstd/Package.swift +36 -0
  8. data/contrib/zstd/README.md +59 -35
  9. data/contrib/zstd/TESTING.md +2 -3
  10. data/contrib/zstd/appveyor.yml +49 -136
  11. data/contrib/zstd/lib/BUCK +5 -7
  12. data/contrib/zstd/lib/Makefile +87 -181
  13. data/contrib/zstd/lib/README.md +23 -6
  14. data/contrib/zstd/lib/common/allocations.h +55 -0
  15. data/contrib/zstd/lib/common/bits.h +200 -0
  16. data/contrib/zstd/lib/common/bitstream.h +33 -59
  17. data/contrib/zstd/lib/common/compiler.h +115 -45
  18. data/contrib/zstd/lib/common/cpu.h +1 -1
  19. data/contrib/zstd/lib/common/debug.c +1 -1
  20. data/contrib/zstd/lib/common/debug.h +1 -1
  21. data/contrib/zstd/lib/common/entropy_common.c +15 -37
  22. data/contrib/zstd/lib/common/error_private.c +9 -2
  23. data/contrib/zstd/lib/common/error_private.h +82 -3
  24. data/contrib/zstd/lib/common/fse.h +9 -85
  25. data/contrib/zstd/lib/common/fse_decompress.c +29 -111
  26. data/contrib/zstd/lib/common/huf.h +84 -172
  27. data/contrib/zstd/lib/common/mem.h +58 -49
  28. data/contrib/zstd/lib/common/pool.c +37 -16
  29. data/contrib/zstd/lib/common/pool.h +9 -3
  30. data/contrib/zstd/lib/common/portability_macros.h +156 -0
  31. data/contrib/zstd/lib/common/threading.c +68 -14
  32. data/contrib/zstd/lib/common/threading.h +5 -10
  33. data/contrib/zstd/lib/common/xxhash.c +7 -809
  34. data/contrib/zstd/lib/common/xxhash.h +5568 -167
  35. data/contrib/zstd/lib/common/zstd_common.c +1 -36
  36. data/contrib/zstd/lib/common/zstd_deps.h +1 -1
  37. data/contrib/zstd/lib/common/zstd_internal.h +64 -150
  38. data/contrib/zstd/lib/common/zstd_trace.h +163 -0
  39. data/contrib/zstd/lib/compress/clevels.h +134 -0
  40. data/contrib/zstd/lib/compress/fse_compress.c +69 -150
  41. data/contrib/zstd/lib/compress/hist.c +1 -1
  42. data/contrib/zstd/lib/compress/hist.h +1 -1
  43. data/contrib/zstd/lib/compress/huf_compress.c +773 -251
  44. data/contrib/zstd/lib/compress/zstd_compress.c +2650 -826
  45. data/contrib/zstd/lib/compress/zstd_compress_internal.h +509 -180
  46. data/contrib/zstd/lib/compress/zstd_compress_literals.c +117 -40
  47. data/contrib/zstd/lib/compress/zstd_compress_literals.h +16 -6
  48. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +28 -19
  49. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +1 -1
  50. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +33 -305
  51. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +1 -1
  52. data/contrib/zstd/lib/compress/zstd_cwksp.h +266 -85
  53. data/contrib/zstd/lib/compress/zstd_double_fast.c +369 -132
  54. data/contrib/zstd/lib/compress/zstd_double_fast.h +3 -2
  55. data/contrib/zstd/lib/compress/zstd_fast.c +722 -258
  56. data/contrib/zstd/lib/compress/zstd_fast.h +3 -2
  57. data/contrib/zstd/lib/compress/zstd_lazy.c +1105 -360
  58. data/contrib/zstd/lib/compress/zstd_lazy.h +41 -1
  59. data/contrib/zstd/lib/compress/zstd_ldm.c +272 -208
  60. data/contrib/zstd/lib/compress/zstd_ldm.h +3 -2
  61. data/contrib/zstd/lib/compress/zstd_ldm_geartab.h +106 -0
  62. data/contrib/zstd/lib/compress/zstd_opt.c +324 -197
  63. data/contrib/zstd/lib/compress/zstd_opt.h +1 -1
  64. data/contrib/zstd/lib/compress/zstdmt_compress.c +109 -53
  65. data/contrib/zstd/lib/compress/zstdmt_compress.h +9 -6
  66. data/contrib/zstd/lib/decompress/huf_decompress.c +1071 -539
  67. data/contrib/zstd/lib/decompress/huf_decompress_amd64.S +576 -0
  68. data/contrib/zstd/lib/decompress/zstd_ddict.c +4 -4
  69. data/contrib/zstd/lib/decompress/zstd_ddict.h +1 -1
  70. data/contrib/zstd/lib/decompress/zstd_decompress.c +507 -82
  71. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +962 -310
  72. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +14 -3
  73. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +54 -6
  74. data/contrib/zstd/lib/deprecated/zbuff.h +1 -1
  75. data/contrib/zstd/lib/deprecated/zbuff_common.c +1 -1
  76. data/contrib/zstd/lib/deprecated/zbuff_compress.c +24 -4
  77. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +3 -1
  78. data/contrib/zstd/lib/dictBuilder/cover.c +44 -32
  79. data/contrib/zstd/lib/dictBuilder/cover.h +6 -5
  80. data/contrib/zstd/lib/dictBuilder/divsufsort.c +1 -1
  81. data/contrib/zstd/lib/dictBuilder/fastcover.c +24 -16
  82. data/contrib/zstd/lib/dictBuilder/zdict.c +88 -95
  83. data/contrib/zstd/lib/legacy/zstd_legacy.h +8 -1
  84. data/contrib/zstd/lib/legacy/zstd_v01.c +16 -53
  85. data/contrib/zstd/lib/legacy/zstd_v01.h +1 -1
  86. data/contrib/zstd/lib/legacy/zstd_v02.c +24 -69
  87. data/contrib/zstd/lib/legacy/zstd_v02.h +1 -1
  88. data/contrib/zstd/lib/legacy/zstd_v03.c +25 -72
  89. data/contrib/zstd/lib/legacy/zstd_v03.h +1 -1
  90. data/contrib/zstd/lib/legacy/zstd_v04.c +23 -69
  91. data/contrib/zstd/lib/legacy/zstd_v04.h +1 -1
  92. data/contrib/zstd/lib/legacy/zstd_v05.c +35 -85
  93. data/contrib/zstd/lib/legacy/zstd_v05.h +1 -1
  94. data/contrib/zstd/lib/legacy/zstd_v06.c +42 -87
  95. data/contrib/zstd/lib/legacy/zstd_v06.h +1 -1
  96. data/contrib/zstd/lib/legacy/zstd_v07.c +35 -82
  97. data/contrib/zstd/lib/legacy/zstd_v07.h +1 -1
  98. data/contrib/zstd/lib/libzstd.mk +214 -0
  99. data/contrib/zstd/lib/libzstd.pc.in +4 -3
  100. data/contrib/zstd/lib/module.modulemap +35 -0
  101. data/contrib/zstd/lib/{dictBuilder/zdict.h → zdict.h} +202 -33
  102. data/contrib/zstd/lib/zstd.h +922 -293
  103. data/contrib/zstd/lib/{common/zstd_errors.h → zstd_errors.h} +27 -8
  104. data/ext/extconf.rb +7 -6
  105. data/ext/extzstd.c +13 -10
  106. data/ext/libzstd_conf.h +0 -1
  107. data/ext/zstd_decompress_asm.S +1 -0
  108. metadata +16 -5
@@ -1,7 +1,7 @@
1
1
  /* ******************************************************************
2
2
  * huff0 huffman codec,
3
3
  * part of Finite State Entropy library
4
- * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
4
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
5
5
  *
6
6
  * You can contact the author at :
7
7
  * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
@@ -21,106 +21,29 @@ extern "C" {
21
21
 
22
22
  /* *** Dependencies *** */
23
23
  #include "zstd_deps.h" /* size_t */
24
-
25
-
26
- /* *** library symbols visibility *** */
27
- /* Note : when linking with -fvisibility=hidden on gcc, or by default on Visual,
28
- * HUF symbols remain "private" (internal symbols for library only).
29
- * Set macro FSE_DLL_EXPORT to 1 if you want HUF symbols visible on DLL interface */
30
- #if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)
31
- # define HUF_PUBLIC_API __attribute__ ((visibility ("default")))
32
- #elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */
33
- # define HUF_PUBLIC_API __declspec(dllexport)
34
- #elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)
35
- # define HUF_PUBLIC_API __declspec(dllimport) /* not required, just to generate faster code (saves a function pointer load from IAT and an indirect jump) */
36
- #else
37
- # define HUF_PUBLIC_API
38
- #endif
39
-
40
-
41
- /* ========================== */
42
- /* *** simple functions *** */
43
- /* ========================== */
44
-
45
- /** HUF_compress() :
46
- * Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'.
47
- * 'dst' buffer must be already allocated.
48
- * Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize).
49
- * `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB.
50
- * @return : size of compressed data (<= `dstCapacity`).
51
- * Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!
52
- * if HUF_isError(return), compression failed (more details using HUF_getErrorName())
53
- */
54
- HUF_PUBLIC_API size_t HUF_compress(void* dst, size_t dstCapacity,
55
- const void* src, size_t srcSize);
56
-
57
- /** HUF_decompress() :
58
- * Decompress HUF data from buffer 'cSrc', of size 'cSrcSize',
59
- * into already allocated buffer 'dst', of minimum size 'dstSize'.
60
- * `originalSize` : **must** be the ***exact*** size of original (uncompressed) data.
61
- * Note : in contrast with FSE, HUF_decompress can regenerate
62
- * RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,
63
- * because it knows size to regenerate (originalSize).
64
- * @return : size of regenerated data (== originalSize),
65
- * or an error code, which can be tested using HUF_isError()
66
- */
67
- HUF_PUBLIC_API size_t HUF_decompress(void* dst, size_t originalSize,
68
- const void* cSrc, size_t cSrcSize);
24
+ #include "mem.h" /* U32 */
25
+ #define FSE_STATIC_LINKING_ONLY
26
+ #include "fse.h"
69
27
 
70
28
 
71
29
  /* *** Tool functions *** */
72
- #define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */
73
- HUF_PUBLIC_API size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */
30
+ #define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */
31
+ size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */
74
32
 
75
33
  /* Error Management */
76
- HUF_PUBLIC_API unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */
77
- HUF_PUBLIC_API const char* HUF_getErrorName(size_t code); /**< provides error code string (useful for debugging) */
78
-
79
-
80
- /* *** Advanced function *** */
81
-
82
- /** HUF_compress2() :
83
- * Same as HUF_compress(), but offers control over `maxSymbolValue` and `tableLog`.
84
- * `maxSymbolValue` must be <= HUF_SYMBOLVALUE_MAX .
85
- * `tableLog` must be `<= HUF_TABLELOG_MAX` . */
86
- HUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity,
87
- const void* src, size_t srcSize,
88
- unsigned maxSymbolValue, unsigned tableLog);
89
-
90
- /** HUF_compress4X_wksp() :
91
- * Same as HUF_compress2(), but uses externally allocated `workSpace`.
92
- * `workspace` must have minimum alignment of 4, and be at least as large as HUF_WORKSPACE_SIZE */
93
- #define HUF_WORKSPACE_SIZE ((6 << 10) + 256)
94
- #define HUF_WORKSPACE_SIZE_U32 (HUF_WORKSPACE_SIZE / sizeof(U32))
95
- HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
96
- const void* src, size_t srcSize,
97
- unsigned maxSymbolValue, unsigned tableLog,
98
- void* workSpace, size_t wkspSize);
99
-
100
- #endif /* HUF_H_298734234 */
34
+ unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */
35
+ const char* HUF_getErrorName(size_t code); /**< provides error code string (useful for debugging) */
101
36
 
102
- /* ******************************************************************
103
- * WARNING !!
104
- * The following section contains advanced and experimental definitions
105
- * which shall never be used in the context of a dynamic library,
106
- * because they are not guaranteed to remain stable in the future.
107
- * Only consider them in association with static linking.
108
- * *****************************************************************/
109
- #if defined(HUF_STATIC_LINKING_ONLY) && !defined(HUF_H_HUF_STATIC_LINKING_ONLY)
110
- #define HUF_H_HUF_STATIC_LINKING_ONLY
111
-
112
- /* *** Dependencies *** */
113
- #include "mem.h" /* U32 */
114
- #define FSE_STATIC_LINKING_ONLY
115
- #include "fse.h"
116
37
 
38
+ #define HUF_WORKSPACE_SIZE ((8 << 10) + 512 /* sorting scratch space */)
39
+ #define HUF_WORKSPACE_SIZE_U64 (HUF_WORKSPACE_SIZE / sizeof(U64))
117
40
 
118
41
  /* *** Constants *** */
119
- #define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
42
+ #define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_TABLELOG_ABSOLUTEMAX */
120
43
  #define HUF_TABLELOG_DEFAULT 11 /* default tableLog value when none specified */
121
44
  #define HUF_SYMBOLVALUE_MAX 255
122
45
 
123
- #define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
46
+ #define HUF_TABLELOG_ABSOLUTEMAX 12 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
124
47
  #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
125
48
  # error "HUF_TABLELOG_MAX is too large !"
126
49
  #endif
@@ -136,15 +59,11 @@ HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
136
59
 
137
60
  /* static allocation of HUF's Compression Table */
138
61
  /* this is a private definition, just exposed for allocation and strict aliasing purpose. never EVER access its members directly */
139
- struct HUF_CElt_s {
140
- U16 val;
141
- BYTE nbBits;
142
- }; /* typedef'd to HUF_CElt */
143
- typedef struct HUF_CElt_s HUF_CElt; /* consider it an incomplete type */
144
- #define HUF_CTABLE_SIZE_U32(maxSymbolValue) ((maxSymbolValue)+1) /* Use tables of U32, for proper alignment */
145
- #define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_U32(maxSymbolValue) * sizeof(U32))
62
+ typedef size_t HUF_CElt; /* consider it an incomplete type */
63
+ #define HUF_CTABLE_SIZE_ST(maxSymbolValue) ((maxSymbolValue)+2) /* Use tables of size_t, for proper alignment */
64
+ #define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_ST(maxSymbolValue) * sizeof(size_t))
146
65
  #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
147
- HUF_CElt name[HUF_CTABLE_SIZE_U32(maxSymbolValue)] /* no final ; */
66
+ HUF_CElt name[HUF_CTABLE_SIZE_ST(maxSymbolValue)] /* no final ; */
148
67
 
149
68
  /* static allocation of HUF's DTable */
150
69
  typedef U32 HUF_DTable;
@@ -158,25 +77,49 @@ typedef U32 HUF_DTable;
158
77
  /* ****************************************
159
78
  * Advanced decompression functions
160
79
  ******************************************/
161
- size_t HUF_decompress4X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
162
- #ifndef HUF_FORCE_DECOMPRESS_X1
163
- size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
164
- #endif
165
80
 
166
- size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< decodes RLE and uncompressed */
167
- 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 */
168
- 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 */
169
- size_t HUF_decompress4X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
170
- 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 */
171
- #ifndef HUF_FORCE_DECOMPRESS_X1
172
- size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
173
- 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 */
174
- #endif
81
+ /**
82
+ * Huffman flags bitset.
83
+ * For all flags, 0 is the default value.
84
+ */
85
+ typedef enum {
86
+ /**
87
+ * If compiled with DYNAMIC_BMI2: Set flag only if the CPU supports BMI2 at runtime.
88
+ * Otherwise: Ignored.
89
+ */
90
+ HUF_flags_bmi2 = (1 << 0),
91
+ /**
92
+ * If set: Test possible table depths to find the one that produces the smallest header + encoded size.
93
+ * If unset: Use heuristic to find the table depth.
94
+ */
95
+ HUF_flags_optimalDepth = (1 << 1),
96
+ /**
97
+ * If set: If the previous table can encode the input, always reuse the previous table.
98
+ * If unset: If the previous table can encode the input, reuse the previous table if it results in a smaller output.
99
+ */
100
+ HUF_flags_preferRepeat = (1 << 2),
101
+ /**
102
+ * If set: Sample the input and check if the sample is uncompressible, if it is then don't attempt to compress.
103
+ * If unset: Always histogram the entire input.
104
+ */
105
+ HUF_flags_suspectUncompressible = (1 << 3),
106
+ /**
107
+ * If set: Don't use assembly implementations
108
+ * If unset: Allow using assembly implementations
109
+ */
110
+ HUF_flags_disableAsm = (1 << 4),
111
+ /**
112
+ * If set: Don't use the fast decoding loop, always use the fallback decoding loop.
113
+ * If unset: Use the fast decoding loop when possible.
114
+ */
115
+ HUF_flags_disableFast = (1 << 5)
116
+ } HUF_flags_e;
175
117
 
176
118
 
177
119
  /* ****************************************
178
120
  * HUF detailed API
179
121
  * ****************************************/
122
+ #define HUF_OPTIMAL_DEPTH_THRESHOLD ZSTD_btultra
180
123
 
181
124
  /*! HUF_compress() does the following:
182
125
  * 1. count symbol occurrence from source[] into table count[] using FSE_count() (exposed within "fse.h")
@@ -189,10 +132,12 @@ size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
189
132
  * For example, it's possible to compress several blocks using the same 'CTable',
190
133
  * or to save and regenerate 'CTable' using external methods.
191
134
  */
192
- unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
193
- size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits); /* @return : maxNbBits; CTable and count can overlap. In which case, CTable will overwrite count content */
194
- size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
195
- size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
135
+ unsigned HUF_minTableLog(unsigned symbolCardinality);
136
+ unsigned HUF_cardinality(const unsigned* count, unsigned maxSymbolValue);
137
+ unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, void* workSpace,
138
+ size_t wkspSize, HUF_CElt* table, const unsigned* count, int flags); /* table is used as scratch space for building and testing tables, not a return value */
139
+ size_t HUF_writeCTable_wksp(void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog, void* workspace, size_t workspaceSize);
140
+ size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int flags);
196
141
  size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
197
142
  int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
198
143
 
@@ -201,22 +146,24 @@ typedef enum {
201
146
  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 */
202
147
  HUF_repeat_valid /**< Can use the previous table and it is assumed to be valid */
203
148
  } HUF_repeat;
149
+
204
150
  /** HUF_compress4X_repeat() :
205
151
  * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
206
152
  * If it uses hufTable it does not modify hufTable or repeat.
207
153
  * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
208
- * If preferRepeat then the old table will always be used if valid. */
154
+ * If preferRepeat then the old table will always be used if valid.
155
+ * If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */
209
156
  size_t HUF_compress4X_repeat(void* dst, size_t dstSize,
210
157
  const void* src, size_t srcSize,
211
158
  unsigned maxSymbolValue, unsigned tableLog,
212
159
  void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
213
- HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
160
+ HUF_CElt* hufTable, HUF_repeat* repeat, int flags);
214
161
 
215
162
  /** HUF_buildCTable_wksp() :
216
163
  * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
217
164
  * `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= HUF_CTABLE_WORKSPACE_SIZE.
218
165
  */
219
- #define HUF_CTABLE_WORKSPACE_SIZE_U32 (2*HUF_SYMBOLVALUE_MAX +1 +1)
166
+ #define HUF_CTABLE_WORKSPACE_SIZE_U32 ((4 * (HUF_SYMBOLVALUE_MAX + 1)) + 192)
220
167
  #define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned))
221
168
  size_t HUF_buildCTable_wksp (HUF_CElt* tree,
222
169
  const unsigned* count, U32 maxSymbolValue, U32 maxNbBits,
@@ -242,17 +189,16 @@ size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize,
242
189
  U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,
243
190
  const void* src, size_t srcSize,
244
191
  void* workspace, size_t wkspSize,
245
- int bmi2);
192
+ int flags);
246
193
 
247
194
  /** HUF_readCTable() :
248
195
  * Loading a CTable saved with HUF_writeCTable() */
249
196
  size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned *hasZeroWeights);
250
197
 
251
- /** HUF_getNbBits() :
198
+ /** HUF_getNbBitsFromCTable() :
252
199
  * Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX
253
- * Note 1 : is not inlined, as HUF_CElt definition is private
254
- * Note 2 : const void* used, so that it can provide a statically allocated table as argument (which uses type U32) */
255
- U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue);
200
+ * Note 1 : is not inlined, as HUF_CElt definition is private */
201
+ U32 HUF_getNbBitsFromCTable(const HUF_CElt* symbolTable, U32 symbolValue);
256
202
 
257
203
  /*
258
204
  * HUF_decompress() does the following:
@@ -278,83 +224,49 @@ U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);
278
224
  * a required workspace size greater than that specified in the following
279
225
  * macro.
280
226
  */
281
- #define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10)
227
+ #define HUF_DECOMPRESS_WORKSPACE_SIZE ((2 << 10) + (1 << 9))
282
228
  #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
283
229
 
284
- #ifndef HUF_FORCE_DECOMPRESS_X2
285
- size_t HUF_readDTableX1 (HUF_DTable* DTable, const void* src, size_t srcSize);
286
- size_t HUF_readDTableX1_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
287
- #endif
288
- #ifndef HUF_FORCE_DECOMPRESS_X1
289
- size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize);
290
- size_t HUF_readDTableX2_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
291
- #endif
292
-
293
- size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
294
- #ifndef HUF_FORCE_DECOMPRESS_X2
295
- size_t HUF_decompress4X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
296
- #endif
297
- #ifndef HUF_FORCE_DECOMPRESS_X1
298
- size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
299
- #endif
300
-
301
230
 
302
231
  /* ====================== */
303
232
  /* single stream variants */
304
233
  /* ====================== */
305
234
 
306
- size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
307
- size_t HUF_compress1X_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */
308
- size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
235
+ size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int flags);
309
236
  /** HUF_compress1X_repeat() :
310
237
  * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
311
238
  * If it uses hufTable it does not modify hufTable or repeat.
312
239
  * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
313
- * If preferRepeat then the old table will always be used if valid. */
240
+ * If preferRepeat then the old table will always be used if valid.
241
+ * If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */
314
242
  size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
315
243
  const void* src, size_t srcSize,
316
244
  unsigned maxSymbolValue, unsigned tableLog,
317
245
  void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
318
- HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
319
-
320
- size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
321
- #ifndef HUF_FORCE_DECOMPRESS_X1
322
- size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */
323
- #endif
324
-
325
- size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
326
- 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);
327
- #ifndef HUF_FORCE_DECOMPRESS_X2
328
- size_t HUF_decompress1X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
329
- 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 */
330
- #endif
331
- #ifndef HUF_FORCE_DECOMPRESS_X1
332
- size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
333
- 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 */
334
- #endif
246
+ HUF_CElt* hufTable, HUF_repeat* repeat, int flags);
335
247
 
336
- 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 */
337
- #ifndef HUF_FORCE_DECOMPRESS_X2
338
- size_t HUF_decompress1X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
339
- #endif
248
+ 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, int flags);
340
249
  #ifndef HUF_FORCE_DECOMPRESS_X1
341
- size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
250
+ 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, int flags); /**< double-symbols decoder */
342
251
  #endif
343
252
 
344
253
  /* BMI2 variants.
345
254
  * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.
346
255
  */
347
- size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
256
+ size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags);
348
257
  #ifndef HUF_FORCE_DECOMPRESS_X2
349
- 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);
258
+ 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, int flags);
350
259
  #endif
351
- size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
352
- 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);
260
+ size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags);
261
+ 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, int flags);
353
262
  #ifndef HUF_FORCE_DECOMPRESS_X2
354
- size_t HUF_readDTableX1_wksp_bmi2(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int bmi2);
263
+ size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int flags);
264
+ #endif
265
+ #ifndef HUF_FORCE_DECOMPRESS_X1
266
+ size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int flags);
355
267
  #endif
356
268
 
357
- #endif /* HUF_STATIC_LINKING_ONLY */
269
+ #endif /* HUF_H_298734234 */
358
270
 
359
271
  #if defined (__cplusplus)
360
272
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -51,6 +51,8 @@ extern "C" {
51
51
  # include <stdint.h> /* intptr_t */
52
52
  # endif
53
53
  typedef uint8_t BYTE;
54
+ typedef uint8_t U8;
55
+ typedef int8_t S8;
54
56
  typedef uint16_t U16;
55
57
  typedef int16_t S16;
56
58
  typedef uint32_t U32;
@@ -63,6 +65,8 @@ extern "C" {
63
65
  # error "this implementation requires char to be exactly 8-bit type"
64
66
  #endif
65
67
  typedef unsigned char BYTE;
68
+ typedef unsigned char U8;
69
+ typedef signed char S8;
66
70
  #if USHRT_MAX != 65535
67
71
  # error "this implementation requires short to be exactly 16-bit type"
68
72
  #endif
@@ -129,23 +133,15 @@ MEM_STATIC size_t MEM_swapST(size_t in);
129
133
  /*-**************************************************************
130
134
  * Memory I/O Implementation
131
135
  *****************************************************************/
132
- /* MEM_FORCE_MEMORY_ACCESS :
133
- * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
134
- * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
135
- * The below switch allow to select different access method for improved performance.
136
- * Method 0 (default) : use `memcpy()`. Safe and portable.
137
- * Method 1 : `__packed` statement. It depends on compiler extension (i.e., not portable).
138
- * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
136
+ /* MEM_FORCE_MEMORY_ACCESS : For accessing unaligned memory:
137
+ * Method 0 : always use `memcpy()`. Safe and portable.
138
+ * Method 1 : Use compiler extension to set unaligned access.
139
139
  * Method 2 : direct access. This method is portable but violate C standard.
140
140
  * It can generate buggy code on targets depending on alignment.
141
- * In some circumstances, it's the only known way to get the most performance (i.e. GCC + ARMv6)
142
- * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
143
- * Prefer these methods in priority order (0 > 1 > 2)
141
+ * Default : method 1 if supported, else method 0
144
142
  */
145
143
  #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
146
- # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
147
- # define MEM_FORCE_MEMORY_ACCESS 2
148
- # elif defined(__INTEL_COMPILER) || defined(__GNUC__) || defined(__ICCARM__)
144
+ # ifdef __GNUC__
149
145
  # define MEM_FORCE_MEMORY_ACCESS 1
150
146
  # endif
151
147
  #endif
@@ -155,8 +151,22 @@ MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }
155
151
 
156
152
  MEM_STATIC unsigned MEM_isLittleEndian(void)
157
153
  {
154
+ #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
155
+ return 1;
156
+ #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
157
+ return 0;
158
+ #elif defined(__clang__) && __LITTLE_ENDIAN__
159
+ return 1;
160
+ #elif defined(__clang__) && __BIG_ENDIAN__
161
+ return 0;
162
+ #elif defined(_MSC_VER) && (_M_AMD64 || _M_IX86)
163
+ return 1;
164
+ #elif defined(__DMC__) && defined(_M_IX86)
165
+ return 1;
166
+ #else
158
167
  const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
159
168
  return one.c[0];
169
+ #endif
160
170
  }
161
171
 
162
172
  #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
@@ -174,30 +184,19 @@ MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; }
174
184
 
175
185
  #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
176
186
 
177
- /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
178
- /* currently only defined for gcc and icc */
179
- #if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32))
180
- __pragma( pack(push, 1) )
181
- typedef struct { U16 v; } unalign16;
182
- typedef struct { U32 v; } unalign32;
183
- typedef struct { U64 v; } unalign64;
184
- typedef struct { size_t v; } unalignArch;
185
- __pragma( pack(pop) )
186
- #else
187
- typedef struct { U16 v; } __attribute__((packed)) unalign16;
188
- typedef struct { U32 v; } __attribute__((packed)) unalign32;
189
- typedef struct { U64 v; } __attribute__((packed)) unalign64;
190
- typedef struct { size_t v; } __attribute__((packed)) unalignArch;
191
- #endif
187
+ typedef __attribute__((aligned(1))) U16 unalign16;
188
+ typedef __attribute__((aligned(1))) U32 unalign32;
189
+ typedef __attribute__((aligned(1))) U64 unalign64;
190
+ typedef __attribute__((aligned(1))) size_t unalignArch;
192
191
 
193
- MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign16*)ptr)->v; }
194
- MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign32*)ptr)->v; }
195
- MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign64*)ptr)->v; }
196
- MEM_STATIC size_t MEM_readST(const void* ptr) { return ((const unalignArch*)ptr)->v; }
192
+ MEM_STATIC U16 MEM_read16(const void* ptr) { return *(const unalign16*)ptr; }
193
+ MEM_STATIC U32 MEM_read32(const void* ptr) { return *(const unalign32*)ptr; }
194
+ MEM_STATIC U64 MEM_read64(const void* ptr) { return *(const unalign64*)ptr; }
195
+ MEM_STATIC size_t MEM_readST(const void* ptr) { return *(const unalignArch*)ptr; }
197
196
 
198
- MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign16*)memPtr)->v = value; }
199
- MEM_STATIC void MEM_write32(void* memPtr, U32 value) { ((unalign32*)memPtr)->v = value; }
200
- MEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign64*)memPtr)->v = value; }
197
+ MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(unalign16*)memPtr = value; }
198
+ MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(unalign32*)memPtr = value; }
199
+ MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(unalign64*)memPtr = value; }
201
200
 
202
201
  #else
203
202
 
@@ -241,6 +240,14 @@ MEM_STATIC void MEM_write64(void* memPtr, U64 value)
241
240
 
242
241
  #endif /* MEM_FORCE_MEMORY_ACCESS */
243
242
 
243
+ MEM_STATIC U32 MEM_swap32_fallback(U32 in)
244
+ {
245
+ return ((in << 24) & 0xff000000 ) |
246
+ ((in << 8) & 0x00ff0000 ) |
247
+ ((in >> 8) & 0x0000ff00 ) |
248
+ ((in >> 24) & 0x000000ff );
249
+ }
250
+
244
251
  MEM_STATIC U32 MEM_swap32(U32 in)
245
252
  {
246
253
  #if defined(_MSC_VER) /* Visual Studio */
@@ -249,22 +256,13 @@ MEM_STATIC U32 MEM_swap32(U32 in)
249
256
  || (defined(__clang__) && __has_builtin(__builtin_bswap32))
250
257
  return __builtin_bswap32(in);
251
258
  #else
252
- return ((in << 24) & 0xff000000 ) |
253
- ((in << 8) & 0x00ff0000 ) |
254
- ((in >> 8) & 0x0000ff00 ) |
255
- ((in >> 24) & 0x000000ff );
259
+ return MEM_swap32_fallback(in);
256
260
  #endif
257
261
  }
258
262
 
259
- MEM_STATIC U64 MEM_swap64(U64 in)
263
+ MEM_STATIC U64 MEM_swap64_fallback(U64 in)
260
264
  {
261
- #if defined(_MSC_VER) /* Visual Studio */
262
- return _byteswap_uint64(in);
263
- #elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
264
- || (defined(__clang__) && __has_builtin(__builtin_bswap64))
265
- return __builtin_bswap64(in);
266
- #else
267
- return ((in << 56) & 0xff00000000000000ULL) |
265
+ return ((in << 56) & 0xff00000000000000ULL) |
268
266
  ((in << 40) & 0x00ff000000000000ULL) |
269
267
  ((in << 24) & 0x0000ff0000000000ULL) |
270
268
  ((in << 8) & 0x000000ff00000000ULL) |
@@ -272,6 +270,17 @@ MEM_STATIC U64 MEM_swap64(U64 in)
272
270
  ((in >> 24) & 0x0000000000ff0000ULL) |
273
271
  ((in >> 40) & 0x000000000000ff00ULL) |
274
272
  ((in >> 56) & 0x00000000000000ffULL);
273
+ }
274
+
275
+ MEM_STATIC U64 MEM_swap64(U64 in)
276
+ {
277
+ #if defined(_MSC_VER) /* Visual Studio */
278
+ return _byteswap_uint64(in);
279
+ #elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
280
+ || (defined(__clang__) && __has_builtin(__builtin_bswap64))
281
+ return __builtin_bswap64(in);
282
+ #else
283
+ return MEM_swap64_fallback(in);
275
284
  #endif
276
285
  }
277
286
 
@@ -308,7 +317,7 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
308
317
 
309
318
  MEM_STATIC U32 MEM_readLE24(const void* memPtr)
310
319
  {
311
- return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
320
+ return (U32)MEM_readLE16(memPtr) + ((U32)(((const BYTE*)memPtr)[2]) << 16);
312
321
  }
313
322
 
314
323
  MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val)