summaryrefslogtreecommitdiff
path: root/Source/Core/Common/XSaveWorkaround.cpp
diff options
context:
space:
mode:
authorShawn Hoffman <godisgovernment@gmail.com>2014-08-10 14:29:03 -0700
committerShawn Hoffman <godisgovernment@gmail.com>2014-08-10 14:50:29 -0700
commitd0c3e46c800ec30492127589e3a55ac912b28e90 (patch)
tree173d44fbc86b3e1900d99c2a1eeeb9a6450dec98 /Source/Core/Common/XSaveWorkaround.cpp
parenta1418192416e012e509c16bbb5c4380f0a30de0c (diff)
Windows: Improve XSaveWorkaround to behave correctly when XSAVE processor feature is enabled, but AVX support isn't available for whatever reason.
Diffstat (limited to 'Source/Core/Common/XSaveWorkaround.cpp')
-rw-r--r--Source/Core/Common/XSaveWorkaround.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/Source/Core/Common/XSaveWorkaround.cpp b/Source/Core/Common/XSaveWorkaround.cpp
index 78f92b9bcf..d73b4a6ee0 100644
--- a/Source/Core/Common/XSaveWorkaround.cpp
+++ b/Source/Core/Common/XSaveWorkaround.cpp
@@ -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,
@@ -19,6 +21,24 @@ int __cdecl EnableXSaveWorkaround()
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);
}
return 0;