1 // Copyright 2011 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
12 // Author: Christian Duvivier (cduvivier@google.com)
16 #if defined(__ANDROID__)
17 #include <cpu-features.h>
20 //------------------------------------------------------------------------------
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
) {
31 : "=a"(cpu_info
[0]), "=D"(cpu_info
[1]), "=c"(cpu_info
[2]), "=d"(cpu_info
[3])
34 #elif defined(__i386__) || defined(__x86_64__)
35 static WEBP_INLINE
void GetCPUInfo(int cpu_info
[4], int info_type
) {
38 : "=a"(cpu_info
[0]), "=b"(cpu_info
[1]), "=c"(cpu_info
[2]), "=d"(cpu_info
[3])
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
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;
52 // Use the raw opcode for xgetbv for compatibility with older toolchains.
54 ".byte 0x0f, 0x01, 0xd0\n"
55 : "=a"(eax
), "=d"(edx
) : "c" (ecx
));
56 return ((uint64_t)edx
<< 32) | eax
;
58 #elif defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 160040219 // >= VS2010 SP1
59 #define xgetbv() _xgetbv(0)
60 #elif defined(_MSC_VER) && defined(_M_IX86)
61 static WEBP_INLINE
uint64_t xgetbv(void) {
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
70 return ((uint64_t)edx_
<< 32) | eax_
;
73 #define xgetbv() 0U // no AVX for older x64 or unrecognized toolchains.
76 #if defined(__i386__) || defined(__x86_64__) || defined(WEBP_MSC_SSE2)
77 static int x86CPUInfo(CPUFeature feature
) {
79 GetCPUInfo(cpu_info
, 1);
80 if (feature
== kSSE2
) {
81 return 0 != (cpu_info
[3] & 0x04000000);
83 if (feature
== kSSE3
) {
84 return 0 != (cpu_info
[2] & 0x00000001);
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;
93 if (feature
== kAVX2
) {
94 if (x86CPUInfo(kAVX
)) {
95 GetCPUInfo(cpu_info
, 7);
96 return ((cpu_info
[1] & 0x00000020) == 0x00000020);
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
));
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
) {
120 VP8CPUInfo VP8GetCPUInfo
= armCPUInfo
;
121 #elif defined(WEBP_USE_MIPS32)
122 static int mipsCPUInfo(CPUFeature feature
) {
126 VP8CPUInfo VP8GetCPUInfo
= mipsCPUInfo
;
128 VP8CPUInfo VP8GetCPUInfo
= NULL
;