zstd-ruby 1.4.4.0 → 1.5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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) 2018-present, Facebook, Inc.
2
+ * Copyright (c) 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
@@ -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) Yann Collet, Facebook, Inc.
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
 
@@ -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) Yann Collet, Facebook, Inc.
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
 
@@ -71,15 +51,6 @@ extern "C" {
71
51
  #endif
72
52
 
73
53
 
74
- /* DEBUGFILE can be defined externally,
75
- * typically through compiler command line.
76
- * note : currently useless.
77
- * Value must be stderr or stdout */
78
- #ifndef DEBUGFILE
79
- # define DEBUGFILE stderr
80
- #endif
81
-
82
-
83
54
  /* recommended values for DEBUGLEVEL :
84
55
  * 0 : release mode, no debug, all run-time checks disabled
85
56
  * 1 : enables assert() only, no display
@@ -96,7 +67,8 @@ extern "C" {
96
67
  */
97
68
 
98
69
  #if (DEBUGLEVEL>=1)
99
- # include <assert.h>
70
+ # define ZSTD_DEPS_NEED_ASSERT
71
+ # include "zstd_deps.h"
100
72
  #else
101
73
  # ifndef assert /* assert may be already defined, due to prior #include <assert.h> */
102
74
  # define assert(condition) ((void)0) /* disable assert (default) */
@@ -104,7 +76,8 @@ extern "C" {
104
76
  #endif
105
77
 
106
78
  #if (DEBUGLEVEL>=2)
107
- # include <stdio.h>
79
+ # define ZSTD_DEPS_NEED_IO
80
+ # include "zstd_deps.h"
108
81
  extern int g_debuglevel; /* the variable is only declared,
109
82
  it actually lives in debug.c,
110
83
  and is shared by the whole process.
@@ -112,14 +85,14 @@ extern int g_debuglevel; /* the variable is only declared,
112
85
  It's useful when enabling very verbose levels
113
86
  on selective conditions (such as position in src) */
114
87
 
115
- # define RAWLOG(l, ...) { \
116
- if (l<=g_debuglevel) { \
117
- fprintf(stderr, __VA_ARGS__); \
88
+ # define RAWLOG(l, ...) { \
89
+ if (l<=g_debuglevel) { \
90
+ ZSTD_DEBUG_PRINT(__VA_ARGS__); \
118
91
  } }
119
- # define DEBUGLOG(l, ...) { \
120
- if (l<=g_debuglevel) { \
121
- fprintf(stderr, __FILE__ ": " __VA_ARGS__); \
122
- fprintf(stderr, " \n"); \
92
+ # define DEBUGLOG(l, ...) { \
93
+ if (l<=g_debuglevel) { \
94
+ ZSTD_DEBUG_PRINT(__FILE__ ": " __VA_ARGS__); \
95
+ ZSTD_DEBUG_PRINT(" \n"); \
123
96
  } }
124
97
  #else
125
98
  # define RAWLOG(l, ...) {} /* disabled */
@@ -1,36 +1,16 @@
1
- /*
2
- Common functions of New Generation Entropy library
3
- Copyright (C) 2016, Yann Collet.
4
-
5
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
-
7
- Redistribution and use in source and binary forms, with or without
8
- modification, are permitted provided that the following conditions are
9
- met:
10
-
11
- * Redistributions of source code must retain the above copyright
12
- notice, this list of conditions and the following disclaimer.
13
- * Redistributions in binary form must reproduce the above
14
- copyright notice, this list of conditions and the following disclaimer
15
- in the documentation and/or other materials provided with the
16
- distribution.
17
-
18
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
-
30
- You can contact the author at :
31
- - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
32
- - Public forum : https://groups.google.com/forum/#!forum/lz4c
33
- *************************************************************************** */
1
+ /* ******************************************************************
2
+ * Common functions of New Generation Entropy library
3
+ * Copyright (c) Yann Collet, Facebook, Inc.
4
+ *
5
+ * You can contact the author at :
6
+ * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
7
+ * - Public forum : https://groups.google.com/forum/#!forum/lz4c
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.
13
+ ****************************************************************** */
34
14
 
35
15
  /* *************************************
36
16
  * Dependencies
@@ -58,8 +38,37 @@ const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
58
38
  /*-**************************************************************
59
39
  * FSE NCount encoding-decoding
60
40
  ****************************************************************/
61
- size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
62
- const void* headerBuffer, size_t hbSize)
41
+ static U32 FSE_ctz(U32 val)
42
+ {
43
+ assert(val != 0);
44
+ {
45
+ # if defined(_MSC_VER) /* Visual */
46
+ if (val != 0) {
47
+ unsigned long r;
48
+ _BitScanForward(&r, val);
49
+ return (unsigned)r;
50
+ } else {
51
+ /* Should not reach this code path */
52
+ __assume(0);
53
+ }
54
+ # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
55
+ return __builtin_ctz(val);
56
+ # elif defined(__ICCARM__) /* IAR Intrinsic */
57
+ return __CTZ(val);
58
+ # else /* Software version */
59
+ U32 count = 0;
60
+ while ((val & 1) == 0) {
61
+ val >>= 1;
62
+ ++count;
63
+ }
64
+ return count;
65
+ # endif
66
+ }
67
+ }
68
+
69
+ FORCE_INLINE_TEMPLATE
70
+ size_t FSE_readNCount_body(short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
71
+ const void* headerBuffer, size_t hbSize)
63
72
  {
64
73
  const BYTE* const istart = (const BYTE*) headerBuffer;
65
74
  const BYTE* const iend = istart + hbSize;
@@ -70,23 +79,23 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
70
79
  U32 bitStream;
71
80
  int bitCount;
72
81
  unsigned charnum = 0;
82
+ unsigned const maxSV1 = *maxSVPtr + 1;
73
83
  int previous0 = 0;
74
84
 
75
- if (hbSize < 4) {
76
- /* This function only works when hbSize >= 4 */
77
- char buffer[4];
78
- memset(buffer, 0, sizeof(buffer));
79
- memcpy(buffer, headerBuffer, hbSize);
85
+ if (hbSize < 8) {
86
+ /* This function only works when hbSize >= 8 */
87
+ char buffer[8] = {0};
88
+ ZSTD_memcpy(buffer, headerBuffer, hbSize);
80
89
  { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
81
90
  buffer, sizeof(buffer));
82
91
  if (FSE_isError(countSize)) return countSize;
83
92
  if (countSize > hbSize) return ERROR(corruption_detected);
84
93
  return countSize;
85
94
  } }
86
- assert(hbSize >= 4);
95
+ assert(hbSize >= 8);
87
96
 
88
97
  /* init */
89
- memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
98
+ ZSTD_memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
90
99
  bitStream = MEM_readLE32(ip);
91
100
  nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
92
101
  if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
@@ -97,36 +106,58 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
97
106
  threshold = 1<<nbBits;
98
107
  nbBits++;
99
108
 
100
- while ((remaining>1) & (charnum<=*maxSVPtr)) {
109
+ for (;;) {
101
110
  if (previous0) {
102
- unsigned n0 = charnum;
103
- while ((bitStream & 0xFFFF) == 0xFFFF) {
104
- n0 += 24;
105
- if (ip < iend-5) {
106
- ip += 2;
107
- bitStream = MEM_readLE32(ip) >> bitCount;
111
+ /* Count the number of repeats. Each time the
112
+ * 2-bit repeat code is 0b11 there is another
113
+ * repeat.
114
+ * Avoid UB by setting the high bit to 1.
115
+ */
116
+ int repeats = FSE_ctz(~bitStream | 0x80000000) >> 1;
117
+ while (repeats >= 12) {
118
+ charnum += 3 * 12;
119
+ if (LIKELY(ip <= iend-7)) {
120
+ ip += 3;
108
121
  } else {
109
- bitStream >>= 16;
110
- bitCount += 16;
111
- } }
112
- while ((bitStream & 3) == 3) {
113
- n0 += 3;
114
- bitStream >>= 2;
115
- bitCount += 2;
122
+ bitCount -= (int)(8 * (iend - 7 - ip));
123
+ bitCount &= 31;
124
+ ip = iend - 4;
125
+ }
126
+ bitStream = MEM_readLE32(ip) >> bitCount;
127
+ repeats = FSE_ctz(~bitStream | 0x80000000) >> 1;
116
128
  }
117
- n0 += bitStream & 3;
129
+ charnum += 3 * repeats;
130
+ bitStream >>= 2 * repeats;
131
+ bitCount += 2 * repeats;
132
+
133
+ /* Add the final repeat which isn't 0b11. */
134
+ assert((bitStream & 3) < 3);
135
+ charnum += bitStream & 3;
118
136
  bitCount += 2;
119
- if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
120
- while (charnum < n0) normalizedCounter[charnum++] = 0;
121
- if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
137
+
138
+ /* This is an error, but break and return an error
139
+ * at the end, because returning out of a loop makes
140
+ * it harder for the compiler to optimize.
141
+ */
142
+ if (charnum >= maxSV1) break;
143
+
144
+ /* We don't need to set the normalized count to 0
145
+ * because we already memset the whole buffer to 0.
146
+ */
147
+
148
+ if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
122
149
  assert((bitCount >> 3) <= 3); /* For first condition to work */
123
150
  ip += bitCount>>3;
124
151
  bitCount &= 7;
125
- bitStream = MEM_readLE32(ip) >> bitCount;
126
152
  } else {
127
- bitStream >>= 2;
128
- } }
129
- { int const max = (2*threshold-1) - remaining;
153
+ bitCount -= (int)(8 * (iend - 4 - ip));
154
+ bitCount &= 31;
155
+ ip = iend - 4;
156
+ }
157
+ bitStream = MEM_readLE32(ip) >> bitCount;
158
+ }
159
+ {
160
+ int const max = (2*threshold-1) - remaining;
130
161
  int count;
131
162
 
132
163
  if ((bitStream & (threshold-1)) < (U32)max) {
@@ -139,24 +170,43 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
139
170
  }
140
171
 
141
172
  count--; /* extra accuracy */
142
- remaining -= count < 0 ? -count : count; /* -1 means +1 */
173
+ /* When it matters (small blocks), this is a
174
+ * predictable branch, because we don't use -1.
175
+ */
176
+ if (count >= 0) {
177
+ remaining -= count;
178
+ } else {
179
+ assert(count == -1);
180
+ remaining += count;
181
+ }
143
182
  normalizedCounter[charnum++] = (short)count;
144
183
  previous0 = !count;
145
- while (remaining < threshold) {
146
- nbBits--;
147
- threshold >>= 1;
184
+
185
+ assert(threshold > 1);
186
+ if (remaining < threshold) {
187
+ /* This branch can be folded into the
188
+ * threshold update condition because we
189
+ * know that threshold > 1.
190
+ */
191
+ if (remaining <= 1) break;
192
+ nbBits = BIT_highbit32(remaining) + 1;
193
+ threshold = 1 << (nbBits - 1);
148
194
  }
195
+ if (charnum >= maxSV1) break;
149
196
 
150
- if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
197
+ if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
151
198
  ip += bitCount>>3;
152
199
  bitCount &= 7;
153
200
  } else {
154
201
  bitCount -= (int)(8 * (iend - 4 - ip));
202
+ bitCount &= 31;
155
203
  ip = iend - 4;
156
204
  }
157
- bitStream = MEM_readLE32(ip) >> (bitCount & 31);
158
- } } /* while ((remaining>1) & (charnum<=*maxSVPtr)) */
205
+ bitStream = MEM_readLE32(ip) >> bitCount;
206
+ } }
159
207
  if (remaining != 1) return ERROR(corruption_detected);
208
+ /* Only possible when there are too many zeros. */
209
+ if (charnum > maxSV1) return ERROR(maxSymbolValue_tooSmall);
160
210
  if (bitCount > 32) return ERROR(corruption_detected);
161
211
  *maxSVPtr = charnum-1;
162
212
 
@@ -164,6 +214,43 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
164
214
  return ip-istart;
165
215
  }
166
216
 
217
+ /* Avoids the FORCE_INLINE of the _body() function. */
218
+ static size_t FSE_readNCount_body_default(
219
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
220
+ const void* headerBuffer, size_t hbSize)
221
+ {
222
+ return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
223
+ }
224
+
225
+ #if DYNAMIC_BMI2
226
+ BMI2_TARGET_ATTRIBUTE static size_t FSE_readNCount_body_bmi2(
227
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
228
+ const void* headerBuffer, size_t hbSize)
229
+ {
230
+ return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
231
+ }
232
+ #endif
233
+
234
+ size_t FSE_readNCount_bmi2(
235
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
236
+ const void* headerBuffer, size_t hbSize, int bmi2)
237
+ {
238
+ #if DYNAMIC_BMI2
239
+ if (bmi2) {
240
+ return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
241
+ }
242
+ #endif
243
+ (void)bmi2;
244
+ return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
245
+ }
246
+
247
+ size_t FSE_readNCount(
248
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
249
+ const void* headerBuffer, size_t hbSize)
250
+ {
251
+ return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, /* bmi2 */ 0);
252
+ }
253
+
167
254
 
168
255
  /*! HUF_readStats() :
169
256
  Read compact Huffman tree, saved by HUF_writeCTable().
@@ -175,6 +262,17 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
175
262
  size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
176
263
  U32* nbSymbolsPtr, U32* tableLogPtr,
177
264
  const void* src, size_t srcSize)
265
+ {
266
+ U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
267
+ return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp, sizeof(wksp), /* bmi2 */ 0);
268
+ }
269
+
270
+ FORCE_INLINE_TEMPLATE size_t
271
+ HUF_readStats_body(BYTE* huffWeight, size_t hwSize, U32* rankStats,
272
+ U32* nbSymbolsPtr, U32* tableLogPtr,
273
+ const void* src, size_t srcSize,
274
+ void* workSpace, size_t wkspSize,
275
+ int bmi2)
178
276
  {
179
277
  U32 weightTotal;
180
278
  const BYTE* ip = (const BYTE*) src;
@@ -183,7 +281,7 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
183
281
 
184
282
  if (!srcSize) return ERROR(srcSize_wrong);
185
283
  iSize = ip[0];
186
- /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
284
+ /* ZSTD_memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
187
285
 
188
286
  if (iSize >= 128) { /* special header */
189
287
  oSize = iSize - 127;
@@ -197,17 +295,17 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
197
295
  huffWeight[n+1] = ip[n/2] & 15;
198
296
  } } }
199
297
  else { /* header compressed with FSE (normal case) */
200
- FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)]; /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */
201
298
  if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
202
- oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6); /* max (hwSize-1) values decoded, as last one is implied */
299
+ /* max (hwSize-1) values decoded, as last one is implied */
300
+ oSize = FSE_decompress_wksp_bmi2(huffWeight, hwSize-1, ip+1, iSize, 6, workSpace, wkspSize, bmi2);
203
301
  if (FSE_isError(oSize)) return oSize;
204
302
  }
205
303
 
206
304
  /* collect weight stats */
207
- memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
305
+ ZSTD_memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
208
306
  weightTotal = 0;
209
307
  { U32 n; for (n=0; n<oSize; n++) {
210
- if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
308
+ if (huffWeight[n] > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
211
309
  rankStats[huffWeight[n]]++;
212
310
  weightTotal += (1 << huffWeight[n]) >> 1;
213
311
  } }
@@ -234,3 +332,37 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
234
332
  *nbSymbolsPtr = (U32)(oSize+1);
235
333
  return iSize+1;
236
334
  }
335
+
336
+ /* Avoids the FORCE_INLINE of the _body() function. */
337
+ static size_t HUF_readStats_body_default(BYTE* huffWeight, size_t hwSize, U32* rankStats,
338
+ U32* nbSymbolsPtr, U32* tableLogPtr,
339
+ const void* src, size_t srcSize,
340
+ void* workSpace, size_t wkspSize)
341
+ {
342
+ return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 0);
343
+ }
344
+
345
+ #if DYNAMIC_BMI2
346
+ static BMI2_TARGET_ATTRIBUTE size_t HUF_readStats_body_bmi2(BYTE* huffWeight, size_t hwSize, U32* rankStats,
347
+ U32* nbSymbolsPtr, U32* tableLogPtr,
348
+ const void* src, size_t srcSize,
349
+ void* workSpace, size_t wkspSize)
350
+ {
351
+ return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 1);
352
+ }
353
+ #endif
354
+
355
+ size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize, U32* rankStats,
356
+ U32* nbSymbolsPtr, U32* tableLogPtr,
357
+ const void* src, size_t srcSize,
358
+ void* workSpace, size_t wkspSize,
359
+ int bmi2)
360
+ {
361
+ #if DYNAMIC_BMI2
362
+ if (bmi2) {
363
+ return HUF_readStats_body_bmi2(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
364
+ }
365
+ #endif
366
+ (void)bmi2;
367
+ return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
368
+ }
@@ -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
@@ -47,6 +47,8 @@ const char* ERR_getErrorString(ERR_enum code)
47
47
  /* following error codes are not stable and may be removed or changed in a future version */
48
48
  case PREFIX(frameIndex_tooLarge): return "Frame index is too large";
49
49
  case PREFIX(seekableIO): return "An I/O error occurred when reading/seeking";
50
+ case PREFIX(dstBuffer_wrong): return "Destination buffer is wrong";
51
+ case PREFIX(srcBuffer_wrong): return "Source buffer is wrong";
50
52
  case PREFIX(maxCode):
51
53
  default: return notErrorCode;
52
54
  }