Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 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 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 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; |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 24 | } |
| 25 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 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; |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 30 | } |
| 31 | |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 32 | #if !CONFIG_RUNTIME_CPU_DETECT |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 33 | |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 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 */ |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 57 | /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/ |
| 58 | #define WIN32_LEAN_AND_MEAN |
| 59 | #define WIN32_EXTRA_LEAN |
| 60 | #include <windows.h> |
| 61 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 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 | */ |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 73 | #if HAVE_EDSP |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 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.*/ |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 81 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 82 | } |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 83 | #if HAVE_MEDIA |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 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 | } |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 93 | #if HAVE_NEON |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 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 | } |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 103 | #endif /* HAVE_NEON */ |
| 104 | #endif /* HAVE_MEDIA */ |
| 105 | #endif /* HAVE_EDSP */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 106 | return flags & mask; |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 107 | } |
| 108 | |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 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__ */ |
John Koleszar | 7b8dfcb | 2012-11-06 16:59:01 -0800 | [diff] [blame] | 136 | |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 137 | #include <stdio.h> |
| 138 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 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). |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 155 | */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 156 | char buf[512]; |
| 157 | while (fgets(buf, 511, fin) != NULL) { |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 158 | #if HAVE_EDSP || HAVE_NEON |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 159 | if (memcmp(buf, "Features", 8) == 0) { |
| 160 | char *p; |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 161 | #if HAVE_EDSP |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 162 | p = strstr(buf, " edsp"); |
| 163 | if (p != NULL && (p[5] == ' ' || p[5] == '\n')) { |
| 164 | flags |= HAS_EDSP; |
| 165 | } |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 166 | #if HAVE_NEON |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 167 | p = strstr(buf, " neon"); |
| 168 | if (p != NULL && (p[5] == ' ' || p[5] == '\n')) { |
| 169 | flags |= HAS_NEON; |
| 170 | } |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 171 | #endif /* HAVE_NEON */ |
| 172 | #endif /* HAVE_EDSP */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 173 | } |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 174 | #endif /* HAVE_EDSP || HAVE_NEON */ |
| 175 | #if HAVE_MEDIA |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 176 | 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. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 181 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 182 | } |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 183 | #endif /* HAVE_MEDIA */ |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 184 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 185 | fclose(fin); |
| 186 | } |
| 187 | return flags & mask; |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 188 | } |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 189 | #else /* end __linux__ */ |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 190 | #error "--enable-runtime-cpu-detect selected, but no CPU detection method " \ |
John Koleszar | 807acf1 | 2012-11-02 15:39:14 -0700 | [diff] [blame] | 191 | "available for your platform. Reconfigure with --disable-runtime-cpu-detect." |
Timothy B. Terriberry | b71962f | 2010-10-20 15:39:11 -0700 | [diff] [blame] | 192 | #endif |