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)
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_FULL_VER) && (_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_FULL_VER) && (_MSC_FULL_VER >= 160040219) && \
40     !defined(__clang__)
41 #define SAFEBUFFERS __declspec(safebuffers)
42 #else
43 #define SAFEBUFFERS
44 #endif
45 
46 // Low level cpuid for X86.
47 #if (defined(_M_IX86) || defined(_M_X64) || \
48     defined(__i386__) || defined(__x86_64__)) && \
49     !defined(__pnacl__) && !defined(__CLR_VER)
50 LIBYUV_API
CpuId(uint32 info_eax,uint32 info_ecx,uint32 * cpu_info)51 void CpuId(uint32 info_eax, uint32 info_ecx, uint32* cpu_info) {
52 #if defined(_MSC_VER)
53 // Visual C version uses intrinsic or inline x86 assembly.
54 #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 160040219)
55   __cpuidex((int*)(cpu_info), info_eax, info_ecx);
56 #elif defined(_M_IX86)
57   __asm {
58     mov        eax, info_eax
59     mov        ecx, info_ecx
60     mov        edi, cpu_info
61     cpuid
62     mov        [edi], eax
63     mov        [edi + 4], ebx
64     mov        [edi + 8], ecx
65     mov        [edi + 12], edx
66   }
67 #else  // Visual C but not x86
68   if (info_ecx == 0) {
69     __cpuid((int*)(cpu_info), info_eax);
70   } else {
71     cpu_info[3] = cpu_info[2] = cpu_info[1] = cpu_info[0] = 0;
72   }
73 #endif
74 // GCC version uses inline x86 assembly.
75 #else  // defined(_MSC_VER)
76   uint32 info_ebx, info_edx;
77   asm volatile (
78 #if defined( __i386__) && defined(__PIC__)
79     // Preserve ebx for fpic 32 bit.
80     "mov %%ebx, %%edi                          \n"
81     "cpuid                                     \n"
82     "xchg %%edi, %%ebx                         \n"
83     : "=D" (info_ebx),
84 #else
85     "cpuid                                     \n"
86     : "=b" (info_ebx),
87 #endif  //  defined( __i386__) && defined(__PIC__)
88       "+a" (info_eax), "+c" (info_ecx), "=d" (info_edx));
89   cpu_info[0] = info_eax;
90   cpu_info[1] = info_ebx;
91   cpu_info[2] = info_ecx;
92   cpu_info[3] = info_edx;
93 #endif  // defined(_MSC_VER)
94 }
95 #else  // (defined(_M_IX86) || defined(_M_X64) ...
96 LIBYUV_API
97 void CpuId(uint32 eax, uint32 ecx, uint32* cpu_info) {
98   cpu_info[0] = cpu_info[1] = cpu_info[2] = cpu_info[3] = 0;
99 }
100 #endif
101 
102 // For VS2010 and earlier emit can be used:
103 //   _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0  // For VS2010 and earlier.
104 //  __asm {
105 //    xor        ecx, ecx    // xcr 0
106 //    xgetbv
107 //    mov        xcr0, eax
108 //  }
109 // For VS2013 and earlier 32 bit, the _xgetbv(0) optimizer produces bad code.
110 // https://code.google.com/p/libyuv/issues/detail?id=529
111 #if defined(_M_IX86) && (_MSC_VER < 1900)
112 #pragma optimize("g", off)
113 #endif
114 #if (defined(_M_IX86) || defined(_M_X64) || \
115     defined(__i386__) || defined(__x86_64__)) && \
116     !defined(__pnacl__) && !defined(__CLR_VER) && !defined(__native_client__)
117 #define HAS_XGETBV
118 // X86 CPUs have xgetbv to detect OS saves high parts of ymm registers.
GetXCR0()119 int GetXCR0() {
120   uint32 xcr0 = 0u;
121 #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 160040219)
122   xcr0 = (uint32)(_xgetbv(0));  // VS2010 SP1 required.
123 #elif defined(__i386__) || defined(__x86_64__)
124   asm(".byte 0x0f, 0x01, 0xd0" : "=a" (xcr0) : "c" (0) : "%edx");
125 #endif  // defined(__i386__) || defined(__x86_64__)
126   return xcr0;
127 }
128 #endif  // defined(_M_IX86) || defined(_M_X64) ..
129 // Return optimization to previous setting.
130 #if defined(_M_IX86) && (_MSC_VER < 1900)
131 #pragma optimize("g", on)
132 #endif
133 
134 // based on libvpx arm_cpudetect.c
135 // For Arm, but public to allow testing on any CPU
136 LIBYUV_API SAFEBUFFERS
ArmCpuCaps(const char * cpuinfo_name)137 int ArmCpuCaps(const char* cpuinfo_name) {
138   char cpuinfo_line[512];
139   FILE* f = fopen(cpuinfo_name, "r");
140   if (!f) {
141     // Assume Neon if /proc/cpuinfo is unavailable.
142     // This will occur for Chrome sandbox for Pepper or Render process.
143     return kCpuHasNEON;
144   }
145   while (fgets(cpuinfo_line, sizeof(cpuinfo_line) - 1, f)) {
146     if (memcmp(cpuinfo_line, "Features", 8) == 0) {
147       char* p = strstr(cpuinfo_line, " neon");
148       if (p && (p[5] == ' ' || p[5] == '\n')) {
149         fclose(f);
150         return kCpuHasNEON;
151       }
152       // aarch64 uses asimd for Neon.
153       p = strstr(cpuinfo_line, " asimd");
154       if (p && (p[6] == ' ' || p[6] == '\n')) {
155         fclose(f);
156         return kCpuHasNEON;
157       }
158     }
159   }
160   fclose(f);
161   return 0;
162 }
163 
164 // CPU detect function for SIMD instruction sets.
165 LIBYUV_API
166 int cpu_info_ = 0;  // cpu_info is not initialized yet.
167 
168 // Test environment variable for disabling CPU features. Any non-zero value
169 // to disable. Zero ignored to make it easy to set the variable on/off.
170 #if !defined(__native_client__) && !defined(_M_ARM)
171 
TestEnv(const char * name)172 static LIBYUV_BOOL TestEnv(const char* name) {
173   const char* var = getenv(name);
174   if (var) {
175     if (var[0] != '0') {
176       return LIBYUV_TRUE;
177     }
178   }
179   return LIBYUV_FALSE;
180 }
181 #else  // nacl does not support getenv().
TestEnv(const char *)182 static LIBYUV_BOOL TestEnv(const char*) {
183   return LIBYUV_FALSE;
184 }
185 #endif
186 
187 LIBYUV_API SAFEBUFFERS
InitCpuFlags(void)188 int InitCpuFlags(void) {
189   // TODO(fbarchard): swap kCpuInit logic so 0 means uninitialized.
190   int cpu_info = 0;
191 #if !defined(__pnacl__) && !defined(__CLR_VER) && defined(CPU_X86)
192   uint32 cpu_info0[4] = { 0, 0, 0, 0 };
193   uint32 cpu_info1[4] = { 0, 0, 0, 0 };
194   uint32 cpu_info7[4] = { 0, 0, 0, 0 };
195   CpuId(0, 0, cpu_info0);
196   CpuId(1, 0, cpu_info1);
197   if (cpu_info0[0] >= 7) {
198     CpuId(7, 0, cpu_info7);
199   }
200   cpu_info = ((cpu_info1[3] & 0x04000000) ? kCpuHasSSE2 : 0) |
201              ((cpu_info1[2] & 0x00000200) ? kCpuHasSSSE3 : 0) |
202              ((cpu_info1[2] & 0x00080000) ? kCpuHasSSE41 : 0) |
203              ((cpu_info1[2] & 0x00100000) ? kCpuHasSSE42 : 0) |
204              ((cpu_info7[1] & 0x00000200) ? kCpuHasERMS : 0) |
205              ((cpu_info1[2] & 0x00001000) ? kCpuHasFMA3 : 0) |
206              kCpuHasX86;
207 
208 #ifdef HAS_XGETBV
209   // AVX requires CPU has AVX, XSAVE and OSXSave for xgetbv
210   if (((cpu_info1[2] & 0x1c000000) == 0x1c000000) &&  // AVX and OSXSave
211       ((GetXCR0() & 6) == 6)) {  // Test OS saves YMM registers
212     cpu_info |= ((cpu_info7[1] & 0x00000020) ? kCpuHasAVX2 : 0) | kCpuHasAVX;
213 
214     // Detect AVX512bw
215     if ((GetXCR0() & 0xe0) == 0xe0) {
216       cpu_info |= (cpu_info7[1] & 0x40000000) ? kCpuHasAVX3 : 0;
217     }
218   }
219 #endif
220 
221   // Environment variable overrides for testing.
222   if (TestEnv("LIBYUV_DISABLE_X86")) {
223     cpu_info &= ~kCpuHasX86;
224   }
225   if (TestEnv("LIBYUV_DISABLE_SSE2")) {
226     cpu_info &= ~kCpuHasSSE2;
227   }
228   if (TestEnv("LIBYUV_DISABLE_SSSE3")) {
229     cpu_info &= ~kCpuHasSSSE3;
230   }
231   if (TestEnv("LIBYUV_DISABLE_SSE41")) {
232     cpu_info &= ~kCpuHasSSE41;
233   }
234   if (TestEnv("LIBYUV_DISABLE_SSE42")) {
235     cpu_info &= ~kCpuHasSSE42;
236   }
237   if (TestEnv("LIBYUV_DISABLE_AVX")) {
238     cpu_info &= ~kCpuHasAVX;
239   }
240   if (TestEnv("LIBYUV_DISABLE_AVX2")) {
241     cpu_info &= ~kCpuHasAVX2;
242   }
243   if (TestEnv("LIBYUV_DISABLE_ERMS")) {
244     cpu_info &= ~kCpuHasERMS;
245   }
246   if (TestEnv("LIBYUV_DISABLE_FMA3")) {
247     cpu_info &= ~kCpuHasFMA3;
248   }
249   if (TestEnv("LIBYUV_DISABLE_AVX3")) {
250     cpu_info &= ~kCpuHasAVX3;
251   }
252 #endif
253 #if defined(__mips__) && defined(__linux__)
254 #if defined(__mips_dspr2)
255   cpu_info |= kCpuHasDSPR2;
256 #endif
257   cpu_info |= kCpuHasMIPS;
258   if (getenv("LIBYUV_DISABLE_DSPR2")) {
259     cpu_info &= ~kCpuHasDSPR2;
260   }
261 #endif
262 #if defined(__arm__) || defined(__aarch64__)
263 // gcc -mfpu=neon defines __ARM_NEON__
264 // __ARM_NEON__ generates code that requires Neon.  NaCL also requires Neon.
265 // For Linux, /proc/cpuinfo can be tested but without that assume Neon.
266 #if defined(__ARM_NEON__) || defined(__native_client__) || !defined(__linux__)
267   cpu_info = kCpuHasNEON;
268 // For aarch64(arm64), /proc/cpuinfo's feature is not complete, e.g. no neon
269 // flag in it.
270 // So for aarch64, neon enabling is hard coded here.
271 #endif
272 #if defined(__aarch64__)
273   cpu_info = kCpuHasNEON;
274 #else
275   // Linux arm parse text file for neon detect.
276   cpu_info = ArmCpuCaps("/proc/cpuinfo");
277 #endif
278   cpu_info |= kCpuHasARM;
279   if (TestEnv("LIBYUV_DISABLE_NEON")) {
280     cpu_info &= ~kCpuHasNEON;
281   }
282 #endif  // __arm__
283   if (TestEnv("LIBYUV_DISABLE_ASM")) {
284     cpu_info = 0;
285   }
286   cpu_info  |= kCpuInitialized;
287   cpu_info_ = cpu_info;
288   return cpu_info;
289 }
290 
291 // Note that use of this function is not thread safe.
292 LIBYUV_API
MaskCpuFlags(int enable_flags)293 void MaskCpuFlags(int enable_flags) {
294   cpu_info_ = InitCpuFlags() & enable_flags;
295 }
296 
297 #ifdef __cplusplus
298 }  // extern "C"
299 }  // namespace libyuv
300 #endif
301