From e65fa006bec60192112f0a3f23174ef0528f8cc4 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 25 Jun 2023 22:10:22 -0400 Subject: [PATCH] Simplify use of cpuid and xgetbv on Windows In the past we were pivoting on Visual Studio versions, and trying to use intrinsics when available. The updated code just uses our versions of the calls. --- cpu.cpp | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/cpu.cpp b/cpu.cpp index 0542855d..44b57c4d 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -78,9 +78,10 @@ unsigned long int getauxval(unsigned long int) { return 0; } # include #endif -// Visual Studio 2008 and below are missing _xgetbv and _cpuidex. -// The 32-bit versions use inline ASM below. The 64-bit versions are in x64dll.asm. -#if defined(CRYPTOPP_MSC_VERSION) && defined(_M_X64) +// Required by Visual Studio 2008 and below and Clang on Windows. +// Use it for all MSVC-compatible compilers. +// XGETBV64 and CPUID64 are in x64dll.asm. +#if defined(_M_X64) && defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY) extern "C" unsigned long long __fastcall XGETBV64(unsigned int); extern "C" unsigned long long __fastcall CPUID64(unsigned int, unsigned int, unsigned int*); #endif @@ -387,19 +388,15 @@ extern bool CPU_ProbeSSE2(); // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85684. word64 XGetBV(word32 num) { -// Visual Studio 2010 SP1 and above, 32 and 64-bit -// https://github.com/weidai11/cryptopp/issues/972 -#if defined(CRYPTOPP_MSC_VERSION) && (_MSC_FULL_VER >= 160040219) - - return _xgetbv(num); - -// Visual Studio 2008 and below, 64-bit -#elif defined(CRYPTOPP_MSC_VERSION) && defined(_M_X64) +// Required by Visual Studio 2008 and below and Clang on Windows. +// Use it for all MSVC-compatible compilers. +#if defined(_M_X64) && defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY) return XGETBV64(num); -// Visual Studio 2008 and below, 32-bit -#elif defined(CRYPTOPP_MSC_VERSION) && defined(_M_IX86) +// Required by Visual Studio 2008 and below and Clang on Windows. +// Use it for all MSVC-compatible compilers. +#elif defined(_M_IX86) && defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY) word32 a=0, d=0; __asm { @@ -449,20 +446,16 @@ word64 XGetBV(word32 num) // cpu.cpp (131): E2211 Inline assembly not allowed in inline and template functions bool CpuId(word32 func, word32 subfunc, word32 output[4]) { -// Visual Studio 2010 and above, 32 and 64-bit -#if defined(CRYPTOPP_MSC_VERSION) && ((CRYPTOPP_MSC_VERSION >= 1600)) - - __cpuidex((int *)output, func, subfunc); - return true; - -// Visual Studio 2008 and below, 64-bit -#elif defined(CRYPTOPP_MSC_VERSION) && defined(_M_X64) +// Required by Visual Studio 2008 and below and Clang on Windows. +// Use it for all MSVC-compatible compilers. +#if defined(_M_X64) && defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY) CPUID64(func, subfunc, output); return true; -// Visual Studio 2008 and below, 32-bit -#elif (defined(CRYPTOPP_MSC_VERSION) && defined(_M_IX86)) || defined(__BORLANDC__) +// Required by Visual Studio 2008 and below and Clang on Windows. +// Use it for all MSVC-compatible compilers. +#elif defined(_M_IX86) && defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY) __try {