From 953252e44d25ce820c9d8b7aa708c0dfc1223e6e Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Fri, 11 Aug 2017 17:13:15 -0400 Subject: [PATCH] Move from 'static' to 'enum' for class constants Enums don't take up space in class objects. Its should result in smaller objects and faster code --- config.h | 13 +++--- cpu.cpp | 116 +++++++++++++++++++++++++-------------------------- validat1.cpp | 6 +-- 3 files changed, 67 insertions(+), 68 deletions(-) diff --git a/config.h b/config.h index ef1fed2b..0cb20bb5 100644 --- a/config.h +++ b/config.h @@ -490,7 +490,7 @@ NAMESPACE_END // Microsoft plans to support ARM-64, but its not clear how to detect it. // TODO: Add MSC_VER and ARM-64 platform define when available #if !defined(CRYPTOPP_BOOL_ARM_CRYPTO_INTRINSICS_AVAILABLE) && !defined(CRYPTOPP_DISABLE_ASM) -# if defined(__ARM_FEATURE_CRYPTO) +# if defined(__ARM_FEATURE_CRYPTO) || (CRYPTOPP_MSC_VERSION >= 1910) # define CRYPTOPP_BOOL_ARM_CRYPTO_INTRINSICS_AVAILABLE 1 # endif #endif @@ -530,11 +530,12 @@ NAMESPACE_END #endif // How to declare class constants -#if (_MSC_VER == 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) -# define CRYPTOPP_CONSTANT(x) enum {x}; -#else -# define CRYPTOPP_CONSTANT(x) static const int x; -#endif +//#if (_MSC_VER == 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) +//# define CRYPTOPP_CONSTANT(x) enum {x}; +//#else +//# define CRYPTOPP_CONSTANT(x) static const int x; +//#endif +#define CRYPTOPP_CONSTANT(x) enum {x}; // Linux provides X32, which is 32-bit integers, longs and pointers on x86_64 using the full x86_64 register set. // Detect via __ILP32__ (http://wiki.debian.org/X32Port). However, __ILP32__ shows up in more places than diff --git a/cpu.cpp b/cpu.cpp index dd2d87fa..366c35e8 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -219,90 +219,88 @@ static inline bool IsVIA(const word32 output[4]) void DetectX86Features() { // Coverity finding CID 171239... - word32 cpuid1[4]={0}, cpuid2[4]={0}, cpuid3[4]={0}; - if (!CpuId(0, cpuid1)) + word32 cpuid0[4]={0}, cpuid1[4]={0}, cpuid2[4]={0}; + if (!CpuId(0, cpuid0)) return; - if (!CpuId(1, cpuid2)) + if (!CpuId(1, cpuid1)) return; - g_hasMMX = (cpuid2[3] & (1 << 23)) != 0; - if ((cpuid2[3] & (1 << 26)) != 0) + g_hasMMX = (cpuid1[3] & (1 << 23)) != 0; + if ((cpuid1[3] & (1 << 26)) != 0) g_hasSSE2 = TrySSE2(); - g_hasSSSE3 = g_hasSSE2 && (cpuid2[2] & (1<<9)); - g_hasSSE4 = g_hasSSE2 && ((cpuid2[2] & (1<<19)) && (cpuid2[2] & (1<<20))); - g_hasAESNI = g_hasSSE2 && (cpuid2[2] & (1<<25)); - g_hasCLMUL = g_hasSSE2 && (cpuid2[2] & (1<<1)); + g_hasSSSE3 = g_hasSSE2 && (cpuid1[2] & (1<<9)); + g_hasSSE4 = g_hasSSE2 && ((cpuid1[2] & (1<<19)) && (cpuid1[2] & (1<<20))); + g_hasAESNI = g_hasSSE2 && (cpuid1[2] & (1<<25)); + g_hasCLMUL = g_hasSSE2 && (cpuid1[2] & (1<<1)); - if ((cpuid2[3] & (1 << 25)) != 0) + if ((cpuid1[3] & (1 << 25)) != 0) g_hasISSE = true; else { - CpuId(0x080000000, cpuid3); - if (cpuid3[0] >= 0x080000001) + CpuId(0x080000000, cpuid2); + if (cpuid2[0] >= 0x080000001) { - CpuId(0x080000001, cpuid3); - g_hasISSE = (cpuid3[3] & (1 << 22)) != 0; + CpuId(0x080000001, cpuid2); + g_hasISSE = (cpuid2[3] & (1 << 22)) != 0; } } - if (IsIntel(cpuid1)) + if (IsIntel(cpuid0)) { - static const unsigned int RDRAND_FLAG = (1 << 30); - static const unsigned int RDSEED_FLAG = (1 << 18); - static const unsigned int SHA_FLAG = (1 << 29); + enum { RDRAND_FLAG = (1 << 30) }; + enum { RDSEED_FLAG = (1 << 18) }; + enum { SHA_FLAG = (1 << 29) }; - g_isP4 = ((cpuid2[0] >> 8) & 0xf) == 0xf; - g_cacheLineSize = 8 * GETBYTE(cpuid2[1], 1); - g_hasRDRAND = !!(cpuid2[2] /*ECX*/ & RDRAND_FLAG); - - if (cpuid1[0] /*EAX*/ >= 7) - { - if (CpuId(7, cpuid3)) - { - g_hasRDSEED = !!(cpuid3[1] /*EBX*/ & RDSEED_FLAG); - g_hasSHA = !!(cpuid3[1] /*EBX*/ & SHA_FLAG); - } - } - } - else if (IsAMD(cpuid1)) - { - static const unsigned int RDRAND_FLAG = (1 << 30); - static const unsigned int RDSEED_FLAG = (1 << 18); - static const unsigned int SHA_FLAG = (1 << 29); - - CpuId(0x01, cpuid1); + g_isP4 = ((cpuid1[0] >> 8) & 0xf) == 0xf; + g_cacheLineSize = 8 * GETBYTE(cpuid1[1], 1); g_hasRDRAND = !!(cpuid1[2] /*ECX*/ & RDRAND_FLAG); - if (cpuid1[0] /*EAX*/ >= 7) + if (cpuid0[0] /*EAX*/ >= 7) { - if (CpuId(7, cpuid3)) + if (CpuId(7, cpuid2)) { - g_hasRDSEED = !!(cpuid3[1] /*EBX*/ & RDSEED_FLAG); - g_hasSHA = !!(cpuid3[1] /*EBX*/ & SHA_FLAG); + g_hasRDSEED = !!(cpuid2[1] /*EBX*/ & RDSEED_FLAG); + g_hasSHA = !!(cpuid2[1] /*EBX*/ & SHA_FLAG); } } - - CpuId(0x80000005, cpuid1); - g_cacheLineSize = GETBYTE(cpuid1[2], 0); } - else if (IsVIA(cpuid1)) + else if (IsAMD(cpuid0)) { - static const unsigned int RNG_FLAGS = (0x3 << 2); - static const unsigned int ACE_FLAGS = (0x3 << 6); - static const unsigned int ACE2_FLAGS = (0x3 << 8); - static const unsigned int PHE_FLAGS = (0x3 << 10); - static const unsigned int PMM_FLAGS = (0x3 << 12); + enum { RDRAND_FLAG = (1 << 30) }; + enum { RDSEED_FLAG = (1 << 18) }; + enum { SHA_FLAG = (1 << 29) }; - CpuId(0xC0000000, cpuid1); - if (cpuid1[0] >= 0xC0000001) + CpuId(0x80000005, cpuid2); + g_cacheLineSize = GETBYTE(cpuid2[2], 0); + g_hasRDRAND = !!(cpuid1[2] /*ECX*/ & RDRAND_FLAG); + + if (cpuid0[0] /*EAX*/ >= 7) + { + if (CpuId(7, cpuid2)) + { + g_hasRDSEED = !!(cpuid2[1] /*EBX*/ & RDSEED_FLAG); + g_hasSHA = !!(cpuid2[1] /*EBX*/ & SHA_FLAG); + } + } + } + else if (IsVIA(cpuid0)) + { + enum { RNG_FLAGS = (0x3 << 2) }; + enum { ACE_FLAGS = (0x3 << 6) }; + enum { ACE2_FLAGS = (0x3 << 8) }; + enum { PHE_FLAGS = (0x3 << 10) }; + enum { PMM_FLAGS = (0x3 << 12) }; + + CpuId(0xC0000000, cpuid0); + if (cpuid0[0] >= 0xC0000001) { // Extended features available - CpuId(0xC0000001, cpuid1); - g_hasPadlockRNG = !!(cpuid1[3] /*EDX*/ & RNG_FLAGS); - g_hasPadlockACE = !!(cpuid1[3] /*EDX*/ & ACE_FLAGS); - g_hasPadlockACE2 = !!(cpuid1[3] /*EDX*/ & ACE2_FLAGS); - g_hasPadlockPHE = !!(cpuid1[3] /*EDX*/ & PHE_FLAGS); - g_hasPadlockPMM = !!(cpuid1[3] /*EDX*/ & PMM_FLAGS); + CpuId(0xC0000001, cpuid0); + g_hasPadlockRNG = !!(cpuid0[3] /*EDX*/ & RNG_FLAGS); + g_hasPadlockACE = !!(cpuid0[3] /*EDX*/ & ACE_FLAGS); + g_hasPadlockACE2 = !!(cpuid0[3] /*EDX*/ & ACE2_FLAGS); + g_hasPadlockPHE = !!(cpuid0[3] /*EDX*/ & PHE_FLAGS); + g_hasPadlockPMM = !!(cpuid0[3] /*EDX*/ & PMM_FLAGS); } } diff --git a/validat1.cpp b/validat1.cpp index 57b67d1e..58ddcae9 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -342,7 +342,6 @@ bool TestSettings() std::cout << std::endl; #ifdef CRYPTOPP_CPUID_AVAILABLE - bool hasISSE = HasISSE(); bool hasSSE2 = HasSSE2(); bool hasSSSE3 = HasSSSE3(); bool hasSSE4 = HasSSE4(); @@ -357,8 +356,9 @@ bool TestSettings() else std::cout << "passed: "; - std::cout << "hasISSE == " << hasISSE << "hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", hasSSE4 == " << hasSSE4; - std::cout << ", hasAESNI == " << HasAESNI() << ", hasCLMUL == " << HasCLMUL() << ", hasRDRAND == " << HasRDRAND() << ", hasRDSEED == " << HasRDSEED(); + std::cout << "hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", hasSSE4 == " << hasSSE4; + std::cout << ", hasAESNI == " << HasAESNI() << ", hasCLMUL == " << HasCLMUL(); + std::cout << ", hasRDRAND == " << HasRDRAND() << ", hasRDSEED == " << HasRDSEED(); std::cout << ", hasSHA == " << HasSHA() << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize << std::endl; #elif (CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64)