deflate-ruby 0.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 (100) hide show
  1. checksums.yaml +7 -0
  2. data/CLAUDE.md +138 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +117 -0
  5. data/ext/deflate_ruby/deflate_ruby.c +301 -0
  6. data/ext/deflate_ruby/extconf.rb +34 -0
  7. data/ext/deflate_ruby/libdeflate/CMakeLists.txt +270 -0
  8. data/ext/deflate_ruby/libdeflate/COPYING +22 -0
  9. data/ext/deflate_ruby/libdeflate/NEWS.md +494 -0
  10. data/ext/deflate_ruby/libdeflate/README.md +228 -0
  11. data/ext/deflate_ruby/libdeflate/common_defs.h +747 -0
  12. data/ext/deflate_ruby/libdeflate/lib/adler32.c +162 -0
  13. data/ext/deflate_ruby/libdeflate/lib/arm/adler32_impl.h +358 -0
  14. data/ext/deflate_ruby/libdeflate/lib/arm/cpu_features.c +230 -0
  15. data/ext/deflate_ruby/libdeflate/lib/arm/cpu_features.h +214 -0
  16. data/ext/deflate_ruby/libdeflate/lib/arm/crc32_impl.h +600 -0
  17. data/ext/deflate_ruby/libdeflate/lib/arm/crc32_pmull_helpers.h +156 -0
  18. data/ext/deflate_ruby/libdeflate/lib/arm/crc32_pmull_wide.h +226 -0
  19. data/ext/deflate_ruby/libdeflate/lib/arm/matchfinder_impl.h +78 -0
  20. data/ext/deflate_ruby/libdeflate/lib/bt_matchfinder.h +342 -0
  21. data/ext/deflate_ruby/libdeflate/lib/cpu_features_common.h +93 -0
  22. data/ext/deflate_ruby/libdeflate/lib/crc32.c +262 -0
  23. data/ext/deflate_ruby/libdeflate/lib/crc32_multipliers.h +377 -0
  24. data/ext/deflate_ruby/libdeflate/lib/crc32_tables.h +587 -0
  25. data/ext/deflate_ruby/libdeflate/lib/decompress_template.h +777 -0
  26. data/ext/deflate_ruby/libdeflate/lib/deflate_compress.c +4129 -0
  27. data/ext/deflate_ruby/libdeflate/lib/deflate_compress.h +15 -0
  28. data/ext/deflate_ruby/libdeflate/lib/deflate_constants.h +56 -0
  29. data/ext/deflate_ruby/libdeflate/lib/deflate_decompress.c +1208 -0
  30. data/ext/deflate_ruby/libdeflate/lib/gzip_compress.c +90 -0
  31. data/ext/deflate_ruby/libdeflate/lib/gzip_constants.h +45 -0
  32. data/ext/deflate_ruby/libdeflate/lib/gzip_decompress.c +144 -0
  33. data/ext/deflate_ruby/libdeflate/lib/hc_matchfinder.h +401 -0
  34. data/ext/deflate_ruby/libdeflate/lib/ht_matchfinder.h +234 -0
  35. data/ext/deflate_ruby/libdeflate/lib/lib_common.h +106 -0
  36. data/ext/deflate_ruby/libdeflate/lib/matchfinder_common.h +224 -0
  37. data/ext/deflate_ruby/libdeflate/lib/riscv/matchfinder_impl.h +97 -0
  38. data/ext/deflate_ruby/libdeflate/lib/utils.c +141 -0
  39. data/ext/deflate_ruby/libdeflate/lib/x86/adler32_impl.h +134 -0
  40. data/ext/deflate_ruby/libdeflate/lib/x86/adler32_template.h +518 -0
  41. data/ext/deflate_ruby/libdeflate/lib/x86/cpu_features.c +183 -0
  42. data/ext/deflate_ruby/libdeflate/lib/x86/cpu_features.h +169 -0
  43. data/ext/deflate_ruby/libdeflate/lib/x86/crc32_impl.h +160 -0
  44. data/ext/deflate_ruby/libdeflate/lib/x86/crc32_pclmul_template.h +495 -0
  45. data/ext/deflate_ruby/libdeflate/lib/x86/decompress_impl.h +57 -0
  46. data/ext/deflate_ruby/libdeflate/lib/x86/matchfinder_impl.h +122 -0
  47. data/ext/deflate_ruby/libdeflate/lib/zlib_compress.c +82 -0
  48. data/ext/deflate_ruby/libdeflate/lib/zlib_constants.h +21 -0
  49. data/ext/deflate_ruby/libdeflate/lib/zlib_decompress.c +104 -0
  50. data/ext/deflate_ruby/libdeflate/libdeflate-config.cmake.in +3 -0
  51. data/ext/deflate_ruby/libdeflate/libdeflate.h +411 -0
  52. data/ext/deflate_ruby/libdeflate/libdeflate.pc.in +18 -0
  53. data/ext/deflate_ruby/libdeflate/programs/CMakeLists.txt +105 -0
  54. data/ext/deflate_ruby/libdeflate/programs/benchmark.c +696 -0
  55. data/ext/deflate_ruby/libdeflate/programs/checksum.c +218 -0
  56. data/ext/deflate_ruby/libdeflate/programs/config.h.in +19 -0
  57. data/ext/deflate_ruby/libdeflate/programs/gzip.c +688 -0
  58. data/ext/deflate_ruby/libdeflate/programs/prog_util.c +521 -0
  59. data/ext/deflate_ruby/libdeflate/programs/prog_util.h +225 -0
  60. data/ext/deflate_ruby/libdeflate/programs/test_checksums.c +200 -0
  61. data/ext/deflate_ruby/libdeflate/programs/test_custom_malloc.c +155 -0
  62. data/ext/deflate_ruby/libdeflate/programs/test_incomplete_codes.c +385 -0
  63. data/ext/deflate_ruby/libdeflate/programs/test_invalid_streams.c +130 -0
  64. data/ext/deflate_ruby/libdeflate/programs/test_litrunlen_overflow.c +72 -0
  65. data/ext/deflate_ruby/libdeflate/programs/test_overread.c +95 -0
  66. data/ext/deflate_ruby/libdeflate/programs/test_slow_decompression.c +472 -0
  67. data/ext/deflate_ruby/libdeflate/programs/test_trailing_bytes.c +151 -0
  68. data/ext/deflate_ruby/libdeflate/programs/test_util.c +237 -0
  69. data/ext/deflate_ruby/libdeflate/programs/test_util.h +61 -0
  70. data/ext/deflate_ruby/libdeflate/programs/tgetopt.c +118 -0
  71. data/ext/deflate_ruby/libdeflate/scripts/android_build.sh +118 -0
  72. data/ext/deflate_ruby/libdeflate/scripts/android_tests.sh +69 -0
  73. data/ext/deflate_ruby/libdeflate/scripts/benchmark.sh +10 -0
  74. data/ext/deflate_ruby/libdeflate/scripts/checksum.sh +10 -0
  75. data/ext/deflate_ruby/libdeflate/scripts/checksum_benchmarks.sh +253 -0
  76. data/ext/deflate_ruby/libdeflate/scripts/cmake-helper.sh +17 -0
  77. data/ext/deflate_ruby/libdeflate/scripts/deflate_benchmarks.sh +119 -0
  78. data/ext/deflate_ruby/libdeflate/scripts/exec_tests.sh +38 -0
  79. data/ext/deflate_ruby/libdeflate/scripts/gen-release-archives.sh +37 -0
  80. data/ext/deflate_ruby/libdeflate/scripts/gen_bitreverse_tab.py +19 -0
  81. data/ext/deflate_ruby/libdeflate/scripts/gen_crc32_multipliers.c +199 -0
  82. data/ext/deflate_ruby/libdeflate/scripts/gen_crc32_tables.c +105 -0
  83. data/ext/deflate_ruby/libdeflate/scripts/gen_default_litlen_costs.py +44 -0
  84. data/ext/deflate_ruby/libdeflate/scripts/gen_offset_slot_map.py +29 -0
  85. data/ext/deflate_ruby/libdeflate/scripts/gzip_tests.sh +523 -0
  86. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_compress/corpus/0 +0 -0
  87. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_compress/fuzz.c +95 -0
  88. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_decompress/corpus/0 +3 -0
  89. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_decompress/fuzz.c +62 -0
  90. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/fuzz.sh +108 -0
  91. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/gzip_decompress/corpus/0 +0 -0
  92. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/gzip_decompress/fuzz.c +19 -0
  93. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/zlib_decompress/corpus/0 +3 -0
  94. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/zlib_decompress/fuzz.c +19 -0
  95. data/ext/deflate_ruby/libdeflate/scripts/run_tests.sh +416 -0
  96. data/ext/deflate_ruby/libdeflate/scripts/toolchain-i686-w64-mingw32.cmake +8 -0
  97. data/ext/deflate_ruby/libdeflate/scripts/toolchain-x86_64-w64-mingw32.cmake +8 -0
  98. data/lib/deflate_ruby/version.rb +5 -0
  99. data/lib/deflate_ruby.rb +71 -0
  100. metadata +191 -0
@@ -0,0 +1,230 @@
1
+ /*
2
+ * arm/cpu_features.c - feature detection for ARM CPUs
3
+ *
4
+ * Copyright 2018 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
+ /*
29
+ * ARM CPUs don't have a standard way for unprivileged programs to detect CPU
30
+ * features. But an OS-specific way can be used when available.
31
+ */
32
+
33
+ #ifdef __APPLE__
34
+ # undef _ANSI_SOURCE
35
+ # undef _DARWIN_C_SOURCE
36
+ # define _DARWIN_C_SOURCE /* for sysctlbyname() */
37
+ #endif
38
+
39
+ #include "../cpu_features_common.h" /* must be included first */
40
+ #include "cpu_features.h"
41
+
42
+ #ifdef ARM_CPU_FEATURES_KNOWN
43
+ /* Runtime ARM CPU feature detection is supported. */
44
+
45
+ #ifdef __linux__
46
+ /*
47
+ * On Linux, arm32 and arm64 CPU features can be detected by reading the
48
+ * AT_HWCAP and AT_HWCAP2 values from /proc/self/auxv.
49
+ *
50
+ * Ideally we'd use the C library function getauxval(), but it's not guaranteed
51
+ * to be available: it was only added to glibc in 2.16, and in Android it was
52
+ * added to API level 18 for arm32 and level 21 for arm64.
53
+ */
54
+
55
+ #include <errno.h>
56
+ #include <fcntl.h>
57
+ #include <string.h>
58
+ #include <unistd.h>
59
+
60
+ #define AT_HWCAP 16
61
+ #define AT_HWCAP2 26
62
+
63
+ static void scan_auxv(unsigned long *hwcap, unsigned long *hwcap2)
64
+ {
65
+ int fd;
66
+ unsigned long auxbuf[32];
67
+ int filled = 0;
68
+ int i;
69
+
70
+ fd = open("/proc/self/auxv", O_RDONLY);
71
+ if (fd < 0)
72
+ return;
73
+
74
+ for (;;) {
75
+ do {
76
+ int ret = read(fd, &((char *)auxbuf)[filled],
77
+ sizeof(auxbuf) - filled);
78
+ if (ret <= 0) {
79
+ if (ret < 0 && errno == EINTR)
80
+ continue;
81
+ goto out;
82
+ }
83
+ filled += ret;
84
+ } while (filled < 2 * sizeof(long));
85
+
86
+ i = 0;
87
+ do {
88
+ unsigned long type = auxbuf[i];
89
+ unsigned long value = auxbuf[i + 1];
90
+
91
+ if (type == AT_HWCAP)
92
+ *hwcap = value;
93
+ else if (type == AT_HWCAP2)
94
+ *hwcap2 = value;
95
+ i += 2;
96
+ filled -= 2 * sizeof(long);
97
+ } while (filled >= 2 * sizeof(long));
98
+
99
+ memmove(auxbuf, &auxbuf[i], filled);
100
+ }
101
+ out:
102
+ close(fd);
103
+ }
104
+
105
+ static u32 query_arm_cpu_features(void)
106
+ {
107
+ u32 features = 0;
108
+ unsigned long hwcap = 0;
109
+ unsigned long hwcap2 = 0;
110
+
111
+ scan_auxv(&hwcap, &hwcap2);
112
+
113
+ #ifdef ARCH_ARM32
114
+ STATIC_ASSERT(sizeof(long) == 4);
115
+ if (hwcap & (1 << 12)) /* HWCAP_NEON */
116
+ features |= ARM_CPU_FEATURE_NEON;
117
+ #else
118
+ STATIC_ASSERT(sizeof(long) == 8);
119
+ if (hwcap & (1 << 1)) /* HWCAP_ASIMD */
120
+ features |= ARM_CPU_FEATURE_NEON;
121
+ if (hwcap & (1 << 4)) /* HWCAP_PMULL */
122
+ features |= ARM_CPU_FEATURE_PMULL;
123
+ if (hwcap & (1 << 7)) /* HWCAP_CRC32 */
124
+ features |= ARM_CPU_FEATURE_CRC32;
125
+ if (hwcap & (1 << 17)) /* HWCAP_SHA3 */
126
+ features |= ARM_CPU_FEATURE_SHA3;
127
+ if (hwcap & (1 << 20)) /* HWCAP_ASIMDDP */
128
+ features |= ARM_CPU_FEATURE_DOTPROD;
129
+ #endif
130
+ return features;
131
+ }
132
+
133
+ #elif defined(__APPLE__)
134
+ /* On Apple platforms, arm64 CPU features can be detected via sysctlbyname(). */
135
+
136
+ #include <sys/types.h>
137
+ #include <sys/sysctl.h>
138
+ #include <TargetConditionals.h>
139
+
140
+ static const struct {
141
+ const char *name;
142
+ u32 feature;
143
+ } feature_sysctls[] = {
144
+ { "hw.optional.neon", ARM_CPU_FEATURE_NEON },
145
+ { "hw.optional.AdvSIMD", ARM_CPU_FEATURE_NEON },
146
+ { "hw.optional.arm.FEAT_PMULL", ARM_CPU_FEATURE_PMULL },
147
+ { "hw.optional.armv8_crc32", ARM_CPU_FEATURE_CRC32 },
148
+ { "hw.optional.armv8_2_sha3", ARM_CPU_FEATURE_SHA3 },
149
+ { "hw.optional.arm.FEAT_SHA3", ARM_CPU_FEATURE_SHA3 },
150
+ { "hw.optional.arm.FEAT_DotProd", ARM_CPU_FEATURE_DOTPROD },
151
+ };
152
+
153
+ static u32 query_arm_cpu_features(void)
154
+ {
155
+ u32 features = 0;
156
+ size_t i;
157
+
158
+ for (i = 0; i < ARRAY_LEN(feature_sysctls); i++) {
159
+ const char *name = feature_sysctls[i].name;
160
+ u32 val = 0;
161
+ size_t valsize = sizeof(val);
162
+
163
+ if (sysctlbyname(name, &val, &valsize, NULL, 0) == 0 &&
164
+ valsize == sizeof(val) && val == 1)
165
+ features |= feature_sysctls[i].feature;
166
+ }
167
+ return features;
168
+ }
169
+ #elif defined(_WIN32)
170
+
171
+ #include <windows.h>
172
+
173
+ #ifndef PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE /* added in Windows SDK 20348 */
174
+ # define PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE 43
175
+ #endif
176
+
177
+ static u32 query_arm_cpu_features(void)
178
+ {
179
+ u32 features = ARM_CPU_FEATURE_NEON;
180
+
181
+ if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
182
+ features |= ARM_CPU_FEATURE_PMULL;
183
+ if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
184
+ features |= ARM_CPU_FEATURE_CRC32;
185
+ if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE))
186
+ features |= ARM_CPU_FEATURE_DOTPROD;
187
+
188
+ /* FIXME: detect SHA3 support too. */
189
+
190
+ return features;
191
+ }
192
+ #else
193
+ #error "unhandled case"
194
+ #endif
195
+
196
+ static const struct cpu_feature arm_cpu_feature_table[] = {
197
+ {ARM_CPU_FEATURE_NEON, "neon"},
198
+ {ARM_CPU_FEATURE_PMULL, "pmull"},
199
+ {ARM_CPU_FEATURE_PREFER_PMULL, "prefer_pmull"},
200
+ {ARM_CPU_FEATURE_CRC32, "crc32"},
201
+ {ARM_CPU_FEATURE_SHA3, "sha3"},
202
+ {ARM_CPU_FEATURE_DOTPROD, "dotprod"},
203
+ };
204
+
205
+ volatile u32 libdeflate_arm_cpu_features = 0;
206
+
207
+ void libdeflate_init_arm_cpu_features(void)
208
+ {
209
+ u32 features = query_arm_cpu_features();
210
+
211
+ /*
212
+ * On the Apple M1 processor, crc32 instructions max out at about 25.5
213
+ * GB/s in the best case of using a 3-way or greater interleaved chunked
214
+ * implementation, whereas a pmull-based implementation achieves 68 GB/s
215
+ * provided that the stride length is large enough (about 10+ vectors
216
+ * with eor3, or 12+ without).
217
+ *
218
+ * Assume that crc32 instructions are preferable in other cases.
219
+ */
220
+ #if (defined(__APPLE__) && TARGET_OS_OSX) || defined(TEST_SUPPORT__DO_NOT_USE)
221
+ features |= ARM_CPU_FEATURE_PREFER_PMULL;
222
+ #endif
223
+
224
+ disable_cpu_features_for_testing(&features, arm_cpu_feature_table,
225
+ ARRAY_LEN(arm_cpu_feature_table));
226
+
227
+ libdeflate_arm_cpu_features = features | ARM_CPU_FEATURES_KNOWN;
228
+ }
229
+
230
+ #endif /* ARM_CPU_FEATURES_KNOWN */
@@ -0,0 +1,214 @@
1
+ /*
2
+ * arm/cpu_features.h - feature detection for ARM CPUs
3
+ *
4
+ * Copyright 2018 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_ARM_CPU_FEATURES_H
29
+ #define LIB_ARM_CPU_FEATURES_H
30
+
31
+ #include "../lib_common.h"
32
+
33
+ #if defined(ARCH_ARM32) || defined(ARCH_ARM64)
34
+
35
+ #define ARM_CPU_FEATURE_NEON (1 << 0)
36
+ #define ARM_CPU_FEATURE_PMULL (1 << 1)
37
+ /*
38
+ * PREFER_PMULL indicates that the CPU has very high pmull throughput, and so
39
+ * the 12x wide pmull-based CRC-32 implementation is likely to be faster than an
40
+ * implementation based on the crc32 instructions.
41
+ */
42
+ #define ARM_CPU_FEATURE_PREFER_PMULL (1 << 2)
43
+ #define ARM_CPU_FEATURE_CRC32 (1 << 3)
44
+ #define ARM_CPU_FEATURE_SHA3 (1 << 4)
45
+ #define ARM_CPU_FEATURE_DOTPROD (1 << 5)
46
+
47
+ #if !defined(FREESTANDING) && \
48
+ (defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)) && \
49
+ (defined(__linux__) || \
50
+ (defined(__APPLE__) && defined(ARCH_ARM64)) || \
51
+ (defined(_WIN32) && defined(ARCH_ARM64)))
52
+ /* Runtime ARM CPU feature detection is supported. */
53
+ # define ARM_CPU_FEATURES_KNOWN (1U << 31)
54
+ extern volatile u32 libdeflate_arm_cpu_features;
55
+
56
+ void libdeflate_init_arm_cpu_features(void);
57
+
58
+ static inline u32 get_arm_cpu_features(void)
59
+ {
60
+ if (libdeflate_arm_cpu_features == 0)
61
+ libdeflate_init_arm_cpu_features();
62
+ return libdeflate_arm_cpu_features;
63
+ }
64
+ #else
65
+ static inline u32 get_arm_cpu_features(void) { return 0; }
66
+ #endif
67
+
68
+ /* NEON */
69
+ #if defined(__ARM_NEON) || (defined(_MSC_VER) && defined(ARCH_ARM64))
70
+ # define HAVE_NEON(features) 1
71
+ # define HAVE_NEON_NATIVE 1
72
+ #else
73
+ # define HAVE_NEON(features) ((features) & ARM_CPU_FEATURE_NEON)
74
+ # define HAVE_NEON_NATIVE 0
75
+ #endif
76
+ /*
77
+ * With both gcc and clang, NEON intrinsics require that the main target has
78
+ * NEON enabled already. Exception: with gcc 6.1 and later (r230411 for arm32,
79
+ * r226563 for arm64), hardware floating point support is sufficient.
80
+ */
81
+ #if (defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)) && \
82
+ (HAVE_NEON_NATIVE || (GCC_PREREQ(6, 1) && defined(__ARM_FP)))
83
+ # define HAVE_NEON_INTRIN 1
84
+ # include <arm_neon.h>
85
+ #else
86
+ # define HAVE_NEON_INTRIN 0
87
+ #endif
88
+
89
+ /* PMULL */
90
+ #ifdef __ARM_FEATURE_CRYPTO
91
+ # define HAVE_PMULL(features) 1
92
+ #else
93
+ # define HAVE_PMULL(features) ((features) & ARM_CPU_FEATURE_PMULL)
94
+ #endif
95
+ #if defined(ARCH_ARM64) && HAVE_NEON_INTRIN && \
96
+ (GCC_PREREQ(7, 1) || defined(__clang__) || defined(_MSC_VER)) && \
97
+ CPU_IS_LITTLE_ENDIAN() /* untested on big endian */
98
+ # define HAVE_PMULL_INTRIN 1
99
+ /* Work around MSVC's vmull_p64() taking poly64x1_t instead of poly64_t */
100
+ # ifdef _MSC_VER
101
+ # define compat_vmull_p64(a, b) vmull_p64(vcreate_p64(a), vcreate_p64(b))
102
+ # else
103
+ # define compat_vmull_p64(a, b) vmull_p64((a), (b))
104
+ # endif
105
+ #else
106
+ # define HAVE_PMULL_INTRIN 0
107
+ #endif
108
+
109
+ /* CRC32 */
110
+ #ifdef __ARM_FEATURE_CRC32
111
+ # define HAVE_CRC32(features) 1
112
+ #else
113
+ # define HAVE_CRC32(features) ((features) & ARM_CPU_FEATURE_CRC32)
114
+ #endif
115
+ #if defined(ARCH_ARM64) && \
116
+ (defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER))
117
+ # define HAVE_CRC32_INTRIN 1
118
+ # if defined(__GNUC__) || defined(__clang__)
119
+ # include <arm_acle.h>
120
+ # endif
121
+ /*
122
+ * Use an inline assembly fallback for clang 15 and earlier, which only
123
+ * defined the crc32 intrinsics when crc32 is enabled in the main target.
124
+ */
125
+ # if defined(__clang__) && !CLANG_PREREQ(16, 0, 16000000) && \
126
+ !defined(__ARM_FEATURE_CRC32)
127
+ # undef __crc32b
128
+ # define __crc32b(a, b) \
129
+ ({ uint32_t res; \
130
+ __asm__("crc32b %w0, %w1, %w2" \
131
+ : "=r" (res) : "r" (a), "r" (b)); \
132
+ res; })
133
+ # undef __crc32h
134
+ # define __crc32h(a, b) \
135
+ ({ uint32_t res; \
136
+ __asm__("crc32h %w0, %w1, %w2" \
137
+ : "=r" (res) : "r" (a), "r" (b)); \
138
+ res; })
139
+ # undef __crc32w
140
+ # define __crc32w(a, b) \
141
+ ({ uint32_t res; \
142
+ __asm__("crc32w %w0, %w1, %w2" \
143
+ : "=r" (res) : "r" (a), "r" (b)); \
144
+ res; })
145
+ # undef __crc32d
146
+ # define __crc32d(a, b) \
147
+ ({ uint32_t res; \
148
+ __asm__("crc32x %w0, %w1, %2" \
149
+ : "=r" (res) : "r" (a), "r" (b)); \
150
+ res; })
151
+ # pragma clang diagnostic ignored "-Wgnu-statement-expression"
152
+ # endif
153
+ #else
154
+ # define HAVE_CRC32_INTRIN 0
155
+ #endif
156
+
157
+ /* SHA3 (needed for the eor3 instruction) */
158
+ #ifdef __ARM_FEATURE_SHA3
159
+ # define HAVE_SHA3(features) 1
160
+ #else
161
+ # define HAVE_SHA3(features) ((features) & ARM_CPU_FEATURE_SHA3)
162
+ #endif
163
+ #if defined(ARCH_ARM64) && HAVE_NEON_INTRIN && \
164
+ (GCC_PREREQ(9, 1) /* r268049 */ || \
165
+ CLANG_PREREQ(7, 0, 10010463) /* r338010 */)
166
+ # define HAVE_SHA3_INTRIN 1
167
+ /*
168
+ * Use an inline assembly fallback for clang 15 and earlier, which only
169
+ * defined the sha3 intrinsics when sha3 is enabled in the main target.
170
+ */
171
+ # if defined(__clang__) && !CLANG_PREREQ(16, 0, 16000000) && \
172
+ !defined(__ARM_FEATURE_SHA3)
173
+ # undef veor3q_u8
174
+ # define veor3q_u8(a, b, c) \
175
+ ({ uint8x16_t res; \
176
+ __asm__("eor3 %0.16b, %1.16b, %2.16b, %3.16b" \
177
+ : "=w" (res) : "w" (a), "w" (b), "w" (c)); \
178
+ res; })
179
+ # pragma clang diagnostic ignored "-Wgnu-statement-expression"
180
+ # endif
181
+ #else
182
+ # define HAVE_SHA3_INTRIN 0
183
+ #endif
184
+
185
+ /* dotprod */
186
+ #ifdef __ARM_FEATURE_DOTPROD
187
+ # define HAVE_DOTPROD(features) 1
188
+ #else
189
+ # define HAVE_DOTPROD(features) ((features) & ARM_CPU_FEATURE_DOTPROD)
190
+ #endif
191
+ #if defined(ARCH_ARM64) && HAVE_NEON_INTRIN && \
192
+ (GCC_PREREQ(8, 1) || CLANG_PREREQ(7, 0, 10010000) || defined(_MSC_VER))
193
+ # define HAVE_DOTPROD_INTRIN 1
194
+ /*
195
+ * Use an inline assembly fallback for clang 15 and earlier, which only
196
+ * defined the dotprod intrinsics when dotprod is enabled in the main target.
197
+ */
198
+ # if defined(__clang__) && !CLANG_PREREQ(16, 0, 16000000) && \
199
+ !defined(__ARM_FEATURE_DOTPROD)
200
+ # undef vdotq_u32
201
+ # define vdotq_u32(a, b, c) \
202
+ ({ uint32x4_t res = (a); \
203
+ __asm__("udot %0.4s, %1.16b, %2.16b" \
204
+ : "+w" (res) : "w" (b), "w" (c)); \
205
+ res; })
206
+ # pragma clang diagnostic ignored "-Wgnu-statement-expression"
207
+ # endif
208
+ #else
209
+ # define HAVE_DOTPROD_INTRIN 0
210
+ #endif
211
+
212
+ #endif /* ARCH_ARM32 || ARCH_ARM64 */
213
+
214
+ #endif /* LIB_ARM_CPU_FEATURES_H */