cpu_id.cc 2.64 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
#include <stdlib.h>  // for getenv
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
14 15 16
#ifdef _MSC_VER
#include <intrin.h>
#endif
17 18 19
#ifdef __ANDROID__
#include <cpu-features.h>
#endif
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
20

frkoenig@google.com's avatar
frkoenig@google.com committed
21 22
#include "libyuv/basic_types.h"  // for CPU_X86

mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
23 24 25
// 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) {
26
  asm volatile (
27 28 29
    "mov %%ebx, %%edi                          \n"
    "cpuid                                     \n"
    "xchg %%edi, %%ebx                         \n"
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
30 31 32 33 34 35
    : "=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) {
36
  asm volatile (
37
    "cpuid                                     \n"
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
38 39 40 41 42 43
    : "=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

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

52
int InitCpuFlags() {
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
53 54 55
#ifdef CPU_X86
  int cpu_info[4];
  __cpuid(cpu_info, 1);
56 57 58
  cpu_info_ = (cpu_info[3] & 0x04000000 ? kCpuHasSSE2 : 0) |
              (cpu_info[2] & 0x00000200 ? kCpuHasSSSE3 : 0) |
              kCpuInitialized;
59 60 61 62 63 64 65 66 67

  // environment variable overrides for testing.
  if (getenv("LIBYUV_DISABLE_SSE2")) {
    cpu_info_ &= ~kCpuHasSSE2;
  }
  // environment variable overrides for testing.
  if (getenv("LIBYUV_DISABLE_SSSE3")) {
    cpu_info_ &= ~kCpuHasSSSE3;
  }
68
#elif defined(__ANDROID__) && defined(__ARM_NEON__)
69 70
  uint64_t features = android_getCpuFeatures();
  cpu_info_ = ((features & ANDROID_CPU_ARM_FEATURE_NEON) ? kCpuHasNEON : 0) |
71
              kCpuInitialized;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
72 73
#elif defined(__ARM_NEON__)
  // gcc -mfpu=neon defines __ARM_NEON__
74 75 76
  // Enable Neon if you want support for Neon and Arm, and use MaskCpuFlags
  // to disable Neon on devices that do not have it.
  cpu_info_ = kCpuHasNEON | kCpuInitialized;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
77
#else
78
  cpu_info_ = kCpuInitialized;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
79
#endif
80
  return cpu_info_;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
81 82
}

83
void MaskCpuFlags(int enable_flags) {
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
84
  InitCpuFlags();
85
  cpu_info_ = (cpu_info_ & enable_flags) | kCpuInitialized;
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
86 87
}

88 89
#ifdef __cplusplus
}  // extern "C"
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
90
}  // namespace libyuv
91
#endif