]> granicus.if.org Git - libvpx/blob - third_party/libyuv/source/cpu_id.cc
update libyuv to r1456
[libvpx] / third_party / libyuv / source / cpu_id.cc
1 /*
2  *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS. All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include "libyuv/cpu_id.h"
12
13 #if (defined(_MSC_VER) && !defined(__clang__)) && !defined(__clang__)
14 #include <intrin.h>  // For __cpuidex()
15 #endif
16 #if !defined(__pnacl__) && !defined(__CLR_VER) && \
17     !defined(__native_client__) && (defined(_M_IX86) || defined(_M_X64)) && \
18     defined(_MSC_VER) && !defined(__clang__) && (_MSC_FULL_VER >= 160040219)
19 #include <immintrin.h>  // For _xgetbv()
20 #endif
21
22 #if !defined(__native_client__)
23 #include <stdlib.h>  // For getenv()
24 #endif
25
26 // For ArmCpuCaps() but unittested on all platforms
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "libyuv/basic_types.h"  // For CPU_X86
31
32 #ifdef __cplusplus
33 namespace libyuv {
34 extern "C" {
35 #endif
36
37 // For functions that use the stack and have runtime checks for overflow,
38 // use SAFEBUFFERS to avoid additional check.
39 #if (defined(_MSC_VER) && !defined(__clang__)) && (_MSC_FULL_VER >= 160040219)
40 #define SAFEBUFFERS __declspec(safebuffers)
41 #else
42 #define SAFEBUFFERS
43 #endif
44
45 // Low level cpuid for X86.
46 #if (defined(_M_IX86) || defined(_M_X64) || \
47     defined(__i386__) || defined(__x86_64__)) && \
48     !defined(__pnacl__) && !defined(__CLR_VER)
49 LIBYUV_API
50 void CpuId(uint32 info_eax, uint32 info_ecx, uint32* cpu_info) {
51 #if (defined(_MSC_VER) && !defined(__clang__)) && !defined(__clang__)
52 // Visual C version uses intrinsic or inline x86 assembly.
53 #if (_MSC_FULL_VER >= 160040219)
54   __cpuidex((int*)(cpu_info), info_eax, info_ecx);
55 #elif defined(_M_IX86)
56   __asm {
57     mov        eax, info_eax
58     mov        ecx, info_ecx
59     mov        edi, cpu_info
60     cpuid
61     mov        [edi], eax
62     mov        [edi + 4], ebx
63     mov        [edi + 8], ecx
64     mov        [edi + 12], edx
65   }
66 #else
67   if (info_ecx == 0) {
68     __cpuid((int*)(cpu_info), info_eax);
69   } else {
70     cpu_info[3] = cpu_info[2] = cpu_info[1] = cpu_info[0] = 0;
71   }
72 #endif
73 // GCC version uses inline x86 assembly.
74 #else  // (defined(_MSC_VER) && !defined(__clang__)) && !defined(__clang__)
75   uint32 info_ebx, info_edx;
76   asm volatile (  // NOLINT
77 #if defined( __i386__) && defined(__PIC__)
78     // Preserve ebx for fpic 32 bit.
79     "mov %%ebx, %%edi                          \n"
80     "cpuid                                     \n"
81     "xchg %%edi, %%ebx                         \n"
82     : "=D" (info_ebx),
83 #else
84     "cpuid                                     \n"
85     : "=b" (info_ebx),
86 #endif  //  defined( __i386__) && defined(__PIC__)
87       "+a" (info_eax), "+c" (info_ecx), "=d" (info_edx));
88   cpu_info[0] = info_eax;
89   cpu_info[1] = info_ebx;
90   cpu_info[2] = info_ecx;
91   cpu_info[3] = info_edx;
92 #endif  // (defined(_MSC_VER) && !defined(__clang__)) && !defined(__clang__)
93 }
94 #else  // (defined(_M_IX86) || defined(_M_X64) ...
95 LIBYUV_API
96 void CpuId(uint32 eax, uint32 ecx, uint32* cpu_info) {
97   cpu_info[0] = cpu_info[1] = cpu_info[2] = cpu_info[3] = 0;
98 }
99 #endif
100
101 // TODO(fbarchard): Enable xgetbv when validator supports it.
102 #if (defined(_M_IX86) || defined(_M_X64) || \
103     defined(__i386__) || defined(__x86_64__)) && \
104     !defined(__pnacl__) && !defined(__CLR_VER) && !defined(__native_client__)
105 #define HAS_XGETBV
106 // X86 CPUs have xgetbv to detect OS saves high parts of ymm registers.
107 int TestOsSaveYmm() {
108   uint32 xcr0 = 0u;
109 #if (defined(_MSC_VER) && !defined(__clang__)) && (_MSC_FULL_VER >= 160040219)
110   xcr0 = (uint32)(_xgetbv(0));  // VS2010 SP1 required.
111 #elif defined(_M_IX86) && defined(_MSC_VER) && !defined(__clang__)
112   __asm {
113     xor        ecx, ecx    // xcr 0
114     _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0  // For VS2010 and earlier.
115     mov        xcr0, eax
116   }
117 #elif defined(__i386__) || defined(__x86_64__)
118   asm(".byte 0x0f, 0x01, 0xd0" : "=a" (xcr0) : "c" (0) : "%edx");
119 #endif  // defined(__i386__) || defined(__x86_64__)
120   return((xcr0 & 6) == 6);  // Is ymm saved?
121 }
122 #endif  // defined(_M_IX86) || defined(_M_X64) ..
123
124 // based on libvpx arm_cpudetect.c
125 // For Arm, but public to allow testing on any CPU
126 LIBYUV_API SAFEBUFFERS
127 int ArmCpuCaps(const char* cpuinfo_name) {
128   char cpuinfo_line[512];
129   FILE* f = fopen(cpuinfo_name, "r");
130   if (!f) {
131     // Assume Neon if /proc/cpuinfo is unavailable.
132     // This will occur for Chrome sandbox for Pepper or Render process.
133     return kCpuHasNEON;
134   }
135   while (fgets(cpuinfo_line, sizeof(cpuinfo_line) - 1, f)) {
136     if (memcmp(cpuinfo_line, "Features", 8) == 0) {
137       char* p = strstr(cpuinfo_line, " neon");
138       if (p && (p[5] == ' ' || p[5] == '\n')) {
139         fclose(f);
140         return kCpuHasNEON;
141       }
142       // aarch64 uses asimd for Neon.
143       p = strstr(cpuinfo_line, " asimd");
144       if (p && (p[6] == ' ' || p[6] == '\n')) {
145         fclose(f);
146         return kCpuHasNEON;
147       }
148     }
149   }
150   fclose(f);
151   return 0;
152 }
153
154 #if defined(__mips__) && defined(__linux__)
155 static int MipsCpuCaps(const char* search_string) {
156   char cpuinfo_line[512];
157   const char* file_name = "/proc/cpuinfo";
158   FILE* f = fopen(file_name, "r");
159   if (!f) {
160     // Assume DSP if /proc/cpuinfo is unavailable.
161     // This will occur for Chrome sandbox for Pepper or Render process.
162     return kCpuHasMIPS_DSP;
163   }
164   while (fgets(cpuinfo_line, sizeof(cpuinfo_line) - 1, f) != NULL) {
165     if (strstr(cpuinfo_line, search_string) != NULL) {
166       fclose(f);
167       return kCpuHasMIPS_DSP;
168     }
169   }
170   fclose(f);
171   return 0;
172 }
173 #endif
174
175 // CPU detect function for SIMD instruction sets.
176 LIBYUV_API
177 int cpu_info_ = kCpuInit;  // cpu_info is not initialized yet.
178
179 // Test environment variable for disabling CPU features. Any non-zero value
180 // to disable. Zero ignored to make it easy to set the variable on/off.
181 #if !defined(__native_client__) && !defined(_M_ARM)
182
183 static LIBYUV_BOOL TestEnv(const char* name) {
184   const char* var = getenv(name);
185   if (var) {
186     if (var[0] != '0') {
187       return LIBYUV_TRUE;
188     }
189   }
190   return LIBYUV_FALSE;
191 }
192 #else  // nacl does not support getenv().
193 static LIBYUV_BOOL TestEnv(const char*) {
194   return LIBYUV_FALSE;
195 }
196 #endif
197
198 LIBYUV_API SAFEBUFFERS
199 int InitCpuFlags(void) {
200 #if !defined(__pnacl__) && !defined(__CLR_VER) && defined(CPU_X86)
201
202   uint32 cpu_info0[4] = { 0, 0, 0, 0 };
203   uint32 cpu_info1[4] = { 0, 0, 0, 0 };
204   uint32 cpu_info7[4] = { 0, 0, 0, 0 };
205   CpuId(0, 0, cpu_info0);
206   CpuId(1, 0, cpu_info1);
207   if (cpu_info0[0] >= 7) {
208     CpuId(7, 0, cpu_info7);
209   }
210   cpu_info_ = ((cpu_info1[3] & 0x04000000) ? kCpuHasSSE2 : 0) |
211               ((cpu_info1[2] & 0x00000200) ? kCpuHasSSSE3 : 0) |
212               ((cpu_info1[2] & 0x00080000) ? kCpuHasSSE41 : 0) |
213               ((cpu_info1[2] & 0x00100000) ? kCpuHasSSE42 : 0) |
214               ((cpu_info7[1] & 0x00000200) ? kCpuHasERMS : 0) |
215               ((cpu_info1[2] & 0x00001000) ? kCpuHasFMA3 : 0) |
216               kCpuHasX86;
217
218 #ifdef HAS_XGETBV
219   if ((cpu_info1[2] & 0x18000000) == 0x18000000 &&  // AVX and OSSave
220       TestOsSaveYmm()) {  // Saves YMM.
221     cpu_info_ |= ((cpu_info7[1] & 0x00000020) ? kCpuHasAVX2 : 0) |
222                  kCpuHasAVX;
223   }
224 #endif
225   // Environment variable overrides for testing.
226   if (TestEnv("LIBYUV_DISABLE_X86")) {
227     cpu_info_ &= ~kCpuHasX86;
228   }
229   if (TestEnv("LIBYUV_DISABLE_SSE2")) {
230     cpu_info_ &= ~kCpuHasSSE2;
231   }
232   if (TestEnv("LIBYUV_DISABLE_SSSE3")) {
233     cpu_info_ &= ~kCpuHasSSSE3;
234   }
235   if (TestEnv("LIBYUV_DISABLE_SSE41")) {
236     cpu_info_ &= ~kCpuHasSSE41;
237   }
238   if (TestEnv("LIBYUV_DISABLE_SSE42")) {
239     cpu_info_ &= ~kCpuHasSSE42;
240   }
241   if (TestEnv("LIBYUV_DISABLE_AVX")) {
242     cpu_info_ &= ~kCpuHasAVX;
243   }
244   if (TestEnv("LIBYUV_DISABLE_AVX2")) {
245     cpu_info_ &= ~kCpuHasAVX2;
246   }
247   if (TestEnv("LIBYUV_DISABLE_ERMS")) {
248     cpu_info_ &= ~kCpuHasERMS;
249   }
250   if (TestEnv("LIBYUV_DISABLE_FMA3")) {
251     cpu_info_ &= ~kCpuHasFMA3;
252   }
253 #endif
254 #if defined(__mips__) && defined(__linux__)
255   // Linux mips parse text file for dsp detect.
256   cpu_info_ = MipsCpuCaps("dsp");  // set kCpuHasMIPS_DSP.
257 #if defined(__mips_dspr2)
258   cpu_info_ |= kCpuHasMIPS_DSPR2;
259 #endif
260   cpu_info_ |= kCpuHasMIPS;
261
262   if (getenv("LIBYUV_DISABLE_MIPS")) {
263     cpu_info_ &= ~kCpuHasMIPS;
264   }
265   if (getenv("LIBYUV_DISABLE_MIPS_DSP")) {
266     cpu_info_ &= ~kCpuHasMIPS_DSP;
267   }
268   if (getenv("LIBYUV_DISABLE_MIPS_DSPR2")) {
269     cpu_info_ &= ~kCpuHasMIPS_DSPR2;
270   }
271 #endif
272 #if defined(__arm__) || defined(__aarch64__)
273 // gcc -mfpu=neon defines __ARM_NEON__
274 // __ARM_NEON__ generates code that requires Neon.  NaCL also requires Neon.
275 // For Linux, /proc/cpuinfo can be tested but without that assume Neon.
276 #if defined(__ARM_NEON__) || defined(__native_client__) || !defined(__linux__)
277   cpu_info_ = kCpuHasNEON;
278 // For aarch64(arm64), /proc/cpuinfo's feature is not complete, e.g. no neon
279 // flag in it.
280 // So for aarch64, neon enabling is hard coded here.
281 #endif
282 #if defined(__aarch64__)
283   cpu_info_ = kCpuHasNEON;
284 #else
285   // Linux arm parse text file for neon detect.
286   cpu_info_ = ArmCpuCaps("/proc/cpuinfo");
287 #endif
288   cpu_info_ |= kCpuHasARM;
289   if (TestEnv("LIBYUV_DISABLE_NEON")) {
290     cpu_info_ &= ~kCpuHasNEON;
291   }
292 #endif  // __arm__
293   if (TestEnv("LIBYUV_DISABLE_ASM")) {
294     cpu_info_ = 0;
295   }
296   return cpu_info_;
297 }
298
299 LIBYUV_API
300 void MaskCpuFlags(int enable_flags) {
301   cpu_info_ = InitCpuFlags() & enable_flags;
302 }
303
304 #ifdef __cplusplus
305 }  // extern "C"
306 }  // namespace libyuv
307 #endif