Skip to content

Commit

Permalink
Use enum properly to directly access kConfig struct (google#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
gchatelet authored Jul 5, 2019
2 parents bdb36d9 + 22c05ed commit 24d7f78
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 252 deletions.
39 changes: 13 additions & 26 deletions src/cpuinfo_aarch64.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "internal/string_view.h"
#include "internal/unix_features_aggregator.h"

#include <assert.h>
#include <ctype.h>

DECLARE_SETTER(Aarch64Features, fp)
Expand All @@ -31,13 +32,13 @@ DECLARE_SETTER(Aarch64Features, sha2)
DECLARE_SETTER(Aarch64Features, crc32)

static const CapabilityConfig kConfigs[] = {
{{AARCH64_HWCAP_FP, 0}, "fp", &set_fp}, //
{{AARCH64_HWCAP_ASIMD, 0}, "asimd", &set_asimd}, //
{{AARCH64_HWCAP_AES, 0}, "aes", &set_aes}, //
{{AARCH64_HWCAP_PMULL, 0}, "pmull", &set_pmull}, //
{{AARCH64_HWCAP_SHA1, 0}, "sha1", &set_sha1}, //
{{AARCH64_HWCAP_SHA2, 0}, "sha2", &set_sha2}, //
{{AARCH64_HWCAP_CRC32, 0}, "crc32", &set_crc32}, //
[AARCH64_FP] = {{AARCH64_HWCAP_FP, 0}, "fp", &set_fp}, //
[AARCH64_ASIMD] = {{AARCH64_HWCAP_ASIMD, 0}, "asimd", &set_asimd}, //
[AARCH64_AES] = {{AARCH64_HWCAP_AES, 0}, "aes", &set_aes}, //
[AARCH64_PMULL] = {{AARCH64_HWCAP_PMULL, 0}, "pmull", &set_pmull}, //
[AARCH64_SHA1] = {{AARCH64_HWCAP_SHA1, 0}, "sha1", &set_sha1}, //
[AARCH64_SHA2] = {{AARCH64_HWCAP_SHA2, 0}, "sha2", &set_sha2}, //
[AARCH64_CRC32] {{AARCH64_HWCAP_CRC32, 0}, "crc32", &set_crc32}, //
};

static const size_t kConfigsSize = sizeof(kConfigs) / sizeof(CapabilityConfig);
Expand Down Expand Up @@ -79,6 +80,8 @@ static void FillProcCpuInfoData(Aarch64Info* const info) {
static const Aarch64Info kEmptyAarch64Info;

Aarch64Info GetAarch64Info(void) {
assert(kConfigsSize == AARCH64_LAST_);

// capabilities are fetched from both getauxval and /proc/cpuinfo so we can
// have some information if the executable is sandboxed (aka no access to
// /proc/cpuinfo).
Expand Down Expand Up @@ -119,23 +122,7 @@ int GetAarch64FeaturesEnumValue(const Aarch64Features* features,
}

const char* GetAarch64FeaturesEnumName(Aarch64FeaturesEnum value) {
switch (value) {
case AARCH64_FP:
return "fp";
case AARCH64_ASIMD:
return "asimd";
case AARCH64_AES:
return "aes";
case AARCH64_PMULL:
return "pmull";
case AARCH64_SHA1:
return "sha1";
case AARCH64_SHA2:
return "sha2";
case AARCH64_CRC32:
return "crc32";
case AARCH64_LAST_:
break;
}
return "unknown feature";
if(value >= kConfigsSize)
return "unknown feature";
return kConfigs[value].proc_cpuinfo_flag;
}
115 changes: 31 additions & 84 deletions src/cpuinfo_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "internal/string_view.h"
#include "internal/unix_features_aggregator.h"

#include <assert.h>
#include <ctype.h>

DECLARE_SETTER(ArmFeatures, swp)
Expand Down Expand Up @@ -52,33 +53,33 @@ DECLARE_SETTER(ArmFeatures, sha2)
DECLARE_SETTER(ArmFeatures, crc32)

static const CapabilityConfig kConfigs[] = {
{{ARM_HWCAP_SWP, 0}, "swp", &set_swp}, //
{{ARM_HWCAP_HALF, 0}, "half", &set_half}, //
{{ARM_HWCAP_THUMB, 0}, "thumb", &set_thumb}, //
{{ARM_HWCAP_26BIT, 0}, "26bit", &set__26bit}, //
{{ARM_HWCAP_FAST_MULT, 0}, "fastmult", &set_fastmult}, //
{{ARM_HWCAP_FPA, 0}, "fpa", &set_fpa}, //
{{ARM_HWCAP_VFP, 0}, "vfp", &set_vfp}, //
{{ARM_HWCAP_EDSP, 0}, "edsp", &set_edsp}, //
{{ARM_HWCAP_JAVA, 0}, "java", &set_java}, //
{{ARM_HWCAP_IWMMXT, 0}, "iwmmxt", &set_iwmmxt}, //
{{ARM_HWCAP_CRUNCH, 0}, "crunch", &set_crunch}, //
{{ARM_HWCAP_THUMBEE, 0}, "thumbee", &set_thumbee}, //
{{ARM_HWCAP_NEON, 0}, "neon", &set_neon}, //
{{ARM_HWCAP_VFPV3, 0}, "vfpv3", &set_vfpv3}, //
{{ARM_HWCAP_VFPV3D16, 0}, "vfpv3d16", &set_vfpv3d16}, //
{{ARM_HWCAP_TLS, 0}, "tls", &set_tls}, //
{{ARM_HWCAP_VFPV4, 0}, "vfpv4", &set_vfpv4}, //
{{ARM_HWCAP_IDIVA, 0}, "idiva", &set_idiva}, //
{{ARM_HWCAP_IDIVT, 0}, "idivt", &set_idivt}, //
{{ARM_HWCAP_VFPD32, 0}, "vfpd32", &set_vfpd32}, //
{{ARM_HWCAP_LPAE, 0}, "lpae", &set_lpae}, //
{{ARM_HWCAP_EVTSTRM, 0}, "evtstrm", &set_evtstrm}, //
{{0, ARM_HWCAP2_AES}, "aes", &set_aes}, //
{{0, ARM_HWCAP2_PMULL}, "pmull", &set_pmull}, //
{{0, ARM_HWCAP2_SHA1}, "sha1", &set_sha1}, //
{{0, ARM_HWCAP2_SHA2}, "sha2", &set_sha2}, //
{{0, ARM_HWCAP2_CRC32}, "crc32", &set_crc32}, //
[ARM_SWP] = {{ARM_HWCAP_SWP, 0}, "swp", &set_swp}, //
[ARM_HALF] = {{ARM_HWCAP_HALF, 0}, "half", &set_half}, //
[ARM_THUMB] = {{ARM_HWCAP_THUMB, 0}, "thumb", &set_thumb}, //
[ARM_26BIT] = {{ARM_HWCAP_26BIT, 0}, "26bit", &set__26bit}, //
[ARM_FASTMULT] = {{ARM_HWCAP_FAST_MULT, 0}, "fastmult", &set_fastmult}, //
[ARM_FPA] = {{ARM_HWCAP_FPA, 0}, "fpa", &set_fpa}, //
[ARM_VFP] = {{ARM_HWCAP_VFP, 0}, "vfp", &set_vfp}, //
[ARM_EDSP] = {{ARM_HWCAP_EDSP, 0}, "edsp", &set_edsp}, //
[ARM_JAVA] = {{ARM_HWCAP_JAVA, 0}, "java", &set_java}, //
[ARM_IWMMXT] = {{ARM_HWCAP_IWMMXT, 0}, "iwmmxt", &set_iwmmxt}, //
[ARM_CRUNCH] = {{ARM_HWCAP_CRUNCH, 0}, "crunch", &set_crunch}, //
[ARM_THUMBEE] = {{ARM_HWCAP_THUMBEE, 0}, "thumbee", &set_thumbee}, //
[ARM_NEON] = {{ARM_HWCAP_NEON, 0}, "neon", &set_neon}, //
[ARM_VFPV3] = {{ARM_HWCAP_VFPV3, 0}, "vfpv3", &set_vfpv3}, //
[ARM_VFPV3D16] = {{ARM_HWCAP_VFPV3D16, 0}, "vfpv3d16", &set_vfpv3d16}, //
[ARM_TLS] = {{ARM_HWCAP_TLS, 0}, "tls", &set_tls}, //
[ARM_VFPV4] = {{ARM_HWCAP_VFPV4, 0}, "vfpv4", &set_vfpv4}, //
[ARM_IDIVA] = {{ARM_HWCAP_IDIVA, 0}, "idiva", &set_idiva}, //
[ARM_IDIVT] = {{ARM_HWCAP_IDIVT, 0}, "idivt", &set_idivt}, //
[ARM_VFPD32] = {{ARM_HWCAP_VFPD32, 0}, "vfpd32", &set_vfpd32}, //
[ARM_LPAE] = {{ARM_HWCAP_LPAE, 0}, "lpae", &set_lpae}, //
[ARM_EVTSTRM] = {{ARM_HWCAP_EVTSTRM, 0}, "evtstrm", &set_evtstrm}, //
[ARM_AES] = {{0, ARM_HWCAP2_AES}, "aes", &set_aes}, //
[ARM_PMULL] = {{0, ARM_HWCAP2_PMULL}, "pmull", &set_pmull}, //
[ARM_SHA1] = {{0, ARM_HWCAP2_SHA1}, "sha1", &set_sha1}, //
[ARM_SHA2] = {{0, ARM_HWCAP2_SHA2}, "sha2", &set_sha2}, //
[ARM_CRC32] = {{0, ARM_HWCAP2_CRC32}, "crc32", &set_crc32}, //
};

static const size_t kConfigsSize = sizeof(kConfigs) / sizeof(CapabilityConfig);
Expand Down Expand Up @@ -285,61 +286,7 @@ int GetArmFeaturesEnumValue(const ArmFeatures* features,
}

const char* GetArmFeaturesEnumName(ArmFeaturesEnum value) {
switch (value) {
case ARM_SWP:
return "swp";
case ARM_HALF:
return "half";
case ARM_THUMB:
return "thumb";
case ARM_FASTMULT:
return "fastmult";
case ARM_FPA:
return "fpa";
case ARM_VFP:
return "vfp";
case ARM_EDSP:
return "edsp";
case ARM_JAVA:
return "java";
case ARM_IWMMXT:
return "iwmmxt";
case ARM_CRUNCH:
return "crunch";
case ARM_THUMBEE:
return "thumbee";
case ARM_NEON:
return "neon";
case ARM_VFPV3:
return "vfpv3";
case ARM_VFPV3D16:
return "vfpv3d16";
case ARM_TLS:
return "tls";
case ARM_VFPV4:
return "vfpv4";
case ARM_IDIVA:
return "idiva";
case ARM_IDIVT:
return "idivt";
case ARM_VFPD32:
return "vfpd32";
case ARM_LPAE:
return "lpae";
case ARM_EVTSTRM:
return "evtstrm";
case ARM_AES:
return "aes";
case ARM_PMULL:
return "pmull";
case ARM_SHA1:
return "sha1";
case ARM_SHA2:
return "sha2";
case ARM_CRC32:
return "crc32";
case ARM_LAST_:
break;
}
return "unknown feature";
if(value >= kConfigsSize)
return "unknown feature";
return kConfigs[value].proc_cpuinfo_flag;
}
24 changes: 10 additions & 14 deletions src/cpuinfo_mips.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
#include "internal/string_view.h"
#include "internal/unix_features_aggregator.h"

#include <assert.h>

DECLARE_SETTER(MipsFeatures, msa)
DECLARE_SETTER(MipsFeatures, eva)
DECLARE_SETTER(MipsFeatures, r6)

static const CapabilityConfig kConfigs[] = {
{{MIPS_HWCAP_MSA, 0}, "msa", &set_msa}, //
{{0, 0}, "eva", &set_eva}, //
{{MIPS_HWCAP_R6, 0}, "r6", &set_r6}, //
[MIPS_MSA] = {{MIPS_HWCAP_MSA, 0}, "msa", &set_msa}, //
[MIPS_EVA] = {{0, 0}, "eva", &set_eva}, //
[MIPS_R6] = {{MIPS_HWCAP_R6, 0}, "r6", &set_r6}, //
};
static const size_t kConfigsSize = sizeof(kConfigs) / sizeof(CapabilityConfig);

Expand Down Expand Up @@ -59,6 +61,8 @@ static void FillProcCpuInfoData(MipsFeatures* const features) {
static const MipsInfo kEmptyMipsInfo;

MipsInfo GetMipsInfo(void) {
assert(kConfigsSize == MIPS_LAST_);

// capabilities are fetched from both getauxval and /proc/cpuinfo so we can
// have some information if the executable is sandboxed (aka no access to
// /proc/cpuinfo).
Expand Down Expand Up @@ -90,15 +94,7 @@ int GetMipsFeaturesEnumValue(const MipsFeatures* features,
}

const char* GetMipsFeaturesEnumName(MipsFeaturesEnum value) {
switch (value) {
case MIPS_MSA:
return "msa";
case MIPS_EVA:
return "eva";
case MIPS_R6:
return "r6";
case MIPS_LAST_:
break;
}
return "unknown feature";
if(value >= kConfigsSize)
return "unknown feature";
return kConfigs[value].proc_cpuinfo_flag;
}
Loading

0 comments on commit 24d7f78

Please sign in to comment.