diff options
Diffstat (limited to 'src/lib/utils/cpuid.cpp')
-rw-r--r-- | src/lib/utils/cpuid.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/lib/utils/cpuid.cpp b/src/lib/utils/cpuid.cpp index 3fafadab7..cb25ed09d 100644 --- a/src/lib/utils/cpuid.cpp +++ b/src/lib/utils/cpuid.cpp @@ -8,6 +8,7 @@ #include <botan/cpuid.h> #include <botan/types.h> #include <botan/loadstor.h> +#include <botan/exceptn.h> #include <botan/mem_ops.h> #include <ostream> @@ -76,6 +77,7 @@ namespace Botan { u64bit CPUID::g_processor_flags[2] = { 0, 0 }; size_t CPUID::g_cache_line_size = BOTAN_TARGET_CPU_DEFAULT_CACHE_LINE_SIZE; bool CPUID::g_initialized = false; +bool CPUID::g_little_endian = false; namespace { @@ -256,6 +258,29 @@ void CPUID::initialize() g_processor_flags[0] = (1 << CPUID_SSE2_BIT) | (1 << CPUID_RDTSC_BIT); #endif + const uint32_t endian32 = 0x01234567; + const uint8_t* e8 = reinterpret_cast<const uint8_t*>(&endian32); + + if(e8[0] == 0x01 && e8[1] == 0x23 && e8[2] == 0x45 && e8[3] == 0x67) + { + g_little_endian = false; + } + else if(e8[0] == 0x67 && e8[1] == 0x45 && e8[2] == 0x23 && e8[3] == 0x01) + { + g_little_endian = true; + } + else + { + throw Internal_Error("Unexpected endian at runtime, neither big nor little"); + } + + // If we were compiled with a known endian, verify if matches at runtime +#if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN) + BOTAN_ASSERT(g_little_endian, "Little-endian build but big-endian at runtime"); +#elif defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN) + BOTAN_ASSERT(!g_little_endian, "Big-endian build but little-endian at runtime"); +#endif + g_initialized = true; } |