diff options
author | Jack Lloyd <[email protected]> | 2017-09-16 12:17:54 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-09-16 12:17:54 -0400 |
commit | e3f2528713840b128ab7645ef826f331ad7cbb29 (patch) | |
tree | d49fed2fdfad126d333188fb5f821e1ff8e93fd3 | |
parent | 5ca7d7d15d410ddd07acedcafa53cb7c6f40d45b (diff) |
Merge duplicated logic for clearing CPUID bits
-rw-r--r-- | src/cli/speed.cpp | 14 | ||||
-rw-r--r-- | src/lib/utils/cpuid.cpp | 40 | ||||
-rw-r--r-- | src/lib/utils/cpuid.h | 2 | ||||
-rw-r--r-- | src/tests/tests.cpp | 41 |
4 files changed, 48 insertions, 49 deletions
diff --git a/src/cli/speed.cpp b/src/cli/speed.cpp index 99e4c157f..e53c3e785 100644 --- a/src/cli/speed.cpp +++ b/src/cli/speed.cpp @@ -1,5 +1,5 @@ /* -* (C) 2009,2010,2014,2015 Jack Lloyd +* (C) 2009,2010,2014,2015,2017 Jack Lloyd * (C) 2015 Simon Warta (Kullo GmbH) * * Botan is released under the Simplified BSD License (see license.txt) @@ -677,14 +677,10 @@ class Speed final : public Command for(std::string cpuid_to_clear : Botan::split_on(get_arg("clear-cpuid"), ',')) { -#if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY) - if(cpuid_to_clear == "avx2") - Botan::CPUID::clear_cpuid_bit(Botan::CPUID::CPUID_AVX2_BIT); - else if(cpuid_to_clear == "sse2") - Botan::CPUID::clear_cpuid_bit(Botan::CPUID::CPUID_SSE2_BIT); - else if(cpuid_to_clear == "sha") - Botan::CPUID::clear_cpuid_bit(Botan::CPUID::CPUID_SHA_BIT); -#endif + for(auto bit : Botan::CPUID::bit_from_string(cpuid_to_clear)) + { + Botan::CPUID::clear_cpuid_bit(bit); + } } const bool using_defaults = (algos.empty()); diff --git a/src/lib/utils/cpuid.cpp b/src/lib/utils/cpuid.cpp index ed3fbc4b2..82654b9b4 100644 --- a/src/lib/utils/cpuid.cpp +++ b/src/lib/utils/cpuid.cpp @@ -458,4 +458,44 @@ CPUID::Endian_status CPUID::runtime_check_endian() return endian; } +std::vector<Botan::CPUID::CPUID_bits> +CPUID::bit_from_string(const std::string& tok) + { +#if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY) + if(tok == "sse2" || tok == "simd") + return {Botan::CPUID::CPUID_SSE2_BIT}; + if(tok == "ssse3") + return {Botan::CPUID::CPUID_SSSE3_BIT}; + if(tok == "aesni") + return {Botan::CPUID::CPUID_AESNI_BIT}; + if(tok == "clmul") + return {Botan::CPUID::CPUID_CLMUL_BIT}; + if(tok == "avx2") + return {Botan::CPUID::CPUID_AVX2_BIT}; + if(tok == "sha") + return {Botan::CPUID::CPUID_SHA_BIT}; + +#elif defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY) + if(tok == "altivec" || tok == "simd") + return {Botan::CPUID::CPUID_ALTIVEC_BIT}; + +#elif defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY) + if(tok == "neon" || tok == "simd") + return {Botan::CPUID::CPUID_ARM_NEON_BIT}; + if(tok == "armv8sha1") + return {Botan::CPUID::CPUID_ARM_SHA1_BIT}; + if(tok == "armv8sha2") + return {Botan::CPUID::CPUID_ARM_SHA2_BIT}; + if(tok == "armv8aes") + return {Botan::CPUID::CPUID_ARM_AES_BIT}; + if(tok == "armv8pmull") + return {Botan::CPUID::CPUID_ARM_PMULL_BIT}; + +#else + BOTAN_UNUSED(tok); +#endif + + return {}; + } + } diff --git a/src/lib/utils/cpuid.h b/src/lib/utils/cpuid.h index c27390729..119395c0d 100644 --- a/src/lib/utils/cpuid.h +++ b/src/lib/utils/cpuid.h @@ -9,6 +9,7 @@ #define BOTAN_CPUID_H__ #include <botan/types.h> +#include <vector> #include <string> #include <iosfwd> @@ -272,6 +273,7 @@ class BOTAN_DLL CPUID return ((g_processor_features & static_cast<uint64_t>(elem)) != 0); } + static std::vector<CPUID::CPUID_bits> bit_from_string(const std::string& tok); private: enum Endian_status : uint32_t { ENDIAN_UNKNOWN = 0x00000000, diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index 8f8aeec82..da1f874cf 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -872,52 +872,13 @@ std::string strip_ws(const std::string& in) return in.substr(first_c, last_c - first_c + 1); } -std::vector<Botan::CPUID::CPUID_bits> map_cpuid_string(const std::string& tok) - { -#if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY) - if(tok == "sse2" || tok == "simd") - return {Botan::CPUID::CPUID_SSE2_BIT}; - if(tok == "ssse3") - return {Botan::CPUID::CPUID_SSSE3_BIT}; - if(tok == "aesni") - return {Botan::CPUID::CPUID_AESNI_BIT}; - if(tok == "clmul") - return {Botan::CPUID::CPUID_CLMUL_BIT}; - if(tok == "avx2") - return {Botan::CPUID::CPUID_AVX2_BIT}; - if(tok == "sha") - return {Botan::CPUID::CPUID_SHA_BIT}; - -#elif defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY) - if(tok == "altivec" || tok == "simd") - return {Botan::CPUID::CPUID_ALTIVEC_BIT}; - -#elif defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY) - if(tok == "neon" || tok == "simd") - return {Botan::CPUID::CPUID_ARM_NEON_BIT}; - if(tok == "armv8sha1") - return {Botan::CPUID::CPUID_ARM_SHA1_BIT}; - if(tok == "armv8sha2") - return {Botan::CPUID::CPUID_ARM_SHA2_BIT}; - if(tok == "armv8aes") - return {Botan::CPUID::CPUID_ARM_AES_BIT}; - if(tok == "armv8pmull") - return {Botan::CPUID::CPUID_ARM_PMULL_BIT}; - -#else - BOTAN_UNUSED(tok); -#endif - - return {}; - } - std::vector<Botan::CPUID::CPUID_bits> parse_cpuid_bits(const std::vector<std::string>& tok) { std::vector<Botan::CPUID::CPUID_bits> bits; for(size_t i = 1; i < tok.size(); ++i) { - const std::vector<Botan::CPUID::CPUID_bits> more = map_cpuid_string(tok[i]); + const std::vector<Botan::CPUID::CPUID_bits> more = Botan::CPUID::bit_from_string(tok[i]); bits.insert(bits.end(), more.begin(), more.end()); } |