Skip to content

Commit 6069d29

Browse files
committed
gh-124951: Enable the SSSE3 base64 kernels with MSVC and clang-cl
Extend the compile-time gate to x64 Windows builds. MSVC accepts the SSSE3 intrinsics without a target option, so the kernels need no attribute there; clang-cl still gets the function target attribute. __builtin_cpu_supports() needs support from the compiler's runtime library, which the MSVC runtime does not provide, so on those toolchains the run-time check asks CPUID directly and caches the answer. Not yet built with MSVC. Checked by compiling with Clang for x86_64-pc-windows-msvc and by running the CPUID branch on Linux with a stand-in for __cpuid(). Co-authored with Claude Fable 5.2.
1 parent 6ecf8ce commit 6069d29

1 file changed

Lines changed: 34 additions & 6 deletions

File tree

‎Modules/binascii.c‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@
6363
#ifdef USE_ZLIB_CRC32
6464
# include "zlib.h"
6565
#endif
66-
#if defined(__x86_64__) && defined(__GNUC__)
66+
#if (defined(__x86_64__) && defined(__GNUC__)) \
67+
|| (defined(_M_X64) && !defined(_M_ARM64EC))
6768
# include <tmmintrin.h> // SSSE3 intrinsics
69+
# ifdef _MSC_VER
70+
# include <intrin.h> // __cpuid()
71+
# endif
6872
# define BASE64_HAVE_SSSE3 1
6973
#endif
7074

@@ -146,16 +150,40 @@ base64_encode_trio(const unsigned char *in, unsigned char *out,
146150
* handled here. Neither kernel reads outside [in, in + in_len) or writes
147151
* more than the scalar code would for the consumed prefix.
148152
*
149-
* SSSE3 is part of x86-64-v2. A build that does not target it compiles
150-
* the kernels with a function target attribute and checks the CPU at run
151-
* time.
153+
* SSSE3 is part of x86-64-v2. A build that does not target it checks the
154+
* CPU at run time. GCC and Clang then need a function target attribute to
155+
* compile the kernels; MSVC accepts the intrinsics as they are.
152156
*/
153157
# ifdef __SSSE3__
154158
# define BASE64_SSSE3_TARGET
155159
# define base64_cpu_has_ssse3() 1
156160
# else
157-
# define BASE64_SSSE3_TARGET __attribute__((target("ssse3")))
158-
# define base64_cpu_has_ssse3() __builtin_cpu_supports("ssse3")
161+
# if defined(__GNUC__) || defined(__clang__)
162+
# define BASE64_SSSE3_TARGET __attribute__((target("ssse3")))
163+
# else
164+
# define BASE64_SSSE3_TARGET
165+
# endif
166+
# ifdef _MSC_VER
167+
/* 0 until CPUID has been asked, then 1 if the CPU has SSSE3 and -1 if not.
168+
* __builtin_cpu_supports() is not used with the MSVC runtime, including by
169+
* clang-cl, since it needs support from the compiler's runtime library. */
170+
static int base64_ssse3_state;
171+
172+
static inline int
173+
base64_cpu_has_ssse3(void)
174+
{
175+
int state = _Py_atomic_load_int_relaxed(&base64_ssse3_state);
176+
if (state == 0) {
177+
int info[4];
178+
__cpuid(info, 1);
179+
state = (info[2] & (1 << 9)) ? 1 : -1; // ECX bit 9: SSSE3
180+
_Py_atomic_store_int_relaxed(&base64_ssse3_state, state);
181+
}
182+
return state > 0;
183+
}
184+
# else
185+
# define base64_cpu_has_ssse3() __builtin_cpu_supports("ssse3")
186+
# endif
159187
# endif
160188

161189
/* Encode groups of 12 bytes into 16 characters. Every step loads 16

0 commit comments

Comments
 (0)