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 (at) 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__) 26 static 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)); 33 } 34 #elif defined(__i386__) || defined(__x86_64__) 35 static 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)); 40 } 41 #elif defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 150030729 // >= VS2008 SP1 42 #define GetCPUInfo(info, type) __cpuidex(info, type, 0) // set ecx=0 43 #elif defined(WEBP_MSC_SSE2) 44 #define GetCPUInfo __cpuid 45 #endif 46 47 // NaCl has no support for xgetbv or the raw opcode. 48 #if !defined(__native_client__) && (defined(__i386__) || defined(__x86_64__)) 49 static WEBP_INLINE uint64_t xgetbv(void) { 50 const uint32_t ecx = 0; 51 uint32_t eax, edx; 52 // Use the raw opcode for xgetbv for compatibility with older toolchains. 53 __asm__ volatile ( 54 ".byte 0x0f, 0x01, 0xd0\n" 55 : "=a"(eax), "=d"(edx) : "c" (ecx)); 56 return ((uint64_t)edx << 32) | eax; 57 } 58 #elif defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 160040219 // >= VS2010 SP1 59 #define xgetbv() _xgetbv(0) 60 #elif defined(_M_IX86) 61 static WEBP_INLINE uint64_t xgetbv(void) { 62 uint32_t eax_, edx_; 63 __asm { 64 xor ecx, ecx // ecx = 0 65 // Use the raw opcode for xgetbv for compatibility with older toolchains. 66 __asm _emit 0x0f __asm _emit 0x01 __asm _emit 0xd0 67 mov eax_, eax 68 mov edx_, edx 69 } 70 return ((uint64_t)edx_ << 32) | eax_; 71 } 72 #else 73 #define xgetbv() 0U // no AVX for older x64 or unrecognized toolchains. 74 #endif 75 76 #if defined(__i386__) || defined(__x86_64__) || defined(WEBP_MSC_SSE2) 77 static int x86CPUInfo(CPUFeature feature) { 78 int cpu_info[4]; 79 GetCPUInfo(cpu_info, 1); 80 if (feature == kSSE2) { 81 return 0 != (cpu_info[3] & 0x04000000); 82 } 83 if (feature == kSSE3) { 84 return 0 != (cpu_info[2] & 0x00000001); 85 } 86 if (feature == kAVX) { 87 // bits 27 (OSXSAVE) & 28 (256-bit AVX) 88 if ((cpu_info[2] & 0x18000000) == 0x18000000) { 89 // XMM state and YMM state enabled by the OS. 90 return (xgetbv() & 0x6) == 0x6; 91 } 92 } 93 if (feature == kAVX2) { 94 if (x86CPUInfo(kAVX)) { 95 GetCPUInfo(cpu_info, 7); 96 return ((cpu_info[1] & 0x00000020) == 0x00000020); 97 } 98 } 99 return 0; 100 } 101 VP8CPUInfo VP8GetCPUInfo = x86CPUInfo; 102 #elif defined(WEBP_ANDROID_NEON) // NB: needs to be before generic NEON test. 103 static int AndroidCPUInfo(CPUFeature feature) { 104 const AndroidCpuFamily cpu_family = android_getCpuFamily(); 105 const uint64_t cpu_features = android_getCpuFeatures(); 106 if (feature == kNEON) { 107 return (cpu_family == ANDROID_CPU_FAMILY_ARM && 108 0 != (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON)); 109 } 110 return 0; 111 } 112 VP8CPUInfo VP8GetCPUInfo = AndroidCPUInfo; 113 #elif defined(WEBP_USE_NEON) 114 // define a dummy function to enable turning off NEON at runtime by setting 115 // VP8DecGetCPUInfo = NULL 116 static int armCPUInfo(CPUFeature feature) { 117 (void)feature; 118 return 1; 119 } 120 VP8CPUInfo VP8GetCPUInfo = armCPUInfo; 121 #elif defined(WEBP_USE_MIPS32) 122 static int mipsCPUInfo(CPUFeature feature) { 123 (void)feature; 124 return 1; 125 } 126 VP8CPUInfo VP8GetCPUInfo = mipsCPUInfo; 127 #else 128 VP8CPUInfo VP8GetCPUInfo = NULL; 129 #endif 130 131