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(); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 53 | #if HAVE_NEON || HAVE_NEON_ASM |
| 54 | flags |= HAS_NEON; |
| 55 | #endif /* HAVE_NEON || HAVE_NEON_ASM */ |
| 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 | |
| 65 | int 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 | */ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 76 | #if HAVE_NEON || HAVE_NEON_ASM |
| 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 || HAVE_NEON_ASM */ |
| 87 | return flags & mask; |
| 88 | } |
| 89 | |
| 90 | #elif defined(__ANDROID__) /* end _MSC_VER */ |
| 91 | #include <cpu-features.h> |
| 92 | |
| 93 | int 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 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 103 | #if HAVE_NEON || HAVE_NEON_ASM |
| 104 | if (features & ANDROID_CPU_ARM_FEATURE_NEON) flags |= HAS_NEON; |
| 105 | #endif /* HAVE_NEON || HAVE_NEON_ASM */ |
| 106 | return flags & mask; |
| 107 | } |
| 108 | |
| 109 | #elif defined(__linux__) /* end __ANDROID__ */ |
| 110 | |
| 111 | #include <stdio.h> |
| 112 | |
| 113 | int 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 || HAVE_NEON_ASM |
| 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 || HAVE_NEON_ASM */ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 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 |