extzstd 0.3.2 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -3
  3. data/contrib/zstd/CHANGELOG +225 -1
  4. data/contrib/zstd/CONTRIBUTING.md +158 -75
  5. data/contrib/zstd/LICENSE +4 -4
  6. data/contrib/zstd/Makefile +106 -69
  7. data/contrib/zstd/Package.swift +36 -0
  8. data/contrib/zstd/README.md +64 -36
  9. data/contrib/zstd/SECURITY.md +15 -0
  10. data/contrib/zstd/TESTING.md +2 -3
  11. data/contrib/zstd/lib/BUCK +5 -7
  12. data/contrib/zstd/lib/Makefile +117 -199
  13. data/contrib/zstd/lib/README.md +37 -7
  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 +80 -86
  17. data/contrib/zstd/lib/common/compiler.h +225 -63
  18. data/contrib/zstd/lib/common/cpu.h +37 -1
  19. data/contrib/zstd/lib/common/debug.c +7 -1
  20. data/contrib/zstd/lib/common/debug.h +21 -12
  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 +93 -5
  24. data/contrib/zstd/lib/common/fse.h +12 -87
  25. data/contrib/zstd/lib/common/fse_decompress.c +37 -117
  26. data/contrib/zstd/lib/common/huf.h +97 -172
  27. data/contrib/zstd/lib/common/mem.h +58 -58
  28. data/contrib/zstd/lib/common/pool.c +38 -17
  29. data/contrib/zstd/lib/common/pool.h +10 -4
  30. data/contrib/zstd/lib/common/portability_macros.h +158 -0
  31. data/contrib/zstd/lib/common/threading.c +74 -14
  32. data/contrib/zstd/lib/common/threading.h +5 -10
  33. data/contrib/zstd/lib/common/xxhash.c +6 -814
  34. data/contrib/zstd/lib/common/xxhash.h +6930 -195
  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 +68 -154
  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 +75 -155
  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 +810 -259
  44. data/contrib/zstd/lib/compress/zstd_compress.c +2864 -919
  45. data/contrib/zstd/lib/compress/zstd_compress_internal.h +523 -192
  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 +251 -412
  51. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +1 -1
  52. data/contrib/zstd/lib/compress/zstd_cwksp.h +284 -97
  53. data/contrib/zstd/lib/compress/zstd_double_fast.c +382 -133
  54. data/contrib/zstd/lib/compress/zstd_double_fast.h +14 -2
  55. data/contrib/zstd/lib/compress/zstd_fast.c +732 -260
  56. data/contrib/zstd/lib/compress/zstd_fast.h +3 -2
  57. data/contrib/zstd/lib/compress/zstd_lazy.c +1177 -390
  58. data/contrib/zstd/lib/compress/zstd_lazy.h +129 -14
  59. data/contrib/zstd/lib/compress/zstd_ldm.c +280 -210
  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 +516 -285
  63. data/contrib/zstd/lib/compress/zstd_opt.h +32 -8
  64. data/contrib/zstd/lib/compress/zstdmt_compress.c +202 -131
  65. data/contrib/zstd/lib/compress/zstdmt_compress.h +9 -6
  66. data/contrib/zstd/lib/decompress/huf_decompress.c +1149 -555
  67. data/contrib/zstd/lib/decompress/huf_decompress_amd64.S +595 -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 +583 -106
  71. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1054 -379
  72. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +14 -3
  73. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +56 -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 +60 -44
  79. data/contrib/zstd/lib/dictBuilder/cover.h +6 -11
  80. data/contrib/zstd/lib/dictBuilder/divsufsort.c +1 -1
  81. data/contrib/zstd/lib/dictBuilder/fastcover.c +26 -18
  82. data/contrib/zstd/lib/dictBuilder/zdict.c +100 -101
  83. data/contrib/zstd/lib/legacy/zstd_legacy.h +38 -1
  84. data/contrib/zstd/lib/legacy/zstd_v01.c +18 -53
  85. data/contrib/zstd/lib/legacy/zstd_v01.h +1 -1
  86. data/contrib/zstd/lib/legacy/zstd_v02.c +28 -85
  87. data/contrib/zstd/lib/legacy/zstd_v02.h +1 -1
  88. data/contrib/zstd/lib/legacy/zstd_v03.c +29 -88
  89. data/contrib/zstd/lib/legacy/zstd_v03.h +1 -1
  90. data/contrib/zstd/lib/legacy/zstd_v04.c +27 -80
  91. data/contrib/zstd/lib/legacy/zstd_v04.h +1 -1
  92. data/contrib/zstd/lib/legacy/zstd_v05.c +36 -85
  93. data/contrib/zstd/lib/legacy/zstd_v05.h +1 -1
  94. data/contrib/zstd/lib/legacy/zstd_v06.c +44 -96
  95. data/contrib/zstd/lib/legacy/zstd_v06.h +1 -1
  96. data/contrib/zstd/lib/legacy/zstd_v07.c +37 -92
  97. data/contrib/zstd/lib/legacy/zstd_v07.h +1 -1
  98. data/contrib/zstd/lib/libzstd.mk +237 -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 +1030 -332
  103. data/contrib/zstd/lib/{common/zstd_errors.h → zstd_errors.h} +27 -8
  104. data/ext/extconf.rb +26 -7
  105. data/ext/extzstd.c +51 -24
  106. data/ext/extzstd.h +33 -6
  107. data/ext/extzstd_stream.c +74 -31
  108. data/ext/libzstd_conf.h +0 -1
  109. data/ext/zstd_decompress_asm.S +1 -0
  110. metadata +17 -7
  111. data/contrib/zstd/appveyor.yml +0 -292
  112. data/ext/depend +0 -2
@@ -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 */
101
-
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
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) */
111
36
 
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,29 @@ 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 : If symbolValue > HUF_readCTableHeader(symbolTable).maxSymbolValue, returns 0
201
+ * Note 2 : is not inlined, as HUF_CElt definition is private
202
+ */
203
+ U32 HUF_getNbBitsFromCTable(const HUF_CElt* symbolTable, U32 symbolValue);
204
+
205
+ typedef struct {
206
+ BYTE tableLog;
207
+ BYTE maxSymbolValue;
208
+ BYTE unused[sizeof(size_t) - 2];
209
+ } HUF_CTableHeader;
210
+
211
+ /** HUF_readCTableHeader() :
212
+ * @returns The header from the CTable specifying the tableLog and the maxSymbolValue.
213
+ */
214
+ HUF_CTableHeader HUF_readCTableHeader(HUF_CElt const* ctable);
256
215
 
257
216
  /*
258
217
  * HUF_decompress() does the following:
@@ -278,83 +237,49 @@ U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);
278
237
  * a required workspace size greater than that specified in the following
279
238
  * macro.
280
239
  */
281
- #define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10)
240
+ #define HUF_DECOMPRESS_WORKSPACE_SIZE ((2 << 10) + (1 << 9))
282
241
  #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
283
242
 
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
243
 
302
244
  /* ====================== */
303
245
  /* single stream variants */
304
246
  /* ====================== */
305
247
 
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);
248
+ size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int flags);
309
249
  /** HUF_compress1X_repeat() :
310
250
  * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
311
251
  * If it uses hufTable it does not modify hufTable or repeat.
312
252
  * 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. */
253
+ * If preferRepeat then the old table will always be used if valid.
254
+ * If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */
314
255
  size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
315
256
  const void* src, size_t srcSize,
316
257
  unsigned maxSymbolValue, unsigned tableLog,
317
258
  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);
259
+ HUF_CElt* hufTable, HUF_repeat* repeat, int flags);
319
260
 
320
- size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
261
+ 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);
321
262
  #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
335
-
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
340
- #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);
263
+ 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
264
  #endif
343
265
 
344
266
  /* BMI2 variants.
345
267
  * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.
346
268
  */
347
- size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
269
+ size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags);
348
270
  #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);
271
+ 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
272
  #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);
273
+ size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int flags);
274
+ 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
275
  #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);
276
+ size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int flags);
277
+ #endif
278
+ #ifndef HUF_FORCE_DECOMPRESS_X1
279
+ size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int flags);
355
280
  #endif
356
281
 
357
- #endif /* HUF_STATIC_LINKING_ONLY */
282
+ #endif /* HUF_H_298734234 */
358
283
 
359
284
  #if defined (__cplusplus)
360
285
  }
@@ -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
@@ -31,15 +31,6 @@ extern "C" {
31
31
  # include <stdlib.h> /* _byteswap_ulong */
32
32
  # include <intrin.h> /* _byteswap_* */
33
33
  #endif
34
- #if defined(__GNUC__)
35
- # define MEM_STATIC static __inline __attribute__((unused))
36
- #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
37
- # define MEM_STATIC static inline
38
- #elif defined(_MSC_VER)
39
- # define MEM_STATIC static __inline
40
- #else
41
- # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
42
- #endif
43
34
 
44
35
  /*-**************************************************************
45
36
  * Basic Types
@@ -51,6 +42,8 @@ extern "C" {
51
42
  # include <stdint.h> /* intptr_t */
52
43
  # endif
53
44
  typedef uint8_t BYTE;
45
+ typedef uint8_t U8;
46
+ typedef int8_t S8;
54
47
  typedef uint16_t U16;
55
48
  typedef int16_t S16;
56
49
  typedef uint32_t U32;
@@ -63,6 +56,8 @@ extern "C" {
63
56
  # error "this implementation requires char to be exactly 8-bit type"
64
57
  #endif
65
58
  typedef unsigned char BYTE;
59
+ typedef unsigned char U8;
60
+ typedef signed char S8;
66
61
  #if USHRT_MAX != 65535
67
62
  # error "this implementation requires short to be exactly 16-bit type"
68
63
  #endif
@@ -129,23 +124,15 @@ MEM_STATIC size_t MEM_swapST(size_t in);
129
124
  /*-**************************************************************
130
125
  * Memory I/O Implementation
131
126
  *****************************************************************/
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`.
127
+ /* MEM_FORCE_MEMORY_ACCESS : For accessing unaligned memory:
128
+ * Method 0 : always use `memcpy()`. Safe and portable.
129
+ * Method 1 : Use compiler extension to set unaligned access.
139
130
  * Method 2 : direct access. This method is portable but violate C standard.
140
131
  * 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)
132
+ * Default : method 1 if supported, else method 0
144
133
  */
145
134
  #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__)
135
+ # ifdef __GNUC__
149
136
  # define MEM_FORCE_MEMORY_ACCESS 1
150
137
  # endif
151
138
  #endif
@@ -155,8 +142,22 @@ MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }
155
142
 
156
143
  MEM_STATIC unsigned MEM_isLittleEndian(void)
157
144
  {
145
+ #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
146
+ return 1;
147
+ #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
148
+ return 0;
149
+ #elif defined(__clang__) && __LITTLE_ENDIAN__
150
+ return 1;
151
+ #elif defined(__clang__) && __BIG_ENDIAN__
152
+ return 0;
153
+ #elif defined(_MSC_VER) && (_M_AMD64 || _M_IX86)
154
+ return 1;
155
+ #elif defined(__DMC__) && defined(_M_IX86)
156
+ return 1;
157
+ #else
158
158
  const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
159
159
  return one.c[0];
160
+ #endif
160
161
  }
161
162
 
162
163
  #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
@@ -174,30 +175,19 @@ MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; }
174
175
 
175
176
  #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
176
177
 
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
178
+ typedef __attribute__((aligned(1))) U16 unalign16;
179
+ typedef __attribute__((aligned(1))) U32 unalign32;
180
+ typedef __attribute__((aligned(1))) U64 unalign64;
181
+ typedef __attribute__((aligned(1))) size_t unalignArch;
192
182
 
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; }
183
+ MEM_STATIC U16 MEM_read16(const void* ptr) { return *(const unalign16*)ptr; }
184
+ MEM_STATIC U32 MEM_read32(const void* ptr) { return *(const unalign32*)ptr; }
185
+ MEM_STATIC U64 MEM_read64(const void* ptr) { return *(const unalign64*)ptr; }
186
+ MEM_STATIC size_t MEM_readST(const void* ptr) { return *(const unalignArch*)ptr; }
197
187
 
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; }
188
+ MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(unalign16*)memPtr = value; }
189
+ MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(unalign32*)memPtr = value; }
190
+ MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(unalign64*)memPtr = value; }
201
191
 
202
192
  #else
203
193
 
@@ -241,6 +231,14 @@ MEM_STATIC void MEM_write64(void* memPtr, U64 value)
241
231
 
242
232
  #endif /* MEM_FORCE_MEMORY_ACCESS */
243
233
 
234
+ MEM_STATIC U32 MEM_swap32_fallback(U32 in)
235
+ {
236
+ return ((in << 24) & 0xff000000 ) |
237
+ ((in << 8) & 0x00ff0000 ) |
238
+ ((in >> 8) & 0x0000ff00 ) |
239
+ ((in >> 24) & 0x000000ff );
240
+ }
241
+
244
242
  MEM_STATIC U32 MEM_swap32(U32 in)
245
243
  {
246
244
  #if defined(_MSC_VER) /* Visual Studio */
@@ -249,22 +247,13 @@ MEM_STATIC U32 MEM_swap32(U32 in)
249
247
  || (defined(__clang__) && __has_builtin(__builtin_bswap32))
250
248
  return __builtin_bswap32(in);
251
249
  #else
252
- return ((in << 24) & 0xff000000 ) |
253
- ((in << 8) & 0x00ff0000 ) |
254
- ((in >> 8) & 0x0000ff00 ) |
255
- ((in >> 24) & 0x000000ff );
250
+ return MEM_swap32_fallback(in);
256
251
  #endif
257
252
  }
258
253
 
259
- MEM_STATIC U64 MEM_swap64(U64 in)
254
+ MEM_STATIC U64 MEM_swap64_fallback(U64 in)
260
255
  {
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) |
256
+ return ((in << 56) & 0xff00000000000000ULL) |
268
257
  ((in << 40) & 0x00ff000000000000ULL) |
269
258
  ((in << 24) & 0x0000ff0000000000ULL) |
270
259
  ((in << 8) & 0x000000ff00000000ULL) |
@@ -272,6 +261,17 @@ MEM_STATIC U64 MEM_swap64(U64 in)
272
261
  ((in >> 24) & 0x0000000000ff0000ULL) |
273
262
  ((in >> 40) & 0x000000000000ff00ULL) |
274
263
  ((in >> 56) & 0x00000000000000ffULL);
264
+ }
265
+
266
+ MEM_STATIC U64 MEM_swap64(U64 in)
267
+ {
268
+ #if defined(_MSC_VER) /* Visual Studio */
269
+ return _byteswap_uint64(in);
270
+ #elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
271
+ || (defined(__clang__) && __has_builtin(__builtin_bswap64))
272
+ return __builtin_bswap64(in);
273
+ #else
274
+ return MEM_swap64_fallback(in);
275
275
  #endif
276
276
  }
277
277
 
@@ -308,7 +308,7 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
308
308
 
309
309
  MEM_STATIC U32 MEM_readLE24(const void* memPtr)
310
310
  {
311
- return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
311
+ return (U32)MEM_readLE16(memPtr) + ((U32)(((const BYTE*)memPtr)[2]) << 16);
312
312
  }
313
313
 
314
314
  MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val)