zstd-ruby 1.4.4.0 → 1.5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +8 -0
  3. data/.github/workflows/ruby.yml +35 -0
  4. data/README.md +2 -2
  5. data/ext/zstdruby/extconf.rb +1 -0
  6. data/ext/zstdruby/libzstd/BUCK +5 -7
  7. data/ext/zstdruby/libzstd/Makefile +241 -173
  8. data/ext/zstdruby/libzstd/README.md +76 -18
  9. data/ext/zstdruby/libzstd/common/bitstream.h +75 -57
  10. data/ext/zstdruby/libzstd/common/compiler.h +196 -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 +208 -76
  15. data/ext/zstdruby/libzstd/common/error_private.c +3 -1
  16. data/ext/zstdruby/libzstd/common/error_private.h +87 -4
  17. data/ext/zstdruby/libzstd/common/fse.h +51 -42
  18. data/ext/zstdruby/libzstd/common/fse_decompress.c +149 -57
  19. data/ext/zstdruby/libzstd/common/huf.h +60 -54
  20. data/ext/zstdruby/libzstd/common/mem.h +87 -98
  21. data/ext/zstdruby/libzstd/common/pool.c +23 -17
  22. data/ext/zstdruby/libzstd/common/pool.h +3 -3
  23. data/ext/zstdruby/libzstd/common/portability_macros.h +131 -0
  24. data/ext/zstdruby/libzstd/common/threading.c +10 -8
  25. data/ext/zstdruby/libzstd/common/threading.h +4 -3
  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 +10 -10
  29. data/ext/zstdruby/libzstd/common/zstd_deps.h +111 -0
  30. data/ext/zstdruby/libzstd/common/zstd_internal.h +252 -108
  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 +105 -85
  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 +831 -259
  37. data/ext/zstdruby/libzstd/compress/zstd_compress.c +3213 -1007
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +493 -71
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +21 -16
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +4 -2
  41. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +51 -24
  42. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +10 -3
  43. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +573 -0
  44. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
  45. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +208 -81
  46. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +315 -137
  47. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +2 -2
  48. data/ext/zstdruby/libzstd/compress/zstd_fast.c +319 -128
  49. data/ext/zstdruby/libzstd/compress/zstd_fast.h +2 -2
  50. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +1156 -171
  51. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +59 -1
  52. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +331 -206
  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 +403 -226
  56. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  57. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +188 -453
  58. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +32 -114
  59. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +1065 -410
  60. data/ext/zstdruby/libzstd/decompress/huf_decompress_amd64.S +571 -0
  61. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +20 -16
  62. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +3 -3
  63. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +691 -230
  64. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +1072 -323
  65. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +16 -7
  66. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +71 -10
  67. data/ext/zstdruby/libzstd/deprecated/zbuff.h +3 -3
  68. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +2 -2
  69. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +24 -4
  70. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -1
  71. data/ext/zstdruby/libzstd/dictBuilder/cover.c +57 -40
  72. data/ext/zstdruby/libzstd/dictBuilder/cover.h +20 -9
  73. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1 -1
  74. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +54 -35
  75. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +151 -57
  76. data/ext/zstdruby/libzstd/dll/example/Makefile +2 -1
  77. data/ext/zstdruby/libzstd/dll/example/README.md +16 -22
  78. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +4 -4
  79. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +25 -19
  80. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +1 -1
  81. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +18 -14
  82. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +1 -1
  83. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +18 -14
  84. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +1 -1
  85. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +22 -16
  86. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +1 -1
  87. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +29 -25
  88. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +2 -2
  89. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +29 -25
  90. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +1 -1
  91. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +34 -26
  92. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +1 -1
  93. data/ext/zstdruby/libzstd/libzstd.mk +185 -0
  94. data/ext/zstdruby/libzstd/libzstd.pc.in +4 -3
  95. data/ext/zstdruby/libzstd/modulemap/module.modulemap +4 -0
  96. data/ext/zstdruby/libzstd/{dictBuilder/zdict.h → zdict.h} +201 -31
  97. data/ext/zstdruby/libzstd/zstd.h +760 -234
  98. data/ext/zstdruby/libzstd/{common/zstd_errors.h → zstd_errors.h} +3 -1
  99. data/ext/zstdruby/zstdruby.c +2 -2
  100. data/lib/zstd-ruby/version.rb +1 -1
  101. metadata +20 -9
  102. data/.travis.yml +0 -14
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -13,8 +13,8 @@
13
13
  /*-*************************************
14
14
  * Dependencies
15
15
  ***************************************/
16
- #include <stdlib.h> /* malloc, calloc, free */
17
- #include <string.h> /* memset */
16
+ #define ZSTD_DEPS_NEED_MALLOC
17
+ #include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
18
18
  #include "error_private.h"
19
19
  #include "zstd_internal.h"
20
20
 
@@ -53,31 +53,31 @@ const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString
53
53
  /*=**************************************************************
54
54
  * Custom allocator
55
55
  ****************************************************************/
56
- void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
56
+ void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
57
57
  {
58
58
  if (customMem.customAlloc)
59
59
  return customMem.customAlloc(customMem.opaque, size);
60
- return malloc(size);
60
+ return ZSTD_malloc(size);
61
61
  }
62
62
 
63
- void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
63
+ void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
64
64
  {
65
65
  if (customMem.customAlloc) {
66
66
  /* calloc implemented as malloc+memset;
67
67
  * not as efficient as calloc, but next best guess for custom malloc */
68
68
  void* const ptr = customMem.customAlloc(customMem.opaque, size);
69
- memset(ptr, 0, size);
69
+ ZSTD_memset(ptr, 0, size);
70
70
  return ptr;
71
71
  }
72
- return calloc(1, size);
72
+ return ZSTD_calloc(1, size);
73
73
  }
74
74
 
75
- void ZSTD_free(void* ptr, ZSTD_customMem customMem)
75
+ void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
76
76
  {
77
77
  if (ptr!=NULL) {
78
78
  if (customMem.customFree)
79
79
  customMem.customFree(customMem.opaque, ptr);
80
80
  else
81
- free(ptr);
81
+ ZSTD_free(ptr);
82
82
  }
83
83
  }
@@ -0,0 +1,111 @@
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
+ /* This file provides common libc dependencies that zstd requires.
12
+ * The purpose is to allow replacing this file with a custom implementation
13
+ * to compile zstd without libc support.
14
+ */
15
+
16
+ /* Need:
17
+ * NULL
18
+ * INT_MAX
19
+ * UINT_MAX
20
+ * ZSTD_memcpy()
21
+ * ZSTD_memset()
22
+ * ZSTD_memmove()
23
+ */
24
+ #ifndef ZSTD_DEPS_COMMON
25
+ #define ZSTD_DEPS_COMMON
26
+
27
+ #include <limits.h>
28
+ #include <stddef.h>
29
+ #include <string.h>
30
+
31
+ #if defined(__GNUC__) && __GNUC__ >= 4
32
+ # define ZSTD_memcpy(d,s,l) __builtin_memcpy((d),(s),(l))
33
+ # define ZSTD_memmove(d,s,l) __builtin_memmove((d),(s),(l))
34
+ # define ZSTD_memset(p,v,l) __builtin_memset((p),(v),(l))
35
+ #else
36
+ # define ZSTD_memcpy(d,s,l) memcpy((d),(s),(l))
37
+ # define ZSTD_memmove(d,s,l) memmove((d),(s),(l))
38
+ # define ZSTD_memset(p,v,l) memset((p),(v),(l))
39
+ #endif
40
+
41
+ #endif /* ZSTD_DEPS_COMMON */
42
+
43
+ /* Need:
44
+ * ZSTD_malloc()
45
+ * ZSTD_free()
46
+ * ZSTD_calloc()
47
+ */
48
+ #ifdef ZSTD_DEPS_NEED_MALLOC
49
+ #ifndef ZSTD_DEPS_MALLOC
50
+ #define ZSTD_DEPS_MALLOC
51
+
52
+ #include <stdlib.h>
53
+
54
+ #define ZSTD_malloc(s) malloc(s)
55
+ #define ZSTD_calloc(n,s) calloc((n), (s))
56
+ #define ZSTD_free(p) free((p))
57
+
58
+ #endif /* ZSTD_DEPS_MALLOC */
59
+ #endif /* ZSTD_DEPS_NEED_MALLOC */
60
+
61
+ /*
62
+ * Provides 64-bit math support.
63
+ * Need:
64
+ * U64 ZSTD_div64(U64 dividend, U32 divisor)
65
+ */
66
+ #ifdef ZSTD_DEPS_NEED_MATH64
67
+ #ifndef ZSTD_DEPS_MATH64
68
+ #define ZSTD_DEPS_MATH64
69
+
70
+ #define ZSTD_div64(dividend, divisor) ((dividend) / (divisor))
71
+
72
+ #endif /* ZSTD_DEPS_MATH64 */
73
+ #endif /* ZSTD_DEPS_NEED_MATH64 */
74
+
75
+ /* Need:
76
+ * assert()
77
+ */
78
+ #ifdef ZSTD_DEPS_NEED_ASSERT
79
+ #ifndef ZSTD_DEPS_ASSERT
80
+ #define ZSTD_DEPS_ASSERT
81
+
82
+ #include <assert.h>
83
+
84
+ #endif /* ZSTD_DEPS_ASSERT */
85
+ #endif /* ZSTD_DEPS_NEED_ASSERT */
86
+
87
+ /* Need:
88
+ * ZSTD_DEBUG_PRINT()
89
+ */
90
+ #ifdef ZSTD_DEPS_NEED_IO
91
+ #ifndef ZSTD_DEPS_IO
92
+ #define ZSTD_DEPS_IO
93
+
94
+ #include <stdio.h>
95
+ #define ZSTD_DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)
96
+
97
+ #endif /* ZSTD_DEPS_IO */
98
+ #endif /* ZSTD_DEPS_NEED_IO */
99
+
100
+ /* Only requested when <stdint.h> is known to be present.
101
+ * Need:
102
+ * intptr_t
103
+ */
104
+ #ifdef ZSTD_DEPS_NEED_STDINT
105
+ #ifndef ZSTD_DEPS_STDINT
106
+ #define ZSTD_DEPS_STDINT
107
+
108
+ #include <stdint.h>
109
+
110
+ #endif /* ZSTD_DEPS_STDINT */
111
+ #endif /* ZSTD_DEPS_NEED_STDINT */
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -20,11 +20,12 @@
20
20
  * Dependencies
21
21
  ***************************************/
22
22
  #include "compiler.h"
23
+ #include "cpu.h"
23
24
  #include "mem.h"
24
25
  #include "debug.h" /* assert, DEBUGLOG, RAWLOG, g_debuglevel */
25
26
  #include "error_private.h"
26
27
  #define ZSTD_STATIC_LINKING_ONLY
27
- #include "zstd.h"
28
+ #include "../zstd.h"
28
29
  #define FSE_STATIC_LINKING_ONLY
29
30
  #include "fse.h"
30
31
  #define HUF_STATIC_LINKING_ONLY
@@ -33,6 +34,11 @@
33
34
  # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
34
35
  #endif
35
36
  #include "xxhash.h" /* XXH_reset, update, digest */
37
+ #ifndef ZSTD_NO_TRACE
38
+ # include "zstd_trace.h"
39
+ #else
40
+ # define ZSTD_TRACE 0
41
+ #endif
36
42
 
37
43
  #if defined (__cplusplus)
38
44
  extern "C" {
@@ -52,50 +58,7 @@ extern "C" {
52
58
  #undef MAX
53
59
  #define MIN(a,b) ((a)<(b) ? (a) : (b))
54
60
  #define MAX(a,b) ((a)>(b) ? (a) : (b))
55
-
56
- /**
57
- * Return the specified error if the condition evaluates to true.
58
- *
59
- * In debug modes, prints additional information.
60
- * In order to do that (particularly, printing the conditional that failed),
61
- * this can't just wrap RETURN_ERROR().
62
- */
63
- #define RETURN_ERROR_IF(cond, err, ...) \
64
- if (cond) { \
65
- RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", __FILE__, __LINE__, ZSTD_QUOTE(cond), ZSTD_QUOTE(ERROR(err))); \
66
- RAWLOG(3, ": " __VA_ARGS__); \
67
- RAWLOG(3, "\n"); \
68
- return ERROR(err); \
69
- }
70
-
71
- /**
72
- * Unconditionally return the specified error.
73
- *
74
- * In debug modes, prints additional information.
75
- */
76
- #define RETURN_ERROR(err, ...) \
77
- do { \
78
- RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", __FILE__, __LINE__, ZSTD_QUOTE(ERROR(err))); \
79
- RAWLOG(3, ": " __VA_ARGS__); \
80
- RAWLOG(3, "\n"); \
81
- return ERROR(err); \
82
- } while(0);
83
-
84
- /**
85
- * If the provided expression evaluates to an error code, returns that error code.
86
- *
87
- * In debug modes, prints additional information.
88
- */
89
- #define FORWARD_IF_ERROR(err, ...) \
90
- do { \
91
- size_t const err_code = (err); \
92
- if (ERR_isError(err_code)) { \
93
- RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", __FILE__, __LINE__, ZSTD_QUOTE(err), ERR_getErrorName(err_code)); \
94
- RAWLOG(3, ": " __VA_ARGS__); \
95
- RAWLOG(3, "\n"); \
96
- return err_code; \
97
- } \
98
- } while(0);
61
+ #define BOUNDED(min,val,max) (MAX(min,MIN(val,max)))
99
62
 
100
63
 
101
64
  /*-*************************************
@@ -105,7 +68,7 @@ extern "C" {
105
68
 
106
69
  #define ZSTD_REP_NUM 3 /* number of repcodes */
107
70
  #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
108
- static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
71
+ static UNUSED_ATTR const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
109
72
 
110
73
  #define KB *(1 <<10)
111
74
  #define MB *(1 <<20)
@@ -119,15 +82,17 @@ static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
119
82
  #define BIT0 1
120
83
 
121
84
  #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
122
- static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
123
- static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
85
+ static UNUSED_ATTR const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
86
+ static UNUSED_ATTR const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
124
87
 
125
88
  #define ZSTD_FRAMEIDSIZE 4 /* magic number size */
126
89
 
127
90
  #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
128
- static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
91
+ static UNUSED_ATTR const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
129
92
  typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
130
93
 
94
+ #define ZSTD_FRAMECHECKSUMSIZE 4
95
+
131
96
  #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
132
97
  #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
133
98
 
@@ -150,51 +115,88 @@ typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingTy
150
115
  #define OffFSELog 8
151
116
  #define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog)
152
117
 
153
- static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
154
- 0, 0, 0, 0, 0, 0, 0, 0,
155
- 1, 1, 1, 1, 2, 2, 3, 3,
156
- 4, 6, 7, 8, 9,10,11,12,
157
- 13,14,15,16 };
158
- static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2,
159
- 2, 2, 2, 2, 2, 1, 1, 1,
160
- 2, 2, 2, 2, 2, 2, 2, 2,
161
- 2, 3, 2, 1, 1, 1, 1, 1,
162
- -1,-1,-1,-1 };
118
+ #define ZSTD_MAX_HUF_HEADER_SIZE 128 /* header + <= 127 byte tree description */
119
+ /* Each table cannot take more than #symbols * FSELog bits */
120
+ #define ZSTD_MAX_FSE_HEADERS_SIZE (((MaxML + 1) * MLFSELog + (MaxLL + 1) * LLFSELog + (MaxOff + 1) * OffFSELog + 7) / 8)
121
+
122
+ static UNUSED_ATTR const U8 LL_bits[MaxLL+1] = {
123
+ 0, 0, 0, 0, 0, 0, 0, 0,
124
+ 0, 0, 0, 0, 0, 0, 0, 0,
125
+ 1, 1, 1, 1, 2, 2, 3, 3,
126
+ 4, 6, 7, 8, 9,10,11,12,
127
+ 13,14,15,16
128
+ };
129
+ static UNUSED_ATTR const S16 LL_defaultNorm[MaxLL+1] = {
130
+ 4, 3, 2, 2, 2, 2, 2, 2,
131
+ 2, 2, 2, 2, 2, 1, 1, 1,
132
+ 2, 2, 2, 2, 2, 2, 2, 2,
133
+ 2, 3, 2, 1, 1, 1, 1, 1,
134
+ -1,-1,-1,-1
135
+ };
163
136
  #define LL_DEFAULTNORMLOG 6 /* for static allocation */
164
- static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
165
-
166
- static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
167
- 0, 0, 0, 0, 0, 0, 0, 0,
168
- 0, 0, 0, 0, 0, 0, 0, 0,
169
- 0, 0, 0, 0, 0, 0, 0, 0,
170
- 1, 1, 1, 1, 2, 2, 3, 3,
171
- 4, 4, 5, 7, 8, 9,10,11,
172
- 12,13,14,15,16 };
173
- static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2,
174
- 2, 1, 1, 1, 1, 1, 1, 1,
175
- 1, 1, 1, 1, 1, 1, 1, 1,
176
- 1, 1, 1, 1, 1, 1, 1, 1,
177
- 1, 1, 1, 1, 1, 1, 1, 1,
178
- 1, 1, 1, 1, 1, 1,-1,-1,
179
- -1,-1,-1,-1,-1 };
137
+ static UNUSED_ATTR const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
138
+
139
+ static UNUSED_ATTR const U8 ML_bits[MaxML+1] = {
140
+ 0, 0, 0, 0, 0, 0, 0, 0,
141
+ 0, 0, 0, 0, 0, 0, 0, 0,
142
+ 0, 0, 0, 0, 0, 0, 0, 0,
143
+ 0, 0, 0, 0, 0, 0, 0, 0,
144
+ 1, 1, 1, 1, 2, 2, 3, 3,
145
+ 4, 4, 5, 7, 8, 9,10,11,
146
+ 12,13,14,15,16
147
+ };
148
+ static UNUSED_ATTR const S16 ML_defaultNorm[MaxML+1] = {
149
+ 1, 4, 3, 2, 2, 2, 2, 2,
150
+ 2, 1, 1, 1, 1, 1, 1, 1,
151
+ 1, 1, 1, 1, 1, 1, 1, 1,
152
+ 1, 1, 1, 1, 1, 1, 1, 1,
153
+ 1, 1, 1, 1, 1, 1, 1, 1,
154
+ 1, 1, 1, 1, 1, 1,-1,-1,
155
+ -1,-1,-1,-1,-1
156
+ };
180
157
  #define ML_DEFAULTNORMLOG 6 /* for static allocation */
181
- static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
182
-
183
- static const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2,
184
- 2, 1, 1, 1, 1, 1, 1, 1,
185
- 1, 1, 1, 1, 1, 1, 1, 1,
186
- -1,-1,-1,-1,-1 };
158
+ static UNUSED_ATTR const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
159
+
160
+ static UNUSED_ATTR const S16 OF_defaultNorm[DefaultMaxOff+1] = {
161
+ 1, 1, 1, 1, 1, 1, 2, 2,
162
+ 2, 1, 1, 1, 1, 1, 1, 1,
163
+ 1, 1, 1, 1, 1, 1, 1, 1,
164
+ -1,-1,-1,-1,-1
165
+ };
187
166
  #define OF_DEFAULTNORMLOG 5 /* for static allocation */
188
- static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
167
+ static UNUSED_ATTR const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
189
168
 
190
169
 
191
170
  /*-*******************************************
192
171
  * Shared functions to include for inlining
193
172
  *********************************************/
194
- static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
195
-
173
+ static void ZSTD_copy8(void* dst, const void* src) {
174
+ #if defined(ZSTD_ARCH_ARM_NEON)
175
+ vst1_u8((uint8_t*)dst, vld1_u8((const uint8_t*)src));
176
+ #else
177
+ ZSTD_memcpy(dst, src, 8);
178
+ #endif
179
+ }
196
180
  #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
197
- static void ZSTD_copy16(void* dst, const void* src) { memcpy(dst, src, 16); }
181
+
182
+ /* Need to use memmove here since the literal buffer can now be located within
183
+ the dst buffer. In circumstances where the op "catches up" to where the
184
+ literal buffer is, there can be partial overlaps in this call on the final
185
+ copy if the literal is being shifted by less than 16 bytes. */
186
+ static void ZSTD_copy16(void* dst, const void* src) {
187
+ #if defined(ZSTD_ARCH_ARM_NEON)
188
+ vst1q_u8((uint8_t*)dst, vld1q_u8((const uint8_t*)src));
189
+ #elif defined(ZSTD_ARCH_X86_SSE2)
190
+ _mm_storeu_si128((__m128i*)dst, _mm_loadu_si128((const __m128i*)src));
191
+ #elif defined(__clang__)
192
+ ZSTD_memmove(dst, src, 16);
193
+ #else
194
+ /* ZSTD_memmove is not inlined properly by gcc */
195
+ BYTE copy16_buf[16];
196
+ ZSTD_memcpy(copy16_buf, src, 16);
197
+ ZSTD_memcpy(dst, copy16_buf, 16);
198
+ #endif
199
+ }
198
200
  #define COPY16(d,s) { ZSTD_copy16(d,s); d+=16; s+=16; }
199
201
 
200
202
  #define WILDCOPY_OVERLENGTH 32
@@ -207,13 +209,13 @@ typedef enum {
207
209
  } ZSTD_overlap_e;
208
210
 
209
211
  /*! ZSTD_wildcopy() :
210
- * Custom version of memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)
212
+ * Custom version of ZSTD_memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)
211
213
  * @param ovtype controls the overlap detection
212
214
  * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.
213
215
  * - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart.
214
216
  * The src buffer must be before the dst buffer.
215
217
  */
216
- MEM_STATIC FORCE_INLINE_ATTR DONT_VECTORIZE
218
+ MEM_STATIC FORCE_INLINE_ATTR
217
219
  void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e const ovtype)
218
220
  {
219
221
  ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;
@@ -221,8 +223,6 @@ void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e
221
223
  BYTE* op = (BYTE*)dst;
222
224
  BYTE* const oend = op + length;
223
225
 
224
- assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff <= -WILDCOPY_VECLEN));
225
-
226
226
  if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) {
227
227
  /* Handle short offset copies. */
228
228
  do {
@@ -230,47 +230,117 @@ void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e
230
230
  } while (op < oend);
231
231
  } else {
232
232
  assert(diff >= WILDCOPY_VECLEN || diff <= -WILDCOPY_VECLEN);
233
- /* Separate out the first two COPY16() calls because the copy length is
233
+ /* Separate out the first COPY16() call because the copy length is
234
234
  * almost certain to be short, so the branches have different
235
- * probabilities.
236
- * On gcc-9 unrolling once is +1.6%, twice is +2%, thrice is +1.8%.
237
- * On clang-8 unrolling once is +1.4%, twice is +3.3%, thrice is +3%.
235
+ * probabilities. Since it is almost certain to be short, only do
236
+ * one COPY16() in the first call. Then, do two calls per loop since
237
+ * at that point it is more likely to have a high trip count.
238
238
  */
239
- COPY16(op, ip);
240
- COPY16(op, ip);
241
- if (op >= oend) return;
239
+ #ifdef __aarch64__
240
+ do {
241
+ COPY16(op, ip);
242
+ }
243
+ while (op < oend);
244
+ #else
245
+ ZSTD_copy16(op, ip);
246
+ if (16 >= length) return;
247
+ op += 16;
248
+ ip += 16;
242
249
  do {
243
250
  COPY16(op, ip);
244
251
  COPY16(op, ip);
245
252
  }
246
253
  while (op < oend);
254
+ #endif
255
+ }
256
+ }
257
+
258
+ MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
259
+ {
260
+ size_t const length = MIN(dstCapacity, srcSize);
261
+ if (length > 0) {
262
+ ZSTD_memcpy(dst, src, length);
247
263
  }
264
+ return length;
248
265
  }
249
266
 
267
+ /* define "workspace is too large" as this number of times larger than needed */
268
+ #define ZSTD_WORKSPACETOOLARGE_FACTOR 3
269
+
270
+ /* when workspace is continuously too large
271
+ * during at least this number of times,
272
+ * context's memory usage is considered wasteful,
273
+ * because it's sized to handle a worst case scenario which rarely happens.
274
+ * In which case, resize it down to free some memory */
275
+ #define ZSTD_WORKSPACETOOLARGE_MAXDURATION 128
276
+
277
+ /* Controls whether the input/output buffer is buffered or stable. */
278
+ typedef enum {
279
+ ZSTD_bm_buffered = 0, /* Buffer the input/output */
280
+ ZSTD_bm_stable = 1 /* ZSTD_inBuffer/ZSTD_outBuffer is stable */
281
+ } ZSTD_bufferMode_e;
282
+
250
283
 
251
284
  /*-*******************************************
252
285
  * Private declarations
253
286
  *********************************************/
254
287
  typedef struct seqDef_s {
255
- U32 offset;
288
+ U32 offset; /* offset == rawOffset + ZSTD_REP_NUM, or equivalently, offCode + 1 */
256
289
  U16 litLength;
257
290
  U16 matchLength;
258
291
  } seqDef;
259
292
 
293
+ /* Controls whether seqStore has a single "long" litLength or matchLength. See seqStore_t. */
294
+ typedef enum {
295
+ ZSTD_llt_none = 0, /* no longLengthType */
296
+ ZSTD_llt_literalLength = 1, /* represents a long literal */
297
+ ZSTD_llt_matchLength = 2 /* represents a long match */
298
+ } ZSTD_longLengthType_e;
299
+
260
300
  typedef struct {
261
301
  seqDef* sequencesStart;
262
- seqDef* sequences;
302
+ seqDef* sequences; /* ptr to end of sequences */
263
303
  BYTE* litStart;
264
- BYTE* lit;
304
+ BYTE* lit; /* ptr to end of literals */
265
305
  BYTE* llCode;
266
306
  BYTE* mlCode;
267
307
  BYTE* ofCode;
268
308
  size_t maxNbSeq;
269
309
  size_t maxNbLit;
270
- U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
271
- U32 longLengthPos;
310
+
311
+ /* longLengthPos and longLengthType to allow us to represent either a single litLength or matchLength
312
+ * in the seqStore that has a value larger than U16 (if it exists). To do so, we increment
313
+ * the existing value of the litLength or matchLength by 0x10000.
314
+ */
315
+ ZSTD_longLengthType_e longLengthType;
316
+ U32 longLengthPos; /* Index of the sequence to apply long length modification to */
272
317
  } seqStore_t;
273
318
 
319
+ typedef struct {
320
+ U32 litLength;
321
+ U32 matchLength;
322
+ } ZSTD_sequenceLength;
323
+
324
+ /**
325
+ * Returns the ZSTD_sequenceLength for the given sequences. It handles the decoding of long sequences
326
+ * indicated by longLengthPos and longLengthType, and adds MINMATCH back to matchLength.
327
+ */
328
+ MEM_STATIC ZSTD_sequenceLength ZSTD_getSequenceLength(seqStore_t const* seqStore, seqDef const* seq)
329
+ {
330
+ ZSTD_sequenceLength seqLen;
331
+ seqLen.litLength = seq->litLength;
332
+ seqLen.matchLength = seq->matchLength + MINMATCH;
333
+ if (seqStore->longLengthPos == (U32)(seq - seqStore->sequencesStart)) {
334
+ if (seqStore->longLengthType == ZSTD_llt_literalLength) {
335
+ seqLen.litLength += 0xFFFF;
336
+ }
337
+ if (seqStore->longLengthType == ZSTD_llt_matchLength) {
338
+ seqLen.matchLength += 0xFFFF;
339
+ }
340
+ }
341
+ return seqLen;
342
+ }
343
+
274
344
  /**
275
345
  * Contains the compressed frame size and an upper-bound for the decompressed frame size.
276
346
  * Note: before using `compressedSize`, check for errors using ZSTD_isError().
@@ -286,9 +356,9 @@ const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBu
286
356
  void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
287
357
 
288
358
  /* custom memory allocation functions */
289
- void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
290
- void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
291
- void ZSTD_free(void* ptr, ZSTD_customMem customMem);
359
+ void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem);
360
+ void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem);
361
+ void ZSTD_customFree(void* ptr, ZSTD_customMem customMem);
292
362
 
293
363
 
294
364
  MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
@@ -296,9 +366,18 @@ MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus
296
366
  assert(val != 0);
297
367
  {
298
368
  # if defined(_MSC_VER) /* Visual */
299
- unsigned long r=0;
300
- _BitScanReverse(&r, val);
301
- return (unsigned)r;
369
+ # if STATIC_BMI2 == 1
370
+ return _lzcnt_u32(val)^31;
371
+ # else
372
+ if (val != 0) {
373
+ unsigned long r;
374
+ _BitScanReverse(&r, val);
375
+ return (unsigned)r;
376
+ } else {
377
+ /* Should not reach this code path */
378
+ __assume(0);
379
+ }
380
+ # endif
302
381
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
303
382
  return __builtin_clz (val) ^ 31;
304
383
  # elif defined(__ICCARM__) /* IAR Intrinsic */
@@ -316,6 +395,63 @@ MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus
316
395
  }
317
396
  }
318
397
 
398
+ /**
399
+ * Counts the number of trailing zeros of a `size_t`.
400
+ * Most compilers should support CTZ as a builtin. A backup
401
+ * implementation is provided if the builtin isn't supported, but
402
+ * it may not be terribly efficient.
403
+ */
404
+ MEM_STATIC unsigned ZSTD_countTrailingZeros(size_t val)
405
+ {
406
+ if (MEM_64bits()) {
407
+ # if defined(_MSC_VER) && defined(_WIN64)
408
+ # if STATIC_BMI2
409
+ return _tzcnt_u64(val);
410
+ # else
411
+ if (val != 0) {
412
+ unsigned long r;
413
+ _BitScanForward64(&r, (U64)val);
414
+ return (unsigned)r;
415
+ } else {
416
+ /* Should not reach this code path */
417
+ __assume(0);
418
+ }
419
+ # endif
420
+ # elif defined(__GNUC__) && (__GNUC__ >= 4)
421
+ return __builtin_ctzll((U64)val);
422
+ # else
423
+ static const int DeBruijnBytePos[64] = { 0, 1, 2, 7, 3, 13, 8, 19,
424
+ 4, 25, 14, 28, 9, 34, 20, 56,
425
+ 5, 17, 26, 54, 15, 41, 29, 43,
426
+ 10, 31, 38, 35, 21, 45, 49, 57,
427
+ 63, 6, 12, 18, 24, 27, 33, 55,
428
+ 16, 53, 40, 42, 30, 37, 44, 48,
429
+ 62, 11, 23, 32, 52, 39, 36, 47,
430
+ 61, 22, 51, 46, 60, 50, 59, 58 };
431
+ return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
432
+ # endif
433
+ } else { /* 32 bits */
434
+ # if defined(_MSC_VER)
435
+ if (val != 0) {
436
+ unsigned long r;
437
+ _BitScanForward(&r, (U32)val);
438
+ return (unsigned)r;
439
+ } else {
440
+ /* Should not reach this code path */
441
+ __assume(0);
442
+ }
443
+ # elif defined(__GNUC__) && (__GNUC__ >= 3)
444
+ return __builtin_ctz((U32)val);
445
+ # else
446
+ static const int DeBruijnBytePos[32] = { 0, 1, 28, 2, 29, 14, 24, 3,
447
+ 30, 22, 20, 15, 25, 17, 4, 8,
448
+ 31, 27, 13, 23, 21, 19, 16, 7,
449
+ 26, 12, 18, 6, 11, 5, 10, 9 };
450
+ return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
451
+ # endif
452
+ }
453
+ }
454
+
319
455
 
320
456
  /* ZSTD_invalidateRepCodes() :
321
457
  * ensures next compression will not use repcodes from previous block.
@@ -342,6 +478,14 @@ size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
342
478
  size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
343
479
  const void* src, size_t srcSize);
344
480
 
481
+ /**
482
+ * @returns true iff the CPU supports dynamic BMI2 dispatch.
483
+ */
484
+ MEM_STATIC int ZSTD_cpuSupportsBmi2(void)
485
+ {
486
+ ZSTD_cpuid_t cpuid = ZSTD_cpuid();
487
+ return ZSTD_cpuid_bmi1(cpuid) && ZSTD_cpuid_bmi2(cpuid);
488
+ }
345
489
 
346
490
  #if defined (__cplusplus)
347
491
  }