michael@0: /* michael@0: * Copyright 2011 The LibYuv Project Authors. All rights reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: #include "libyuv/cpu_id.h" michael@0: michael@0: #ifdef _MSC_VER michael@0: #include // For __cpuidex() michael@0: #endif michael@0: #if !defined(__pnacl__) && !defined(__CLR_VER) && \ michael@0: !defined(__native_client__) && defined(_M_X64) && \ michael@0: defined(_MSC_VER) && (_MSC_FULL_VER >= 160040219) michael@0: #include // For _xgetbv() michael@0: #endif michael@0: michael@0: #if !defined(__native_client__) michael@0: #include // For getenv() michael@0: #endif michael@0: michael@0: // For ArmCpuCaps() but unittested on all platforms michael@0: #include michael@0: #include michael@0: michael@0: #include "libyuv/basic_types.h" // For CPU_X86 michael@0: michael@0: #ifdef __cplusplus michael@0: namespace libyuv { michael@0: extern "C" { michael@0: #endif michael@0: michael@0: // For functions that use the stack and have runtime checks for overflow, michael@0: // use SAFEBUFFERS to avoid additional check. michael@0: #if defined(_MSC_VER) && (_MSC_FULL_VER >= 160040219) michael@0: #define SAFEBUFFERS __declspec(safebuffers) michael@0: #else michael@0: #define SAFEBUFFERS michael@0: #endif michael@0: michael@0: // Low level cpuid for X86. Returns zeros on other CPUs. michael@0: #if !defined(__pnacl__) && !defined(__CLR_VER) && \ michael@0: (defined(_M_IX86) || defined(_M_X64) || \ michael@0: defined(__i386__) || defined(__x86_64__)) michael@0: LIBYUV_API michael@0: void CpuId(uint32 info_eax, uint32 info_ecx, uint32* cpu_info) { michael@0: #if defined(_MSC_VER) michael@0: #if (_MSC_FULL_VER >= 160040219) michael@0: __cpuidex((int*)(cpu_info), info_eax, info_ecx); michael@0: #elif defined(_M_IX86) michael@0: __asm { michael@0: mov eax, info_eax michael@0: mov ecx, info_ecx michael@0: mov edi, cpu_info michael@0: cpuid michael@0: mov [edi], eax michael@0: mov [edi + 4], ebx michael@0: mov [edi + 8], ecx michael@0: mov [edi + 12], edx michael@0: } michael@0: #else michael@0: if (info_ecx == 0) { michael@0: __cpuid((int*)(cpu_info), info_eax); michael@0: } else { michael@0: cpu_info[3] = cpu_info[2] = cpu_info[1] = cpu_info[0] = 0; michael@0: } michael@0: #endif michael@0: #else // defined(_MSC_VER) michael@0: uint32 info_ebx, info_edx; michael@0: asm volatile ( // NOLINT michael@0: #if defined( __i386__) && defined(__PIC__) michael@0: // Preserve ebx for fpic 32 bit. michael@0: "mov %%ebx, %%edi \n" michael@0: "cpuid \n" michael@0: "xchg %%edi, %%ebx \n" michael@0: : "=D" (info_ebx), michael@0: #else michael@0: "cpuid \n" michael@0: : "=b" (info_ebx), michael@0: #endif // defined( __i386__) && defined(__PIC__) michael@0: "+a" (info_eax), "+c" (info_ecx), "=d" (info_edx)); michael@0: cpu_info[0] = info_eax; michael@0: cpu_info[1] = info_ebx; michael@0: cpu_info[2] = info_ecx; michael@0: cpu_info[3] = info_edx; michael@0: #endif // defined(_MSC_VER) michael@0: } michael@0: michael@0: #if !defined(__native_client__) michael@0: #define HAS_XGETBV michael@0: // X86 CPUs have xgetbv to detect OS saves high parts of ymm registers. michael@0: int TestOsSaveYmm() { michael@0: uint32 xcr0 = 0u; michael@0: #if defined(_MSC_VER) && defined(_XCR_XFEATURE_ENABLED_MASK) michael@0: xcr0 = (uint32)(_xgetbv(_XCR_XFEATURE_ENABLED_MASK)); michael@0: #elif defined(_MSC_VER) && defined(_M_IX86) michael@0: __asm { michael@0: xor ecx, ecx // xcr 0 michael@0: _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0 // For VS2010 and earlier. michael@0: mov xcr0, eax michael@0: } michael@0: #elif defined(__i386__) || defined(__x86_64__) michael@0: asm(".byte 0x0f, 0x01, 0xd0" : "=a" (xcr0) : "c" (0) : "%edx"); michael@0: #endif // defined(_MSC_VER) michael@0: return((xcr0 & 6) == 6); // Is ymm saved? michael@0: } michael@0: #endif // !defined(__native_client__) michael@0: #else michael@0: LIBYUV_API michael@0: void CpuId(uint32 eax, uint32 ecx, uint32* cpu_info) { michael@0: cpu_info[0] = cpu_info[1] = cpu_info[2] = cpu_info[3] = 0; michael@0: } michael@0: #endif michael@0: michael@0: // based on libvpx arm_cpudetect.c michael@0: // For Arm, but public to allow testing on any CPU michael@0: LIBYUV_API SAFEBUFFERS michael@0: int ArmCpuCaps(const char* cpuinfo_name) { michael@0: FILE* f = fopen(cpuinfo_name, "r"); michael@0: if (f) { michael@0: char cpuinfo_line[512]; michael@0: while (fgets(cpuinfo_line, sizeof(cpuinfo_line) - 1, f)) { michael@0: if (memcmp(cpuinfo_line, "Features", 8) == 0) { michael@0: char* p = strstr(cpuinfo_line, " neon"); michael@0: if (p && (p[5] == ' ' || p[5] == '\n')) { michael@0: fclose(f); michael@0: return kCpuHasNEON; michael@0: } michael@0: } michael@0: } michael@0: fclose(f); michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: #if defined(__mips__) && defined(__linux__) michael@0: static int MipsCpuCaps(const char* search_string) { michael@0: const char* file_name = "/proc/cpuinfo"; michael@0: char cpuinfo_line[256]; michael@0: FILE* f = NULL; michael@0: if ((f = fopen(file_name, "r")) != NULL) { michael@0: while (fgets(cpuinfo_line, sizeof(cpuinfo_line) - 1, f) != NULL) { michael@0: if (strstr(cpuinfo_line, search_string) != NULL) { michael@0: fclose(f); michael@0: return kCpuHasMIPS_DSP; michael@0: } michael@0: } michael@0: fclose(f); michael@0: } michael@0: /* Did not find string in the proc file, or not Linux ELF. */ michael@0: return 0; michael@0: } michael@0: #endif michael@0: michael@0: // CPU detect function for SIMD instruction sets. michael@0: LIBYUV_API michael@0: int cpu_info_ = kCpuInit; // cpu_info is not initialized yet. michael@0: michael@0: // Test environment variable for disabling CPU features. Any non-zero value michael@0: // to disable. Zero ignored to make it easy to set the variable on/off. michael@0: #if !defined(__native_client__) && !defined(_M_ARM) michael@0: michael@0: static LIBYUV_BOOL TestEnv(const char* name) { michael@0: const char* var = getenv(name); michael@0: if (var) { michael@0: if (var[0] != '0') { michael@0: return LIBYUV_TRUE; michael@0: } michael@0: } michael@0: return LIBYUV_FALSE; michael@0: } michael@0: #else // nacl does not support getenv(). michael@0: static LIBYUV_BOOL TestEnv(const char*) { michael@0: return LIBYUV_FALSE; michael@0: } michael@0: #endif michael@0: michael@0: LIBYUV_API SAFEBUFFERS michael@0: int InitCpuFlags(void) { michael@0: #if !defined(__pnacl__) && !defined(__CLR_VER) && defined(CPU_X86) michael@0: michael@0: uint32 cpu_info1[4] = { 0, 0, 0, 0 }; michael@0: uint32 cpu_info7[4] = { 0, 0, 0, 0 }; michael@0: CpuId(1, 0, cpu_info1); michael@0: CpuId(7, 0, cpu_info7); michael@0: cpu_info_ = ((cpu_info1[3] & 0x04000000) ? kCpuHasSSE2 : 0) | michael@0: ((cpu_info1[2] & 0x00000200) ? kCpuHasSSSE3 : 0) | michael@0: ((cpu_info1[2] & 0x00080000) ? kCpuHasSSE41 : 0) | michael@0: ((cpu_info1[2] & 0x00100000) ? kCpuHasSSE42 : 0) | michael@0: ((cpu_info7[1] & 0x00000200) ? kCpuHasERMS : 0) | michael@0: ((cpu_info1[2] & 0x00001000) ? kCpuHasFMA3 : 0) | michael@0: kCpuHasX86; michael@0: #ifdef HAS_XGETBV michael@0: if ((cpu_info1[2] & 0x18000000) == 0x18000000 && // AVX and OSSave michael@0: TestOsSaveYmm()) { // Saves YMM. michael@0: cpu_info_ |= ((cpu_info7[1] & 0x00000020) ? kCpuHasAVX2 : 0) | michael@0: kCpuHasAVX; michael@0: } michael@0: #endif michael@0: // Environment variable overrides for testing. michael@0: if (TestEnv("LIBYUV_DISABLE_X86")) { michael@0: cpu_info_ &= ~kCpuHasX86; michael@0: } michael@0: if (TestEnv("LIBYUV_DISABLE_SSE2")) { michael@0: cpu_info_ &= ~kCpuHasSSE2; michael@0: } michael@0: if (TestEnv("LIBYUV_DISABLE_SSSE3")) { michael@0: cpu_info_ &= ~kCpuHasSSSE3; michael@0: } michael@0: if (TestEnv("LIBYUV_DISABLE_SSE41")) { michael@0: cpu_info_ &= ~kCpuHasSSE41; michael@0: } michael@0: if (TestEnv("LIBYUV_DISABLE_SSE42")) { michael@0: cpu_info_ &= ~kCpuHasSSE42; michael@0: } michael@0: if (TestEnv("LIBYUV_DISABLE_AVX")) { michael@0: cpu_info_ &= ~kCpuHasAVX; michael@0: } michael@0: if (TestEnv("LIBYUV_DISABLE_AVX2")) { michael@0: cpu_info_ &= ~kCpuHasAVX2; michael@0: } michael@0: if (TestEnv("LIBYUV_DISABLE_ERMS")) { michael@0: cpu_info_ &= ~kCpuHasERMS; michael@0: } michael@0: if (TestEnv("LIBYUV_DISABLE_FMA3")) { michael@0: cpu_info_ &= ~kCpuHasFMA3; michael@0: } michael@0: #elif defined(__mips__) && defined(__linux__) michael@0: // Linux mips parse text file for dsp detect. michael@0: cpu_info_ = MipsCpuCaps("dsp"); // set kCpuHasMIPS_DSP. michael@0: #if defined(__mips_dspr2) michael@0: cpu_info_ |= kCpuHasMIPS_DSPR2; michael@0: #endif michael@0: cpu_info_ |= kCpuHasMIPS; michael@0: michael@0: if (getenv("LIBYUV_DISABLE_MIPS")) { michael@0: cpu_info_ &= ~kCpuHasMIPS; michael@0: } michael@0: if (getenv("LIBYUV_DISABLE_MIPS_DSP")) { michael@0: cpu_info_ &= ~kCpuHasMIPS_DSP; michael@0: } michael@0: if (getenv("LIBYUV_DISABLE_MIPS_DSPR2")) { michael@0: cpu_info_ &= ~kCpuHasMIPS_DSPR2; michael@0: } michael@0: #elif defined(__arm__) michael@0: #if defined(__linux__) && (defined(__ARM_NEON__) || defined(LIBYUV_NEON)) && \ michael@0: !defined(__native_client__) michael@0: // Linux arm parse text file for neon detect. michael@0: cpu_info_ = ArmCpuCaps("/proc/cpuinfo"); michael@0: #elif defined(__ARM_NEON__) || defined(__native_client__) michael@0: // gcc -mfpu=neon defines __ARM_NEON__ michael@0: // Enable Neon if you want support for Neon and Arm, and use MaskCpuFlags michael@0: // to disable Neon on devices that do not have it. michael@0: cpu_info_ = kCpuHasNEON; michael@0: #endif michael@0: cpu_info_ |= kCpuHasARM; michael@0: if (TestEnv("LIBYUV_DISABLE_NEON")) { michael@0: cpu_info_ &= ~kCpuHasNEON; michael@0: } michael@0: #endif // __arm__ michael@0: if (TestEnv("LIBYUV_DISABLE_ASM")) { michael@0: cpu_info_ = 0; michael@0: } michael@0: return cpu_info_; michael@0: } michael@0: michael@0: LIBYUV_API michael@0: void MaskCpuFlags(int enable_flags) { michael@0: cpu_info_ = InitCpuFlags() & enable_flags; michael@0: } michael@0: michael@0: #ifdef __cplusplus michael@0: } // extern "C" michael@0: } // namespace libyuv michael@0: #endif