Merge pull request #776 from shuffle2/improve-xsaveworkaround

Windows: Improve XSaveWorkaround to behave correctly when XSAVE processor feature is enabled, but AVX support isn't available for whatever reason.
This commit is contained in:
shuffle2 2014-08-11 12:47:39 -07:00
commit edf660f21f

View File

@ -7,6 +7,8 @@
#include <math.h>
#include <Windows.h>
typedef decltype(&GetEnabledXStateFeatures) GetEnabledXStateFeatures_t;
int __cdecl EnableXSaveWorkaround()
{
// Some Windows environments may have hardware support for AVX/FMA,
@ -17,6 +19,24 @@ int __cdecl EnableXSaveWorkaround()
// The API name is somewhat misleading - we're testing for OS support
// here.
if (!IsProcessorFeaturePresent(PF_XSAVE_ENABLED))
{
_set_FMA3_enable(0);
return 0;
}
// Even if XSAVE feature is enabled, we have to see if
// GetEnabledXStateFeatures function is present, and see what it says about
// AVX state.
auto kernel32Handle = GetModuleHandle(TEXT("kernel32.dll"));
if (kernel32Handle == nullptr)
{
std::abort();
}
auto pGetEnabledXStateFeatures = (GetEnabledXStateFeatures_t)GetProcAddress(
kernel32Handle, "GetEnabledXStateFeatures");
if (pGetEnabledXStateFeatures == nullptr ||
(pGetEnabledXStateFeatures() & XSTATE_MASK_AVX) == 0)
{
_set_FMA3_enable(0);
}