Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include "aom_ports/arm.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 15 | #include "./aom_config.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 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 | |
| 24 | static int arm_cpu_env_flags(int *flags) { |
| 25 | char *env; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 26 | env = getenv("AOM_SIMD_CAPS"); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 27 | if (env && *env) { |
| 28 | *flags = (int)strtol(env, NULL, 0); |
| 29 | return 0; |
| 30 | } |
| 31 | *flags = 0; |
| 32 | return -1; |
| 33 | } |
| 34 | |
| 35 | static int arm_cpu_env_mask(void) { |
| 36 | char *env; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 37 | env = getenv("AOM_SIMD_CAPS_MASK"); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 38 | return env && *env ? (int)strtol(env, NULL, 0) : ~0; |
| 39 | } |
| 40 | |
| 41 | #if !CONFIG_RUNTIME_CPU_DETECT |
| 42 | |
| 43 | int 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_MEDIA |
| 54 | flags |= HAS_MEDIA; |
| 55 | #endif /* HAVE_MEDIA */ |
| 56 | #if HAVE_NEON || HAVE_NEON_ASM |
| 57 | flags |= HAS_NEON; |
| 58 | #endif /* HAVE_NEON || HAVE_NEON_ASM */ |
| 59 | return flags & mask; |
| 60 | } |
| 61 | |
| 62 | #elif defined(_MSC_VER) /* end !CONFIG_RUNTIME_CPU_DETECT */ |
| 63 | /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/ |
| 64 | #define WIN32_LEAN_AND_MEAN |
| 65 | #define WIN32_EXTRA_LEAN |
| 66 | #include <windows.h> |
| 67 | |
| 68 | int arm_cpu_caps(void) { |
| 69 | int flags; |
| 70 | int mask; |
| 71 | if (!arm_cpu_env_flags(&flags)) { |
| 72 | return flags; |
| 73 | } |
| 74 | mask = arm_cpu_env_mask(); |
| 75 | /* MSVC has no inline __asm support for ARM, but it does let you __emit |
| 76 | * instructions via their assembled hex code. |
| 77 | * All of these instructions should be essentially nops. |
| 78 | */ |
| 79 | #if HAVE_MEDIA |
| 80 | if (mask & HAS_MEDIA) { |
| 81 | __try { |
| 82 | /*SHADD8 r3,r3,r3*/ |
| 83 | __emit(0xE6333F93); |
| 84 | flags |= HAS_MEDIA; |
| 85 | } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) { |
| 86 | /*Ignore exception.*/ |
| 87 | } |
| 88 | } |
| 89 | #endif /* HAVE_MEDIA */ |
| 90 | #if HAVE_NEON || HAVE_NEON_ASM |
| 91 | if (mask & HAS_NEON) { |
| 92 | __try { |
| 93 | /*VORR q0,q0,q0*/ |
| 94 | __emit(0xF2200150); |
| 95 | flags |= HAS_NEON; |
| 96 | } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) { |
| 97 | /*Ignore exception.*/ |
| 98 | } |
| 99 | } |
| 100 | #endif /* HAVE_NEON || HAVE_NEON_ASM */ |
| 101 | return flags & mask; |
| 102 | } |
| 103 | |
| 104 | #elif defined(__ANDROID__) /* end _MSC_VER */ |
| 105 | #include <cpu-features.h> |
| 106 | |
| 107 | int arm_cpu_caps(void) { |
| 108 | int flags; |
| 109 | int mask; |
| 110 | uint64_t features; |
| 111 | if (!arm_cpu_env_flags(&flags)) { |
| 112 | return flags; |
| 113 | } |
| 114 | mask = arm_cpu_env_mask(); |
| 115 | features = android_getCpuFeatures(); |
| 116 | |
| 117 | #if HAVE_MEDIA |
| 118 | flags |= HAS_MEDIA; |
| 119 | #endif /* HAVE_MEDIA */ |
| 120 | #if HAVE_NEON || HAVE_NEON_ASM |
| 121 | if (features & ANDROID_CPU_ARM_FEATURE_NEON) flags |= HAS_NEON; |
| 122 | #endif /* HAVE_NEON || HAVE_NEON_ASM */ |
| 123 | return flags & mask; |
| 124 | } |
| 125 | |
| 126 | #elif defined(__linux__) /* end __ANDROID__ */ |
| 127 | |
| 128 | #include <stdio.h> |
| 129 | |
| 130 | int arm_cpu_caps(void) { |
| 131 | FILE *fin; |
| 132 | int flags; |
| 133 | int mask; |
| 134 | if (!arm_cpu_env_flags(&flags)) { |
| 135 | return flags; |
| 136 | } |
| 137 | mask = arm_cpu_env_mask(); |
| 138 | /* Reading /proc/self/auxv would be easier, but that doesn't work reliably |
| 139 | * on Android. |
| 140 | * This also means that detection will fail in Scratchbox. |
| 141 | */ |
| 142 | fin = fopen("/proc/cpuinfo", "r"); |
| 143 | if (fin != NULL) { |
| 144 | /* 512 should be enough for anybody (it's even enough for all the flags |
| 145 | * that x86 has accumulated... so far). |
| 146 | */ |
| 147 | char buf[512]; |
| 148 | while (fgets(buf, 511, fin) != NULL) { |
| 149 | #if HAVE_NEON || HAVE_NEON_ASM |
| 150 | if (memcmp(buf, "Features", 8) == 0) { |
| 151 | char *p; |
| 152 | p = strstr(buf, " neon"); |
| 153 | if (p != NULL && (p[5] == ' ' || p[5] == '\n')) { |
| 154 | flags |= HAS_NEON; |
| 155 | } |
| 156 | } |
| 157 | #endif /* HAVE_NEON || HAVE_NEON_ASM */ |
| 158 | #if HAVE_MEDIA |
| 159 | if (memcmp(buf, "CPU architecture:", 17) == 0) { |
| 160 | int version; |
| 161 | version = atoi(buf + 17); |
| 162 | if (version >= 6) { |
| 163 | flags |= HAS_MEDIA; |
| 164 | } |
| 165 | } |
| 166 | #endif /* HAVE_MEDIA */ |
| 167 | } |
| 168 | fclose(fin); |
| 169 | } |
| 170 | return flags & mask; |
| 171 | } |
| 172 | #else /* end __linux__ */ |
| 173 | #error \ |
| 174 | "--enable-runtime-cpu-detect selected, but no CPU detection method " \ |
| 175 | "available for your platform. Reconfigure with --disable-runtime-cpu-detect." |
| 176 | #endif |