1
0
mirror of https://github.com/google/cpu_features.git synced 2025-04-27 23:22:31 +02:00

Converts list_cpu_features to C

This commit is contained in:
Guillaume Chatelet 2018-02-09 10:08:23 +01:00
parent 11e3e20496
commit 4189efff92
3 changed files with 109 additions and 113 deletions

View File

@ -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

108
src/list_cpu_features.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -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 <stdio.h>
#include <algorithm>
#include <string>
#include <vector>
#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 <typename HasFeatureFun, typename FeatureNameFun, typename FeatureType,
typename EnumType>
std::string GetFlags(const HasFeatureFun HasFeature,
const FeatureNameFun FeatureName,
const FeatureType* features, const EnumType last) {
std::vector<std::string> flags;
for (int i = 0; i < last; ++i) {
const EnumType enum_value = static_cast<EnumType>(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;
}