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.
This commit is contained in:
Jeffrey Walton 2023-06-25 22:10:22 -04:00
parent a89a27b72c
commit e65fa006be
No known key found for this signature in database
GPG Key ID: B36AB348921B1838

39
cpu.cpp
View File

@ -78,9 +78,10 @@ unsigned long int getauxval(unsigned long int) { return 0; }
# include <setjmp.h>
#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
{