1 /*
2  *  Copyright (c) 2011 The LibYuv project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "third_party/libyuv/include/libyuv/cpu_id.h"
12 
13 #ifdef _MSC_VER
14 #include <intrin.h>
15 #endif
16 #ifdef __ANDROID__
17 #include <cpu-features.h>
18 #endif
19 
20 #include "third_party/libyuv/include/libyuv/basic_types.h"  // for CPU_X86
21 
22 // TODO(fbarchard): Use cpuid.h when gcc 4.4 is used on OSX and Linux.
23 #if (defined(__pic__) || defined(__APPLE__)) && defined(__i386__)
__cpuid(int cpu_info[4],int info_type)24 static inline void __cpuid(int cpu_info[4], int info_type) {
25   asm volatile (
26     "mov %%ebx, %%edi                          \n"
27     "cpuid                                     \n"
28     "xchg %%edi, %%ebx                         \n"
29     : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
30     : "a"(info_type)
31   );
32 }
33 #elif defined(__i386__) || defined(__x86_64__)
__cpuid(int cpu_info[4],int info_type)34 static inline void __cpuid(int cpu_info[4], int info_type) {
35   asm volatile (
36     "cpuid                                     \n"
37     : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
38     : "a"(info_type)
39   );
40 }
41 #endif
42 
43 #ifdef __cplusplus
44 namespace libyuv {
45 extern "C" {
46 #endif
47 
48 // CPU detect function for SIMD instruction sets.
49 int cpu_info_ = 0;
50 
InitCpuFlags()51 int InitCpuFlags() {
52 #ifdef CPU_X86
53   int cpu_info[4];
54   __cpuid(cpu_info, 1);
55   cpu_info_ = (cpu_info[3] & 0x04000000 ? kCpuHasSSE2 : 0) |
56               (cpu_info[2] & 0x00000200 ? kCpuHasSSSE3 : 0) |
57               kCpuInitialized;
58 #elif defined(__ANDROID__) && defined(__ARM_NEON__)
59   uint64_t features = android_getCpuFeatures();
60   cpu_info_ = ((features & ANDROID_CPU_ARM_FEATURE_NEON) ? kCpuHasNEON : 0) |
61               kCpuInitialized;
62 #elif defined(__ARM_NEON__)
63   // gcc -mfpu=neon defines __ARM_NEON__
64   // Enable Neon if you want support for Neon and Arm, and use MaskCpuFlags
65   // to disable Neon on devices that do not have it.
66   cpu_info_ = kCpuHasNEON | kCpuInitialized;
67 #else
68   cpu_info_ = kCpuInitialized;
69 #endif
70   return cpu_info_;
71 }
72 
MaskCpuFlags(int enable_flags)73 void MaskCpuFlags(int enable_flags) {
74   InitCpuFlags();
75   cpu_info_ = (cpu_info_ & enable_flags) | kCpuInitialized;
76 }
77 
78 #ifdef __cplusplus
79 }  // extern "C"
80 }  // namespace libyuv
81 #endif
82