blob: 84927ebc3e2b730fd637ee3519d6881c8751d556 [file] [log] [blame]
Shri Borde7cd81492011-11-02 13:20:24 -07001/*
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -08002 * Copyright 2011 The LibYuv Project Authors. All rights reserved.
Shri Borde7cd81492011-11-02 13:20:24 -07003 *
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
Hangyu Kuangf047e7c2016-07-06 14:21:45 -07007 * in the file PATENTS. All contributing project authors may
Shri Borde7cd81492011-11-02 13:20:24 -07008 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "libyuv/cpu_id.h"
Shri Borde7cd81492011-11-02 13:20:24 -070012
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070013#if defined(_MSC_VER)
14#include <intrin.h> // For __cpuidex()
Shri Borde7cd81492011-11-02 13:20:24 -070015#endif
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070016#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)
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080019#include <immintrin.h> // For _xgetbv()
20#endif
21
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070022#if !defined(__native_client__)
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080023#include <stdlib.h> // For getenv()
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070024#endif
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080025
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
Shri Borde7cd81492011-11-02 13:20:24 -070031
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080032#ifdef __cplusplus
Shri Borde7cd81492011-11-02 13:20:24 -070033namespace libyuv {
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080034extern "C" {
35#endif
36
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070037// 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)
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080042#else
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070043#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)
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080050LIBYUV_API
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070051void 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) ...
96LIBYUV_API
97void CpuId(uint32 eax, uint32 ecx, uint32* cpu_info) {
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080098 cpu_info[0] = cpu_info[1] = cpu_info[2] = cpu_info[3] = 0;
99}
100#endif
101
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700102// 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)
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800113#endif
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700114#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.
119int 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)
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800132#endif
133
134// based on libvpx arm_cpudetect.c
135// For Arm, but public to allow testing on any CPU
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700136LIBYUV_API SAFEBUFFERS
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800137int ArmCpuCaps(const char* cpuinfo_name) {
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700138 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;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800157 }
158 }
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800159 }
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700160 fclose(f);
161 return 0;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800162}
Shri Borde7cd81492011-11-02 13:20:24 -0700163
164// CPU detect function for SIMD instruction sets.
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800165LIBYUV_API
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700166int cpu_info_ = 0; // cpu_info is not initialized yet.
Shri Borde7cd81492011-11-02 13:20:24 -0700167
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800168// 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.
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700170#if !defined(__native_client__) && !defined(_M_ARM)
171
172static LIBYUV_BOOL TestEnv(const char* name) {
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800173 const char* var = getenv(name);
174 if (var) {
175 if (var[0] != '0') {
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700176 return LIBYUV_TRUE;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800177 }
178 }
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700179 return LIBYUV_FALSE;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800180}
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700181#else // nacl does not support getenv().
182static LIBYUV_BOOL TestEnv(const char*) {
183 return LIBYUV_FALSE;
184}
185#endif
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800186
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700187LIBYUV_API SAFEBUFFERS
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800188int InitCpuFlags(void) {
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700189 // 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
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800208#ifdef HAS_XGETBV
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700209 // 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;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800217 }
218 }
219#endif
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700220
221 // Environment variable overrides for testing.
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800222 if (TestEnv("LIBYUV_DISABLE_X86")) {
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700223 cpu_info &= ~kCpuHasX86;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800224 }
225 if (TestEnv("LIBYUV_DISABLE_SSE2")) {
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700226 cpu_info &= ~kCpuHasSSE2;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800227 }
228 if (TestEnv("LIBYUV_DISABLE_SSSE3")) {
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700229 cpu_info &= ~kCpuHasSSSE3;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800230 }
231 if (TestEnv("LIBYUV_DISABLE_SSE41")) {
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700232 cpu_info &= ~kCpuHasSSE41;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800233 }
234 if (TestEnv("LIBYUV_DISABLE_SSE42")) {
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700235 cpu_info &= ~kCpuHasSSE42;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800236 }
237 if (TestEnv("LIBYUV_DISABLE_AVX")) {
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700238 cpu_info &= ~kCpuHasAVX;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800239 }
240 if (TestEnv("LIBYUV_DISABLE_AVX2")) {
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700241 cpu_info &= ~kCpuHasAVX2;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800242 }
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700243 if (TestEnv("LIBYUV_DISABLE_ERMS")) {
244 cpu_info &= ~kCpuHasERMS;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800245 }
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700246 if (TestEnv("LIBYUV_DISABLE_FMA3")) {
247 cpu_info &= ~kCpuHasFMA3;
248 }
249 if (TestEnv("LIBYUV_DISABLE_AVX3")) {
250 cpu_info &= ~kCpuHasAVX3;
251 }
Shri Borde7cd81492011-11-02 13:20:24 -0700252#endif
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700253#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;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800260 }
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700261#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;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800281 }
282#endif // __arm__
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700283 if (TestEnv("LIBYUV_DISABLE_ASM")) {
284 cpu_info = 0;
285 }
286 cpu_info |= kCpuInitialized;
287 cpu_info_ = cpu_info;
288 return cpu_info;
Shri Borde7cd81492011-11-02 13:20:24 -0700289}
290
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700291// Note that use of this function is not thread safe.
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800292LIBYUV_API
Shri Borde7cd81492011-11-02 13:20:24 -0700293void MaskCpuFlags(int enable_flags) {
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700294 cpu_info_ = InitCpuFlags() & enable_flags;
Shri Borde7cd81492011-11-02 13:20:24 -0700295}
296
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800297#ifdef __cplusplus
298} // extern "C"
Shri Borde7cd81492011-11-02 13:20:24 -0700299} // namespace libyuv
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800300#endif