blob: c4eb1fa9a25ea38a284af0630d2ad0296feebfc8 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
2 * Copyright (c) 2010 The WebM 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 <stdlib.h>
12#include <string.h>
13#include "aom_ports/arm.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070014#include "./aom_config.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070015
16#ifdef WINAPI_FAMILY
17#include <winapifamily.h>
18#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
19#define getenv(x) NULL
20#endif
21#endif
22
23static int arm_cpu_env_flags(int *flags) {
24 char *env;
Yaowu Xuf883b422016-08-30 14:01:10 -070025 env = getenv("AOM_SIMD_CAPS");
Yaowu Xuc27fc142016-08-22 16:08:15 -070026 if (env && *env) {
27 *flags = (int)strtol(env, NULL, 0);
28 return 0;
29 }
30 *flags = 0;
31 return -1;
32}
33
34static int arm_cpu_env_mask(void) {
35 char *env;
Yaowu Xuf883b422016-08-30 14:01:10 -070036 env = getenv("AOM_SIMD_CAPS_MASK");
Yaowu Xuc27fc142016-08-22 16:08:15 -070037 return env && *env ? (int)strtol(env, NULL, 0) : ~0;
38}
39
40#if !CONFIG_RUNTIME_CPU_DETECT
41
42int arm_cpu_caps(void) {
43 /* This function should actually be a no-op. There is no way to adjust any of
44 * these because the RTCD tables do not exist: the functions are called
45 * statically */
46 int flags;
47 int mask;
48 if (!arm_cpu_env_flags(&flags)) {
49 return flags;
50 }
51 mask = arm_cpu_env_mask();
52#if HAVE_MEDIA
53 flags |= HAS_MEDIA;
54#endif /* HAVE_MEDIA */
55#if HAVE_NEON || HAVE_NEON_ASM
56 flags |= HAS_NEON;
57#endif /* HAVE_NEON || HAVE_NEON_ASM */
58 return flags & mask;
59}
60
61#elif defined(_MSC_VER) /* end !CONFIG_RUNTIME_CPU_DETECT */
62/*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
63#define WIN32_LEAN_AND_MEAN
64#define WIN32_EXTRA_LEAN
65#include <windows.h>
66
67int arm_cpu_caps(void) {
68 int flags;
69 int mask;
70 if (!arm_cpu_env_flags(&flags)) {
71 return flags;
72 }
73 mask = arm_cpu_env_mask();
74/* MSVC has no inline __asm support for ARM, but it does let you __emit
75 * instructions via their assembled hex code.
76 * All of these instructions should be essentially nops.
77 */
78#if HAVE_MEDIA
79 if (mask & HAS_MEDIA) {
80 __try {
81 /*SHADD8 r3,r3,r3*/
82 __emit(0xE6333F93);
83 flags |= HAS_MEDIA;
84 } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
85 /*Ignore exception.*/
86 }
87 }
88#endif /* HAVE_MEDIA */
89#if HAVE_NEON || HAVE_NEON_ASM
90 if (mask & HAS_NEON) {
91 __try {
92 /*VORR q0,q0,q0*/
93 __emit(0xF2200150);
94 flags |= HAS_NEON;
95 } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
96 /*Ignore exception.*/
97 }
98 }
99#endif /* HAVE_NEON || HAVE_NEON_ASM */
100 return flags & mask;
101}
102
103#elif defined(__ANDROID__) /* end _MSC_VER */
104#include <cpu-features.h>
105
106int arm_cpu_caps(void) {
107 int flags;
108 int mask;
109 uint64_t features;
110 if (!arm_cpu_env_flags(&flags)) {
111 return flags;
112 }
113 mask = arm_cpu_env_mask();
114 features = android_getCpuFeatures();
115
116#if HAVE_MEDIA
117 flags |= HAS_MEDIA;
118#endif /* HAVE_MEDIA */
119#if HAVE_NEON || HAVE_NEON_ASM
120 if (features & ANDROID_CPU_ARM_FEATURE_NEON) flags |= HAS_NEON;
121#endif /* HAVE_NEON || HAVE_NEON_ASM */
122 return flags & mask;
123}
124
125#elif defined(__linux__) /* end __ANDROID__ */
126
127#include <stdio.h>
128
129int arm_cpu_caps(void) {
130 FILE *fin;
131 int flags;
132 int mask;
133 if (!arm_cpu_env_flags(&flags)) {
134 return flags;
135 }
136 mask = arm_cpu_env_mask();
137 /* Reading /proc/self/auxv would be easier, but that doesn't work reliably
138 * on Android.
139 * This also means that detection will fail in Scratchbox.
140 */
141 fin = fopen("/proc/cpuinfo", "r");
142 if (fin != NULL) {
143 /* 512 should be enough for anybody (it's even enough for all the flags
144 * that x86 has accumulated... so far).
145 */
146 char buf[512];
147 while (fgets(buf, 511, fin) != NULL) {
148#if HAVE_NEON || HAVE_NEON_ASM
149 if (memcmp(buf, "Features", 8) == 0) {
150 char *p;
151 p = strstr(buf, " neon");
152 if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
153 flags |= HAS_NEON;
154 }
155 }
156#endif /* HAVE_NEON || HAVE_NEON_ASM */
157#if HAVE_MEDIA
158 if (memcmp(buf, "CPU architecture:", 17) == 0) {
159 int version;
160 version = atoi(buf + 17);
161 if (version >= 6) {
162 flags |= HAS_MEDIA;
163 }
164 }
165#endif /* HAVE_MEDIA */
166 }
167 fclose(fin);
168 }
169 return flags & mask;
170}
171#else /* end __linux__ */
172#error \
173 "--enable-runtime-cpu-detect selected, but no CPU detection method " \
174"available for your platform. Reconfigure with --disable-runtime-cpu-detect."
175#endif