cpu_id.cc 4.06 KB
Newer Older
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
1 2 3 4 5 6 7 8 9 10
/*
 *  Copyright (c) 2011 The LibYuv project authors. All Rights Reserved.
 *
 *  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 16
#ifdef _MSC_VER
#include <intrin.h>  // For __cpuid()
#endif

17
#include <stdlib.h>  // For getenv()
18 19 20 21

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

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

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
// 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) {
  asm volatile (
    "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) {
  asm volatile (
    "cpuid                                     \n"
    : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
    : "a"(info_type));
}
#endif

44
#ifdef __cplusplus
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
45
namespace libyuv {
46 47
extern "C" {
#endif
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
48

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

59
// based on libvpx arm_cpudetect.c
60 61
// For Arm, but public to allow testing on any CPU
int ArmCpuCaps(const char* cpuinfo_name) {
62
  int flags = 0;
63
  FILE* fin = fopen(cpuinfo_name, "r");
64 65 66 67
  if (fin) {
    char buf[512];
    while (fgets(buf, 511, fin)) {
      if (memcmp(buf, "Features", 8) == 0) {
68
        flags |= kCpuInitialized;
69 70 71 72 73 74 75 76 77 78 79 80
        char* p = strstr(buf, " neon");
        if (p && (p[5] == ' ' || p[5] == '\n')) {
          flags |= kCpuHasNEON;
          break;
        }
      }
    }
    fclose(fin);
  }
  return flags;
}

mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
81
// CPU detect function for SIMD instruction sets.
82
int cpu_info_ = 0;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
83

84
int InitCpuFlags() {
85
#if !defined(__CLR_VER) && defined(CPU_X86)
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
86 87
  int cpu_info[4];
  __cpuid(cpu_info, 1);
88 89 90 91 92
  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) |
93
              kCpuInitialized | kCpuHasX86;
94 95

  // environment variable overrides for testing.
96 97 98
  if (getenv("LIBYUV_DISABLE_X86")) {
    cpu_info_ &= ~kCpuHasX86;
  }
99 100 101 102 103 104
  if (getenv("LIBYUV_DISABLE_SSE2")) {
    cpu_info_ &= ~kCpuHasSSE2;
  }
  if (getenv("LIBYUV_DISABLE_SSSE3")) {
    cpu_info_ &= ~kCpuHasSSSE3;
  }
105 106 107
  if (getenv("LIBYUV_DISABLE_SSE41")) {
    cpu_info_ &= ~kCpuHasSSE41;
  }
108 109 110 111 112 113
  if (getenv("LIBYUV_DISABLE_SSE42")) {
    cpu_info_ &= ~kCpuHasSSE42;
  }
  if (getenv("LIBYUV_DISABLE_AVX")) {
    cpu_info_ &= ~kCpuHasAVX;
  }
114 115 116
  if (getenv("LIBYUV_DISABLE_ASM")) {
    cpu_info_ = kCpuInitialized;
  }
117 118 119 120
#elif defined(__arm__)
#if defined(__linux__) && defined(__ARM_NEON__)
  // linux arm parse text file for neon detect.
  cpu_info_ = ArmCpuCaps("/proc/cpuinfo");
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
121 122
#elif defined(__ARM_NEON__)
  // gcc -mfpu=neon defines __ARM_NEON__
123 124
  // Enable Neon if you want support for Neon and Arm, and use MaskCpuFlags
  // to disable Neon on devices that do not have it.
125 126 127
  cpu_info_ = kCpuHasNEON;
#endif
  cpu_info_ |= kCpuInitialized | kCpuHasARM;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
128
#endif
129
  return cpu_info_;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
130 131
}

132
void MaskCpuFlags(int enable_flags) {
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
133
  InitCpuFlags();
134
  cpu_info_ = (cpu_info_ & enable_flags) | kCpuInitialized;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
135 136
}

137 138
#ifdef __cplusplus
}  // extern "C"
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
139
}  // namespace libyuv
140
#endif