Skip to content

Commit

Permalink
change cpu feature behavior for M1 (#5394)
Browse files Browse the repository at this point in the history
ref #5368
  • Loading branch information
SchrodingerZhu authored Jul 21, 2022
1 parent 7d14a1b commit c483f1b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
4 changes: 3 additions & 1 deletion contrib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ SET (BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "Disable google-benchmark testi
add_subdirectory(benchmark)

set (BUILD_TESTING OFF CACHE BOOL "Disable cpu-features testing" FORCE)
add_subdirectory(cpu_features)
if ((NOT APPLE) AND (NOT ARCH_AARCH64))
add_subdirectory(cpu_features)
endif()

if (ARCH_AARCH64 AND ARCH_LINUX)
add_subdirectory(arm-optimized-routines-cmake)
Expand Down
8 changes: 7 additions & 1 deletion libs/libcommon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ target_include_directories (common PUBLIC ${COMMON_INCLUDE_DIR})

target_include_directories (common BEFORE PUBLIC ${Boost_INCLUDE_DIRS})

if(APPLE AND ARCH_AARCH64)
set(CPU_FEATURES_LIBRARY)
else()
set(CPU_FEATURES_LIBRARY cpu_features)
endif()

# `libcommon` provides a bridge for many other libs.
# We expose the linkage to public because we want headers to be inherited.
target_link_libraries (
Expand All @@ -138,7 +144,7 @@ target_link_libraries (
${GLIBC_COMPATIBILITY_LIBRARIES}
${MEMCPY_LIBRARIES}
fmt
cpu_features
${CPU_FEATURES_LIBRARY}
)

if (RT_LIBRARY)
Expand Down
37 changes: 37 additions & 0 deletions libs/libcommon/include/common/detect_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#if defined(__aarch64__) || defined(__arm64__) || defined(__arm64) || defined(__ARM64) || defined(__AARCH64__)
#ifndef __APPLE__
#include <cpuinfo_aarch64.h>
namespace common
{
Expand All @@ -24,6 +25,42 @@ using CPUInfo = cpu_features::Aarch64Info;
extern const CPUInfo cpu_info;
static inline const CPUFeatures & cpu_feature_flags = cpu_info.features;
} // namespace common
#else
#include <sys/sysctl.h>
#include <sys/types.h>
namespace common
{
static inline int detect(const char * name)
{
int enabled;
size_t enabled_len = sizeof(enabled);
const int failure = ::sysctlbyname(name, &enabled, &enabled_len, NULL, 0);
return failure ? 0 : enabled;
}
static inline const struct CPUFeatures
{
bool asimd = detect("hw.optional.neon");
bool sve = false; // currently not supported
bool sve2 = false; // currently not supported

bool atomics = detect("hw.optional.armv8_1_atomics");
bool hpfp = detect("hw.optional.neon_hpfp");
bool fp16 = detect("hw.optional.neon_fp16");
bool fhm = detect("hw.optional.armv8_2_fhm");
bool crc32 = detect("hw.optional.armv8_crc32");
bool sha512 = detect("hw.optional.armv8_2_sha512");
bool sha3 = detect("hw.optional.armv8_2_sha3");

// the followings are assumed to be true.
// see https://github.com/golang/sys/pull/114/files
bool cpuid = true;
bool aes = true;
bool pmull = true;
bool sha1 = true;
bool sha2 = true;
} cpu_feature_flags;
} // namespace common
#endif
#endif

#if defined(__x86_64__) || defined(__x86_64) || defined(__amd64) || defined(__amd64__)
Expand Down

0 comments on commit c483f1b

Please sign in to comment.