Skip to content

SSE detection fix on non-AVX CPUs #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ macro(add_cpu_features_headers_and_sources HDRS_LIST_NAME SRCS_LIST_NAME)
endif()
endmacro()

if(UNIX AND PROCESSOR_IS_X86)
include(CheckCSourceCompiles)
check_c_source_compiles("
#include <stdlib.h>
int main(void)
{
__builtin_cpu_init();
}" HAVE_CPU_INIT)
if(NOT HAVE_CPU_INIT)
check_include_file(sys/utsname.h HAVE_UTSNAME_H)
endif()
endif()

#
# library : utils
#
Expand Down Expand Up @@ -148,6 +161,18 @@ set_property(TARGET cpu_features PROPERTY POSITION_INDEPENDENT_CODE ${BUILD_PIC}
target_include_directories(cpu_features
PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cpu_features>
)
if(PROCESSOR_IS_X86)
if(HAVE_CPU_INIT)
target_compile_definitions(cpu_features PRIVATE HAVE_CPU_INIT)
else()
if(HAVE_UTSNAME_H)
target_compile_definitions(cpu_features PRIVATE HAVE_UTSNAME_H)
endif()
if(APPLE)
target_compile_definitions(cpu_features PRIVATE HAVE_SYSCTLBYNAME)
endif()
endif()
endif()
add_library(CpuFeature::cpu_features ALIAS cpu_features)

#
Expand Down
72 changes: 69 additions & 3 deletions src/cpuinfo_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,24 @@ typedef struct {
bool have_avx512;
} OsSupport;

#if defined(CPU_FEATURES_COMPILER_CLANG) || defined(CPU_FEATURES_COMPILER_GCC)
#if defined(HAVE_CPU_INIT)
#include <stdlib.h>
#else
#if defined(HAVE_UTSNAME_H)
#include "internal/filesystem.h"
#include "internal/stack_line_reader.h"
#include "internal/string_view.h"
#include <sys/utsname.h>
#if defined(HAVE_SYSCTLBYNAME)
#include <sys/sysctl.h>
#endif
#endif
#endif
#elif defined(CPU_FEATURES_COMPILER_MSC)
#include <windows.h>
#endif

// Reference https://en.wikipedia.org/wiki/CPUID.
static void ParseCpuId(const uint32_t max_cpuid_leaf, X86Info* info, OsSupport* os_support) {
const Leaf leaf_1 = SafeCpuId(max_cpuid_leaf, 1);
Expand All @@ -1055,9 +1073,57 @@ static void ParseCpuId(const uint32_t max_cpuid_leaf, X86Info* info, OsSupport*
const bool have_xsave = IsBitSet(leaf_1.ecx, 26);
const bool have_osxsave = IsBitSet(leaf_1.ecx, 27);
const uint32_t xcr0_eax = (have_xsave && have_osxsave) ? GetXCR0Eax() : 0;
os_support->have_sse = HasXmmOsXSave(xcr0_eax);
os_support->have_avx = HasYmmOsXSave(xcr0_eax);
os_support->have_avx512 = HasZmmOsXSave(xcr0_eax);
if (xcr0_eax) {
os_support->have_sse = HasXmmOsXSave(xcr0_eax);
os_support->have_avx = HasYmmOsXSave(xcr0_eax);
os_support->have_avx512 = HasZmmOsXSave(xcr0_eax);
} else {
#if defined(CPU_FEATURES_COMPILER_CLANG) || defined(CPU_FEATURES_COMPILER_GCC)
#if defined(HAVE_CPU_INIT)
__builtin_cpu_init();
os_support->have_sse = __builtin_cpu_supports("sse");
#else
#if defined(HAVE_UTSNAME_H)
struct utsname buf;
uname(&buf);
if (strncmp(buf.sysname, "Darwin", sizeof(buf.sysname)) == 0) {
#if defined(HAVE_SYSCTLBYNAME)
int enabled, result;
size_t len = sizeof(enabled);
result = sysctlbyname("hw.optional.sse", &enabled, &len, NULL, 0);
if (result == 0) {
os_support->have_sse = enabled;
}
#endif
} else if (strncmp(buf.sysname, "Linux", sizeof(buf.sysname)) == 0) {
const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
if (fd >= 0) {
StackLineReader reader;
StackLineReader_Initialize(&reader, fd);
for (;;) {
const LineResult result = StackLineReader_NextLine(&reader);
StringView line = result.line;
StringView key, value;
if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
if (CpuFeatures_StringView_IsEquals(key, str("flags"))) {
os_support->have_sse = CpuFeatures_StringView_HasWord(value, "sse");
break;
}
}
if (result.eof) {
break;
}
}
CpuFeatures_CloseFile(fd);
}
}
#endif // defined(HAVE_UTSNAME_H)
#endif // defined(CPU_FEATURES_COMPILER_CLANG) || defined(CPU_FEATURES_COMPILER_GCC)
#elif defined(CPU_FEATURES_COMPILER_MSC)
// see: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-isprocessorfeaturepresent
os_support->have_sse = IsProcessorFeaturePresent(PF_XMMI_INSTRUCTIONS_AVAILABLE);
#endif
}

const uint32_t family = ExtractBitRange(leaf_1.eax, 11, 8);
const uint32_t extended_family = ExtractBitRange(leaf_1.eax, 27, 20);
Expand Down