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])
32 : "a"(info_type
), "c"(0));
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])
39 : "a"(info_type
), "c"(0));
41 #elif (defined(_M_X64) || defined(_M_IX86)) && \
42 defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 150030729 // >= VS2008 SP1
44 #define GetCPUInfo(info, type) __cpuidex(info, type, 0) // set ecx=0
45 #elif defined(WEBP_MSC_SSE2)
46 #define GetCPUInfo __cpuid
49 // NaCl has no support for xgetbv or the raw opcode.
50 #if !defined(__native_client__) && (defined(__i386__) || defined(__x86_64__))
51 static WEBP_INLINE
uint64_t xgetbv(void) {
52 const uint32_t ecx
= 0;
54 // Use the raw opcode for xgetbv for compatibility with older toolchains.
56 ".byte 0x0f, 0x01, 0xd0\n"
57 : "=a"(eax
), "=d"(edx
) : "c" (ecx
));
58 return ((uint64_t)edx
<< 32) | eax
;
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)
65 static WEBP_INLINE
uint64_t xgetbv(void) {
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
74 return ((uint64_t)edx_
<< 32) | eax_
;
77 #define xgetbv() 0U // no AVX for older x64 or unrecognized toolchains.
80 #if defined(__i386__) || defined(__x86_64__) || defined(WEBP_MSC_SSE2)
81 static int x86CPUInfo(CPUFeature feature
) {
83 GetCPUInfo(cpu_info
, 1);
84 if (feature
== kSSE2
) {
85 return 0 != (cpu_info
[3] & 0x04000000);
87 if (feature
== kSSE3
) {
88 return 0 != (cpu_info
[2] & 0x00000001);
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;
97 if (feature
== kAVX2
) {
98 if (x86CPUInfo(kAVX
)) {
99 GetCPUInfo(cpu_info
, 7);
100 return ((cpu_info
[1] & 0x00000020) == 0x00000020);
105 VP8CPUInfo VP8GetCPUInfo
= x86CPUInfo
;
106 #elif defined(WEBP_ANDROID_NEON) // NB: needs to be before generic NEON test.
107 static 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
));
116 VP8CPUInfo 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
120 static int armCPUInfo(CPUFeature feature
) {
124 VP8CPUInfo VP8GetCPUInfo
= armCPUInfo
;
125 #elif defined(WEBP_USE_MIPS32)
126 static int mipsCPUInfo(CPUFeature feature
) {
130 VP8CPUInfo VP8GetCPUInfo
= mipsCPUInfo
;
132 VP8CPUInfo VP8GetCPUInfo
= NULL
;