cpu_id.cc 6.88 KB
Newer Older
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
1
/*
2
 *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
3 4 5 6 7 8 9 10
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

11
#include "libyuv/cpu_id.h"
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
12

13 14 15
#ifdef _MSC_VER
#include <intrin.h>  // For __cpuid()
#endif
16 17 18 19
#if !defined(__CLR_VER) && defined(_M_X64) && \
    defined(_MSC_VER) && (_MSC_FULL_VER >= 160040219)
#include <immintrin.h>  // For _xgetbv()
#endif
20

21
#include <stdlib.h>  // For getenv()
22 23 24 25

// For ArmCpuCaps() but unittested on all platforms
#include <stdio.h>
#include <string.h>
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
26

27
#include "libyuv/basic_types.h"  // For CPU_X86
frkoenig@google.com's avatar
frkoenig@google.com committed
28

29 30 31
// TODO(fbarchard): Use cpuid.h when gcc 4.4 is used on OSX and Linux.
#if (defined(__pic__) || defined(__APPLE__)) && defined(__i386__)
static __inline void __cpuid(int cpu_info[4], int info_type) {
32
  asm volatile (  // NOLINT
33 34 35 36 37 38 39 40
    "mov %%ebx, %%edi                          \n"
    "cpuid                                     \n"
    "xchg %%edi, %%ebx                         \n"
    : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
    : "a"(info_type));
}
#elif defined(__i386__) || defined(__x86_64__)
static __inline void __cpuid(int cpu_info[4], int info_type) {
41
  asm volatile (  // NOLINT
42 43 44 45 46 47
    "cpuid                                     \n"
    : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
    : "a"(info_type));
}
#endif

48
#ifdef __cplusplus
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
49
namespace libyuv {
50 51
extern "C" {
#endif
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
52

53
// Low level cpuid for X86. Returns zeros on other CPUs.
54 55
#if !defined(__CLR_VER) && (defined(_M_IX86) || defined(_M_X64) || \
    defined(__i386__) || defined(__x86_64__))
56
LIBYUV_API
57
void CpuId(int cpu_info[4], int info_type) {
58
  __cpuid(cpu_info, info_type);
59
}
60
#else
61
LIBYUV_API
62
void CpuId(int cpu_info[4], int) {
63 64 65 66
  cpu_info[0] = cpu_info[1] = cpu_info[2] = cpu_info[3] = 0;
}
#endif

67
// X86 CPUs have xgetbv to detect OS saves high parts of ymm registers.
68 69
#if !defined(__CLR_VER) && defined(_M_X64) && \
    defined(_MSC_VER) && (_MSC_FULL_VER >= 160040219)
70 71 72 73 74 75 76 77 78 79
#define HAS_XGETBV
static uint32 XGetBV(unsigned int xcr) {
  return static_cast<uint32>(_xgetbv(xcr));
}
#elif !defined(__CLR_VER) && defined(_M_IX86)
#define HAS_XGETBV
__declspec(naked) __declspec(align(16))
static uint32 XGetBV(unsigned int xcr) {
  __asm {
    mov        ecx, [esp + 4]    // xcr
80
    _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0  // xgetbv for vs2005.
81 82 83 84
    ret
  }
}
#elif defined(__i386__) || defined(__x86_64__)
85
#define HAS_XGETBV
86 87
static uint32 XGetBV(unsigned int xcr) {
  uint32 xcr_feature_mask;
88
  asm volatile (  // NOLINT
89 90 91 92 93
    ".byte 0x0f, 0x01, 0xd0\n"
    : "=a"(xcr_feature_mask)
    : "c"(xcr)
    : "memory", "cc", "edx");  // edx unused.
  return xcr_feature_mask;
94
}
95
#endif
96 97 98
#ifdef HAS_XGETBV
static const int kXCR_XFEATURE_ENABLED_MASK = 0;
#endif
99

100
// based on libvpx arm_cpudetect.c
101
// For Arm, but public to allow testing on any CPU
102
LIBYUV_API
103
int ArmCpuCaps(const char* cpuinfo_name) {
104 105
  FILE* f = fopen(cpuinfo_name, "r");
  if (f) {
106
    char buf[512];
107
    while (fgets(buf, 511, f)) {
108 109 110
      if (memcmp(buf, "Features", 8) == 0) {
        char* p = strstr(buf, " neon");
        if (p && (p[5] == ' ' || p[5] == '\n')) {
111 112
          fclose(f);
          return kCpuHasNEON;
113 114 115
        }
      }
    }
116
    fclose(f);
117
  }
118
  return 0;
119 120
}

121
#if defined(__mips__) && defined(__linux__)
122 123
static int MipsCpuCaps(const char* search_string) {
  const char* file_name = "/proc/cpuinfo";
124
  char cpuinfo_line[256];
125 126 127 128 129 130
  FILE* f = NULL;
  if ((f = fopen(file_name, "r")) != NULL) {
    while (fgets(cpuinfo_line, sizeof(cpuinfo_line), f) != NULL) {
      if (strstr(cpuinfo_line, search_string) != NULL) {
        fclose(f);
        return kCpuHasMIPS_DSP;
131 132
      }
    }
133
    fclose(f);
134 135
  }
  /* Did not find string in the proc file, or not Linux ELF. */
136
  return 0;
137
}
138
#endif
139

mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
140
// CPU detect function for SIMD instruction sets.
141
LIBYUV_API
142
int cpu_info_ = kCpuInit;  // cpu_info is not initialized yet.
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
143

144 145 146 147 148 149 150 151 152 153 154 155
// Test environment variable for disabling CPU features. Any non-zero value
// to disable. Zero ignored to make it easy to set the variable on/off.
static bool TestEnv(const char* name) {
  const char* var = getenv(name);
  if (var) {
    if (var[0] != '0') {
      return true;
    }
  }
  return false;
}

156 157
LIBYUV_API
int InitCpuFlags(void) {
158
#if !defined(__CLR_VER) && defined(CPU_X86)
159
  int cpu_info[4] = { 0, 0, 0, 0 };
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
160
  __cpuid(cpu_info, 1);
161 162 163 164 165
  cpu_info_ = ((cpu_info[3] & 0x04000000) ? kCpuHasSSE2 : 0) |
              ((cpu_info[2] & 0x00000200) ? kCpuHasSSSE3 : 0) |
              ((cpu_info[2] & 0x00080000) ? kCpuHasSSE41 : 0) |
              ((cpu_info[2] & 0x00100000) ? kCpuHasSSE42 : 0) |
              (((cpu_info[2] & 0x18000000) == 0x18000000) ? kCpuHasAVX : 0) |
166
              kCpuHasX86;
167 168 169
#ifdef HAS_XGETBV
  if (cpu_info_ & kCpuHasAVX) {
    __cpuid(cpu_info, 7);
170 171
    if ((cpu_info[1] & 0x00000020) &&
        ((XGetBV(kXCR_XFEATURE_ENABLED_MASK) & 0x06) == 0x06)) {
172 173 174 175
      cpu_info_ |= kCpuHasAVX2;
    }
  }
#endif
176
  // Environment variable overrides for testing.
177
  if (TestEnv("LIBYUV_DISABLE_X86")) {
178 179
    cpu_info_ &= ~kCpuHasX86;
  }
180
  if (TestEnv("LIBYUV_DISABLE_SSE2")) {
181 182
    cpu_info_ &= ~kCpuHasSSE2;
  }
183
  if (TestEnv("LIBYUV_DISABLE_SSSE3")) {
184 185
    cpu_info_ &= ~kCpuHasSSSE3;
  }
186
  if (TestEnv("LIBYUV_DISABLE_SSE41")) {
187 188
    cpu_info_ &= ~kCpuHasSSE41;
  }
189
  if (TestEnv("LIBYUV_DISABLE_SSE42")) {
190 191
    cpu_info_ &= ~kCpuHasSSE42;
  }
192
  if (TestEnv("LIBYUV_DISABLE_AVX")) {
193 194
    cpu_info_ &= ~kCpuHasAVX;
  }
195
  if (TestEnv("LIBYUV_DISABLE_AVX2")) {
196 197
    cpu_info_ &= ~kCpuHasAVX2;
  }
198
#elif defined(__mips__) && defined(__linux__)
199
  // Linux mips parse text file for dsp detect.
200
  cpu_info_ = MipsCpuCaps("dsp");  // set kCpuHasMIPS_DSP.
201 202 203 204 205 206 207 208 209 210
#if defined(__mips_dspr2)
  cpu_info_ |= kCpuHasMIPS_DSPR2;
#endif
  cpu_info_ |= kCpuHasMIPS;

  if (getenv("LIBYUV_DISABLE_MIPS")) {
    cpu_info_ &= ~kCpuHasMIPS;
  }
  if (getenv("LIBYUV_DISABLE_MIPS_DSP")) {
    cpu_info_ &= ~kCpuHasMIPS_DSP;
211
  }
212 213 214
  if (getenv("LIBYUV_DISABLE_MIPS_DSPR2")) {
    cpu_info_ &= ~kCpuHasMIPS_DSPR2;
  }
215
#elif defined(__arm__)
216
#if defined(__linux__) && (defined(__ARM_NEON__) || defined(LIBYUV_NEON))
217
  // Linux arm parse text file for neon detect.
218
  cpu_info_ = ArmCpuCaps("/proc/cpuinfo");
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
219 220
#elif defined(__ARM_NEON__)
  // gcc -mfpu=neon defines __ARM_NEON__
221 222
  // Enable Neon if you want support for Neon and Arm, and use MaskCpuFlags
  // to disable Neon on devices that do not have it.
223 224
  cpu_info_ = kCpuHasNEON;
#endif
225
  cpu_info_ |= kCpuHasARM;
226
  if (TestEnv("LIBYUV_DISABLE_NEON")) {
227 228
    cpu_info_ &= ~kCpuHasNEON;
  }
229
#endif  // __arm__
230
  if (TestEnv("LIBYUV_DISABLE_ASM")) {
231
    cpu_info_ = 0;
232
  }
233
  return cpu_info_;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
234 235
}

236
LIBYUV_API
237
void MaskCpuFlags(int enable_flags) {
238
  cpu_info_ = InitCpuFlags() & enable_flags;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
239 240
}

241 242
#ifdef __cplusplus
}  // extern "C"
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
243
}  // namespace libyuv
244
#endif