diff options
-rw-r--r-- | doc/examples/cpuid.cpp | 6 | ||||
-rw-r--r-- | src/libstate/libstate.cpp | 3 | ||||
-rw-r--r-- | src/utils/cpuid.cpp | 47 | ||||
-rw-r--r-- | src/utils/cpuid.h | 25 |
4 files changed, 32 insertions, 49 deletions
diff --git a/doc/examples/cpuid.cpp b/doc/examples/cpuid.cpp index f4d441ba2..059cfdbc0 100644 --- a/doc/examples/cpuid.cpp +++ b/doc/examples/cpuid.cpp @@ -14,15 +14,17 @@ namespace { void print_if_feature(const std::string& feature_name, bool exists) { if(exists) - std::cout << feature_name << '\n'; + std::cout << "Y: " << feature_name << "\n"; else - std::cout << '[' << feature_name << ']' << '\n'; + std::cout << "N: " << feature_name << "\n"; } } int main() { + CPUID::initialize(); + std::cout << "Cache line size = " << CPUID::cache_line_size() << "\n"; print_if_feature("RDTSC", CPUID::has_rdtsc()); diff --git a/src/libstate/libstate.cpp b/src/libstate/libstate.cpp index 83fb31406..1db9ca44c 100644 --- a/src/libstate/libstate.cpp +++ b/src/libstate/libstate.cpp @@ -8,6 +8,7 @@ #include <botan/libstate.h> #include <botan/charset.h> #include <botan/engine.h> +#include <botan/cpuid.h> #include <botan/internal/defalloc.h> #include <botan/internal/default_engine.h> #include <botan/internal/mutex.h> @@ -241,6 +242,8 @@ RandomNumberGenerator& Library_State::global_rng() */ void Library_State::initialize(bool thread_safe) { + CPUID::initialize(); + if(mutex_factory) throw Invalid_State("Library_State has already been initialized"); diff --git a/src/utils/cpuid.cpp b/src/utils/cpuid.cpp index 4837e7ac4..b76210865 100644 --- a/src/utils/cpuid.cpp +++ b/src/utils/cpuid.cpp @@ -1,6 +1,6 @@ /* * Runtime CPU detection -* (C) 2009 Jack Lloyd +* (C) 2009-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -47,6 +47,10 @@ namespace Botan { +u64bit CPUID::x86_processor_flags = 0; +u32bit CPUID::cache_line = 32; +bool CPUID::altivec_capable = false; + namespace { u32bit get_x86_cache_line_size() @@ -140,54 +144,21 @@ bool altivec_check_pvr_emul() } -/* -* Call the x86 CPUID instruction and return the contents of ecx and -* edx, which contain the feature masks. -*/ -u64bit CPUID::x86_processor_flags() +void CPUID::initialize() { - static u64bit proc_flags = 0; - - if(proc_flags) - return proc_flags; - u32bit cpuid[4] = { 0 }; CALL_CPUID(1, cpuid); - // Set the FPU bit on to force caching in proc_flags - proc_flags = ((u64bit)cpuid[2] << 32) | cpuid[3] | 1; - - return proc_flags; - } - -u32bit CPUID::cache_line_size() - { - static u32bit cl_size = 0; - - if(cl_size) - return cl_size; + x86_processor_flags = ((u64bit)cpuid[2] << 32) | cpuid[3]; - cl_size = get_x86_cache_line_size(); + cache_line = get_x86_cache_line_size(); - return cl_size; - } + altivec_capable = false; -bool CPUID::has_altivec() - { - static bool first_time = true; - static bool altivec_capable = false; - - if(first_time) - { #if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY) if(altivec_check_sysctl() || altivec_check_pvr_emul()) altivec_capable = true; #endif - - first_time = false; - } - - return altivec_capable; } } diff --git a/src/utils/cpuid.h b/src/utils/cpuid.h index a6a278a6e..6339a0117 100644 --- a/src/utils/cpuid.h +++ b/src/utils/cpuid.h @@ -1,6 +1,6 @@ /* * Runtime CPU detection -* (C) 2009 Jack Lloyd +* (C) 2009-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -19,9 +19,14 @@ class BOTAN_DLL CPUID { public: /** + * Probe the CPU and see what extensions are supported + */ + static void initialize(); + + /** * Return a best guess of the cache line size */ - static u32bit cache_line_size(); + static u32bit cache_line_size() { return cache_line; } /** * Check if the processor supports RDTSC @@ -68,13 +73,8 @@ class BOTAN_DLL CPUID /** * Check if the processor supports AltiVec/VMX */ - static bool has_altivec(); + static bool has_altivec() { return altivec_capable; } private: - static bool x86_processor_flags_has(u64bit bit) - { - return ((x86_processor_flags() >> bit) & 1); - } - enum CPUID_bits { CPUID_RDTSC_BIT = 4, CPUID_SSE2_BIT = 26, @@ -85,7 +85,14 @@ class BOTAN_DLL CPUID CPUID_AVX_BIT = 60 }; - static u64bit x86_processor_flags(); + static bool x86_processor_flags_has(u64bit bit) + { + return ((x86_processor_flags >> bit) & 1); + } + + static u64bit x86_processor_flags; + static u32bit cache_line; + static bool altivec_capable; }; } |