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
pull/461/head
Jeffrey Walton 2017-08-11 17:13:15 -04:00
parent 173dd0b530
commit 953252e44d
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
3 changed files with 67 additions and 68 deletions

View File

@ -490,7 +490,7 @@ NAMESPACE_END
// Microsoft plans to support ARM-64, but its not clear how to detect it. // 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 // 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(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 # define CRYPTOPP_BOOL_ARM_CRYPTO_INTRINSICS_AVAILABLE 1
# endif # endif
#endif #endif
@ -530,11 +530,12 @@ NAMESPACE_END
#endif #endif
// How to declare class constants // How to declare class constants
#if (_MSC_VER == 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) //#if (_MSC_VER == 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__)
# define CRYPTOPP_CONSTANT(x) enum {x}; //# define CRYPTOPP_CONSTANT(x) enum {x};
#else //#else
# define CRYPTOPP_CONSTANT(x) static const int x; //# define CRYPTOPP_CONSTANT(x) static const int x;
#endif //#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. // 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 // Detect via __ILP32__ (http://wiki.debian.org/X32Port). However, __ILP32__ shows up in more places than

116
cpu.cpp
View File

@ -219,90 +219,88 @@ static inline bool IsVIA(const word32 output[4])
void DetectX86Features() void DetectX86Features()
{ {
// Coverity finding CID 171239... // Coverity finding CID 171239...
word32 cpuid1[4]={0}, cpuid2[4]={0}, cpuid3[4]={0}; word32 cpuid0[4]={0}, cpuid1[4]={0}, cpuid2[4]={0};
if (!CpuId(0, cpuid1)) if (!CpuId(0, cpuid0))
return; return;
if (!CpuId(1, cpuid2)) if (!CpuId(1, cpuid1))
return; return;
g_hasMMX = (cpuid2[3] & (1 << 23)) != 0; g_hasMMX = (cpuid1[3] & (1 << 23)) != 0;
if ((cpuid2[3] & (1 << 26)) != 0) if ((cpuid1[3] & (1 << 26)) != 0)
g_hasSSE2 = TrySSE2(); g_hasSSE2 = TrySSE2();
g_hasSSSE3 = g_hasSSE2 && (cpuid2[2] & (1<<9)); g_hasSSSE3 = g_hasSSE2 && (cpuid1[2] & (1<<9));
g_hasSSE4 = g_hasSSE2 && ((cpuid2[2] & (1<<19)) && (cpuid2[2] & (1<<20))); g_hasSSE4 = g_hasSSE2 && ((cpuid1[2] & (1<<19)) && (cpuid1[2] & (1<<20)));
g_hasAESNI = g_hasSSE2 && (cpuid2[2] & (1<<25)); g_hasAESNI = g_hasSSE2 && (cpuid1[2] & (1<<25));
g_hasCLMUL = g_hasSSE2 && (cpuid2[2] & (1<<1)); g_hasCLMUL = g_hasSSE2 && (cpuid1[2] & (1<<1));
if ((cpuid2[3] & (1 << 25)) != 0) if ((cpuid1[3] & (1 << 25)) != 0)
g_hasISSE = true; g_hasISSE = true;
else else
{ {
CpuId(0x080000000, cpuid3); CpuId(0x080000000, cpuid2);
if (cpuid3[0] >= 0x080000001) if (cpuid2[0] >= 0x080000001)
{ {
CpuId(0x080000001, cpuid3); CpuId(0x080000001, cpuid2);
g_hasISSE = (cpuid3[3] & (1 << 22)) != 0; g_hasISSE = (cpuid2[3] & (1 << 22)) != 0;
} }
} }
if (IsIntel(cpuid1)) if (IsIntel(cpuid0))
{ {
static const unsigned int RDRAND_FLAG = (1 << 30); enum { RDRAND_FLAG = (1 << 30) };
static const unsigned int RDSEED_FLAG = (1 << 18); enum { RDSEED_FLAG = (1 << 18) };
static const unsigned int SHA_FLAG = (1 << 29); enum { SHA_FLAG = (1 << 29) };
g_isP4 = ((cpuid2[0] >> 8) & 0xf) == 0xf; g_isP4 = ((cpuid1[0] >> 8) & 0xf) == 0xf;
g_cacheLineSize = 8 * GETBYTE(cpuid2[1], 1); g_cacheLineSize = 8 * GETBYTE(cpuid1[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_hasRDRAND = !!(cpuid1[2] /*ECX*/ & RDRAND_FLAG); 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_hasRDSEED = !!(cpuid2[1] /*EBX*/ & RDSEED_FLAG);
g_hasSHA = !!(cpuid3[1] /*EBX*/ & SHA_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); enum { RDRAND_FLAG = (1 << 30) };
static const unsigned int ACE_FLAGS = (0x3 << 6); enum { RDSEED_FLAG = (1 << 18) };
static const unsigned int ACE2_FLAGS = (0x3 << 8); enum { SHA_FLAG = (1 << 29) };
static const unsigned int PHE_FLAGS = (0x3 << 10);
static const unsigned int PMM_FLAGS = (0x3 << 12);
CpuId(0xC0000000, cpuid1); CpuId(0x80000005, cpuid2);
if (cpuid1[0] >= 0xC0000001) 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 // Extended features available
CpuId(0xC0000001, cpuid1); CpuId(0xC0000001, cpuid0);
g_hasPadlockRNG = !!(cpuid1[3] /*EDX*/ & RNG_FLAGS); g_hasPadlockRNG = !!(cpuid0[3] /*EDX*/ & RNG_FLAGS);
g_hasPadlockACE = !!(cpuid1[3] /*EDX*/ & ACE_FLAGS); g_hasPadlockACE = !!(cpuid0[3] /*EDX*/ & ACE_FLAGS);
g_hasPadlockACE2 = !!(cpuid1[3] /*EDX*/ & ACE2_FLAGS); g_hasPadlockACE2 = !!(cpuid0[3] /*EDX*/ & ACE2_FLAGS);
g_hasPadlockPHE = !!(cpuid1[3] /*EDX*/ & PHE_FLAGS); g_hasPadlockPHE = !!(cpuid0[3] /*EDX*/ & PHE_FLAGS);
g_hasPadlockPMM = !!(cpuid1[3] /*EDX*/ & PMM_FLAGS); g_hasPadlockPMM = !!(cpuid0[3] /*EDX*/ & PMM_FLAGS);
} }
} }

View File

@ -342,7 +342,6 @@ bool TestSettings()
std::cout << std::endl; std::cout << std::endl;
#ifdef CRYPTOPP_CPUID_AVAILABLE #ifdef CRYPTOPP_CPUID_AVAILABLE
bool hasISSE = HasISSE();
bool hasSSE2 = HasSSE2(); bool hasSSE2 = HasSSE2();
bool hasSSSE3 = HasSSSE3(); bool hasSSSE3 = HasSSSE3();
bool hasSSE4 = HasSSE4(); bool hasSSE4 = HasSSE4();
@ -357,8 +356,9 @@ bool TestSettings()
else else
std::cout << "passed: "; std::cout << "passed: ";
std::cout << "hasISSE == " << hasISSE << "hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", hasSSE4 == " << hasSSE4; std::cout << "hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", hasSSE4 == " << hasSSE4;
std::cout << ", hasAESNI == " << HasAESNI() << ", hasCLMUL == " << HasCLMUL() << ", hasRDRAND == " << HasRDRAND() << ", hasRDSEED == " << HasRDSEED(); std::cout << ", hasAESNI == " << HasAESNI() << ", hasCLMUL == " << HasCLMUL();
std::cout << ", hasRDRAND == " << HasRDRAND() << ", hasRDSEED == " << HasRDSEED();
std::cout << ", hasSHA == " << HasSHA() << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize << std::endl; std::cout << ", hasSHA == " << HasSHA() << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize << std::endl;
#elif (CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64) #elif (CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64)