cpu.c revision 1e7bf8805bd030c19924a5306837ecd72c295751
1// Copyright 2011 Google Inc. All Rights Reserved.
2//
3// This code is licensed under the same terms as WebM:
4//  Software License Agreement:  http://www.webmproject.org/license/software/
5//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// CPU detection
9//
10// Author: Christian Duvivier (cduvivier@google.com)
11
12#include "./dsp.h"
13
14#if defined(__ANDROID__)
15#include "./cpu-features.h"
16#endif
17
18#if defined(__cplusplus) || defined(c_plusplus)
19extern "C" {
20#endif
21
22//------------------------------------------------------------------------------
23// SSE2 detection.
24//
25
26// apple/darwin gcc-4.0.1 defines __PIC__, but not __pic__ with -fPIC.
27#if (defined(__pic__) || defined(__PIC__)) && defined(__i386__)
28static WEBP_INLINE void GetCPUInfo(int cpu_info[4], int info_type) {
29  __asm__ volatile (
30    "mov %%ebx, %%edi\n"
31    "cpuid\n"
32    "xchg %%edi, %%ebx\n"
33    : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
34    : "a"(info_type));
35}
36#elif defined(__i386__) || defined(__x86_64__)
37static WEBP_INLINE void GetCPUInfo(int cpu_info[4], int info_type) {
38  __asm__ volatile (
39    "cpuid\n"
40    : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
41    : "a"(info_type));
42}
43#elif defined(WEBP_MSC_SSE2)
44#define GetCPUInfo __cpuid
45#endif
46
47#if defined(__i386__) || defined(__x86_64__) || defined(WEBP_MSC_SSE2)
48static int x86CPUInfo(CPUFeature feature) {
49  int cpu_info[4];
50  GetCPUInfo(cpu_info, 1);
51  if (feature == kSSE2) {
52    return 0 != (cpu_info[3] & 0x04000000);
53  }
54  if (feature == kSSE3) {
55    return 0 != (cpu_info[2] & 0x00000001);
56  }
57  return 0;
58}
59VP8CPUInfo VP8GetCPUInfo = x86CPUInfo;
60#elif defined(WEBP_ANDROID_NEON)
61static int AndroidCPUInfo(CPUFeature feature) {
62  const AndroidCpuFamily cpu_family = android_getCpuFamily();
63  const uint64_t cpu_features = android_getCpuFeatures();
64  if (feature == kNEON) {
65    return (cpu_family == ANDROID_CPU_FAMILY_ARM &&
66            0 != (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON));
67  }
68  return 0;
69}
70VP8CPUInfo VP8GetCPUInfo = AndroidCPUInfo;
71#elif defined(__ARM_NEON__)
72// define a dummy function to enable turning off NEON at runtime by setting
73// VP8DecGetCPUInfo = NULL
74static int armCPUInfo(CPUFeature feature) {
75  (void)feature;
76  return 1;
77}
78VP8CPUInfo VP8GetCPUInfo = armCPUInfo;
79#else
80VP8CPUInfo VP8GetCPUInfo = NULL;
81#endif
82
83#if defined(__cplusplus) || defined(c_plusplus)
84}    // extern "C"
85#endif
86