deflate-ruby 1.0.1 → 1.0.2

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 (138) hide show
  1. checksums.yaml +4 -4
  2. data/CLAUDE.md +95 -92
  3. data/LICENSE.txt +6 -6
  4. data/README.md +87 -65
  5. data/Rakefile +23 -0
  6. data/ext/deflate_ruby/{libdeflate/lib/x86/adler32_impl.h → adler32_impl.h} +8 -7
  7. data/ext/deflate_ruby/common_defs.h +748 -0
  8. data/ext/deflate_ruby/{libdeflate/lib/x86/cpu_features.c → cpu_features.c} +46 -16
  9. data/ext/deflate_ruby/{libdeflate/lib/x86/cpu_features.h → cpu_features.h} +2 -1
  10. data/ext/deflate_ruby/{libdeflate/lib/x86/crc32_impl.h → crc32_impl.h} +22 -23
  11. data/ext/deflate_ruby/{libdeflate/lib/crc32_multipliers.h → crc32_multipliers.h} +2 -4
  12. data/ext/deflate_ruby/{libdeflate/lib/x86/crc32_pclmul_template.h → crc32_pclmul_template.h} +23 -94
  13. data/ext/deflate_ruby/{libdeflate/lib/crc32_tables.h → crc32_tables.h} +1 -1
  14. data/ext/deflate_ruby/{libdeflate/lib/deflate_compress.c → deflate_compress.c} +59 -60
  15. data/ext/deflate_ruby/deflate_ruby.c +392 -218
  16. data/ext/deflate_ruby/deflate_ruby.h +6 -0
  17. data/ext/deflate_ruby/extconf.rb +35 -25
  18. data/ext/deflate_ruby/libdeflate/adler32.c +162 -0
  19. data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/adler32_impl.h +14 -7
  20. data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/crc32_impl.h +25 -31
  21. data/ext/deflate_ruby/libdeflate/arm/crc32_pmull_helpers.h +156 -0
  22. data/ext/deflate_ruby/libdeflate/arm/crc32_pmull_wide.h +226 -0
  23. data/ext/deflate_ruby/libdeflate/bt_matchfinder.h +342 -0
  24. data/ext/deflate_ruby/libdeflate/common_defs.h +2 -1
  25. data/ext/deflate_ruby/libdeflate/cpu_features_common.h +93 -0
  26. data/ext/deflate_ruby/libdeflate/crc32.c +262 -0
  27. data/ext/deflate_ruby/libdeflate/crc32_multipliers.h +375 -0
  28. data/ext/deflate_ruby/libdeflate/crc32_tables.h +587 -0
  29. data/ext/deflate_ruby/libdeflate/decompress_template.h +777 -0
  30. data/ext/deflate_ruby/libdeflate/deflate_compress.c +4128 -0
  31. data/ext/deflate_ruby/libdeflate/deflate_compress.h +15 -0
  32. data/ext/deflate_ruby/libdeflate/deflate_constants.h +56 -0
  33. data/ext/deflate_ruby/libdeflate/deflate_decompress.c +1208 -0
  34. data/ext/deflate_ruby/libdeflate/gzip_compress.c +90 -0
  35. data/ext/deflate_ruby/libdeflate/gzip_constants.h +45 -0
  36. data/ext/deflate_ruby/libdeflate/gzip_decompress.c +144 -0
  37. data/ext/deflate_ruby/libdeflate/hc_matchfinder.h +401 -0
  38. data/ext/deflate_ruby/libdeflate/ht_matchfinder.h +234 -0
  39. data/ext/deflate_ruby/libdeflate/lib_common.h +106 -0
  40. data/ext/deflate_ruby/libdeflate/libdeflate.h +2 -2
  41. data/ext/deflate_ruby/libdeflate/{lib/matchfinder_common.h → matchfinder_common.h} +3 -3
  42. data/ext/deflate_ruby/libdeflate/x86/adler32_impl.h +135 -0
  43. data/ext/deflate_ruby/libdeflate/x86/adler32_template.h +518 -0
  44. data/ext/deflate_ruby/libdeflate/x86/cpu_features.c +213 -0
  45. data/ext/deflate_ruby/libdeflate/x86/cpu_features.h +170 -0
  46. data/ext/deflate_ruby/libdeflate/x86/crc32_impl.h +159 -0
  47. data/ext/deflate_ruby/libdeflate/x86/crc32_pclmul_template.h +424 -0
  48. data/ext/deflate_ruby/libdeflate/x86/decompress_impl.h +57 -0
  49. data/ext/deflate_ruby/libdeflate.h +411 -0
  50. data/ext/deflate_ruby/matchfinder_common.h +224 -0
  51. data/ext/deflate_ruby/matchfinder_impl.h +122 -0
  52. data/ext/deflate_ruby/utils.c +141 -0
  53. data/ext/deflate_ruby/zlib_compress.c +82 -0
  54. data/ext/deflate_ruby/zlib_constants.h +21 -0
  55. data/ext/deflate_ruby/zlib_decompress.c +104 -0
  56. data/lib/deflate_ruby/version.rb +1 -1
  57. data/lib/deflate_ruby.rb +1 -63
  58. data/sig/deflate_ruby.rbs +4 -0
  59. data/test/test_deflate_ruby.rb +220 -0
  60. data/test/test_helper.rb +6 -0
  61. metadata +89 -144
  62. data/ext/deflate_ruby/libdeflate/CMakeLists.txt +0 -270
  63. data/ext/deflate_ruby/libdeflate/NEWS.md +0 -494
  64. data/ext/deflate_ruby/libdeflate/README.md +0 -228
  65. data/ext/deflate_ruby/libdeflate/libdeflate-config.cmake.in +0 -3
  66. data/ext/deflate_ruby/libdeflate/libdeflate.pc.in +0 -18
  67. data/ext/deflate_ruby/libdeflate/programs/CMakeLists.txt +0 -105
  68. data/ext/deflate_ruby/libdeflate/programs/benchmark.c +0 -696
  69. data/ext/deflate_ruby/libdeflate/programs/checksum.c +0 -218
  70. data/ext/deflate_ruby/libdeflate/programs/config.h.in +0 -19
  71. data/ext/deflate_ruby/libdeflate/programs/gzip.c +0 -688
  72. data/ext/deflate_ruby/libdeflate/programs/prog_util.c +0 -521
  73. data/ext/deflate_ruby/libdeflate/programs/prog_util.h +0 -225
  74. data/ext/deflate_ruby/libdeflate/programs/test_checksums.c +0 -200
  75. data/ext/deflate_ruby/libdeflate/programs/test_custom_malloc.c +0 -155
  76. data/ext/deflate_ruby/libdeflate/programs/test_incomplete_codes.c +0 -385
  77. data/ext/deflate_ruby/libdeflate/programs/test_invalid_streams.c +0 -130
  78. data/ext/deflate_ruby/libdeflate/programs/test_litrunlen_overflow.c +0 -72
  79. data/ext/deflate_ruby/libdeflate/programs/test_overread.c +0 -95
  80. data/ext/deflate_ruby/libdeflate/programs/test_slow_decompression.c +0 -472
  81. data/ext/deflate_ruby/libdeflate/programs/test_trailing_bytes.c +0 -151
  82. data/ext/deflate_ruby/libdeflate/programs/test_util.c +0 -237
  83. data/ext/deflate_ruby/libdeflate/programs/test_util.h +0 -61
  84. data/ext/deflate_ruby/libdeflate/programs/tgetopt.c +0 -118
  85. data/ext/deflate_ruby/libdeflate/scripts/android_build.sh +0 -118
  86. data/ext/deflate_ruby/libdeflate/scripts/android_tests.sh +0 -69
  87. data/ext/deflate_ruby/libdeflate/scripts/benchmark.sh +0 -10
  88. data/ext/deflate_ruby/libdeflate/scripts/checksum.sh +0 -10
  89. data/ext/deflate_ruby/libdeflate/scripts/checksum_benchmarks.sh +0 -253
  90. data/ext/deflate_ruby/libdeflate/scripts/cmake-helper.sh +0 -17
  91. data/ext/deflate_ruby/libdeflate/scripts/deflate_benchmarks.sh +0 -119
  92. data/ext/deflate_ruby/libdeflate/scripts/exec_tests.sh +0 -38
  93. data/ext/deflate_ruby/libdeflate/scripts/gen-release-archives.sh +0 -37
  94. data/ext/deflate_ruby/libdeflate/scripts/gen_bitreverse_tab.py +0 -19
  95. data/ext/deflate_ruby/libdeflate/scripts/gen_crc32_multipliers.c +0 -199
  96. data/ext/deflate_ruby/libdeflate/scripts/gen_crc32_tables.c +0 -105
  97. data/ext/deflate_ruby/libdeflate/scripts/gen_default_litlen_costs.py +0 -44
  98. data/ext/deflate_ruby/libdeflate/scripts/gen_offset_slot_map.py +0 -29
  99. data/ext/deflate_ruby/libdeflate/scripts/gzip_tests.sh +0 -523
  100. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_compress/corpus/0 +0 -0
  101. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_compress/fuzz.c +0 -95
  102. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_decompress/corpus/0 +0 -3
  103. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_decompress/fuzz.c +0 -62
  104. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/fuzz.sh +0 -108
  105. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/gzip_decompress/corpus/0 +0 -0
  106. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/gzip_decompress/fuzz.c +0 -19
  107. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/zlib_decompress/corpus/0 +0 -3
  108. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/zlib_decompress/fuzz.c +0 -19
  109. data/ext/deflate_ruby/libdeflate/scripts/run_tests.sh +0 -416
  110. data/ext/deflate_ruby/libdeflate/scripts/toolchain-i686-w64-mingw32.cmake +0 -8
  111. data/ext/deflate_ruby/libdeflate/scripts/toolchain-x86_64-w64-mingw32.cmake +0 -8
  112. /data/ext/deflate_ruby/{libdeflate/lib/adler32.c → adler32.c} +0 -0
  113. /data/ext/deflate_ruby/{libdeflate/lib/x86/adler32_template.h → adler32_template.h} +0 -0
  114. /data/ext/deflate_ruby/{libdeflate/lib/bt_matchfinder.h → bt_matchfinder.h} +0 -0
  115. /data/ext/deflate_ruby/{libdeflate/lib/cpu_features_common.h → cpu_features_common.h} +0 -0
  116. /data/ext/deflate_ruby/{libdeflate/lib/crc32.c → crc32.c} +0 -0
  117. /data/ext/deflate_ruby/{libdeflate/lib/arm/crc32_pmull_helpers.h → crc32_pmull_helpers.h} +0 -0
  118. /data/ext/deflate_ruby/{libdeflate/lib/arm/crc32_pmull_wide.h → crc32_pmull_wide.h} +0 -0
  119. /data/ext/deflate_ruby/{libdeflate/lib/x86/decompress_impl.h → decompress_impl.h} +0 -0
  120. /data/ext/deflate_ruby/{libdeflate/lib/decompress_template.h → decompress_template.h} +0 -0
  121. /data/ext/deflate_ruby/{libdeflate/lib/deflate_compress.h → deflate_compress.h} +0 -0
  122. /data/ext/deflate_ruby/{libdeflate/lib/deflate_constants.h → deflate_constants.h} +0 -0
  123. /data/ext/deflate_ruby/{libdeflate/lib/deflate_decompress.c → deflate_decompress.c} +0 -0
  124. /data/ext/deflate_ruby/{libdeflate/lib/gzip_compress.c → gzip_compress.c} +0 -0
  125. /data/ext/deflate_ruby/{libdeflate/lib/gzip_constants.h → gzip_constants.h} +0 -0
  126. /data/ext/deflate_ruby/{libdeflate/lib/gzip_decompress.c → gzip_decompress.c} +0 -0
  127. /data/ext/deflate_ruby/{libdeflate/lib/hc_matchfinder.h → hc_matchfinder.h} +0 -0
  128. /data/ext/deflate_ruby/{libdeflate/lib/ht_matchfinder.h → ht_matchfinder.h} +0 -0
  129. /data/ext/deflate_ruby/{libdeflate/lib/lib_common.h → lib_common.h} +0 -0
  130. /data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/cpu_features.c +0 -0
  131. /data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/cpu_features.h +0 -0
  132. /data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/matchfinder_impl.h +0 -0
  133. /data/ext/deflate_ruby/libdeflate/{lib/riscv → riscv}/matchfinder_impl.h +0 -0
  134. /data/ext/deflate_ruby/libdeflate/{lib/utils.c → utils.c} +0 -0
  135. /data/ext/deflate_ruby/libdeflate/{lib/x86 → x86}/matchfinder_impl.h +0 -0
  136. /data/ext/deflate_ruby/libdeflate/{lib/zlib_compress.c → zlib_compress.c} +0 -0
  137. /data/ext/deflate_ruby/libdeflate/{lib/zlib_constants.h → zlib_constants.h} +0 -0
  138. /data/ext/deflate_ruby/libdeflate/{lib/zlib_decompress.c → zlib_decompress.c} +0 -0
@@ -0,0 +1,213 @@
1
+ /*
2
+ * x86/cpu_features.c - feature detection for x86 CPUs
3
+ *
4
+ * Copyright 2016 Eric Biggers
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person
7
+ * obtaining a copy of this software and associated documentation
8
+ * files (the "Software"), to deal in the Software without
9
+ * restriction, including without limitation the rights to use,
10
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the
12
+ * Software is furnished to do so, subject to the following
13
+ * conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
+ * OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ #include "../cpu_features_common.h" /* must be included first */
29
+ #include "cpu_features.h"
30
+
31
+ #ifdef X86_CPU_FEATURES_KNOWN
32
+ /* Runtime x86 CPU feature detection is supported. */
33
+
34
+ /* Execute the CPUID instruction. */
35
+ static inline void
36
+ cpuid(u32 leaf, u32 subleaf, u32 *a, u32 *b, u32 *c, u32 *d)
37
+ {
38
+ #ifdef _MSC_VER
39
+ int result[4];
40
+
41
+ __cpuidex(result, leaf, subleaf);
42
+ *a = result[0];
43
+ *b = result[1];
44
+ *c = result[2];
45
+ *d = result[3];
46
+ #else
47
+ __asm__ volatile("cpuid" : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d)
48
+ : "a" (leaf), "c" (subleaf));
49
+ #endif
50
+ }
51
+
52
+ /* Read an extended control register. */
53
+ static inline u64
54
+ read_xcr(u32 index)
55
+ {
56
+ #ifdef _MSC_VER
57
+ return _xgetbv(index);
58
+ #else
59
+ u32 d, a;
60
+
61
+ /*
62
+ * Execute the "xgetbv" instruction. Old versions of binutils do not
63
+ * recognize this instruction, so list the raw bytes instead.
64
+ *
65
+ * This must be 'volatile' to prevent this code from being moved out
66
+ * from under the check for OSXSAVE.
67
+ */
68
+ __asm__ volatile(".byte 0x0f, 0x01, 0xd0" :
69
+ "=d" (d), "=a" (a) : "c" (index));
70
+
71
+ return ((u64)d << 32) | a;
72
+ #endif
73
+ }
74
+
75
+ static const struct cpu_feature x86_cpu_feature_table[] = {
76
+ {X86_CPU_FEATURE_SSE2, "sse2"},
77
+ {X86_CPU_FEATURE_PCLMULQDQ, "pclmulqdq"},
78
+ {X86_CPU_FEATURE_AVX, "avx"},
79
+ {X86_CPU_FEATURE_AVX2, "avx2"},
80
+ {X86_CPU_FEATURE_BMI2, "bmi2"},
81
+ {X86_CPU_FEATURE_ZMM, "zmm"},
82
+ {X86_CPU_FEATURE_AVX512BW, "avx512bw"},
83
+ {X86_CPU_FEATURE_AVX512VL, "avx512vl"},
84
+ {X86_CPU_FEATURE_VPCLMULQDQ, "vpclmulqdq"},
85
+ {X86_CPU_FEATURE_AVX512VNNI, "avx512_vnni"},
86
+ {X86_CPU_FEATURE_AVXVNNI, "avx_vnni"},
87
+ };
88
+
89
+ volatile u32 libdeflate_x86_cpu_features = 0;
90
+
91
+ static inline bool
92
+ os_supports_avx512(u64 xcr0)
93
+ {
94
+ #ifdef __APPLE__
95
+ /*
96
+ * The Darwin kernel had a bug where it could corrupt the opmask
97
+ * registers. See
98
+ * https://community.intel.com/t5/Software-Tuning-Performance/MacOS-Darwin-kernel-bug-clobbers-AVX-512-opmask-register-state/m-p/1327259
99
+ * Darwin also does not initially set the XCR0 bits for AVX512, but they
100
+ * are set if the thread tries to use AVX512 anyway. Thus, to safely
101
+ * and consistently use AVX512 on macOS we'd need to check the kernel
102
+ * version as well as detect AVX512 support using a macOS-specific
103
+ * method. We don't bother with this, especially given Apple's
104
+ * transition to arm64.
105
+ */
106
+ return false;
107
+ #else
108
+ return (xcr0 & 0xe6) == 0xe6;
109
+ #endif
110
+ }
111
+
112
+ /*
113
+ * Don't use 512-bit vectors (ZMM registers) on Intel CPUs before Rocket Lake
114
+ * and Sapphire Rapids, due to the overly-eager downclocking which can reduce
115
+ * the performance of workloads that use ZMM registers only occasionally.
116
+ */
117
+ static inline bool
118
+ allow_512bit_vectors(const u32 manufacturer[3], u32 family, u32 model)
119
+ {
120
+ #ifdef TEST_SUPPORT__DO_NOT_USE
121
+ return true;
122
+ #endif
123
+ if (memcmp(manufacturer, "GenuineIntel", 12) != 0)
124
+ return true;
125
+ if (family != 6)
126
+ return true;
127
+ switch (model) {
128
+ case 85: /* Skylake (Server), Cascade Lake, Cooper Lake */
129
+ case 106: /* Ice Lake (Server) */
130
+ case 108: /* Ice Lake (Server) */
131
+ case 126: /* Ice Lake (Client) */
132
+ case 140: /* Tiger Lake */
133
+ case 141: /* Tiger Lake */
134
+ return false;
135
+ }
136
+ return true;
137
+ }
138
+
139
+ /* Initialize libdeflate_x86_cpu_features. */
140
+ void libdeflate_init_x86_cpu_features(void)
141
+ {
142
+ u32 max_leaf;
143
+ u32 manufacturer[3];
144
+ u32 family, model;
145
+ u32 a, b, c, d;
146
+ u64 xcr0 = 0;
147
+ u32 features = 0;
148
+
149
+ /* EAX=0: Highest Function Parameter and Manufacturer ID */
150
+ cpuid(0, 0, &max_leaf, &manufacturer[0], &manufacturer[2],
151
+ &manufacturer[1]);
152
+ if (max_leaf < 1)
153
+ goto out;
154
+
155
+ /* EAX=1: Processor Info and Feature Bits */
156
+ cpuid(1, 0, &a, &b, &c, &d);
157
+ family = (a >> 8) & 0xf;
158
+ model = (a >> 4) & 0xf;
159
+ if (family == 6 || family == 0xf)
160
+ model += (a >> 12) & 0xf0;
161
+ if (family == 0xf)
162
+ family += (a >> 20) & 0xff;
163
+ if (d & (1 << 26))
164
+ features |= X86_CPU_FEATURE_SSE2;
165
+ /*
166
+ * No known CPUs have pclmulqdq without sse4.1, so in practice code
167
+ * targeting pclmulqdq can use sse4.1 instructions. But to be safe,
168
+ * explicitly check for both the pclmulqdq and sse4.1 bits.
169
+ */
170
+ if ((c & (1 << 1)) && (c & (1 << 19)))
171
+ features |= X86_CPU_FEATURE_PCLMULQDQ;
172
+ if (c & (1 << 27))
173
+ xcr0 = read_xcr(0);
174
+ if ((c & (1 << 28)) && ((xcr0 & 0x6) == 0x6))
175
+ features |= X86_CPU_FEATURE_AVX;
176
+
177
+ if (max_leaf < 7)
178
+ goto out;
179
+
180
+ /* EAX=7, ECX=0: Extended Features */
181
+ cpuid(7, 0, &a, &b, &c, &d);
182
+ if (b & (1 << 8))
183
+ features |= X86_CPU_FEATURE_BMI2;
184
+ if ((xcr0 & 0x6) == 0x6) {
185
+ if (b & (1 << 5))
186
+ features |= X86_CPU_FEATURE_AVX2;
187
+ if (c & (1 << 10))
188
+ features |= X86_CPU_FEATURE_VPCLMULQDQ;
189
+ }
190
+ if (os_supports_avx512(xcr0)) {
191
+ if (allow_512bit_vectors(manufacturer, family, model))
192
+ features |= X86_CPU_FEATURE_ZMM;
193
+ if (b & (1 << 30))
194
+ features |= X86_CPU_FEATURE_AVX512BW;
195
+ if (b & (1U << 31))
196
+ features |= X86_CPU_FEATURE_AVX512VL;
197
+ if (c & (1 << 11))
198
+ features |= X86_CPU_FEATURE_AVX512VNNI;
199
+ }
200
+
201
+ /* EAX=7, ECX=1: Extended Features */
202
+ cpuid(7, 1, &a, &b, &c, &d);
203
+ if ((a & (1 << 4)) && ((xcr0 & 0x6) == 0x6))
204
+ features |= X86_CPU_FEATURE_AVXVNNI;
205
+
206
+ out:
207
+ disable_cpu_features_for_testing(&features, x86_cpu_feature_table,
208
+ ARRAY_LEN(x86_cpu_feature_table));
209
+
210
+ libdeflate_x86_cpu_features = features | X86_CPU_FEATURES_KNOWN;
211
+ }
212
+
213
+ #endif /* X86_CPU_FEATURES_KNOWN */
@@ -0,0 +1,170 @@
1
+ /*
2
+ * x86/cpu_features.h - feature detection for x86 CPUs
3
+ *
4
+ * Copyright 2016 Eric Biggers
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person
7
+ * obtaining a copy of this software and associated documentation
8
+ * files (the "Software"), to deal in the Software without
9
+ * restriction, including without limitation the rights to use,
10
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the
12
+ * Software is furnished to do so, subject to the following
13
+ * conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
+ * OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ #ifndef LIB_X86_CPU_FEATURES_H
29
+ #define LIB_X86_CPU_FEATURES_H
30
+
31
+ #include "../lib_common.h"
32
+
33
+ #if defined(ARCH_X86_32) || defined(ARCH_X86_64)
34
+
35
+ #define X86_CPU_FEATURE_SSE2 (1 << 0)
36
+ #define X86_CPU_FEATURE_PCLMULQDQ (1 << 1)
37
+ #define X86_CPU_FEATURE_AVX (1 << 2)
38
+ #define X86_CPU_FEATURE_AVX2 (1 << 3)
39
+ #define X86_CPU_FEATURE_BMI2 (1 << 4)
40
+ /*
41
+ * ZMM indicates whether 512-bit vectors (zmm registers) should be used. On
42
+ * some CPUs, to avoid downclocking issues we don't set ZMM even if the CPU and
43
+ * operating system support AVX-512. On these CPUs, we may still use AVX-512
44
+ * instructions, but only with xmm and ymm registers.
45
+ */
46
+ #define X86_CPU_FEATURE_ZMM (1 << 5)
47
+ #define X86_CPU_FEATURE_AVX512BW (1 << 6)
48
+ #define X86_CPU_FEATURE_AVX512VL (1 << 7)
49
+ #define X86_CPU_FEATURE_VPCLMULQDQ (1 << 8)
50
+ #define X86_CPU_FEATURE_AVX512VNNI (1 << 9)
51
+ #define X86_CPU_FEATURE_AVXVNNI (1 << 10)
52
+
53
+ #if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
54
+ /* Runtime x86 CPU feature detection is supported. */
55
+ # define X86_CPU_FEATURES_KNOWN (1U << 31)
56
+ extern volatile u32 libdeflate_x86_cpu_features;
57
+
58
+ void libdeflate_init_x86_cpu_features(void);
59
+
60
+ static inline u32 get_x86_cpu_features(void)
61
+ {
62
+ if (libdeflate_x86_cpu_features == 0)
63
+ libdeflate_init_x86_cpu_features();
64
+ return libdeflate_x86_cpu_features;
65
+ }
66
+ /*
67
+ * x86 intrinsics are also supported. Include the headers needed to use them.
68
+ * Normally just immintrin.h suffices. With clang in MSVC compatibility mode,
69
+ * immintrin.h incorrectly skips including sub-headers, so include those too.
70
+ */
71
+ # include <immintrin.h>
72
+ # if defined(_MSC_VER) && defined(__clang__)
73
+ # include <tmmintrin.h>
74
+ # include <smmintrin.h>
75
+ # include <wmmintrin.h>
76
+ # include <avxintrin.h>
77
+ # include <avx2intrin.h>
78
+ # include <avx512fintrin.h>
79
+ # include <avx512bwintrin.h>
80
+ # include <avx512vlintrin.h>
81
+ # if __has_include(<avx512vlbwintrin.h>)
82
+ # include <avx512vlbwintrin.h>
83
+ # endif
84
+ # if __has_include(<vpclmulqdqintrin.h>)
85
+ # include <vpclmulqdqintrin.h>
86
+ # endif
87
+ # if __has_include(<avx512vnniintrin.h>)
88
+ # include <avx512vnniintrin.h>
89
+ # endif
90
+ # if __has_include(<avx512vlvnniintrin.h>)
91
+ # include <avx512vlvnniintrin.h>
92
+ # endif
93
+ # if __has_include(<avxvnniintrin.h>)
94
+ # include <avxvnniintrin.h>
95
+ # endif
96
+ # endif
97
+ #else
98
+ static inline u32 get_x86_cpu_features(void) { return 0; }
99
+ #endif
100
+
101
+ #if defined(__SSE2__) || \
102
+ (defined(_MSC_VER) && \
103
+ (defined(ARCH_X86_64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 2)))
104
+ # define HAVE_SSE2(features) 1
105
+ # define HAVE_SSE2_NATIVE 1
106
+ #else
107
+ # define HAVE_SSE2(features) ((features) & X86_CPU_FEATURE_SSE2)
108
+ # define HAVE_SSE2_NATIVE 0
109
+ #endif
110
+
111
+ #if (defined(__PCLMUL__) && defined(__SSE4_1__)) || \
112
+ (defined(_MSC_VER) && defined(__AVX2__))
113
+ # define HAVE_PCLMULQDQ(features) 1
114
+ #else
115
+ # define HAVE_PCLMULQDQ(features) ((features) & X86_CPU_FEATURE_PCLMULQDQ)
116
+ #endif
117
+
118
+ #ifdef __AVX__
119
+ # define HAVE_AVX(features) 1
120
+ #else
121
+ # define HAVE_AVX(features) ((features) & X86_CPU_FEATURE_AVX)
122
+ #endif
123
+
124
+ #ifdef __AVX2__
125
+ # define HAVE_AVX2(features) 1
126
+ #else
127
+ # define HAVE_AVX2(features) ((features) & X86_CPU_FEATURE_AVX2)
128
+ #endif
129
+
130
+ #if defined(__BMI2__) || (defined(_MSC_VER) && defined(__AVX2__))
131
+ # define HAVE_BMI2(features) 1
132
+ # define HAVE_BMI2_NATIVE 1
133
+ #else
134
+ # define HAVE_BMI2(features) ((features) & X86_CPU_FEATURE_BMI2)
135
+ # define HAVE_BMI2_NATIVE 0
136
+ #endif
137
+
138
+ #ifdef __AVX512BW__
139
+ # define HAVE_AVX512BW(features) 1
140
+ #else
141
+ # define HAVE_AVX512BW(features) ((features) & X86_CPU_FEATURE_AVX512BW)
142
+ #endif
143
+
144
+ #ifdef __AVX512VL__
145
+ # define HAVE_AVX512VL(features) 1
146
+ #else
147
+ # define HAVE_AVX512VL(features) ((features) & X86_CPU_FEATURE_AVX512VL)
148
+ #endif
149
+
150
+ #ifdef __VPCLMULQDQ__
151
+ # define HAVE_VPCLMULQDQ(features) 1
152
+ #else
153
+ # define HAVE_VPCLMULQDQ(features) ((features) & X86_CPU_FEATURE_VPCLMULQDQ)
154
+ #endif
155
+
156
+ #ifdef __AVX512VNNI__
157
+ # define HAVE_AVX512VNNI(features) 1
158
+ #else
159
+ # define HAVE_AVX512VNNI(features) ((features) & X86_CPU_FEATURE_AVX512VNNI)
160
+ #endif
161
+
162
+ #ifdef __AVXVNNI__
163
+ # define HAVE_AVXVNNI(features) 1
164
+ #else
165
+ # define HAVE_AVXVNNI(features) ((features) & X86_CPU_FEATURE_AVXVNNI)
166
+ #endif
167
+
168
+ #endif /* ARCH_X86_32 || ARCH_X86_64 */
169
+
170
+ #endif /* LIB_X86_CPU_FEATURES_H */
@@ -0,0 +1,159 @@
1
+ /*
2
+ * x86/crc32_impl.h - x86 implementations of the gzip CRC-32 algorithm
3
+ *
4
+ * Copyright 2016 Eric Biggers
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person
7
+ * obtaining a copy of this software and associated documentation
8
+ * files (the "Software"), to deal in the Software without
9
+ * restriction, including without limitation the rights to use,
10
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the
12
+ * Software is furnished to do so, subject to the following
13
+ * conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
+ * OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ #ifndef LIB_X86_CRC32_IMPL_H
29
+ #define LIB_X86_CRC32_IMPL_H
30
+
31
+ #include "cpu_features.h"
32
+
33
+ /*
34
+ * pshufb(x, shift_tab[len..len+15]) left shifts x by 16-len bytes.
35
+ * pshufb(x, shift_tab[len+16..len+31]) right shifts x by len bytes.
36
+ */
37
+ static const u8 MAYBE_UNUSED shift_tab[48] = {
38
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
40
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
41
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
42
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44
+ };
45
+
46
+ #if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
47
+ /*
48
+ * PCLMULQDQ implementation. This targets PCLMULQDQ+SSE4.1, since in practice
49
+ * all CPUs that support PCLMULQDQ also support SSE4.1.
50
+ */
51
+ # define crc32_x86_pclmulqdq crc32_x86_pclmulqdq
52
+ # define SUFFIX _pclmulqdq
53
+ # define ATTRIBUTES _target_attribute("pclmul,sse4.1")
54
+ # define VL 16
55
+ # define USE_AVX512 0
56
+ # include "crc32_pclmul_template.h"
57
+
58
+ /*
59
+ * PCLMULQDQ/AVX implementation. Same as above, but this is compiled with AVX
60
+ * enabled so that the compiler can generate VEX-coded instructions which can be
61
+ * slightly more efficient. It still uses 128-bit vectors.
62
+ */
63
+ # define crc32_x86_pclmulqdq_avx crc32_x86_pclmulqdq_avx
64
+ # define SUFFIX _pclmulqdq_avx
65
+ # define ATTRIBUTES _target_attribute("pclmul,avx")
66
+ # define VL 16
67
+ # define USE_AVX512 0
68
+ # include "crc32_pclmul_template.h"
69
+ #endif
70
+
71
+ /*
72
+ * VPCLMULQDQ/AVX2 implementation. This is used on CPUs that have AVX2 and
73
+ * VPCLMULQDQ but don't have AVX-512, for example Intel Alder Lake.
74
+ *
75
+ * Currently this can't be enabled with MSVC because MSVC has a bug where it
76
+ * incorrectly assumes that VPCLMULQDQ implies AVX-512:
77
+ * https://developercommunity.visualstudio.com/t/Compiler-incorrectly-assumes-VAES-and-VP/10578785
78
+ *
79
+ * gcc 8.1 and 8.2 had a similar bug where they assumed that
80
+ * _mm256_clmulepi64_epi128() always needed AVX512. It's fixed in gcc 8.3.
81
+ *
82
+ * _mm256_zextsi128_si256() requires gcc 10.
83
+ */
84
+ #if (GCC_PREREQ(10, 1) || CLANG_PREREQ(6, 0, 10000000)) && \
85
+ !defined(LIBDEFLATE_ASSEMBLER_DOES_NOT_SUPPORT_VPCLMULQDQ)
86
+ # define crc32_x86_vpclmulqdq_avx2 crc32_x86_vpclmulqdq_avx2
87
+ # define SUFFIX _vpclmulqdq_avx2
88
+ # define ATTRIBUTES _target_attribute("vpclmulqdq,pclmul,avx2")
89
+ # define VL 32
90
+ # define USE_AVX512 0
91
+ # include "crc32_pclmul_template.h"
92
+ #endif
93
+
94
+ #if (GCC_PREREQ(10, 1) || CLANG_PREREQ(6, 0, 10000000) || MSVC_PREREQ(1920)) && \
95
+ !defined(LIBDEFLATE_ASSEMBLER_DOES_NOT_SUPPORT_VPCLMULQDQ)
96
+ /*
97
+ * VPCLMULQDQ/AVX512 implementation using 256-bit vectors. This is very similar
98
+ * to the VPCLMULQDQ/AVX2 implementation but takes advantage of the vpternlog
99
+ * instruction and more registers. This is used on certain older Intel CPUs,
100
+ * specifically Ice Lake and Tiger Lake, which support VPCLMULQDQ and AVX512 but
101
+ * downclock a bit too eagerly when ZMM registers are used.
102
+ *
103
+ * _mm256_zextsi128_si256() requires gcc 10.
104
+ */
105
+ # define crc32_x86_vpclmulqdq_avx512_vl256 crc32_x86_vpclmulqdq_avx512_vl256
106
+ # define SUFFIX _vpclmulqdq_avx512_vl256
107
+ # define ATTRIBUTES _target_attribute("vpclmulqdq,pclmul,avx512bw,avx512vl")
108
+ # define VL 32
109
+ # define USE_AVX512 1
110
+ # include "crc32_pclmul_template.h"
111
+
112
+ /*
113
+ * VPCLMULQDQ/AVX512 implementation using 512-bit vectors. This is used on CPUs
114
+ * that have a good AVX-512 implementation including VPCLMULQDQ.
115
+ *
116
+ * _mm512_zextsi128_si512() requires gcc 10.
117
+ */
118
+ # define crc32_x86_vpclmulqdq_avx512_vl512 crc32_x86_vpclmulqdq_avx512_vl512
119
+ # define SUFFIX _vpclmulqdq_avx512_vl512
120
+ # define ATTRIBUTES _target_attribute("vpclmulqdq,pclmul,avx512bw,avx512vl")
121
+ # define VL 64
122
+ # define USE_AVX512 1
123
+ # include "crc32_pclmul_template.h"
124
+ #endif
125
+
126
+ static inline crc32_func_t
127
+ arch_select_crc32_func(void)
128
+ {
129
+ const u32 features MAYBE_UNUSED = get_x86_cpu_features();
130
+
131
+ #ifdef crc32_x86_vpclmulqdq_avx512_vl512
132
+ if ((features & X86_CPU_FEATURE_ZMM) &&
133
+ HAVE_VPCLMULQDQ(features) && HAVE_PCLMULQDQ(features) &&
134
+ HAVE_AVX512BW(features) && HAVE_AVX512VL(features))
135
+ return crc32_x86_vpclmulqdq_avx512_vl512;
136
+ #endif
137
+ #ifdef crc32_x86_vpclmulqdq_avx512_vl256
138
+ if (HAVE_VPCLMULQDQ(features) && HAVE_PCLMULQDQ(features) &&
139
+ HAVE_AVX512BW(features) && HAVE_AVX512VL(features))
140
+ return crc32_x86_vpclmulqdq_avx512_vl256;
141
+ #endif
142
+ #ifdef crc32_x86_vpclmulqdq_avx2
143
+ if (HAVE_VPCLMULQDQ(features) && HAVE_PCLMULQDQ(features) &&
144
+ HAVE_AVX2(features))
145
+ return crc32_x86_vpclmulqdq_avx2;
146
+ #endif
147
+ #ifdef crc32_x86_pclmulqdq_avx
148
+ if (HAVE_PCLMULQDQ(features) && HAVE_AVX(features))
149
+ return crc32_x86_pclmulqdq_avx;
150
+ #endif
151
+ #ifdef crc32_x86_pclmulqdq
152
+ if (HAVE_PCLMULQDQ(features))
153
+ return crc32_x86_pclmulqdq;
154
+ #endif
155
+ return NULL;
156
+ }
157
+ #define arch_select_crc32_func arch_select_crc32_func
158
+
159
+ #endif /* LIB_X86_CRC32_IMPL_H */