From 0e009366f8b76b14cf032ee7909dad3f59d3a48a Mon Sep 17 00:00:00 2001 From: James Zern Date: Tue, 28 Apr 2015 17:46:07 -0700 Subject: [PATCH] dsp/cpu.c(x86): check maximum supported cpuid feature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit structured extended feature flags require eax = 7; avoids incorrectly detecting avx2 on some older processors that support avx. for completeness also check for value=1 support used by the other checks. from [1]: INPUT EAX = 0: Returns CPUID’s Highest Value for Basic Processor Information and the Vendor Identification String [1] http://www.intel.com/content/www/us/en/processors/processor-identification-cpuid-instruction-note.html Change-Id: I60b20d661a978d551614dbf7acdc25db19cb6046 --- src/dsp/cpu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/dsp/cpu.c b/src/dsp/cpu.c index 6d16171b..0f8107e7 100644 --- a/src/dsp/cpu.c +++ b/src/dsp/cpu.c @@ -79,7 +79,16 @@ static WEBP_INLINE uint64_t xgetbv(void) { #if defined(__i386__) || defined(__x86_64__) || defined(WEBP_MSC_SSE2) static int x86CPUInfo(CPUFeature feature) { + int max_cpuid_value; int cpu_info[4]; + + // get the highest feature value cpuid supports + GetCPUInfo(cpu_info, 0); + max_cpuid_value = cpu_info[0]; + if (max_cpuid_value < 1) { + return 0; + } + GetCPUInfo(cpu_info, 1); if (feature == kSSE2) { return 0 != (cpu_info[3] & 0x04000000); @@ -98,7 +107,7 @@ static int x86CPUInfo(CPUFeature feature) { } } if (feature == kAVX2) { - if (x86CPUInfo(kAVX)) { + if (x86CPUInfo(kAVX) && max_cpuid_value >= 7) { GetCPUInfo(cpu_info, 7); return ((cpu_info[1] & 0x00000020) == 0x00000020); }