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__)
14#include <intrin.h>  // For __cpuidex()
15#endif
16#if !defined(__pnacl__) && !defined(__CLR_VER) && \
17    !defined(__native_client__) && defined(_M_X64) && \
18    defined(_MSC_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
33namespace libyuv {
34extern "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) && (_MSC_FULL_VER >= 160040219)
40#define SAFEBUFFERS __declspec(safebuffers)
41#else
42#define SAFEBUFFERS
43#endif
44
45// Low level cpuid for X86. Returns zeros on other CPUs.
46#if !defined(__pnacl__) && !defined(__CLR_VER) && \
47    (defined(_M_IX86) || defined(_M_X64) || \
48    defined(__i386__) || defined(__x86_64__))
49LIBYUV_API
50void CpuId(uint32 info_eax, uint32 info_ecx, uint32* cpu_info) {
51#if defined(_MSC_VER) && !defined(__clang__)
52#if (_MSC_FULL_VER >= 160040219)
53  __cpuidex((int*)(cpu_info), info_eax, info_ecx);
54#elif defined(_M_IX86)
55  __asm {
56    mov        eax, info_eax
57    mov        ecx, info_ecx
58    mov        edi, cpu_info
59    cpuid
60    mov        [edi], eax
61    mov        [edi + 4], ebx
62    mov        [edi + 8], ecx
63    mov        [edi + 12], edx
64  }
65#else
66  if (info_ecx == 0) {
67    __cpuid((int*)(cpu_info), info_eax);
68  } else {
69    cpu_info[3] = cpu_info[2] = cpu_info[1] = cpu_info[0] = 0;
70  }
71#endif
72#else  // defined(_MSC_VER)
73  uint32 info_ebx, info_edx;
74  asm volatile (  // NOLINT
75#if defined( __i386__) && defined(__PIC__)
76    // Preserve ebx for fpic 32 bit.
77    "mov %%ebx, %%edi                          \n"
78    "cpuid                                     \n"
79    "xchg %%edi, %%ebx                         \n"
80    : "=D" (info_ebx),
81#else
82    "cpuid                                     \n"
83    : "=b" (info_ebx),
84#endif  //  defined( __i386__) && defined(__PIC__)
85      "+a" (info_eax), "+c" (info_ecx), "=d" (info_edx));
86  cpu_info[0] = info_eax;
87  cpu_info[1] = info_ebx;
88  cpu_info[2] = info_ecx;
89  cpu_info[3] = info_edx;
90#endif  // defined(_MSC_VER)
91}
92
93#if !defined(__native_client__)
94#define HAS_XGETBV
95// X86 CPUs have xgetbv to detect OS saves high parts of ymm registers.
96int TestOsSaveYmm() {
97  uint32 xcr0 = 0u;
98#if defined(_MSC_VER) && (_MSC_FULL_VER >= 160040219)
99  xcr0 = (uint32)(_xgetbv(0));  // VS2010 SP1 required.
100#elif defined(_M_IX86)
101  __asm {
102    xor        ecx, ecx    // xcr 0
103    _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0  // For VS2010 and earlier.
104    mov        xcr0, eax
105  }
106#elif defined(__i386__) || defined(__x86_64__)
107  asm(".byte 0x0f, 0x01, 0xd0" : "=a" (xcr0) : "c" (0) : "%edx");
108#endif  // defined(_MSC_VER)
109  return((xcr0 & 6) == 6);  // Is ymm saved?
110}
111#endif  // !defined(__native_client__)
112#else
113LIBYUV_API
114void CpuId(uint32 eax, uint32 ecx, uint32* cpu_info) {
115  cpu_info[0] = cpu_info[1] = cpu_info[2] = cpu_info[3] = 0;
116}
117#endif
118
119// based on libvpx arm_cpudetect.c
120// For Arm, but public to allow testing on any CPU
121LIBYUV_API SAFEBUFFERS
122int ArmCpuCaps(const char* cpuinfo_name) {
123  char cpuinfo_line[512];
124  FILE* f = fopen(cpuinfo_name, "r");
125  if (!f) {
126    // Assume Neon if /proc/cpuinfo is unavailable.
127    // This will occur for Chrome sandbox for Pepper or Render process.
128    return kCpuHasNEON;
129  }
130  while (fgets(cpuinfo_line, sizeof(cpuinfo_line) - 1, f)) {
131    if (memcmp(cpuinfo_line, "Features", 8) == 0) {
132      char* p = strstr(cpuinfo_line, " neon");
133      if (p && (p[5] == ' ' || p[5] == '\n')) {
134        fclose(f);
135        return kCpuHasNEON;
136      }
137    }
138  }
139  fclose(f);
140  return 0;
141}
142
143#if defined(__mips__) && defined(__linux__)
144static int MipsCpuCaps(const char* search_string) {
145  char cpuinfo_line[512];
146  const char* file_name = "/proc/cpuinfo";
147  FILE* f = fopen(file_name, "r");
148  if (!f) {
149    // Assume DSP if /proc/cpuinfo is unavailable.
150    // This will occur for Chrome sandbox for Pepper or Render process.
151    return kCpuHasMIPS_DSP;
152  }
153  while (fgets(cpuinfo_line, sizeof(cpuinfo_line) - 1, f) != NULL) {
154    if (strstr(cpuinfo_line, search_string) != NULL) {
155      fclose(f);
156      return kCpuHasMIPS_DSP;
157    }
158  }
159  fclose(f);
160  return 0;
161}
162#endif
163
164// CPU detect function for SIMD instruction sets.
165LIBYUV_API
166int cpu_info_ = kCpuInit;  // 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
172static 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().
182static LIBYUV_BOOL TestEnv(const char*) {
183  return LIBYUV_FALSE;
184}
185#endif
186
187LIBYUV_API SAFEBUFFERS
188int InitCpuFlags(void) {
189#if !defined(__pnacl__) && !defined(__CLR_VER) && defined(CPU_X86)
190
191  uint32 cpu_info0[4] = { 0, 0, 0, 0 };
192  uint32 cpu_info1[4] = { 0, 0, 0, 0 };
193  uint32 cpu_info7[4] = { 0, 0, 0, 0 };
194  CpuId(0, 0, cpu_info0);
195  CpuId(1, 0, cpu_info1);
196  if (cpu_info0[0] >= 7) {
197    CpuId(7, 0, cpu_info7);
198  }
199  cpu_info_ = ((cpu_info1[3] & 0x04000000) ? kCpuHasSSE2 : 0) |
200              ((cpu_info1[2] & 0x00000200) ? kCpuHasSSSE3 : 0) |
201              ((cpu_info1[2] & 0x00080000) ? kCpuHasSSE41 : 0) |
202              ((cpu_info1[2] & 0x00100000) ? kCpuHasSSE42 : 0) |
203              ((cpu_info7[1] & 0x00000200) ? kCpuHasERMS : 0) |
204              ((cpu_info1[2] & 0x00001000) ? kCpuHasFMA3 : 0) |
205              kCpuHasX86;
206
207#ifdef HAS_XGETBV
208  if ((cpu_info1[2] & 0x18000000) == 0x18000000 &&  // AVX and OSSave
209      TestOsSaveYmm()) {  // Saves YMM.
210    cpu_info_ |= ((cpu_info7[1] & 0x00000020) ? kCpuHasAVX2 : 0) |
211                 kCpuHasAVX;
212  }
213#endif
214  // Environment variable overrides for testing.
215  if (TestEnv("LIBYUV_DISABLE_X86")) {
216    cpu_info_ &= ~kCpuHasX86;
217  }
218  if (TestEnv("LIBYUV_DISABLE_SSE2")) {
219    cpu_info_ &= ~kCpuHasSSE2;
220  }
221  if (TestEnv("LIBYUV_DISABLE_SSSE3")) {
222    cpu_info_ &= ~kCpuHasSSSE3;
223  }
224  if (TestEnv("LIBYUV_DISABLE_SSE41")) {
225    cpu_info_ &= ~kCpuHasSSE41;
226  }
227  if (TestEnv("LIBYUV_DISABLE_SSE42")) {
228    cpu_info_ &= ~kCpuHasSSE42;
229  }
230  if (TestEnv("LIBYUV_DISABLE_AVX")) {
231    cpu_info_ &= ~kCpuHasAVX;
232  }
233  if (TestEnv("LIBYUV_DISABLE_AVX2")) {
234    cpu_info_ &= ~kCpuHasAVX2;
235  }
236  if (TestEnv("LIBYUV_DISABLE_ERMS")) {
237    cpu_info_ &= ~kCpuHasERMS;
238  }
239  if (TestEnv("LIBYUV_DISABLE_FMA3")) {
240    cpu_info_ &= ~kCpuHasFMA3;
241  }
242#elif defined(__mips__) && defined(__linux__)
243  // Linux mips parse text file for dsp detect.
244  cpu_info_ = MipsCpuCaps("dsp");  // set kCpuHasMIPS_DSP.
245#if defined(__mips_dspr2)
246  cpu_info_ |= kCpuHasMIPS_DSPR2;
247#endif
248  cpu_info_ |= kCpuHasMIPS;
249
250  if (getenv("LIBYUV_DISABLE_MIPS")) {
251    cpu_info_ &= ~kCpuHasMIPS;
252  }
253  if (getenv("LIBYUV_DISABLE_MIPS_DSP")) {
254    cpu_info_ &= ~kCpuHasMIPS_DSP;
255  }
256  if (getenv("LIBYUV_DISABLE_MIPS_DSPR2")) {
257    cpu_info_ &= ~kCpuHasMIPS_DSPR2;
258  }
259#elif defined(__arm__)
260// gcc -mfpu=neon defines __ARM_NEON__
261// __ARM_NEON__ generates code that requires Neon.  NaCL also requires Neon.
262// For Linux, /proc/cpuinfo can be tested but without that assume Neon.
263#if defined(__ARM_NEON__) || defined(__native_client__) || !defined(__linux__)
264  cpu_info_ = kCpuHasNEON;
265#else
266  // Linux arm parse text file for neon detect.
267  cpu_info_ = ArmCpuCaps("/proc/cpuinfo");
268#endif
269  cpu_info_ |= kCpuHasARM;
270  if (TestEnv("LIBYUV_DISABLE_NEON")) {
271    cpu_info_ &= ~kCpuHasNEON;
272  }
273#endif  // __arm__
274  if (TestEnv("LIBYUV_DISABLE_ASM")) {
275    cpu_info_ = 0;
276  }
277  return cpu_info_;
278}
279
280LIBYUV_API
281void MaskCpuFlags(int enable_flags) {
282  cpu_info_ = InitCpuFlags() & enable_flags;
283}
284
285#ifdef __cplusplus
286}  // extern "C"
287}  // namespace libyuv
288#endif
289