blob: 542ff6786d3c03b1949987e1d3c463080cb55446 [file] [log] [blame]
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -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 "arm.h"
14
John Koleszarc6b90392012-07-13 15:21:29 -070015static int arm_cpu_env_flags(int *flags) {
16 char *env;
17 env = getenv("VPX_SIMD_CAPS");
18 if (env && *env) {
19 *flags = (int)strtol(env, NULL, 0);
20 return 0;
21 }
22 *flags = 0;
23 return -1;
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -070024}
25
John Koleszarc6b90392012-07-13 15:21:29 -070026static int arm_cpu_env_mask(void) {
27 char *env;
28 env = getenv("VPX_SIMD_CAPS_MASK");
29 return env && *env ? (int)strtol(env, NULL, 0) : ~0;
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -070030}
31
John Koleszar807acf12012-11-02 15:39:14 -070032#if !CONFIG_RUNTIME_CPU_DETECT
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -070033
John Koleszar807acf12012-11-02 15:39:14 -070034int arm_cpu_caps(void) {
35 /* This function should actually be a no-op. There is no way to adjust any of
36 * these because the RTCD tables do not exist: the functions are called
37 * statically */
38 int flags;
39 int mask;
40 if (!arm_cpu_env_flags(&flags)) {
41 return flags;
42 }
43 mask = arm_cpu_env_mask();
44#if HAVE_EDSP
45 flags |= HAS_EDSP;
46#endif /* HAVE_EDSP */
47#if HAVE_MEDIA
48 flags |= HAS_MEDIA;
49#endif /* HAVE_MEDIA */
50#if HAVE_NEON
51 flags |= HAS_NEON;
52#endif /* HAVE_NEON */
53 return flags & mask;
54}
55
56#elif defined(_MSC_VER) /* end !CONFIG_RUNTIME_CPU_DETECT */
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -070057/*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
58#define WIN32_LEAN_AND_MEAN
59#define WIN32_EXTRA_LEAN
60#include <windows.h>
61
John Koleszarc6b90392012-07-13 15:21:29 -070062int arm_cpu_caps(void) {
63 int flags;
64 int mask;
65 if (!arm_cpu_env_flags(&flags)) {
66 return flags;
67 }
68 mask = arm_cpu_env_mask();
69 /* MSVC has no inline __asm support for ARM, but it does let you __emit
70 * instructions via their assembled hex code.
71 * All of these instructions should be essentially nops.
72 */
John Koleszar807acf12012-11-02 15:39:14 -070073#if HAVE_EDSP
John Koleszarc6b90392012-07-13 15:21:29 -070074 if (mask & HAS_EDSP) {
75 __try {
76 /*PLD [r13]*/
77 __emit(0xF5DDF000);
78 flags |= HAS_EDSP;
79 } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
80 /*Ignore exception.*/
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -070081 }
John Koleszarc6b90392012-07-13 15:21:29 -070082 }
John Koleszar807acf12012-11-02 15:39:14 -070083#if HAVE_MEDIA
John Koleszarc6b90392012-07-13 15:21:29 -070084 if (mask & HAS_MEDIA)
85 __try {
86 /*SHADD8 r3,r3,r3*/
87 __emit(0xE6333F93);
88 flags |= HAS_MEDIA;
89 } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
90 /*Ignore exception.*/
91 }
92}
John Koleszar807acf12012-11-02 15:39:14 -070093#if HAVE_NEON
John Koleszarc6b90392012-07-13 15:21:29 -070094if (mask &HAS_NEON) {
95 __try {
96 /*VORR q0,q0,q0*/
97 __emit(0xF2200150);
98 flags |= HAS_NEON;
99 } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
100 /*Ignore exception.*/
101 }
102}
John Koleszar807acf12012-11-02 15:39:14 -0700103#endif /* HAVE_NEON */
104#endif /* HAVE_MEDIA */
105#endif /* HAVE_EDSP */
John Koleszarc6b90392012-07-13 15:21:29 -0700106return flags & mask;
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -0700107}
108
John Koleszar807acf12012-11-02 15:39:14 -0700109#elif defined(__ANDROID__) /* end _MSC_VER */
110#include <cpu-features.h>
111
112int arm_cpu_caps(void) {
113 int flags;
114 int mask;
115 uint64_t features;
116 if (!arm_cpu_env_flags(&flags)) {
117 return flags;
118 }
119 mask = arm_cpu_env_mask();
120 features = android_getCpuFeatures();
121
122#if HAVE_EDSP
123 flags |= HAS_EDSP;
124#endif /* HAVE_EDSP */
125#if HAVE_MEDIA
126 flags |= HAS_MEDIA;
127#endif /* HAVE_MEDIA */
128#if HAVE_NEON
129 if (features & ANDROID_CPU_ARM_FEATURE_NEON)
130 flags |= HAS_NEON;
131#endif /* HAVE_NEON */
132 return flags & mask;
133}
134
135#elif defined(__linux__) /* end __ANDROID__ */
John Koleszar7b8dfcb2012-11-06 16:59:01 -0800136
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -0700137#include <stdio.h>
138
John Koleszarc6b90392012-07-13 15:21:29 -0700139int arm_cpu_caps(void) {
140 FILE *fin;
141 int flags;
142 int mask;
143 if (!arm_cpu_env_flags(&flags)) {
144 return flags;
145 }
146 mask = arm_cpu_env_mask();
147 /* Reading /proc/self/auxv would be easier, but that doesn't work reliably
148 * on Android.
149 * This also means that detection will fail in Scratchbox.
150 */
151 fin = fopen("/proc/cpuinfo", "r");
152 if (fin != NULL) {
153 /* 512 should be enough for anybody (it's even enough for all the flags
154 * that x86 has accumulated... so far).
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -0700155 */
John Koleszarc6b90392012-07-13 15:21:29 -0700156 char buf[512];
157 while (fgets(buf, 511, fin) != NULL) {
John Koleszar807acf12012-11-02 15:39:14 -0700158#if HAVE_EDSP || HAVE_NEON
John Koleszarc6b90392012-07-13 15:21:29 -0700159 if (memcmp(buf, "Features", 8) == 0) {
160 char *p;
John Koleszar807acf12012-11-02 15:39:14 -0700161#if HAVE_EDSP
John Koleszarc6b90392012-07-13 15:21:29 -0700162 p = strstr(buf, " edsp");
163 if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
164 flags |= HAS_EDSP;
165 }
John Koleszar807acf12012-11-02 15:39:14 -0700166#if HAVE_NEON
John Koleszarc6b90392012-07-13 15:21:29 -0700167 p = strstr(buf, " neon");
168 if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
169 flags |= HAS_NEON;
170 }
John Koleszar807acf12012-11-02 15:39:14 -0700171#endif /* HAVE_NEON */
172#endif /* HAVE_EDSP */
John Koleszarc6b90392012-07-13 15:21:29 -0700173 }
John Koleszar807acf12012-11-02 15:39:14 -0700174#endif /* HAVE_EDSP || HAVE_NEON */
175#if HAVE_MEDIA
John Koleszarc6b90392012-07-13 15:21:29 -0700176 if (memcmp(buf, "CPU architecture:", 17) == 0) {
177 int version;
178 version = atoi(buf + 17);
179 if (version >= 6) {
180 flags |= HAS_MEDIA;
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -0700181 }
John Koleszarc6b90392012-07-13 15:21:29 -0700182 }
John Koleszar807acf12012-11-02 15:39:14 -0700183#endif /* HAVE_MEDIA */
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -0700184 }
John Koleszarc6b90392012-07-13 15:21:29 -0700185 fclose(fin);
186 }
187 return flags & mask;
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -0700188}
John Koleszar807acf12012-11-02 15:39:14 -0700189#else /* end __linux__ */
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -0700190#error "--enable-runtime-cpu-detect selected, but no CPU detection method " \
John Koleszar807acf12012-11-02 15:39:14 -0700191"available for your platform. Reconfigure with --disable-runtime-cpu-detect."
Timothy B. Terriberryb71962f2010-10-20 15:39:11 -0700192#endif