arm_cpudetect.c revision 1b362b15af34006e6a11974088a46d42b903418e
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 15static int arm_cpu_env_flags(int *flags) 16{ 17 char *env; 18 env = getenv("VPX_SIMD_CAPS"); 19 if (env && *env) 20 { 21 *flags = (int)strtol(env, NULL, 0); 22 return 0; 23 } 24 *flags = 0; 25 return -1; 26} 27 28static int arm_cpu_env_mask(void) 29{ 30 char *env; 31 env = getenv("VPX_SIMD_CAPS_MASK"); 32 return env && *env ? (int)strtol(env, NULL, 0) : ~0; 33} 34 35#if !CONFIG_RUNTIME_CPU_DETECT 36 37int arm_cpu_caps(void) 38{ 39 /* This function should actually be a no-op. There is no way to adjust any of 40 * these because the RTCD tables do not exist: the functions are called 41 * statically */ 42 int flags; 43 int mask; 44 if (!arm_cpu_env_flags(&flags)) 45 { 46 return flags; 47 } 48 mask = arm_cpu_env_mask(); 49#if HAVE_EDSP 50 flags |= HAS_EDSP; 51#endif /* HAVE_EDSP */ 52#if HAVE_MEDIA 53 flags |= HAS_MEDIA; 54#endif /* HAVE_MEDIA */ 55#if HAVE_NEON 56 flags |= HAS_NEON; 57#endif /* HAVE_NEON */ 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{ 69 int flags; 70 int mask; 71 if (!arm_cpu_env_flags(&flags)) 72 { 73 return flags; 74 } 75 mask = arm_cpu_env_mask(); 76 /* MSVC has no inline __asm support for ARM, but it does let you __emit 77 * instructions via their assembled hex code. 78 * All of these instructions should be essentially nops. 79 */ 80#if HAVE_EDSP 81 if (mask & HAS_EDSP) 82 { 83 __try 84 { 85 /*PLD [r13]*/ 86 __emit(0xF5DDF000); 87 flags |= HAS_EDSP; 88 } 89 __except(GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) 90 { 91 /*Ignore exception.*/ 92 } 93 } 94#if HAVE_MEDIA 95 if (mask & HAS_MEDIA) 96 __try 97 { 98 /*SHADD8 r3,r3,r3*/ 99 __emit(0xE6333F93); 100 flags |= HAS_MEDIA; 101 } 102 __except(GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) 103 { 104 /*Ignore exception.*/ 105 } 106 } 107#if HAVE_NEON 108 if (mask & HAS_NEON) 109 { 110 __try 111 { 112 /*VORR q0,q0,q0*/ 113 __emit(0xF2200150); 114 flags |= HAS_NEON; 115 } 116 __except(GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) 117 { 118 /*Ignore exception.*/ 119 } 120 } 121#endif /* HAVE_NEON */ 122#endif /* HAVE_MEDIA */ 123#endif /* HAVE_EDSP */ 124 return flags & mask; 125} 126 127#elif defined(__ANDROID__) /* end _MSC_VER */ 128#include <cpu-features.h> 129 130int arm_cpu_caps(void) 131{ 132 int flags; 133 int mask; 134 uint64_t features; 135 if (!arm_cpu_env_flags(&flags)) 136 { 137 return flags; 138 } 139 mask = arm_cpu_env_mask(); 140 features = android_getCpuFeatures(); 141 142#if HAVE_EDSP 143 flags |= HAS_EDSP; 144#endif /* HAVE_EDSP */ 145#if HAVE_MEDIA 146 flags |= HAS_MEDIA; 147#endif /* HAVE_MEDIA */ 148#if HAVE_NEON 149 if (features & ANDROID_CPU_ARM_FEATURE_NEON) 150 flags |= HAS_NEON; 151#endif /* HAVE_NEON */ 152 return flags & mask; 153} 154 155#elif defined(__linux__) /* end __ANDROID__ */ 156#include <stdio.h> 157 158int arm_cpu_caps(void) 159{ 160 FILE *fin; 161 int flags; 162 int mask; 163 if (!arm_cpu_env_flags(&flags)) 164 { 165 return flags; 166 } 167 mask = arm_cpu_env_mask(); 168 /* Reading /proc/self/auxv would be easier, but that doesn't work reliably 169 * on Android. 170 * This also means that detection will fail in Scratchbox. 171 */ 172 fin = fopen("/proc/cpuinfo","r"); 173 if(fin != NULL) 174 { 175 /* 512 should be enough for anybody (it's even enough for all the flags 176 * that x86 has accumulated... so far). 177 */ 178 char buf[512]; 179 while (fgets(buf, 511, fin) != NULL) 180 { 181#if HAVE_EDSP || HAVE_NEON 182 if (memcmp(buf, "Features", 8) == 0) 183 { 184 char *p; 185#if HAVE_EDSP 186 p=strstr(buf, " edsp"); 187 if (p != NULL && (p[5] == ' ' || p[5] == '\n')) 188 { 189 flags |= HAS_EDSP; 190 } 191#if HAVE_NEON 192 p = strstr(buf, " neon"); 193 if (p != NULL && (p[5] == ' ' || p[5] == '\n')) 194 { 195 flags |= HAS_NEON; 196 } 197#endif /* HAVE_NEON */ 198#endif /* HAVE_EDSP */ 199 } 200#endif /* HAVE_EDSP || HAVE_NEON */ 201#if HAVE_MEDIA 202 if (memcmp(buf, "CPU architecture:",17) == 0){ 203 int version; 204 version = atoi(buf+17); 205 if (version >= 6) 206 { 207 flags |= HAS_MEDIA; 208 } 209 } 210#endif /* HAVE_MEDIA */ 211 } 212 fclose(fin); 213 } 214 return flags & mask; 215} 216#else /* end __linux__ */ 217#error "--enable-runtime-cpu-detect selected, but no CPU detection method " \ 218 "available for your platform. Reconfigure with --disable-runtime-cpu-detect." 219#endif 220