1// Copyright 2011 Google Inc. All Rights Reserved. 2// 3// Use of this source code is governed by a BSD-style license 4// that can be found in the COPYING file in the root of the source 5// tree. An additional intellectual property rights grant can be found 6// in the file PATENTS. All contributing project authors may 7// be found in the AUTHORS file in the root of the source tree. 8// ----------------------------------------------------------------------------- 9// 10// CPU detection 11// 12// Author: Christian Duvivier (cduvivier@google.com) 13 14#include "./dsp.h" 15 16#if defined(__ANDROID__) 17#include "cpu-features.h" 18#endif 19 20//------------------------------------------------------------------------------ 21// SSE2 detection. 22// 23 24// apple/darwin gcc-4.0.1 defines __PIC__, but not __pic__ with -fPIC. 25#if (defined(__pic__) || defined(__PIC__)) && defined(__i386__) 26static WEBP_INLINE void GetCPUInfo(int cpu_info[4], int info_type) { 27 __asm__ volatile ( 28 "mov %%ebx, %%edi\n" 29 "cpuid\n" 30 "xchg %%edi, %%ebx\n" 31 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3]) 32 : "a"(info_type), "c"(0)); 33} 34#elif defined(__i386__) || defined(__x86_64__) 35static WEBP_INLINE void GetCPUInfo(int cpu_info[4], int info_type) { 36 __asm__ volatile ( 37 "cpuid\n" 38 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3]) 39 : "a"(info_type), "c"(0)); 40} 41#elif (defined(_M_X64) || defined(_M_IX86)) && \ 42 defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 150030729 // >= VS2008 SP1 43#include <intrin.h> 44#define GetCPUInfo(info, type) __cpuidex(info, type, 0) // set ecx=0 45#elif defined(WEBP_MSC_SSE2) 46#define GetCPUInfo __cpuid 47#endif 48 49// NaCl has no support for xgetbv or the raw opcode. 50#if !defined(__native_client__) && (defined(__i386__) || defined(__x86_64__)) 51static WEBP_INLINE uint64_t xgetbv(void) { 52 const uint32_t ecx = 0; 53 uint32_t eax, edx; 54 // Use the raw opcode for xgetbv for compatibility with older toolchains. 55 __asm__ volatile ( 56 ".byte 0x0f, 0x01, 0xd0\n" 57 : "=a"(eax), "=d"(edx) : "c" (ecx)); 58 return ((uint64_t)edx << 32) | eax; 59} 60#elif (defined(_M_X64) || defined(_M_IX86)) && \ 61 defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 160040219 // >= VS2010 SP1 62#include <immintrin.h> 63#define xgetbv() _xgetbv(0) 64#elif defined(_MSC_VER) && defined(_M_IX86) 65static WEBP_INLINE uint64_t xgetbv(void) { 66 uint32_t eax_, edx_; 67 __asm { 68 xor ecx, ecx // ecx = 0 69 // Use the raw opcode for xgetbv for compatibility with older toolchains. 70 __asm _emit 0x0f __asm _emit 0x01 __asm _emit 0xd0 71 mov eax_, eax 72 mov edx_, edx 73 } 74 return ((uint64_t)edx_ << 32) | eax_; 75} 76#else 77#define xgetbv() 0U // no AVX for older x64 or unrecognized toolchains. 78#endif 79 80#if defined(__i386__) || defined(__x86_64__) || defined(WEBP_MSC_SSE2) 81static int x86CPUInfo(CPUFeature feature) { 82 int cpu_info[4]; 83 GetCPUInfo(cpu_info, 1); 84 if (feature == kSSE2) { 85 return 0 != (cpu_info[3] & 0x04000000); 86 } 87 if (feature == kSSE3) { 88 return 0 != (cpu_info[2] & 0x00000001); 89 } 90 if (feature == kAVX) { 91 // bits 27 (OSXSAVE) & 28 (256-bit AVX) 92 if ((cpu_info[2] & 0x18000000) == 0x18000000) { 93 // XMM state and YMM state enabled by the OS. 94 return (xgetbv() & 0x6) == 0x6; 95 } 96 } 97 if (feature == kAVX2) { 98 if (x86CPUInfo(kAVX)) { 99 GetCPUInfo(cpu_info, 7); 100 return ((cpu_info[1] & 0x00000020) == 0x00000020); 101 } 102 } 103 return 0; 104} 105VP8CPUInfo VP8GetCPUInfo = x86CPUInfo; 106#elif defined(WEBP_ANDROID_NEON) // NB: needs to be before generic NEON test. 107static int AndroidCPUInfo(CPUFeature feature) { 108 const AndroidCpuFamily cpu_family = android_getCpuFamily(); 109 const uint64_t cpu_features = android_getCpuFeatures(); 110 if (feature == kNEON) { 111 return (cpu_family == ANDROID_CPU_FAMILY_ARM && 112 0 != (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON)); 113 } 114 return 0; 115} 116VP8CPUInfo VP8GetCPUInfo = AndroidCPUInfo; 117#elif defined(WEBP_USE_NEON) 118// define a dummy function to enable turning off NEON at runtime by setting 119// VP8DecGetCPUInfo = NULL 120static int armCPUInfo(CPUFeature feature) { 121 (void)feature; 122 return 1; 123} 124VP8CPUInfo VP8GetCPUInfo = armCPUInfo; 125#elif defined(WEBP_USE_MIPS32) 126static int mipsCPUInfo(CPUFeature feature) { 127 (void)feature; 128 return 1; 129} 130VP8CPUInfo VP8GetCPUInfo = mipsCPUInfo; 131#else 132VP8CPUInfo VP8GetCPUInfo = NULL; 133#endif 134 135