Skip to content

Commit

Permalink
Use _xgetbv intrinsic on Windows for AVX2 detection.
Browse files Browse the repository at this point in the history
The intrinsic is supported since VS2010 SP1, which is below our minimum
supported MSVC version. The intrinsic is the proper way to detect
support for AVX register state by the OS instead of the undocumented
GetEnabledExtendedFeatures WinAPI function.
  • Loading branch information
Lastique committed Apr 13, 2024
1 parent 2d932dd commit 38bf38e
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include <boost/cstdint.hpp>
#include <boost/log/utility/manipulators/dump.hpp>
#if defined(_MSC_VER) && (defined(BOOST_LOG_USE_SSSE3) || defined(BOOST_LOG_USE_AVX2))
#include <boost/winapi/dll.hpp>
#include <intrin.h> // __cpuid
#include <immintrin.h> // _xgetbv
#endif
#include <boost/log/detail/header.hpp>

Expand Down Expand Up @@ -180,19 +180,8 @@ struct function_pointer_initializer
: "c" (0)
);
mmstate = (eax & 6u) == 6u;
#elif defined(BOOST_WINDOWS)
// MSVC does not have an intrinsic for xgetbv, we have to query OS
boost::winapi::HMODULE_ hKernel32 = boost::winapi::GetModuleHandleW(L"kernel32.dll");
if (hKernel32)
{
typedef uint64_t (BOOST_WINAPI_WINAPI_CC* get_enabled_extended_features_t)(uint64_t);
get_enabled_extended_features_t get_enabled_extended_features = (get_enabled_extended_features_t)boost::winapi::get_proc_address(hKernel32, "GetEnabledExtendedFeatures");
if (get_enabled_extended_features)
{
// XSTATE_MASK_LEGACY_SSE | XSTATE_MASK_GSSE == 6
mmstate = get_enabled_extended_features(6u) == 6u;
}
}
#elif defined(_MSC_VER)
mmstate = (_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 6u) == 6u;
#endif

if (mmstate)
Expand Down

0 comments on commit 38bf38e

Please sign in to comment.