1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include "aom_ports/arm.h"
15 #include "config/aom_config.h"
16
17 #ifdef WINAPI_FAMILY
18 #include <winapifamily.h>
19 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
20 #define getenv(x) NULL
21 #endif
22 #endif
23
arm_cpu_env_flags(int * flags)24 static int arm_cpu_env_flags(int *flags) {
25 char *env;
26 env = getenv("AOM_SIMD_CAPS");
27 if (env && *env) {
28 *flags = (int)strtol(env, NULL, 0);
29 return 0;
30 }
31 *flags = 0;
32 return -1;
33 }
34
arm_cpu_env_mask(void)35 static int arm_cpu_env_mask(void) {
36 char *env;
37 env = getenv("AOM_SIMD_CAPS_MASK");
38 return env && *env ? (int)strtol(env, NULL, 0) : ~0;
39 }
40
41 #if !CONFIG_RUNTIME_CPU_DETECT
42
aom_arm_cpu_caps(void)43 int aom_arm_cpu_caps(void) {
44 /* This function should actually be a no-op. There is no way to adjust any of
45 * these because the RTCD tables do not exist: the functions are called
46 * statically */
47 int flags;
48 int mask;
49 if (!arm_cpu_env_flags(&flags)) {
50 return flags;
51 }
52 mask = arm_cpu_env_mask();
53 #if HAVE_NEON
54 flags |= HAS_NEON;
55 #endif /* HAVE_NEON */
56 return flags & mask;
57 }
58
59 #elif defined(_MSC_VER) /* end !CONFIG_RUNTIME_CPU_DETECT */
60 /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
61 #define WIN32_LEAN_AND_MEAN
62 #define WIN32_EXTRA_LEAN
63 #include <windows.h>
64
aom_arm_cpu_caps(void)65 int aom_arm_cpu_caps(void) {
66 int flags;
67 int mask;
68 if (!arm_cpu_env_flags(&flags)) {
69 return flags;
70 }
71 mask = arm_cpu_env_mask();
72 /* MSVC has no inline __asm support for ARM, but it does let you __emit
73 * instructions via their assembled hex code.
74 * All of these instructions should be essentially nops.
75 */
76 #if HAVE_NEON
77 if (mask & HAS_NEON) {
78 __try {
79 /*VORR q0,q0,q0*/
80 __emit(0xF2200150);
81 flags |= HAS_NEON;
82 } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
83 /*Ignore exception.*/
84 }
85 }
86 #endif /* HAVE_NEON */
87 return flags & mask;
88 }
89
90 #elif defined(__ANDROID__) /* end _MSC_VER */
91 #include <cpu-features.h>
92
aom_arm_cpu_caps(void)93 int aom_arm_cpu_caps(void) {
94 int flags;
95 int mask;
96 uint64_t features;
97 if (!arm_cpu_env_flags(&flags)) {
98 return flags;
99 }
100 mask = arm_cpu_env_mask();
101 features = android_getCpuFeatures();
102
103 #if HAVE_NEON
104 if (features & ANDROID_CPU_ARM_FEATURE_NEON) flags |= HAS_NEON;
105 #endif /* HAVE_NEON */
106 return flags & mask;
107 }
108
109 #elif defined(__linux__) /* end __ANDROID__ */
110
111 #include <stdio.h>
112
aom_arm_cpu_caps(void)113 int aom_arm_cpu_caps(void) {
114 FILE *fin;
115 int flags;
116 int mask;
117 if (!arm_cpu_env_flags(&flags)) {
118 return flags;
119 }
120 mask = arm_cpu_env_mask();
121 /* Reading /proc/self/auxv would be easier, but that doesn't work reliably
122 * on Android.
123 * This also means that detection will fail in Scratchbox.
124 */
125 fin = fopen("/proc/cpuinfo", "r");
126 if (fin != NULL) {
127 /* 512 should be enough for anybody (it's even enough for all the flags
128 * that x86 has accumulated... so far).
129 */
130 char buf[512];
131 while (fgets(buf, 511, fin) != NULL) {
132 #if HAVE_NEON
133 if (memcmp(buf, "Features", 8) == 0) {
134 char *p;
135 p = strstr(buf, " neon");
136 if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
137 flags |= HAS_NEON;
138 }
139 }
140 #endif /* HAVE_NEON */
141 }
142 fclose(fin);
143 }
144 return flags & mask;
145 }
146 #else /* end __linux__ */
147 #error \
148 "--enable-runtime-cpu-detect selected, but no CPU detection method " \
149 "available for your platform. Reconfigure with --disable-runtime-cpu-detect."
150 #endif
151