zstd-ruby 1.4.4.0 → 1.5.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/README.md +78 -5
  4. data/Rakefile +8 -2
  5. data/ext/zstdruby/common.h +15 -0
  6. data/ext/zstdruby/extconf.rb +3 -2
  7. data/ext/zstdruby/libzstd/common/allocations.h +55 -0
  8. data/ext/zstdruby/libzstd/common/bits.h +200 -0
  9. data/ext/zstdruby/libzstd/common/bitstream.h +74 -97
  10. data/ext/zstdruby/libzstd/common/compiler.h +219 -20
  11. data/ext/zstdruby/libzstd/common/cpu.h +1 -3
  12. data/ext/zstdruby/libzstd/common/debug.c +11 -31
  13. data/ext/zstdruby/libzstd/common/debug.h +22 -49
  14. data/ext/zstdruby/libzstd/common/entropy_common.c +184 -80
  15. data/ext/zstdruby/libzstd/common/error_private.c +11 -2
  16. data/ext/zstdruby/libzstd/common/error_private.h +87 -4
  17. data/ext/zstdruby/libzstd/common/fse.h +47 -116
  18. data/ext/zstdruby/libzstd/common/fse_decompress.c +127 -127
  19. data/ext/zstdruby/libzstd/common/huf.h +112 -197
  20. data/ext/zstdruby/libzstd/common/mem.h +124 -142
  21. data/ext/zstdruby/libzstd/common/pool.c +54 -27
  22. data/ext/zstdruby/libzstd/common/pool.h +11 -5
  23. data/ext/zstdruby/libzstd/common/portability_macros.h +156 -0
  24. data/ext/zstdruby/libzstd/common/threading.c +78 -22
  25. data/ext/zstdruby/libzstd/common/threading.h +9 -13
  26. data/ext/zstdruby/libzstd/common/xxhash.c +15 -873
  27. data/ext/zstdruby/libzstd/common/xxhash.h +5572 -191
  28. data/ext/zstdruby/libzstd/common/zstd_common.c +2 -37
  29. data/ext/zstdruby/libzstd/common/zstd_deps.h +111 -0
  30. data/ext/zstdruby/libzstd/common/zstd_internal.h +186 -144
  31. data/ext/zstdruby/libzstd/common/zstd_trace.h +163 -0
  32. data/ext/zstdruby/libzstd/compress/clevels.h +134 -0
  33. data/ext/zstdruby/libzstd/compress/fse_compress.c +99 -196
  34. data/ext/zstdruby/libzstd/compress/hist.c +41 -63
  35. data/ext/zstdruby/libzstd/compress/hist.h +13 -33
  36. data/ext/zstdruby/libzstd/compress/huf_compress.c +968 -331
  37. data/ext/zstdruby/libzstd/compress/zstd_compress.c +4120 -1191
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +688 -159
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +121 -40
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +16 -6
  41. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +62 -35
  42. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +10 -3
  43. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +577 -0
  44. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
  45. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +322 -115
  46. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +394 -154
  47. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +4 -3
  48. data/ext/zstdruby/libzstd/compress/zstd_fast.c +729 -253
  49. data/ext/zstdruby/libzstd/compress/zstd_fast.h +4 -3
  50. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +1289 -247
  51. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +61 -1
  52. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +339 -212
  53. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +15 -3
  54. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +106 -0
  55. data/ext/zstdruby/libzstd/compress/zstd_opt.c +508 -282
  56. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  57. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +217 -466
  58. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +35 -114
  59. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +1220 -572
  60. data/ext/zstdruby/libzstd/decompress/huf_decompress_amd64.S +576 -0
  61. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +23 -19
  62. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +3 -3
  63. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +859 -273
  64. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +1244 -375
  65. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +21 -7
  66. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +74 -11
  67. data/ext/zstdruby/libzstd/dictBuilder/cover.c +75 -54
  68. data/ext/zstdruby/libzstd/dictBuilder/cover.h +20 -9
  69. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1 -1
  70. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +55 -36
  71. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +126 -110
  72. data/ext/zstdruby/libzstd/{dictBuilder/zdict.h → zdict.h} +248 -56
  73. data/ext/zstdruby/libzstd/zstd.h +1277 -306
  74. data/ext/zstdruby/libzstd/{common/zstd_errors.h → zstd_errors.h} +29 -8
  75. data/ext/zstdruby/main.c +20 -0
  76. data/ext/zstdruby/skippable_frame.c +63 -0
  77. data/ext/zstdruby/streaming_compress.c +177 -0
  78. data/ext/zstdruby/streaming_compress.h +5 -0
  79. data/ext/zstdruby/streaming_decompress.c +123 -0
  80. data/ext/zstdruby/zstdruby.c +114 -32
  81. data/lib/zstd-ruby/version.rb +1 -1
  82. data/lib/zstd-ruby.rb +0 -1
  83. data/zstd-ruby.gemspec +1 -1
  84. metadata +24 -39
  85. data/.travis.yml +0 -14
  86. data/ext/zstdruby/libzstd/.gitignore +0 -3
  87. data/ext/zstdruby/libzstd/BUCK +0 -234
  88. data/ext/zstdruby/libzstd/Makefile +0 -289
  89. data/ext/zstdruby/libzstd/README.md +0 -159
  90. data/ext/zstdruby/libzstd/deprecated/zbuff.h +0 -214
  91. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +0 -26
  92. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +0 -147
  93. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +0 -75
  94. data/ext/zstdruby/libzstd/dll/example/Makefile +0 -47
  95. data/ext/zstdruby/libzstd/dll/example/README.md +0 -69
  96. data/ext/zstdruby/libzstd/dll/example/build_package.bat +0 -20
  97. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.sln +0 -25
  98. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj +0 -181
  99. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +0 -415
  100. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +0 -2152
  101. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +0 -94
  102. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +0 -3514
  103. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +0 -93
  104. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +0 -3156
  105. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +0 -93
  106. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +0 -3641
  107. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +0 -142
  108. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +0 -4046
  109. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +0 -162
  110. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +0 -4150
  111. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +0 -172
  112. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +0 -4533
  113. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +0 -187
  114. data/ext/zstdruby/libzstd/libzstd.pc.in +0 -15
  115. data/ext/zstdruby/zstdruby.h +0 -6
@@ -1,35 +1,15 @@
1
1
  /* ******************************************************************
2
- bitstream
3
- Part of FSE library
4
- Copyright (C) 2013-present, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
-
12
- * Redistributions of source code must retain the above copyright
13
- notice, this list of conditions and the following disclaimer.
14
- * Redistributions in binary form must reproduce the above
15
- copyright notice, this list of conditions and the following disclaimer
16
- in the documentation and/or other materials provided with the
17
- distribution.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
-
31
- You can contact the author at :
32
- - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
2
+ * bitstream
3
+ * Part of FSE library
4
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
5
+ *
6
+ * You can contact the author at :
7
+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
8
+ *
9
+ * This source code is licensed under both the BSD-style license (found in the
10
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11
+ * in the COPYING file in the root directory of this source tree).
12
+ * You may select, at your option, one of the above-listed licenses.
33
13
  ****************************************************************** */
34
14
  #ifndef BITSTREAM_H_MODULE
35
15
  #define BITSTREAM_H_MODULE
@@ -37,7 +17,6 @@
37
17
  #if defined (__cplusplus)
38
18
  extern "C" {
39
19
  #endif
40
-
41
20
  /*
42
21
  * This API consists of small unitary functions, which must be inlined for best performance.
43
22
  * Since link-time-optimization is not available for all compilers,
@@ -48,17 +27,21 @@ extern "C" {
48
27
  * Dependencies
49
28
  ******************************************/
50
29
  #include "mem.h" /* unaligned access routines */
30
+ #include "compiler.h" /* UNLIKELY() */
51
31
  #include "debug.h" /* assert(), DEBUGLOG(), RAWLOG() */
52
32
  #include "error_private.h" /* error codes and messages */
33
+ #include "bits.h" /* ZSTD_highbit32 */
53
34
 
54
35
 
55
36
  /*=========================================
56
37
  * Target specific
57
38
  =========================================*/
58
- #if defined(__BMI__) && defined(__GNUC__)
59
- # include <immintrin.h> /* support for bextr (experimental) */
60
- #elif defined(__ICCARM__)
61
- # include <intrinsics.h>
39
+ #ifndef ZSTD_NO_INTRINSICS
40
+ # if (defined(__BMI__) || defined(__BMI2__)) && defined(__GNUC__)
41
+ # include <immintrin.h> /* support for bextr (experimental)/bzhi */
42
+ # elif defined(__ICCARM__)
43
+ # include <intrinsics.h>
44
+ # endif
62
45
  #endif
63
46
 
64
47
  #define STREAM_ACCUMULATOR_MIN_32 25
@@ -150,39 +133,6 @@ MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
150
133
  MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
151
134
  /* faster, but works only if nbBits >= 1 */
152
135
 
153
-
154
-
155
- /*-**************************************************************
156
- * Internal functions
157
- ****************************************************************/
158
- MEM_STATIC unsigned BIT_highbit32 (U32 val)
159
- {
160
- assert(val != 0);
161
- {
162
- # if defined(_MSC_VER) /* Visual */
163
- unsigned long r=0;
164
- _BitScanReverse ( &r, val );
165
- return (unsigned) r;
166
- # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
167
- return __builtin_clz (val) ^ 31;
168
- # elif defined(__ICCARM__) /* IAR Intrinsic */
169
- return 31 - __CLZ(val);
170
- # else /* Software version */
171
- static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29,
172
- 11, 14, 16, 18, 22, 25, 3, 30,
173
- 8, 12, 20, 28, 15, 17, 24, 7,
174
- 19, 27, 23, 6, 26, 5, 4, 31 };
175
- U32 v = val;
176
- v |= v >> 1;
177
- v |= v >> 2;
178
- v |= v >> 4;
179
- v |= v >> 8;
180
- v |= v >> 16;
181
- return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
182
- # endif
183
- }
184
- }
185
-
186
136
  /*===== Local Constants =====*/
187
137
  static const unsigned BIT_mask[] = {
188
138
  0, 1, 3, 7, 0xF, 0x1F,
@@ -212,16 +162,26 @@ MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC,
212
162
  return 0;
213
163
  }
214
164
 
165
+ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
166
+ {
167
+ #if defined(STATIC_BMI2) && STATIC_BMI2 == 1 && !defined(ZSTD_NO_INTRINSICS)
168
+ return _bzhi_u64(bitContainer, nbBits);
169
+ #else
170
+ assert(nbBits < BIT_MASK_SIZE);
171
+ return bitContainer & BIT_mask[nbBits];
172
+ #endif
173
+ }
174
+
215
175
  /*! BIT_addBits() :
216
176
  * can add up to 31 bits into `bitC`.
217
177
  * Note : does not check for register overflow ! */
218
178
  MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC,
219
179
  size_t value, unsigned nbBits)
220
180
  {
221
- MEM_STATIC_ASSERT(BIT_MASK_SIZE == 32);
181
+ DEBUG_STATIC_ASSERT(BIT_MASK_SIZE == 32);
222
182
  assert(nbBits < BIT_MASK_SIZE);
223
183
  assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);
224
- bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
184
+ bitC->bitContainer |= BIT_getLowerBits(value, nbBits) << bitC->bitPos;
225
185
  bitC->bitPos += nbBits;
226
186
  }
227
187
 
@@ -291,7 +251,7 @@ MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
291
251
  */
292
252
  MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
293
253
  {
294
- if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
254
+ if (srcSize < 1) { ZSTD_memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
295
255
 
296
256
  bitD->start = (const char*)srcBuffer;
297
257
  bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer);
@@ -300,7 +260,7 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
300
260
  bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);
301
261
  bitD->bitContainer = MEM_readLEST(bitD->ptr);
302
262
  { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
303
- bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
263
+ bitD->bitsConsumed = lastByte ? 8 - ZSTD_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
304
264
  if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
305
265
  } else {
306
266
  bitD->ptr = bitD->start;
@@ -308,27 +268,27 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
308
268
  switch(srcSize)
309
269
  {
310
270
  case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
311
- /* fall-through */
271
+ ZSTD_FALLTHROUGH;
312
272
 
313
273
  case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
314
- /* fall-through */
274
+ ZSTD_FALLTHROUGH;
315
275
 
316
276
  case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
317
- /* fall-through */
277
+ ZSTD_FALLTHROUGH;
318
278
 
319
279
  case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
320
- /* fall-through */
280
+ ZSTD_FALLTHROUGH;
321
281
 
322
282
  case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
323
- /* fall-through */
283
+ ZSTD_FALLTHROUGH;
324
284
 
325
285
  case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
326
- /* fall-through */
286
+ ZSTD_FALLTHROUGH;
327
287
 
328
288
  default: break;
329
289
  }
330
290
  { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
331
- bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;
291
+ bitD->bitsConsumed = lastByte ? 8 - ZSTD_highbit32(lastByte) : 0;
332
292
  if (lastByte == 0) return ERROR(corruption_detected); /* endMark not present */
333
293
  }
334
294
  bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;
@@ -337,23 +297,26 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
337
297
  return srcSize;
338
298
  }
339
299
 
340
- MEM_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
300
+ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
341
301
  {
342
302
  return bitContainer >> start;
343
303
  }
344
304
 
345
- MEM_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
305
+ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
346
306
  {
347
307
  U32 const regMask = sizeof(bitContainer)*8 - 1;
348
308
  /* if start > regMask, bitstream is corrupted, and result is undefined */
349
309
  assert(nbBits < BIT_MASK_SIZE);
310
+ /* x86 transform & ((1 << nbBits) - 1) to bzhi instruction, it is better
311
+ * than accessing memory. When bmi2 instruction is not present, we consider
312
+ * such cpus old (pre-Haswell, 2013) and their performance is not of that
313
+ * importance.
314
+ */
315
+ #if defined(__x86_64__) || defined(_M_X86)
316
+ return (bitContainer >> (start & regMask)) & ((((U64)1) << nbBits) - 1);
317
+ #else
350
318
  return (bitContainer >> (start & regMask)) & BIT_mask[nbBits];
351
- }
352
-
353
- MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
354
- {
355
- assert(nbBits < BIT_MASK_SIZE);
356
- return bitContainer & BIT_mask[nbBits];
319
+ #endif
357
320
  }
358
321
 
359
322
  /*! BIT_lookBits() :
@@ -362,7 +325,7 @@ MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
362
325
  * On 32-bits, maxNbBits==24.
363
326
  * On 64-bits, maxNbBits==56.
364
327
  * @return : value extracted */
365
- MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
328
+ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
366
329
  {
367
330
  /* arbitrate between double-shift and shift+mask */
368
331
  #if 1
@@ -385,7 +348,7 @@ MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
385
348
  return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> (((regMask+1)-nbBits) & regMask);
386
349
  }
387
350
 
388
- MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
351
+ MEM_STATIC FORCE_INLINE_ATTR void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
389
352
  {
390
353
  bitD->bitsConsumed += nbBits;
391
354
  }
@@ -394,7 +357,7 @@ MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
394
357
  * Read (consume) next n bits from local register and update.
395
358
  * Pay attention to not read more than nbBits contained into local register.
396
359
  * @return : extracted value. */
397
- MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits)
360
+ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits)
398
361
  {
399
362
  size_t const value = BIT_lookBits(bitD, nbBits);
400
363
  BIT_skipBits(bitD, nbBits);
@@ -402,7 +365,7 @@ MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits)
402
365
  }
403
366
 
404
367
  /*! BIT_readBitsFast() :
405
- * unsafe version; only works only if nbBits >= 1 */
368
+ * unsafe version; only works if nbBits >= 1 */
406
369
  MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits)
407
370
  {
408
371
  size_t const value = BIT_lookBitsFast(bitD, nbBits);
@@ -411,21 +374,35 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits)
411
374
  return value;
412
375
  }
413
376
 
377
+ /*! BIT_reloadDStreamFast() :
378
+ * Similar to BIT_reloadDStream(), but with two differences:
379
+ * 1. bitsConsumed <= sizeof(bitD->bitContainer)*8 must hold!
380
+ * 2. Returns BIT_DStream_overflow when bitD->ptr < bitD->limitPtr, at this
381
+ * point you must use BIT_reloadDStream() to reload.
382
+ */
383
+ MEM_STATIC BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD)
384
+ {
385
+ if (UNLIKELY(bitD->ptr < bitD->limitPtr))
386
+ return BIT_DStream_overflow;
387
+ assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer)*8);
388
+ bitD->ptr -= bitD->bitsConsumed >> 3;
389
+ bitD->bitsConsumed &= 7;
390
+ bitD->bitContainer = MEM_readLEST(bitD->ptr);
391
+ return BIT_DStream_unfinished;
392
+ }
393
+
414
394
  /*! BIT_reloadDStream() :
415
395
  * Refill `bitD` from buffer previously set in BIT_initDStream() .
416
396
  * This function is safe, it guarantees it will not read beyond src buffer.
417
397
  * @return : status of `BIT_DStream_t` internal register.
418
398
  * when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */
419
- MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
399
+ MEM_STATIC FORCE_INLINE_ATTR BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
420
400
  {
421
401
  if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* overflow detected, like end of stream */
422
402
  return BIT_DStream_overflow;
423
403
 
424
404
  if (bitD->ptr >= bitD->limitPtr) {
425
- bitD->ptr -= bitD->bitsConsumed >> 3;
426
- bitD->bitsConsumed &= 7;
427
- bitD->bitContainer = MEM_readLEST(bitD->ptr);
428
- return BIT_DStream_unfinished;
405
+ return BIT_reloadDStreamFast(bitD);
429
406
  }
430
407
  if (bitD->ptr == bitD->start) {
431
408
  if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, 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
@@ -11,13 +11,15 @@
11
11
  #ifndef ZSTD_COMPILER_H
12
12
  #define ZSTD_COMPILER_H
13
13
 
14
+ #include "portability_macros.h"
15
+
14
16
  /*-*******************************************************
15
17
  * Compiler specifics
16
18
  *********************************************************/
17
19
  /* force inlining */
18
20
 
19
21
  #if !defined(ZSTD_NO_INLINE)
20
- #if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
22
+ #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
21
23
  # define INLINE_KEYWORD inline
22
24
  #else
23
25
  # define INLINE_KEYWORD
@@ -38,6 +40,17 @@
38
40
 
39
41
  #endif
40
42
 
43
+ /**
44
+ On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC).
45
+ This explicitly marks such functions as __cdecl so that the code will still compile
46
+ if a CC other than __cdecl has been made the default.
47
+ */
48
+ #if defined(_MSC_VER)
49
+ # define WIN_CDECL __cdecl
50
+ #else
51
+ # define WIN_CDECL
52
+ #endif
53
+
41
54
  /**
42
55
  * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
43
56
  * parameters. They must be inlined for the compiler to eliminate the constant
@@ -79,30 +92,19 @@
79
92
  # endif
80
93
  #endif
81
94
 
95
+
82
96
  /* target attribute */
83
- #ifndef __has_attribute
84
- #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */
85
- #endif
86
97
  #if defined(__GNUC__) || defined(__ICCARM__)
87
98
  # define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
88
99
  #else
89
100
  # define TARGET_ATTRIBUTE(target)
90
101
  #endif
91
102
 
92
- /* Enable runtime BMI2 dispatch based on the CPU.
93
- * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
103
+ /* Target attribute for BMI2 dynamic dispatch.
104
+ * Enable lzcnt, bmi, and bmi2.
105
+ * We test for bmi1 & bmi2. lzcnt is included in bmi1.
94
106
  */
95
- #ifndef DYNAMIC_BMI2
96
- #if ((defined(__clang__) && __has_attribute(__target__)) \
97
- || (defined(__GNUC__) \
98
- && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
99
- && (defined(__x86_64__) || defined(_M_X86)) \
100
- && !defined(__BMI2__)
101
- # define DYNAMIC_BMI2 1
102
- #else
103
- # define DYNAMIC_BMI2 0
104
- #endif
105
- #endif
107
+ #define BMI2_TARGET_ATTRIBUTE TARGET_ATTRIBUTE("lzcnt,bmi,bmi2")
106
108
 
107
109
  /* prefetch
108
110
  * can be disabled, by declaring NO_PREFETCH build macro */
@@ -117,6 +119,9 @@
117
119
  # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
118
120
  # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
119
121
  # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
122
+ # elif defined(__aarch64__)
123
+ # define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
124
+ # define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
120
125
  # else
121
126
  # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
122
127
  # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
@@ -135,8 +140,9 @@
135
140
  }
136
141
 
137
142
  /* vectorization
138
- * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */
139
- #if !defined(__clang__) && defined(__GNUC__)
143
+ * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax,
144
+ * and some compilers, like Intel ICC and MCST LCC, do not support it at all. */
145
+ #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) && !defined(__LCC__)
140
146
  # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
141
147
  # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
142
148
  # else
@@ -146,6 +152,25 @@
146
152
  # define DONT_VECTORIZE
147
153
  #endif
148
154
 
155
+ /* Tell the compiler that a branch is likely or unlikely.
156
+ * Only use these macros if it causes the compiler to generate better code.
157
+ * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc
158
+ * and clang, please do.
159
+ */
160
+ #if defined(__GNUC__)
161
+ #define LIKELY(x) (__builtin_expect((x), 1))
162
+ #define UNLIKELY(x) (__builtin_expect((x), 0))
163
+ #else
164
+ #define LIKELY(x) (x)
165
+ #define UNLIKELY(x) (x)
166
+ #endif
167
+
168
+ #if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
169
+ # define ZSTD_UNREACHABLE { assert(0), __builtin_unreachable(); }
170
+ #else
171
+ # define ZSTD_UNREACHABLE { assert(0); }
172
+ #endif
173
+
149
174
  /* disable warnings */
150
175
  #ifdef _MSC_VER /* Visual Studio */
151
176
  # include <intrin.h> /* For Visual 2005 */
@@ -156,4 +181,178 @@
156
181
  # pragma warning(disable : 4324) /* disable: C4324: padded structure */
157
182
  #endif
158
183
 
184
+ /*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/
185
+ #ifndef STATIC_BMI2
186
+ # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86))
187
+ # ifdef __AVX2__ //MSVC does not have a BMI2 specific flag, but every CPU that supports AVX2 also supports BMI2
188
+ # define STATIC_BMI2 1
189
+ # endif
190
+ # elif defined(__BMI2__) && defined(__x86_64__) && defined(__GNUC__)
191
+ # define STATIC_BMI2 1
192
+ # endif
193
+ #endif
194
+
195
+ #ifndef STATIC_BMI2
196
+ #define STATIC_BMI2 0
197
+ #endif
198
+
199
+ /* compile time determination of SIMD support */
200
+ #if !defined(ZSTD_NO_INTRINSICS)
201
+ # if defined(__SSE2__) || defined(_M_AMD64) || (defined (_M_IX86) && defined(_M_IX86_FP) && (_M_IX86_FP >= 2))
202
+ # define ZSTD_ARCH_X86_SSE2
203
+ # endif
204
+ # if defined(__ARM_NEON) || defined(_M_ARM64)
205
+ # define ZSTD_ARCH_ARM_NEON
206
+ # endif
207
+ #
208
+ # if defined(ZSTD_ARCH_X86_SSE2)
209
+ # include <emmintrin.h>
210
+ # elif defined(ZSTD_ARCH_ARM_NEON)
211
+ # include <arm_neon.h>
212
+ # endif
213
+ #endif
214
+
215
+ /* C-language Attributes are added in C23. */
216
+ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)
217
+ # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
218
+ #else
219
+ # define ZSTD_HAS_C_ATTRIBUTE(x) 0
220
+ #endif
221
+
222
+ /* Only use C++ attributes in C++. Some compilers report support for C++
223
+ * attributes when compiling with C.
224
+ */
225
+ #if defined(__cplusplus) && defined(__has_cpp_attribute)
226
+ # define ZSTD_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
227
+ #else
228
+ # define ZSTD_HAS_CPP_ATTRIBUTE(x) 0
229
+ #endif
230
+
231
+ /* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute.
232
+ * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough
233
+ * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough
234
+ * - Else: __attribute__((__fallthrough__))
235
+ */
236
+ #ifndef ZSTD_FALLTHROUGH
237
+ # if ZSTD_HAS_C_ATTRIBUTE(fallthrough)
238
+ # define ZSTD_FALLTHROUGH [[fallthrough]]
239
+ # elif ZSTD_HAS_CPP_ATTRIBUTE(fallthrough)
240
+ # define ZSTD_FALLTHROUGH [[fallthrough]]
241
+ # elif __has_attribute(__fallthrough__)
242
+ /* Leading semicolon is to satisfy gcc-11 with -pedantic. Without the semicolon
243
+ * gcc complains about: a label can only be part of a statement and a declaration is not a statement.
244
+ */
245
+ # define ZSTD_FALLTHROUGH ; __attribute__((__fallthrough__))
246
+ # else
247
+ # define ZSTD_FALLTHROUGH
248
+ # endif
249
+ #endif
250
+
251
+ /*-**************************************************************
252
+ * Alignment check
253
+ *****************************************************************/
254
+
255
+ /* this test was initially positioned in mem.h,
256
+ * but this file is removed (or replaced) for linux kernel
257
+ * so it's now hosted in compiler.h,
258
+ * which remains valid for both user & kernel spaces.
259
+ */
260
+
261
+ #ifndef ZSTD_ALIGNOF
262
+ # if defined(__GNUC__) || defined(_MSC_VER)
263
+ /* covers gcc, clang & MSVC */
264
+ /* note : this section must come first, before C11,
265
+ * due to a limitation in the kernel source generator */
266
+ # define ZSTD_ALIGNOF(T) __alignof(T)
267
+
268
+ # elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
269
+ /* C11 support */
270
+ # include <stdalign.h>
271
+ # define ZSTD_ALIGNOF(T) alignof(T)
272
+
273
+ # else
274
+ /* No known support for alignof() - imperfect backup */
275
+ # define ZSTD_ALIGNOF(T) (sizeof(void*) < sizeof(T) ? sizeof(void*) : sizeof(T))
276
+
277
+ # endif
278
+ #endif /* ZSTD_ALIGNOF */
279
+
280
+ /*-**************************************************************
281
+ * Sanitizer
282
+ *****************************************************************/
283
+
284
+ /* Issue #3240 reports an ASAN failure on an llvm-mingw build. Out of an
285
+ * abundance of caution, disable our custom poisoning on mingw. */
286
+ #ifdef __MINGW32__
287
+ #ifndef ZSTD_ASAN_DONT_POISON_WORKSPACE
288
+ #define ZSTD_ASAN_DONT_POISON_WORKSPACE 1
289
+ #endif
290
+ #ifndef ZSTD_MSAN_DONT_POISON_WORKSPACE
291
+ #define ZSTD_MSAN_DONT_POISON_WORKSPACE 1
292
+ #endif
293
+ #endif
294
+
295
+ #if ZSTD_MEMORY_SANITIZER && !defined(ZSTD_MSAN_DONT_POISON_WORKSPACE)
296
+ /* Not all platforms that support msan provide sanitizers/msan_interface.h.
297
+ * We therefore declare the functions we need ourselves, rather than trying to
298
+ * include the header file... */
299
+ #include <stddef.h> /* size_t */
300
+ #define ZSTD_DEPS_NEED_STDINT
301
+ #include "zstd_deps.h" /* intptr_t */
302
+
303
+ /* Make memory region fully initialized (without changing its contents). */
304
+ void __msan_unpoison(const volatile void *a, size_t size);
305
+
306
+ /* Make memory region fully uninitialized (without changing its contents).
307
+ This is a legacy interface that does not update origin information. Use
308
+ __msan_allocated_memory() instead. */
309
+ void __msan_poison(const volatile void *a, size_t size);
310
+
311
+ /* Returns the offset of the first (at least partially) poisoned byte in the
312
+ memory range, or -1 if the whole range is good. */
313
+ intptr_t __msan_test_shadow(const volatile void *x, size_t size);
314
+
315
+ /* Print shadow and origin for the memory range to stderr in a human-readable
316
+ format. */
317
+ void __msan_print_shadow(const volatile void *x, size_t size);
318
+ #endif
319
+
320
+ #if ZSTD_ADDRESS_SANITIZER && !defined(ZSTD_ASAN_DONT_POISON_WORKSPACE)
321
+ /* Not all platforms that support asan provide sanitizers/asan_interface.h.
322
+ * We therefore declare the functions we need ourselves, rather than trying to
323
+ * include the header file... */
324
+ #include <stddef.h> /* size_t */
325
+
326
+ /**
327
+ * Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable.
328
+ *
329
+ * This memory must be previously allocated by your program. Instrumented
330
+ * code is forbidden from accessing addresses in this region until it is
331
+ * unpoisoned. This function is not guaranteed to poison the entire region -
332
+ * it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan
333
+ * alignment restrictions.
334
+ *
335
+ * \note This function is not thread-safe because no two threads can poison or
336
+ * unpoison memory in the same memory region simultaneously.
337
+ *
338
+ * \param addr Start of memory region.
339
+ * \param size Size of memory region. */
340
+ void __asan_poison_memory_region(void const volatile *addr, size_t size);
341
+
342
+ /**
343
+ * Marks a memory region (<c>[addr, addr+size)</c>) as addressable.
344
+ *
345
+ * This memory must be previously allocated by your program. Accessing
346
+ * addresses in this region is allowed until this region is poisoned again.
347
+ * This function could unpoison a super-region of <c>[addr, addr+size)</c> due
348
+ * to ASan alignment restrictions.
349
+ *
350
+ * \note This function is not thread-safe because no two threads can
351
+ * poison or unpoison memory in the same memory region simultaneously.
352
+ *
353
+ * \param addr Start of memory region.
354
+ * \param size Size of memory region. */
355
+ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
356
+ #endif
357
+
159
358
  #endif /* ZSTD_COMPILER_H */
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2018-present, 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
@@ -16,8 +16,6 @@
16
16
  * https://github.com/facebook/folly/blob/master/folly/CpuId.h
17
17
  */
18
18
 
19
- #include <string.h>
20
-
21
19
  #include "mem.h"
22
20
 
23
21
  #ifdef _MSC_VER
@@ -1,35 +1,15 @@
1
1
  /* ******************************************************************
2
- debug
3
- Part of FSE library
4
- Copyright (C) 2013-present, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
-
12
- * Redistributions of source code must retain the above copyright
13
- notice, this list of conditions and the following disclaimer.
14
- * Redistributions in binary form must reproduce the above
15
- copyright notice, this list of conditions and the following disclaimer
16
- in the documentation and/or other materials provided with the
17
- distribution.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
-
31
- You can contact the author at :
32
- - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
2
+ * debug
3
+ * Part of FSE library
4
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
5
+ *
6
+ * You can contact the author at :
7
+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
8
+ *
9
+ * This source code is licensed under both the BSD-style license (found in the
10
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11
+ * in the COPYING file in the root directory of this source tree).
12
+ * You may select, at your option, one of the above-listed licenses.
33
13
  ****************************************************************** */
34
14
 
35
15