Debian Codegraph @

mypyc/lib-rt/base64/codec_choose.c

raw
1 #include <stdbool.h>
2 #include <stdint.h>
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <stdio.h>
6
7 #include "libbase64.h"
8 #include "codecs.h"
9 #include "config.h"
10 #include "env.h"
11
12 #if (__x86_64__ || __i386__ || _M_X86 || _M_X64)
13 #define BASE64_X86
14 #if (HAVE_SSSE3 || HAVE_SSE41 || HAVE_SSE42 || HAVE_AVX || HAVE_AVX2 || HAVE_AVX512)
15 #define BASE64_X86_SIMD
16 #endif
17 #endif
18
19 #ifdef BASE64_X86
20 #ifdef _MSC_VER
21 #include <intrin.h>
22 #define __cpuid_count(__level, __count, __eax, __ebx, __ecx, __edx) \
23 { \
24 int info[4]; \
25 __cpuidex(info, __level, __count); \
26 __eax = info[0]; \
27 __ebx = info[1]; \
28 __ecx = info[2]; \
29 __edx = info[3]; \
30 }
31 #define __cpuid(__level, __eax, __ebx, __ecx, __edx) \
32 __cpuid_count(__level, 0, __eax, __ebx, __ecx, __edx)
33 #else
34 #include <cpuid.h>
35 #if HAVE_AVX512 || HAVE_AVX2 || HAVE_AVX
36 #if ((__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 2) || (__clang_major__ >= 3))
37 static inline uint64_t _xgetbv (uint32_t index)
38 {
39 uint32_t eax, edx;
40 __asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
41 return ((uint64_t)edx << 32) | eax;
42 }
43 #else
44 #error "Platform not supported"
45 #endif
46 #endif
47 #endif
48
49 #ifndef bit_AVX512vl
50 #define bit_AVX512vl (1 << 31)
51 #endif
52 #ifndef bit_AVX512vbmi
53 #define bit_AVX512vbmi (1 << 1)
54 #endif
55 #ifndef bit_AVX2
56 #define bit_AVX2 (1 << 5)
57 #endif
58 #ifndef bit_SSSE3
59 #define bit_SSSE3 (1 << 9)
60 #endif
61 #ifndef bit_SSE41
62 #define bit_SSE41 (1 << 19)
63 #endif
64 #ifndef bit_SSE42
65 #define bit_SSE42 (1 << 20)
66 #endif
67 #ifndef bit_AVX
68 #define bit_AVX (1 << 28)
69 #endif
70
71 #define bit_XSAVE_XRSTORE (1 << 27)
72
73 #ifndef _XCR_XFEATURE_ENABLED_MASK
74 #define _XCR_XFEATURE_ENABLED_MASK 0
75 #endif
76
77 #define bit_XMM (1 << 1)
78 #define bit_YMM (1 << 2)
79 #define bit_OPMASK (1 << 5)
80 #define bit_ZMM (1 << 6)
81 #define bit_HIGH_ZMM (1 << 7)
82
83 #define _XCR_XMM_AND_YMM_STATE_ENABLED_BY_OS (bit_XMM | bit_YMM)
84
85 #define _AVX_512_ENABLED_BY_OS (bit_XMM | bit_YMM | bit_OPMASK | bit_ZMM | bit_HIGH_ZMM)
86
87 #endif
88
89 // Function declarations:
90 #define BASE64_CODEC_FUNCS(arch) \
91 extern void base64_stream_encode_ ## arch BASE64_ENC_PARAMS; \
92 extern int base64_stream_decode_ ## arch BASE64_DEC_PARAMS;
93
94 BASE64_CODEC_FUNCS(avx512)
95 BASE64_CODEC_FUNCS(avx2)
96 BASE64_CODEC_FUNCS(neon32)
97 BASE64_CODEC_FUNCS(neon64)
98 BASE64_CODEC_FUNCS(plain)
99 BASE64_CODEC_FUNCS(ssse3)
100 BASE64_CODEC_FUNCS(sse41)
101 BASE64_CODEC_FUNCS(sse42)
102 BASE64_CODEC_FUNCS(avx)
103
104 static bool
105 codec_choose_forced (struct codec *codec, int flags)
106 {
107 // If the user wants to use a certain codec,
108 // always allow it, even if the codec is a no-op.
109 // For testing purposes.
110
111 if (!(flags & 0xFFFF)) {
112 return false;
113 }
114
115 if (flags & BASE64_FORCE_AVX2) {
116 codec->enc = base64_stream_encode_avx2;
117 codec->dec = base64_stream_decode_avx2;
118 return true;
119 }
120 if (flags & BASE64_FORCE_NEON32) {
121 codec->enc = base64_stream_encode_neon32;
122 codec->dec = base64_stream_decode_neon32;
123 return true;
124 }
125 if (flags & BASE64_FORCE_NEON64) {
126 codec->enc = base64_stream_encode_neon64;
127 codec->dec = base64_stream_decode_neon64;
128 return true;
129 }
130 if (flags & BASE64_FORCE_PLAIN) {
131 codec->enc = base64_stream_encode_plain;
132 codec->dec = base64_stream_decode_plain;
133 return true;
134 }
135 if (flags & BASE64_FORCE_SSSE3) {
136 codec->enc = base64_stream_encode_ssse3;
137 codec->dec = base64_stream_decode_ssse3;
138 return true;
139 }
140 if (flags & BASE64_FORCE_SSE41) {
141 codec->enc = base64_stream_encode_sse41;
142 codec->dec = base64_stream_decode_sse41;
143 return true;
144 }
145 if (flags & BASE64_FORCE_SSE42) {
146 codec->enc = base64_stream_encode_sse42;
147 codec->dec = base64_stream_decode_sse42;
148 return true;
149 }
150 if (flags & BASE64_FORCE_AVX) {
151 codec->enc = base64_stream_encode_avx;
152 codec->dec = base64_stream_decode_avx;
153 return true;
154 }
155 if (flags & BASE64_FORCE_AVX512) {
156 codec->enc = base64_stream_encode_avx512;
157 codec->dec = base64_stream_decode_avx512;
158 return true;
159 }
160 return false;
161 }
162
163 static bool
164 codec_choose_arm (struct codec *codec)
165 {
166 #if HAVE_NEON64 || ((defined(__ARM_NEON__) || defined(__ARM_NEON)) && HAVE_NEON32)
167
168 // Unfortunately there is no portable way to check for NEON
169 // support at runtime from userland in the same way that x86
170 // has cpuid, so just stick to the compile-time configuration:
171
172 #if HAVE_NEON64
173 codec->enc = base64_stream_encode_neon64;
174 codec->dec = base64_stream_decode_neon64;
175 #else
176 codec->enc = base64_stream_encode_neon32;
177 codec->dec = base64_stream_decode_neon32;
178 #endif
179
180 return true;
181
182 #else
183 (void)codec;
184 return false;
185 #endif
186 }
187
188 static bool
189 codec_choose_x86 (struct codec *codec)
190 {
191 #ifdef BASE64_X86_SIMD
192
193 unsigned int eax, ebx = 0, ecx = 0, edx;
194 unsigned int max_level;
195
196 #ifdef _MSC_VER
197 int info[4];
198 __cpuidex(info, 0, 0);
199 max_level = info[0];
200 #else
201 max_level = __get_cpuid_max(0, NULL);
202 #endif
203
204 #if HAVE_AVX512 || HAVE_AVX2 || HAVE_AVX
205 // Check for AVX/AVX2/AVX512 support:
206 // Checking for AVX requires 3 things:
207 // 1) CPUID indicates that the OS uses XSAVE and XRSTORE instructions
208 // (allowing saving YMM registers on context switch)
209 // 2) CPUID indicates support for AVX
210 // 3) XGETBV indicates the AVX registers will be saved and restored on
211 // context switch
212 //
213 // Note that XGETBV is only available on 686 or later CPUs, so the
214 // instruction needs to be conditionally run.
215 if (max_level >= 1) {
216 __cpuid_count(1, 0, eax, ebx, ecx, edx);
217 if (ecx & bit_XSAVE_XRSTORE) {
218 uint64_t xcr_mask;
219 xcr_mask = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
220 if ((xcr_mask & _XCR_XMM_AND_YMM_STATE_ENABLED_BY_OS) == _XCR_XMM_AND_YMM_STATE_ENABLED_BY_OS) { // check multiple bits at once
221 #if HAVE_AVX512
222 if (max_level >= 7 && ((xcr_mask & _AVX_512_ENABLED_BY_OS) == _AVX_512_ENABLED_BY_OS)) {
223 __cpuid_count(7, 0, eax, ebx, ecx, edx);
224 if ((ebx & bit_AVX512vl) && (ecx & bit_AVX512vbmi)) {
225 codec->enc = base64_stream_encode_avx512;
226 codec->dec = base64_stream_decode_avx512;
227 return true;
228 }
229 }
230 #endif
231 #if HAVE_AVX2
232 if (max_level >= 7) {
233 __cpuid_count(7, 0, eax, ebx, ecx, edx);
234 if (ebx & bit_AVX2) {
235 codec->enc = base64_stream_encode_avx2;
236 codec->dec = base64_stream_decode_avx2;
237 return true;
238 }
239 }
240 #endif
241 #if HAVE_AVX
242 __cpuid_count(1, 0, eax, ebx, ecx, edx);
243 if (ecx & bit_AVX) {
244 codec->enc = base64_stream_encode_avx;
245 codec->dec = base64_stream_decode_avx;
246 return true;
247 }
248 #endif
249 }
250 }
251 }
252 #endif
253
254 #if HAVE_SSE42
255 // Check for SSE42 support:
256 if (max_level >= 1) {
257 __cpuid(1, eax, ebx, ecx, edx);
258 if (ecx & bit_SSE42) {
259 codec->enc = base64_stream_encode_sse42;
260 codec->dec = base64_stream_decode_sse42;
261 return true;
262 }
263 }
264 #endif
265
266 #if HAVE_SSE41
267 // Check for SSE41 support:
268 if (max_level >= 1) {
269 __cpuid(1, eax, ebx, ecx, edx);
270 if (ecx & bit_SSE41) {
271 codec->enc = base64_stream_encode_sse41;
272 codec->dec = base64_stream_decode_sse41;
273 return true;
274 }
275 }
276 #endif
277
278 #if HAVE_SSSE3
279 // Check for SSSE3 support:
280 if (max_level >= 1) {
281 __cpuid(1, eax, ebx, ecx, edx);
282 if (ecx & bit_SSSE3) {
283 codec->enc = base64_stream_encode_ssse3;
284 codec->dec = base64_stream_decode_ssse3;
285 return true;
286 }
287 }
288 #endif
289
290 #else
291 (void)codec;
292 #endif
293
294 return false;
295 }
296
297 void
298 codec_choose (struct codec *codec, int flags)
299 {
300 // User forced a codec:
301 if (codec_choose_forced(codec, flags)) {
302 return;
303 }
304
305 // Runtime feature detection:
306 if (codec_choose_arm(codec)) {
307 return;
308 }
309 if (codec_choose_x86(codec)) {
310 return;
311 }
312 codec->enc = base64_stream_encode_plain;
313 codec->dec = base64_stream_decode_plain;
314 }