From 4189efff9209b99d80074e473ba3596c7f03c46c Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Fri, 9 Feb 2018 10:08:23 +0100 Subject: [PATCH] Converts list_cpu_features to C --- CMakeLists.txt | 3 +- src/list_cpu_features.c | 108 +++++++++++++++++++++++++++++++++++++ src/list_cpu_features.cc | 111 --------------------------------------- 3 files changed, 109 insertions(+), 113 deletions(-) create mode 100644 src/list_cpu_features.c delete mode 100644 src/list_cpu_features.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c03b7b..bb9d538 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,9 +53,8 @@ target_link_libraries(cpu_features PUBLIC ${CMAKE_DL_LIBS}) # program : list_cpu_features # -add_executable(list_cpu_features src/list_cpu_features.cc) +add_executable(list_cpu_features src/list_cpu_features.c) target_link_libraries(list_cpu_features PRIVATE cpu_features) -target_compile_features(list_cpu_features PRIVATE cxx_range_for) # # tests diff --git a/src/list_cpu_features.c b/src/list_cpu_features.c new file mode 100644 index 0000000..7b3dd2f --- /dev/null +++ b/src/list_cpu_features.c @@ -0,0 +1,108 @@ +// Copyright 2017 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include "cpu_features_macros.h" +#include "cpuinfo_aarch64.h" +#include "cpuinfo_arm.h" +#include "cpuinfo_mips.h" +#include "cpuinfo_x86.h" + +// Prints a named numeric value in both decimal and hexadecimal. +void PrintN(const char* field, int value) { + printf("%-15s : %3d (0x%02X)\n", field, value, value); +} + +// Prints a named string. +void PrintS(const char* field, const char* value) { + printf("%-15s : %s\n", field, value); +} + +static int cmp(const void* p1, const void* p2) { + return strcmp(*(const char* const*)p1, *(const char* const*)p2); +} + +#define DEFINE_PRINT_FLAGS(HasFeature, FeatureName, FeatureType, LastEnum) \ + void PrintFlags(const FeatureType* features) { \ + size_t i; \ + const char* ptrs[LastEnum] = {}; \ + size_t count = 0; \ + for (i = 0; i < LastEnum; ++i) { \ + if (HasFeature(features, i)) { \ + ptrs[count] = FeatureName(i); \ + ++count; \ + } \ + } \ + qsort(ptrs, count, sizeof(char*), cmp); \ + printf("%-15s : ", "flags"); \ + for (i = 0; i < count; ++i) { \ + if (i > 0) printf(", "); \ + printf("%s", ptrs[i]); \ + } \ + printf("\n"); \ + } + +#if defined(CPU_FEATURES_ARCH_X86) +DEFINE_PRINT_FLAGS(GetX86FeaturesEnumValue, GetX86FeaturesEnumName, X86Features, + X86_LAST_) +#elif defined(CPU_FEATURES_ARCH_ARM) +DEFINE_PRINT_FLAGS(GetArmFeaturesEnumValue, GetArmFeaturesEnumName, ArmFeatures, + ARM_LAST_) +#elif defined(CPU_FEATURES_ARCH_AARCH64) +DEFINE_PRINT_FLAGS(GetAarch64FeaturesEnumValue, GetAarch64FeaturesEnumName, + Aarch64Features, AARCH64_LAST_) +#elif defined(CPU_FEATURES_ARCH_MIPS) +DEFINE_PRINT_FLAGS(GetMipsFeaturesEnumValue, GetMipsFeaturesEnumName, + MipsFeatures, MIPS_LAST_) +#endif + +int main(int argc, char** argv) { +#if defined(CPU_FEATURES_ARCH_X86) + char brand_string[49]; + const X86Info info = GetX86Info(); + FillX86BrandString(brand_string); + PrintS("arch", "x86"); + PrintS("brand", brand_string); + PrintN("family", info.family); + PrintN("model", info.model); + PrintN("stepping", info.stepping); + PrintS("uarch", GetX86MicroarchitectureName(GetX86Microarchitecture(&info))); + PrintFlags(&info.features); +#elif defined(CPU_FEATURES_ARCH_ARM) + const ArmInfo info = GetArmInfo(); + PrintS("arch", "ARM"); + PrintN("implementer", info.implementer); + PrintN("architecture", info.architecture); + PrintN("variant", info.variant); + PrintN("part", info.part); + PrintN("revision", info.revision); + PrintFlags(&info.features); +#elif defined(CPU_FEATURES_ARCH_AARCH64) + const Aarch64Info info = GetAarch64Info(); + PrintS("arch", "aarch64"); + PrintN("implementer", info.implementer); + PrintN("variant", info.variant); + PrintN("part", info.part); + PrintN("revision", info.revision); + PrintFlags(&info.features); +#elif defined(CPU_FEATURES_ARCH_MIPS) + const MipsInfo info = GetMipsInfo(); + PrintS("arch", "mips"); + PrintFlags(&info.features); +#endif + return 0; +} diff --git a/src/list_cpu_features.cc b/src/list_cpu_features.cc deleted file mode 100644 index 64f3da3..0000000 --- a/src/list_cpu_features.cc +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright 2017 Google Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include - -#include -#include -#include - -#include "cpu_features_macros.h" -#include "cpuinfo_aarch64.h" -#include "cpuinfo_arm.h" -#include "cpuinfo_mips.h" -#include "cpuinfo_x86.h" - -namespace cpu_features { - -// Prints a named numeric value in both decimal and hexadecimal. -void PrintN(const char* field, int value) { - printf("%-15s : %3d (0x%02X)\n", field, value, value); -} - -// Prints a named string. -void PrintS(const char* field, const char* value) { - printf("%-15s : %s\n", field, value); -} - -template -std::string GetFlags(const HasFeatureFun HasFeature, - const FeatureNameFun FeatureName, - const FeatureType* features, const EnumType last) { - std::vector flags; - for (int i = 0; i < last; ++i) { - const EnumType enum_value = static_cast(i); - if (HasFeature(features, enum_value)) { - flags.push_back(FeatureName(enum_value)); - } - } - std::sort(flags.begin(), flags.end()); - std::string buffer; - for (const auto& flag : flags) { - if (!buffer.empty()) buffer += ' '; - buffer += flag; - } - return buffer; -} - -void Main() { -#if defined(CPU_FEATURES_ARCH_X86) - char brand_string[49]; - const X86Info info = GetX86Info(); - const auto flags = GetFlags(&GetX86FeaturesEnumValue, &GetX86FeaturesEnumName, - &info.features, X86FeaturesEnum::X86_LAST_); - FillX86BrandString(brand_string); - PrintS("arch", "x86"); - PrintS("brand", brand_string); - PrintN("family", info.family); - PrintN("model", info.model); - PrintN("stepping", info.stepping); - PrintS("uarch", GetX86MicroarchitectureName(GetX86Microarchitecture(&info))); - PrintS("flags", flags.c_str()); -#elif defined(CPU_FEATURES_ARCH_ARM) - const ArmInfo info = GetArmInfo(); - const auto flags = GetFlags(&GetArmFeaturesEnumValue, &GetArmFeaturesEnumName, - &info.features, ArmFeaturesEnum::ARM_LAST_); - PrintS("arch", "ARM"); - PrintN("implementer", info.implementer); - PrintN("architecture", info.architecture); - PrintN("variant", info.variant); - PrintN("part", info.part); - PrintN("revision", info.revision); - PrintS("flags", flags.c_str()); -#elif defined(CPU_FEATURES_ARCH_AARCH64) - const Aarch64Info info = GetAarch64Info(); - const auto flags = - GetFlags(&GetAarch64FeaturesEnumValue, &GetAarch64FeaturesEnumName, - &info.features, Aarch64FeaturesEnum::AARCH64_LAST_); - PrintS("arch", "aarch64"); - PrintN("implementer", info.implementer); - PrintN("variant", info.variant); - PrintN("part", info.part); - PrintN("revision", info.revision); - PrintS("flags", flags.c_str()); -#elif defined(CPU_FEATURES_ARCH_MIPS) - const MipsInfo info = GetMipsInfo(); - const auto flags = - GetFlags(&GetMipsFeaturesEnumValue, &GetMipsFeaturesEnumName, - &info.features, MipsFeaturesEnum::MIPS_LAST_); - PrintS("arch", "mips"); - PrintS("flags", flags.c_str()); -#endif -} - -} // namespace cpu_features - -int main(int argc, char** argv) { - cpu_features::Main(); - return 0; -}