Skip to content

Commit

Permalink
Fix list_cpu_features.exe does not detect SSE42 on Xeon X5650 (Window…
Browse files Browse the repository at this point in the history
…s) (google#220)
  • Loading branch information
Mykola Hohsadze authored Jan 31, 2022
1 parent 2f5a7bf commit f1801f0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/impl_x86__base_implementation.inl
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ typedef struct {
// These two functions have to be implemented by the OS, that is the file
// including this file.
static void OverrideOsPreserves(OsPreserves* os_preserves);
static void DetectFeaturesFromOs(X86Features* features);
static void DetectFeaturesFromOs(X86Info* info, X86Features* features);

// Reference https://en.wikipedia.org/wiki/CPUID.
static void ParseCpuId(const Leaves* leaves, X86Info* info,
Expand Down Expand Up @@ -371,7 +371,7 @@ static void ParseCpuId(const Leaves* leaves, X86Info* info,
} else {
// When XCR0 is not available (Atom based or older cpus) we need to defer to
// the OS via custom code.
DetectFeaturesFromOs(features);
DetectFeaturesFromOs(info, features);
// Now that we have queried the OS for SSE support, we report this back to
// os_preserves. This is needed in case of AMD CPU's to enable testing of
// sse4a (See ParseExtraAMDCpuId below).
Expand Down
3 changes: 2 additions & 1 deletion src/impl_x86_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ static void OverrideOsPreserves(OsPreserves* os_preserves) {
#include "internal/stack_line_reader.h"
#include "internal/string_view.h"

static void DetectFeaturesFromOs(X86Features* features) {
static void DetectFeaturesFromOs(X86Info* info, X86Features* features) {
(void)info;
// Handling FreeBSD platform through parsing /var/run/dmesg.boot.
const int fd = CpuFeatures_OpenFile("/var/run/dmesg.boot");
if (fd >= 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/impl_x86_linux_or_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ static void OverrideOsPreserves(OsPreserves* os_preserves) {
#include "internal/filesystem.h"
#include "internal/stack_line_reader.h"
#include "internal/string_view.h"
static void DetectFeaturesFromOs(X86Features* features) {
static void DetectFeaturesFromOs(X86Info* info, X86Features* features) {
(void)info;
// Handling Linux platform through /proc/cpuinfo.
const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
if (fd >= 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/impl_x86_macos.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ static void OverrideOsPreserves(OsPreserves* os_preserves) {
os_preserves->avx512_registers = GetDarwinSysCtlByName("hw.optional.avx512f");
}

static void DetectFeaturesFromOs(X86Features* features) {
static void DetectFeaturesFromOs(X86Info* info, X86Features* features) {
(void)info;
// Handling Darwin platform through sysctlbyname.
features->sse = GetDarwinSysCtlByName("hw.optional.sse");
features->sse2 = GetDarwinSysCtlByName("hw.optional.sse2");
Expand Down
11 changes: 10 additions & 1 deletion src/impl_x86_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static bool GetWindowsIsProcessorFeaturePresent(DWORD ProcessorFeature) {
}
#endif

static void DetectFeaturesFromOs(X86Features* features) {
static void DetectFeaturesFromOs(X86Info* info, X86Features* features) {
// Handling Windows platform through IsProcessorFeaturePresent.
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-isprocessorfeaturepresent
features->sse =
Expand All @@ -43,6 +43,15 @@ static void DetectFeaturesFromOs(X86Features* features) {
GetWindowsIsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE);
features->sse3 =
GetWindowsIsProcessorFeaturePresent(PF_SSE3_INSTRUCTIONS_AVAILABLE);

// https://github.com/google/cpu_features/issues/200
#if (_WIN32_WINNT >= 0x0601) // Win7+
if (GetX86Microarchitecture(info) == INTEL_WSM) {
features->ssse3 = true;
features->sse4_1 = true;
features->sse4_2 = true;
}
#endif
}

#endif // CPU_FEATURES_OS_WINDOWS
Expand Down
26 changes: 26 additions & 0 deletions test/cpuinfo_x86_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,32 @@ TEST_F(CpuidX86Test, INTEL_KNIGHTS_LANDING) {
X86Microarchitecture::INTEL_KNIGHTS_L);
}

// https://github.com/google/cpu_features/issues/200
// http://users.atw.hu/instlatx64/GenuineIntel/GenuineIntel00206F2_Eagleton_CPUID.txt
#if defined(CPU_FEATURES_OS_WINDOWS)
TEST_F(CpuidX86Test, WIN_INTEL_WESTMERE_EX) {
cpu().SetLeaves({
{{0x00000000, 0}, Leaf{0x0000000B, 0x756E6547, 0x6C65746E, 0x49656E69}},
{{0x00000001, 0}, Leaf{0x000206F2, 0x00400800, 0x02BEE3FF, 0xBFEBFBFF}},
});
const auto info = GetX86Info();

EXPECT_EQ(info.family, 0x06);
EXPECT_EQ(info.model, 0x2F);
EXPECT_EQ(GetX86Microarchitecture(&info), X86Microarchitecture::INTEL_WSM);

#if (_WIN32_WINNT < 0x0601) // before Win7
EXPECT_FALSE(info.features.ssse3);
EXPECT_FALSE(info.features.sse4_1);
EXPECT_FALSE(info.features.sse4_2);
#else
EXPECT_TRUE(info.features.ssse3);
EXPECT_TRUE(info.features.sse4_1);
EXPECT_TRUE(info.features.sse4_2);
#endif
}
#endif // CPU_FEATURES_OS_WINDOWS

// TODO(user): test what happens when xsave/osxsave are not present.
// TODO(user): test what happens when xmm/ymm/zmm os support are not
// present.
Expand Down

0 comments on commit f1801f0

Please sign in to comment.