Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shuffle2 committed Aug 11, 2014
2 parents 5928eac + d0c3e46 commit edf660f
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Source/Core/Common/XSaveWorkaround.cpp
Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down

0 comments on commit edf660f

Please sign in to comment.