From 9ab233b52aa0b2669433454f80aebc819e82209b Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Fri, 11 Aug 2017 16:09:28 -0400 Subject: [PATCH] Switch from 'static const' to 'enum' for class constants Enums do not take up space in a class. It should lead to smaller objects and faster runtimes --- config.h | 11 ++++--- cpu.cpp | 88 ++++++++++++++++++++++++++--------------------------- default.cpp | 16 +++++----- 3 files changed, 57 insertions(+), 58 deletions(-) diff --git a/config.h b/config.h index 96de92c6..b92f1d16 100644 --- a/config.h +++ b/config.h @@ -616,11 +616,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}; // ***************** Initialization and Constructor priorities ******************** diff --git a/cpu.cpp b/cpu.cpp index 26851a2b..bb3ec57c 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -220,78 +220,76 @@ 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; - if ((cpuid2[3] & (1 << 26)) != 0) + if ((cpuid1[3] & (1 << 26)) != 0) g_hasSSE2 = TrySSE2(); - g_hasSSSE3 = g_hasSSE2 && (cpuid2[2] & (1<<9)); - g_hasSSE41 = g_hasSSE2 && (cpuid2[2] & (1<<19)); - g_hasSSE42 = g_hasSSE2 && (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_hasSSE41 = g_hasSSE2 && (cpuid1[2] & (1<<19)); + g_hasSSE42 = g_hasSSE2 && (cpuid1[2] & (1<<20)); + g_hasAESNI = g_hasSSE2 && (cpuid1[2] & (1<<25)); + g_hasCLMUL = g_hasSSE2 && (cpuid1[2] & (1<<1)); - 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); + 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); } } } - else if (IsAMD(cpuid1)) + else if (IsAMD(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) }; - CpuId(0x01, cpuid1); + CpuId(0x80000005, cpuid2); + g_cacheLineSize = GETBYTE(cpuid2[2], 0); g_hasRDRAND = !!(cpuid1[2] /*ECX*/ & RDRAND_FLAG); if (cpuid1[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 (IsVIA(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 { 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, cpuid1); - if (cpuid1[0] >= 0xC0000001) + 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/default.cpp b/default.cpp index 8383499f..5db0f6fc 100644 --- a/default.cpp +++ b/default.cpp @@ -80,16 +80,16 @@ template DataEncryptor::DataEncryptor(const char *passphrase, BufferedTransformation *attachment) : ProxyFilter(NULLPTR, 0, 0, attachment), m_passphrase((const byte *)passphrase, strlen(passphrase)) { - CRYPTOPP_COMPILE_ASSERT(SALTLENGTH <= DIGESTSIZE); - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE <= DIGESTSIZE); + CRYPTOPP_COMPILE_ASSERT((int)SALTLENGTH <= DIGESTSIZE); + CRYPTOPP_COMPILE_ASSERT((int)BLOCKSIZE <= (int)DIGESTSIZE); } template DataEncryptor::DataEncryptor(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment) : ProxyFilter(NULLPTR, 0, 0, attachment), m_passphrase(passphrase, passphraseLength) { - CRYPTOPP_COMPILE_ASSERT(SALTLENGTH <= DIGESTSIZE); - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE <= DIGESTSIZE); + CRYPTOPP_COMPILE_ASSERT((int)SALTLENGTH <= (int)DIGESTSIZE); + CRYPTOPP_COMPILE_ASSERT((int)BLOCKSIZE <= (int)DIGESTSIZE); } template @@ -140,8 +140,8 @@ DataDecryptor::DataDecryptor(const char *p, BufferedTransformation *a , m_passphrase((const byte *)p, strlen(p)) , m_throwException(throwException) { - CRYPTOPP_COMPILE_ASSERT(SALTLENGTH <= DIGESTSIZE); - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE <= DIGESTSIZE); + CRYPTOPP_COMPILE_ASSERT((int)SALTLENGTH <= (int)DIGESTSIZE); + CRYPTOPP_COMPILE_ASSERT((int)BLOCKSIZE <= (int)DIGESTSIZE); } template @@ -151,8 +151,8 @@ DataDecryptor::DataDecryptor(const byte *passphrase, size_t passphras , m_passphrase(passphrase, passphraseLength) , m_throwException(throwException) { - CRYPTOPP_COMPILE_ASSERT(SALTLENGTH <= DIGESTSIZE); - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE <= DIGESTSIZE); + CRYPTOPP_COMPILE_ASSERT((int)SALTLENGTH <= (int)DIGESTSIZE); + CRYPTOPP_COMPILE_ASSERT((int)BLOCKSIZE <= (int)DIGESTSIZE); } template