From 891091c6d456699df6abf7f61211cbc7612991fd Mon Sep 17 00:00:00 2001 From: "fbarchard@google.com" Date: Sat, 29 Oct 2011 00:18:58 +0000 Subject: [PATCH] cpu_id using one variable and make it more public how to set flags to disable SIMD BUG=none TEST=scale unittest in talk disables SSSE3 Review URL: http://webrtc-codereview.appspot.com/239018 git-svn-id: http://libyuv.googlecode.com/svn/trunk@47 16f28f9a-4ce2-e073-06de-1de4eb20be90 --- include/libyuv/cpu_id.h | 5 +++-- source/cpu_id.cc | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/libyuv/cpu_id.h b/include/libyuv/cpu_id.h index efe17e23e..0e1ab48fc 100644 --- a/include/libyuv/cpu_id.h +++ b/include/libyuv/cpu_id.h @@ -17,14 +17,15 @@ namespace libyuv { static const int kCpuHasSSE2 = 1; static const int kCpuHasSSSE3 = 2; -// SIMD support on ARM processors +// These flags are only valid on ARM processors static const int kCpuHasNEON = 4; // Detect CPU has SSE2 etc. bool TestCpuFlag(int flag); // For testing, allow CPU flags to be disabled. -void MaskCpuFlagsForTest(int enable_flags); +// ie MaskCpuFlags(~kCpuHasSSSE3) to disable SSSE3. -1 to enable all. +void MaskCpuFlags(int enable_flags); } // namespace libyuv diff --git a/source/cpu_id.cc b/source/cpu_id.cc index e986015ea..fc388ba83 100644 --- a/source/cpu_id.cc +++ b/source/cpu_id.cc @@ -15,6 +15,9 @@ #include #endif +// Internal flag to indicate cpuid is initialized. +static const int kCpuInitialized = 16; + // TODO(fbarchard): Use cpuid.h when gcc 4.4 is used on OSX and Linux. #if (defined(__pic__) || defined(__APPLE__)) && defined(__i386__) static inline void __cpuid(int cpu_info[4], int info_type) { @@ -39,33 +42,33 @@ static inline void __cpuid(int cpu_info[4], int info_type) { namespace libyuv { // CPU detect function for SIMD instruction sets. -static bool cpu_info_initialized_ = false; static int cpu_info_ = 0; -// Global lock for cpu initialization. +// TODO(fbarchard): (cpu_info[2] & 0x10000000 ? kCpuHasAVX : 0) static void InitCpuFlags() { #ifdef CPU_X86 int cpu_info[4]; __cpuid(cpu_info, 1); - cpu_info_ = (cpu_info[2] & 0x00000200 ? kCpuHasSSSE3 : 0) | - (cpu_info[3] & 0x04000000 ? kCpuHasSSE2 : 0); + cpu_info_ = (cpu_info[3] & 0x04000000 ? kCpuHasSSE2 : 0) | + (cpu_info[2] & 0x00000200 ? kCpuHasSSSE3 : 0) | + kCpuInitialized; #elif defined(__ARM_NEON__) // gcc -mfpu=neon defines __ARM_NEON__ - // if code is specifically built for Neon-only, enable the flag. - cpu_info_ |= kCpuHasNEON; + // Enable Neon if you want support for Neon and Arm, and use MaskCpuFlags + // to disable Neon on devices that do not have it. + cpu_info_ = kCpuHasNEON | kCpuInitialized; #else - cpu_info_ = 0; + cpu_info_ = kCpuInitialized; #endif - cpu_info_initialized_ = true; } -void MaskCpuFlagsForTest(int enable_flags) { +void MaskCpuFlags(int enable_flags) { InitCpuFlags(); - cpu_info_ &= enable_flags; + cpu_info_ = (cpu_info_ & enable_flags) | kCpuInitialized; } bool TestCpuFlag(int flag) { - if (!cpu_info_initialized_) { + if (!cpu_info_) { InitCpuFlags(); } return cpu_info_ & flag ? true : false;