multi_compress 0.6.1 → 0.7.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 (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -0
  3. data/GET_STARTED.md +4 -2
  4. data/README.md +1 -1
  5. data/THIRD_PARTY_NOTICES.md +2 -2
  6. data/ext/multi_compress/exports.darwin +1 -0
  7. data/ext/multi_compress/exports.map +4 -0
  8. data/ext/multi_compress/extconf.rb +20 -0
  9. data/ext/multi_compress/multi_compress.c +123 -73
  10. data/ext/multi_compress/vendor/.vendored +1 -1
  11. data/ext/multi_compress/vendor/zstd/lib/BUCK +10 -5
  12. data/ext/multi_compress/vendor/zstd/lib/README.md +25 -5
  13. data/ext/multi_compress/vendor/zstd/lib/common/allocations.h +6 -2
  14. data/ext/multi_compress/vendor/zstd/lib/common/bits.h +1 -1
  15. data/ext/multi_compress/vendor/zstd/lib/common/bitstream.h +3 -3
  16. data/ext/multi_compress/vendor/zstd/lib/common/bmi2.h +40 -0
  17. data/ext/multi_compress/vendor/zstd/lib/common/compiler.h +37 -11
  18. data/ext/multi_compress/vendor/zstd/lib/common/entropy_common.c +7 -6
  19. data/ext/multi_compress/vendor/zstd/lib/common/fse_decompress.c +4 -3
  20. data/ext/multi_compress/vendor/zstd/lib/common/pool.c +16 -19
  21. data/ext/multi_compress/vendor/zstd/lib/common/portability_macros.h +31 -0
  22. data/ext/multi_compress/vendor/zstd/lib/common/threading.c +16 -2
  23. data/ext/multi_compress/vendor/zstd/lib/common/zstd_common.c +9 -0
  24. data/ext/multi_compress/vendor/zstd/lib/common/zstd_internal.h +4 -2
  25. data/ext/multi_compress/vendor/zstd/lib/compress/hist.c +300 -3
  26. data/ext/multi_compress/vendor/zstd/lib/compress/hist.h +4 -0
  27. data/ext/multi_compress/vendor/zstd/lib/compress/huf_compress.c +4 -1
  28. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress.c +775 -214
  29. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_internal.h +35 -6
  30. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_sequences.c +9 -5
  31. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_superblock.c +14 -5
  32. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_double_fast.c +2 -2
  33. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_fast.c +2 -2
  34. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_lazy.c +49 -5
  35. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.c +11 -11
  36. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.h +11 -0
  37. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_opt.c +16 -18
  38. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_preSplit.c +3 -3
  39. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.c +64 -39
  40. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.h +30 -2
  41. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress.c +78 -64
  42. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress_amd64.S +182 -18
  43. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.c +59 -27
  44. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.h +11 -0
  45. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress.c +194 -93
  46. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_block.c +225 -85
  47. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_internal.h +19 -2
  48. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/cover.c +136 -91
  49. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/divsufsort.c +77 -77
  50. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/fastcover.c +48 -42
  51. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/zdict.c +18 -14
  52. data/ext/multi_compress/vendor/zstd/lib/dll/example/build_package.bat +66 -20
  53. data/ext/multi_compress/vendor/zstd/lib/dll/example/fullbench-dll.vcxproj +10 -6
  54. data/ext/multi_compress/vendor/zstd/lib/install_oses.mk +17 -0
  55. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v01.c +10 -8
  56. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v02.c +5 -364
  57. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v03.c +4 -5
  58. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v04.c +7 -3
  59. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v05.c +8 -5
  60. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v06.c +3 -3
  61. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v07.c +1 -0
  62. data/ext/multi_compress/vendor/zstd/lib/libzstd.mk +8 -7
  63. data/ext/multi_compress/vendor/zstd/lib/libzstd.pc.in +1 -0
  64. data/ext/multi_compress/vendor/zstd/lib/module.modulemap +0 -21
  65. data/ext/multi_compress/vendor/zstd/lib/zstd.h +209 -112
  66. data/lib/multi_compress/reader/read_operations.rb +2 -0
  67. data/lib/multi_compress/version.rb +1 -1
  68. data/multi_compress.gemspec +2 -0
  69. data/mysql_udf/Makefile +12 -5
  70. data/mysql_udf/multi_compress_mysql.exports +6 -0
  71. data/postgres_extension/Makefile +2 -1
  72. data/postgres_extension/test/run_e2e.sh +28 -10
  73. metadata +6 -5
  74. data/ext/multi_compress/vendor/lz4/lib/Makefile +0 -244
  75. data/ext/multi_compress/vendor/lz4/lib/dll/example/Makefile +0 -63
  76. data/ext/multi_compress/vendor/zstd/lib/Makefile +0 -389
  77. data/ext/multi_compress/vendor/zstd/lib/dll/example/Makefile +0 -48
@@ -19,6 +19,12 @@
19
19
  #include "../common/error_private.h" /* ERROR */
20
20
  #include "hist.h"
21
21
 
22
+ #if defined(ZSTD_ARCH_ARM_SVE2)
23
+ #define HIST_FAST_THRESHOLD 500
24
+ #else
25
+ #define HIST_FAST_THRESHOLD 1500
26
+ #endif
27
+
22
28
 
23
29
  /* --- Error management --- */
24
30
  unsigned HIST_isError(size_t code) { return ERR_isError(code); }
@@ -65,6 +71,286 @@ unsigned HIST_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,
65
71
 
66
72
  typedef enum { trustInput, checkMaxSymbolValue } HIST_checkInput_e;
67
73
 
74
+ #if defined(ZSTD_ARCH_ARM_SVE2)
75
+ /* U16 counters must hold even an all-identical chunk. Round the limit down
76
+ * to 16 bytes so zero-padding the final vector cannot overflow them. */
77
+ #define HIST_SVE2_MAX_CHUNK_SIZE ((1U << 16) - 16)
78
+ FORCE_INLINE_TEMPLATE size_t min_size(size_t a, size_t b) { return a < b ? a : b; }
79
+
80
+ static
81
+ svuint16_t HIST_count_6_sve2(const BYTE* const src, size_t size, U32* const dst,
82
+ const svuint8_t c0, const svuint8_t c1,
83
+ const svuint8_t c2, const svuint8_t c3,
84
+ const svuint8_t c4, const svuint8_t c5,
85
+ const svuint16_t histmax, size_t maxCount)
86
+ {
87
+ const svbool_t vl128 = svptrue_pat_b8(SV_VL16);
88
+ svuint16_t hh0 = svdup_n_u16(0);
89
+ svuint16_t hh1 = svdup_n_u16(0);
90
+ svuint16_t hh2 = svdup_n_u16(0);
91
+ svuint16_t hh3 = svdup_n_u16(0);
92
+ svuint16_t hh4 = svdup_n_u16(0);
93
+ svuint16_t hh5 = svdup_n_u16(0);
94
+ svuint16_t hh6 = svdup_n_u16(0);
95
+ svuint16_t hh7 = svdup_n_u16(0);
96
+ svuint16_t hh8 = svdup_n_u16(0);
97
+ svuint16_t hh9 = svdup_n_u16(0);
98
+ svuint16_t hha = svdup_n_u16(0);
99
+ svuint16_t hhb = svdup_n_u16(0);
100
+
101
+ size_t i = 0;
102
+ while (i < size) {
103
+ /* We can only accumulate 15 (15 * 16 <= 255) iterations of histogram
104
+ * in 8-bit accumulators! */
105
+ const size_t size240 = min_size(i + 240, size);
106
+
107
+ svbool_t pred = svwhilelt_b8_u64(i, size);
108
+ svuint8_t c = svld1rq_u8(pred, src + i);
109
+ svuint8_t h0 = svhistseg_u8(c0, c);
110
+ svuint8_t h1 = svhistseg_u8(c1, c);
111
+ svuint8_t h2 = svhistseg_u8(c2, c);
112
+ svuint8_t h3 = svhistseg_u8(c3, c);
113
+ svuint8_t h4 = svhistseg_u8(c4, c);
114
+ svuint8_t h5 = svhistseg_u8(c5, c);
115
+
116
+ for (i += 16; i < size240; i += 16) {
117
+ pred = svwhilelt_b8_u64(i, size);
118
+ c = svld1rq_u8(pred, src + i);
119
+ h0 = svadd_u8_x(vl128, h0, svhistseg_u8(c0, c));
120
+ h1 = svadd_u8_x(vl128, h1, svhistseg_u8(c1, c));
121
+ h2 = svadd_u8_x(vl128, h2, svhistseg_u8(c2, c));
122
+ h3 = svadd_u8_x(vl128, h3, svhistseg_u8(c3, c));
123
+ h4 = svadd_u8_x(vl128, h4, svhistseg_u8(c4, c));
124
+ h5 = svadd_u8_x(vl128, h5, svhistseg_u8(c5, c));
125
+ }
126
+
127
+ hh0 = svaddwb_u16(hh0, h0);
128
+ hh1 = svaddwt_u16(hh1, h0);
129
+ hh2 = svaddwb_u16(hh2, h1);
130
+ hh3 = svaddwt_u16(hh3, h1);
131
+ hh4 = svaddwb_u16(hh4, h2);
132
+ hh5 = svaddwt_u16(hh5, h2);
133
+ hh6 = svaddwb_u16(hh6, h3);
134
+ hh7 = svaddwt_u16(hh7, h3);
135
+ hh8 = svaddwb_u16(hh8, h4);
136
+ hh9 = svaddwt_u16(hh9, h4);
137
+ hha = svaddwb_u16(hha, h5);
138
+ hhb = svaddwt_u16(hhb, h5);
139
+ }
140
+
141
+ svst1_u32(svwhilelt_b32_u64( 0, maxCount), dst + 0, svshllb_n_u32(hh0, 0));
142
+ svst1_u32(svwhilelt_b32_u64( 4, maxCount), dst + 4, svshllt_n_u32(hh0, 0));
143
+ svst1_u32(svwhilelt_b32_u64( 8, maxCount), dst + 8, svshllb_n_u32(hh1, 0));
144
+ svst1_u32(svwhilelt_b32_u64(12, maxCount), dst + 12, svshllt_n_u32(hh1, 0));
145
+ svst1_u32(svwhilelt_b32_u64(16, maxCount), dst + 16, svshllb_n_u32(hh2, 0));
146
+ svst1_u32(svwhilelt_b32_u64(20, maxCount), dst + 20, svshllt_n_u32(hh2, 0));
147
+ svst1_u32(svwhilelt_b32_u64(24, maxCount), dst + 24, svshllb_n_u32(hh3, 0));
148
+ svst1_u32(svwhilelt_b32_u64(28, maxCount), dst + 28, svshllt_n_u32(hh3, 0));
149
+ svst1_u32(svwhilelt_b32_u64(32, maxCount), dst + 32, svshllb_n_u32(hh4, 0));
150
+ svst1_u32(svwhilelt_b32_u64(36, maxCount), dst + 36, svshllt_n_u32(hh4, 0));
151
+ svst1_u32(svwhilelt_b32_u64(40, maxCount), dst + 40, svshllb_n_u32(hh5, 0));
152
+ svst1_u32(svwhilelt_b32_u64(44, maxCount), dst + 44, svshllt_n_u32(hh5, 0));
153
+ svst1_u32(svwhilelt_b32_u64(48, maxCount), dst + 48, svshllb_n_u32(hh6, 0));
154
+ svst1_u32(svwhilelt_b32_u64(52, maxCount), dst + 52, svshllt_n_u32(hh6, 0));
155
+ svst1_u32(svwhilelt_b32_u64(56, maxCount), dst + 56, svshllb_n_u32(hh7, 0));
156
+ svst1_u32(svwhilelt_b32_u64(60, maxCount), dst + 60, svshllt_n_u32(hh7, 0));
157
+ svst1_u32(svwhilelt_b32_u64(64, maxCount), dst + 64, svshllb_n_u32(hh8, 0));
158
+ svst1_u32(svwhilelt_b32_u64(68, maxCount), dst + 68, svshllt_n_u32(hh8, 0));
159
+ svst1_u32(svwhilelt_b32_u64(72, maxCount), dst + 72, svshllb_n_u32(hh9, 0));
160
+ svst1_u32(svwhilelt_b32_u64(76, maxCount), dst + 76, svshllt_n_u32(hh9, 0));
161
+ svst1_u32(svwhilelt_b32_u64(80, maxCount), dst + 80, svshllb_n_u32(hha, 0));
162
+ svst1_u32(svwhilelt_b32_u64(84, maxCount), dst + 84, svshllt_n_u32(hha, 0));
163
+ svst1_u32(svwhilelt_b32_u64(88, maxCount), dst + 88, svshllb_n_u32(hhb, 0));
164
+ svst1_u32(svwhilelt_b32_u64(92, maxCount), dst + 92, svshllt_n_u32(hhb, 0));
165
+
166
+ hh0 = svmax_u16_x(vl128, hh0, hh1);
167
+ hh2 = svmax_u16_x(vl128, hh2, hh3);
168
+ hh4 = svmax_u16_x(vl128, hh4, hh5);
169
+ hh6 = svmax_u16_x(vl128, hh6, hh7);
170
+ hh8 = svmax_u16_x(vl128, hh8, hh9);
171
+ hha = svmax_u16_x(vl128, hha, hhb);
172
+ hh0 = svmax_u16_x(vl128, hh0, hh2);
173
+ hh4 = svmax_u16_x(vl128, hh4, hh6);
174
+ hh8 = svmax_u16_x(vl128, hh8, hha);
175
+ hh0 = svmax_u16_x(vl128, hh0, hh4);
176
+ hh8 = svmax_u16_x(vl128, hh8, histmax);
177
+ return svmax_u16_x(vl128, hh0, hh8);
178
+ }
179
+
180
+ static size_t HIST_count_sve2_chunk(unsigned* count, unsigned* maxSymbolValuePtr,
181
+ const void* source, size_t sourceSize,
182
+ HIST_checkInput_e check)
183
+ {
184
+ const BYTE* ip = (const BYTE*)source;
185
+ const size_t maxCount = *maxSymbolValuePtr + 1;
186
+
187
+ assert(*maxSymbolValuePtr <= 255);
188
+ assert(sourceSize <= HIST_SVE2_MAX_CHUNK_SIZE);
189
+ if (!sourceSize) {
190
+ ZSTD_memset(count, 0, maxCount * sizeof(*count));
191
+ *maxSymbolValuePtr = 0;
192
+ return 0;
193
+ }
194
+
195
+ { const svbool_t vl128 = svptrue_pat_b8(SV_VL16);
196
+ const svuint8_t c0 = svreinterpret_u8(svindex_u32(0x0C040800, 0x01010101));
197
+ const svuint8_t c1 = svadd_n_u8_x(vl128, c0, 16);
198
+ const svuint8_t c2 = svadd_n_u8_x(vl128, c0, 32);
199
+ const svuint8_t c3 = svadd_n_u8_x(vl128, c1, 32);
200
+
201
+ svuint8_t symbolMax = svdup_n_u8(0);
202
+ svuint16_t hh0 = svdup_n_u16(0);
203
+ svuint16_t hh1 = svdup_n_u16(0);
204
+ svuint16_t hh2 = svdup_n_u16(0);
205
+ svuint16_t hh3 = svdup_n_u16(0);
206
+ svuint16_t hh4 = svdup_n_u16(0);
207
+ svuint16_t hh5 = svdup_n_u16(0);
208
+ svuint16_t hh6 = svdup_n_u16(0);
209
+ svuint16_t hh7 = svdup_n_u16(0);
210
+ svuint16_t max;
211
+ size_t maxSymbolValue;
212
+
213
+ size_t i = 0;
214
+ while (i < sourceSize) {
215
+ /* We can only accumulate 15 (15 * 16 <= 255) iterations of
216
+ * histogram in 8-bit accumulators! */
217
+ const size_t size240 = min_size(i + 240, sourceSize);
218
+
219
+ svbool_t pred = svwhilelt_b8_u64(i, sourceSize);
220
+ svuint8_t c = svld1rq_u8(pred, ip + i);
221
+ svuint8_t h0 = svhistseg_u8(c0, c);
222
+ svuint8_t h1 = svhistseg_u8(c1, c);
223
+ svuint8_t h2 = svhistseg_u8(c2, c);
224
+ svuint8_t h3 = svhistseg_u8(c3, c);
225
+ symbolMax = svmax_u8_x(vl128, symbolMax, c);
226
+
227
+ for (i += 16; i < size240; i += 16) {
228
+ pred = svwhilelt_b8_u64(i, sourceSize);
229
+ c = svld1rq_u8(pred, ip + i);
230
+ h0 = svadd_u8_x(vl128, h0, svhistseg_u8(c0, c));
231
+ h1 = svadd_u8_x(vl128, h1, svhistseg_u8(c1, c));
232
+ h2 = svadd_u8_x(vl128, h2, svhistseg_u8(c2, c));
233
+ h3 = svadd_u8_x(vl128, h3, svhistseg_u8(c3, c));
234
+ symbolMax = svmax_u8_x(vl128, symbolMax, c);
235
+ }
236
+
237
+ hh0 = svaddwb_u16(hh0, h0);
238
+ hh1 = svaddwt_u16(hh1, h0);
239
+ hh2 = svaddwb_u16(hh2, h1);
240
+ hh3 = svaddwt_u16(hh3, h1);
241
+ hh4 = svaddwb_u16(hh4, h2);
242
+ hh5 = svaddwt_u16(hh5, h2);
243
+ hh6 = svaddwb_u16(hh6, h3);
244
+ hh7 = svaddwt_u16(hh7, h3);
245
+ }
246
+ maxSymbolValue = svmaxv_u8(vl128, symbolMax);
247
+
248
+ if (check && maxSymbolValue > *maxSymbolValuePtr) return ERROR(maxSymbolValue_tooSmall);
249
+ *maxSymbolValuePtr = maxSymbolValue;
250
+
251
+ /* If the buffer size is not divisible by 16, the last elements of the final
252
+ * vector register read will be zeros, and these elements must be subtracted
253
+ * from the histogram.
254
+ */
255
+ hh0 = svsub_n_u16_m(svptrue_pat_b32(SV_VL1), hh0, -sourceSize & 15);
256
+
257
+ svst1_u32(svwhilelt_b32_u64( 0, maxCount), count + 0, svshllb_n_u32(hh0, 0));
258
+ svst1_u32(svwhilelt_b32_u64( 4, maxCount), count + 4, svshllt_n_u32(hh0, 0));
259
+ svst1_u32(svwhilelt_b32_u64( 8, maxCount), count + 8, svshllb_n_u32(hh1, 0));
260
+ svst1_u32(svwhilelt_b32_u64(12, maxCount), count + 12, svshllt_n_u32(hh1, 0));
261
+ svst1_u32(svwhilelt_b32_u64(16, maxCount), count + 16, svshllb_n_u32(hh2, 0));
262
+ svst1_u32(svwhilelt_b32_u64(20, maxCount), count + 20, svshllt_n_u32(hh2, 0));
263
+ svst1_u32(svwhilelt_b32_u64(24, maxCount), count + 24, svshllb_n_u32(hh3, 0));
264
+ svst1_u32(svwhilelt_b32_u64(28, maxCount), count + 28, svshllt_n_u32(hh3, 0));
265
+ svst1_u32(svwhilelt_b32_u64(32, maxCount), count + 32, svshllb_n_u32(hh4, 0));
266
+ svst1_u32(svwhilelt_b32_u64(36, maxCount), count + 36, svshllt_n_u32(hh4, 0));
267
+ svst1_u32(svwhilelt_b32_u64(40, maxCount), count + 40, svshllb_n_u32(hh5, 0));
268
+ svst1_u32(svwhilelt_b32_u64(44, maxCount), count + 44, svshllt_n_u32(hh5, 0));
269
+ svst1_u32(svwhilelt_b32_u64(48, maxCount), count + 48, svshllb_n_u32(hh6, 0));
270
+ svst1_u32(svwhilelt_b32_u64(52, maxCount), count + 52, svshllt_n_u32(hh6, 0));
271
+ svst1_u32(svwhilelt_b32_u64(56, maxCount), count + 56, svshllb_n_u32(hh7, 0));
272
+ svst1_u32(svwhilelt_b32_u64(60, maxCount), count + 60, svshllt_n_u32(hh7, 0));
273
+
274
+ hh0 = svmax_u16_x(vl128, hh0, hh1);
275
+ hh2 = svmax_u16_x(vl128, hh2, hh3);
276
+ hh4 = svmax_u16_x(vl128, hh4, hh5);
277
+ hh6 = svmax_u16_x(vl128, hh6, hh7);
278
+ hh0 = svmax_u16_x(vl128, hh0, hh2);
279
+ hh4 = svmax_u16_x(vl128, hh4, hh6);
280
+ max = svmax_u16_x(vl128, hh0, hh4);
281
+
282
+ maxSymbolValue = min_size(maxSymbolValue, maxCount);
283
+ if (maxSymbolValue >= 64) {
284
+ const svuint8_t c4 = svadd_n_u8_x(vl128, c0, 64);
285
+ const svuint8_t c5 = svadd_n_u8_x(vl128, c1, 64);
286
+ const svuint8_t c6 = svadd_n_u8_x(vl128, c2, 64);
287
+ const svuint8_t c7 = svadd_n_u8_x(vl128, c3, 64);
288
+ const svuint8_t c8 = svadd_n_u8_x(vl128, c0, 128);
289
+ const svuint8_t c9 = svadd_n_u8_x(vl128, c1, 128);
290
+
291
+ max = HIST_count_6_sve2(ip, sourceSize, count + 64, c4, c5, c6, c7,
292
+ c8, c9, max, maxCount - 64);
293
+
294
+ if (maxSymbolValue >= 160) {
295
+ const svuint8_t ca = svadd_n_u8_x(vl128, c2, 128);
296
+ const svuint8_t cb = svadd_n_u8_x(vl128, c3, 128);
297
+ const svuint8_t cc = svadd_n_u8_x(vl128, c4, 128);
298
+ const svuint8_t cd = svadd_n_u8_x(vl128, c5, 128);
299
+ const svuint8_t ce = svadd_n_u8_x(vl128, c6, 128);
300
+ const svuint8_t cf = svadd_n_u8_x(vl128, c7, 128);
301
+
302
+ max = HIST_count_6_sve2(ip, sourceSize, count + 160, ca, cb, cc,
303
+ cd, ce, cf, max, maxCount - 160);
304
+ } else if (maxCount > 160) {
305
+ ZSTD_memset(count + 160, 0, (maxCount - 160) * sizeof(*count));
306
+ }
307
+ } else if (maxCount > 64) {
308
+ ZSTD_memset(count + 64, 0, (maxCount - 64) * sizeof(*count));
309
+ }
310
+
311
+ return svmaxv_u16(vl128, max);
312
+ }
313
+ }
314
+
315
+ static size_t HIST_count_sve2(unsigned* count, unsigned* maxSymbolValuePtr,
316
+ const void* source, size_t sourceSize,
317
+ HIST_checkInput_e check)
318
+ {
319
+ if (sourceSize <= HIST_SVE2_MAX_CHUNK_SIZE)
320
+ return HIST_count_sve2_chunk(count, maxSymbolValuePtr, source, sourceSize, check);
321
+
322
+ { const BYTE* const input = (const BYTE*)source;
323
+ unsigned const maxSymbolLimit = *maxSymbolValuePtr;
324
+ unsigned maxSymbolValue = maxSymbolLimit;
325
+ U32 chunkCounts[256];
326
+ size_t largestCount = HIST_count_sve2_chunk(count, &maxSymbolValue, input,
327
+ HIST_SVE2_MAX_CHUNK_SIZE, check);
328
+ size_t offset = HIST_SVE2_MAX_CHUNK_SIZE;
329
+ if (HIST_isError(largestCount)) return largestCount;
330
+
331
+ /* Merge each bounded histogram into U32 totals. Wrapping a present
332
+ * symbol to zero breaks Huffman coding, and undercounting breaks RLE. */
333
+ while (offset < sourceSize) {
334
+ size_t const chunkSize = min_size(sourceSize - offset, HIST_SVE2_MAX_CHUNK_SIZE);
335
+ unsigned chunkMaxSymbol = maxSymbolLimit;
336
+ size_t const result = HIST_count_sve2_chunk(chunkCounts, &chunkMaxSymbol,
337
+ input + offset, chunkSize, check);
338
+ unsigned symbol;
339
+ if (HIST_isError(result)) return result;
340
+ if (chunkMaxSymbol > maxSymbolValue) maxSymbolValue = chunkMaxSymbol;
341
+ for (symbol = 0; symbol <= chunkMaxSymbol; ++symbol) {
342
+ count[symbol] += chunkCounts[symbol];
343
+ if (count[symbol] > largestCount) largestCount = count[symbol];
344
+ }
345
+ offset += chunkSize;
346
+ }
347
+
348
+ *maxSymbolValuePtr = maxSymbolValue;
349
+ return largestCount;
350
+ }
351
+ }
352
+ #endif
353
+
68
354
  /* HIST_count_parallel_wksp() :
69
355
  * store histogram into 4 intermediate tables, recombined at the end.
70
356
  * this design makes better use of OoO cpus,
@@ -73,8 +359,8 @@ typedef enum { trustInput, checkMaxSymbolValue } HIST_checkInput_e;
73
359
  * `workSpace` must be a U32 table of size >= HIST_WKSP_SIZE_U32.
74
360
  * @return : largest histogram frequency,
75
361
  * or an error code (notably when histogram's alphabet is larger than *maxSymbolValuePtr) */
76
- static size_t HIST_count_parallel_wksp(
77
- unsigned* count, unsigned* maxSymbolValuePtr,
362
+ static UNUSED_ATTR
363
+ size_t HIST_count_parallel_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
78
364
  const void* source, size_t sourceSize,
79
365
  HIST_checkInput_e check,
80
366
  U32* const workSpace)
@@ -151,11 +437,17 @@ size_t HIST_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
151
437
  const void* source, size_t sourceSize,
152
438
  void* workSpace, size_t workSpaceSize)
153
439
  {
154
- if (sourceSize < 1500) /* heuristic threshold */
440
+ if (sourceSize < HIST_FAST_THRESHOLD) /* heuristic threshold */
155
441
  return HIST_count_simple(count, maxSymbolValuePtr, source, sourceSize);
442
+ #if defined(ZSTD_ARCH_ARM_SVE2)
443
+ (void)workSpace;
444
+ (void)workSpaceSize;
445
+ return HIST_count_sve2(count, maxSymbolValuePtr, source, sourceSize, trustInput);
446
+ #else
156
447
  if ((size_t)workSpace & 3) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */
157
448
  if (workSpaceSize < HIST_WKSP_SIZE) return ERROR(workSpace_tooSmall);
158
449
  return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, trustInput, (U32*)workSpace);
450
+ #endif
159
451
  }
160
452
 
161
453
  /* HIST_count_wksp() :
@@ -165,10 +457,15 @@ size_t HIST_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
165
457
  const void* source, size_t sourceSize,
166
458
  void* workSpace, size_t workSpaceSize)
167
459
  {
460
+ #if defined(ZSTD_ARCH_ARM_SVE2)
461
+ if (*maxSymbolValuePtr < 255)
462
+ return HIST_count_sve2(count, maxSymbolValuePtr, source, sourceSize, checkMaxSymbolValue);
463
+ #else
168
464
  if ((size_t)workSpace & 3) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */
169
465
  if (workSpaceSize < HIST_WKSP_SIZE) return ERROR(workSpace_tooSmall);
170
466
  if (*maxSymbolValuePtr < 255)
171
467
  return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, checkMaxSymbolValue, (U32*)workSpace);
468
+ #endif
172
469
  *maxSymbolValuePtr = 255;
173
470
  return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace, workSpaceSize);
174
471
  }
@@ -35,7 +35,11 @@ unsigned HIST_isError(size_t code); /**< tells if a return value is an error co
35
35
 
36
36
  /* --- advanced histogram functions --- */
37
37
 
38
+ #if defined(__ARM_FEATURE_SVE2)
39
+ #define HIST_WKSP_SIZE_U32 0
40
+ #else
38
41
  #define HIST_WKSP_SIZE_U32 1024
42
+ #endif
39
43
  #define HIST_WKSP_SIZE (HIST_WKSP_SIZE_U32 * sizeof(unsigned))
40
44
  /** HIST_count_wksp() :
41
45
  * Same as HIST_count(), but using an externally provided scratch buffer.
@@ -24,6 +24,7 @@
24
24
  * Includes
25
25
  ****************************************************************/
26
26
  #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memset */
27
+ #include "../common/bmi2.h"
27
28
  #include "../common/compiler.h"
28
29
  #include "../common/bitstream.h"
29
30
  #include "hist.h"
@@ -425,6 +426,7 @@ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 targetNbBits
425
426
  * gain back half the rank.
426
427
  */
427
428
  U32 nBitsToDecrease = ZSTD_highbit32((U32)totalCost) + 1;
429
+ assert(nBitsToDecrease <= HUF_TABLELOG_MAX+1);
428
430
  for ( ; nBitsToDecrease > 1; nBitsToDecrease--) {
429
431
  U32 const highPos = rankLast[nBitsToDecrease];
430
432
  U32 const lowPos = rankLast[nBitsToDecrease-1];
@@ -1140,9 +1142,10 @@ HUF_compress1X_usingCTable_internal(void* dst, size_t dstSize,
1140
1142
  const void* src, size_t srcSize,
1141
1143
  const HUF_CElt* CTable, const int flags)
1142
1144
  {
1143
- if (flags & HUF_flags_bmi2) {
1145
+ if (ZSTD_USE_BMI2(flags & HUF_flags_bmi2)) {
1144
1146
  return HUF_compress1X_usingCTable_internal_bmi2(dst, dstSize, src, srcSize, CTable);
1145
1147
  }
1148
+ (void)flags;
1146
1149
  return HUF_compress1X_usingCTable_internal_default(dst, dstSize, src, srcSize, CTable);
1147
1150
  }
1148
1151