1 /* 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 15 static 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; 24 } 25 26 static 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; 30 } 31 32 #if !CONFIG_RUNTIME_CPU_DETECT 33 34 int 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 */ 57 /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/ 58 #define WIN32_LEAN_AND_MEAN 59 #define WIN32_EXTRA_LEAN 60 #include <windows.h> 61 62 int 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 */ 73 #if HAVE_EDSP 74 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.*/ 81 } 82 } 83 #if HAVE_MEDIA 84 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 } 93 #if HAVE_NEON 94 if (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 } 103 #endif /* HAVE_NEON */ 104 #endif /* HAVE_MEDIA */ 105 #endif /* HAVE_EDSP */ 106 return flags & mask; 107 } 108 109 #elif defined(__ANDROID__) /* end _MSC_VER */ 110 #include <cpu-features.h> 111 112 int 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__ */ 136 137 #include <stdio.h> 138 139 int 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). 155 */ 156 char buf[512]; 157 while (fgets(buf, 511, fin) != NULL) { 158 #if HAVE_EDSP || HAVE_NEON 159 if (memcmp(buf, "Features", 8) == 0) { 160 char *p; 161 #if HAVE_EDSP 162 p = strstr(buf, " edsp"); 163 if (p != NULL && (p[5] == ' ' || p[5] == '\n')) { 164 flags |= HAS_EDSP; 165 } 166 #if HAVE_NEON 167 p = strstr(buf, " neon"); 168 if (p != NULL && (p[5] == ' ' || p[5] == '\n')) { 169 flags |= HAS_NEON; 170 } 171 #endif /* HAVE_NEON */ 172 #endif /* HAVE_EDSP */ 173 } 174 #endif /* HAVE_EDSP || HAVE_NEON */ 175 #if HAVE_MEDIA 176 if (memcmp(buf, "CPU architecture:", 17) == 0) { 177 int version; 178 version = atoi(buf + 17); 179 if (version >= 6) { 180 flags |= HAS_MEDIA; 181 } 182 } 183 #endif /* HAVE_MEDIA */ 184 } 185 fclose(fin); 186 } 187 return flags & mask; 188 } 189 #else /* end __linux__ */ 190 #error "--enable-runtime-cpu-detect selected, but no CPU detection method " \ 191 "available for your platform. Reconfigure with --disable-runtime-cpu-detect." 192 #endif 193