diff options
Diffstat (limited to 'src/hwcaps.c')
-rw-r--r-- | src/hwcaps.c | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/src/hwcaps.c b/src/hwcaps.c index 815e5c1..dd17e3b 100644 --- a/src/hwcaps.c +++ b/src/hwcaps.c @@ -1,4 +1,4 @@ -// Copyright 2017 Google Inc. +// Copyright 2017 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,14 +12,33 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "internal/hwcaps.h" + #include <stdlib.h> #include <string.h> #include "cpu_features_macros.h" #include "internal/filesystem.h" -#include "internal/hwcaps.h" #include "internal/string_view.h" +static bool IsSet(const uint32_t mask, const uint32_t value) { + if (mask == 0) return false; + return (value & mask) == mask; +} + +bool CpuFeatures_IsHwCapsSet(const HardwareCapabilities hwcaps_mask, + const HardwareCapabilities hwcaps) { + return IsSet(hwcaps_mask.hwcaps, hwcaps.hwcaps) || + IsSet(hwcaps_mask.hwcaps2, hwcaps.hwcaps2); +} + +#ifdef CPU_FEATURES_TEST +// In test mode, hwcaps_for_testing will define the following functions. +HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void); +PlatformType CpuFeatures_GetPlatformType(void); +#else + +// Debug facilities #if defined(NDEBUG) #define D(...) #else @@ -35,9 +54,12 @@ // Implementation of GetElfHwcapFromGetauxval //////////////////////////////////////////////////////////////////////////////// -#if defined(CPU_FEATURES_MOCK_GET_ELF_HWCAP_FROM_GETAUXVAL) -// Implementation will be provided by test/hwcaps_for_testing.cc. -#elif defined(HAVE_STRONG_GETAUXVAL) +#define AT_HWCAP 16 +#define AT_HWCAP2 26 +#define AT_PLATFORM 15 +#define AT_BASE_PLATFORM 24 + +#if defined(HAVE_STRONG_GETAUXVAL) #include <sys/auxv.h> static unsigned long GetElfHwcapFromGetauxval(uint32_t hwcap_type) { return getauxval(hwcap_type); @@ -59,17 +81,13 @@ static unsigned long GetElfHwcapFromGetauxval(uint32_t hwcap_type) { // initialization layer. #include <dlfcn.h> -#define AT_HWCAP 16 -#define AT_HWCAP2 26 -#define AT_PLATFORM 15 -#define AT_BASE_PLATFORM 24 typedef unsigned long getauxval_func_t(unsigned long); static uint32_t GetElfHwcapFromGetauxval(uint32_t hwcap_type) { uint32_t ret = 0; - void* libc_handle = NULL; - getauxval_func_t* func = NULL; + void *libc_handle = NULL; + getauxval_func_t *func = NULL; dlerror(); // Cleaning error state before calling dlopen. libc_handle = dlopen("libc.so", RTLD_NOW); @@ -77,7 +95,7 @@ static uint32_t GetElfHwcapFromGetauxval(uint32_t hwcap_type) { D("Could not dlopen() C library: %s\n", dlerror()); return 0; } - func = (getauxval_func_t*)dlsym(libc_handle, "getauxval"); + func = (getauxval_func_t *)dlsym(libc_handle, "getauxval"); if (!func) { D("Could not find getauxval() in C library\n"); } else { @@ -109,7 +127,7 @@ static uint32_t GetElfHwcapFromProcSelfAuxv(uint32_t hwcap_type) { return 0; } for (;;) { - const int ret = CpuFeatures_ReadFile(fd, (char*)&entry, sizeof entry); + const int ret = CpuFeatures_ReadFile(fd, (char *)&entry, sizeof entry); if (ret < 0) { D("Error while reading %s\n", filepath); break; @@ -160,3 +178,5 @@ PlatformType CpuFeatures_GetPlatformType(void) { sizeof(type.base_platform)); return type; } + +#endif // CPU_FEATURES_TEST |