Home | History | Annotate | Download | only in source
      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 #ifdef _MSC_VER
     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
     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) && (_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__))
     49 LIBYUV_API
     50 void CpuId(uint32 info_eax, uint32 info_ecx, uint32* cpu_info) {
     51 #if defined(_MSC_VER)
     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.
     96 int 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
    113 LIBYUV_API
    114 void 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
    121 LIBYUV_API SAFEBUFFERS
    122 int 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__)
    144 static 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.
    165 LIBYUV_API
    166 int 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 
    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().
    182 static LIBYUV_BOOL TestEnv(const char*) {
    183   return LIBYUV_FALSE;
    184 }
    185 #endif
    186 
    187 LIBYUV_API SAFEBUFFERS
    188 int InitCpuFlags(void) {
    189 #if !defined(__pnacl__) && !defined(__CLR_VER) && defined(CPU_X86)
    190 
    191   uint32 cpu_info1[4] = { 0, 0, 0, 0 };
    192   uint32 cpu_info7[4] = { 0, 0, 0, 0 };
    193   CpuId(1, 0, cpu_info1);
    194   CpuId(7, 0, cpu_info7);
    195   cpu_info_ = ((cpu_info1[3] & 0x04000000) ? kCpuHasSSE2 : 0) |
    196               ((cpu_info1[2] & 0x00000200) ? kCpuHasSSSE3 : 0) |
    197               ((cpu_info1[2] & 0x00080000) ? kCpuHasSSE41 : 0) |
    198               ((cpu_info1[2] & 0x00100000) ? kCpuHasSSE42 : 0) |
    199               ((cpu_info7[1] & 0x00000200) ? kCpuHasERMS : 0) |
    200               ((cpu_info1[2] & 0x00001000) ? kCpuHasFMA3 : 0) |
    201               kCpuHasX86;
    202 #ifdef HAS_XGETBV
    203   if ((cpu_info1[2] & 0x18000000) == 0x18000000 &&  // AVX and OSSave
    204       TestOsSaveYmm()) {  // Saves YMM.
    205     cpu_info_ |= ((cpu_info7[1] & 0x00000020) ? kCpuHasAVX2 : 0) |
    206                  kCpuHasAVX;
    207   }
    208 #endif
    209   // Environment variable overrides for testing.
    210   if (TestEnv("LIBYUV_DISABLE_X86")) {
    211     cpu_info_ &= ~kCpuHasX86;
    212   }
    213   if (TestEnv("LIBYUV_DISABLE_SSE2")) {
    214     cpu_info_ &= ~kCpuHasSSE2;
    215   }
    216   if (TestEnv("LIBYUV_DISABLE_SSSE3")) {
    217     cpu_info_ &= ~kCpuHasSSSE3;
    218   }
    219   if (TestEnv("LIBYUV_DISABLE_SSE41")) {
    220     cpu_info_ &= ~kCpuHasSSE41;
    221   }
    222   if (TestEnv("LIBYUV_DISABLE_SSE42")) {
    223     cpu_info_ &= ~kCpuHasSSE42;
    224   }
    225   if (TestEnv("LIBYUV_DISABLE_AVX")) {
    226     cpu_info_ &= ~kCpuHasAVX;
    227   }
    228   if (TestEnv("LIBYUV_DISABLE_AVX2")) {
    229     cpu_info_ &= ~kCpuHasAVX2;
    230   }
    231   if (TestEnv("LIBYUV_DISABLE_ERMS")) {
    232     cpu_info_ &= ~kCpuHasERMS;
    233   }
    234   if (TestEnv("LIBYUV_DISABLE_FMA3")) {
    235     cpu_info_ &= ~kCpuHasFMA3;
    236   }
    237 #elif defined(__mips__) && defined(__linux__)
    238   // Linux mips parse text file for dsp detect.
    239   cpu_info_ = MipsCpuCaps("dsp");  // set kCpuHasMIPS_DSP.
    240 #if defined(__mips_dspr2)
    241   cpu_info_ |= kCpuHasMIPS_DSPR2;
    242 #endif
    243   cpu_info_ |= kCpuHasMIPS;
    244 
    245   if (getenv("LIBYUV_DISABLE_MIPS")) {
    246     cpu_info_ &= ~kCpuHasMIPS;
    247   }
    248   if (getenv("LIBYUV_DISABLE_MIPS_DSP")) {
    249     cpu_info_ &= ~kCpuHasMIPS_DSP;
    250   }
    251   if (getenv("LIBYUV_DISABLE_MIPS_DSPR2")) {
    252     cpu_info_ &= ~kCpuHasMIPS_DSPR2;
    253   }
    254 #elif defined(__arm__)
    255 // gcc -mfpu=neon defines __ARM_NEON__
    256 // __ARM_NEON__ generates code that requires Neon.  NaCL also requires Neon.
    257 // For Linux, /proc/cpuinfo can be tested but without that assume Neon.
    258 #if defined(__ARM_NEON__) || defined(__native_client__) || !defined(__linux__)
    259   cpu_info_ = kCpuHasNEON;
    260 #else
    261   // Linux arm parse text file for neon detect.
    262   cpu_info_ = ArmCpuCaps("/proc/cpuinfo");
    263 #endif
    264   cpu_info_ |= kCpuHasARM;
    265   if (TestEnv("LIBYUV_DISABLE_NEON")) {
    266     cpu_info_ &= ~kCpuHasNEON;
    267   }
    268 #endif  // __arm__
    269   if (TestEnv("LIBYUV_DISABLE_ASM")) {
    270     cpu_info_ = 0;
    271   }
    272   return cpu_info_;
    273 }
    274 
    275 LIBYUV_API
    276 void MaskCpuFlags(int enable_flags) {
    277   cpu_info_ = InitCpuFlags() & enable_flags;
    278 }
    279 
    280 #ifdef __cplusplus
    281 }  // extern "C"
    282 }  // namespace libyuv
    283 #endif
    284