Debian Codegraph @

external/blake3/blake3_dispatch.c

raw symbols SCIP
1 #cxx . . $ `<file>/external/blake3/blake3_dispatch.c`/include <stdbool.h>
2 #include <stddef.h>
3 #include <stdint.h>
4
5 #include "blake3_impl.h"cxx . . $ `<file>/external/blake3/blake3_impl.h`/
6
7 #if defined(_MSC_VER)
8 #include <Windows.h>
9 #endif
10
11 #if defined(IS_X86cxx . . $ `external/blake3/blake3_impl.h:42:9`!)
12 #if defined(_MSC_VER)
13 #include <intrin.h>
14 #elif defined(__GNUC__cxx . . $ `<built-in>:8:9`!)
15 #include <immintrin.h>
16 #else
17 #undef IS_X86 /* Unimplemented! */
18 #endif
19 #endif
20
21 #if !defined(BLAKE3_ATOMICS)
22 #if defined(__has_include)
23 #if __has_include(<stdatomic.h>) && !defined(_MSC_VER)
24 #define BLAKE3_ATOMICScxx . . $ `external/blake3/blake3_dispatch.c:24:9`! 1
25 #else
26 #define BLAKE3_ATOMICS 0
27 #endif /* __has_include(<stdatomic.h>) && !defined(_MSC_VER) */
28 #else
29 #define BLAKE3_ATOMICS 0
30 #endif /* defined(__has_include) */
31 #endif /* BLAKE3_ATOMICS */
32
33 #if BLAKE3_ATOMICScxx . . $ `external/blake3/blake3_dispatch.c:24:9`!
34 #define ATOMIC_INTcxx . . $ `external/blake3/blake3_dispatch.c:34:9`! _Atomic int
35 #define ATOMIC_LOADcxx . . $ `external/blake3/blake3_dispatch.c:35:9`!(x) x
36 #define ATOMIC_STOREcxx . . $ `external/blake3/blake3_dispatch.c:36:9`!(x, y) x = y
37 #elif defined(_MSC_VER)
38 #define ATOMIC_INT LONG
39 #define ATOMIC_LOAD(x) InterlockedOr(&x, 0)
40 #define ATOMIC_STORE(x, y) InterlockedExchange(&x, y)
41 #else
42 #define ATOMIC_INT int
43 #define ATOMIC_LOAD(x) x
44 #define ATOMIC_STORE(x, y) x = y
45 #endif
46
47 #define MAYBE_UNUSEDcxx . . $ `external/blake3/blake3_dispatch.c:47:9`!(x) (void)((x))
48
49 #if defined(IS_X86cxx . . $ `external/blake3/blake3_impl.h:42:9`!)
50 static uint64_tcxx . . $ uint64_t# xgetbvcxx . . $ xgetbv(f708dce9a8856c70).(void) {
51 #if defined(_MSC_VER)
52 return _xgetbv(0);
53 #else
54 uint32_tcxx . . $ uint32_t# eax = 0, edx = 0;
55 __asm__ __volatile__("xgetbv\n" : "=a"(eax), "=d"(edx) : "c"(0));
56 return ((uint64_tcxx . . $ uint64_t#)edx << 32) | eax;
57 #endif
58 }
59
60 static void cpuidcxx . . $ cpuid(d89196f67e9c91e5).(uint32_tcxx . . $ uint32_t# out[4], uint32_tcxx . . $ uint32_t# id) {
61 #if defined(_MSC_VER)
62 __cpuid((int *)out, id);
63 #elif defined(__i386__) || defined(_M_IX86)
64 __asm__ __volatile__("movl %%ebx, %1\n"
65 "cpuid\n"
66 "xchgl %1, %%ebx\n"
67 : "=a"(out[0]), "=r"(out[1]), "=c"(out[2]), "=d"(out[3])
68 : "a"(id));
69 #else
70 __asm__ __volatile__("cpuid\n"
71 : "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3])
72 : "a"(id));
73 #endif
74 }
75
76 static void cpuidexcxx . . $ cpuidex(c28fbbcaa6c23e04).(uint32_tcxx . . $ uint32_t# out[4], uint32_tcxx . . $ uint32_t# id, uint32_tcxx . . $ uint32_t# sid) {
77 #if defined(_MSC_VER)
78 __cpuidex((int *)out, id, sid);
79 #elif defined(__i386__) || defined(_M_IX86)
80 __asm__ __volatile__("movl %%ebx, %1\n"
81 "cpuid\n"
82 "xchgl %1, %%ebx\n"
83 : "=a"(out[0]), "=r"(out[1]), "=c"(out[2]), "=d"(out[3])
84 : "a"(id), "c"(sid));
85 #else
86 __asm__ __volatile__("cpuid\n"
87 : "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3])
88 : "a"(id), "c"(sid));
89 #endif
90 }
91
92
93 enum cpu_featurecxx . . $ cpu_feature# {
94 SSE2cxx . . $ SSE2. = 1 << 0,
95 SSSE3cxx . . $ SSSE3. = 1 << 1,
96 SSE41cxx . . $ SSE41. = 1 << 2,
97 AVXcxx . . $ AVX. = 1 << 3,
98 AVX2cxx . . $ AVX2. = 1 << 4,
99 AVX512Fcxx . . $ AVX512F. = 1 << 5,
100 AVX512VLcxx . . $ AVX512VL. = 1 << 6,
101 /* ... */
102 UNDEFINEDcxx . . $ UNDEFINED. = 1 << 30
103 };
104
105 #if !defined(BLAKE3_TESTING)
106 static /* Allow the variable to be controlled manually for testing */
107 #endif
108 ATOMIC_INTcxx . . $ `external/blake3/blake3_dispatch.c:34:9`! g_cpu_featurescxx . . $ g_cpu_features. = UNDEFINEDcxx . . $ UNDEFINED.;
109
110 #if !defined(BLAKE3_TESTING)
111 static
112 #endif
113 enum cpu_featurecxx . . $ cpu_feature#
114 get_cpu_featurescxx . . $ get_cpu_features(ee396e7ce9b53f7e).(void) {
115
116 /* If TSAN detects a data race here, try compiling with -DBLAKE3_ATOMICS=1 */
117 enum cpu_featurecxx . . $ cpu_feature# features = ATOMIC_LOADcxx . . $ `external/blake3/blake3_dispatch.c:35:9`!(g_cpu_features);
118 if (features != UNDEFINEDcxx . . $ UNDEFINED.) {
119 return features;
120 } else {
121 #if defined(IS_X86cxx . . $ `external/blake3/blake3_impl.h:42:9`!)
122 uint32_tcxx . . $ uint32_t# regs[4] = {0};
123 uint32_tcxx . . $ uint32_t# *eax = &regs[0], *ebx = &regs[1], *ecx = &regs[2], *edx = &regs[3];
124 (void)edx;
125 features = 0;
126 cpuidcxx . . $ cpuid(d89196f67e9c91e5).(regs, 0);
127 const int max_id = *eax;
128 cpuidcxx . . $ cpuid(d89196f67e9c91e5).(regs, 1);
129 #if defined(__amd64__cxx . . $ `<built-in>:371:9`!) || defined(_M_X64)
130 features |= SSE2cxx . . $ SSE2.;
131 #else
132 if (*edx & (1UL << 26))
133 features |= SSE2;
134 #endif
135 if (*ecx & (1UL << 9))
136 features |= SSSE3cxx . . $ SSSE3.;
137 if (*ecx & (1UL << 19))
138 features |= SSE41cxx . . $ SSE41.;
139
140 if (*ecx & (1UL << 27)) { // OSXSAVE
141 const uint64_tcxx . . $ uint64_t# mask = xgetbvcxx . . $ xgetbv(f708dce9a8856c70).();
142 if ((mask & 6) == 6) { // SSE and AVX states
143 if (*ecx & (1UL << 28))
144 features |= AVXcxx . . $ AVX.;
145 if (max_id >= 7) {
146 cpuidexcxx . . $ cpuidex(c28fbbcaa6c23e04).(regs, 7, 0);
147 if (*ebx & (1UL << 5))
148 features |= AVX2cxx . . $ AVX2.;
149 if ((mask & 224) == 224) { // Opmask, ZMM_Hi256, Hi16_Zmm
150 if (*ebx & (1UL << 31))
151 features |= AVX512VLcxx . . $ AVX512VL.;
152 if (*ebx & (1UL << 16))
153 features |= AVX512Fcxx . . $ AVX512F.;
154 }
155 }
156 }
157 }
158 ATOMIC_STOREcxx . . $ `external/blake3/blake3_dispatch.c:36:9`!(g_cpu_features, features);
159 return features;
160 #else
161 /* How to detect NEON? */
162 return 0;
163 #endif
164 }
165 }
166 #endif
167
168 void blake3_compress_in_placecxx . . $ blake3_compress_in_place(389a45aab4533bf).(uint32_tcxx . . $ uint32_t# cv[8],
169 const uint8_tcxx . . $ uint8_t# block[BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!],
170 uint8_tcxx . . $ uint8_t# block_len, uint64_tcxx . . $ uint64_t# counter,
171 uint8_tcxx . . $ uint8_t# flags) {
172 #if defined(IS_X86cxx . . $ `external/blake3/blake3_impl.h:42:9`!)
173 const enum cpu_featurecxx . . $ cpu_feature# features = get_cpu_featurescxx . . $ get_cpu_features(ee396e7ce9b53f7e).();
174 MAYBE_UNUSEDcxx . . $ `external/blake3/blake3_dispatch.c:47:9`!(features);
175 #if !defined(BLAKE3_NO_AVX512cxx . . $ `external/blake3/blake3.h:12:9`!)
176 if (features & AVX512VL) {
177 blake3_compress_in_place_avx512(cv, block, block_len, counter, flags);
178 return;
179 }
180 #endif
181 #if !defined(BLAKE3_NO_SSE41cxx . . $ `external/blake3/blake3.h:10:9`!)
182 if (features & SSE41) {
183 blake3_compress_in_place_sse41(cv, block, block_len, counter, flags);
184 return;
185 }
186 #endif
187 #if !defined(BLAKE3_NO_SSE2cxx . . $ `external/blake3/blake3.h:9:9`!)
188 if (features & SSE2) {
189 blake3_compress_in_place_sse2(cv, block, block_len, counter, flags);
190 return;
191 }
192 #endif
193 #endif
194 blake3_compress_in_place_portablecxx . . $ blake3_compress_in_place_portable(389a45aab4533bf).(cv, block, block_len, counter, flags);
195 }
196
197 void blake3_compress_xofcxx . . $ blake3_compress_xof(bd57d77544a3558d).(const uint32_tcxx . . $ uint32_t# cv[8],
198 const uint8_tcxx . . $ uint8_t# block[BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!],
199 uint8_tcxx . . $ uint8_t# block_len, uint64_tcxx . . $ uint64_t# counter, uint8_tcxx . . $ uint8_t# flags,
200 uint8_tcxx . . $ uint8_t# out[64]) {
201 #if defined(IS_X86cxx . . $ `external/blake3/blake3_impl.h:42:9`!)
202 const enum cpu_featurecxx . . $ cpu_feature# features = get_cpu_featurescxx . . $ get_cpu_features(ee396e7ce9b53f7e).();
203 MAYBE_UNUSEDcxx . . $ `external/blake3/blake3_dispatch.c:47:9`!(features);
204 #if !defined(BLAKE3_NO_AVX512cxx . . $ `external/blake3/blake3.h:12:9`!)
205 if (features & AVX512VL) {
206 blake3_compress_xof_avx512(cv, block, block_len, counter, flags, out);
207 return;
208 }
209 #endif
210 #if !defined(BLAKE3_NO_SSE41cxx . . $ `external/blake3/blake3.h:10:9`!)
211 if (features & SSE41) {
212 blake3_compress_xof_sse41(cv, block, block_len, counter, flags, out);
213 return;
214 }
215 #endif
216 #if !defined(BLAKE3_NO_SSE2cxx . . $ `external/blake3/blake3.h:9:9`!)
217 if (features & SSE2) {
218 blake3_compress_xof_sse2(cv, block, block_len, counter, flags, out);
219 return;
220 }
221 #endif
222 #endif
223 blake3_compress_xof_portablecxx . . $ blake3_compress_xof_portable(bd57d77544a3558d).(cv, block, block_len, counter, flags, out);
224 }
225
226
227 void blake3_xof_manycxx . . $ blake3_xof_many(79b1dc8506f1c101).(const uint32_tcxx . . $ uint32_t# cv[8],
228 const uint8_tcxx . . $ uint8_t# block[BLAKE3_BLOCK_LENcxx . . $ `external/blake3/blake3.h:44:9`!],
229 uint8_tcxx . . $ uint8_t# block_len, uint64_tcxx . . $ uint64_t# counter, uint8_tcxx . . $ uint8_t# flags,
230 uint8_tcxx . . $ uint8_t# out[64], size_tcxx . . $ size_t# outblocks) {
231 if (outblocks == 0) {
232 // The current assembly implementation always outputs at least 1 block.
233 return;
234 }
235 #if defined(IS_X86cxx . . $ `external/blake3/blake3_impl.h:42:9`!)
236 const enum cpu_featurecxx . . $ cpu_feature# features = get_cpu_featurescxx . . $ get_cpu_features(ee396e7ce9b53f7e).();
237 MAYBE_UNUSEDcxx . . $ `external/blake3/blake3_dispatch.c:47:9`!(features);
238 #if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(BLAKE3_NO_AVX512cxx . . $ `external/blake3/blake3.h:12:9`!)
239 if (features & AVX512VL) {
240 blake3_xof_many_avx512(cv, block, block_len, counter, flags, out, outblocks);
241 return;
242 }
243 #endif
244 #endif
245 for(size_tcxx . . $ size_t# i = 0; i < outblocks; ++i) {
246 blake3_compress_xofcxx . . $ blake3_compress_xof(bd57d77544a3558d).(cv, block, block_len, counter + i, flags, out + 64*i);
247 }
248 }
249
250 void blake3_hash_manycxx . . $ blake3_hash_many(6d9d480067751c4).(const uint8_tcxx . . $ uint8_t# *const *inputs, size_tcxx . . $ size_t# num_inputs,
251 size_tcxx . . $ size_t# blocks, const uint32_tcxx . . $ uint32_t# key[8], uint64_tcxx . . $ uint64_t# counter,
252 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! increment_counter, uint8_tcxx . . $ uint8_t# flags,
253 uint8_tcxx . . $ uint8_t# flags_start, uint8_tcxx . . $ uint8_t# flags_end, uint8_tcxx . . $ uint8_t# *out) {
254 #if defined(IS_X86cxx . . $ `external/blake3/blake3_impl.h:42:9`!)
255 const enum cpu_featurecxx . . $ cpu_feature# features = get_cpu_featurescxx . . $ get_cpu_features(ee396e7ce9b53f7e).();
256 MAYBE_UNUSEDcxx . . $ `external/blake3/blake3_dispatch.c:47:9`!(features);
257 #if !defined(BLAKE3_NO_AVX512cxx . . $ `external/blake3/blake3.h:12:9`!)
258 if ((features & (AVX512F|AVX512VL)) == (AVX512F|AVX512VL)) {
259 blake3_hash_many_avx512(inputs, num_inputs, blocks, key, counter,
260 increment_counter, flags, flags_start, flags_end,
261 out);
262 return;
263 }
264 #endif
265 #if !defined(BLAKE3_NO_AVX2cxx . . $ `external/blake3/blake3.h:11:9`!)
266 if (features & AVX2) {
267 blake3_hash_many_avx2(inputs, num_inputs, blocks, key, counter,
268 increment_counter, flags, flags_start, flags_end,
269 out);
270 return;
271 }
272 #endif
273 #if !defined(BLAKE3_NO_SSE41cxx . . $ `external/blake3/blake3.h:10:9`!)
274 if (features & SSE41) {
275 blake3_hash_many_sse41(inputs, num_inputs, blocks, key, counter,
276 increment_counter, flags, flags_start, flags_end,
277 out);
278 return;
279 }
280 #endif
281 #if !defined(BLAKE3_NO_SSE2cxx . . $ `external/blake3/blake3.h:9:9`!)
282 if (features & SSE2) {
283 blake3_hash_many_sse2(inputs, num_inputs, blocks, key, counter,
284 increment_counter, flags, flags_start, flags_end,
285 out);
286 return;
287 }
288 #endif
289 #endif
290
291 #if BLAKE3_USE_NEONcxx . . $ `external/blake3/blake3.h:13:9`! == 1
292 blake3_hash_many_neon(inputs, num_inputs, blocks, key, counter,
293 increment_counter, flags, flags_start, flags_end, out);
294 return;
295 #endif
296
297 blake3_hash_many_portablecxx . . $ blake3_hash_many_portable(6d9d480067751c4).(inputs, num_inputs, blocks, key, counter,
298 increment_counter, flags, flags_start, flags_end,
299 out);
300 }
301
302 // The dynamically detected SIMD degree of the current platform.
303 size_tcxx . . $ size_t# blake3_simd_degreecxx . . $ blake3_simd_degree(f708dce9a8856c70).(void) {
304 #if defined(IS_X86cxx . . $ `external/blake3/blake3_impl.h:42:9`!)
305 const enum cpu_featurecxx . . $ cpu_feature# features = get_cpu_featurescxx . . $ get_cpu_features(ee396e7ce9b53f7e).();
306 MAYBE_UNUSEDcxx . . $ `external/blake3/blake3_dispatch.c:47:9`!(features);
307 #if !defined(BLAKE3_NO_AVX512cxx . . $ `external/blake3/blake3.h:12:9`!)
308 if ((features & (AVX512F|AVX512VL)) == (AVX512F|AVX512VL)) {
309 return 16;
310 }
311 #endif
312 #if !defined(BLAKE3_NO_AVX2cxx . . $ `external/blake3/blake3.h:11:9`!)
313 if (features & AVX2) {
314 return 8;
315 }
316 #endif
317 #if !defined(BLAKE3_NO_SSE41cxx . . $ `external/blake3/blake3.h:10:9`!)
318 if (features & SSE41) {
319 return 4;
320 }
321 #endif
322 #if !defined(BLAKE3_NO_SSE2cxx . . $ `external/blake3/blake3.h:9:9`!)
323 if (features & SSE2) {
324 return 4;
325 }
326 #endif
327 #endif
328 #if BLAKE3_USE_NEONcxx . . $ `external/blake3/blake3.h:13:9`! == 1
329 return 4;
330 #endif
331 return 1;
332 }