zstd-ruby 1.5.0.0 → 1.5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +2 -2
  3. data/README.md +1 -1
  4. data/ext/zstdruby/extconf.rb +1 -0
  5. data/ext/zstdruby/libzstd/Makefile +50 -175
  6. data/ext/zstdruby/libzstd/README.md +7 -1
  7. data/ext/zstdruby/libzstd/common/bitstream.h +24 -9
  8. data/ext/zstdruby/libzstd/common/compiler.h +89 -43
  9. data/ext/zstdruby/libzstd/common/entropy_common.c +11 -5
  10. data/ext/zstdruby/libzstd/common/error_private.h +79 -0
  11. data/ext/zstdruby/libzstd/common/fse.h +2 -1
  12. data/ext/zstdruby/libzstd/common/fse_decompress.c +1 -1
  13. data/ext/zstdruby/libzstd/common/huf.h +24 -22
  14. data/ext/zstdruby/libzstd/common/mem.h +18 -0
  15. data/ext/zstdruby/libzstd/common/portability_macros.h +131 -0
  16. data/ext/zstdruby/libzstd/common/xxhash.c +5 -805
  17. data/ext/zstdruby/libzstd/common/xxhash.h +5568 -167
  18. data/ext/zstdruby/libzstd/common/zstd_internal.h +92 -88
  19. data/ext/zstdruby/libzstd/common/zstd_trace.h +12 -3
  20. data/ext/zstdruby/libzstd/compress/clevels.h +134 -0
  21. data/ext/zstdruby/libzstd/compress/fse_compress.c +63 -27
  22. data/ext/zstdruby/libzstd/compress/huf_compress.c +537 -104
  23. data/ext/zstdruby/libzstd/compress/zstd_compress.c +194 -278
  24. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +102 -44
  25. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +4 -3
  26. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +3 -1
  27. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +5 -4
  28. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +3 -2
  29. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +3 -3
  30. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +289 -114
  31. data/ext/zstdruby/libzstd/compress/zstd_fast.c +302 -123
  32. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +418 -502
  33. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +4 -4
  34. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +1 -1
  35. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +4 -1
  36. data/ext/zstdruby/libzstd/compress/zstd_opt.c +186 -108
  37. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +59 -29
  38. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +727 -189
  39. data/ext/zstdruby/libzstd/decompress/huf_decompress_amd64.S +571 -0
  40. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +85 -22
  41. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +744 -220
  42. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +8 -2
  43. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +34 -3
  44. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +23 -3
  45. data/ext/zstdruby/libzstd/dictBuilder/cover.c +9 -2
  46. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +11 -4
  47. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +99 -28
  48. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +2 -6
  49. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +3 -7
  50. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +3 -7
  51. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +3 -7
  52. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +3 -7
  53. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +3 -7
  54. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +3 -7
  55. data/ext/zstdruby/libzstd/libzstd.mk +185 -0
  56. data/ext/zstdruby/libzstd/libzstd.pc.in +1 -0
  57. data/ext/zstdruby/libzstd/modulemap/module.modulemap +4 -0
  58. data/ext/zstdruby/libzstd/zdict.h +4 -4
  59. data/ext/zstdruby/libzstd/zstd.h +179 -136
  60. data/ext/zstdruby/zstdruby.c +2 -2
  61. data/lib/zstd-ruby/version.rb +1 -1
  62. metadata +8 -3
@@ -22,6 +22,8 @@ extern "C" {
22
22
  * Dependencies
23
23
  ******************************************/
24
24
  #include "../zstd_errors.h" /* enum list */
25
+ #include "compiler.h"
26
+ #include "debug.h"
25
27
  #include "zstd_deps.h" /* size_t */
26
28
 
27
29
 
@@ -73,6 +75,83 @@ ERR_STATIC const char* ERR_getErrorName(size_t code)
73
75
  return ERR_getErrorString(ERR_getErrorCode(code));
74
76
  }
75
77
 
78
+ /**
79
+ * Ignore: this is an internal helper.
80
+ *
81
+ * This is a helper function to help force C99-correctness during compilation.
82
+ * Under strict compilation modes, variadic macro arguments can't be empty.
83
+ * However, variadic function arguments can be. Using a function therefore lets
84
+ * us statically check that at least one (string) argument was passed,
85
+ * independent of the compilation flags.
86
+ */
87
+ static INLINE_KEYWORD UNUSED_ATTR
88
+ void _force_has_format_string(const char *format, ...) {
89
+ (void)format;
90
+ }
91
+
92
+ /**
93
+ * Ignore: this is an internal helper.
94
+ *
95
+ * We want to force this function invocation to be syntactically correct, but
96
+ * we don't want to force runtime evaluation of its arguments.
97
+ */
98
+ #define _FORCE_HAS_FORMAT_STRING(...) \
99
+ if (0) { \
100
+ _force_has_format_string(__VA_ARGS__); \
101
+ }
102
+
103
+ #define ERR_QUOTE(str) #str
104
+
105
+ /**
106
+ * Return the specified error if the condition evaluates to true.
107
+ *
108
+ * In debug modes, prints additional information.
109
+ * In order to do that (particularly, printing the conditional that failed),
110
+ * this can't just wrap RETURN_ERROR().
111
+ */
112
+ #define RETURN_ERROR_IF(cond, err, ...) \
113
+ if (cond) { \
114
+ RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
115
+ __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
116
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
117
+ RAWLOG(3, ": " __VA_ARGS__); \
118
+ RAWLOG(3, "\n"); \
119
+ return ERROR(err); \
120
+ }
121
+
122
+ /**
123
+ * Unconditionally return the specified error.
124
+ *
125
+ * In debug modes, prints additional information.
126
+ */
127
+ #define RETURN_ERROR(err, ...) \
128
+ do { \
129
+ RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
130
+ __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \
131
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
132
+ RAWLOG(3, ": " __VA_ARGS__); \
133
+ RAWLOG(3, "\n"); \
134
+ return ERROR(err); \
135
+ } while(0);
136
+
137
+ /**
138
+ * If the provided expression evaluates to an error code, returns that error code.
139
+ *
140
+ * In debug modes, prints additional information.
141
+ */
142
+ #define FORWARD_IF_ERROR(err, ...) \
143
+ do { \
144
+ size_t const err_code = (err); \
145
+ if (ERR_isError(err_code)) { \
146
+ RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
147
+ __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
148
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
149
+ RAWLOG(3, ": " __VA_ARGS__); \
150
+ RAWLOG(3, "\n"); \
151
+ return err_code; \
152
+ } \
153
+ } while(0);
154
+
76
155
  #if defined (__cplusplus)
77
156
  }
78
157
  #endif
@@ -336,8 +336,9 @@ size_t FSE_buildCTable_rle (FSE_CTable* ct, unsigned char symbolValue);
336
336
  /* FSE_buildCTable_wksp() :
337
337
  * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
338
338
  * `wkspSize` must be >= `FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog)` of `unsigned`.
339
+ * See FSE_buildCTable_wksp() for breakdown of workspace usage.
339
340
  */
340
- #define FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog) (maxSymbolValue + 2 + (1ull << (tableLog - 2)))
341
+ #define FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog) (((maxSymbolValue + 2) + (1ull << (tableLog)))/2 + sizeof(U64)/sizeof(U32) /* additional 8 bytes for potential table overwrite */)
341
342
  #define FSE_BUILD_CTABLE_WORKSPACE_SIZE(maxSymbolValue, tableLog) (sizeof(unsigned) * FSE_BUILD_CTABLE_WORKSPACE_SIZE_U32(maxSymbolValue, tableLog))
342
343
  size_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
343
344
 
@@ -365,7 +365,7 @@ static size_t FSE_decompress_wksp_body_default(void* dst, size_t dstCapacity, co
365
365
  }
366
366
 
367
367
  #if DYNAMIC_BMI2
368
- TARGET_ATTRIBUTE("bmi2") static size_t FSE_decompress_wksp_body_bmi2(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize)
368
+ BMI2_TARGET_ATTRIBUTE static size_t FSE_decompress_wksp_body_bmi2(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, unsigned maxLog, void* workSpace, size_t wkspSize)
369
369
  {
370
370
  return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 1);
371
371
  }
@@ -89,9 +89,9 @@ HUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity,
89
89
 
90
90
  /** HUF_compress4X_wksp() :
91
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))
92
+ * `workspace` must be at least as large as HUF_WORKSPACE_SIZE */
93
+ #define HUF_WORKSPACE_SIZE ((8 << 10) + 512 /* sorting scratch space */)
94
+ #define HUF_WORKSPACE_SIZE_U64 (HUF_WORKSPACE_SIZE / sizeof(U64))
95
95
  HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
96
96
  const void* src, size_t srcSize,
97
97
  unsigned maxSymbolValue, unsigned tableLog,
@@ -116,11 +116,11 @@ HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
116
116
 
117
117
 
118
118
  /* *** Constants *** */
119
- #define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
119
+ #define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_TABLELOG_ABSOLUTEMAX */
120
120
  #define HUF_TABLELOG_DEFAULT 11 /* default tableLog value when none specified */
121
121
  #define HUF_SYMBOLVALUE_MAX 255
122
122
 
123
- #define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
123
+ #define HUF_TABLELOG_ABSOLUTEMAX 12 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
124
124
  #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
125
125
  # error "HUF_TABLELOG_MAX is too large !"
126
126
  #endif
@@ -136,15 +136,11 @@ HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
136
136
 
137
137
  /* static allocation of HUF's Compression Table */
138
138
  /* 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))
139
+ typedef size_t HUF_CElt; /* consider it an incomplete type */
140
+ #define HUF_CTABLE_SIZE_ST(maxSymbolValue) ((maxSymbolValue)+2) /* Use tables of size_t, for proper alignment */
141
+ #define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_ST(maxSymbolValue) * sizeof(size_t))
146
142
  #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
147
- HUF_CElt name[HUF_CTABLE_SIZE_U32(maxSymbolValue)] /* no final ; */
143
+ HUF_CElt name[HUF_CTABLE_SIZE_ST(maxSymbolValue)] /* no final ; */
148
144
 
149
145
  /* static allocation of HUF's DTable */
150
146
  typedef U32 HUF_DTable;
@@ -194,6 +190,7 @@ size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSym
194
190
  size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
195
191
  size_t HUF_writeCTable_wksp(void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog, void* workspace, size_t workspaceSize);
196
192
  size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
193
+ size_t HUF_compress4X_usingCTable_bmi2(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int bmi2);
197
194
  size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
198
195
  int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
199
196
 
@@ -206,12 +203,13 @@ typedef enum {
206
203
  * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
207
204
  * If it uses hufTable it does not modify hufTable or repeat.
208
205
  * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
209
- * If preferRepeat then the old table will always be used if valid. */
206
+ * If preferRepeat then the old table will always be used if valid.
207
+ * If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */
210
208
  size_t HUF_compress4X_repeat(void* dst, size_t dstSize,
211
209
  const void* src, size_t srcSize,
212
210
  unsigned maxSymbolValue, unsigned tableLog,
213
211
  void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
214
- HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
212
+ HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2, unsigned suspectUncompressible);
215
213
 
216
214
  /** HUF_buildCTable_wksp() :
217
215
  * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
@@ -249,11 +247,10 @@ size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize,
249
247
  * Loading a CTable saved with HUF_writeCTable() */
250
248
  size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned *hasZeroWeights);
251
249
 
252
- /** HUF_getNbBits() :
250
+ /** HUF_getNbBitsFromCTable() :
253
251
  * Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX
254
- * Note 1 : is not inlined, as HUF_CElt definition is private
255
- * Note 2 : const void* used, so that it can provide a statically allocated table as argument (which uses type U32) */
256
- U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue);
252
+ * Note 1 : is not inlined, as HUF_CElt definition is private */
253
+ U32 HUF_getNbBitsFromCTable(const HUF_CElt* symbolTable, U32 symbolValue);
257
254
 
258
255
  /*
259
256
  * HUF_decompress() does the following:
@@ -305,18 +302,20 @@ size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* c
305
302
  /* ====================== */
306
303
 
307
304
  size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
308
- 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 */
305
+ 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_U64 U64 */
309
306
  size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
307
+ size_t HUF_compress1X_usingCTable_bmi2(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable, int bmi2);
310
308
  /** HUF_compress1X_repeat() :
311
309
  * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
312
310
  * If it uses hufTable it does not modify hufTable or repeat.
313
311
  * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
314
- * If preferRepeat then the old table will always be used if valid. */
312
+ * If preferRepeat then the old table will always be used if valid.
313
+ * If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */
315
314
  size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
316
315
  const void* src, size_t srcSize,
317
316
  unsigned maxSymbolValue, unsigned tableLog,
318
317
  void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
319
- HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
318
+ HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2, unsigned suspectUncompressible);
320
319
 
321
320
  size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
322
321
  #ifndef HUF_FORCE_DECOMPRESS_X1
@@ -354,6 +353,9 @@ size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t ds
354
353
  #ifndef HUF_FORCE_DECOMPRESS_X2
355
354
  size_t HUF_readDTableX1_wksp_bmi2(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int bmi2);
356
355
  #endif
356
+ #ifndef HUF_FORCE_DECOMPRESS_X1
357
+ size_t HUF_readDTableX2_wksp_bmi2(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int bmi2);
358
+ #endif
357
359
 
358
360
  #endif /* HUF_STATIC_LINKING_ONLY */
359
361
 
@@ -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
@@ -153,8 +157,22 @@ MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }
153
157
 
154
158
  MEM_STATIC unsigned MEM_isLittleEndian(void)
155
159
  {
160
+ #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
161
+ return 1;
162
+ #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
163
+ return 0;
164
+ #elif defined(__clang__) && __LITTLE_ENDIAN__
165
+ return 1;
166
+ #elif defined(__clang__) && __BIG_ENDIAN__
167
+ return 0;
168
+ #elif defined(_MSC_VER) && (_M_AMD64 || _M_IX86)
169
+ return 1;
170
+ #elif defined(__DMC__) && defined(_M_IX86)
171
+ return 1;
172
+ #else
156
173
  const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
157
174
  return one.c[0];
175
+ #endif
158
176
  }
159
177
 
160
178
  #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
@@ -0,0 +1,131 @@
1
+ /*
2
+ * Copyright (c) Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under both the BSD-style license (found in the
6
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
+ * in the COPYING file in the root directory of this source tree).
8
+ * You may select, at your option, one of the above-listed licenses.
9
+ */
10
+
11
+ #ifndef ZSTD_PORTABILITY_MACROS_H
12
+ #define ZSTD_PORTABILITY_MACROS_H
13
+
14
+ /**
15
+ * This header file contains macro defintions to support portability.
16
+ * This header is shared between C and ASM code, so it MUST only
17
+ * contain macro definitions. It MUST not contain any C code.
18
+ *
19
+ * This header ONLY defines macros to detect platforms/feature support.
20
+ *
21
+ */
22
+
23
+
24
+ /* compat. with non-clang compilers */
25
+ #ifndef __has_attribute
26
+ #define __has_attribute(x) 0
27
+ #endif
28
+
29
+ /* compat. with non-clang compilers */
30
+ #ifndef __has_builtin
31
+ # define __has_builtin(x) 0
32
+ #endif
33
+
34
+ /* compat. with non-clang compilers */
35
+ #ifndef __has_feature
36
+ # define __has_feature(x) 0
37
+ #endif
38
+
39
+ /* detects whether we are being compiled under msan */
40
+ #ifndef ZSTD_MEMORY_SANITIZER
41
+ # if __has_feature(memory_sanitizer)
42
+ # define ZSTD_MEMORY_SANITIZER 1
43
+ # else
44
+ # define ZSTD_MEMORY_SANITIZER 0
45
+ # endif
46
+ #endif
47
+
48
+ /* detects whether we are being compiled under asan */
49
+ #ifndef ZSTD_ADDRESS_SANITIZER
50
+ # if __has_feature(address_sanitizer)
51
+ # define ZSTD_ADDRESS_SANITIZER 1
52
+ # elif defined(__SANITIZE_ADDRESS__)
53
+ # define ZSTD_ADDRESS_SANITIZER 1
54
+ # else
55
+ # define ZSTD_ADDRESS_SANITIZER 0
56
+ # endif
57
+ #endif
58
+
59
+ /* detects whether we are being compiled under dfsan */
60
+ #ifndef ZSTD_DATAFLOW_SANITIZER
61
+ # if __has_feature(dataflow_sanitizer)
62
+ # define ZSTD_DATAFLOW_SANITIZER 1
63
+ # else
64
+ # define ZSTD_DATAFLOW_SANITIZER 0
65
+ # endif
66
+ #endif
67
+
68
+
69
+ /* Enable runtime BMI2 dispatch based on the CPU.
70
+ * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
71
+ */
72
+ #ifndef DYNAMIC_BMI2
73
+ #if ((defined(__clang__) && __has_attribute(__target__)) \
74
+ || (defined(__GNUC__) \
75
+ && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
76
+ && (defined(__x86_64__) || defined(_M_X64)) \
77
+ && !defined(__BMI2__)
78
+ # define DYNAMIC_BMI2 1
79
+ #else
80
+ # define DYNAMIC_BMI2 0
81
+ #endif
82
+ #endif
83
+
84
+ /**
85
+ * Only enable assembly for GNUC comptabile compilers,
86
+ * because other platforms may not support GAS assembly syntax.
87
+ *
88
+ * Only enable assembly for Linux / MacOS, other platforms may
89
+ * work, but they haven't been tested. This could likely be
90
+ * extended to BSD systems.
91
+ *
92
+ * Disable assembly when MSAN is enabled, because MSAN requires
93
+ * 100% of code to be instrumented to work.
94
+ */
95
+ #if defined(__GNUC__)
96
+ # if defined(__linux__) || defined(__linux) || defined(__APPLE__)
97
+ # if ZSTD_MEMORY_SANITIZER
98
+ # define ZSTD_ASM_SUPPORTED 0
99
+ # elif ZSTD_DATAFLOW_SANITIZER
100
+ # define ZSTD_ASM_SUPPORTED 0
101
+ # else
102
+ # define ZSTD_ASM_SUPPORTED 1
103
+ # endif
104
+ # else
105
+ # define ZSTD_ASM_SUPPORTED 0
106
+ # endif
107
+ #else
108
+ # define ZSTD_ASM_SUPPORTED 0
109
+ #endif
110
+
111
+ /**
112
+ * Determines whether we should enable assembly for x86-64
113
+ * with BMI2.
114
+ *
115
+ * Enable if all of the following conditions hold:
116
+ * - ASM hasn't been explicitly disabled by defining ZSTD_DISABLE_ASM
117
+ * - Assembly is supported
118
+ * - We are compiling for x86-64 and either:
119
+ * - DYNAMIC_BMI2 is enabled
120
+ * - BMI2 is supported at compile time
121
+ */
122
+ #if !defined(ZSTD_DISABLE_ASM) && \
123
+ ZSTD_ASM_SUPPORTED && \
124
+ defined(__x86_64__) && \
125
+ (DYNAMIC_BMI2 || defined(__BMI2__))
126
+ # define ZSTD_ENABLE_ASM_X86_64_BMI2 1
127
+ #else
128
+ # define ZSTD_ENABLE_ASM_X86_64_BMI2 0
129
+ #endif
130
+
131
+ #endif /* ZSTD_PORTABILITY_MACROS_H */