cpu_id.cc 7.19 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
 *
 *  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
7
 *  in the file PATENTS. All contributing project authors may
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
8 9 10
 *  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

fbarchard@google.com's avatar
fbarchard@google.com committed
29 30 31
// TODO(fbarchard): Consider cpu functionality for breakpoints, timer and cache.
// arm - bkpt vs intel int 3

32 33 34
// 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) {
35
  asm volatile (  // NOLINT
36 37 38 39 40 41 42 43
    "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) {
44
  asm volatile (  // NOLINT
45 46 47 48 49 50
    "cpuid                                     \n"
    : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
    : "a"(info_type));
}
#endif

51
#ifdef __cplusplus
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
52
namespace libyuv {
53 54
extern "C" {
#endif
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
55

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

70
// X86 CPUs have xgetbv to detect OS saves high parts of ymm registers.
71 72
#if !defined(__CLR_VER) && defined(_M_X64) && \
    defined(_MSC_VER) && (_MSC_FULL_VER >= 160040219)
73 74 75 76 77 78 79 80 81 82
#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
fbarchard@google.com's avatar
fbarchard@google.com committed
83
    push       edx
84
    _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0  // xgetbv for vs2005.
fbarchard@google.com's avatar
fbarchard@google.com committed
85
    pop        edx
86 87 88 89
    ret
  }
}
#elif defined(__i386__) || defined(__x86_64__)
90
#define HAS_XGETBV
91 92
static uint32 XGetBV(unsigned int xcr) {
  uint32 xcr_feature_mask;
93
  asm volatile (  // NOLINT
94 95 96 97 98
    ".byte 0x0f, 0x01, 0xd0\n"
    : "=a"(xcr_feature_mask)
    : "c"(xcr)
    : "memory", "cc", "edx");  // edx unused.
  return xcr_feature_mask;
99
}
100
#endif
101 102 103
#ifdef HAS_XGETBV
static const int kXCR_XFEATURE_ENABLED_MASK = 0;
#endif
104

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

126
#if defined(__mips__) && defined(__linux__)
127 128
static int MipsCpuCaps(const char* search_string) {
  const char* file_name = "/proc/cpuinfo";
129
  char cpuinfo_line[256];
130 131 132 133 134 135
  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;
136 137
      }
    }
138
    fclose(f);
139 140
  }
  /* Did not find string in the proc file, or not Linux ELF. */
141
  return 0;
142
}
143
#endif
144

mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
145
// CPU detect function for SIMD instruction sets.
146
LIBYUV_API
147
int cpu_info_ = kCpuInit;  // cpu_info is not initialized yet.
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
148

149 150 151 152 153 154 155 156 157 158 159 160
// 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;
}

161 162
LIBYUV_API
int InitCpuFlags(void) {
163
#if !defined(__CLR_VER) && defined(CPU_X86)
164 165 166 167 168 169 170 171 172
  int cpu_info1[4] = { 0, 0, 0, 0 };
  int cpu_info7[4] = { 0, 0, 0, 0 };
  __cpuid(cpu_info1, 1);
  __cpuid(cpu_info7, 7);
  cpu_info_ = ((cpu_info1[3] & 0x04000000) ? kCpuHasSSE2 : 0) |
              ((cpu_info1[2] & 0x00000200) ? kCpuHasSSSE3 : 0) |
              ((cpu_info1[2] & 0x00080000) ? kCpuHasSSE41 : 0) |
              ((cpu_info1[2] & 0x00100000) ? kCpuHasSSE42 : 0) |
              ((cpu_info7[1] & 0x00000200) ? kCpuHasERMS : 0) |
173
              kCpuHasX86;
174
#ifdef HAS_XGETBV
175
  if ((cpu_info1[2] & 0x18000000) == 0x18000000 &&  // AVX and OSSave
176 177
      (XGetBV(kXCR_XFEATURE_ENABLED_MASK) & 0x06) == 0x06) {  // Saves YMM.
    cpu_info_ |= ((cpu_info7[1] & 0x00000020) ? kCpuHasAVX2 : 0) |
fbarchard@google.com's avatar
fbarchard@google.com committed
178
                 kCpuHasAVX;
179 180
  }
#endif
181
  // Environment variable overrides for testing.
182
  if (TestEnv("LIBYUV_DISABLE_X86")) {
183 184
    cpu_info_ &= ~kCpuHasX86;
  }
185
  if (TestEnv("LIBYUV_DISABLE_SSE2")) {
186 187
    cpu_info_ &= ~kCpuHasSSE2;
  }
188
  if (TestEnv("LIBYUV_DISABLE_SSSE3")) {
189 190
    cpu_info_ &= ~kCpuHasSSSE3;
  }
191
  if (TestEnv("LIBYUV_DISABLE_SSE41")) {
192 193
    cpu_info_ &= ~kCpuHasSSE41;
  }
194
  if (TestEnv("LIBYUV_DISABLE_SSE42")) {
195 196
    cpu_info_ &= ~kCpuHasSSE42;
  }
197
  if (TestEnv("LIBYUV_DISABLE_AVX")) {
198 199
    cpu_info_ &= ~kCpuHasAVX;
  }
200
  if (TestEnv("LIBYUV_DISABLE_AVX2")) {
201 202
    cpu_info_ &= ~kCpuHasAVX2;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
203 204 205
  if (TestEnv("LIBYUV_DISABLE_ERMS")) {
    cpu_info_ &= ~kCpuHasERMS;
  }
206
#elif defined(__mips__) && defined(__linux__)
207
  // Linux mips parse text file for dsp detect.
208
  cpu_info_ = MipsCpuCaps("dsp");  // set kCpuHasMIPS_DSP.
209 210 211 212 213 214 215 216 217 218
#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;
219
  }
220 221 222
  if (getenv("LIBYUV_DISABLE_MIPS_DSPR2")) {
    cpu_info_ &= ~kCpuHasMIPS_DSPR2;
  }
223
#elif defined(__arm__)
224
#if defined(__linux__) && (defined(__ARM_NEON__) || defined(LIBYUV_NEON))
225
  // Linux arm parse text file for neon detect.
226
  cpu_info_ = ArmCpuCaps("/proc/cpuinfo");
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
227 228
#elif defined(__ARM_NEON__)
  // gcc -mfpu=neon defines __ARM_NEON__
229 230
  // Enable Neon if you want support for Neon and Arm, and use MaskCpuFlags
  // to disable Neon on devices that do not have it.
231 232
  cpu_info_ = kCpuHasNEON;
#endif
233
  cpu_info_ |= kCpuHasARM;
234
  if (TestEnv("LIBYUV_DISABLE_NEON")) {
235 236
    cpu_info_ &= ~kCpuHasNEON;
  }
237
#endif  // __arm__
238
  if (TestEnv("LIBYUV_DISABLE_ASM")) {
239
    cpu_info_ = 0;
240
  }
241
  return cpu_info_;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
242 243
}

244
LIBYUV_API
245
void MaskCpuFlags(int enable_flags) {
246
  cpu_info_ = InitCpuFlags() & enable_flags;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
247 248
}

249 250
#ifdef __cplusplus
}  // extern "C"
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
251
}  // namespace libyuv
252
#endif