2012-01-06 23:49:06 +01:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
2011-09-02 23:30:08 +02:00
|
|
|
//
|
2013-06-07 08:05:58 +02:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2011-09-02 23:30:08 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// CPU detection
|
|
|
|
//
|
|
|
|
// Author: Christian Duvivier (cduvivier@google.com)
|
|
|
|
|
|
|
|
#include "./dsp.h"
|
|
|
|
|
2012-05-04 21:34:53 +02:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
#include <cpu-features.h>
|
|
|
|
#endif
|
|
|
|
|
2011-09-02 23:30:08 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// SSE2 detection.
|
|
|
|
//
|
|
|
|
|
2012-01-18 00:53:45 +01:00
|
|
|
// apple/darwin gcc-4.0.1 defines __PIC__, but not __pic__ with -fPIC.
|
|
|
|
#if (defined(__pic__) || defined(__PIC__)) && defined(__i386__)
|
2011-11-05 03:44:57 +01:00
|
|
|
static WEBP_INLINE void GetCPUInfo(int cpu_info[4], int info_type) {
|
2011-09-02 23:30:08 +02:00
|
|
|
__asm__ volatile (
|
|
|
|
"mov %%ebx, %%edi\n"
|
|
|
|
"cpuid\n"
|
|
|
|
"xchg %%edi, %%ebx\n"
|
|
|
|
: "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
|
|
|
|
: "a"(info_type));
|
|
|
|
}
|
|
|
|
#elif defined(__i386__) || defined(__x86_64__)
|
2011-11-05 03:44:57 +01:00
|
|
|
static WEBP_INLINE void GetCPUInfo(int cpu_info[4], int info_type) {
|
2011-09-02 23:30:08 +02:00
|
|
|
__asm__ volatile (
|
|
|
|
"cpuid\n"
|
|
|
|
: "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
|
|
|
|
: "a"(info_type));
|
|
|
|
}
|
2014-05-22 07:59:47 +02:00
|
|
|
#elif defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 150030729 // >= VS2008 SP1
|
2014-05-17 03:43:11 +02:00
|
|
|
#define GetCPUInfo(info, type) __cpuidex(info, type, 0) // set ecx=0
|
2011-12-16 02:03:57 +01:00
|
|
|
#elif defined(WEBP_MSC_SSE2)
|
2011-09-02 23:30:08 +02:00
|
|
|
#define GetCPUInfo __cpuid
|
|
|
|
#endif
|
|
|
|
|
2014-05-29 23:30:10 +02:00
|
|
|
// NaCl has no support for xgetbv or the raw opcode.
|
|
|
|
#if !defined(__native_client__) && (defined(__i386__) || defined(__x86_64__))
|
2014-05-17 03:43:11 +02:00
|
|
|
static WEBP_INLINE uint64_t xgetbv(void) {
|
|
|
|
const uint32_t ecx = 0;
|
|
|
|
uint32_t eax, edx;
|
|
|
|
// Use the raw opcode for xgetbv for compatibility with older toolchains.
|
|
|
|
__asm__ volatile (
|
|
|
|
".byte 0x0f, 0x01, 0xd0\n"
|
|
|
|
: "=a"(eax), "=d"(edx) : "c" (ecx));
|
|
|
|
return ((uint64_t)edx << 32) | eax;
|
|
|
|
}
|
|
|
|
#elif defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 160040219 // >= VS2010 SP1
|
|
|
|
#define xgetbv() _xgetbv(0)
|
2014-08-16 00:11:40 +02:00
|
|
|
#elif defined(_MSC_VER) && defined(_M_IX86)
|
2014-05-17 03:43:11 +02:00
|
|
|
static WEBP_INLINE uint64_t xgetbv(void) {
|
|
|
|
uint32_t eax_, edx_;
|
|
|
|
__asm {
|
|
|
|
xor ecx, ecx // ecx = 0
|
|
|
|
// Use the raw opcode for xgetbv for compatibility with older toolchains.
|
|
|
|
__asm _emit 0x0f __asm _emit 0x01 __asm _emit 0xd0
|
|
|
|
mov eax_, eax
|
|
|
|
mov edx_, edx
|
|
|
|
}
|
|
|
|
return ((uint64_t)edx_ << 32) | eax_;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define xgetbv() 0U // no AVX for older x64 or unrecognized toolchains.
|
|
|
|
#endif
|
|
|
|
|
2011-12-16 02:03:57 +01:00
|
|
|
#if defined(__i386__) || defined(__x86_64__) || defined(WEBP_MSC_SSE2)
|
2011-09-02 23:30:08 +02:00
|
|
|
static int x86CPUInfo(CPUFeature feature) {
|
|
|
|
int cpu_info[4];
|
|
|
|
GetCPUInfo(cpu_info, 1);
|
|
|
|
if (feature == kSSE2) {
|
|
|
|
return 0 != (cpu_info[3] & 0x04000000);
|
|
|
|
}
|
|
|
|
if (feature == kSSE3) {
|
|
|
|
return 0 != (cpu_info[2] & 0x00000001);
|
|
|
|
}
|
2014-05-17 03:43:11 +02:00
|
|
|
if (feature == kAVX) {
|
|
|
|
// bits 27 (OSXSAVE) & 28 (256-bit AVX)
|
|
|
|
if ((cpu_info[2] & 0x18000000) == 0x18000000) {
|
|
|
|
// XMM state and YMM state enabled by the OS.
|
|
|
|
return (xgetbv() & 0x6) == 0x6;
|
|
|
|
}
|
|
|
|
}
|
2014-05-17 03:43:11 +02:00
|
|
|
if (feature == kAVX2) {
|
|
|
|
if (x86CPUInfo(kAVX)) {
|
|
|
|
GetCPUInfo(cpu_info, 7);
|
|
|
|
return ((cpu_info[1] & 0x00000020) == 0x00000020);
|
|
|
|
}
|
|
|
|
}
|
2011-09-02 23:30:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
VP8CPUInfo VP8GetCPUInfo = x86CPUInfo;
|
2014-05-14 08:37:01 +02:00
|
|
|
#elif defined(WEBP_ANDROID_NEON) // NB: needs to be before generic NEON test.
|
2012-05-04 21:34:53 +02:00
|
|
|
static int AndroidCPUInfo(CPUFeature feature) {
|
|
|
|
const AndroidCpuFamily cpu_family = android_getCpuFamily();
|
|
|
|
const uint64_t cpu_features = android_getCpuFeatures();
|
|
|
|
if (feature == kNEON) {
|
|
|
|
return (cpu_family == ANDROID_CPU_FAMILY_ARM &&
|
|
|
|
0 != (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
VP8CPUInfo VP8GetCPUInfo = AndroidCPUInfo;
|
2014-05-14 08:37:01 +02:00
|
|
|
#elif defined(WEBP_USE_NEON)
|
2011-09-12 15:03:01 +02:00
|
|
|
// define a dummy function to enable turning off NEON at runtime by setting
|
|
|
|
// VP8DecGetCPUInfo = NULL
|
|
|
|
static int armCPUInfo(CPUFeature feature) {
|
2012-01-28 02:39:47 +01:00
|
|
|
(void)feature;
|
2011-09-12 15:03:01 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
VP8CPUInfo VP8GetCPUInfo = armCPUInfo;
|
2014-08-12 11:47:36 +02:00
|
|
|
#elif defined(WEBP_USE_MIPS32) || defined(WEBP_USE_MIPS_DSP_R2)
|
2013-12-25 12:16:01 +01:00
|
|
|
static int mipsCPUInfo(CPUFeature feature) {
|
2014-08-12 11:47:36 +02:00
|
|
|
if ((feature == kMIPS32) || (feature == kMIPSdspR2)) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:16:01 +01:00
|
|
|
}
|
|
|
|
VP8CPUInfo VP8GetCPUInfo = mipsCPUInfo;
|
2011-09-02 23:30:08 +02:00
|
|
|
#else
|
|
|
|
VP8CPUInfo VP8GetCPUInfo = NULL;
|
|
|
|
#endif
|
|
|
|
|